inprovise 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +4 -0
  3. data/.travis.yml +28 -0
  4. data/Gemfile +9 -0
  5. data/LICENSE +8 -0
  6. data/README.md +197 -0
  7. data/Rakefile.rb +9 -0
  8. data/bin/rig +5 -0
  9. data/inprovise.gemspec +22 -0
  10. data/lib/inprovise/channel/ssh.rb +202 -0
  11. data/lib/inprovise/cli/group.rb +86 -0
  12. data/lib/inprovise/cli/node.rb +95 -0
  13. data/lib/inprovise/cli/provision.rb +84 -0
  14. data/lib/inprovise/cli.rb +105 -0
  15. data/lib/inprovise/cmd_channel.rb +100 -0
  16. data/lib/inprovise/cmd_helper.rb +150 -0
  17. data/lib/inprovise/control.rb +326 -0
  18. data/lib/inprovise/execution_context.rb +277 -0
  19. data/lib/inprovise/group.rb +67 -0
  20. data/lib/inprovise/helper/cygwin.rb +43 -0
  21. data/lib/inprovise/helper/linux.rb +181 -0
  22. data/lib/inprovise/helper/windows.rb +123 -0
  23. data/lib/inprovise/infra.rb +122 -0
  24. data/lib/inprovise/local_file.rb +120 -0
  25. data/lib/inprovise/logger.rb +79 -0
  26. data/lib/inprovise/node.rb +271 -0
  27. data/lib/inprovise/remote_file.rb +128 -0
  28. data/lib/inprovise/resolver.rb +36 -0
  29. data/lib/inprovise/script.rb +175 -0
  30. data/lib/inprovise/script_index.rb +46 -0
  31. data/lib/inprovise/script_runner.rb +110 -0
  32. data/lib/inprovise/sniff.rb +46 -0
  33. data/lib/inprovise/sniffer/linux.rb +64 -0
  34. data/lib/inprovise/sniffer/platform.rb +46 -0
  35. data/lib/inprovise/sniffer/unknown.rb +11 -0
  36. data/lib/inprovise/sniffer/windows.rb +32 -0
  37. data/lib/inprovise/template/inprovise.rb.erb +92 -0
  38. data/lib/inprovise/template.rb +38 -0
  39. data/lib/inprovise/trigger_runner.rb +36 -0
  40. data/lib/inprovise/version.rb +10 -0
  41. data/lib/inprovise.rb +145 -0
  42. data/test/cli_test.rb +314 -0
  43. data/test/cli_test_helper.rb +19 -0
  44. data/test/dsl_test.rb +43 -0
  45. data/test/fixtures/example.txt +1 -0
  46. data/test/fixtures/include.rb +4 -0
  47. data/test/fixtures/inprovise.rb +1 -0
  48. data/test/fixtures/myscheme.rb +1 -0
  49. data/test/infra_test.rb +189 -0
  50. data/test/local_file_test.rb +64 -0
  51. data/test/remote_file_test.rb +106 -0
  52. data/test/resolver_test.rb +66 -0
  53. data/test/script_index_test.rb +53 -0
  54. data/test/script_test.rb +56 -0
  55. data/test/test_helper.rb +237 -0
  56. metadata +182 -0
