omf_rc 6.0.0.pre.5 → 6.0.0.pre.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/Rakefile +0 -6
  2. data/bin/omf_rc +2 -2
  3. data/lib/omf_rc/resource_factory.rb +10 -4
  4. data/lib/omf_rc/resource_proxy/abstract_resource.rb +120 -79
  5. data/lib/omf_rc/resource_proxy/generic_application.rb +471 -0
  6. data/lib/omf_rc/resource_proxy/net.rb +7 -0
  7. data/lib/omf_rc/resource_proxy/node.rb +33 -7
  8. data/lib/omf_rc/resource_proxy/openflow_slice.rb +79 -0
  9. data/lib/omf_rc/resource_proxy/openflow_slice_factory.rb +71 -0
  10. data/lib/omf_rc/resource_proxy/wlan.rb +20 -0
  11. data/lib/omf_rc/resource_proxy_dsl.rb +142 -8
  12. data/lib/omf_rc/util/common_tools.rb +61 -0
  13. data/lib/omf_rc/util/hostapd.rb +52 -0
  14. data/lib/omf_rc/util/ip.rb +28 -0
  15. data/lib/omf_rc/util/iw.rb +119 -6
  16. data/lib/omf_rc/util/mock.rb +2 -1
  17. data/lib/omf_rc/util/openflow_tools.rb +103 -0
  18. data/lib/omf_rc/util/platform_tools.rb +164 -0
  19. data/lib/omf_rc/util/wpa.rb +42 -0
  20. data/lib/omf_rc/version.rb +1 -1
  21. data/omf_rc.gemspec +3 -1
  22. data/test/fixture/ip/addr_show +5 -0
  23. data/test/fixture/iw/info +4 -0
  24. data/test/fixture/sys/class/ieee80211/phy0/device/uevent +6 -0
  25. data/test/fixture/sys/class/ieee80211/phy0/uevent +0 -0
  26. data/test/fixture/sys/class/net/eth0/device/uevent +6 -0
  27. data/test/fixture/sys/class/net/eth0/uevent +2 -0
  28. data/test/fixture/sys/class/net/wlan0/device/uevent +6 -0
  29. data/test/fixture/sys/class/net/wlan0/uevent +3 -0
  30. data/test/omf_rc/message_process_error_spec.rb +11 -0
  31. data/test/omf_rc/resource_factory_spec.rb +8 -1
  32. data/test/omf_rc/resource_proxy/abstract_resource_spec.rb +57 -1
  33. data/test/omf_rc/resource_proxy/generic_application_spec.rb +347 -0
  34. data/test/omf_rc/resource_proxy/mock_spec.rb +15 -0
  35. data/test/omf_rc/resource_proxy/node_spec.rb +32 -1
  36. data/test/omf_rc/resource_proxy_dsl_spec.rb +81 -10
  37. data/test/omf_rc/util/common_tools_spec.rb +30 -0
  38. data/test/omf_rc/util/ip_spec.rb +51 -0
  39. data/test/omf_rc/util/iw_spec.rb +136 -25
  40. data/test/omf_rc/util/mock_spec.rb +26 -0
  41. data/test/omf_rc/util/mod_spec.rb +8 -11
  42. data/test/test_helper.rb +11 -0
  43. metadata +62 -6
  44. data/lib/omf_rc/resource_proxy/wifi.rb +0 -8
  45. data/test/mock_helper.rb +0 -15
