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,386 @@
1
+ require 'spec_helper'
2
+
3
+ describe MB::CliGateway do
4
+ let(:ui) { described_class.ui }
5
+
6
+ before do
7
+ ui.stub :error
8
+ ui.stub :info
9
+ ui.stub :say
10
+ end
11
+
12
+ describe "ClassMethods" do
13
+ subject { described_class }
14
+
15
+ describe "::new" do
16
+ describe "specifying a configuration file" do
17
+ let(:location) { tmp_path.join('config.json').to_s }
18
+
19
+ before(:each) do
20
+ generate_valid_config(location)
21
+ end
22
+
23
+ it "loads the specified config file into the ConfigManager" do
24
+ config = MB::Config.from_file(location)
25
+ invoker = subject.new([], config: location)
26
+
27
+ MB::ConfigManager.instance.config._attributes_.should eql(config._attributes_)
28
+ end
29
+
30
+ it "exits with a ConfigNotFound error when the specified path does not exist" do
31
+ lambda {
32
+ invoker = subject.new([], config: tmp_path.join("not_there.json"))
33
+ }.should exit_with(MB::ConfigNotFound)
34
+ end
35
+
36
+ it "exits with a ConfigNotFound error when the specified path is a directory" do
37
+ lambda {
38
+ invoker = subject.new([], config: tmp_path)
39
+ }.should exit_with(MB::ConfigNotFound)
40
+ end
41
+ end
42
+ end
43
+
44
+ describe "::requires_environment?" do
45
+ context "no arguments" do
46
+ it "should not require an environment" do
47
+ subject.requires_environment?([]).should be_false
48
+ end
49
+ end
50
+
51
+ context "base command" do
52
+ it "should not require an environment for versions" do
53
+ subject.requires_environment?(["versions"]).should be_false
54
+ end
55
+ end
56
+
57
+ context "plugin argument" do
58
+ it "should not require an environment for a plugin" do
59
+ subject.requires_environment?(["myface"]).should be_false
60
+ end
61
+ end
62
+
63
+ context "plugin command" do
64
+ it "should require an environment for bootstrap" do
65
+ subject.requires_environment?(["myface", "bootstrap"]).should be_true
66
+ end
67
+
68
+ it "should not require an environment for help" do
69
+ subject.requires_environment?(["myface", "help"]).should be_false
70
+ end
71
+
72
+ it "should not require an environment for subtask help" do
73
+ subject.requires_environment?(["myface", "help", "bootstrap"]).should be_false
74
+ end
75
+ end
76
+ end
77
+
78
+ describe "::start_mb_application?" do
79
+ let(:args) {["myface"]}
80
+
81
+ context "not a config task" do
82
+ it "should be true" do
83
+ subject.start_mb_application?(args).should be_true
84
+ end
85
+ end
86
+
87
+ context "is a config task" do
88
+ it "should be false" do
89
+ subject::SKIP_CONFIG_TASKS.each do |config_task|
90
+ args = [config_task]
91
+ subject.start_mb_application?(args)
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ describe "::register_plugin" do
98
+ let(:name) { "myface" }
99
+ let(:description) { "Ivey should use SublimeText 2" }
100
+ let(:plugin) { MB::Plugin.new(metadata) }
101
+ let(:metadata) do
102
+ double('metadata',
103
+ name: name,
104
+ description: description
105
+ )
106
+ end
107
+
108
+ it "registers a subcommand with self" do
109
+ subcommand = double
110
+ MB::Cli::SubCommand.should_receive(:new).with(plugin).and_return(subcommand)
111
+ subject.should_receive(:register_subcommand).with(subcommand)
112
+
113
+ subject.register_plugin(plugin)
114
+ end
115
+
116
+ it "returns a subcommand with the plugin set as #plugin" do
117
+ subject.register_plugin(plugin).plugin.should eql(plugin)
118
+ end
119
+
120
+ it "has a name matching the plugin" do
121
+ subject.register_plugin(plugin).name.should eql(name)
122
+ end
123
+
124
+ it "has a description matching the plugin" do
125
+ subject.register_plugin(plugin).description.should eql(description)
126
+ end
127
+ end
128
+
129
+ describe "::find_plugin" do
130
+ let(:plugin_manager) { double('plugin_manager') }
131
+ let(:name) { "myface" }
132
+ let(:options) do
133
+ {
134
+ plugin_version: nil,
135
+ environment: nil
136
+ }
137
+ end
138
+ let(:plugin) { double('asdf') }
139
+
140
+ subject { described_class.find_plugin(name, options) }
141
+
142
+ before do
143
+ described_class.stub(plugin_manager: plugin_manager)
144
+ described_class.stub(local_plugin?: false)
145
+ end
146
+
147
+ context "given no value for :plugin_version or :environment" do
148
+ before do
149
+ options[:plugin_version] = nil
150
+ options[:environment] = nil
151
+ end
152
+
153
+ it "finds the installed or remote latest version of the plugin with the given name" do
154
+ plugin_manager.should_receive(:latest).with(name, remote: true).and_return(plugin)
155
+
156
+ subject.should eql(plugin)
157
+ end
158
+
159
+ it "prints an error to the UI and exits if no plugin is found" do
160
+ plugin_manager.should_receive(:latest).with(name, remote: true).and_return(nil)
161
+ ui.should_receive(:error).with(anything)
162
+
163
+ expect {
164
+ subject
165
+ }.to raise_error(SystemExit)
166
+ end
167
+ end
168
+
169
+ context "given a value for :plugin_version" do
170
+ let(:plugin_version) { "1.2.3" }
171
+ before { options[:plugin_version] = plugin_version }
172
+
173
+ it "finds a plugin on local and remote of the given name and version" do
174
+ plugin_manager.should_receive(:find).with(name, plugin_version, remote: true).and_return(plugin)
175
+
176
+ subject.should eql(plugin)
177
+ end
178
+
179
+ it "prints an error to the UI and exits if no plugin is found" do
180
+ plugin_manager.should_receive(:find).with(name, plugin_version, remote: true).and_return(nil)
181
+ ui.should_receive(:error).with(anything)
182
+
183
+ expect {
184
+ subject
185
+ }.to raise_error(SystemExit)
186
+ end
187
+ end
188
+
189
+ context "given a value for :environment" do
190
+ let(:environment) { "rspec-test" }
191
+ before { options[:environment] = environment }
192
+
193
+ it "finds the best suitable plugin for the environment" do
194
+ plugin_manager.should_receive(:for_environment).with(name, environment, remote: true).and_return(plugin)
195
+
196
+ subject.should eql(plugin)
197
+ end
198
+
199
+ context "when an environment of the given name is not found" do
200
+ before do
201
+ plugin_manager.should_receive(:for_environment).and_raise(MB::EnvironmentNotFound.new(environment))
202
+ end
203
+
204
+ it "finds the latest plugin on local and remote of the given name" do
205
+ plugin_manager.should_receive(:latest).with(name, remote: true).and_return(plugin)
206
+
207
+ subject.should eql(plugin)
208
+ end
209
+
210
+ it "prints an error to the UI and exits if no plugin is found" do
211
+ plugin_manager.should_receive(:latest).with(name, remote: true).and_return(nil)
212
+
213
+ expect {
214
+ subject
215
+ }.to raise_error(SystemExit)
216
+ end
217
+ end
218
+ end
219
+
220
+ context "given both a plugin version and environment" do
221
+ let(:environment) { "rspec-test" }
222
+ let(:plugin_version) { "1.2.3" }
223
+
224
+ before do
225
+ options[:environment] = environment
226
+ options[:plugin_version] = plugin_version
227
+ end
228
+
229
+ it "finds a plugin on local and remote of the given name and version" do
230
+ plugin_manager.should_receive(:find).with(name, plugin_version, remote: true).and_return(plugin)
231
+
232
+ subject.should eql(plugin)
233
+ end
234
+ end
235
+
236
+ context "given a local plugin" do
237
+ before { described_class.stub(local_plugin?: true) }
238
+
239
+ it "should prefer the local plugin if no version was specified" do
240
+ plugin_manager.should_receive(:load_installed).and_return(plugin)
241
+
242
+ subject.should eql(plugin)
243
+ end
244
+
245
+ it "should use the version specified version of the plugin if a version is specified" do
246
+ options[:plugin_version] = '1.2.3'
247
+ plugin_manager.should_not_receive(:load_installed)
248
+ plugin_manager.should_receive(:find).with('myface', '1.2.3', remote: true).and_return(plugin)
249
+
250
+ subject.should eql(plugin)
251
+ end
252
+
253
+ it "should use the local version if the environment is specified" do
254
+ options[:environment] = "abc"
255
+ plugin_manager.should_receive(:load_installed).and_return(plugin)
256
+
257
+ subject.should eql(plugin)
258
+ end
259
+ end
260
+ end
261
+ end
262
+
263
+ describe "#validate_environment" do
264
+ subject(:validate_environment) { cli_gateway.send(:validate_environment) }
265
+ let(:cli_gateway) { described_class.new(args, options, config) }
266
+
267
+ let(:args) { Array.new }
268
+ let(:config) { { current_command: double('Config', name: "help") } }
269
+ let(:environment_name) { "my_env" }
270
+ let(:options) { { environment: environment_name } }
271
+ let(:environment_manager) { MB::EnvironmentManager.instance.wrapped_object }
272
+
273
+ before do
274
+ cli_gateway.stub :ask
275
+ cli_gateway.stub testing?: false
276
+ cli_gateway.stub(environment_manager: environment_manager)
277
+
278
+ environment_manager.stub(:find).and_return(Hash.new)
279
+ end
280
+
281
+ it "doesn't ask the user" do
282
+ cli_gateway.should_not_receive(:ask)
283
+
284
+ validate_environment
285
+ end
286
+
287
+ context "without an environment" do
288
+ let(:options) { Hash.new }
289
+
290
+ it "doesn't ask the user" do
291
+ cli_gateway.should_not_receive(:ask)
292
+
293
+ validate_environment
294
+ end
295
+ end
296
+
297
+ context "if the environment does not exist" do
298
+ before do
299
+ environment_manager.stub(:find).and_raise(
300
+ MB::EnvironmentNotFound.new("nope")
301
+ )
302
+ end
303
+
304
+ it "doesn't ask the user and raises an error" do
305
+ cli_gateway.should_not_receive(:ask)
306
+
307
+ -> { validate_environment }.should raise_error(MB::EnvironmentNotFound)
308
+ end
309
+ end
310
+
311
+ context "if the environment does not exist and given a create task" do
312
+ let(:args) { ["bootstrap"] }
313
+
314
+ before do
315
+ environment_manager.stub(:find).and_raise(
316
+ MB::EnvironmentNotFound.new("nope")
317
+ )
318
+ end
319
+
320
+ context "and it should 'quit' on missing environments" do
321
+ let(:options) { { environment: environment_name, on_environment_missing: "quit" } }
322
+
323
+ it "should quit without asking" do
324
+ cli_gateway.should_not_receive(:ask)
325
+ cli_gateway.should_receive(:abort)
326
+
327
+ validate_environment
328
+ end
329
+ end
330
+
331
+ context "and it should 'create' on missing environments" do
332
+ let(:options) { { environment: environment_name, on_environment_missing: "create" } }
333
+
334
+ it "should create the environment without asking" do
335
+ cli_gateway.should_not_receive(:ask)
336
+ cli_gateway.should_receive(:create_environment).with(environment_name)
337
+
338
+ validate_environment
339
+ end
340
+
341
+ end
342
+
343
+
344
+ context "and it should 'prompt' on missing environments" do
345
+
346
+ let(:options) { { environment: environment_name, on_environment_missing: "prompt" } }
347
+
348
+ context "if the user responds yes" do
349
+ before do
350
+ cli_gateway.stub ask: "y"
351
+ end
352
+
353
+ it "creates an environment" do
354
+ cli_gateway.should_receive(:create_environment).with(environment_name)
355
+
356
+ validate_environment
357
+ end
358
+ end
359
+
360
+ context "if the user responds no" do
361
+ before do
362
+ cli_gateway.stub ask: "n"
363
+ end
364
+
365
+ it "doesn't create an environment" do
366
+ environment_manager.should_not_receive(:create).with(environment_name)
367
+
368
+ validate_environment
369
+ end
370
+ end
371
+
372
+ context "if the user responds quit" do
373
+ before do
374
+ cli_gateway.stub ask: "q"
375
+ end
376
+
377
+ it "exits the cli" do
378
+ cli_gateway.should_receive(:abort)
379
+
380
+ validate_environment
381
+ end
382
+ end
383
+ end
384
+ end
385
+ end
386
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe MB::CommandInvoker::Worker do
4
+ let(:component) { double('component', name: 'chat') }
5
+ let(:command) { double('command', name: 'stop', type: :component, scope: component) }
6
+ let(:environment) { "rspec-env" }
7
+
8
+ subject { described_class.new(command, environment) }
9
+
10
+ describe "#run" do
11
+ let(:job) { double('job') }
12
+ let(:arguments) { Array.new }
13
+
14
+ before do
15
+ job.stub(set_status: nil)
16
+ command.stub(invoke: nil)
17
+ end
18
+
19
+ let(:run) { subject.run(job, arguments) }
20
+
21
+ it "sets a status message" do
22
+ job.should_receive(:set_status)
23
+
24
+ run
25
+ end
26
+
27
+ it "invokes the command" do
28
+ command.should_receive(:invoke).with(job, environment, nil)
29
+
30
+ run
31
+ end
32
+
33
+ context "when given additional arguments" do
34
+ let(:arguments) { [1,2,3] }
35
+
36
+ it "invokes the command with the additional arguments" do
37
+ command.should_receive(:invoke).with(job, environment, nil, *arguments)
38
+
39
+ run
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,230 @@
1
+ require 'spec_helper'
2
+
3
+ describe MB::CommandInvoker do
4
+ let(:plugin_id) { "rspec-test" }
5
+ let(:command_id) { "start" }
6
+ let(:component_id) { "default" }
7
+ let(:environment) { "rspec-testenv" }
8
+
9
+ let(:options) do
10
+ {
11
+ version: "1.0.0"
12
+ }
13
+ end
14
+
15
+ subject { described_class.new }
16
+
17
+ describe "#async_invoke" do
18
+ let(:job_ticket) { double(MB::JobRecord) }
19
+ let(:job) { double(MB::Job, ticket: job_ticket) }
20
+ let(:command_name) { "stop" }
21
+ let(:options) { Hash.new }
22
+
23
+ before do
24
+ MB::Job.stub(:new).and_return(job)
25
+ end
26
+
27
+ it "asynchronously calls {#invoke}" do
28
+ subject.should_receive(:async).with(:invoke, job, command_name, options)
29
+
30
+ subject.async_invoke(command_name, options)
31
+ end
32
+
33
+ it "returns a JobRecord" do
34
+ subject.stub(:async).with(:invoke, job, command_name, options)
35
+
36
+ subject.async_invoke(command_name, options).should eql(job_ticket)
37
+ end
38
+ end
39
+
40
+ describe "#find" do
41
+ let(:command) { "stop" }
42
+ let(:plugin) { "test-plugin" }
43
+ let(:options) do
44
+ {
45
+ version: nil,
46
+ component: nil,
47
+ environment: nil
48
+ }
49
+ end
50
+
51
+ let(:run) { subject.find(command, plugin, options) }
52
+
53
+ it "delegates to #find_latest" do
54
+ subject.should_receive(:find_latest).with(command, plugin, options[:component])
55
+ run
56
+ end
57
+
58
+ context "when given a value for :version" do
59
+ before do
60
+ options[:version] = "1.2.3"
61
+ end
62
+
63
+ it "delegates to #for_version" do
64
+ subject.should_receive(:for_version).with(options[:version], command, plugin, options[:component])
65
+ run
66
+ end
67
+ end
68
+
69
+ context "when given a value for :environment" do
70
+ before do
71
+ options[:environment] = "test-env"
72
+ end
73
+
74
+ it "delegates to #for_environment" do
75
+ subject.should_receive(:for_environment).with(options[:environment], command, plugin, options[:component])
76
+ run
77
+ end
78
+ end
79
+ end
80
+
81
+ describe "#invoke" do
82
+ let(:command_name) { "stop" }
83
+ let(:command) { double(invoke: nil) }
84
+ let(:plugin) { "chat" }
85
+ let(:component) { "default" }
86
+ let(:environment) { "rspec-test" }
87
+ let(:version) { "1.2.3" }
88
+ let(:job) { double(MB::Job, alive?: true, set_status: nil, terminate: nil) }
89
+ let(:worker) { double('worker', alive?: true, terminate: nil) }
90
+ let(:environment_manager) { double('env-man') }
91
+ let(:options) do
92
+ {
93
+ plugin: plugin,
94
+ component: component,
95
+ environment: environment,
96
+ version: version,
97
+ arguments: Array.new
98
+ }
99
+ end
100
+
101
+ let(:run) { subject.invoke(job, command_name, options) }
102
+
103
+ before(:each) do
104
+ MB::CommandInvoker::Worker.stub(:new).and_return(worker)
105
+ subject.stub(find: command, environment_manager: environment_manager, plugin_manager: plugin_manager)
106
+ job.stub(set_status: nil, report_running: nil, report_failure: nil, report_success: nil)
107
+ environment_manager.stub(find: nil)
108
+ worker.stub(run: nil)
109
+ end
110
+
111
+ it "wraps the invocation in a lock" do
112
+ MB::ChefMutex.any_instance.should_receive :synchronize
113
+
114
+ run
115
+ end
116
+
117
+ it "marks the job as running and then a success on success" do
118
+ job.should_receive(:report_running).ordered
119
+ job.should_receive(:report_success).ordered
120
+
121
+ run
122
+ end
123
+
124
+ it "terminates the running job on completion" do
125
+ job.should_receive(:terminate).once
126
+
127
+ run
128
+ end
129
+
130
+ it "creates a new worker to run the command in" do
131
+ MB::CommandInvoker::Worker.should_receive(:new).with(command, environment, nil).and_return(worker)
132
+
133
+ run
134
+ end
135
+
136
+ it "runs the worker with the job and given arguments" do
137
+ worker.should_receive(:run).with(job, options[:arguments])
138
+
139
+ run
140
+ end
141
+
142
+ it "shuts the worker down after completion" do
143
+ worker.should_receive(:terminate).once
144
+
145
+ run
146
+ end
147
+
148
+ context "when the plugin option is nil" do
149
+ before { options[:plugin] = nil }
150
+
151
+ it "sets the job to failure" do
152
+ job.should_receive(:report_failure)
153
+ run
154
+ end
155
+ end
156
+
157
+ context "when the environment option is nil" do
158
+ before { options[:environment] = nil }
159
+
160
+ it "sets the job to failure" do
161
+ job.should_receive(:report_failure)
162
+ run
163
+ end
164
+ end
165
+
166
+ context "when the target environment does not exist" do
167
+ let(:exception) { MB::EnvironmentNotFound.new(environment) }
168
+
169
+ before do
170
+ subject.should_receive(:find).
171
+ with(command_name, plugin, component: component, environment: environment, version: version).
172
+ and_raise(exception)
173
+ end
174
+
175
+ it "should set the job state to :failure" do
176
+ job.should_receive(:report_failure).with(exception)
177
+
178
+ run
179
+ end
180
+ end
181
+
182
+ context "when a plugin of the given name is not found" do
183
+ let(:exception) { MB::PluginNotFound.new(plugin) }
184
+
185
+ before do
186
+ subject.should_receive(:find).
187
+ with(command_name, plugin, component: component, environment: environment, version: version).
188
+ and_raise(exception)
189
+ end
190
+
191
+ it "sets the job to failure with a command not found message" do
192
+ job.should_receive(:report_failure).with(exception)
193
+
194
+ run
195
+ end
196
+ end
197
+
198
+ context "when a component of the given name is not found" do
199
+ let(:exception) { MB::ComponentNotFound.new(component, plugin) }
200
+
201
+ before do
202
+ subject.should_receive(:find).
203
+ with(command_name, plugin, component: component, environment: environment, version: version).
204
+ and_raise(exception)
205
+ end
206
+
207
+ it "sets the job to failure with a command not found message" do
208
+ job.should_receive(:report_failure).with(exception)
209
+
210
+ run
211
+ end
212
+ end
213
+
214
+ context "when a plugin command for the given plugin is not found" do
215
+ let(:exception) { MB::CommandNotFound.new(component, plugin) }
216
+
217
+ before do
218
+ subject.should_receive(:find).
219
+ with(command_name, plugin, component: component, environment: environment, version: version).
220
+ and_raise(exception)
221
+ end
222
+
223
+ it "completes the job as a failure with a command not found message" do
224
+ job.should_receive(:report_failure).with(exception)
225
+
226
+ run
227
+ end
228
+ end
229
+ end
230
+ end