Add puppet/archive module
[mirror/dsa-puppet.git] / 3rdparty / modules / archive / spec / support / shared_behaviour.rb
1 # rubocop:disable RSpec/MultipleExpectations
2 require 'spec_helper'
3 require 'tmpdir'
4
5 RSpec.shared_examples 'an archive provider' do |provider_class|
6   describe provider_class do
7     let(:resource) do
8       Puppet::Type::Archive.new(name: '/tmp/example.zip', source: 'http://home.lan/example.zip')
9     end
10
11     let(:provider) do
12       provider_class.new(resource)
13     end
14
15     let(:zipfile) do
16       File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'files', 'test.zip'))
17     end
18
19     it '#checksum?' do
20       Dir.mktmpdir do |dir|
21         resource[:path] = File.join(dir, resource[:filename])
22         FileUtils.cp(zipfile, resource[:path])
23
24         resource[:checksum] = '377ec712d7fdb7266221db3441e3af2055448ead'
25         resource[:checksum_type] = :sha1
26         expect(provider.checksum?).to eq true
27
28         resource[:checksum] = '557e2ebb67b35d1fddff18090b6bc26b'
29         resource[:checksum_type] = :md5
30         expect(provider.checksum?).to eq true
31
32         resource[:checksum] = '557e2ebb67b35d1fddff18090b6bc26b'
33         resource[:checksum_type] = :sha1
34         expect(provider.checksum?).to eq false
35       end
36     end
37
38     it '#extract' do
39       skip 'jruby not supported' if defined? JRUBY_VERSION
40       Dir.mktmpdir do |dir|
41         resource[:path] = File.join(dir, resource[:filename])
42         extracted_file = File.join(dir, 'test')
43         FileUtils.cp(zipfile, resource[:path])
44
45         resource[:extract] = :true
46         resource[:creates] = extracted_file
47         resource[:extract_path] = dir
48
49         provider.extract
50         expect(File.read(extracted_file)).to eq "hello world\n"
51       end
52     end
53
54     it '#extracted?' do
55       skip 'jruby not supported' if defined? JRUBY_VERSION
56       Dir.mktmpdir do |dir|
57         resource[:path] = File.join(dir, resource[:filename])
58         extracted_file = File.join(dir, 'test')
59         FileUtils.cp(zipfile, resource[:path])
60
61         resource[:extract] = :true
62         resource[:creates] = extracted_file
63         resource[:extract_path] = dir
64
65         expect(provider.extracted?).to eq false
66         provider.extract
67         expect(provider.extracted?).to eq true
68       end
69     end
70
71     it '#cleanup' do
72       skip 'jruby not supported' if defined? JRUBY_VERSION
73       Dir.mktmpdir do |dir|
74         resource[:path] = File.join(dir, resource[:filename])
75         extracted_file = File.join(dir, 'test')
76         FileUtils.cp(zipfile, resource[:path])
77
78         resource[:extract] = :true
79         resource[:cleanup] = :true
80         resource[:creates] = extracted_file
81         resource[:extract_path] = dir
82
83         provider.extract
84         provider.cleanup
85         expect(File.exist?(resource[:path])).to eq false
86       end
87     end
88
89     it '#create' do
90       skip 'jruby not supported' if defined? JRUBY_VERSION
91       Dir.mktmpdir do |dir|
92         resource[:path] = File.join(dir, resource[:filename])
93         extracted_file = File.join(dir, 'test')
94         FileUtils.cp(zipfile, resource[:path])
95
96         resource[:extract] = :true
97         resource[:cleanup] = :true
98         resource[:creates] = extracted_file
99         resource[:extract_path] = dir
100
101         provider.create
102         expect(File.read(extracted_file)).to eq "hello world\n"
103         expect(File.exist?(resource[:path])).to eq false
104       end
105     end
106   end
107 end