@@ -0,0 +1,347 @@
1
+ require 'test_helper'
2
+ require 'omf_rc/resource_proxy/generic_application'
3
+
4
+
5
+
6
+ describe OmfRc::ResourceProxy::GenericApplication do
7
+
8
+ before do
9
+ @app_test = OmfRc::ResourceFactory.new(:generic_application, { hrn: 'an_application' })
10
+ @app_test.comm = MiniTest::Mock.new
11
+ @app_test.comm.expect :publish, nil, [String,OmfCommon::Message]
12
+ end
13
+
14
+ describe "when initialised" do
15
+ it "must respond to an 'on_app_event' call back" do
16
+ #OmfRc::ResourceProxy::GenericApplication.method_defined?(:on_app_event).must_equal true
17
+ @app_test.must_respond_to :on_app_event
18
+ end
19
+
20
+ it "must have its properties set to sensible initial values" do
21
+ @app_test.request_state.to_sym.must_equal :stop
22
+ @app_test.request_tarball_install_path.must_equal '/'
23
+ @app_test.request_force_tarball_install.must_equal 'false'
24
+ @app_test.request_installed.must_equal 'false'
25
+ @app_test.request_map_err_to_out.must_equal 'false'
26
+ end
27
+
28
+ it "must be able to configure/request its basic properties" do
29
+ basic_prop = %w(binary_path pkg_tarball pkg_ubuntu pkg_fedora force_tarball_install map_err_to_out tarball_install_path)
30
+ basic_prop.each do |p|
31
+ @app_test.method("configure_#{p}".to_sym).call('foo')
32
+ @app_test.method("request_#{p}".to_sym).call.must_equal 'foo'
33
+ end
34
+ end
35
+
36
+ it "must be able to tell which platform it is running on (either: unknown | ubuntu | fedora)" do
37
+ @app_test.request_platform.must_match /unknown|ubuntu|fedora/
38
+ end
39
+
40
+ it "must be able to configure its environments property" do
41
+ # First give it a valid environment property
42
+ test_environments = { 'foo' => 123, 'bar_bar' => 'bar_123' }
43
+ @app_test.method(:configure_environments).call(test_environments)
44
+ # Then give it an invalid one, which it should ignore
45
+ @app_test.method(:configure_environments).call(nil)
46
+ @app_test.property.environments.must_be_kind_of Hash
47
+ @app_test.property.environments['foo'].must_equal 123
48
+ @app_test.property.environments['bar_bar'].must_equal 'bar_123'
49
+ end
50
+ end
51
+
52
+ describe "when configuring its parameters property" do
53
+ it "must be able to set its parameters property" do
54
+ # First give it a valid parameter property
55
+ test_params = { :p1 => { :cmd => '--foo', :value => 'foo'} }
56
+ @app_test.method(:configure_parameters).call(test_params)
57
+ # Then give it a couple of invalid ones, which it should ignore
58
+ @app_test.stub :log_inform_error, nil do
59
+ @app_test.method(:configure_parameters).call(nil)
60
+ @app_test.method(:configure_parameters).call( { :p1 => nil } )
61
+ end
62
+ @app_test.property.parameters.must_be_kind_of Hash
63
+ @app_test.property.parameters[:p1].must_be_kind_of Hash
64
+ @app_test.property.parameters[:p1][:cmd].must_equal '--foo'
65
+ @app_test.property.parameters[:p1][:value].must_equal 'foo'
66
+ end
67
+
68
+ it "must be able to merge new parameters into existing ones" do
69
+ old_params = { :p1 => { :cmd => '--foo', :default => 'old_foo'} }
70
+ @app_test.property.parameters = old_params
71
+ new_params = { :p1 => { :default => 'new_foo', :value => 'val_foo'},
72
+ :p2 => { :cmd => 'bar', :default => 'bar_bar'} }
73
+ @app_test.method(:configure_parameters).call(new_params)
74
+ @app_test.property.parameters[:p1][:cmd].must_equal '--foo'
75
+ @app_test.property.parameters[:p1][:default].must_equal 'new_foo'
76
+ @app_test.property.parameters[:p1][:value].must_equal 'val_foo'
77
+ @app_test.property.parameters[:p2][:cmd].must_equal 'bar'
78
+ @app_test.property.parameters[:p2][:default].must_equal 'bar_bar'
79
+ end
80
+
81
+ it "must be able to sanitize its parameters property" do
82
+ test_params = { :p1 => { :mandatory => 'true', :dynamic => false},
83
+ :p2 => { :mandatory => true, :dynamic => 'false'},
84
+ :p3 => { :type => 'Boolean', :default => true, :value => 'false'},
85
+ :p4 => { :type => 'Boolean', :default => 'true', :value => false} }
86
+ @app_test.method(:configure_parameters).call(test_params)
87
+ @app_test.property.parameters[:p1][:mandatory].must_be_kind_of TrueClass
88
+ @app_test.property.parameters[:p1][:dynamic].must_be_kind_of FalseClass
89
+ @app_test.property.parameters[:p2][:mandatory].must_be_kind_of TrueClass
90
+ @app_test.property.parameters[:p2][:dynamic].must_be_kind_of FalseClass
91
+ @app_test.property.parameters[:p3][:default].must_be_kind_of TrueClass
92
+ @app_test.property.parameters[:p3][:value].must_be_kind_of FalseClass
93
+ @app_test.property.parameters[:p4][:default].must_be_kind_of TrueClass
94
+ @app_test.property.parameters[:p4][:value].must_be_kind_of FalseClass
95
+ end
96
+
97
+ it "must be able to validate the correct type of a defined parameter" do
98
+ test_params = { :p1 => { :type => 'String', :default => 'foo', :value => 'bar'},
99
+ :p2 => { :type => 'Numeric', :default => 123, :value => 456},
100
+ :p3 => { :type => 'Boolean', :default => true, :value => true},
101
+ :p4 => { :type => 'Boolean'},
102
+ :p5 => { :type => 'Boolean', :default => true},
103
+ :p6 => { :type => 'Boolean', :value => true},
104
+ :p7 => { :type => 'Numeric'},
105
+ :p8 => { :type => 'Numeric', :default => 123},
106
+ :p9 => { :type => 'Numeric', :value => 123} }
107
+ @app_test.method(:configure_parameters).call(test_params)
108
+ @app_test.property.parameters[:p1][:default].must_be_kind_of String
109
+ @app_test.property.parameters[:p1][:value].must_be_kind_of String
110
+ @app_test.property.parameters[:p2][:default].must_be_kind_of Numeric
111
+ @app_test.property.parameters[:p2][:value].must_be_kind_of Numeric
112
+ @app_test.property.parameters[:p3][:default].must_be_kind_of TrueClass
113
+ @app_test.property.parameters[:p3][:value].must_be_kind_of TrueClass
114
+ @app_test.property.parameters[:p4][:default].must_be_nil
115
+ @app_test.property.parameters[:p4][:value].must_be_nil
116
+ @app_test.property.parameters[:p5][:default].must_be_kind_of TrueClass
117
+ @app_test.property.parameters[:p6][:value].must_be_kind_of TrueClass
118
+ @app_test.property.parameters[:p7][:default].must_be_nil
119
+ @app_test.property.parameters[:p7][:value].must_be_nil
120
+ @app_test.property.parameters[:p8][:default].must_be_kind_of Numeric
121
+ @app_test.property.parameters[:p9][:value].must_be_kind_of Numeric
122
+ end
123
+
124
+ it "must be able to detect incorrect type setting for a defined parameter, and DO NOT update the parameter in that case" do
125
+ old_params = { :p1 => { :type => 'String', :value => 'foo'},
126
+ :p2 => { :type => 'Numeric', :default => 123, :value => 456 },
127
+ :p3 => { :type => 'Boolean', :default => true, :value => true} }
128
+ @app_test.property.parameters = old_params
129
+ new_params = { :p1 => { :type => 'String', :value => true},
130
+ :p2 => { :type => 'Numeric', :default => 456, :value => '456' },
131
+ :p3 => { :type => 'Boolean', :default => 123, :value => false} }
132
+ @app_test.stub :log_inform_error, nil do
133
+ @app_test.method(:configure_parameters).call(new_params)
134
+ end
135
+ @app_test.property.parameters[:p1][:value].must_equal 'foo'
136
+ @app_test.property.parameters[:p2][:default].must_equal 123
137
+ @app_test.property.parameters[:p2][:value].must_equal 456
138
+ @app_test.property.parameters[:p3][:default].must_be_kind_of TrueClass
139
+ @app_test.property.parameters[:p3][:value].must_be_kind_of TrueClass
140
+ end
141
+
142
+ it "must update any valid dynamic parameter with the given value" do
143
+ # set the parameter as dynamic
144
+ params1 = { :p1 => { :cmd => '--foo', :default => 'old_foo', :dynamic => true} }
145
+ @app_test.method(:configure_parameters).call(params1)
146
+ # then update it
147
+ params2 = { :p1 => { :value => 'bar'} }
148
+ @app_test.property.state = :run
149
+ class ExecApp
150
+ def initialize(app_id, res, cmd_line, err_out_map); end
151
+ def ExecApp.[](id); return ExecApp.new(nil,nil,nil,nil) end
152
+ def stdin(msg); msg.must_equal '--foo bar' end
153
+ end
154
+ @app_test.method(:configure_parameters).call(params2)
155
+ end
156
+
157
+ end
158
+
159
+ describe "when receiving an event from a running application instance" do
160
+ it "must publish an INFORM message to relay that event" do
161
+ @app_test.on_app_event('STDOUT', 'app_instance_id', 'Some text here').must_be_nil
162
+ assert @app_test.comm.verify
163
+ end
164
+
165
+ it "must increments its event_sequence after publishig that INFORM message" do
166
+ i = @app_test.property.event_sequence
167
+ @app_test.on_app_event('STDOUT', 'app_instance_id', 'Some text here')
168
+ @app_test.property.event_sequence.must_equal i+1
169
+ end
170
+
171
+ it "must switch its state to 'stop' if the event is of a type 'DONE'" do
172
+ @app_test.on_app_event('DONE.OK', 'app_instance_id', 'Some text here')
173
+ @app_test.request_state.to_sym.must_equal :stop
174
+ end
175
+
176
+ it "must set installed property to true if the event is 'DONE.OK' and the app_id's suffix is '_INSTALL'" do
177
+ @app_test.on_app_event('DONE.OK', 'app_instance_id_INSTALL', 'Some text here')
178
+ @app_test.request_state.to_sym.must_equal :stop
179
+ @app_test.request_installed.must_equal "true"
180
+ end
181
+ end
182
+
183
+ describe "when configuring its state property to :install" do
184
+ it "must do nothing if its original state is not :stop" do
185
+ @app_test.property.state = :run
186
+ @app_test.method(:configure_state).call(:install)
187
+ @app_test.property.state.must_equal :run
188
+ end
189
+
190
+ it "must do nothing if its original state is :stop and it is already installed" do
191
+ @app_test.property.state = :stop
192
+ @app_test.property.installed = true
193
+ @app_test.method(:configure_state).call(:install)
194
+ @app_test.property.state.must_equal :stop
195
+ end
196
+
197
+ it "must use the tarball install method if it does not know its OS platform or if force_tarball_install is set" do
198
+ @app_test.property.pkg_tarball = 'foo'
199
+ @app_test.property.tarball_install_path = '/bar/'
200
+ @stub_tarball_tasks = Proc.new do |pkg,path|
201
+ pkg.must_equal 'foo'
202
+ path.must_equal '/bar/'
203
+ @did_call_install_tarball = true
204
+ end
205
+ def call_configure
206
+ @app_test.stub :install_tarball, @stub_tarball_tasks do
207
+ @app_test.method(:configure_state).call(:install).must_equal :install
208
+ @did_call_install_tarball.must_equal true
209
+ end
210
+ end
211
+ # Unknown Platform...
212
+ @did_call_install_tarball = false
213
+ @app_test.property.state = :stop
214
+ @app_test.property.installed = false
215
+ @app_test.property.platform = :unknown
216
+ call_configure
217
+ # Force Install Tarball...
218
+ @did_call_install_tarball = false
219
+ @app_test.property.state = :stop
220
+ @app_test.property.installed = false
221
+ @app_test.property.platform = :ubuntu
222
+ @app_test.property.force_tarball_install = true
223
+ call_configure
224
+ end
225
+
226
+ it "must use the ubuntu install method if its OS platform is ubuntu" do
227
+ @did_call_install_ubuntu = false
228
+ @app_test.property.state = :stop
229
+ @app_test.property.installed = false
230
+ @app_test.property.platform = :ubuntu
231
+ @app_test.property.pkg_ubuntu = 'foo'
232
+ @stub_ubuntu_tasks = Proc.new do |pkg|
233
+ pkg.must_equal 'foo'
234
+ @did_call_install_ubuntu = true
235
+ end
236
+ @app_test.stub :install_ubuntu, @stub_ubuntu_tasks do
237
+ @app_test.method(:configure_state).call(:install).must_equal :install
238
+ @did_call_install_ubuntu.must_equal true
239
+ end
240
+ end
241
+
242
+ it "must use the fedora install method if its OS platform is fedora" do
243
+ @did_call_install_fedora = false
244
+ @app_test.property.state = :stop
245
+ @app_test.property.installed = false
246
+ @app_test.property.platform = :fedora
247
+ @app_test.property.pkg_fedora = 'foo'
248
+ @stub_fedora_tasks = Proc.new do |pkg|
249
+ pkg.must_equal 'foo'
250
+ @did_call_install_fedora = true
251
+ end
252
+ @app_test.stub :install_fedora, @stub_fedora_tasks do
253
+ @app_test.method(:configure_state).call(:install).must_equal :install
254
+ @did_call_install_fedora.must_equal true
255
+ end
256
+ end
257
+ end
258
+
259
+ describe "when configuring its state property to :run" do
260
+ it "must do nothing if its original state is :install" do
261
+ @app_test.property.state = :install
262
+ @app_test.method(:configure_state).call(:run)
263
+ @app_test.property.state.must_equal :install
264
+ end
265
+
266
+ it "must get back to the :run state if its original state is :pause" do
267
+ @app_test.property.state = :pause
268
+ @app_test.method(:configure_state).call(:run)
269
+ @app_test.property.state.must_equal :run
270
+ end
271
+
272
+ it "must do nothing if its binary path is not set" do
273
+ @app_test.property.state = :stop
274
+ @app_test.method(:configure_state).call(:run)
275
+ @app_test.property.state.must_equal :stop
276
+ end
277
+
278
+ it "must start an app using ExecApp and a correct command line if its original state is :stop" do
279
+ class ExecApp
280
+ def initialize(app_id, res, cmd_line, err_out_map)
281
+ app_id.must_equal "an_application"
282
+ res.must_be_kind_of OmfRc::ResourceProxy::AbstractResource
283
+ cmd_line.must_equal "env -i FOO=123 BAR_BAR='bar_123' my_cmd 123 -param1 bar p3 hello "
284
+ err_out_map.must_equal false
285
+ end
286
+ end
287
+ @app_test.property.state = :stop
288
+ @app_test.property.binary_path = "my_cmd"
289
+ test_env = { 'foo' => 123, 'bar_bar' => 'bar_123' }
290
+ test_params = { :p1 => { :type => 'String', :mandatory => true, :cmd => '-param1', :default => 'foo', :value => 'bar', :order => 2},
291
+ :p2 => { :type => 'Numeric', :mandatory => true, :default => 123, :order => 1 },
292
+ :p3 => { :type => 'Boolean', :cmd => 'p3', :default => false, :value => true},
293
+ :p4 => { :type => 'String', :default => 'hi', :value => 'hello'},
294
+ :p5 => { :type => 'Numeric', :default => 456}, }
295
+ @app_test.method(:configure_environments).call(test_env)
296
+ @app_test.method(:configure_parameters).call(test_params)
297
+ @app_test.method(:configure_state).call(:run)
298
+ @app_test.property.state.must_equal :run
299
+ end
300
+ end
301
+
302
+ describe "when configuring its state property to :pause" do
303
+ it "must do nothing if its original state is :stop or :install" do
304
+ @app_test.property.state = :stop
305
+ @app_test.method(:configure_state).call(:pause)
306
+ @app_test.property.state.must_equal :stop
307
+ @app_test.property.state = :install
308
+ @app_test.method(:configure_state).call(:pause)
309
+ @app_test.property.state.must_equal :install
310
+ end
311
+
312
+ it "must do switch its state to :pause if its original state is :run or :pause" do
313
+ @app_test.property.state = :run
314
+ @app_test.method(:configure_state).call(:pause)
315
+ @app_test.property.state.must_equal :pause
316
+ @app_test.property.state = :pause
317
+ @app_test.method(:configure_state).call(:pause)
318
+ @app_test.property.state.must_equal :pause
319
+ end
320
+ end
321
+
322
+ describe "when configuring its state property to :stop" do
323
+ it "must do nothing if its original state is :stop or :install" do
324
+ @app_test.property.state = :stop
325
+ @app_test.method(:configure_state).call(:pause)
326
+ @app_test.property.state.must_equal :stop
327
+ @app_test.property.state = :install
328
+ @app_test.method(:configure_state).call(:pause)
329
+ @app_test.property.state.must_equal :install
330
+ end
331
+
332
+ it "must stop its running application if its original state is :run or :pause" do
333
+ @app_test.property.state = :run
334
+ class ExecApp
335
+ def initialize(app_id, res, cmd_line, err_out_map); end
336
+ def ExecApp.[](id); return ExecApp.new(nil,nil,nil,nil) end
337
+ def stdin(msg); msg.must_equal 'exit' end
338
+ def signal(sig); sig.must_equal 'TERM' end
339
+ def kill(sig); sig.must_equal 'KILL' end
340
+ end
341
+ @app_test.method(:configure_state).call(:stop)
342
+ @app_test.property.state.must_equal :stop
343
+ end
344
+ end
345
+
346
+
347
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+ require 'omf_rc/resource_proxy/mock'
3
+
4
+ describe OmfRc::ResourceProxy::Mock do
5
+ before do
6
+ @mock = OmfRc::ResourceFactory.new(:mock, hrn: 'mock_test')
7
+ end
8
+
9
+ describe "when included in the resource instance" do
10
+ it "must provide hooks" do
11
+ @mock.before_ready
12
+ @mock.before_release
13
+ end
14
+ end
15
+ end
@@ -2,9 +2,40 @@ require 'test_helper'
2
2
  require 'omf_rc/resource_proxy/node'
