motherbrain 0.0.0.placeholder → 0.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +9 -0
  5. data/CHANGELOG.md +196 -0
  6. data/COMMANDS.md +9 -0
  7. data/CONTRIBUTING.md +24 -0
  8. data/Dockerfile +26 -0
  9. data/Gemfile +60 -2
  10. data/Guardfile +30 -0
  11. data/LICENSE +10 -0
  12. data/MANIFESTS.md +90 -0
  13. data/OPERATORS_GUIDE.md +195 -0
  14. data/PLUGINS.md +268 -0
  15. data/README.md +304 -16
  16. data/Thorfile +123 -0
  17. data/VAGRANT.md +116 -0
  18. data/bin/boot +9 -0
  19. data/bin/mb +5 -0
  20. data/bin/mbsrv +5 -0
  21. data/config.json +32 -0
  22. data/features/cli/bootstrap_command/configurable_scripts.feature +32 -0
  23. data/features/cli/configure_command.feature +57 -0
  24. data/features/cli/environment_command/create_command.feature +22 -0
  25. data/features/cli/environment_command/destroy_command.feature +33 -0
  26. data/features/cli/environment_command/from_command.feature +29 -0
  27. data/features/cli/environment_command/list_command.feature +0 -0
  28. data/features/cli/node_limiting.feature +47 -0
  29. data/features/cli/plugin_command/list_command.feature +46 -0
  30. data/features/cli/service_command/service_command.feature +21 -0
  31. data/features/cli/template_command.feature +10 -0
  32. data/features/cli/validate_config.feature +46 -0
  33. data/features/step_definitions/bootstrap_steps.rb +57 -0
  34. data/features/step_definitions/chef_server_steps.rb +3 -0
  35. data/features/step_definitions/configuration_steps.rb +18 -0
  36. data/features/step_definitions/core_cli_steps.rb +33 -0
  37. data/features/step_definitions/environment_steps.rb +43 -0
  38. data/features/step_definitions/node_steps.rb +3 -0
  39. data/features/step_definitions/plugin_steps.rb +15 -0
  40. data/features/step_definitions/template_steps.rb +7 -0
  41. data/features/support/env.rb +68 -0
  42. data/lib/mb/api.rb +8 -0
  43. data/lib/mb/api/application.rb +7 -0
  44. data/lib/mb/api/endpoint.rb +5 -0
  45. data/lib/mb/api/helpers.rb +38 -0
  46. data/lib/mb/api/v1.rb +56 -0
  47. data/lib/mb/api/v1/config_endpoint.rb +12 -0
  48. data/lib/mb/api/v1/environments_endpoint.rb +174 -0
  49. data/lib/mb/api/v1/jobs_endpoint.rb +31 -0
  50. data/lib/mb/api/v1/plugins_endpoint.rb +90 -0
  51. data/lib/mb/api/validators.rb +5 -0
  52. data/lib/mb/api/validators/sem_ver.rb +18 -0
  53. data/lib/mb/application.rb +148 -0
  54. data/lib/mb/berkshelf.rb +50 -0
  55. data/lib/mb/bootstrap.rb +9 -0
  56. data/lib/mb/bootstrap/manager.rb +250 -0
  57. data/lib/mb/bootstrap/manifest.rb +131 -0
  58. data/lib/mb/bootstrap/routine.rb +199 -0
  59. data/lib/mb/bootstrap/template.rb +73 -0
  60. data/lib/mb/bootstrap/worker.rb +227 -0
  61. data/lib/mb/chef.rb +6 -0
  62. data/lib/mb/chef/config.rb +69 -0
  63. data/lib/mb/chef/run_list_item.rb +115 -0
  64. data/lib/mb/chef_mutex.rb +304 -0
  65. data/lib/mb/clean_room_base.rb +39 -0
  66. data/lib/mb/cli.rb +50 -0
  67. data/lib/mb/cli/base.rb +51 -0
  68. data/lib/mb/cli/shell.rb +29 -0
  69. data/lib/mb/cli/shell/basic.rb +11 -0
  70. data/lib/mb/cli/shell/color.rb +11 -0
  71. data/lib/mb/cli/shell/ext.rb +41 -0
  72. data/lib/mb/cli/sub_command.rb +95 -0
  73. data/lib/mb/cli/sub_command/component.rb +56 -0
  74. data/lib/mb/cli/sub_command/plugin.rb +232 -0
  75. data/lib/mb/cli_client.rb +178 -0
  76. data/lib/mb/cli_gateway.rb +426 -0
  77. data/lib/mb/cli_gateway/sub_commands.rb +3 -0
  78. data/lib/mb/cli_gateway/sub_commands/environment.rb +124 -0
  79. data/lib/mb/cli_gateway/sub_commands/plugin.rb +148 -0
  80. data/lib/mb/command.rb +88 -0
  81. data/lib/mb/command_invoker.rb +235 -0
  82. data/lib/mb/command_invoker/worker.rb +40 -0
  83. data/lib/mb/command_runner.rb +233 -0
  84. data/lib/mb/component.rb +245 -0
  85. data/lib/mb/config.rb +275 -0
  86. data/lib/mb/config_manager.rb +75 -0
  87. data/lib/mb/console.rb +35 -0
  88. data/lib/mb/cookbook_metadata.rb +73 -0
  89. data/lib/mb/core_ext.rb +3 -0
  90. data/lib/mb/core_ext/dir.rb +37 -0
  91. data/lib/mb/core_ext/enumerable.rb +48 -0
  92. data/lib/mb/core_ext/file.rb +24 -0
  93. data/lib/mb/core_ext/signal.rb +11 -0
  94. data/lib/mb/environment_manager.rb +195 -0
  95. data/lib/mb/error_handler.rb +212 -0
  96. data/lib/mb/errors.rb +693 -0
  97. data/lib/mb/file_system.rb +60 -0
  98. data/lib/mb/file_system/tempfile.rb +25 -0
  99. data/lib/mb/gear.rb +154 -0
  100. data/lib/mb/gears.rb +8 -0
  101. data/lib/mb/gears/dynamic_service.rb +218 -0
  102. data/lib/mb/gears/jmx.rb +24 -0
  103. data/lib/mb/gears/jmx/action.rb +46 -0
  104. data/lib/mb/gears/mysql.rb +20 -0
  105. data/lib/mb/gears/mysql/action.rb +190 -0
  106. data/lib/mb/gears/service.rb +163 -0
  107. data/lib/mb/gears/service/action.rb +58 -0
  108. data/lib/mb/gears/service/action_runner.rb +161 -0
  109. data/lib/mb/grape_ext.rb +3 -0
  110. data/lib/mb/grape_ext/endpoint.rb +13 -0
  111. data/lib/mb/group.rb +143 -0
  112. data/lib/mb/job.rb +183 -0
  113. data/lib/mb/job/state_machine.rb +34 -0
  114. data/lib/mb/job/states.rb +46 -0
  115. data/lib/mb/job_manager.rb +96 -0
  116. data/lib/mb/job_record.rb +67 -0
  117. data/lib/mb/job_ticket.rb +25 -0
  118. data/lib/mb/lock_manager.rb +116 -0
  119. data/lib/mb/logging.rb +134 -0
  120. data/lib/mb/logging/basic_format.rb +31 -0
  121. data/lib/mb/manifest.rb +128 -0
  122. data/lib/mb/mixin.rb +3 -0
  123. data/lib/mb/mixin/attribute_setting.rb +265 -0
  124. data/lib/mb/mixin/coded_exit.rb +49 -0
  125. data/lib/mb/mixin/locks.rb +54 -0
  126. data/lib/mb/mixin/services.rb +100 -0
  127. data/lib/mb/node_filter.rb +97 -0
  128. data/lib/mb/node_querier.rb +527 -0
  129. data/lib/mb/plugin.rb +300 -0
  130. data/lib/mb/plugin_manager.rb +589 -0
  131. data/lib/mb/provisioner.rb +186 -0
  132. data/lib/mb/provisioner/manager.rb +213 -0
  133. data/lib/mb/provisioner/manifest.rb +125 -0
  134. data/lib/mb/provisioner/provision_data.rb +96 -0
  135. data/lib/mb/provisioners.rb +5 -0
  136. data/lib/mb/provisioners/aws.rb +395 -0
  137. data/lib/mb/rest_gateway.rb +72 -0
  138. data/lib/mb/ridley_ext.rb +5 -0
  139. data/lib/mb/ridley_ext/cookbook_object.rb +15 -0
  140. data/lib/mb/srv_ctl.rb +183 -0
  141. data/lib/mb/test.rb +104 -0
  142. data/lib/mb/thor_ext.rb +49 -0
  143. data/lib/mb/upgrade.rb +6 -0
  144. data/lib/mb/upgrade/manager.rb +85 -0
  145. data/lib/mb/upgrade/worker.rb +149 -0
  146. data/lib/mb/version.rb +1 -1
  147. data/lib/motherbrain.rb +166 -2
  148. data/man/man_helper.rb +81 -0
  149. data/man/mb.1 +494 -0
  150. data/man/mb.1.html +300 -0
  151. data/man/mb.1.ronn.erb +62 -0
  152. data/motherbrain.gemspec +56 -20
  153. data/scripts/node_name.rb +14 -0
  154. data/spec/fixtures/cb_metadata.json +7 -0
  155. data/spec/fixtures/cb_metadata.rb +14 -0
  156. data/spec/fixtures/fake_id_rsa +27 -0
  157. data/spec/fixtures/fake_key.pem +27 -0
  158. data/spec/fixtures/myface-0.1.0/metadata.rb +14 -0
  159. data/spec/fixtures/myface-0.1.0/motherbrain.rb +0 -0
  160. data/spec/fixtures/test_env.json +15 -0
  161. data/spec/spec_helper.rb +67 -0
  162. data/spec/support/actor_mocking.rb +7 -0
  163. data/spec/support/berkshelf.rb +24 -0
  164. data/spec/support/chef_server.rb +102 -0
  165. data/spec/support/doubles.rb +11 -0
  166. data/spec/support/klass.rb +10 -0
  167. data/spec/support/matchers/each.rb +12 -0
  168. data/spec/support/matchers/error_codes.rb +5 -0
  169. data/spec/support/matchers/exit_codes.rb +57 -0
  170. data/spec/support/matchers/jobs.rb +11 -0
  171. data/spec/support/spec_helpers.rb +145 -0
  172. data/spec/unit/mb/api/application_spec.rb +11 -0
  173. data/spec/unit/mb/api/helpers_spec.rb +5 -0
  174. data/spec/unit/mb/api/v1/config_endpoint_spec.rb +19 -0
  175. data/spec/unit/mb/api/v1/environments_endpoint_spec.rb +71 -0
  176. data/spec/unit/mb/api/v1/jobs_endpoint_spec.rb +24 -0
  177. data/spec/unit/mb/api/v1/plugins_endpoint_spec.rb +298 -0
  178. data/spec/unit/mb/api/v1_spec.rb +37 -0
  179. data/spec/unit/mb/api/validators/sem_ver_spec.rb +5 -0
  180. data/spec/unit/mb/application_spec.rb +19 -0
  181. data/spec/unit/mb/berkshelf_spec.rb +38 -0
  182. data/spec/unit/mb/bootstrap/manager_spec.rb +347 -0
  183. data/spec/unit/mb/bootstrap/manifest_spec.rb +333 -0
  184. data/spec/unit/mb/bootstrap/routine_spec.rb +393 -0
  185. data/spec/unit/mb/bootstrap/template_spec.rb +100 -0
  186. data/spec/unit/mb/bootstrap/worker_spec.rb +194 -0
  187. data/spec/unit/mb/chef/config_spec.rb +33 -0
  188. data/spec/unit/mb/chef/run_list_item_spec.rb +34 -0
  189. data/spec/unit/mb/chef_mutex_spec.rb +314 -0
  190. data/spec/unit/mb/clean_room_base_spec.rb +31 -0
  191. data/spec/unit/mb/cli/base_spec.rb +43 -0
  192. data/spec/unit/mb/cli/shell/basic_spec.rb +5 -0
  193. data/spec/unit/mb/cli/shell/color_spec.rb +5 -0
  194. data/spec/unit/mb/cli/shell/ext_spec.rb +11 -0
  195. data/spec/unit/mb/cli/shell_spec.rb +38 -0
  196. data/spec/unit/mb/cli/sub_command/base_spec.rb +102 -0
  197. data/spec/unit/mb/cli/sub_command/component_spec.rb +5 -0
  198. data/spec/unit/mb/cli/sub_command/plugin_spec.rb +91 -0
  199. data/spec/unit/mb/cli/sub_command_spec.rb +43 -0
  200. data/spec/unit/mb/cli/ui.rb +0 -0
  201. data/spec/unit/mb/cli_client_spec.rb +51 -0
  202. data/spec/unit/mb/cli_gateway_spec.rb +386 -0
  203. data/spec/unit/mb/command_invoker/worker_spec.rb +43 -0
  204. data/spec/unit/mb/command_invoker_spec.rb +230 -0
  205. data/spec/unit/mb/command_runner_spec.rb +299 -0
  206. data/spec/unit/mb/command_spec.rb +76 -0
  207. data/spec/unit/mb/component_spec.rb +185 -0
  208. data/spec/unit/mb/config_manager_spec.rb +31 -0
  209. data/spec/unit/mb/config_spec.rb +408 -0
  210. data/spec/unit/mb/cookbook_metadata_spec.rb +89 -0
  211. data/spec/unit/mb/core_ext/dir_spec.rb +92 -0
  212. data/spec/unit/mb/core_ext/enumerable_spec.rb +104 -0
  213. data/spec/unit/mb/core_ext/file_spec.rb +58 -0
  214. data/spec/unit/mb/core_ext/signal_spec.rb +24 -0
  215. data/spec/unit/mb/environment_manager_spec.rb +166 -0
  216. data/spec/unit/mb/error_handler_spec.rb +173 -0
  217. data/spec/unit/mb/errors_spec.rb +132 -0
  218. data/spec/unit/mb/file_system/tempfile_spec.rb +14 -0
  219. data/spec/unit/mb/file_system_spec.rb +69 -0
  220. data/spec/unit/mb/gear_spec.rb +125 -0
  221. data/spec/unit/mb/gears/dynamic_service_spec.rb +187 -0
  222. data/spec/unit/mb/gears/jmx/action_spec.rb +34 -0
  223. data/spec/unit/mb/gears/jmx_spec.rb +32 -0
  224. data/spec/unit/mb/gears/mysql/action_spec.rb +118 -0
  225. data/spec/unit/mb/gears/mysql_spec.rb +21 -0
  226. data/spec/unit/mb/gears/service/action_runner_spec.rb +182 -0
  227. data/spec/unit/mb/gears/service/action_spec.rb +44 -0
  228. data/spec/unit/mb/gears/service_spec.rb +124 -0
  229. data/spec/unit/mb/group_spec.rb +280 -0
  230. data/spec/unit/mb/job_manager_spec.rb +56 -0
  231. data/spec/unit/mb/job_record_spec.rb +60 -0
  232. data/spec/unit/mb/job_spec.rb +201 -0
  233. data/spec/unit/mb/locks_manager_spec.rb +88 -0
  234. data/spec/unit/mb/logging_spec.rb +133 -0
  235. data/spec/unit/mb/manifest_spec.rb +105 -0
  236. data/spec/unit/mb/mixin/attribute_setting_spec.rb +180 -0
  237. data/spec/unit/mb/mixin/coded_exit_spec.rb +25 -0
  238. data/spec/unit/mb/mixin/locks_spec.rb +32 -0
  239. data/spec/unit/mb/mixin/services_spec.rb +75 -0
  240. data/spec/unit/mb/node_filter_spec.rb +86 -0
  241. data/spec/unit/mb/node_querier_spec.rb +532 -0
  242. data/spec/unit/mb/plugin_manager_spec.rb +724 -0
  243. data/spec/unit/mb/plugin_spec.rb +247 -0
  244. data/spec/unit/mb/provisioner/manager_spec.rb +141 -0
  245. data/spec/unit/mb/provisioner/manifest_spec.rb +182 -0
  246. data/spec/unit/mb/provisioner/provision_data_spec.rb +113 -0
  247. data/spec/unit/mb/provisioner_spec.rb +251 -0
  248. data/spec/unit/mb/provisioners/aws_spec.rb +392 -0
  249. data/spec/unit/mb/provisioners/environment_factory_spec.rb +108 -0
  250. data/spec/unit/mb/rest_gateway_spec.rb +13 -0
  251. data/spec/unit/mb/ridley_ext/cookbook_object_spec.rb +105 -0
  252. data/spec/unit/mb/srv_ctl_spec.rb +142 -0
  253. data/spec/unit/mb/upgrade/manager_spec.rb +37 -0
  254. data/spec/unit/mb/upgrade/worker_spec.rb +219 -0
  255. data/spec/unit/motherbrain_spec.rb +58 -0
  256. data/templates/bootstrap.json +8 -0
  257. data/templates/motherbrain.rb +44 -0
  258. metadata +694 -15
  259. data/Rakefile +0 -1
