diff --git a/spec/unit/puppet/provider/archive/gs_spec.rb b/spec/unit/puppet/provider/archive/gs_spec.rb index e5f821f..ad4ba3a 100644 --- a/spec/unit/puppet/provider/archive/gs_spec.rb +++ b/spec/unit/puppet/provider/archive/gs_spec.rb @@ -1,97 +1,97 @@ require 'spec_helper' ruby_provider = Puppet::Type.type(:archive).provider(:ruby) RSpec.describe ruby_provider do it_behaves_like 'an archive provider', ruby_provider describe 'ruby provider' do let(:name) { '/tmp/example.zip' } let(:resource_properties) do { name: name, source: 'gs://home.lan/example.zip' } end let(:resource) { Puppet::Type::Archive.new(resource_properties) } let(:provider) { ruby_provider.new(resource) } let(:gs_download_options) do ['cp', 'gs://home.lan/example.zip', String] end before do allow(provider).to receive(:gsutil) end context 'default resource property' do it '#gs_download' do provider.gs_download(name) expect(provider).to have_received(:gsutil).with(gs_download_options) end it '#extract nothing' do expect(provider.extract).to be_nil end end describe '#checksum' do subject { provider.checksum } let(:url) { nil } let(:remote_hash) { nil } before do resource[:checksum_url] = url if url allow(PuppetX::Bodeco::Util).to receive(:content) .\ with(url, any_args).and_return(remote_hash) end context 'unset' do it { is_expected.to be_nil } end context 'with a url' do let(:url) { 'http://example.com/checksum' } context 'responds with hash' do let(:remote_hash) { 'a0c38e1aeb175201b0dacd65e2f37e187657050a' } it { is_expected.to eq('a0c38e1aeb175201b0dacd65e2f37e187657050a') } end context 'responds with hash and newline' do let(:remote_hash) { "a0c38e1aeb175201b0dacd65e2f37e187657050a\n" } it { is_expected.to eq('a0c38e1aeb175201b0dacd65e2f37e187657050a') } end context 'responds with `sha1sum README.md` output' do let(:remote_hash) { "a0c38e1aeb175201b0dacd65e2f37e187657050a README.md\n" } it { is_expected.to eq('a0c38e1aeb175201b0dacd65e2f37e187657050a') } end context 'responds with `openssl dgst -hex -sha256 README.md` output' do let(:remote_hash) { "SHA256(README.md)= 8fa3f0ff1f2557657e460f0f78232679380a9bcdb8670e3dcb33472123b22428\n" } it { is_expected.to eq('8fa3f0ff1f2557657e460f0f78232679380a9bcdb8670e3dcb33472123b22428') } end end end describe 'download options' do let(:resource_properties) do { name: name, source: 'gs://home.lan/example.zip', - download_options: ['--region', 'eu-central-1'] + download_options: [] } end context 'default resource property' do it '#gs_download' do provider.gs_download(name) - expect(provider).to have_received(:gsutil).with(gs_download_options << '--region' << 'eu-central-1') + expect(provider).to have_received(:gsutil).with(gs_download_options) end end end end end diff --git a/spec/unit/puppet/type/archive_spec.rb b/spec/unit/puppet/type/archive_spec.rb index a6806eb..0864c40 100644 --- a/spec/unit/puppet/type/archive_spec.rb +++ b/spec/unit/puppet/type/archive_spec.rb @@ -1,161 +1,162 @@ require 'spec_helper' require 'puppet' describe Puppet::Type.type(:archive) do let(:resource) do Puppet::Type.type(:archive).new( path: '/tmp/example.zip', source: 'http://home.lan/example.zip' ) end context 'resource defaults' do it { expect(resource[:path]).to eq '/tmp/example.zip' } it { expect(resource[:name]).to eq '/tmp/example.zip' } it { expect(resource[:filename]).to eq 'example.zip' } it { expect(resource[:extract]).to eq :false } it { expect(resource[:cleanup]).to eq :true } it { expect(resource[:checksum_type]).to eq :none } it { expect(resource[:digest_type]).to eq nil } it { expect(resource[:checksum_verify]).to eq :true } it { expect(resource[:extract_flags]).to eq :undef } it { expect(resource[:allow_insecure]).to eq false } it { expect(resource[:download_options]).to eq nil } it { expect(resource[:temp_dir]).to eq nil } end it 'verify resource[:path] is absolute filepath' do expect do resource[:path] = 'relative/file' end.to raise_error(Puppet::Error, %r{archive path must be absolute: }) end it 'verify resource[:temp_dir] is absolute filetemp_dir' do expect do resource[:temp_dir] = 'relative/file' end.to raise_error(Puppet::Error, %r{Invalid temp_dir}) end describe 'on posix', if: Puppet.features.posix? do it 'accepts valid resource[:source]' do expect do resource[:source] = 'http://home.lan/example.zip' resource[:source] = 'https://home.lan/example.zip' resource[:source] = 'ftp://home.lan/example.zip' resource[:source] = 's3://home.lan/example.zip' + resource[:source] = 'gs://home.lan/example.zip' resource[:source] = '/tmp/example.zip' end.not_to raise_error end %w[ afp://home.lan/example.zip \tmp D:/example.zip ].each do |s| it 'rejects invalid resource[:source]' do expect do resource[:source] = s end.to raise_error(Puppet::Error, %r{invalid source url: }) end end end describe 'on windows', if: Puppet.features.microsoft_windows? do it 'accepts valid windows resource[:source]' do expect do resource[:source] = 'D:/example.zip' end.not_to raise_error end %w[ /tmp/example.zip \Z: ].each do |s| it 'rejects invalid windows resource[:source]' do expect do resource[:source] = s end.to raise_error(Puppet::Error, %r{invalid source url: }) end end end %w[ 557e2ebb67b35d1fddff18090b6bc26b 557e2ebb67b35d1fddff18090b6bc26557e2ebb67b35d1fddff18090b6bc26bb ].each do |cs| it 'accepts valid resource[:checksum]' do expect do resource[:checksum] = cs end.not_to raise_error end end %w[ z57e2ebb67b35d1fddff18090b6bc26b 557e ].each do |cs| it 'rejects bad checksum' do expect do resource[:checksum] = cs end.to raise_error(Puppet::Error, %r{Invalid value}) end end it 'accepts valid resource[:checksum_type]' do expect do [:none, :md5, :sha1, :sha2, :sha256, :sha384, :sha512].each do |type| resource[:checksum_type] = type end end.not_to raise_error end it 'rejects invalid resource[:checksum_type]' do expect do resource[:checksum_type] = :crc32 end.to raise_error(Puppet::Error, %r{Invalid value}) end it 'verify resource[:allow_insecure] is valid' do expect do [:true, :false, :yes, :no].each do |type| resource[:allow_insecure] = type end end.not_to raise_error end it 'verify resource[:download_options] is valid' do expect do ['--tlsv1', ['--region', 'eu-central-1']].each do |type| resource[:download_options] = type end end.not_to raise_error end describe 'archive autorequire' do let(:file_resource) { Puppet::Type.type(:file).new(name: '/tmp') } let(:archive_resource) do described_class.new( path: '/tmp/example.zip', source: 'http://home.lan/example.zip' ) end let(:auto_req) do catalog = Puppet::Resource::Catalog.new catalog.add_resource file_resource catalog.add_resource archive_resource archive_resource.autorequire end it 'creates relationship' do expect(auto_req.size).to be 1 end it 'links to archive resource' do expect(auto_req[0].target).to eql archive_resource end it 'autorequires parent directory' do expect(auto_req[0].source).to eql file_resource end end end