3
3
 
4
4
  describe OmfRc::ResourceProxy::Node do
5
+ before do
6
+ @node = OmfRc::ResourceFactory.new(:node, hrn: 'node_test')
7
+ end
8
+
5
9
  describe "when included in the resource instance" do
6
10
  it "must be able to tell registered proxies" do
7
- OmfRc::ResourceFactory.new(:node, hrn: 'node_test').request_proxies.must_include :node
11
+ @node.request_proxies.must_include :node
12
+ end
13
+
14
+ it "must provide a list of supported network devices" do
15
+ devices = [
16
+ { name: 'eth0', driver: 'e1000e', category: 'net', proxy: 'net' },
17
+ { name: 'phy0', driver: 'iwlwifi', category: 'net', subcategory: 'wlan', proxy: 'wlan' }
18
+ ]
19
+
20
+ Dir.stub :chdir, proc { |*args, &block| block.call } do
21
+ glob_proc = proc do |pattern|
22
+ sys_dir = "#{File.dirname(__FILE__)}/../../fixture/sys/class"
23
+ case pattern
24
+ when "net"
25
+ ["#{sys_dir}/net"]
26
+ when "ieee80211"
27
+ ["#{sys_dir}/ieee80211"]
28
+ when "ieee80211/*"
29
+ ["#{sys_dir}/ieee80211/phy0"]
30
+ when "net/eth*"
31
+ ["#{sys_dir}/net/eth0"]
32
+ end
33
+ end
34
+ Dir.stub :glob, glob_proc do
35
+ @node.request_devices.must_be_kind_of Array
36
+ @node.request_devices.must_equal devices
37
+ end
38
+ end
8
39
  end
