chefspec-chef 9.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +30 -0
  3. data/LICENSE +22 -0
  4. data/Rakefile +85 -0
  5. data/chefspec-chef.gemspec +30 -0
  6. data/lib/chefspec/api/core.rb +217 -0
  7. data/lib/chefspec/api/described.rb +53 -0
  8. data/lib/chefspec/api/do_nothing.rb +26 -0
  9. data/lib/chefspec/api/include_any_recipe.rb +24 -0
  10. data/lib/chefspec/api/include_recipe.rb +28 -0
  11. data/lib/chefspec/api/link.rb +28 -0
  12. data/lib/chefspec/api/notifications.rb +40 -0
  13. data/lib/chefspec/api/reboot.rb +14 -0
  14. data/lib/chefspec/api/render_file.rb +37 -0
  15. data/lib/chefspec/api/state_attrs.rb +30 -0
  16. data/lib/chefspec/api/stubs.rb +183 -0
  17. data/lib/chefspec/api/stubs_for.rb +139 -0
  18. data/lib/chefspec/api/subscriptions.rb +37 -0
  19. data/lib/chefspec/api/user.rb +230 -0
  20. data/lib/chefspec/api.rb +39 -0
  21. data/lib/chefspec/berkshelf.rb +63 -0
  22. data/lib/chefspec/cacher.rb +64 -0
  23. data/lib/chefspec/coverage/filters.rb +82 -0
  24. data/lib/chefspec/coverage.rb +247 -0
  25. data/lib/chefspec/deprecations.rb +46 -0
  26. data/lib/chefspec/errors.rb +48 -0
  27. data/lib/chefspec/expect_exception.rb +51 -0
  28. data/lib/chefspec/extensions/chef/client.rb +21 -0
  29. data/lib/chefspec/extensions/chef/conditional.rb +16 -0
  30. data/lib/chefspec/extensions/chef/cookbook/gem_installer.rb +33 -0
  31. data/lib/chefspec/extensions/chef/cookbook_loader.rb +14 -0
  32. data/lib/chefspec/extensions/chef/cookbook_uploader.rb +12 -0
  33. data/lib/chefspec/extensions/chef/data_query.rb +49 -0
  34. data/lib/chefspec/extensions/chef/lwrp_base.rb +29 -0
  35. data/lib/chefspec/extensions/chef/provider.rb +39 -0
  36. data/lib/chefspec/extensions/chef/resource/freebsd_package.rb +17 -0
  37. data/lib/chefspec/extensions/chef/resource.rb +188 -0
  38. data/lib/chefspec/extensions/chef/run_context/cookbook_compiler.rb +84 -0
  39. data/lib/chefspec/extensions/chef/securable.rb +19 -0
  40. data/lib/chefspec/extensions/ohai/system.rb +11 -0
  41. data/lib/chefspec/extensions.rb +21 -0
  42. data/lib/chefspec/file_cache_path_proxy.rb +15 -0
  43. data/lib/chefspec/formatter.rb +282 -0
  44. data/lib/chefspec/librarian.rb +51 -0
  45. data/lib/chefspec/matchers/do_nothing_matcher.rb +52 -0
  46. data/lib/chefspec/matchers/include_any_recipe_matcher.rb +51 -0
  47. data/lib/chefspec/matchers/include_recipe_matcher.rb +46 -0
  48. data/lib/chefspec/matchers/link_to_matcher.rb +37 -0
  49. data/lib/chefspec/matchers/notifications_matcher.rb +143 -0
  50. data/lib/chefspec/matchers/render_file_matcher.rb +140 -0
  51. data/lib/chefspec/matchers/resource_matcher.rb +175 -0
  52. data/lib/chefspec/matchers/state_attrs_matcher.rb +71 -0
  53. data/lib/chefspec/matchers/subscribes_matcher.rb +72 -0
  54. data/lib/chefspec/matchers.rb +13 -0
  55. data/lib/chefspec/mixins/normalize.rb +22 -0
  56. data/lib/chefspec/policyfile.rb +69 -0
  57. data/lib/chefspec/renderer.rb +145 -0
  58. data/lib/chefspec/rspec.rb +21 -0
  59. data/lib/chefspec/runner.rb +8 -0
  60. data/lib/chefspec/server.rb +4 -0
  61. data/lib/chefspec/server_methods.rb +173 -0
  62. data/lib/chefspec/server_runner.rb +76 -0
  63. data/lib/chefspec/solo_runner.rb +516 -0
  64. data/lib/chefspec/stubs/command_registry.rb +11 -0
  65. data/lib/chefspec/stubs/command_stub.rb +37 -0
  66. data/lib/chefspec/stubs/data_bag_item_registry.rb +13 -0
  67. data/lib/chefspec/stubs/data_bag_item_stub.rb +25 -0
  68. data/lib/chefspec/stubs/data_bag_registry.rb +13 -0
  69. data/lib/chefspec/stubs/data_bag_stub.rb +23 -0
  70. data/lib/chefspec/stubs/registry.rb +32 -0
  71. data/lib/chefspec/stubs/search_registry.rb +13 -0
  72. data/lib/chefspec/stubs/search_stub.rb +25 -0
  73. data/lib/chefspec/stubs/stub.rb +38 -0
  74. data/lib/chefspec/util.rb +58 -0
  75. data/lib/chefspec/version.rb +3 -0
  76. data/lib/chefspec/zero_server.rb +142 -0
  77. data/lib/chefspec.rb +75 -0
  78. data/spec/spec_helper.rb +12 -0
  79. data/spec/support/hash.rb +35 -0
  80. data/spec/unit/cacher_spec.rb +70 -0
  81. data/spec/unit/coverage/filters_spec.rb +60 -0
  82. data/spec/unit/deprecations_spec.rb +52 -0
  83. data/spec/unit/errors_spec.rb +57 -0
  84. data/spec/unit/expect_exception_spec.rb +32 -0
  85. data/spec/unit/macros_spec.rb +119 -0
  86. data/spec/unit/matchers/do_nothing_matcher.rb +5 -0
  87. data/spec/unit/matchers/include_any_recipe_matcher_spec.rb +52 -0
  88. data/spec/unit/matchers/include_recipe_matcher_spec.rb +38 -0
  89. data/spec/unit/matchers/link_to_matcher_spec.rb +55 -0
  90. data/spec/unit/matchers/notifications_matcher_spec.rb +39 -0
  91. data/spec/unit/matchers/render_file_matcher_spec.rb +68 -0
  92. data/spec/unit/matchers/resource_matcher_spec.rb +5 -0
  93. data/spec/unit/matchers/state_attrs_matcher_spec.rb +68 -0
  94. data/spec/unit/matchers/subscribes_matcher_spec.rb +63 -0
  95. data/spec/unit/renderer_spec.rb +69 -0
  96. data/spec/unit/server_runner_spec.rb +28 -0
  97. data/spec/unit/solo_runner_spec.rb +171 -0
  98. data/spec/unit/stubs/command_registry_spec.rb +27 -0
  99. data/spec/unit/stubs/command_stub_spec.rb +61 -0
  100. data/spec/unit/stubs/data_bag_item_registry_spec.rb +39 -0
  101. data/spec/unit/stubs/data_bag_item_stub_spec.rb +36 -0
  102. data/spec/unit/stubs/data_bag_registry_spec.rb +39 -0
  103. data/spec/unit/stubs/data_bag_stub_spec.rb +35 -0
  104. data/spec/unit/stubs/registry_spec.rb +29 -0
  105. data/spec/unit/stubs/search_registry_spec.rb +39 -0
  106. data/spec/unit/stubs/search_stub_spec.rb +36 -0
  107. data/spec/unit/stubs/stub_spec.rb +64 -0
  108. data/templates/coverage/human.erb +22 -0
  109. data/templates/coverage/json.erb +8 -0
  110. data/templates/coverage/table.erb +14 -0
  111. data/templates/errors/cookbook_path_not_found.erb +3 -0
  112. data/templates/errors/erb_template_parse_error.erb +5 -0
  113. data/templates/errors/gem_load_error.erb +7 -0
  114. data/templates/errors/invalid_berkshelf_options.erb +4 -0
  115. data/templates/errors/may_need_to_specify_platform.erb +25 -0
  116. data/templates/errors/no_conversion_error.erb +1 -0
  117. data/templates/errors/not_stubbed.erb +7 -0
  118. data/templates/errors/shell_out_not_stubbed.erb +10 -0
  119. data/templates/errors/template_not_found.erb +9 -0
  120. metadata +221 -0