data/test/cli_test.rb ADDED
@@ -0,0 +1,314 @@
1
+ # CLI tests for Inprovise
2
+ #
3
+ # Author:: Martin Corino
4
+ # License:: Distributes under the same license as Ruby
5
+
6
+ require_relative 'cli_test_helper'
7
+
8
+ describe Inprovise::Cli do
9
+ describe "'node' commands" do
10
+ after :each do
11
+ reset_infrastructure!
12
+ end
13
+
14
+ it 'adds a node to the infrastructure' do
15
+ Inprovise::Cli.run(%w{node add -a host.address --no-sniff MyHost})
16
+ tgt = Inprovise::Infrastructure.find('MyHost')
17
+ tgt.must_be_instance_of Inprovise::Infrastructure::Node
18
+ tgt.name.must_equal 'MyHost'
19
+ tgt.host.must_equal 'host.address'
20
+ end
21
+
22
+ it 'adds a node with implicit host address' do
23
+ Inprovise::Cli.run(%w{node add --no-sniff my.host.addr})
24
+ tgt = Inprovise::Infrastructure.find('my.host.addr')
25
+ tgt.name.must_equal 'my.host.addr'
26
+ tgt.host.must_equal tgt.name
27
+ end
28
+
29
+ it 'adds a node with custom config' do
30
+ Inprovise::Cli.run(%w{node add -a host.address -c config1=value1 -c config2=123 -c config3=Time.now --no-sniff MyHost})
31
+ tgt = Inprovise::Infrastructure.find('MyHost')
32
+ tgt.config[:config1].must_equal 'value1'
33
+ tgt.config[:config2].must_equal 123
34
+ tgt.config[:config3].must_be_instance_of Time
35
+ end
36
+
37
+ it 'removes a node from the infrastructure' do
38
+ Inprovise::Cli.run(%w{node add -a host.address --no-sniff MyHost})
39
+ tgt = Inprovise::Infrastructure.find('MyHost')
40
+ tgt.must_be_instance_of Inprovise::Infrastructure::Node
41
+ tgt.name.must_equal 'MyHost'
42
+ Inprovise::Cli.run(%w{node remove MyHost})
43
+ tgt = Inprovise::Infrastructure.find('MyHost')
44
+ tgt.must_be_nil
45
+ end
46
+
47
+ it 'updates a node from the infrastructure' do
48
+ Inprovise::Cli.run(%w{node add -a host.address --no-sniff MyHost})
49
+ tgt = Inprovise::Infrastructure.find('MyHost')
50
+ tgt.must_be_instance_of Inprovise::Infrastructure::Node
51
+ tgt.name.must_equal 'MyHost'
52
+ tgt.config[:config1].must_be_nil
53
+ Inprovise::Cli.run(%w{node update -c config1=value1 --no-sniff MyHost})
54
+ tgt = Inprovise::Infrastructure.find('MyHost')
55
+ tgt.must_be_instance_of Inprovise::Infrastructure::Node
56
+ tgt.name.must_equal 'MyHost'
57
+ tgt.config[:config1].must_equal 'value1'
58
+ end
59
+ end
60
+
61
+ describe "'group' commands" do
62
+ after :each do
63
+ reset_infrastructure!
64
+ end
65
+
66
+ it 'adds a group to the infrastructure' do
67
+ Inprovise::Cli.run(%w{group add MyGroup})
68
+ tgt = Inprovise::Infrastructure.find('MyGroup')
69
+ tgt.must_be_instance_of Inprovise::Infrastructure::Group
70
+ tgt.name.must_equal 'MyGroup'
71
+ end
72
+
73
+ it 'removes a group from the infrastructure' do
74
+ Inprovise::Cli.run(%w{group add MyGroup})
75
+ tgt = Inprovise::Infrastructure.find('MyGroup')
76
+ tgt.must_be_instance_of Inprovise::Infrastructure::Group
77
+ tgt.name.must_equal 'MyGroup'
78
+ Inprovise::Cli.run(%w{group remove MyGroup})
79
+ tgt = Inprovise::Infrastructure.find('MyGroup')
80
+ tgt.must_be_nil
81
+ end
82
+
83
+ it 'adds a group with custom config' do
84
+ Inprovise::Cli.run(%w{group add -c config1=value1 -c config2=123 -c config3=Time.now MyGroup})
85
+ tgt = Inprovise::Infrastructure.find('MyGroup')
86
+ tgt.config[:config1].must_equal 'value1'
87
+ tgt.config[:config2].must_equal 123
88
+ tgt.config[:config3].must_be_instance_of Time
89
+ end
90
+
91
+ it 'adds a node to a group' do
92
+ Inprovise::Cli.run(%w{group add MyGroup})
93
+ grp = Inprovise::Infrastructure.find('MyGroup')
94
+ grp.must_be_instance_of Inprovise::Infrastructure::Group
95
+ grp.name.must_equal 'MyGroup'
96
+ Inprovise::Cli.run(%w{node add -g MyGroup --no-sniff MyHost})
97
+ tgt = Inprovise::Infrastructure.find('MyHost')
98
+ tgt.must_be_instance_of Inprovise::Infrastructure::Node
99
+ tgt.name.must_equal 'MyHost'
100
+ grp.targets.must_equal [tgt]
101
+ end
102
+
103
+ it 'adds a group including a node' do
104
+ Inprovise::Cli.run(%w{node add --no-sniff MyHost})
105
+ tgt = Inprovise::Infrastructure.find('MyHost')
106
+ tgt.must_be_instance_of Inprovise::Infrastructure::Node
107
+ tgt.name.must_equal 'MyHost'
108
+ Inprovise::Cli.run(%w{group add -t MyHost MyGroup})
109
+ grp = Inprovise::Infrastructure.find('MyGroup')
110
+ grp.must_be_instance_of Inprovise::Infrastructure::Group
111
+ grp.name.must_equal 'MyGroup'
112
+ grp.targets.must_equal [tgt]
113
+ end
114
+
115
+ it 'updates a group with a node' do
116
+ Inprovise::Cli.run(%w{node add --no-sniff MyHost})
117
+ tgt = Inprovise::Infrastructure.find('MyHost')
118
+ tgt.must_be_instance_of Inprovise::Infrastructure::Node
119
+ tgt.name.must_equal 'MyHost'
120
+ Inprovise::Cli.run(%w{group add MyGroup})
121
+ grp = Inprovise::Infrastructure.find('MyGroup')
122
+ grp.must_be_instance_of Inprovise::Infrastructure::Group
123
+ grp.name.must_equal 'MyGroup'
124
+ Inprovise::Cli.run(%w{group update -t MyHost MyGroup})
125
+ grp.targets.must_equal [tgt]
126
+ end
127
+
128
+ it 'updates a group with custom config' do
129
+ Inprovise::Cli.run(%w{group add MyGroup})
130
+ tgt = Inprovise::Infrastructure.find('MyGroup')
131
+ tgt.must_be_instance_of Inprovise::Infrastructure::Group
132
+ tgt.name.must_equal 'MyGroup'
133
+ tgt.config[:config1].must_be_nil
134
+ Inprovise::Cli.run(%w{group update -c config1='value1' MyGroup})
135
+ tgt.config[:config1].must_equal 'value1'
136
+ end
137
+ end
138
+
139
+ describe "'apply' command" do
140
+
141
+ it 'invokes the Inprovise::Controller#run appropriately' do
142
+ ctl = Inprovise::Controller.new
143
+ ctl.expects(:run_provisioning_command).with() do |cmd,tgt,opts,*args|
144
+ cmd.must_equal :apply
145
+ tgt.must_equal 'script'
146
+ args.shift.must_equal 'node'
147
+ end
148
+ Inprovise::Controller.stub(:new, ctl) do
149
+ Inprovise::Cli.run(%w{apply script node})
150
+ Inprovise.loaded?('inprovise.rb').must_equal true
151
+ end
152
+ end
153
+
154
+ it 'invokes the Inprovise::Controller#run with custom scheme' do
155
+ ctl = Inprovise::Controller.new
156
+ ctl.expects(:run_provisioning_command).with() do |cmd,tgt,opts,*args|
157
+ cmd.must_equal :apply
158
+ tgt.must_equal 'script'
159
+ args.shift.must_equal 'node'
160
+ end
161
+ Inprovise::Controller.stub(:new, ctl) do
162
+ Inprovise::Cli.run(%w{apply -s myscheme.rb script node})
163
+ Inprovise.loaded?('myscheme.rb').must_equal true
164
+ end
165
+ end
166
+
167
+ it 'invokes the Inprovise::Controller#run with custom config' do
168
+ ctl = Inprovise::Controller.new
169
+ ctl.expects(:run_provisioning_command).with() do |cmd,tgt,opts,*args|
170
+ cmd.must_equal :apply
171
+ opts[:config1] = 'value1'
172
+ tgt.must_equal 'script'
173
+ args.shift.must_equal 'node'
174
+ end
175
+ Inprovise::Controller.stub(:new, ctl) do
176
+ Inprovise::Cli.run(%w{apply -c config1=value1 script node})
177
+ Inprovise.loaded?('inprovise.rb').must_equal true
178
+ end
179
+ end
180
+
181
+ end
182
+
183
+ describe "'revert' command" do
184
+
185
+ it 'invokes the Inprovise::Controller#run appropriately' do
186
+ ctl = Inprovise::Controller.new
187
+ ctl.expects(:run_provisioning_command).with() do |cmd, tgt, opts,*args|
188
+ cmd.must_equal :revert
189
+ tgt.must_equal 'script'
190
+ args.shift.must_equal 'node'
191
+ end
192
+ Inprovise::Controller.stub(:new, ctl) do
193
+ Inprovise::Cli.run(%w{revert script node})
194
+ Inprovise.loaded?('inprovise.rb').must_equal true
195
+ end
196
+ end
197
+
198
+ it 'invokes the Inprovise::Controller#run with custom scheme' do
199
+ ctl = Inprovise::Controller.new
200
+ ctl.expects(:run_provisioning_command).with() do |cmd,tgt,opts,*args|
201
+ cmd.must_equal :revert
202
+ tgt.must_equal 'script'
203
+ args.shift.must_equal 'node'
204
+ end
205
+ Inprovise::Controller.stub(:new, ctl) do
206
+ Inprovise::Cli.run(%w{revert -s myscheme.rb script node})
207
+ Inprovise.loaded?('myscheme.rb').must_equal true
208
+ end
209
+ end
210
+
211
+ it 'invokes the Inprovise::Controller#run with custom config' do
212
+ ctl = Inprovise::Controller.new
213
+ ctl.expects(:run_provisioning_command).with() do |cmd,tgt,opts,*args|
214
+ cmd.must_equal :revert
215
+ opts[:config1] = 'value1'
216
+ tgt.must_equal 'script'
217
+ args.shift.must_equal 'node'
218
+ end
219
+ Inprovise::Controller.stub(:new, ctl) do
220
+ Inprovise::Cli.run(%w{revert -c config1=value1 script node})
221
+ Inprovise.loaded?('inprovise.rb').must_equal true
222
+ end
223
+ end
224
+
225
+ end
226
+
227
+ describe "'validate' command" do
228
+
229
+ it 'invokes the Inprovise::Controller#run appropriately' do
230
+ ctl = Inprovise::Controller.new
231
+ ctl.expects(:run_provisioning_command).with() do |cmd,tgt,opts,*args|
232
+ cmd.must_equal :validate
233
+ tgt.must_equal 'script'
234
+ args.shift.must_equal 'node'
235
+ end
236
+ Inprovise::Controller.stub(:new, ctl) do
237
+ Inprovise::Cli.run(%w{validate script node})
238
+ Inprovise.loaded?('inprovise.rb').must_equal true
239
+ end
240
+ end
241
+
242
+ it 'invokes the Inprovise::Controller#run with custom scheme' do
243
+ ctl = Inprovise::Controller.new
244
+ ctl.expects(:run_provisioning_command).with() do |cmd,tgt,opts,*args|
245
+ cmd.must_equal :validate
246
+ tgt.must_equal 'script'
247
+ args.shift.must_equal 'node'
248
+ end
249
+ Inprovise::Controller.stub(:new, ctl) do
250
+ Inprovise::Cli.run(%w{validate -s myscheme.rb script node})
251
+ Inprovise.loaded?('myscheme.rb').must_equal true
252
+ end
253
+ end
254
+
255
+ it 'invokes the Inprovise::Controller#run with custom config' do
256
+ ctl = Inprovise::Controller.new
257
+ ctl.expects(:run_provisioning_command).with() do |cmd,tgt,opts,*args|
258
+ cmd.must_equal :validate
259
+ opts[:config1] = 'value1'
260
+ tgt.must_equal 'script'
261
+ args.shift.must_equal 'node'
262
+ end
263
+ Inprovise::Controller.stub(:new, ctl) do
264
+ Inprovise::Cli.run(%w{validate -c config1=value1 script node})
265
+ Inprovise.loaded?('inprovise.rb').must_equal true
266
+ end
267
+ end
268
+
269
+ end
270
+
271
+ describe "'trigger' command" do
272
+
273
+ it 'invokes the Inprovise::Controller#run appropriately' do
274
+ ctl = Inprovise::Controller.new
275
+ ctl.expects(:run_provisioning_command).with() do |cmd,tgt,opts,*args|
276
+ cmd.must_equal :trigger
277
+ tgt.must_equal 'script:action'
278
+ args.shift.must_equal 'node'
279
+ end
280
+ Inprovise::Controller.stub(:new, ctl) do
281
+ Inprovise::Cli.run(%w{trigger script:action node})
282
+ Inprovise.loaded?('inprovise.rb').must_equal true
283
+ end
284
+ end
285
+
286
+ it 'invokes the Inprovise::Controller#run with custom scheme' do
287
+ ctl = Inprovise::Controller.new
288
+ ctl.expects(:run_provisioning_command).with() do |cmd,tgt,opts,*args|
289
+ cmd.must_equal :trigger
290
+ tgt.must_equal 'script:action'
291
+ args.shift.must_equal 'node'
292
+ end
293
+ Inprovise::Controller.stub(:new, ctl) do
294
+ Inprovise::Cli.run(%w{trigger -s myscheme.rb script:action node})
295
+ Inprovise.loaded?('myscheme.rb').must_equal true
296
+ end
297
+ end
298
+
299
+ it 'invokes the Inprovise::Controller#run with custom config' do
300
+ ctl = Inprovise::Controller.new
301
+ ctl.expects(:run_provisioning_command).with() do |cmd,tgt,opts,*args|
302
+ cmd.must_equal :trigger
303
+ opts[:config1] = 'value1'
304
+ tgt.must_equal 'script:action'
305
+ args.shift.must_equal 'node'
306
+ end
307
+ Inprovise::Controller.stub(:new, ctl) do
308
+ Inprovise::Cli.run(%w{trigger -c config1=value1 script:action node})
309
+ Inprovise.loaded?('inprovise.rb').must_equal true
310
+ end
311
+ end
312
+
313
+ end
314
+ end
@@ -0,0 +1,19 @@
1
+ # CLI-test helper for Inprovise
2
+ #
3
+ # Author:: Martin Corino
4
+ # License:: Distributes under the same license as Ruby
5
+
6
+ require_relative './test_helper'
7
+
8
+ # prevent CLI error handling from exiting
9
+ class Inprovise::Cli
10
+ on_error do |exception|
11
+ # Error logic here
12
+ # return false to skip default error handling
13
+ $stderr.puts "ERROR: #{exception.message}".red
14
+ if Inprovise.verbosity > 0
15
+ $stderr.puts "#{exception}\n#{exception.backtrace.join("\n")}"
16
+ end
17
+ true
18
+ end
19
+ end
data/test/dsl_test.rb ADDED
@@ -0,0 +1,43 @@
1
+ # DSL tests for Inprovise
2
+ #
3
+ # Author:: Martin Corino
4
+ # License:: Distributes under the same license as Ruby
5
+
6
+ require_relative 'test_helper'
7
+
8
+ describe Inprovise::DSL do
9
+ describe "'script' command" do
10
+ after :each do
11
+ reset_script_index!
12
+ end
13
+
14
+ it 'adds a script to the index' do
15
+ Inprovise::DSL.script('my-script') { nil }
16
+ Inprovise::ScriptIndex.default.get('my-script').must_be_instance_of Inprovise::Script
17
+ end
18
+
19
+ it 'creates a new script based on the supplied name' do
20
+ Inprovise::DSL.script('my-script') { nil }
21
+ script = Inprovise::ScriptIndex.default.get('my-script')
22
+ script.name.must_equal 'my-script'
23
+ end
24
+
25
+ it 'executes the given definition block in the script context' do
26
+ Inprovise::DSL.script('my-script') { depends_on 'other-script' }
27
+ script = Inprovise::ScriptIndex.default.get('my-script')
28
+ script.dependencies.must_equal ['other-script']
29
+ end
30
+ end
31
+
32
+ describe "'include' command" do
33
+ after :each do
34
+ reset_script_index!
35
+ end
36
+
37
+ it 'includes a provisioning scheme' do
38
+ Inprovise::DSL.include(File.join(File.dirname(__FILE__), 'fixtures', 'include.rb'))
39
+ script = Inprovise::ScriptIndex.default.get('included')
40
+ script.name.must_equal 'included'
41
+ end
42
+ end
43
+ end
@@ -0,0 +1 @@
1
+ example
@@ -0,0 +1,4 @@
1
+ # test scheme to include
2
+
3
+ script 'included' do
4
+ end
@@ -0,0 +1 @@
1
+ # default test scheme
@@ -0,0 +1 @@
1
+ # custom test scheme
@@ -0,0 +1,189 @@
1
+ # Infrastructure tests for Inprovise
2
+ #
3
+ # Author:: Martin Corino
4
+ # License:: Distributes under the same license as Ruby
5
+
6
+ require_relative 'test_helper'
7
+
8
+ describe Inprovise::Infrastructure do
9
+ after :each do
10
+ reset_infrastructure!
11
+ end
12
+
13
+ describe '#new' do
14
+ it 'creates a new node from parameters' do
15
+ node = Inprovise::Infrastructure::Node.new('myNode')
16
+ node.must_equal Inprovise::Infrastructure.find('myNode')
17
+ node.host.must_equal 'myNode'
18
+ node.user.must_equal 'root'
19
+ node = Inprovise::Infrastructure::Node.new('myNode2', host: 'my.node2.addr', user: 'me')
20
+ node.must_equal Inprovise::Infrastructure.find('myNode2')
21
+ node.host.must_equal 'my.node2.addr'
22
+ node.user.must_equal 'me'
23
+ end
24
+
25
+ it 'creates a new group from parameters' do
26
+ grp = Inprovise::Infrastructure::Group.new('myGroup')
27
+ grp.must_equal Inprovise::Infrastructure.find('myGroup')
28
+ grp = Inprovise::Infrastructure::Group.new('myGroup2', myval: 'value')
29
+ grp.must_equal Inprovise::Infrastructure.find('myGroup2')
30
+ grp.config[:myval].must_equal 'value'
31
+ end
32
+ end
33
+
34
+ describe '#get/#set' do
35
+
36
+ before :each do
37
+ Inprovise::Infrastructure::Node.new('myNode')
38
+ Inprovise::Infrastructure::Node.new('myNode2', host: 'my.node2.addr', user: 'me')
39
+ Inprovise::Infrastructure::Group.new('myGroup')
40
+ Inprovise::Infrastructure::Group.new('myGroup2', myval: 'value')
41
+ end
42
+
43
+ it 'sets a property value' do
44
+ node = Inprovise::Infrastructure.find('myNode')
45
+ node.set(:setting1, 5)
46
+ node.config[:setting1].must_equal 5
47
+ end
48
+
49
+ it 'gets a property value' do
50
+ grp = Inprovise::Infrastructure.find('myGroup2')
51
+ grp.get(:myval).must_equal 'value'
52
+ end
53
+
54
+ end
55
+
56
+ describe '#add_to/#remove_from' do
57
+
58
+ before :each do
59
+ Inprovise::Infrastructure::Node.new('myNode')
60
+ Inprovise::Infrastructure::Node.new('myNode2', host: 'my.node2.addr', user: 'me')
61
+ Inprovise::Infrastructure::Group.new('myGroup')
62
+ Inprovise::Infrastructure::Group.new('myGroup2', myval: 'value')
63
+ end
64
+
65
+ it 'adds a node to a group' do
66
+ node = Inprovise::Infrastructure.find('myNode')
67
+ node.add_to(Inprovise::Infrastructure.find('myGroup'))
68
+ grp = Inprovise::Infrastructure.find('myGroup')
69
+ assert grp.includes?('myNode')
70
+ assert grp.includes?(node)
71
+ end
72
+
73
+ it 'adds a group to a group' do
74
+ grp = Inprovise::Infrastructure.find('myGroup')
75
+ grp.add_to(Inprovise::Infrastructure.find('myGroup2'))
76
+ grp2 = Inprovise::Infrastructure.find('myGroup2')
77
+ assert grp2.includes?('myGroup')
78
+ assert grp2.includes?(grp)
79
+ end
80
+
81
+ it 'allows overlapping inclusion' do
82
+ node = Inprovise::Infrastructure.find('myNode')
83
+ node.add_to(Inprovise::Infrastructure.find('myGroup'))
84
+ grp = Inprovise::Infrastructure.find('myGroup')
85
+ grp.add_to(Inprovise::Infrastructure.find('myGroup2'))
86
+ node.add_to(Inprovise::Infrastructure.find('myGroup2'))
87
+ grp2 = Inprovise::Infrastructure.find('myGroup2')
88
+ assert grp.includes?('myNode')
89
+ assert grp.includes?(node)
90
+ assert grp2.includes?('myNode')
91
+ assert grp2.includes?(node)
92
+ end
93
+
94
+ it 'resolves inclusion recursively' do
95
+ node = Inprovise::Infrastructure.find('myNode')
96
+ grp = Inprovise::Infrastructure.find('myGroup')
97
+ node.add_to(grp)
98
+ grp.add_to(Inprovise::Infrastructure.find('myGroup2'))
99
+ grp2 = Inprovise::Infrastructure.find('myGroup2')
100
+ assert grp2.includes?('myGroup')
101
+ assert grp2.includes?(grp)
102
+ assert grp2.includes?('myNode')
103
+ assert grp2.includes?(node)
104
+ end
105
+
106
+ it 'removes targets non-recursively' do
107
+ node = Inprovise::Infrastructure.find('myNode')
108
+ grp = Inprovise::Infrastructure.find('myGroup')
109
+ node.add_to(grp)
110
+ grp.add_to(Inprovise::Infrastructure.find('myGroup2'))
111
+ grp2 = Inprovise::Infrastructure.find('myGroup2')
112
+ assert grp2.includes?('myGroup')
113
+ assert grp2.includes?(grp)
114
+ assert grp2.includes?('myNode')
115
+ assert grp2.includes?(node)
116
+ node.remove_from(grp2)
117
+ assert grp2.includes?(node)
118
+ node.remove_from(grp)
119
+ assert !grp2.includes?(node)
120
+ assert grp2.includes?(grp)
121
+ grp.remove_from(grp2)
122
+ assert !grp2.includes?(grp)
123
+ end
124
+
125
+ end
126
+
127
+ describe '#targets' do
128
+
129
+ before :each do
130
+ n = Inprovise::Infrastructure::Node.new('myNode')
131
+ n2 = Inprovise::Infrastructure::Node.new('myNode2', host: 'my.node2.addr', user: 'me')
132
+ g = Inprovise::Infrastructure::Group.new('myGroup')
133
+ g2 = Inprovise::Infrastructure::Group.new('myGroup2', myval: 'value')
134
+ n.add_to(g)
135
+ n.add_to(g2)
136
+ n2.add_to(g2)
137
+ g.add_to(g2)
138
+ end
139
+
140
+
141
+ it 'resolves included (device) targets' do
142
+ grp = Inprovise::Infrastructure.find('myGroup')
143
+ grp.targets.size.must_equal 1
144
+ grp.targets.must_include Inprovise::Infrastructure.find('myNode')
145
+ end
146
+
147
+ it 'resolves (device) targets recursively and uniquely' do
148
+ grp2 = Inprovise::Infrastructure.find('myGroup2')
149
+ grp2.targets.size.must_equal 2
150
+ grp2.targets.must_include Inprovise::Infrastructure.find('myNode')
151
+ grp2.targets.must_include Inprovise::Infrastructure.find('myNode2')
152
+ end
153
+
154
+ end
155
+
156
+ describe '#targets_with_config' do
157
+
158
+ before :each do
159
+ n = Inprovise::Infrastructure::Node.new('myNode', nodeval: 666)
160
+ n2 = Inprovise::Infrastructure::Node.new('myNode2', host: 'my.node2.addr', user: 'me')
161
+ g = Inprovise::Infrastructure::Group.new('myGroup', myval: 'value')
162
+ g2 = Inprovise::Infrastructure::Group.new('myGroup2', myval2: 'value2')
163
+ n.add_to(g)
164
+ n.add_to(g2)
165
+ n2.add_to(g2)
166
+ g.add_to(g2)
167
+ end
168
+
169
+ it 'resolves (device) targets and config recursively and uniquely' do
170
+ grp = Inprovise::Infrastructure.find('myGroup')
171
+ grp.targets_with_config.size.must_equal 1
172
+ grp.targets_with_config.must_include Inprovise::Infrastructure.find('myNode')
173
+ grp.targets_with_config[Inprovise::Infrastructure.find('myNode')][:nodeval].must_equal 666
174
+ grp.targets_with_config[Inprovise::Infrastructure.find('myNode')][:myval].must_equal 'value'
175
+ grp.targets_with_config[Inprovise::Infrastructure.find('myNode')][:myval2].must_be_nil
176
+ grp2 = Inprovise::Infrastructure.find('myGroup2')
177
+ grp2.targets_with_config.size.must_equal 2
178
+ grp2.targets_with_config.must_include Inprovise::Infrastructure.find('myNode')
179
+ grp2.targets_with_config[Inprovise::Infrastructure.find('myNode')][:nodeval].must_equal 666
180
+ grp2.targets_with_config[Inprovise::Infrastructure.find('myNode')][:myval].must_equal 'value'
181
+ grp2.targets_with_config[Inprovise::Infrastructure.find('myNode')][:myval2].must_equal 'value2'
182
+ grp2.targets_with_config.must_include Inprovise::Infrastructure.find('myNode2')
183
+ grp2.targets_with_config[Inprovise::Infrastructure.find('myNode2')][:nodeval].must_be_nil
184
+ grp2.targets_with_config[Inprovise::Infrastructure.find('myNode2')][:myval].must_be_nil
185
+ grp2.targets_with_config[Inprovise::Infrastructure.find('myNode2')][:myval2].must_equal 'value2'
186
+ end
187
+
188
+ end
189
+ end
@@ -0,0 +1,64 @@
1
+ # Local file tests for Inprovise
2
+ #
3
+ # Author:: Martin Corino
4
+ # License:: Distributes under the same license as Ruby
5
+
6
+ require_relative 'test_helper'
7
+
8
+ describe Inprovise::LocalFile do
9
+ before :each do
10
+ @local_file_path = File.join(File.dirname(__FILE__), 'fixtures', 'example.txt')
11
+ @local_file = Inprovise::LocalFile.new(@local_file_path)
12
+ end
13
+
14
+ describe 'path' do
15
+ it 'returns the absolute path of a local file' do
16
+ @local_file.path.must_equal @local_file_path
17
+ end
18
+ end
19
+
20
+ describe 'hash' do
21
+ it 'returns the sha1 of a local file' do
22
+ @local_file.hash.must_equal 'c3499c2729730a7f807efb8676a92dcb6f8a3f8f'
23
+ end
24
+ end
25
+
26
+ describe 'matches' do
27
+ it 'returns true if the passed file has a matching hash' do
28
+ @local_file.matches?(@local_file).must_equal true
29
+ end
30
+ end
31
+
32
+ describe 'exists?' do
33
+ it 'checks if a file exists' do
34
+ @local_file.exists?.must_equal true
35
+ end
36
+ end
37
+
38
+ describe 'copy_to' do
39
+ before(:each) { @destination = Inprovise::LocalFile.new("/tmp/example-#{Time.now.to_i}.txt") }
40
+ after(:each) { @destination.delete! }
41
+
42
+ it 'copies a file to another local location' do
43
+ @destination.exists?.must_equal false
44
+ @local_file.copy_to(@destination).must_equal @destination
45
+ @destination.exists?.must_equal true
46
+ end
47
+
48
+ it 'copies a file to a remote location by uploading it' do
49
+ @remote_destination_context = mock()
50
+ @remote_destination = Inprovise::RemoteFile.new(@remote_destination_context, '/tmp/example-dest.txt')
51
+ @remote_destination_context.expects(:upload).with(@local_file.path, @remote_destination.path)
52
+ @local_file.copy_to(@remote_destination).must_equal @remote_destination
53
+ end
54
+ end
55
+
56
+ describe 'set_permissions' do
57
+ it 'sets permissions on the file based on a mask' do
58
+ @local_file.set_permissions(0664).must_equal @local_file
59
+ @local_file.permissions.must_equal 0664
60
+ @local_file.set_permissions(0644).must_equal @local_file
61
+ @local_file.permissions.must_equal 0644
62
+ end
63
+ end
64
+ end