9
40
  end
10
41
  end
@@ -5,20 +5,57 @@ describe OmfRc::ResourceProxyDSL do
5
5
  before do
6
6
  module OmfRc::Util::MockUtility
7
7
  include OmfRc::ResourceProxyDSL
8
+
8
9
  configure :alpha
9
- request :alpha
10
+
11
+ request :alpha do |resource|
12
+ resource.uid
13
+ end
14
+
15
+ work :bravo do |resource, *random_arguments, block|
16
+ if block
17
+ block.call("working on #{random_arguments.first}")
18
+ else
19
+ random_arguments.first
20
+ end
21
+ end
22
+
23
+ request :zulu do |resource, options|
24
+ "You called zulu with: #{options.keys.join('|')}"
25
+ end
26
+ end
27
+
28
+ module OmfRc::ResourceProxy::MockRootProxy
29
+ include OmfRc::ResourceProxyDSL
30
+
31
+ register_proxy :mock_root_proxy
10
32
  end
11
33
 
12
34
  module OmfRc::ResourceProxy::MockProxy
13
35
  include OmfRc::ResourceProxyDSL
14
36
 
15
- register_proxy :mock_proxy
37
+ register_proxy :mock_proxy, :create_by => :mock_root_proxy
38
+
16
39
  utility :mock_utility