@@ -0,0 +1,119 @@
1
+ require "spec_helper"
2
+
3
+ describe ChefSpec::API::Stubs do
4
+ describe "#stub_command" do
5
+ let(:command_stub) { double("command") }
6
+
7
+ it "adds the command to the command registry" do
8
+ allow(ChefSpec::Stubs::CommandStub).to receive(:new).and_return(command_stub)
9
+ stub_command('echo "hello"')
10
+
11
+ expect(ChefSpec::Stubs::CommandRegistry.stubs).to include(command_stub)
12
+ end
13
+ end
14
+
15
+ describe "#stub_search" do
16
+ let(:search_stub) { double("search") }
17
+
18
+ it "adds the query to the search registry" do
19
+ allow(ChefSpec::Stubs::SearchStub).to receive(:new).and_return(search_stub)
20
+ stub_search(:node, "*:*")
21
+
22
+ expect(ChefSpec::Stubs::SearchRegistry.stubs).to include(search_stub)
23
+ end
24
+ end
25
+
26
+ describe "#stub_data_bag" do
27
+ let(:data_bag_stub) { double("data_bag") }
28
+
29
+ it "adds the query to the data_bag registry" do
30
+ allow(ChefSpec::Stubs::DataBagStub).to receive(:new).and_return(data_bag_stub)
31
+ stub_data_bag(:users)
32
+
33
+ expect(ChefSpec::Stubs::DataBagRegistry.stubs).to include(data_bag_stub)
34
+ end
35
+ end
36
+
37
+ describe "#stub_data_bag_item" do
38
+ let(:data_bag_item_stub) { double("data_bag_item") }
39
+
40
+ it "adds the query to the data_bag_item registry" do
41
+ allow(ChefSpec::Stubs::DataBagItemStub).to receive(:new).and_return(data_bag_item_stub)
42
+ stub_data_bag_item(:users, "id")
43
+
44
+ expect(ChefSpec::Stubs::DataBagItemRegistry.stubs).to include(data_bag_item_stub)
45
+ end
46
+ end
47
+
48
+ describe "#stub_node" do
49
+ it "returns a Chef::Node" do
50
+ expect(stub_node).to be_a(Chef::Node)
51
+ end
52
+
53
+ it "defaults the node name to `node.example`" do
54
+ node = stub_node
55
+ expect(node.name).to eq("node.example")
56
+ end
57
+
58
+ it "sets the node name when given" do
59
+ node = stub_node("example.com")
60
+ expect(node.name).to eq("example.com")
61
+ end
62
+
63
+ it "sets the automatic attributes" do
64
+ node = stub_node
65
+ expect(node.automatic).to eq(Fauxhai.mock.data)
66
+ end
67
+
68
+ it "sets the automatic attributes with ohai overrides" do
69
+ node = stub_node("node.example", ohai: { ipaddress: "1.2.3.4" })
70
+ expect(node["ipaddress"]).to eq("1.2.3.4")
71
+ end
72
+
73
+ it "sets the automatic attributes for a specific platform and version" do
74
+ node = stub_node("node.example", platform: "ubuntu", version: "18.04")
75
+ expect(node.automatic).to eq(Fauxhai.mock(platform: "ubuntu", version: "18.04").data)
76
+ end
77
+
78
+ it "sets the automatic attributes from a JSON data path" do
79
+ allow(File).to receive(:exist?).with("/path/to/json").and_return(true)
80
+ allow(File).to receive(:read).with("/path/to/json").and_return('{ "ipaddress": "1.2.3.4" }')
81
+ node = stub_node("node.example", path: "/path/to/json")
82
+ expect(node["ipaddress"]).to eq("1.2.3.4")
83
+ end
84
+
85
+ it "yields a block" do
86
+ expect { |block| stub_node(&block) }.to yield_with_args(Chef::Node)
87
+ end
88
+ end
89
+ end
90
+
91
+ describe "nginx::source" do
92
+ describe "#described_cookbook" do
93
+ describe "nginx::source" do
94
+ it "returns the name of the cookbook" do
95
+ expect(described_cookbook).to eq("nginx")
96
+ end
97
+
98
+ context "in a nested context" do
99
+ it "still returns the name of the cookbook" do
100
+ expect(described_cookbook).to eq("nginx")
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ describe "#described_recipe" do
107
+ describe "nginx::source" do
108
+ it "returns the cookbook::recipe" do
109
+ expect(described_recipe).to eq("nginx::source")
110
+ end
111
+
112
+ context "in a nested context" do
113
+ it "still retrns the cookbook::recipe" do
114
+ expect(described_recipe).to eq("nginx::source")
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,5 @@
1
+ require "spec_helper"
2
+
3
+ describe ChefSpec::Matchers::DoNothingMatcher do
4
+ pending
5
+ end
@@ -0,0 +1,52 @@
1
+ require "spec_helper"
2
+
3
+ describe ChefSpec::Matchers::IncludeAnyRecipeMatcher do
4
+ let(:node) { double("Chef::Node", run_list: run_list) }
5
+ let(:run_list) { double("Chef::RunList", run_list_items: run_list_items) }
6
+ let(:run_list_items) { [run_list_item_one] }
7
+ let(:run_list_item_one) { double("Chef::RunList::RunListItem", name: "one") }
8
+ let(:run_list_item_two) { double("Chef::RunList::RunListItem", name: "two") }
9
+ let(:loaded_recipes) { %w{one} }
10
+ let(:chef_run) { double("chef run", run_context: { loaded_recipes: loaded_recipes, node: node }) }
11
+
12
+ subject { described_class.new }
13
+
14
+ describe "#failure_message" do
15
+ it "has the right value" do
16
+ subject.matches?(chef_run)
17
+ expect(subject.failure_message).to eq("expected to include any recipe")
18
+ end
19
+ end
20
+
21
+ describe "#failure_message_when_negated" do
22
+ it "has the right value" do
23
+ subject.matches?(chef_run)
24
+ expect(subject.failure_message_when_negated).to eq("expected not to include any recipes")
25
+ end
26
+ end
27
+
28
+ describe "#description" do
29
+ it "has the right value" do
30
+ subject.matches?(chef_run)
31
+ expect(subject.description).to eq("include any recipe")
32
+ end
33
+ end
34
+
35
+ describe "#matches?" do
36
+ context "when 0 recipes are included" do
37
+ let(:loaded_recipes) { %w{one} }
38
+
39
+ it "returns false" do
40
+ expect(subject.matches?(chef_run)).to be false
41
+ end
42
+ end
43
+
44
+ context "when at least one recipe is included" do
45
+ let(:loaded_recipes) { %w{one two} }
46
+
47
+ it "returns true" do
48
+ expect(subject.matches?(chef_run)).to be true
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,38 @@
1
+ require "spec_helper"
2
+
3
+ describe ChefSpec::Matchers::IncludeRecipeMatcher do
4
+ let(:chef_run) { double("chef run", run_context: { loaded_recipes: %w{one two three} }) }
5
+ subject { described_class.new("one::default") }
6
+
7
+ describe "#failure_message" do
8
+ it "has the right value" do
9
+ subject.matches?(chef_run)
10
+ expect(subject.failure_message)
11
+ .to eq(%q{expected ["one::default", "two::default", "three::default"] to include "one::default"})
12
+ end
13
+ end
14
+
15
+ describe "#failure_message_when_negated" do
16
+ it "has the right value" do
17
+ subject.matches?(chef_run)
18
+ expect(subject.failure_message_when_negated)
19
+ .to eq(%q{expected "one::default" to not be included})
20
+ end
21
+ end
22
+
23
+ describe "#description" do
24
+ it "has the right value" do
25
+ subject.matches?(chef_run)
26
+ expect(subject.description).to eq(%q{include recipe "one::default"})
27
+ end
28
+ end
29
+
30
+ it "matches when the recipe is included" do
31
+ expect(subject.matches?(chef_run)).to be_truthy
32
+ end
33
+
34
+ it "does not match when the recipe is not included" do
35
+ failure = described_class.new("nope")
36
+ expect(failure.matches?(chef_run)).to be_falsy
37
+ end
38
+ end
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+
3
+ describe ChefSpec::Matchers::LinkToMatcher do
4
+ let(:from) { "/var/www" }
5
+ let(:to) { "/var/html" }
6
+ let(:link) do
7
+ Chef::Resource::Link.new(from).tap do |link|
8
+ link.to(to)
9
+ link.perform_action(:create)
10
+ end
11
+ end
12
+ subject { described_class.new(to) }
13
+
14
+ describe "#failure_message" do
15
+ it "has the right value" do
16
+ subject.matches?(link)
17
+ expect(subject.failure_message)
18
+ .to eq(%Q{expected "link[#{from}]" to link to "#{to}" but was "#{to}"})
19
+ end
20
+ end
21
+
22
+ describe "#failure_message_when_negated" do
23
+ it "has the right value" do
24
+ subject.matches?(link)
25
+ expect(subject.failure_message_when_negated)
26
+ .to eq(%Q{expected "link[#{from}]" to not link to "#{to}"})
27
+ end
28
+ end
29
+
30
+ describe "#description" do
31
+ it "has the right value" do
32
+ subject.matches?(link)
33
+ expect(subject.description).to eq(%Q{link to "#{to}"})
34
+ end
35
+ end
36
+
37
+ context "when the link is correct" do
38
+ it "matches" do
39
+ expect(subject.matches?(link)).to be_truthy
40
+ end
41
+
42
+ it "adds the link to the coverage report" do
43
+ expect(ChefSpec::Coverage).to receive(:cover!).with(link)
44
+ subject.matches?(link)
45
+ end
46
+ end
47
+
48
+ context "when the link is not correct" do
49
+ subject { described_class.new("/nope/bad/path/bro") }
50
+
51
+ it "does not match" do
52
+ expect(subject.matches?(link)).to be_falsy
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+
3
+ describe ChefSpec::Matchers::NotificationsMatcher do
4
+ subject { described_class.new("execute[install]") }
5
+ let(:package) do
6
+ double("package",
7
+ name: "package",
8
+ to_s: "package[foo]",
9
+ is_a?: true,
10
+ performed_action?: true,
11
+ immediate_notifications: [],
12
+ delayed_notifications: [],
13
+ before_notifications: [])
14
+ end
15
+
16
+ describe "#failure_message" do
17
+ it "has the right value" do
18
+ subject.matches?(package)
19
+ expect(subject.failure_message)
20
+ .to include %{expected "package[foo]" to notify "execute[install]", but did not.}
21
+ end
22
+ end
23
+
24
+ describe "#failure_message_when_negated" do
25
+ it "has the right value" do
26
+ subject.matches?(package)
27
+ expect(subject.failure_message_when_negated)
28
+ .to eq %{expected "package[foo]" to not notify "execute[install]", but it did.}
29
+ end
30
+ end
31
+
32
+ describe "#description" do
33
+ it "has the right value" do
34
+ subject.matches?(package)
35
+ expect(subject.description)
36
+ .to eq %{notify "execute[install]"}
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+
3
+ describe ChefSpec::Matchers::RenderFileMatcher do
4
+ let(:path) { "/tmp/thing" }
5
+ let(:file) { double("file", to: path, to_s: "file[#{path}]", performed_action?: true) }
6
+ let(:chef_run) { double("chef run", find_resource: file) }
7
+ subject { described_class.new(path) }
8
+
9
+ describe "#with_content" do
10
+ it "accepts do/end syntax" do
11
+ subject.matches?(chef_run)
12
+ expect(
13
+ subject.with_content do |content|
14
+ "Does not raise ArgumentError"
15
+ end.expected_content.first.call
16
+ ).to eq("Does not raise ArgumentError")
17
+ end
18
+ end
19
+
20
+ describe "#failure_message" do
21
+ it "has the right value" do
22
+ subject.matches?(chef_run)
23
+ expect(subject.failure_message)
24
+ .to eq(%Q{expected Chef run to render "#{path}"})
25
+ end
26
+ end
27
+
28
+ describe "#failure_message_when_negated" do
29
+ it "has the right value" do
30
+ subject.matches?(chef_run)
31
+ expect(subject.failure_message_when_negated)
32
+ .to eq(%Q{expected file "#{path}" to not be in Chef run})
33
+ end
34
+ end
35
+
36
+ describe "#description" do
37
+ it "has the right value" do
38
+ subject.matches?(chef_run)
39
+ expect(subject.description).to eq(%Q{render file "#{path}"})
40
+ end
41
+
42
+ it "has the right value when with_content is chained" do
43
+ subject.matches?(chef_run)
44
+ expect(
45
+ subject.with_content("foo").with_content("bar").description
46
+ ).to eq(%Q{render file "#{path}" with content "foo" with content "bar"})
47
+ end
48
+ end
49
+
50
+ context "when the file is correct" do
51
+ it "matches" do
52
+ expect(subject.matches?(chef_run)).to be_truthy
53
+ end
54
+
55
+ it "adds the resource to the coverage report" do
56
+ expect(ChefSpec::Coverage).to receive(:cover!).with(file)
57
+ subject.matches?(chef_run)
58
+ end
59
+ end
60
+
61
+ context "when the file is not correct" do
62
+ it "does not match" do
63
+ allow(chef_run).to receive(:find_resource).and_return(nil)
64
+ failure = described_class.new("nope")
65
+ expect(failure.matches?(chef_run)).to be_falsy
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,5 @@
1
+ require "spec_helper"
2
+
3
+ describe ChefSpec::Matchers::ResourceMatcher do
4
+ pending
5
+ end
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+
3
+ describe ChefSpec::Matchers::StateAttrsMatcher do
4
+ subject { described_class.new(%i{a b}) }
5
+
6
+ context "when the resource does not exist" do
7
+ let(:resource) { nil }
8
+ before { subject.matches?(resource) }
9
+
10
+ it "does not match" do
11
+ expect(subject).to_not be_matches(resource)
12
+ end
13
+
14
+ it "has the correct description" do
15
+ expect(subject.description).to eq("have state attributes [:a, :b]")
16
+ end
17
+
18
+ it "has the correct failure message for should" do
19
+ expect(subject.failure_message).to include <<-EOH.gsub(/^ {8}/, "")
20
+ expected _something_ to have state attributes, but the _something_ you gave me was nil!
21
+ Ensure the resource exists before making assertions:
22
+
23
+ expect(resource).to be
24
+ EOH
25
+ end
26
+
27
+ it "has the correct failure message for should not" do
28
+ expect(subject.failure_message_when_negated).to include <<-EOH.gsub(/^ {8}/, "")
29
+ expected _something_ to not have state attributes, but the _something_ you gave me was nil!
30
+ Ensure the resource exists before making assertions:
31
+
32
+ expect(resource).to be
33
+ EOH
34
+ end
35
+ end
36
+
37
+ context "when the resource exists" do
38
+ let(:klass) { double("class", state_attrs: %i{a b}) }
39
+ let(:resource) { double("resource", class: klass) }
40
+ before { subject.matches?(resource) }
41
+
42
+ it "has the correct description" do
43
+ expect(subject.description).to eq("have state attributes [:a, :b]")
44
+ end
45
+
46
+ it "has the correct failure message for should" do
47
+ expect(subject.failure_message).to eq("expected [:a, :b] to equal [:a, :b]")
48
+ end
49
+
50
+ it "has the correct failure message for should not" do
51
+ expect(subject.failure_message_when_negated).to eq("expected [:a, :b] to not equal [:a, :b]")
52
+ end
53
+
54
+ it "matches when the state attributes are correct" do
55
+ expect(subject).to be_matches(resource)
56
+ end
57
+
58
+ it "does not match when the state attributes are incorrect" do
59
+ allow(klass).to receive(:state_attrs).and_return(%i{c d})
60
+ expect(subject).to_not be_matches(resource)
61
+ end
62
+
63
+ it "does not match when partial state attribute are incorrect" do
64
+ allow(klass).to receive(:state_attrs).and_return(%i{b c})
65
+ expect(subject).to_not be_matches(resource)
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,63 @@
1
+ require "spec_helper"
2
+
3
+ describe ChefSpec::Matchers::SubscribesMatcher do
4
+ subject { described_class.new("execute[install]") }
5
+ let(:runner) { double("runner", find_resource: nil) }
6
+ let(:run_context) { double("run_context", node: node) }
7
+ let(:node) { double("node", runner: runner) }
8
+ let(:package) do
9
+ double("package",
10
+ name: "package",
11
+ to_s: "package[foo]",
12
+ run_context: run_context)
13
+ end
14
+
15
+ context "when no resource is found" do
16
+ describe "#failure_message" do
17
+ it "has the right value" do
18
+ subject.matches?(package)
19
+ expect(subject.failure_message)
20
+ .to include %{expected _something_ to notify "package[foo]", but the _something_ you gave me was nil! If you are running a test like:}
21
+ end
22
+ end
23
+ end
24
+
25
+ context "when the resource exists" do
26
+ let(:execute) do
27
+ double("execute",
28
+ name: "execute",
29
+ to_s: "execute[install]",
30
+ immediate_notifications: [],
31
+ delayed_notifications: [],
32
+ before_notifications: [])
33
+ end
34
+
35
+ before do
36
+ allow(runner).to receive(:find_resource).and_return(execute)
37
+ end
38
+
39
+ describe "#failure_message" do
40
+ it "has the right value" do
41
+ subject.matches?(package)
42
+ expect(subject.failure_message)
43
+ .to include %{expected "execute[install]" to notify "package[foo]", but did not.}
44
+ end
45
+ end
46
+
47
+ describe "#failure_message_when_negated" do
48
+ it "has the right value" do
49
+ subject.matches?(package)
50
+ expect(subject.failure_message_when_negated)
51
+ .to eq %{expected "execute[install]" to not notify "package[foo]", but it did.}
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "#description" do
57
+ it "has the right value" do
58
+ subject.matches?(package)
59
+ expect(subject.description)
60
+ .to eq %{notify "package[foo]"}
61
+ end
62
+ end
63
+ end
@@ -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