chefspec-chef 9.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +30 -0
- data/LICENSE +22 -0
- data/Rakefile +85 -0
- data/chefspec-chef.gemspec +30 -0
- data/lib/chefspec/api/core.rb +217 -0
- data/lib/chefspec/api/described.rb +53 -0
- data/lib/chefspec/api/do_nothing.rb +26 -0
- data/lib/chefspec/api/include_any_recipe.rb +24 -0
- data/lib/chefspec/api/include_recipe.rb +28 -0
- data/lib/chefspec/api/link.rb +28 -0
- data/lib/chefspec/api/notifications.rb +40 -0
- data/lib/chefspec/api/reboot.rb +14 -0
- data/lib/chefspec/api/render_file.rb +37 -0
- data/lib/chefspec/api/state_attrs.rb +30 -0
- data/lib/chefspec/api/stubs.rb +183 -0
- data/lib/chefspec/api/stubs_for.rb +139 -0
- data/lib/chefspec/api/subscriptions.rb +37 -0
- data/lib/chefspec/api/user.rb +230 -0
- data/lib/chefspec/api.rb +39 -0
- data/lib/chefspec/berkshelf.rb +63 -0
- data/lib/chefspec/cacher.rb +64 -0
- data/lib/chefspec/coverage/filters.rb +82 -0
- data/lib/chefspec/coverage.rb +247 -0
- data/lib/chefspec/deprecations.rb +46 -0
- data/lib/chefspec/errors.rb +48 -0
- data/lib/chefspec/expect_exception.rb +51 -0
- data/lib/chefspec/extensions/chef/client.rb +21 -0
- data/lib/chefspec/extensions/chef/conditional.rb +16 -0
- data/lib/chefspec/extensions/chef/cookbook/gem_installer.rb +33 -0
- data/lib/chefspec/extensions/chef/cookbook_loader.rb +14 -0
- data/lib/chefspec/extensions/chef/cookbook_uploader.rb +12 -0
- data/lib/chefspec/extensions/chef/data_query.rb +49 -0
- data/lib/chefspec/extensions/chef/lwrp_base.rb +29 -0
- data/lib/chefspec/extensions/chef/provider.rb +39 -0
- data/lib/chefspec/extensions/chef/resource/freebsd_package.rb +17 -0
- data/lib/chefspec/extensions/chef/resource.rb +188 -0
- data/lib/chefspec/extensions/chef/run_context/cookbook_compiler.rb +84 -0
- data/lib/chefspec/extensions/chef/securable.rb +19 -0
- data/lib/chefspec/extensions/ohai/system.rb +11 -0
- data/lib/chefspec/extensions.rb +21 -0
- data/lib/chefspec/file_cache_path_proxy.rb +15 -0
- data/lib/chefspec/formatter.rb +282 -0
- data/lib/chefspec/librarian.rb +51 -0
- data/lib/chefspec/matchers/do_nothing_matcher.rb +52 -0
- data/lib/chefspec/matchers/include_any_recipe_matcher.rb +51 -0
- data/lib/chefspec/matchers/include_recipe_matcher.rb +46 -0
- data/lib/chefspec/matchers/link_to_matcher.rb +37 -0
- data/lib/chefspec/matchers/notifications_matcher.rb +143 -0
- data/lib/chefspec/matchers/render_file_matcher.rb +140 -0
- data/lib/chefspec/matchers/resource_matcher.rb +175 -0
- data/lib/chefspec/matchers/state_attrs_matcher.rb +71 -0
- data/lib/chefspec/matchers/subscribes_matcher.rb +72 -0
- data/lib/chefspec/matchers.rb +13 -0
- data/lib/chefspec/mixins/normalize.rb +22 -0
- data/lib/chefspec/policyfile.rb +69 -0
- data/lib/chefspec/renderer.rb +145 -0
- data/lib/chefspec/rspec.rb +21 -0
- data/lib/chefspec/runner.rb +8 -0
- data/lib/chefspec/server.rb +4 -0
- data/lib/chefspec/server_methods.rb +173 -0
- data/lib/chefspec/server_runner.rb +76 -0
- data/lib/chefspec/solo_runner.rb +516 -0
- data/lib/chefspec/stubs/command_registry.rb +11 -0
- data/lib/chefspec/stubs/command_stub.rb +37 -0
- data/lib/chefspec/stubs/data_bag_item_registry.rb +13 -0
- data/lib/chefspec/stubs/data_bag_item_stub.rb +25 -0
- data/lib/chefspec/stubs/data_bag_registry.rb +13 -0
- data/lib/chefspec/stubs/data_bag_stub.rb +23 -0
- data/lib/chefspec/stubs/registry.rb +32 -0
- data/lib/chefspec/stubs/search_registry.rb +13 -0
- data/lib/chefspec/stubs/search_stub.rb +25 -0
- data/lib/chefspec/stubs/stub.rb +38 -0
- data/lib/chefspec/util.rb +58 -0
- data/lib/chefspec/version.rb +3 -0
- data/lib/chefspec/zero_server.rb +142 -0
- data/lib/chefspec.rb +75 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/hash.rb +35 -0
- data/spec/unit/cacher_spec.rb +70 -0
- data/spec/unit/coverage/filters_spec.rb +60 -0
- data/spec/unit/deprecations_spec.rb +52 -0
- data/spec/unit/errors_spec.rb +57 -0
- data/spec/unit/expect_exception_spec.rb +32 -0
- data/spec/unit/macros_spec.rb +119 -0
- data/spec/unit/matchers/do_nothing_matcher.rb +5 -0
- data/spec/unit/matchers/include_any_recipe_matcher_spec.rb +52 -0
- data/spec/unit/matchers/include_recipe_matcher_spec.rb +38 -0
- data/spec/unit/matchers/link_to_matcher_spec.rb +55 -0
- data/spec/unit/matchers/notifications_matcher_spec.rb +39 -0
- data/spec/unit/matchers/render_file_matcher_spec.rb +68 -0
- data/spec/unit/matchers/resource_matcher_spec.rb +5 -0
- data/spec/unit/matchers/state_attrs_matcher_spec.rb +68 -0
- data/spec/unit/matchers/subscribes_matcher_spec.rb +63 -0
- data/spec/unit/renderer_spec.rb +69 -0
- data/spec/unit/server_runner_spec.rb +28 -0
- data/spec/unit/solo_runner_spec.rb +171 -0
- data/spec/unit/stubs/command_registry_spec.rb +27 -0
- data/spec/unit/stubs/command_stub_spec.rb +61 -0
- data/spec/unit/stubs/data_bag_item_registry_spec.rb +39 -0
- data/spec/unit/stubs/data_bag_item_stub_spec.rb +36 -0
- data/spec/unit/stubs/data_bag_registry_spec.rb +39 -0
- data/spec/unit/stubs/data_bag_stub_spec.rb +35 -0
- data/spec/unit/stubs/registry_spec.rb +29 -0
- data/spec/unit/stubs/search_registry_spec.rb +39 -0
- data/spec/unit/stubs/search_stub_spec.rb +36 -0
- data/spec/unit/stubs/stub_spec.rb +64 -0
- data/templates/coverage/human.erb +22 -0
- data/templates/coverage/json.erb +8 -0
- data/templates/coverage/table.erb +14 -0
- data/templates/errors/cookbook_path_not_found.erb +3 -0
- data/templates/errors/erb_template_parse_error.erb +5 -0
- data/templates/errors/gem_load_error.erb +7 -0
- data/templates/errors/invalid_berkshelf_options.erb +4 -0
- data/templates/errors/may_need_to_specify_platform.erb +25 -0
- data/templates/errors/no_conversion_error.erb +1 -0
- data/templates/errors/not_stubbed.erb +7 -0
- data/templates/errors/shell_out_not_stubbed.erb +10 -0
- data/templates/errors/template_not_found.erb +9 -0
- metadata +221 -0
@@ -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("../../..", __dir__)])
|
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(/5.4.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
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ChefSpec::Stubs::DataBagStub 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 and block" do
|
10
|
+
block = Proc.new {}
|
11
|
+
stub = described_class.new("bag", &block)
|
12
|
+
|
13
|
+
expect(stub.bag).to eq("bag")
|
14
|
+
expect(stub.block).to eq(block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#signature" do
|
19
|
+
context "when a value is given" do
|
20
|
+
subject { described_class.new("bag").and_return(false) }
|
21
|
+
|
22
|
+
it "includes the value" do
|
23
|
+
expect(subject.signature).to eq('stub_data_bag("bag").and_return(false)')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "when a block is given" do
|
28
|
+
subject { described_class.new("bag") { 1 == 2 } }
|
29
|
+
|
30
|
+
it "includes a comment about the block" do
|
31
|
+
expect(subject.signature).to eq('stub_data_bag("bag") { # Ruby code }')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ChefSpec::Stubs::Registry do
|
4
|
+
before { described_class.reset! }
|
5
|
+
|
6
|
+
describe "#reset!" do
|
7
|
+
it "cleans the list of stubs" do
|
8
|
+
described_class.stubs = [1, 2, 3]
|
9
|
+
described_class.reset!
|
10
|
+
|
11
|
+
expect(described_class.stubs).to be_empty
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#register" do
|
16
|
+
it "adds the stub to the registry" do
|
17
|
+
described_class.register("bacon")
|
18
|
+
expect(described_class.stubs).to include("bacon")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#stub_for" do
|
23
|
+
it "finds a stub by name" do
|
24
|
+
expect {
|
25
|
+
described_class.stub_for
|
26
|
+
}.to raise_error(ArgumentError)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ChefSpec::Stubs::SearchRegistry 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", type: "node", query: "*:*")
|
13
|
+
described_class.register(search)
|
14
|
+
expect(described_class.stub_for("node", "*:*")).to eq(search)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "finds a stub by Regex" do
|
18
|
+
search = double("search", type: "node", query: /name:(.+)/)
|
19
|
+
described_class.register(search)
|
20
|
+
expect(described_class.stub_for("node", "name:example.com")).to eq(search)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "matches when the type is Symbol" do
|
24
|
+
search = double("search", type: :node, query: "*:*")
|
25
|
+
described_class.register(search)
|
26
|
+
expect(described_class.stub_for("node", "*:*")).to eq(search)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "matches when the stub is Symbol" do
|
30
|
+
search = double("search", type: "node", query: "*:*")
|
31
|
+
described_class.register(search)
|
32
|
+
expect(described_class.stub_for(:node, "*:*")).to eq(search)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "returns nil when no searches are matched" do
|
36
|
+
expect(described_class.stub_for("node", "name:example.com")).to eq(nil)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ChefSpec::Stubs::SearchStub 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 type, query, and block" do
|
10
|
+
block = Proc.new {}
|
11
|
+
stub = described_class.new("type", "query", &block)
|
12
|
+
|
13
|
+
expect(stub.type).to eq("type")
|
14
|
+
expect(stub.query).to eq("query")
|
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("type", "query").and_return(false) }
|
22
|
+
|
23
|
+
it "includes the value" do
|
24
|
+
expect(subject.signature).to eq('stub_search("type", "query").and_return(false)')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when a block is given" do
|
29
|
+
subject { described_class.new("type", "query") { 1 == 2 } }
|
30
|
+
|
31
|
+
it "includes a comment about the block" do
|
32
|
+
expect(subject.signature).to eq('stub_search("type", "query") { # Ruby code }')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ChefSpec::Stubs::Stub do
|
4
|
+
describe "#and_return" do
|
5
|
+
subject { described_class.new.and_return("value") }
|
6
|
+
|
7
|
+
it "sets the value" do
|
8
|
+
expect(subject.value).to eq("value")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns an instance of the stub" do
|
12
|
+
expect(subject.and_return("value")).to be(subject)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#and_raise" do
|
17
|
+
subject { described_class.new.and_raise(ArgumentError) }
|
18
|
+
|
19
|
+
it "sets the block" do
|
20
|
+
expect(subject.instance_variable_get(:@block)).to be_a(Proc)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns an instance of the stub" do
|
24
|
+
expect(subject.and_raise(ArgumentError)).to be(subject)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#result" do
|
29
|
+
context "when a value is given" do
|
30
|
+
subject { described_class.new.and_return("value") }
|
31
|
+
|
32
|
+
it "returns the value" do
|
33
|
+
expect(subject.result).to eq("value")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "when a block is given" do
|
38
|
+
subject { described_class.new }
|
39
|
+
|
40
|
+
it "calls the block" do
|
41
|
+
subject.instance_variable_set(:@block, Proc.new { 1 == 2 })
|
42
|
+
expect(subject.result).to eq(false)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "when an exception block is given" do
|
47
|
+
subject { described_class.new.and_raise(ArgumentError) }
|
48
|
+
|
49
|
+
it "raises the exception" do
|
50
|
+
expect {
|
51
|
+
subject.result
|
52
|
+
}.to raise_error(ArgumentError)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "when the value is a Hash" do
|
57
|
+
subject { described_class.new.and_return([ { name: "a" } ]) }
|
58
|
+
|
59
|
+
it "recursively mashifies the value" do
|
60
|
+
expect(subject.result.first).to be_a(Mash)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
|
2
|
+
<% if @total == 0 %>
|
3
|
+
No Chef resources found, skipping coverage calculation...
|
4
|
+
<% else %>
|
5
|
+
ChefSpec Coverage report generated...
|
6
|
+
|
7
|
+
Total Resources: <%= @total %>
|
8
|
+
Touched Resources: <%= @touched %>
|
9
|
+
Touch Coverage: <%= @coverage %>%
|
10
|
+
|
11
|
+
<% if @untouched_resources.empty? %>
|
12
|
+
You are awesome and so is your test coverage! Have a fantastic day!
|
13
|
+
|
14
|
+
<% else %>
|
15
|
+
Untouched Resources:
|
16
|
+
|
17
|
+
<% @untouched_resources.each do |resource| %>
|
18
|
+
<%= resource.to_s.ljust(32) %> <%= resource.source_file %>:<%= resource.source_line %>
|
19
|
+
<% end %>
|
20
|
+
|
21
|
+
<% end %>
|
22
|
+
<% end %>
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<%='Touched'.to_s.ljust(8) %> <%='Resource'.to_s.ljust(48) %> File:Line
|
2
|
+
<% @all_resources.each do |resource| %>
|
3
|
+
<%=resource.touched?.to_s.ljust(8) %> <%= resource.to_s.ljust(48) %> <%= resource.source_file %>:<%= resource.source_line %>
|
4
|
+
<% end %>
|
5
|
+
|
6
|
+
<% if @total == 0 %>
|
7
|
+
No Chef resources found, skipping coverage calculation...
|
8
|
+
<% else %>
|
9
|
+
ChefSpec Coverage report generated...
|
10
|
+
|
11
|
+
Total Resources: <%= @total %>
|
12
|
+
Touched Resources: <%= @touched %>
|
13
|
+
Touch Coverage: <%= @coverage %>%
|
14
|
+
<% end %>
|
@@ -0,0 +1,7 @@
|
|
1
|
+
I could not load the '<%= @name %>' gem! You must have the gem installed
|
2
|
+
on your local system before you can use the <%= @gem %> plugin.
|
3
|
+
You can install <%= @gem %> by running:
|
4
|
+
|
5
|
+
gem install <%= @gem %>
|
6
|
+
|
7
|
+
or add <%= @name %> to your Gemfile and run the `bundle` command to install.
|