derelict_m 0.6.2a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +7 -0
  2. data/.cane +2 -0
  3. data/.coveralls.yml +1 -0
  4. data/.gitignore +18 -0
  5. data/.travis.yml +16 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +99 -0
  9. data/Rakefile +22 -0
  10. data/derelict.gemspec +63 -0
  11. data/lib/derelict.rb +74 -0
  12. data/lib/derelict/box.rb +29 -0
  13. data/lib/derelict/box/manager.rb +111 -0
  14. data/lib/derelict/box/not_found.rb +16 -0
  15. data/lib/derelict/connection.rb +84 -0
  16. data/lib/derelict/connection/invalid.rb +14 -0
  17. data/lib/derelict/connection/not_found.rb +13 -0
  18. data/lib/derelict/exception.rb +6 -0
  19. data/lib/derelict/exception/optional_reason.rb +32 -0
  20. data/lib/derelict/executer.rb +237 -0
  21. data/lib/derelict/instance.rb +147 -0
  22. data/lib/derelict/instance/command_failed.rb +30 -0
  23. data/lib/derelict/instance/invalid.rb +14 -0
  24. data/lib/derelict/instance/missing_binary.rb +13 -0
  25. data/lib/derelict/instance/non_directory.rb +13 -0
  26. data/lib/derelict/instance/not_found.rb +13 -0
  27. data/lib/derelict/parser.rb +27 -0
  28. data/lib/derelict/parser/box_list.rb +53 -0
  29. data/lib/derelict/parser/box_list/invalid_format.rb +16 -0
  30. data/lib/derelict/parser/plugin_list.rb +63 -0
  31. data/lib/derelict/parser/plugin_list/invalid_format.rb +16 -0
  32. data/lib/derelict/parser/plugin_list/needs_reinstall.rb +22 -0
  33. data/lib/derelict/parser/status.rb +90 -0
  34. data/lib/derelict/parser/status/invalid_format.rb +16 -0
  35. data/lib/derelict/parser/version.rb +28 -0
  36. data/lib/derelict/parser/version/invalid_format.rb +16 -0
  37. data/lib/derelict/plugin.rb +29 -0
  38. data/lib/derelict/plugin/manager.rb +107 -0
  39. data/lib/derelict/plugin/not_found.rb +14 -0
  40. data/lib/derelict/utils.rb +11 -0
  41. data/lib/derelict/utils/logger.rb +59 -0
  42. data/lib/derelict/utils/logger/array_outputter.rb +43 -0
  43. data/lib/derelict/utils/logger/invalid_type.rb +15 -0
  44. data/lib/derelict/utils/logger/raw_formatter.rb +12 -0
  45. data/lib/derelict/version.rb +3 -0
  46. data/lib/derelict/virtual_machine.rb +190 -0
  47. data/lib/derelict/virtual_machine/invalid.rb +14 -0
  48. data/lib/derelict/virtual_machine/not_found.rb +18 -0
  49. data/spec/coverage_helper.rb +19 -0
  50. data/spec/derelict/box/manager_spec.rb +171 -0
  51. data/spec/derelict/box/not_found_spec.rb +13 -0
  52. data/spec/derelict/box_spec.rb +37 -0
  53. data/spec/derelict/connection/invalid_spec.rb +16 -0
  54. data/spec/derelict/connection/not_found_spec.rb +13 -0
  55. data/spec/derelict/connection_spec.rb +107 -0
  56. data/spec/derelict/exception/optional_reason_spec.rb +41 -0
  57. data/spec/derelict/exception_spec.rb +11 -0
  58. data/spec/derelict/executer_spec.rb +129 -0
  59. data/spec/derelict/instance/command_failed_spec.rb +40 -0
  60. data/spec/derelict/instance/invalid_spec.rb +16 -0
  61. data/spec/derelict/instance/missing_binary_spec.rb +13 -0
  62. data/spec/derelict/instance/non_directory_spec.rb +13 -0
  63. data/spec/derelict/instance/not_found_spec.rb +13 -0
  64. data/spec/derelict/instance_spec.rb +258 -0
  65. data/spec/derelict/parser/box_list/invalid_format_spec.rb +16 -0
  66. data/spec/derelict/parser/box_list_spec.rb +64 -0
  67. data/spec/derelict/parser/plugin_list/invalid_format_spec.rb +16 -0
  68. data/spec/derelict/parser/plugin_list/needs_reinstall_spec.rb +13 -0
  69. data/spec/derelict/parser/plugin_list_spec.rb +82 -0
  70. data/spec/derelict/parser/status/invalid_format_spec.rb +16 -0
  71. data/spec/derelict/parser/status_spec.rb +214 -0
  72. data/spec/derelict/parser/version/invalid_format_spec.rb +16 -0
  73. data/spec/derelict/parser/version_spec.rb +42 -0
  74. data/spec/derelict/parser_spec.rb +24 -0
  75. data/spec/derelict/plugin/manager_spec.rb +208 -0
  76. data/spec/derelict/plugin/not_found_spec.rb +13 -0
  77. data/spec/derelict/plugin_spec.rb +37 -0
  78. data/spec/derelict/utils/logger/array_outputter_spec.rb +40 -0
  79. data/spec/derelict/utils/logger/invalid_type_spec.rb +13 -0
  80. data/spec/derelict/utils/logger/raw_formatter_spec.rb +17 -0
  81. data/spec/derelict/utils/logger_spec.rb +35 -0
  82. data/spec/derelict/virtual_machine/invalid_spec.rb +16 -0
  83. data/spec/derelict/virtual_machine/not_found_spec.rb +34 -0
  84. data/spec/derelict/virtual_machine_spec.rb +356 -0
  85. data/spec/derelict_spec.rb +50 -0
  86. data/spec/spec_helper.rb +32 -0
  87. data/spec/support/log_context.rb +36 -0
  88. metadata +332 -0