17
40
 
18
41
  hook :before_ready
19
42
  hook :before_release
20
- configure :bravo
21
- request :bravo
43
+
44
+ bravo("printing") do |v|
45
+ request :charlie do
46
+ v
47
+ end
48
+ end
49
+
50
+ request :delta do
51
+ bravo("printing")
52
+ end
53
+ end
54
+
55
+ module OmfRc::ResourceProxy::UselessProxy
56
+ include OmfRc::ResourceProxyDSL
57
+
58
+ register_proxy :useless_proxy
22
59
  end
23
60
  end
24
61
 
@@ -28,12 +65,46 @@ describe OmfRc::ResourceProxyDSL do
28
65
  end
29
66
 
30
67
  it "must be able to define methods" do
31
- OmfRc::Util::MockUtility.method_defined?(:configure_alpha).must_equal true
32
- OmfRc::Util::MockUtility.method_defined?(:request_alpha).must_equal true
33
- OmfRc::ResourceProxy::MockProxy.method_defined?(:configure_alpha).must_equal true
34
- OmfRc::ResourceProxy::MockProxy.method_defined?(:request_alpha).must_equal true
35
- OmfRc::ResourceProxy::MockProxy.method_defined?(:before_ready).must_equal true
36
- OmfRc::ResourceProxy::MockProxy.method_defined?(:before_release).must_equal true
68
+ %w(configure_alpha request_alpha bravo).each do |m|
69
+ OmfRc::Util::MockUtility.method_defined?(m.to_sym).must_equal true
70
+ end
71
+
72
+ %w(configure_alpha request_alpha before_ready before_release bravo).each do |m|
73
+ OmfRc::ResourceProxy::MockProxy.method_defined?(m.to_sym).must_equal true
74
+ end
75
+
76
+ mock_proxy = OmfRc::ResourceFactory.new(:mock_proxy)
77
+ mock_proxy.request_alpha.must_equal mock_proxy.uid
78
+ mock_proxy.request_delta.must_equal "printing"
79
+ mock_proxy.request_charlie.must_equal "working on printing"
80
+ mock_proxy.bravo("magic", "second parameter") do |v|
81
+ v.must_equal "working on magic"
82
+ end
83
+ mock_proxy.bravo("something", "something else").must_equal "something"
84
+ mock_proxy.request_zulu(country: 'uk').must_equal "You called zulu with: country"
85
+ end
86
+
87
+ it "must be able to include utility" do
88
+ Class.new do
89
+ include OmfRc::ResourceProxyDSL
90
+ utility :mock_utility
91
+ end.new.must_respond_to :request_alpha
92
+ end
93
+
94
+ it "must log error if utility can't be found" do
95
+ Class.new do
96
+ include OmfRc::ResourceProxyDSL
97
+ utility :wont_be_found_utility
98
+ stub :require, true do
99
+ utility :wont_be_found_utility
100
+ end
101
+ end
102
+ end
103
+
104
+ it "must check new proxy's create_by option when ask a proxy create a new proxy" do
105
+ OmfRc::ResourceFactory.new(:mock_root_proxy).create(:mock_proxy)
106
+ OmfRc::ResourceFactory.new(:mock_root_proxy).create(:useless_proxy)
107
+ lambda { OmfRc::ResourceFactory.new(:useless_proxy).create(:mock_proxy) }.must_raise StandardError
37
108
  end
