diff --git a/spec/functions/archive_go_md5_spec.rb b/spec/functions/archive_go_md5_spec.rb index d880bdb..8081de0 100644 --- a/spec/functions/archive_go_md5_spec.rb +++ b/spec/functions/archive_go_md5_spec.rb @@ -1,21 +1,21 @@ require 'spec_helper' describe 'archive::go_md5' do let(:example_md5) { File.read(fixtures('checksum', 'gocd.md5')) } let(:url) { 'https://gocd.lan/path/file.md5' } let(:uri) { URI(url) } it { is_expected.not_to eq(nil) } it 'retreives file md5' do - PuppetX::Bodeco::Util.stubs(:content).with(uri, username: 'user', password: 'pass').returns(example_md5) + allow(PuppetX::Bodeco::Util).to receive(:content).with(uri, username: 'user', password: 'pass').and_return(example_md5) is_expected.to run.with_params('user', 'pass', 'filea', url).and_return('283158c7da8c0ada74502794fa8745eb') end context 'when file doesn\'t exist' do it 'raises error' do - PuppetX::Bodeco::Util.stubs(:content).with(uri, username: 'user', password: 'pass').returns(example_md5) + allow(PuppetX::Bodeco::Util).to receive(:content).with(uri, username: 'user', password: 'pass').and_return(example_md5) is_expected.to run.with_params('user', 'pass', 'non-existent-file', url).and_raise_error(RuntimeError, "Could not parse md5 from url https://gocd\.lan/path/file\.md5 response: #{example_md5}") end end end diff --git a/spec/functions/artifactory_checksum_spec.rb b/spec/functions/artifactory_checksum_spec.rb index e6ad09a..e8ea2b1 100644 --- a/spec/functions/artifactory_checksum_spec.rb +++ b/spec/functions/artifactory_checksum_spec.rb @@ -1,21 +1,20 @@ require 'spec_helper' describe 'archive::artifactory_checksum' do + let(:example_json) { File.read(fixtures('checksum', 'artifactory.json')) } + let(:url) { 'https://repo.jfrog.org/artifactory/distributions/images/Artifactory_120x75.png' } + let(:uri) { URI(url.sub('/artifactory/', '/artifactory/api/storage/')) } + it { is_expected.not_to eq(nil) } it { is_expected.to run.with_params.and_raise_error(ArgumentError) } it { is_expected.to run.with_params('not_a_url').and_raise_error(ArgumentError) } - - example_json = File.read(fixtures('checksum', 'artifactory.json')) - url = 'https://repo.jfrog.org/artifactory/distributions/images/Artifactory_120x75.png' - uri = URI(url.sub('/artifactory/', '/artifactory/api/storage/')) - it 'defaults to and parses sha1' do - PuppetX::Bodeco::Util.stubs(:content).with(uri).returns(example_json) + allow(PuppetX::Bodeco::Util).to receive(:content).with(uri).and_return(example_json) is_expected.to run.with_params(url).and_return('a359e93636e81f9dd844b2dfb4b89fa876e5d4fa') end it 'parses md5' do - PuppetX::Bodeco::Util.stubs(:content).with(uri).returns(example_json) + allow(PuppetX::Bodeco::Util).to receive(:content).with(uri).and_return(example_json) is_expected.to run.with_params(url, 'md5').and_return('00f32568be85929fe95be38f9f5f3519') end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e7cb1d5..60c7ce7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,20 +1,21 @@ +RSpec.configure do |c| + c.formatter = 'documentation' + c.mock_framework = :rspec +end + require 'puppetlabs_spec_helper/module_spec_helper' + require 'rspec-puppet-utils' require 'rspec/mocks' require 'rspec-puppet-facts' include RspecPuppetFacts unless RUBY_VERSION =~ %r{^1.9} require 'coveralls' Coveralls.wear! end # # Require all support files # Dir['./spec/support/**/*.rb'].sort.each { |f| require f } - -RSpec.configure do |c| - c.formatter = 'documentation' - c.mock_framework = :rspec -end diff --git a/spec/support/shared_contexts.rb b/spec/support/shared_contexts.rb index 7147b9f..c5141dd 100644 --- a/spec/support/shared_contexts.rb +++ b/spec/support/shared_contexts.rb @@ -1,6 +1,24 @@ +require 'tmpdir' +require 'pathname' + shared_context :some_context do # example only, let(:hiera_data) do {} end end + +shared_context 'uses temp dir' do + around do |example| + Dir.mktmpdir('rspec-') do |dir| + @temp_dir = dir + example.run + end + end + + attr_reader :temp_dir + + def temp_dir_path + Pathname(temp_dir) + end +end diff --git a/spec/unit/facter/archive_windir_spec.rb b/spec/unit/facter/archive_windir_spec.rb index 36725d2..9c5c5d5 100644 --- a/spec/unit/facter/archive_windir_spec.rb +++ b/spec/unit/facter/archive_windir_spec.rb @@ -1,25 +1,26 @@ require 'spec_helper' require 'facter/archive_windir' describe 'archive_windir fact specs', type: :fact do before { Facter.clear } after { Facter.clear } + subject { Facter.fact(:archive_windir).value } context 'RedHat' do before do - Facter.fact(:osfamily).stubs(:value).returns 'RedHat' + allow(Facter.fact(:osfamily)).to receive(:value).and_return('RedHat') end it 'is nil on RedHat' do - expect(Facter.fact(:archive_windir).value).to be_nil + is_expected.to be_nil end end context 'Windows' do before do - Facter.fact(:osfamily).stubs(:value).returns 'windows' + allow(Facter.fact(:osfamily)).to receive(:value).and_return('windows') end it 'defaults to C:\\staging on windows' do - expect(Facter.fact(:archive_windir).value).to eq('C:\\staging') + is_expected.to eq('C:\\staging') end end end diff --git a/spec/unit/puppet_x/bodeco/archive_spec.rb b/spec/unit/puppet_x/bodeco/archive_spec.rb index 8d96e1b..f4efd0f 100644 --- a/spec/unit/puppet_x/bodeco/archive_spec.rb +++ b/spec/unit/puppet_x/bodeco/archive_spec.rb @@ -1,101 +1,152 @@ -# rubocop:disable RSpec/MultipleExpectations require 'spec_helper' require 'puppet_x/bodeco/archive' describe PuppetX::Bodeco::Archive do let(:zipfile) do - File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'files', 'test.zip')) + File.expand_path(File.join(__dir__, '..', '..', '..', '..', 'files', 'test.zip')) end - it '#checksum' do - Dir.mktmpdir do |dir| - tempfile = File.join(dir, 'test.zip') - FileUtils.cp(zipfile, tempfile) + describe '#checksum' do + include_context 'uses temp dir' - archive = described_class.new(tempfile) - expect(archive.checksum(:none)).to be nil - expect(archive.checksum(:md5)).to eq '557e2ebb67b35d1fddff18090b6bc26b' - expect(archive.checksum(:sha1)).to eq '377ec712d7fdb7266221db3441e3af2055448ead' - end - end + subject { described_class.new(tempfile) } + + let(:tempfile) { File.join(temp_dir, 'test.zip') } - it '#parse_flags' do - archive = described_class.new('test.tar.gz') - expect(archive.send(:parse_flags, 'xf', :undef, 'tar')).to eq 'xf' - expect(archive.send(:parse_flags, 'xf', 'xvf', 'tar')).to eq 'xvf' - expect(archive.send(:parse_flags, 'xf', { 'tar' => 'xzf', '7z' => '-y x' }, 'tar')).to eq 'xzf' + before { FileUtils.cp(zipfile, tempfile) } + it { expect(subject.checksum(:none)).to be nil } + it { expect(subject.checksum(:md5)).to eq '557e2ebb67b35d1fddff18090b6bc26b' } + it { expect(subject.checksum(:sha1)).to eq '377ec712d7fdb7266221db3441e3af2055448ead' } end - it '#command on RedHat' do - Facter.stubs(:value).with(:osfamily).returns 'RedHat' - - tar = described_class.new('test.tar.gz') - expect(tar.send(:command, :undef)).to eq 'tar xzf test.tar.gz' - expect(tar.send(:command, 'xvf')).to eq 'tar xvf test.tar.gz' - tar = described_class.new('test.tar.bz2') - expect(tar.send(:command, :undef)).to eq 'tar xjf test.tar.bz2' - expect(tar.send(:command, 'xjf')).to eq 'tar xjf test.tar.bz2' - tar = described_class.new('test.tar.xz') - expect(tar.send(:command, :undef)).to eq 'unxz -dc test.tar.xz | tar xf -' - gunzip = described_class.new('test.gz') - expect(gunzip.send(:command, :undef)).to eq 'gunzip -d test.gz' - bunzip2 = described_class.new('test.bz2') - expect(bunzip2.send(:command, :undef)).to eq 'bunzip2 -d test.bz2' - zip = described_class.new('test.zip') - expect(zip.send(:command, :undef)).to eq 'unzip -o test.zip' - expect(zip.send(:command, '-a')).to eq 'unzip -a test.zip' - - zip = described_class.new('/tmp/fun folder/test.zip') - expect(zip.send(:command, :undef)).to eq 'unzip -o /tmp/fun\ folder/test.zip' - expect(zip.send(:command, '-a')).to eq 'unzip -a /tmp/fun\ folder/test.zip' + describe '#parse_flags' do + subject { described_class.new('test.tar.gz') } + + it { expect(subject.send(:parse_flags, 'xf', :undef, 'tar')).to eq 'xf' } + it { expect(subject.send(:parse_flags, 'xf', 'xvf', 'tar')).to eq 'xvf' } + it { expect(subject.send(:parse_flags, 'xf', { 'tar' => 'xzf', '7z' => '-y x' }, 'tar')).to eq 'xzf' } end - system_v = %w[Solaris AIX] - system_v.each do |os| - it "#command on #{os}" do - Facter.stubs(:value).with(:osfamily).returns os - - tar = described_class.new('test.tar.gz') - expect(tar.send(:command, :undef)).to eq 'gunzip -dc test.tar.gz | tar xf -' - expect(tar.send(:command, 'gunzip' => '-dc', 'tar' => 'xvf')).to eq 'gunzip -dc test.tar.gz | tar xvf -' - tar = described_class.new('test.tar.bz2') - expect(tar.send(:command, :undef)).to eq 'bunzip2 -dc test.tar.bz2 | tar xf -' - expect(tar.send(:command, 'bunzip' => '-dc', 'tar' => 'xvf')).to eq 'bunzip2 -dc test.tar.bz2 | tar xvf -' - tar = described_class.new('test.tar.xz') - expect(tar.send(:command, :undef)).to eq 'unxz -dc test.tar.xz | tar xf -' - gunzip = described_class.new('test.gz') - expect(gunzip.send(:command, :undef)).to eq 'gunzip -d test.gz' - zip = described_class.new('test.zip') - expect(zip.send(:command, :undef)).to eq 'unzip -o test.zip' - expect(zip.send(:command, '-a')).to eq 'unzip -a test.zip' - - zip = described_class.new('/tmp/fun folder/test.zip') - expect(zip.send(:command, :undef)).to eq 'unzip -o /tmp/fun\ folder/test.zip' - expect(zip.send(:command, '-a')).to eq 'unzip -a /tmp/fun\ folder/test.zip' - - tar = described_class.new('test.tar.Z') - expect(tar.send(:command, :undef)).to eq 'uncompress -c test.tar.Z | tar xf -' + describe '#command' do + subject { |example| described_class.new(example.metadata[:filename]) } + + before { allow(Facter).to receive(:value).with(:osfamily).and_return(os) } + after { expect(Facter).to have_received(:value).with(:osfamily).at_least(:twice) } + + describe 'on RedHat' do + let(:os) { 'RedHat' } + + describe 'tar.gz', filename: 'test.tar.gz' do + it { expect(subject.send(:command, :undef)).to eq 'tar xzf test.tar.gz' } + it { expect(subject.send(:command, 'xvf')).to eq 'tar xvf test.tar.gz' } + end + + describe 'tar.bz2', filename: 'test.tar.bz2' do + it { expect(subject.send(:command, :undef)).to eq 'tar xjf test.tar.bz2' } + it { expect(subject.send(:command, 'xjf')).to eq 'tar xjf test.tar.bz2' } + end + + describe 'tar.xz', filename: 'test.tar.xz' do + it { expect(subject.send(:command, :undef)).to eq 'unxz -dc test.tar.xz | tar xf -' } + end + + describe 'gz', filename: 'test.gz' do + it { expect(subject.send(:command, :undef)).to eq 'gunzip -d test.gz' } + end + + describe 'bz2', filename: 'test.bz2' do + it { expect(subject.send(:command, :undef)).to eq 'bunzip2 -d test.bz2' } + end + + describe 'zip' do + describe 'filename', filename: 'test.zip' do + it { expect(subject.send(:command, :undef)).to eq 'unzip -o test.zip' } + it { expect(subject.send(:command, '-a')).to eq 'unzip -a test.zip' } + end + + describe 'path with space', filename: '/tmp/fun folder/test.zip' do + it { expect(subject.send(:command, :undef)).to eq 'unzip -o /tmp/fun\ folder/test.zip' } + it { expect(subject.send(:command, '-a')).to eq 'unzip -a /tmp/fun\ folder/test.zip' } + end + end end - end - it '#command on Windows' do - Facter.stubs(:value).with(:osfamily).returns 'windows' + system_v = %w[Solaris AIX] + system_v.each do |os| + describe "on #{os}" do + let(:os) { os } + + describe 'tar.gz', filename: 'test.tar.gz' do + it { expect(subject.send(:command, :undef)).to eq 'gunzip -dc test.tar.gz | tar xf -' } + it { expect(subject.send(:command, 'gunzip' => '-dc', 'tar' => 'xvf')).to eq 'gunzip -dc test.tar.gz | tar xvf -' } + end + + describe 'tar.bz2', filename: 'test.tar.bz2' do + it { expect(subject.send(:command, :undef)).to eq 'bunzip2 -dc test.tar.bz2 | tar xf -' } + it { expect(subject.send(:command, 'bunzip' => '-dc', 'tar' => 'xvf')).to eq 'bunzip2 -dc test.tar.bz2 | tar xvf -' } + end + + describe 'tar.xz', filename: 'test.tar.xz' do + it { expect(subject.send(:command, :undef)).to eq 'unxz -dc test.tar.xz | tar xf -' } + end + + describe 'gz', filename: 'test.gz' do + it { expect(subject.send(:command, :undef)).to eq 'gunzip -d test.gz' } + end + + describe 'zip' do + describe 'filename', filename: 'test.zip' do + it { expect(subject.send(:command, :undef)).to eq 'unzip -o test.zip' } + it { expect(subject.send(:command, '-a')).to eq 'unzip -a test.zip' } + end + + describe 'path with space' do + subject { described_class.new('/tmp/fun folder/test.zip') } + + it { expect(subject.send(:command, :undef)).to eq 'unzip -o /tmp/fun\ folder/test.zip' } + it { expect(subject.send(:command, '-a')).to eq 'unzip -a /tmp/fun\ folder/test.zip' } + end + end + + describe 'tar.Z' do + subject { described_class.new('test.tar.Z') } + + it { expect(subject.send(:command, :undef)).to eq 'uncompress -c test.tar.Z | tar xf -' } + end + end + end + + describe 'on Windows' do + let(:os) { 'windows' } + + # rubocop:disable RSpec/SubjectStub + before { allow(subject).to receive(:win_7zip).and_return(zip_cmd) } + # rubocop:enable RSpec/SubjectStub + + context '7z.exe' do + let(:zip_cmd) { '7z.exe' } - tar = described_class.new('test.tar.gz') - tar.stubs(:win_7zip).returns('7z.exe') - expect(tar.send(:command, :undef)).to eq '7z.exe x -aoa "test.tar.gz"' - expect(tar.send(:command, 'x -aot')).to eq '7z.exe x -aot "test.tar.gz"' + describe 'tar.gz', filename: 'test.tar.gz' do + it { expect(subject.send(:command, :undef)).to eq '7z.exe x -aoa "test.tar.gz"' } + it { expect(subject.send(:command, 'x -aot')).to eq '7z.exe x -aot "test.tar.gz"' } + end - zip = described_class.new('test.zip') - zip.stubs(:win_7zip).returns('7z.exe') - expect(zip.send(:command, :undef)).to eq '7z.exe x -aoa "test.zip"' + describe 'zip' do + describe 'filename', filename: 'test.zip' do + it { expect(subject.send(:command, :undef)).to eq '7z.exe x -aoa "test.zip"' } + end - zip = described_class.new('C:/Program Files/test.zip') - zip.stubs(:win_7zip).returns('7z.exe') - expect(zip.send(:command, :undef)).to eq '7z.exe x -aoa "C:/Program Files/test.zip"' + describe 'path with space', filename: 'C:/Program Files/test.zip' do + it { expect(subject.send(:command, :undef)).to eq '7z.exe x -aoa "C:/Program Files/test.zip"' } + end + end + end - zip = described_class.new('C:/Program Files/test.zip') - zip.stubs(:win_7zip).returns('powershell') - expect(zip.send(:command, :undef)).to eq 'powershell' + describe 'powershell', filename: 'C:/Program Files/test.zip' do + let(:zip_cmd) { 'powershell' } + + it { expect(subject.send(:command, :undef)).to eq 'powershell' } + end + end end end