chefspec 7.3.3 → 7.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +23 -0
  3. data/Rakefile +77 -0
  4. data/chefspec.gemspec +29 -0
  5. data/lib/chefspec/version.rb +1 -1
  6. data/spec/spec_helper.rb +13 -0
  7. data/spec/support/hash.rb +35 -0
  8. data/spec/unit/cacher_spec.rb +70 -0
  9. data/spec/unit/coverage/filters_spec.rb +60 -0
  10. data/spec/unit/deprecations_spec.rb +53 -0
  11. data/spec/unit/errors_spec.rb +57 -0
  12. data/spec/unit/expect_exception_spec.rb +32 -0
  13. data/spec/unit/macros_spec.rb +119 -0
  14. data/spec/unit/matchers/do_nothing_matcher.rb +5 -0
  15. data/spec/unit/matchers/include_recipe_matcher_spec.rb +38 -0
  16. data/spec/unit/matchers/link_to_matcher_spec.rb +55 -0
  17. data/spec/unit/matchers/notifications_matcher_spec.rb +40 -0
  18. data/spec/unit/matchers/render_file_matcher_spec.rb +68 -0
  19. data/spec/unit/matchers/resource_matcher_spec.rb +5 -0
  20. data/spec/unit/matchers/state_attrs_matcher_spec.rb +68 -0
  21. data/spec/unit/matchers/subscribes_matcher_spec.rb +65 -0
  22. data/spec/unit/renderer_spec.rb +69 -0
  23. data/spec/unit/server_runner_spec.rb +28 -0
  24. data/spec/unit/solo_runner_spec.rb +171 -0
  25. data/spec/unit/stubs/command_registry_spec.rb +27 -0
  26. data/spec/unit/stubs/command_stub_spec.rb +61 -0
  27. data/spec/unit/stubs/data_bag_item_registry_spec.rb +39 -0
  28. data/spec/unit/stubs/data_bag_item_stub_spec.rb +36 -0
  29. data/spec/unit/stubs/data_bag_registry_spec.rb +39 -0
  30. data/spec/unit/stubs/data_bag_stub_spec.rb +35 -0
  31. data/spec/unit/stubs/registry_spec.rb +29 -0
  32. data/spec/unit/stubs/search_registry_spec.rb +39 -0
  33. data/spec/unit/stubs/search_stub_spec.rb +36 -0
  34. data/spec/unit/stubs/stub_spec.rb +64 -0
  35. metadata +34 -2
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChefSpec::Renderer do
4
+ describe '.initialize' do
5
+ it 'accepts two arguments and assigns their instance variables' do
6
+ instance = described_class.new('runner', 'resource')
7
+ expect(instance.chef_run).to eq('runner')
8
+ expect(instance.resource).to eq('resource')
9
+ end
10
+ end
11
+
12
+ let(:chef_run) { double('chef_run', {node: 'node'}) }
13
+ let(:resource) { double('resource', {cookbook: 'cookbook', source: 'source', variables: {}}) }
14
+ subject { described_class.new(chef_run, resource) }
15
+
16
+ describe '#content' do
17
+ before do
18
+ allow(subject).to receive(:content_from_cookbook_file).and_return('cookbook_file content')
19
+ allow(subject).to receive(:content_from_file).and_return('file content')
20
+ allow(subject).to receive(:content_from_template).and_return('template content')
21
+ end
22
+
23
+ context 'when the resource is a cookbook_file' do
24
+ it 'renders the cookbook_file content' do
25
+ allow(resource).to receive(:resource_name).and_return('cookbook_file')
26
+ expect(subject.content).to eq('cookbook_file content')
27
+ end
28
+ end
29
+
30
+ context 'when the resource is a file' do
31
+ it 'renders the file content' do
32
+ allow(resource).to receive(:resource_name).and_return('file')
33
+ expect(subject.content).to eq('file content')
34
+ end
35
+ end
36
+
37
+ context 'when the resource is a template' do
38
+ it 'renders the template content' do
39
+ allow(resource).to receive(:resource_name).and_return('template')
40
+ expect(subject.content).to eq('template content')
41
+ end
42
+ end
43
+
44
+ context 'when the resource is not a file type' do
45
+ it 'returns nil' do
46
+ allow(resource).to receive(:resource_name).and_return('service')
47
+ expect(subject.content).to be_nil
48
+ end
49
+ end
50
+ end
51
+
52
+ describe 'content_from_template' do
53
+ it 'renders the template by extending modules for rendering paritals within the template' do
54
+ cookbook_collection = {}
55
+ cookbook_collection['cookbook'] = double('', {preferred_filename_on_disk_location: "/template/location"} )
56
+ allow(subject).to receive(:cookbook_collection).with('node').and_return(cookbook_collection)
57
+ allow(subject).to receive(:template_finder)
58
+
59
+ allow(resource).to receive(:helper_modules).and_return([Module.new])
60
+ allow(resource).to receive(:resource_name).and_return('template')
61
+
62
+ chef_template_context = double('context', {render_template: 'rendered template content',update: nil})
63
+ allow(Chef::Mixin::Template::TemplateContext).to receive(:new).and_return(chef_template_context)
64
+
65
+ expect(chef_template_context).to receive(:_extend_modules).with(resource.helper_modules)
66
+ expect(subject.content).to eq('rendered template content')
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChefSpec::ServerRunner do
4
+ context 'when the RSpec config is set' do
5
+ it 'does not create a tmpdir' do
6
+ allow(RSpec.configuration).to receive(:file_cache_path)
7
+ .and_return("/foo/bar")
8
+ expect(ChefSpec::FileCachePathProxy).to_not receive(:instance)
9
+ described_class.new
10
+ end
11
+ end
12
+
13
+ context 'when the RSpec config is not set' do
14
+ it 'creates and returns a tmpdir' do
15
+ expect(ChefSpec::FileCachePathProxy).to receive(:instance)
16
+ .and_call_original
17
+ described_class.new
18
+ expect(Chef::Config[:file_cache_path]).to match(/chefspec/)
19
+ end
20
+
21
+ it 'uses the same path' do
22
+ val = ChefSpec::FileCachePathProxy.instance.file_cache_path
23
+ expect(described_class.new.options[:file_cache_path]).to eq(val)
24
+ expect(described_class.new.options[:file_cache_path]).to eq(val)
25
+ expect(described_class.new.options[:file_cache_path]).to eq(val)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,171 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChefSpec::SoloRunner do
4
+ before do
5
+ allow_any_instance_of(ChefSpec::SoloRunner)
6
+ .to receive(:dry_run?)
7
+ .and_return(true)
8
+ end
9
+
10
+ describe '#initialize' do
11
+ let(:windows_caller_stack) do
12
+ [
13
+ "C:/cookbooks/Temp/spec/test_spec.rb:11:in `block (2 levels) in <top (required)>'",
14
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:114:in `instance_eval'",
15
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:114:in `block in run'",
16
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:254:in `with_around_each_hooks'",
17
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example.rb:111:in `run'",
18
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:390:in `block in run_examples'",
19
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `map'",
20
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:386:in `run_examples'",
21
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:371:in `run'",
22
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'",
23
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `map'",
24
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:28:in `block in run'",
25
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/reporter.rb:58:in `report'",
26
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:25:in `run'",
27
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'",
28
+ "C:/Ruby193/lib/ruby/gems/1.9.1/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'",
29
+ ]
30
+ end
31
+
32
+ it 'defaults the log level to :warn' do
33
+ described_class.new
34
+ expect(Chef::Log.level).to eq(:warn)
35
+ end
36
+
37
+ it 'sets the log level' do
38
+ described_class.new(log_level: :error)
39
+ expect(Chef::Log.level).to eq(:error)
40
+ end
41
+
42
+ it 'defaults the cookbook_path to the calling spec' do
43
+ described_class.new
44
+ expect(Chef::Config.cookbook_path).to eq([File.expand_path('../../../..', __FILE__)])
45
+ end
46
+
47
+ it 'defaults the cookbook_path to the calling spec when using windows paths' do
48
+ runner = described_class.new
49
+ windows_path = runner.instance_exec(windows_caller_stack) { |callstack|
50
+ calling_cookbook_path({}, callstack)
51
+ }
52
+ # There's got to be a better way to do this the File.expand_path returns
53
+ # something like /home/user/repos/chefspec/C:/cookbooks" which is less
54
+ # than ideal as a robust test
55
+ expect(windows_path).to end_with("C:/cookbooks")
56
+ end
57
+
58
+ it 'sets the cookbook path' do
59
+ described_class.new(cookbook_path: '/tmp/bacon')
60
+ expect(Chef::Config.cookbook_path).to eq(['/tmp/bacon'])
61
+ end
62
+
63
+ it 'sets the file cache path' do
64
+ described_class.new( file_cache_path: '/tmp/pantoa')
65
+ expect(Chef::Config.file_cache_path).to eq('/tmp/pantoa')
66
+ end
67
+
68
+ it 'sets the Chef::Config' do
69
+ expect(Chef::Config.cache_type).to eq('Memory')
70
+ expect(Chef::Config.force_logger).to be_truthy
71
+ expect(Chef::Config.no_lazy_load).to be_truthy
72
+ expect(Chef::Config.solo).to be_truthy
73
+ expect(Chef::Config.use_policyfile).to be_falsey
74
+ end
75
+
76
+ it 'yields a block to set node attributes' do
77
+ expect { |block| described_class.new({}, &block) }.to yield_with_args(Chef::Node)
78
+ end
79
+
80
+ context 'default ohai attributes' do
81
+ let(:hash) { described_class.new.node.to_hash }
82
+
83
+ it 'sets the default attributes' do
84
+ expect(hash['os']).to eq('chefspec')
85
+ expect(hash['languages']['ruby']['bin_dir']).to eq('/usr/local/bin')
86
+ expect(hash['os_version']).to eq('0.6.1')
87
+ expect(hash['fqdn']).to eq('chefspec.local')
88
+ expect(hash['domain']).to eq('local')
89
+ expect(hash['ipaddress']).to eq('127.0.0.1')
90
+ expect(hash['hostname']).to eq('chefspec')
91
+ expect(hash['kernel']['machine']).to eq('i386')
92
+ end
93
+ end
94
+
95
+ context 'fauxhai attributes' do
96
+ let(:hash) { described_class.new(platform: 'ubuntu', version: '18.04').node.to_hash }
97
+
98
+ it 'sets the attributes from fauxhai' do
99
+ expect(hash['os']).to eq('linux')
100
+ expect(hash['languages']['ruby']['ruby_bin']).to eq('/usr/local/bin/ruby')
101
+ expect(hash['os_version']).to match(/4.15.0-.*-.*/) # avoid failing when fauxhai data changes
102
+ expect(hash['fqdn']).to eq('fauxhai.local')
103
+ expect(hash['domain']).to eq('local')
104
+ expect(hash['ipaddress']).to eq('10.0.0.2')
105
+ expect(hash['hostname']).to eq('Fauxhai')
106
+ expect(hash['kernel']['machine']).to eq('x86_64')
107
+ end
108
+ end
109
+
110
+ context 'RSpec global configuration' do
111
+ before do
112
+ allow(RSpec.configuration).to receive(:cookbook_path).and_return('./path')
113
+ allow(RSpec.configuration).to receive(:environment_path).and_return('./env-path')
114
+ allow(RSpec.configuration).to receive(:file_cache_path).and_return('./file-cache-path')
115
+ allow(RSpec.configuration).to receive(:log_level).and_return(:fatal)
116
+ allow(RSpec.configuration).to receive(:path).and_return('ohai.json')
117
+ allow(RSpec.configuration).to receive(:platform).and_return('ubuntu')
118
+ allow(RSpec.configuration).to receive(:version).and_return('12.04')
119
+ end
120
+
121
+ it 'uses the RSpec values' do
122
+ options = described_class.new.options
123
+ expect(options[:cookbook_path]).to eq('./path')
124
+ expect(options[:environment_path]).to eq('./env-path')
125
+ expect(options[:file_cache_path]).to eq('./file-cache-path')
126
+ expect(options[:log_level]).to eq(:fatal)
127
+ expect(options[:path]).to eq('ohai.json')
128
+ expect(options[:platform]).to eq('ubuntu')
129
+ expect(options[:version]).to eq('12.04')
130
+ end
131
+ end
132
+ end
133
+
134
+ describe '#node' do
135
+ it 'returns the Chef::Node' do
136
+ expect(subject.node).to be_a(Chef::Node)
137
+ end
138
+
139
+ it 'defines a #runner method that returns self' do
140
+ expect(subject.node.methods).to include(:runner)
141
+ expect(subject.node.runner).to be(subject)
142
+ end
143
+
144
+ it 'allows attributes to be set on the node' do
145
+ subject.node.normal['bacon'] = 'ham'
146
+ expect(subject.node.bacon).to eq('ham')
147
+ end
148
+ end
149
+
150
+ describe '#to_s' do
151
+ it 'overrides the default string representation to something readable' do
152
+ expect(subject.converge('apache2::default').to_s)
153
+ .to eq('#<ChefSpec::SoloRunner run_list: [recipe[apache2::default]]>')
154
+ end
155
+
156
+ it 'is ok when a convergence has not yet taken place' do
157
+ expect(subject.to_s).to eq('#<ChefSpec::SoloRunner run_list: []>')
158
+ end
159
+
160
+ it 'includes the entire run_list' do
161
+ expect(subject.converge('apache2::default', 'apache2::mod_ssl').to_s)
162
+ .to eq('#<ChefSpec::SoloRunner run_list: [recipe[apache2::default], recipe[apache2::mod_ssl]]>')
163
+ end
164
+
165
+ it 'has the run_list only for the last convergence' do
166
+ ['mysql::client', 'mysql::server'].each { |recipe| subject.converge(recipe) }
167
+ expect(subject.to_s)
168
+ .to eq('#<ChefSpec::SoloRunner run_list: [recipe[mysql::server]]>')
169
+ end
170
+ end
171
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChefSpec::Stubs::CommandRegistry do
4
+ before { described_class.reset! }
5
+
6
+ it 'inherits from Registry' do
7
+ expect(described_class.superclass).to be(ChefSpec::Stubs::Registry)
8
+ end
9
+
10
+ describe '#stub_for' do
11
+ it 'finds a stub by name' do
12
+ command = double('command', command: 'test -f /var/www')
13
+ described_class.register(command)
14
+ expect(described_class.stub_for('test -f /var/www')).to eq(command)
15
+ end
16
+
17
+ it 'finds a stub by regex' do
18
+ command = double('command', command: /test -f (.+)/)
19
+ described_class.register(command)
20
+ expect(described_class.stub_for('test -f /var/www')).to eq(command)
21
+ end
22
+
23
+ it 'returns nil when no commands are matched' do
24
+ expect(described_class.stub_for('which a2ensite')).to eq(nil)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChefSpec::Stubs::CommandStub do
4
+ describe '#initialize' do
5
+ it 'sets the command and block' do
6
+ block = Proc.new {}
7
+ stub = described_class.new('command', &block)
8
+
9
+ expect(stub.command).to eq('command')
10
+ expect(stub.block).to eq(block)
11
+ end
12
+ end
13
+
14
+ describe '#and_return' do
15
+ subject { described_class.new('command').and_return('value') }
16
+
17
+ it 'sets the value' do
18
+ expect(subject.value).to eq('value')
19
+ end
20
+
21
+ it 'returns an instance of the stub' do
22
+ expect(subject.and_return('value')).to be(subject)
23
+ end
24
+ end
25
+
26
+ describe '#result' do
27
+ context 'when a value is given' do
28
+ subject { described_class.new('command').and_return('value') }
29
+
30
+ it 'returns the value' do
31
+ expect(subject.result).to eq('value')
32
+ end
33
+ end
34
+
35
+ context 'when a block is given' do
36
+ subject { described_class.new('command') { 1 == 2 } }
37
+
38
+ it 'calls the block' do
39
+ expect(subject.result).to eq(false)
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '#signature' do
45
+ context 'when a value is given' do
46
+ subject { described_class.new('command').and_return(false) }
47
+
48
+ it 'includes the value' do
49
+ expect(subject.signature).to eq('stub_command("command").and_return(false)')
50
+ end
51
+ end
52
+
53
+ context 'when a block is given' do
54
+ subject { described_class.new('command') { 1 == 2 } }
55
+
56
+ it 'includes a comment about the block' do
57
+ expect(subject.signature).to eq('stub_command("command") { # Ruby code }')
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChefSpec::Stubs::DataBagItemRegistry do
4
+ before { described_class.reset! }
5
+
6
+ it 'inherits from Registry' do
7
+ expect(described_class.superclass).to be(ChefSpec::Stubs::Registry)
8
+ end
9
+
10
+ describe '#stub_for' do
11
+ it 'finds a stub by name' do
12
+ search = double('search', bag: 'users', id: 'svargo')
13
+ described_class.register(search)
14
+ expect(described_class.stub_for('users', 'svargo')).to eq(search)
15
+ end
16
+
17
+ it 'finds a stub by Regex' do
18
+ search = double('search', bag: 'users', id: /sv(.+)/)
19
+ described_class.register(search)
20
+ expect(described_class.stub_for('users', 'svargo')).to eq(search)
21
+ end
22
+
23
+ it 'matches when the bag is a Symbol' do
24
+ search = double('search', bag: :users, id: 'svargo')
25
+ described_class.register(search)
26
+ expect(described_class.stub_for('users', 'svargo')).to eq(search)
27
+ end
28
+
29
+ it 'matches when the stub is a Symbol' do
30
+ search = double('search', bag: 'users', id: 'svargo')
31
+ described_class.register(search)
32
+ expect(described_class.stub_for(:users, 'svargo')).to eq(search)
33
+ end
34
+
35
+ it 'returns nil when no searches are matched' do
36
+ expect(described_class.stub_for('users', 'svargo')).to eq(nil)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChefSpec::Stubs::DataBagItemStub do
4
+ it 'inherts from Stub' do
5
+ expect(described_class.superclass).to be(ChefSpec::Stubs::Stub)
6
+ end
7
+
8
+ describe '#initialize' do
9
+ it 'sets the bag, id, and block' do
10
+ block = Proc.new {}
11
+ stub = described_class.new('bag', 'id', &block)
12
+
13
+ expect(stub.bag).to eq('bag')
14
+ expect(stub.id).to eq('id')
15
+ expect(stub.block).to eq(block)
16
+ end
17
+ end
18
+
19
+ describe '#signature' do
20
+ context 'when a value is given' do
21
+ subject { described_class.new('bag', 'id').and_return(false) }
22
+
23
+ it 'includes the value' do
24
+ expect(subject.signature).to eq('stub_data_bag_item("bag", "id").and_return(false)')
25
+ end
26
+ end
27
+
28
+ context 'when a block is given' do
29
+ subject { described_class.new('bag', 'id') { 1 == 2 } }
30
+
31
+ it 'includes a comment about the block' do
32
+ expect(subject.signature).to eq('stub_data_bag_item("bag", "id") { # Ruby code }')
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChefSpec::Stubs::DataBagRegistry do
4
+ before { described_class.reset! }
5
+
6
+ it 'inherits from Registry' do
7
+ expect(described_class.superclass).to be(ChefSpec::Stubs::Registry)
8
+ end
9
+
10
+ describe '#stub_for' do
11
+ it 'finds a stub by name' do
12
+ search = double('search', bag: 'users')
13
+ described_class.register(search)
14
+ expect(described_class.stub_for('users')).to eq(search)
15
+ end
16
+
17
+ it 'finds a stub by Regex' do
18
+ search = double('search', bag: 'users')
19
+ described_class.register(search)
20
+ expect(described_class.stub_for('users')).to eq(search)
21
+ end
22
+
23
+ it 'matches when the bag is Symbol' do
24
+ search = double('search', bag: :users)
25
+ described_class.register(search)
26
+ expect(described_class.stub_for('users')).to eq(search)
27
+ end
28
+
29
+ it 'matches when the stub is Symbol' do
30
+ search = double('search', bag: 'users')
31
+ described_class.register(search)
32
+ expect(described_class.stub_for(:users)).to eq(search)
33
+ end
34
+
35
+ it 'returns nil when no searches are matched' do
36
+ expect(described_class.stub_for('users')).to eq(nil)
37
+ end
38
+ end
39
+ end