38
109
  end
39
110
  end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+ require 'omf_rc/util/common_tools'
3
+
4
+ describe OmfRc::Util::CommonTools do
5
+
6
+ describe "when included in the resource proxy" do
7
+ before do
8
+ module OmfRc::ResourceProxy::Test
9
+ include OmfRc::ResourceProxyDSL
10
+ register_proxy :test
11
+ utility :common_tools
12
+ end
13
+ # Mocking communicator
14
+ @client = Blather::Client.new
15
+ @stream = MiniTest::Mock.new
16
+ @stream.expect(:send, true, [Blather::Stanza])
17
+ @client.post_init @stream, Blather::JID.new('n@d/r')
18
+ end
19
+
20
+ it "must be able to log and inform error/warn messages" do
21
+ Blather::Client.stub :new, @client do
22
+ @test = OmfRc::ResourceFactory.new(:test)
23
+ @client.stub :write, true do
24
+ @test.log_inform_error "bob"
25
+ @test.log_inform_warn "bob"
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,51 @@
1
+ require 'minitest/mock'
2
+ require 'test_helper'
3
+
4
+ require 'omf_rc/util/ip'
5
+
6
+ describe OmfRc::Util::Ip do
7
+ describe "when included in the resource instance" do
8
+ before do
9
+ module OmfRc::ResourceProxy::IpTest
10
+ include OmfRc::ResourceProxyDSL
11
+ register_proxy :ip_test
12
+ utility :ip
13
+ end
14
+
15
+ @wlan00 = OmfRc::ResourceFactory.new(:ip_test, hrn: 'wlan00')
16
+ @command = MiniTest::Mock.new
17
+ end
18
+
19
+ it "must provide features defined in proxy" do
20
+ %w(request_ip_addr request_mac_addr configure_ip_addr).each do |m|
21
+ OmfRc::Util::Ip.method_defined?(m).must_equal true
22
+ end
23
+ end
24
+
25
+ it "could request ip address of the device" do
26
+ Cocaine::CommandLine.stub(:new, @command) do
27
+ @command.expect(:run, fixture("ip/addr_show"))
28
+ @wlan00.request_ip_addr.must_equal "192.168.1.124/24"
29
+ @command.verify
30
+ end
31
+ end
32
+
33
+ it "could request mac address of the device" do
34
+ Cocaine::CommandLine.stub(:new, @command) do
35
+ @command.expect(:run, fixture("ip/addr_show"))
36
+ @wlan00.request_mac_addr.must_equal "00:00:00:29:00:00"
37
+ @command.verify
38
+ end
39
+ end
40
+
41
+ it "could configure the device's prorperty" do
42
+ lambda { @wlan00.configure_ip_addr("192.168.1.124/24") }.must_raise Cocaine::ExitStatusError
43
+ Cocaine::CommandLine.stub(:new, @command) do
44
+ @command.expect(:run, "")
45
+ @command.expect(:run, fixture("ip/addr_show"))
46
+ @wlan00.configure_ip_addr("192.168.1.124/24")
47
+ @command.verify
48
+ end
49
+ end
50
+ end
51
+ end