@@ -0,0 +1,356 @@
1
+ require "spec_helper"
2
+
3
+ describe Derelict::VirtualMachine do
4
+ let(:connection) { double("connection", :description => "test") }
5
+ let(:name) { double("name", :inspect => "testvm") }
6
+
7
+ let(:vm) { Derelict::VirtualMachine.new connection, name }
8
+ subject { vm }
9
+
10
+ describe "#initialize" do
11
+ it "should succeed" do
12
+ expect { subject }.to_not raise_error
13
+ end
14
+
15
+ include_context "logged messages"
16
+ let(:expected_logs) {[
17
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
18
+ ]}
19
+ end
20
+
21
+ describe "#validate!" do
22
+ before { expect(vm).to receive(:exists?).and_return(exists?) }
23
+ subject { vm.validate! }
24
+
25
+ context "when exists? is false" do
26
+ let(:exists?) { false }
27
+ it "should raise NotFound" do
28
+ expect { subject }.to raise_error Derelict::VirtualMachine::NotFound
29
+ end
30
+
31
+ include_context "logged messages"
32
+ let(:expected_logs) {[
33
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
34
+ "DEBUG virtualmachine: Starting validation for Derelict::VirtualMachine 'testvm' from test\n",
35
+ ]}
36
+ end
37
+
38
+ context "when exists? is true" do
39
+ let(:exists?) { true }
40
+ it "shouldn't raise any errors" do
41
+ expect { subject }.to_not raise_error
42
+ end
43
+
44
+ it "should be chainable" do
45
+ expect(subject).to be subject
46
+ end
47
+
48
+ include_context "logged messages"
49
+ let(:expected_logs) {[
50
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
51
+ "DEBUG virtualmachine: Starting validation for Derelict::VirtualMachine 'testvm' from test\n",
52
+ " INFO virtualmachine: Successfully validated Derelict::VirtualMachine 'testvm' from test\n",
53
+ ]}
54
+ end
55
+ end
56
+
57
+ describe "#exists?" do
58
+ let(:status) { double("status", :exists? => exists?) }
59
+ let(:exists?) { double("exists") }
60
+
61
+ before { expect(vm).to receive(:status).and_return(status) }
62
+ subject { vm.exists? }
63
+
64
+ it "should delegate to the status parser" do
65
+ expect(subject).to be exists?
66
+ end
67
+
68
+ include_context "logged messages"
69
+ let(:expected_logs) {[
70
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
71
+ ]}
72
+ end
73
+
74
+ describe "#state" do
75
+ let(:status) { double("status", :state => state) }
76
+ let(:state) { double("state") }
77
+
78
+ before { expect(vm).to receive(:status).and_return(status) }
79
+ subject { vm.state }
80
+
81
+ it "should delegate to the status parser" do
82
+ expect(subject).to be state
83
+ end
84
+ end
85
+
86
+ describe "#running?" do
87
+ before { expect(vm).to receive(:state).and_return(state) }
88
+ subject { vm.running? }
89
+
90
+ context "when state is :running" do
91
+ let(:state) { :running }
92
+ it { should be true }
93
+ end
94
+
95
+ context "when state is :foo" do
96
+ let(:state) { :foo }
97
+ it { should be false }
98
+ end
99
+ end
100
+
101
+ describe "#up!" do
102
+ let(:options) { Hash.new }
103
+ let(:result) { double("result") }
104
+ subject { vm.up! options }
105
+
106
+ context "with too few arguments" do
107
+ before do
108
+ expect(connection).to receive(:execute!).with(:up, name)
109
+ end
110
+
111
+ subject { vm.up! }
112
+
113
+ it "should not raise ArgumentError" do
114
+ expect { subject }.to_not raise_error
115
+ end
116
+ end
117
+
118
+ context "with too many arguments" do
119
+ subject { vm.up! options, :extra }
120
+
121
+ it "should raise ArgumentError" do
122
+ expect { subject }.to raise_error ArgumentError
123
+ end
124
+ end
125
+
126
+ context "with correct number of arguments" do
127
+ let(:args) { [:up, name] }
128
+
129
+ context "with external logging disabled" do
130
+ before do
131
+ expect(connection).to receive(:execute!).with(*args).and_return result
132
+ end
133
+
134
+ include_context "logged messages"
135
+ let(:expected_logs) {[
136
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
137
+ " INFO virtualmachine: Bringing up Derelict::VirtualMachine 'testvm' from test\n",
138
+ ]}
139
+
140
+ context "with different provider" do
141
+ let(:options) { {:provider => "foobar"} }
142
+ let(:args) { [:up, name, "--provider", "foobar"] }
143
+
144
+ it { should be result }
145
+ end
146
+ end
147
+
148
+ context "with external logging enabled" do
149
+ before do
150
+ expect(connection).to receive(:execute!).with(*args).and_yield("foo", nil).and_return result
151
+ end
152
+
153
+ let(:options) { {:log => true} }
154
+
155
+ include_context "logged messages"
156
+ let(:expected_logs) {[
157
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
158
+ " INFO virtualmachine: Bringing up Derelict::VirtualMachine 'testvm' from test\n",
159
+ " INFO external: foo\n",
160
+ ]}
161
+
162
+ context "with color enabled" do
163
+ let(:options) { {:log => true, :color => true} }
164
+ let(:args) { [:up, name, '--color'] }
165
+ it { should be result }
166
+ end
167
+
168
+ context "in chars mode" do
169
+ let(:options) { {:log => true, :log_mode => :chars} }
170
+ let(:args) { [:up, name, {:mode => :chars}] }
171
+ it { should be result }
172
+ end
173
+ end
174
+ end
175
+ end
176
+
177
+ describe "#halt!" do
178
+ let(:options) { Hash.new }
179
+ let(:result) { double("result") }
180
+ subject { vm.halt! options }
181
+
182
+ let(:args) { [:halt, name] }
183
+ before do
184
+ expect(connection).to receive(:execute!).with(*args).and_yield("foo", nil).and_return result
185
+ end
186
+
187
+ context "with external logging disabled" do
188
+ include_context "logged messages"
189
+ let(:expected_logs) {[
190
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
191
+ " INFO virtualmachine: Halting Derelict::VirtualMachine 'testvm' from test\n",
192
+ ]}
193
+ end
194
+
195
+ context "with external logging enabled" do
196
+ let(:options) { {:log => true} }
197
+
198
+ include_context "logged messages"
199
+ let(:expected_logs) {[
200
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
201
+ " INFO virtualmachine: Halting Derelict::VirtualMachine 'testvm' from test\n",
202
+ " INFO external: foo\n",
203
+ ]}
204
+
205
+ context "with color enabled" do
206
+ let(:options) { {:log => true, :color => true} }
207
+ let(:args) { [:halt, name, '--color'] }
208
+ it { should be result }
209
+ end
210
+ end
211
+ end
212
+
213
+ describe "#destroy!" do
214
+ let(:options) { Hash.new }
215
+ let(:result) { double("result") }
216
+ subject { vm.destroy! options }
217
+
218
+ let(:args) { [:destroy, name, '--force'] }
219
+ before do
220
+ expect(connection).to receive(:execute!).with(*args).and_yield("foo", nil).and_return result
221
+ end
222
+
223
+ context "with external logging disabled" do
224
+ include_context "logged messages"
225
+ let(:expected_logs) {[
226
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
227
+ " INFO virtualmachine: Destroying Derelict::VirtualMachine 'testvm' from test\n",
228
+ ]}
229
+ end
230
+
231
+ context "with external logging enabled" do
232
+ let(:options) { {:log => true} }
233
+
234
+ include_context "logged messages"
235
+ let(:expected_logs) {[
236
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
237
+ " INFO virtualmachine: Destroying Derelict::VirtualMachine 'testvm' from test\n",
238
+ " INFO external: foo\n",
239
+ ]}
240
+
241
+ context "with color enabled" do
242
+ let(:options) { {:log => true, :color => true} }
243
+ let(:args) { [:destroy, name, '--force', '--color'] }
244
+ it { should be result }
245
+ end
246
+ end
247
+ end
248
+
249
+ describe "#reload!" do
250
+ let(:options) { Hash.new }
251
+ let(:result) { double("result") }
252
+ subject { vm.reload! options }
253
+
254
+ before do
255
+ expect(connection).to receive(:execute!).with(:reload, name).and_yield("foo", nil).and_return result
256
+ end
257
+
258
+ context "with external logging disabled" do
259
+ include_context "logged messages"
260
+ let(:expected_logs) {[
261
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
262
+ " INFO virtualmachine: Reloading Derelict::VirtualMachine 'testvm' from test\n",
263
+ ]}
264
+ end
265
+
266
+ context "with external logging enabled" do
267
+ let(:options) { {:log => true} }
268
+
269
+ include_context "logged messages"
270
+ let(:expected_logs) {[
271
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
272
+ " INFO virtualmachine: Reloading Derelict::VirtualMachine 'testvm' from test\n",
273
+ " INFO external: foo\n",
274
+ ]}
275
+ end
276
+ end
277
+
278
+ describe "#suspend!" do
279
+ let(:options) { Hash.new }
280
+ let(:result) { double("result") }
281
+ subject { vm.suspend! options }
282
+
283
+ before do
284
+ expect(connection).to receive(:execute!).with(:suspend, name).and_yield("foo", nil).and_return result
285
+ end
286
+
287
+ context "with external logging disabled" do
288
+ include_context "logged messages"
289
+ let(:expected_logs) {[
290
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
291
+ " INFO virtualmachine: Suspending Derelict::VirtualMachine 'testvm' from test\n",
292
+ ]}
293
+ end
294
+
295
+ context "with external logging enabled" do
296
+ let(:options) { {:log => true} }
297
+
298
+ include_context "logged messages"
299
+ let(:expected_logs) {[
300
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
301
+ " INFO virtualmachine: Suspending Derelict::VirtualMachine 'testvm' from test\n",
302
+ " INFO external: foo\n",
303
+ ]}
304
+ end
305
+ end
306
+
307
+ describe "#resume!" do
308
+ let(:options) { Hash.new }
309
+ let(:result) { double("result") }
310
+ subject { vm.resume! options }
311
+
312
+ before do
313
+ expect(connection).to receive(:execute!).with(:resume, name).and_yield("foo", nil).and_return result
314
+ end
315
+
316
+ context "with external logging disabled" do
317
+ include_context "logged messages"
318
+ let(:expected_logs) {[
319
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
320
+ " INFO virtualmachine: Resuming Derelict::VirtualMachine 'testvm' from test\n",
321
+ ]}
322
+ end
323
+
324
+ context "with external logging enabled" do
325
+ let(:options) { {:log => true} }
326
+
327
+ include_context "logged messages"
328
+ let(:expected_logs) {[
329
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
330
+ " INFO virtualmachine: Resuming Derelict::VirtualMachine 'testvm' from test\n",
331
+ " INFO external: foo\n",
332
+ ]}
333
+ end
334
+ end
335
+
336
+ describe "#status" do
337
+ let(:result) { double("result", :stdout => stdout) }
338
+ let(:stdout) { double("stdout") }
339
+ subject { vm.status }
340
+
341
+ before do
342
+ expect(connection).to receive(:execute!).with(:status).and_return(result)
343
+ expect(Derelict::Parser::Status).to receive(:new).with(stdout).and_return(:return_value)
344
+ end
345
+
346
+ it "should parse status data from the connection" do
347
+ expect(subject).to be :return_value
348
+ end
349
+
350
+ include_context "logged messages"
351
+ let(:expected_logs) {[
352
+ "DEBUG virtualmachine: Successfully initialized Derelict::VirtualMachine 'testvm' from test\n",
353
+ " INFO virtualmachine: Retrieving Vagrant status for Derelict::VirtualMachine 'testvm' from test\n",
354
+ ]}
355
+ end
356
+ end
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+
3
+ describe Derelict do
4
+ describe "#instance" do
5
+ let(:instance) { double("instance") }
6
+ before {
7
+ expect(Derelict::Instance).to receive(:new).and_return(instance)
8
+ expect(instance).to receive(:validate!).and_return(instance)
9
+ }
10
+
11
+ subject { Derelict.instance }
12
+ it { should be instance }
13
+
14
+ include_context "logged messages"
15
+ let(:expected_logs) {[
16
+ " INFO derelict: Creating and validating new instance for '/Applications/Vagrant'\n"
17
+ ]}
18
+ end
19
+
20
+ describe "#debug!" do
21
+ let(:enabled) { double("enabled") }
22
+ let(:level) { double("level") }
23
+ let(:logger) { Derelict.logger }
24
+ let(:stderr) { Log4r::Outputter.stderr }
25
+
26
+ subject { Derelict.debug! :enabled => enabled, :level => level }
27
+
28
+ context "when enabling debug mode" do
29
+ before do
30
+ expect(logger).to receive(:level=).with(level).and_return(nil)
31
+ expect(logger).to receive(:outputters).and_return(Array.new)
32
+ expect(logger).to receive(:add).with(stderr).and_return(logger)
33
+ end
34
+
35
+ let(:enabled) { true }
36
+ it { should be Derelict }
37
+ end
38
+
39
+ context "when disabling debug mode" do
40
+ before do
41
+ expect(logger).to receive(:level=).with(Log4r::OFF).and_return(nil)
42
+ expect(logger).to receive(:remove).with("stderr").and_return(logger)
43
+ end
44
+
45
+ let(:real_level) { Log4r::OFF }
46
+ let(:enabled) { false }
47
+ it { should be Derelict }
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,32 @@
1
+ require "coverage_helper"
2
+ require "derelict"
3
+ require File.join(File.dirname(__FILE__), "support", "log_context")
4
+
5
+ derelict_logger = Derelict.logger
6
+ external_logger = Derelict.logger :type => :external
7
+ array_outputter = Derelict::Utils::Logger::ArrayOutputter.new "rspec"
8
+
9
+ RSpec.configure do |config|
10
+ config.before :each do
11
+ # Start each spec with an empty ArrayOutputter
12
+ array_outputter.flush
13
+
14
+ # Remove any outputters set on other loggers
15
+ Log4r::Logger.each {|fullname, logger| logger.outputters = [] }
16
+
17
+ # Add the ArrayOutputter to the base Derelict logger
18
+ derelict_logger.outputters = [array_outputter]
19
+ external_logger.outputters = [array_outputter]
20
+ end
21
+
22
+ # Forbid .should syntax
23
+ config.expect_with :rspec do |c|
24
+ c.syntax = :expect
25
+ end
26
+
27
+ # Run specs in random order to surface order dependencies. If you find an
28
+ # order dependency and want to debug it, you can fix the order by providing
29
+ # the seed, which is printed after each run.
30
+ # --seed 1234
31
+ config.order = "random"
32
+ end
@@ -0,0 +1,36 @@
1
+ require "spec_helper"
2
+
3
+ # Provides set-up and examples to assert logged messages
4
+ #
5
+ # Usage:
6
+ #
7
+ # * Ensure that the "subject" from the parent context will perform
8
+ # the action which is expected to produce the log messages
9
+ # * Provide a "let(:expected_logs)" block, defining which logs are
10
+ # expected to result from the action performed in the "before"
11
+ # block (otherwise, it defaults to expecting no log messages)
12
+ shared_context "logged messages" do
13
+ let(:outputter) { Log4r::Outputter["rspec"] }
14
+ let(:messages) { outputter.messages }
15
+
16
+ # Override this let block when including this shared context
17
+ let(:expected_logs) { [] }
18
+
19
+ # Add an additional context for readability in the output
20
+ describe "logged messages" do
21
+ before do
22
+ begin
23
+ subject
24
+ rescue Exception
25
+ end
26
+ end
27
+
28
+ it "should be an Array" do
29
+ expect(messages).to be_an Array
30
+ end
31
+
32
+ it "should contain the expected log messages" do
33
+ expect(messages).to eq expected_logs
34
+ end
35
+ end
36
+ end