@@ -0,0 +1,194 @@
1
+ require 'spec_helper'
2
+
3
+ describe MB::Bootstrap::Worker do
4
+ let(:node_querier) { double('node_querier') }
5
+ let(:chef_connection) { double('chef_connection') }
6
+
7
+ let(:address) { "cloud-1.riotgames.com" }
8
+ let(:host) { described_class::Host.new(address) }
9
+
10
+ let(:options) { Hash.new }
11
+
12
+ let(:instance) { described_class.new }
13
+ subject { instance }
14
+
15
+ before { host.stub(node_name: nil) }
16
+
17
+ describe "#run" do
18
+ pending
19
+ end
20
+
21
+ describe "#full_bootstrap" do
22
+ let(:chef_connection) { double('chef_connection') }
23
+ let(:response) { Ridley::HostConnector::Response.new("cloud-1.riotgames.com", exit_code: 0) }
24
+
25
+ before do
26
+ subject.stub(chef_connection: chef_connection)
27
+ chef_connection.stub_chain(:node, :bootstrap).and_return(response)
28
+ end
29
+
30
+ let(:result) { subject.full_bootstrap(host) }
31
+
32
+ it "returns an Hash" do
33
+ expect(result).to be_a(Hash)
34
+ end
35
+
36
+ it "has a :node_name key" do
37
+ expect(result).to have_key(:node_name)
38
+ end
39
+
40
+ it "has a :hostname key" do
41
+ expect(result).to have_key(:hostname)
42
+ end
43
+
44
+ it "has a :bootstrap_type key with the value :full" do
45
+ expect(result).to have_key(:bootstrap_type)
46
+ expect(result[:bootstrap_type]).to eql(:full)
47
+ end
48
+
49
+ it "has a :message key" do
50
+ expect(result).to have_key(:message)
51
+ end
52
+
53
+ it "has a :status key" do
54
+ expect(result).to have_key(:status)
55
+ end
56
+
57
+ context "when response is a failure" do
58
+ before do
59
+ response.exit_code = -1
60
+ response.stderr = "OH NO AN ERROR"
61
+ end
62
+
63
+ it "sets the value of the :status key to :error" do
64
+ expect(result[:status]).to eql(:error)
65
+ end
66
+
67
+ it "has the value of STDERR for :message" do
68
+ expect(result[:message]).to eql(response.stderr)
69
+ end
70
+ end
71
+
72
+ context "when response is a success" do
73
+ before { response.exit_code = 0 }
74
+
75
+ it "sets the value of the :status key to :ok" do
76
+ expect(result[:status]).to eql(:ok)
77
+ end
78
+
79
+ it "has a blank value for :message" do
80
+ expect(result[:message]).to be_blank
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "#partial_bootstrap" do
86
+ before { host.stub(node_name: "cloud-1") }
87
+
88
+ before(:each) do
89
+ subject.stub(node_querier: node_querier, chef_connection: chef_connection)
90
+ node_querier.stub(put_secret: nil, chef_run: nil)
91
+ chef_connection.stub_chain(:node, :merge_data)
92
+ end
93
+
94
+ let(:result) { subject.partial_bootstrap(host) }
95
+ let(:options) { Hash.new }
96
+
97
+ it "merges the given data with chef, puts the chef secret on the node, and then runs chef" do
98
+ chef_connection.node.should_receive(:merge_data).with(host.node_name, options)
99
+ node_querier.should_receive(:put_secret).with(host.hostname).ordered
100
+ node_querier.should_receive(:chef_run).with(host.hostname).ordered
101
+
102
+ subject.partial_bootstrap(host)
103
+ end
104
+
105
+ it "returns a Hash" do
106
+ expect(result).to be_a(Hash)
107
+ end
108
+
109
+ it "has a :node_name key" do
110
+ expect(result).to have_key(:node_name)
111
+ end
112
+
113
+ it "has a :hostname key" do
114
+ expect(result).to have_key(:hostname)
115
+ end
116
+
117
+ it "has a value of :partial for :bootstrap_type" do
118
+ expect(result[:bootstrap_type]).to eql(:partial)
119
+ end
120
+
121
+ it "has a value of :ok for :status" do
122
+ expect(result[:status]).to eql(:ok)
123
+ end
124
+
125
+ it "has a :message key" do
126
+ expect(result).to have_key(:message)
127
+ end
128
+
129
+ context "when placing the secret file on the node fails" do
130
+ let(:exception) { MB::RemoteFileCopyError.new("error in copy") }
131
+
132
+ before { node_querier.should_receive(:put_secret).and_raise(exception) }
133
+
134
+ it "sets the value of the :status key to :error" do
135
+ expect(result[:status]).to eql(:error)
136
+ end
137
+
138
+ it "has the string representation of the raised exception for :message" do
139
+ expect(result[:message]).to eql(exception.to_s)
140
+ end
141
+ end
142
+
143
+ context "when running chef on the node fails" do
144
+ let(:exception) { MB::RemoteCommandError.new("error in command") }
145
+
146
+ before { node_querier.should_receive(:chef_run).and_raise(exception) }
147
+
148
+ it "sets the value of the :status key to :error" do
149
+ expect(result[:status]).to eql(:error)
150
+ end
151
+
152
+ it "has the string representation of the raised exception for :message" do
153
+ expect(result[:message]).to eql(exception.to_s)
154
+ end
155
+ end
156
+
157
+ context "when the node does not have a node object in the Chef server" do
158
+ let(:node_resource) { double('node_resource') }
159
+ let(:options) { { run_list: "some_list", attributes: "some_attrs" } }
160
+ before do
161
+ chef_connection.stub(node: node_resource)
162
+ node_resource.should_receive(:merge_data).and_raise(Ridley::Errors::ResourceNotFound)
163
+ end
164
+
165
+ it "sets the response to error" do
166
+ expect(result[:status]).to eq(:error)
167
+ end
168
+ end
169
+ end
170
+ end
171
+
172
+ describe MB::Bootstrap::Worker::Host do
173
+ let(:address) { "reset.riotgames.com" }
174
+ subject { described_class.new(address) }
175
+
176
+ its(:hostname) { should eql(address) }
177
+
178
+ context "when the host is registered to Chef" do
179
+ before { MB::NodeQuerier.instance.stub(:registered_as).with(address).and_return(nil) }
180
+
181
+ its(:node_name) { should be_nil }
182
+ its(:partial_bootstrap?) { should be_false }
183
+ its(:full_bootstrap?) { should be_true }
184
+ end
185
+
186
+ context "when the host is not registered with Chef" do
187
+ let(:node_name) { "reset" }
188
+ before { MB::NodeQuerier.instance.stub(:registered_as).with(address).and_return(node_name) }
189
+
190
+ its(:node_name) { should eql(node_name) }
191
+ its(:partial_bootstrap?) { should be_true }
192
+ its(:full_bootstrap?) { should be_false }
193
+ end
194
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe MotherBrain::Chef::Config do
4
+ subject(:chef_config) { described_class.new(path) }
5
+
6
+ let(:path) { nil }
7
+ let(:file_contents) {
8
+ <<-CHEF_CONFIG
9
+ user "root"
10
+ pass "secret"
11
+ mode :none
12
+ variable = 123
13
+ CHEF_CONFIG
14
+ }
15
+
16
+ before do
17
+ chef_config.stub(
18
+ file_contents: file_contents
19
+ )
20
+ end
21
+
22
+ describe "#parse" do
23
+ before { chef_config.parse }
24
+
25
+ it "parses each line and creates a key in the hash" do
26
+ expect(chef_config[:user]).to eq("root")
27
+ expect(chef_config[:pass]).to eq("secret")
28
+ expect(chef_config[:mode]).to eq(:none)
29
+
30
+ expect(chef_config[:variable]).to be_nil
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe MotherBrain::Chef::RunListItem do
4
+ describe "#cookbook_name" do
5
+ context "recipe item" do
6
+ shared_examples "a recipe item" do
7
+ it "should return the cookbook" do
8
+ expect(subject.cookbook_name).to eq("foo")
9
+ end
10
+ end
11
+
12
+ context do
13
+ subject { described_class.new("recipe[foo::server]") }
14
+ it_behaves_like "a recipe item"
15
+ end
16
+
17
+ context do
18
+ subject { described_class.new("recipe[foo::server@1.1.2]") }
19
+ it_behaves_like "a recipe item"
20
+ end
21
+
22
+ context do
23
+ subject { described_class.new("recipe[foo]") }
24
+ it_behaves_like "a recipe item"
25
+ end
26
+ end
27
+ context "role item" do
28
+ subject { described_class.new("role[foo]") }
29
+ it "should return nil" do
30
+ expect(subject.cookbook_name).to be_nil
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,314 @@
1
+ require 'spec_helper'
2
+
3
+ describe MB::ChefMutex do
4
+ describe "ClassMethods" do
5
+ subject { described_class }
6
+
7
+ describe "::new" do
8
+ before { MB::LockManager.instance.reset }
9
+
10
+ it "adds the new lock to the lock manager" do
11
+ mutex = subject.new
12
+ expect(MB::LockManager.instance.locks).to have(1).item
13
+ expect(MB::LockManager.instance.locks.first).to eq(mutex)
14
+ end
15
+ end
16
+
17
+ describe "::synchronize" do
18
+ before { MB::LockManager.instance.reset }
19
+ let(:probe) { double('test-probe', test: nil) }
20
+
21
+ it "executes the given block" do
22
+ probe.should_receive(:test)
23
+
24
+ subject.synchronize(options) do
25
+ probe.test
26
+ end
27
+ end
28
+
29
+ it "removes the lock from the lock manager" do
30
+ subject.synchronize(options) do
31
+ probe.test
32
+ end
33
+
34
+ expect(MB::LockManager.instance.locks).to have(0).items
35
+ end
36
+
37
+ context "when the block encounters an error" do
38
+ before { probe.should_receive(:test).and_raise(RuntimeError) }
39
+
40
+ it "removes the lock from the lock manager" do
41
+ expect {
42
+ subject.synchronize(options) do
43
+ probe.test
44
+ end
45
+ }.to raise_error(RuntimeError)
46
+
47
+ expect(MB::LockManager.instance.locks).to have(0).items
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ subject { chef_mutex }
54
+
55
+ let(:chef_mutex) { klass.new(options.merge lockset) }
56
+
57
+ let(:client_name) { "johndoe" }
58
+ let(:lockset) { { chef_environment: "my_environment" } }
59
+ let(:options) { lockset }
60
+
61
+ let(:chef_connection_stub) { double client_name: client_name }
62
+ let(:locks_stub) { double(
63
+ delete: true,
64
+ find: nil,
65
+ new: double(save: true)
66
+ ) }
67
+ let(:job_stub) { double(
68
+ report_boolean: nil,
69
+ report_running: nil
70
+ ) }
71
+
72
+ before do
73
+ chef_mutex.stub locks: locks_stub
74
+ end
75
+
76
+ its(:type) { should == lockset.keys.first }
77
+ its(:name) { should == lockset.values.first }
78
+
79
+ its(:to_s) { should == "#{chef_mutex.type}:#{chef_mutex.name}" }
80
+ its(:data_bag_id) { should == "#{chef_mutex.type}-#{chef_mutex.name}" }
81
+
82
+ describe "#lock" do
83
+ subject(:lock) { chef_mutex.lock }
84
+
85
+ it "attempts a lock" do
86
+ chef_mutex.should_receive :attempt_lock
87
+
88
+ lock
89
+ end
90
+
91
+ context "with no existing lock" do
92
+ before { chef_mutex.stub read: false, write: true }
93
+
94
+ it { should be_true }
95
+
96
+ context "and the lock attempt fails" do
97
+ before { chef_mutex.stub write: false }
98
+
99
+ it { should be_false }
100
+ end
101
+ end
102
+
103
+ context "with an existing lock" do
104
+ before { chef_mutex.stub read: {}, write: true }
105
+
106
+ it { should be_false }
107
+
108
+ context "and force enabled" do
109
+ let(:options) { { force: true } }
110
+
111
+ it { should be_true }
112
+ end
113
+ end
114
+
115
+ context "without a valid lock type" do
116
+ let(:lockset) { { something: "something" } }
117
+
118
+ it { -> { lock }.should raise_error MB::InvalidLockType }
119
+ end
120
+
121
+ context "when passed a job" do
122
+ let(:options) { { job: job_stub } }
123
+
124
+ it "sets the job status" do
125
+ job_stub.should_receive(:set_status).with(
126
+ "Locking chef_environment:my_environment"
127
+ )
128
+
129
+ lock
130
+ end
131
+ end
132
+ end
133
+
134
+ describe "#locked?" do
135
+ subject(:locked?) { chef_mutex.locked? }
136
+
137
+ it { should be_false }
138
+
139
+ context "when the environment is locked" do
140
+ before do
141
+ chef_mutex.stub read: true
142
+ end
143
+
144
+ it { should be_true }
145
+ end
146
+ end
147
+
148
+ describe "#synchronize" do
149
+ subject(:synchronize) { chef_mutex.synchronize(&test_block) }
150
+
151
+ TestProbe = Object.new
152
+
153
+ let(:test_block) { -> { TestProbe.testing } }
154
+
155
+ before do
156
+ chef_mutex.stub lock: true, unlock: true
157
+
158
+ TestProbe.stub :testing
159
+ end
160
+
161
+ it "runs the block" do
162
+ TestProbe.should_receive :testing
163
+
164
+ synchronize
165
+ end
166
+
167
+ it "obtains a lock" do
168
+ chef_mutex.should_receive :lock
169
+
170
+ synchronize
171
+ end
172
+
173
+ it "releases the lock" do
174
+ chef_mutex.should_receive :unlock
175
+
176
+ synchronize
177
+ end
178
+
179
+ context "when the lock is unobtainable" do
180
+ before do
181
+ chef_mutex.stub lock: false, read: {}
182
+ end
183
+
184
+ it "raises a ResourceLocked error" do
185
+ chef_mutex.should_not_receive :unlock
186
+
187
+ expect {
188
+ synchronize
189
+ }.to raise_error(MB::ResourceLocked)
190
+ end
191
+
192
+ context "and force enabled" do
193
+ let(:options) { { force: true } }
194
+
195
+ it "locks with force" do
196
+ chef_mutex.should_receive(:lock).and_return(true)
197
+
198
+ synchronize
199
+ end
200
+ end
201
+ end
202
+
203
+ context "on block failure" do
204
+ before do
205
+ TestProbe.stub(:testing).and_raise(RuntimeError)
206
+ end
207
+
208
+ it "raises the error" do
209
+ expect { synchronize }.to raise_error(RuntimeError)
210
+ end
211
+
212
+ it "releases the lock" do
213
+ chef_mutex.should_receive(:unlock)
214
+
215
+ expect { synchronize }.to raise_error(RuntimeError)
216
+ end
217
+
218
+ it "does not crash the mutex actor" do
219
+ expect { chef_mutex.synchronize(&test_block) }.to raise_error(RuntimeError)
220
+
221
+ expect { chef_mutex.to_s }.to_not raise_error
222
+ end
223
+
224
+ context "and passed unlock_on_failure: false" do
225
+ before do
226
+ chef_mutex.stub(unlock_on_failure: false)
227
+ end
228
+
229
+ it "does not release the lock" do
230
+ chef_mutex.should_not_receive :unlock
231
+
232
+ expect { synchronize }.to raise_error(RuntimeError)
233
+ end
234
+ end
235
+ end
236
+ end
237
+
238
+ describe "#unlock" do
239
+ subject(:unlock) { chef_mutex.unlock }
240
+
241
+ it "attempts an unlock" do
242
+ chef_mutex.should_receive :attempt_unlock
243
+
244
+ unlock
245
+ end
246
+
247
+ it "returns true" do
248
+ expect(unlock).to be_true
249
+ end
250
+
251
+ context "when passed a job" do
252
+ let(:options) { { job: job_stub } }
253
+
254
+ it "sets the job status" do
255
+ job_stub.should_receive(:set_status).with(
256
+ "Unlocking chef_environment:my_environment"
257
+ )
258
+
259
+ unlock
260
+ end
261
+ end
262
+ end
263
+
264
+ describe "#our_lock?" do
265
+ before do
266
+ chef_mutex.class.class_eval { public :our_lock? }
267
+ chef_mutex.stub(:client_name).and_return("johndoe")
268
+ end
269
+
270
+ after do
271
+ chef_mutex.class.class_eval { private :our_lock? }
272
+ end
273
+
274
+ subject(:our_lock?) { chef_mutex.our_lock?(current_lock) }
275
+ let(:current_lock) { { id: "_chef_environment_:my_environment"} }
276
+
277
+ context "when current_lock is not from our client" do
278
+ before { current_lock['client_name'] = "janedoe" }
279
+
280
+ it { should be_false }
281
+ end
282
+
283
+ context "when current_lock is from our client" do
284
+ before { current_lock['client_name'] = "johndoe" }
285
+
286
+ context "when process_id is our process" do
287
+ before do
288
+ Process.stub(:pid).and_return(12345)
289
+ current_lock['process_id'] = 12345
290
+ end
291
+ it { should be_true }
292
+ end
293
+
294
+ context "when process_id is not our process" do
295
+ before do
296
+ Process.stub(:pid).and_return(12345)
297
+ current_lock['process_id'] = 23456
298
+ end
299
+ it { should be_false }
300
+ end
301
+ end
302
+ end
303
+
304
+ describe "#terminate" do
305
+ before { MB::LockManager.instance.reset }
306
+
307
+ it "removes the mutex from the lock manager" do
308
+ mutex = described_class.new(options)
309
+ expect(MB::LockManager.instance.locks).to have(1).item
310
+ mutex.terminate
311
+ expect(MB::LockManager.instance.locks).to have(0).items
312
+ end
313
+ end
314
+ end