mcollective-client 1.3.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of mcollective-client might be problematic. Click here for more details.

Files changed (103) hide show
  1. data/bin/mc-call-agent +54 -0
  2. data/bin/mco +27 -0
  3. data/lib/mcollective.rb +70 -0
  4. data/lib/mcollective/agents.rb +160 -0
  5. data/lib/mcollective/application.rb +354 -0
  6. data/lib/mcollective/applications.rb +145 -0
  7. data/lib/mcollective/client.rb +292 -0
  8. data/lib/mcollective/config.rb +202 -0
  9. data/lib/mcollective/connector.rb +18 -0
  10. data/lib/mcollective/connector/base.rb +24 -0
  11. data/lib/mcollective/facts.rb +39 -0
  12. data/lib/mcollective/facts/base.rb +86 -0
  13. data/lib/mcollective/log.rb +103 -0
  14. data/lib/mcollective/logger.rb +5 -0
  15. data/lib/mcollective/logger/base.rb +73 -0
  16. data/lib/mcollective/logger/console_logger.rb +61 -0
  17. data/lib/mcollective/logger/file_logger.rb +46 -0
  18. data/lib/mcollective/logger/syslog_logger.rb +53 -0
  19. data/lib/mcollective/matcher.rb +16 -0
  20. data/lib/mcollective/matcher/parser.rb +93 -0
  21. data/lib/mcollective/matcher/scanner.rb +123 -0
  22. data/lib/mcollective/message.rb +201 -0
  23. data/lib/mcollective/monkey_patches.rb +104 -0
  24. data/lib/mcollective/optionparser.rb +164 -0
  25. data/lib/mcollective/pluginmanager.rb +180 -0
  26. data/lib/mcollective/pluginpackager.rb +26 -0
  27. data/lib/mcollective/pluginpackager/agent_definition.rb +79 -0
  28. data/lib/mcollective/pluginpackager/standard_definition.rb +59 -0
  29. data/lib/mcollective/registration.rb +16 -0
  30. data/lib/mcollective/registration/base.rb +75 -0
  31. data/lib/mcollective/rpc.rb +188 -0
  32. data/lib/mcollective/rpc/actionrunner.rb +142 -0
  33. data/lib/mcollective/rpc/agent.rb +441 -0
  34. data/lib/mcollective/rpc/audit.rb +38 -0
  35. data/lib/mcollective/rpc/client.rb +793 -0
  36. data/lib/mcollective/rpc/ddl.rb +258 -0
  37. data/lib/mcollective/rpc/helpers.rb +339 -0
  38. data/lib/mcollective/rpc/progress.rb +63 -0
  39. data/lib/mcollective/rpc/reply.rb +61 -0
  40. data/lib/mcollective/rpc/request.rb +51 -0
  41. data/lib/mcollective/rpc/result.rb +41 -0
  42. data/lib/mcollective/rpc/stats.rb +185 -0
  43. data/lib/mcollective/runnerstats.rb +90 -0
  44. data/lib/mcollective/security.rb +26 -0
  45. data/lib/mcollective/security/base.rb +237 -0
  46. data/lib/mcollective/shell.rb +87 -0
  47. data/lib/mcollective/ssl.rb +246 -0
  48. data/lib/mcollective/unix_daemon.rb +37 -0
  49. data/lib/mcollective/util.rb +274 -0
  50. data/lib/mcollective/vendor.rb +41 -0
  51. data/lib/mcollective/vendor/require_vendored.rb +2 -0
  52. data/lib/mcollective/windows_daemon.rb +25 -0
  53. data/spec/Rakefile +16 -0
  54. data/spec/fixtures/application/test.rb +7 -0
  55. data/spec/fixtures/test-cert.pem +15 -0
  56. data/spec/fixtures/test-private.pem +15 -0
  57. data/spec/fixtures/test-public.pem +6 -0
  58. data/spec/monkey_patches/instance_variable_defined.rb +7 -0
  59. data/spec/spec.opts +1 -0
  60. data/spec/spec_helper.rb +25 -0
  61. data/spec/unit/agents_spec.rb +280 -0
  62. data/spec/unit/application_spec.rb +636 -0
  63. data/spec/unit/applications_spec.rb +155 -0
  64. data/spec/unit/array.rb +30 -0
  65. data/spec/unit/config_spec.rb +148 -0
  66. data/spec/unit/facts/base_spec.rb +118 -0
  67. data/spec/unit/facts_spec.rb +39 -0
  68. data/spec/unit/log_spec.rb +71 -0
  69. data/spec/unit/logger/base_spec.rb +110 -0
  70. data/spec/unit/logger/syslog_logger_spec.rb +86 -0
  71. data/spec/unit/matcher/parser_spec.rb +106 -0
  72. data/spec/unit/matcher/scanner_spec.rb +71 -0
  73. data/spec/unit/message_spec.rb +401 -0
  74. data/spec/unit/optionparser_spec.rb +113 -0
  75. data/spec/unit/pluginmanager_spec.rb +173 -0
  76. data/spec/unit/pluginpackager/agent_definition_spec.rb +130 -0
  77. data/spec/unit/pluginpackager/standard_definition_spec.rb +75 -0
  78. data/spec/unit/plugins/mcollective/connector/activemq_spec.rb +533 -0
  79. data/spec/unit/plugins/mcollective/connector/stomp/eventlogger_spec.rb +34 -0
  80. data/spec/unit/plugins/mcollective/connector/stomp_spec.rb +417 -0
  81. data/spec/unit/plugins/mcollective/packagers/ospackage_spec.rb +229 -0
  82. data/spec/unit/plugins/mcollective/security/psk_spec.rb +156 -0
  83. data/spec/unit/registration/base_spec.rb +77 -0
  84. data/spec/unit/rpc/actionrunner_spec.rb +213 -0
  85. data/spec/unit/rpc/agent_spec.rb +155 -0
  86. data/spec/unit/rpc/client_spec.rb +523 -0
  87. data/spec/unit/rpc/ddl_spec.rb +388 -0
  88. data/spec/unit/rpc/helpers_spec.rb +55 -0
  89. data/spec/unit/rpc/reply_spec.rb +143 -0
  90. data/spec/unit/rpc/request_spec.rb +115 -0
  91. data/spec/unit/rpc/result_spec.rb +66 -0
  92. data/spec/unit/rpc/stats_spec.rb +288 -0
  93. data/spec/unit/runnerstats_spec.rb +40 -0
  94. data/spec/unit/security/base_spec.rb +279 -0
  95. data/spec/unit/shell_spec.rb +144 -0
  96. data/spec/unit/ssl_spec.rb +244 -0
  97. data/spec/unit/symbol.rb +11 -0
  98. data/spec/unit/unix_daemon.rb +41 -0
  99. data/spec/unit/util_spec.rb +342 -0
  100. data/spec/unit/vendor_spec.rb +34 -0
  101. data/spec/unit/windows_daemon.rb +43 -0
  102. data/spec/windows_spec.opts +1 -0
  103. metadata +242 -0
@@ -0,0 +1,244 @@
1
+ #!/usr/bin/env rspec
2
+
3
+ require 'spec_helper'
4
+
5
+ module MCollective
6
+ describe SSL do
7
+ before do
8
+ @rootdir = File.dirname(__FILE__)
9
+ @ssl = SSL.new("#{@rootdir}/../fixtures/test-public.pem", "#{@rootdir}/../fixtures/test-private.pem")
10
+ end
11
+
12
+ it "should be able to decode base64 text it encoded" do
13
+ @ssl.base64_decode(@ssl.base64_encode("foo")).should == "foo"
14
+ end
15
+
16
+ it "should decrypt what it encrypted with RSA" do
17
+ crypted = @ssl.aes_encrypt("foo")
18
+ decrypted = @ssl.aes_decrypt(crypted[:key], crypted[:data])
19
+
20
+ decrypted.should == "foo"
21
+ end
22
+
23
+ it "should be able to decrypt using RSA private key what it encrypted with RSA public key" do
24
+ crypted = @ssl.rsa_encrypt_with_public("foo")
25
+ decrypted = @ssl.rsa_decrypt_with_private(crypted)
26
+
27
+ decrypted.should == "foo"
28
+ end
29
+
30
+ it "should be able to decrypt using RSA public key what it encrypted with RSA private key" do
31
+ crypted = @ssl.rsa_encrypt_with_private("foo")
32
+ decrypted = @ssl.rsa_decrypt_with_public(crypted)
33
+
34
+ decrypted.should == "foo"
35
+ end
36
+
37
+ it "using a helper it should be able to decrypt with private key what it encrypted using the public key" do
38
+ @ssl.decrypt_with_private(@ssl.encrypt_with_public("foo")).should == "foo"
39
+ @ssl.decrypt_with_private(@ssl.encrypt_with_public("foo", false), false).should == "foo"
40
+ end
41
+
42
+ it "using a helper it should be able to decrypt with public key what it encrypted using the private key" do
43
+ @ssl.decrypt_with_public(@ssl.encrypt_with_private("foo")).should == "foo"
44
+ @ssl.decrypt_with_public(@ssl.encrypt_with_private("foo", false), false).should == "foo"
45
+ end
46
+
47
+ describe "#initialize" do
48
+ it "should default to aes-256-cbc" do
49
+ @ssl.ssl_cipher.should == "aes-256-cbc"
50
+ end
51
+
52
+ it "should take the configured value when present" do
53
+ Config.any_instance.stubs("ssl_cipher").returns("aes-128-cbc")
54
+ @ssl = SSL.new("#{@rootdir}/../fixtures/test-public.pem", "#{@rootdir}/../fixtures/test-private.pem")
55
+
56
+ @ssl.ssl_cipher.should == "aes-128-cbc"
57
+ end
58
+
59
+ it "should set the supplied ssl cipher" do
60
+ @ssl = SSL.new("#{@rootdir}/../fixtures/test-public.pem", "#{@rootdir}/../fixtures/test-private.pem", nil, "aes-128-cbc")
61
+ @ssl.ssl_cipher.should == "aes-128-cbc"
62
+ end
63
+
64
+ it "should prefer the supplied cipher over configured cipher" do
65
+ Config.any_instance.stubs("aes_key_size").returns("foo-foo-foo")
66
+ @ssl = SSL.new("#{@rootdir}/../fixtures/test-public.pem", "#{@rootdir}/../fixtures/test-private.pem", nil, "aes-128-cbc")
67
+
68
+ @ssl.ssl_cipher.should == "aes-128-cbc"
69
+ end
70
+
71
+ it "should fail on invalid ciphers" do
72
+ expect {
73
+ @ssl = SSL.new("#{@rootdir}/../fixtures/test-public.pem", "#{@rootdir}/../fixtures/test-private.pem", nil, "foo-foo-foo")
74
+ }.to raise_error("The supplied cipher 'foo-foo-foo' is not supported")
75
+ end
76
+ end
77
+
78
+ describe "#read_key" do
79
+ it "should fail on non exiting files" do
80
+ expect {
81
+ @ssl.read_key(:public, "/nonexisting")
82
+ }.to raise_error("Could not find key /nonexisting")
83
+ end
84
+
85
+ it "should fail on unknown key types" do
86
+ expect {
87
+ @ssl.read_key(:unknown, @ssl.public_key_file)
88
+ }.to raise_error("Can only load :public or :private keys")
89
+ end
90
+
91
+ it "should read a public key" do
92
+ @ssl.read_key(:public, "#{@rootdir}/../fixtures/test-public.pem")
93
+ end
94
+
95
+ it "should read the public key from a certificate" do
96
+ @ssl.read_key(:public, "#{@rootdir}/../fixtures/test-cert.pem").to_s.should match /.+BEGIN.+PUBLIC KEY.+END.+PUBLIC KEY.+/m
97
+ end
98
+
99
+ it "should return nil if no key was given" do
100
+ @ssl.read_key(:public).should == nil
101
+ end
102
+
103
+ it "should return nil if nil key was given" do
104
+ @ssl.read_key(:public, nil).should == nil
105
+ end
106
+
107
+ it "should clear the OpenSSL error queue on ruby 1.8" do
108
+ Util.expects(:ruby_version).returns("1.8.7")
109
+ OpenSSL.expects(:errors)
110
+ @ssl.read_key(:public, "#{@rootdir}/../fixtures/test-public.pem")
111
+ @ssl.read_key(:private, "#{@rootdir}/../fixtures/test-private.pem")
112
+ end
113
+
114
+ it "should not clear the OpenSSL error queue on ruby > 1.8" do
115
+ Util.expects(:ruby_version).returns("1.9.3")
116
+ OpenSSL.expects(:errors).never
117
+ @ssl.read_key(:public, "#{@rootdir}/../fixtures/test-public.pem")
118
+ @ssl.read_key(:private, "#{@rootdir}/../fixtures/test-private.pem")
119
+ end
120
+ end
121
+
122
+ describe "#base64_encode" do
123
+ it "should correctly encode" do
124
+ @ssl.base64_encode("foo").should == "Zm9v\n"
125
+ SSL.base64_encode("foo").should == "Zm9v\n"
126
+ end
127
+ end
128
+
129
+ describe "#base64_decode" do
130
+ it "should correctly decode" do
131
+ @ssl.base64_decode("Zm9v").should == "foo"
132
+ SSL.base64_decode("Zm9v").should == "foo"
133
+ end
134
+ end
135
+
136
+ describe "#aes_encrypt" do
137
+ it "should create a key and data" do
138
+ crypted = @ssl.aes_encrypt("foo")
139
+
140
+ crypted.include?(:key).should == true
141
+ crypted.include?(:data).should == true
142
+ end
143
+ end
144
+
145
+ describe "#aes_decrypt" do
146
+ it "should decrypt correctly given key and data" do
147
+ key = @ssl.base64_decode("rAaCyW6qB0XqZNa9hji0qHwrI3P47t8diLNXoemW9ss=")
148
+ data = @ssl.base64_decode("mSthvO/wSl0ArNOcgysTVw==")
149
+
150
+ @ssl.aes_decrypt(key, data).should == "foo"
151
+ end
152
+
153
+ it "should decrypt correctly given key, data and cipher" do
154
+ key = @ssl.base64_decode("VEma3a/R7fjw2M4d0NIctA==")
155
+ data = @ssl.base64_decode("FkH6qLvKTn7a+uNPe8ciHA==")
156
+
157
+ # the default aes-256-cbc should fail here, the key above is 128 bit
158
+ # the exception classes changed mid-1.9.2 :(
159
+ if OpenSSL.constants.include?("CipherError")
160
+ expect { @ssl.aes_decrypt(key, data) }.to raise_error(OpenSSL::CipherError)
161
+ else
162
+ expect { @ssl.aes_decrypt(key, data) }.to raise_error(OpenSSL::Cipher::CipherError)
163
+ end
164
+
165
+ # new ssl instance configured for aes-128-cbc, should work
166
+ @ssl = SSL.new("#{@rootdir}/../fixtures/test-public.pem", "#{@rootdir}/../fixtures/test-private.pem", nil, "aes-128-cbc")
167
+ @ssl.aes_decrypt(key, data).should == "foo"
168
+ end
169
+ end
170
+
171
+ describe "#md5" do
172
+ it "should produce correct md5 sums" do
173
+ # echo -n 'hello world'|md5sum
174
+ @ssl.md5("hello world").should == "5eb63bbbe01eeed093cb22bb8f5acdc3"
175
+ end
176
+ end
177
+ describe "#sign" do
178
+ it "should sign the message without base64 by default" do
179
+ SSL.md5(@ssl.sign("hello world")).should == "8269b23f55945aaa82efbff857c845a6"
180
+ end
181
+
182
+ it "should support base64 encoding messages" do
183
+ SSL.md5(@ssl.sign("hello world", true)).should == "8a4eb3c3d44d22c46dc36a7e441d8db0"
184
+ end
185
+ end
186
+
187
+ describe "#verify_signature" do
188
+ it "should correctly verify a message signed using the same keypair" do
189
+ @ssl.verify_signature(@ssl.sign("hello world"), "hello world").should == true
190
+ @ssl.verify_signature(@ssl.sign("hello world", true), "hello world", true).should == true
191
+ end
192
+
193
+ it "should fail to verify messages not signed by the key" do
194
+ @ssl.verify_signature("evil fake signature", "hello world").should == false
195
+ end
196
+ end
197
+
198
+ describe "#decrypt_with_public" do
199
+ it "should decrypt correctly given key and data in base64 format" do
200
+ crypted = {:key=> "YaRcSDdcKgnRZ4Eu2eirl/+lzDgVkPZ41kXAQQNOi+6AfjdbbOW7Zblibx9r\n3TzZAi0ulA94gqNAXPvPC8LaO8W9TtJwlto/RHwDM7ZdfqEImSYoVACFNq28\n+0MLr3K3hIBsB1pyxgFTQul+MrCq+3Fik7Nj7ZKkJUT2veyqbg8=",
201
+ :data=>"TLVw1EYeOaGDmEC/R2I/cA=="}
202
+
203
+ @ssl.decrypt_with_public(crypted).should == "foo"
204
+ end
205
+ end
206
+
207
+ describe "#decrypt_with_private" do
208
+ it "should decrypt correctly given key and data in base64 format" do
209
+ crypted = {:key=> "kO1kUgJBiEBdoajN4OHp9BOie6dCznf1YKbBnp3LOyBxcDDQtjxEBlPmjQve\npXrQJ5xpLX6oNBxzU18Pf2SKYUZSbzIkDUb97GQY0WoBQsdM2OwPXH+HtF2A\no5N8iIx9srPAEAFa6hZAdqvcmRT/SzhP1kH+Gyy8fyvW8HGBjNY=",
210
+ :data=>"gDTaHCmes/Yua4jtjmgukQ=="}
211
+
212
+ @ssl.decrypt_with_private(crypted).should == "foo"
213
+ end
214
+ end
215
+
216
+ describe "#decrypt_with_private" do
217
+ it "should fail if not given a key" do
218
+ expect {
219
+ @ssl.decrypt_with_private({:iv => "x", :data => "x"})
220
+ }.to raise_error("Crypted data should include a key")
221
+ end
222
+
223
+ it "should fail if not given data" do
224
+ expect {
225
+ @ssl.decrypt_with_private({:iv => "x", :key => "x"})
226
+ }.to raise_error("Crypted data should include data")
227
+ end
228
+ end
229
+
230
+ describe "#decrypt_with_public" do
231
+ it "should fail if not given a key" do
232
+ expect {
233
+ @ssl.decrypt_with_public({:iv => "x", :data => "x"})
234
+ }.to raise_error("Crypted data should include a key")
235
+ end
236
+
237
+ it "should fail if not given data" do
238
+ expect {
239
+ @ssl.decrypt_with_public({:iv => "x", :key => "x"})
240
+ }.to raise_error("Crypted data should include data")
241
+ end
242
+ end
243
+ end
244
+ end
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rspec
2
+
3
+ require 'spec_helper'
4
+
5
+ class Symbol
6
+ describe "#<=>" do
7
+ it "should be sortable" do
8
+ [:foo, :bar].sort.should == [:bar, :foo]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env rspec
2
+
3
+ require 'spec_helper'
4
+ require 'mcollective/unix_daemon'
5
+
6
+ module MCollective
7
+ describe UnixDaemon do
8
+ describe "#daemonize_runner" do
9
+ it "should not run on the windows platform" do
10
+ Util.expects("windows?").returns(true)
11
+ expect { UnixDaemon.daemonize_runner }.to raise_error("The Unix Daemonizer can not be used on the Windows Platform")
12
+ end
13
+
14
+ it "should write the pid file if requested" do
15
+ f = mock
16
+ f.expects(:write).with(Process.pid)
17
+
18
+ File.expects(:open).with("/nonexisting", "w").yields(f)
19
+
20
+ r = mock
21
+ r.expects(:run)
22
+
23
+ Runner.expects(:new).returns(r)
24
+ UnixDaemon.expects(:daemonize).yields
25
+
26
+ UnixDaemon.daemonize_runner("/nonexisting")
27
+ end
28
+
29
+ it "should not write a pid file unless requested" do
30
+ r = mock
31
+ r.expects(:run)
32
+
33
+ UnixDaemon.expects(:daemonize).yields
34
+ Runner.expects(:new).returns(r)
35
+ File.expects(:open).never
36
+
37
+ UnixDaemon.daemonize_runner(nil)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,342 @@
1
+ #!/usr/bin/env rspec
2
+
3
+ require 'spec_helper'
4
+
5
+ module MCollective
6
+ describe Util do
7
+ before do
8
+ class MCollective::Connector::Stomp<MCollective::Connector::Base; end
9
+
10
+ PluginManager.clear
11
+ PluginManager << {:type => "connector_plugin", :class => MCollective::Connector::Stomp.new}
12
+ end
13
+
14
+ describe "#windows?" do
15
+ it "should correctly detect windows on unix platforms" do
16
+ RbConfig::CONFIG.expects("[]").returns("linux")
17
+ Util.windows?.should == false
18
+ end
19
+
20
+ it "should correctly detect windows on windows platforms" do
21
+ RbConfig::CONFIG.expects("[]").returns("win32")
22
+ Util.windows?.should == true
23
+ end
24
+ end
25
+
26
+ describe "#setup_windows_sleeper" do
27
+ it "should set up a thread on the windows platform" do
28
+ Thread.expects(:new)
29
+ Util.expects("windows?").returns(true).once
30
+ Util.setup_windows_sleeper
31
+ end
32
+
33
+ it "should not set up a thread on other platforms" do
34
+ Thread.expects(:new).never
35
+ Util.expects("windows?").returns(false).once
36
+ Util.setup_windows_sleeper
37
+ end
38
+ end
39
+
40
+ describe "#has_cf_class?" do
41
+ before do
42
+ logger = mock
43
+ logger.stubs(:log)
44
+ logger.stubs(:start)
45
+ Log.configure(logger)
46
+
47
+ config = mock
48
+ config.stubs(:classesfile).returns("/some/file")
49
+ Config.expects(:instance).returns(config)
50
+ end
51
+
52
+ it "should read the classes lines from the correct file" do
53
+ File.expects(:readlines).with("/some/file")
54
+
55
+ Util.has_cf_class?("test")
56
+ end
57
+
58
+ it "should support regular expression searches" do
59
+ File.stubs(:readlines).returns(["test_class_test"])
60
+ String.any_instance.expects(:match).with("^/").returns(true)
61
+ String.any_instance.expects(:match).with(Regexp.new("class")).returns(true)
62
+
63
+ Util.has_cf_class?("/class/").should == true
64
+ end
65
+
66
+ it "should support exact string matches" do
67
+ File.stubs(:readlines).returns(["test_class_test"])
68
+ String.any_instance.expects(:match).with("^/").returns(false)
69
+ String.any_instance.expects(:match).with(Regexp.new("test_class_test")).never
70
+
71
+ Util.has_cf_class?("test_class_test").should == true
72
+ end
73
+
74
+ it "should report a warning when the classes file cannot be parsed" do
75
+ File.stubs(:readlines).returns(nil)
76
+ Log.expects(:warn).with("Parsing classes file '/some/file' failed: NoMethodError: undefined method `each' for nil:NilClass")
77
+
78
+ Util.has_cf_class?("test_class_test").should == false
79
+ end
80
+ end
81
+
82
+ describe "#shellescape" do
83
+ it "should return '' for empty strings" do
84
+ Util.shellescape("").should == "''"
85
+ end
86
+
87
+ it "should quote newlines" do
88
+ Util.shellescape("\n").should == "'\n'"
89
+ end
90
+
91
+ it "should escape unwanted characters" do
92
+ Util.shellescape("foo;bar").should == 'foo\;bar'
93
+ Util.shellescape('foo`bar').should == 'foo\`bar'
94
+ Util.shellescape('foo$bar').should == 'foo\$bar'
95
+ Util.shellescape('foo|bar').should == 'foo\|bar'
96
+ Util.shellescape('foo&&bar').should == 'foo\&\&bar'
97
+ Util.shellescape('foo||bar').should == 'foo\|\|bar'
98
+ Util.shellescape('foo>bar').should == 'foo\>bar'
99
+ Util.shellescape('foo<bar').should == 'foo\<bar'
100
+ Util.shellescape('foobar').should == 'foobar'
101
+ end
102
+ end
103
+
104
+ describe "#make_subscription" do
105
+ it "should validate target types" do
106
+ expect {
107
+ Util.make_subscriptions("test", "test", "test")
108
+ }.to raise_error("Unknown target type test")
109
+
110
+ Config.any_instance.stubs(:collectives).returns(["test"])
111
+ Util.make_subscriptions("test", :broadcast, "test")
112
+ end
113
+
114
+ it "should return a subscription for each collective" do
115
+ Config.any_instance.stubs(:collectives).returns(["collective1", "collective2"])
116
+ Util.make_subscriptions("test", :broadcast).should == [{:type=>:broadcast,
117
+ :agent=>"test",
118
+ :collective=>"collective1"},
119
+ {:type=>:broadcast,
120
+ :agent=>"test",
121
+ :collective=>"collective2"}]
122
+ end
123
+
124
+ it "should validate given collective" do
125
+ Config.any_instance.stubs(:collectives).returns(["collective1", "collective2"])
126
+
127
+ expect {
128
+ Util.make_subscriptions("test", :broadcast, "test")
129
+ }.to raise_error("Unknown collective 'test' known collectives are 'collective1, collective2'")
130
+ end
131
+
132
+ it "should return a single subscription array given a collective" do
133
+ Config.any_instance.stubs(:collectives).returns(["collective1", "collective2"])
134
+ Util.make_subscriptions("test", :broadcast, "collective1").should == [{:type=>:broadcast, :agent=>"test", :collective=>"collective1"}]
135
+ end
136
+ end
137
+
138
+ describe "#subscribe" do
139
+ it "should subscribe to multiple topics given an Array" do
140
+ subs1 = {:agent => "test_agent", :type => "test_type", :collective => "test_collective"}
141
+ subs2 = {:agent => "test_agent2", :type => "test_type2", :collective => "test_collective2"}
142
+
143
+ MCollective::Connector::Stomp.any_instance.expects(:subscribe).with("test_agent", "test_type", "test_collective").once
144
+ MCollective::Connector::Stomp.any_instance.expects(:subscribe).with("test_agent2", "test_type2", "test_collective2").once
145
+
146
+ Util.subscribe([subs1, subs2])
147
+ end
148
+
149
+ it "should subscribe to a single topic given a hash" do
150
+ MCollective::Connector::Stomp.any_instance.expects(:subscribe).with("test_agent", "test_type", "test_collective").once
151
+ Util.subscribe({:agent => "test_agent", :type => "test_type", :collective => "test_collective"})
152
+ end
153
+ end
154
+
155
+ describe "#unsubscribe" do
156
+ it "should unsubscribe to multiple topics given an Array" do
157
+ subs1 = {:agent => "test_agent", :type => "test_type", :collective => "test_collective"}
158
+ subs2 = {:agent => "test_agent2", :type => "test_type2", :collective => "test_collective2"}
159
+ MCollective::Connector::Stomp.any_instance.expects(:unsubscribe).with("test_agent", "test_type", "test_collective").once
160
+ MCollective::Connector::Stomp.any_instance.expects(:unsubscribe).with("test_agent2", "test_type2", "test_collective2").once
161
+
162
+ Util.unsubscribe([subs1, subs2])
163
+ end
164
+
165
+ it "should subscribe to a single topic given a hash" do
166
+ MCollective::Connector::Stomp.any_instance.expects(:unsubscribe).with("test_agent", "test_type", "test_collective").once
167
+ Util.unsubscribe({:agent => "test_agent", :type => "test_type", :collective => "test_collective"})
168
+ end
169
+ end
170
+
171
+ describe "#empty_filter?" do
172
+ it "should correctly compare empty filters" do
173
+ Util.empty_filter?(Util.empty_filter).should == true
174
+ end
175
+
176
+ it "should treat an empty hash as an empty filter" do
177
+ Util.empty_filter?({}).should == true
178
+ end
179
+
180
+ it "should detect non empty filters correctly" do
181
+ filter = Util.empty_filter
182
+ filter["cf_class"] << "meh"
183
+
184
+
185
+ Util.empty_filter?(filter).should == false
186
+ end
187
+ end
188
+
189
+ describe "#empty_filter" do
190
+ it "should create correct empty filters" do
191
+ Util.empty_filter.should == {"fact" => [], "cf_class" => [], "agent" => [], "identity" => [], "compound" => []}
192
+ end
193
+ end
194
+
195
+ describe "#default_options" do
196
+ it "should supply correct default options" do
197
+ empty_filter = Util.empty_filter
198
+ config_file = Util.config_file_for_user
199
+
200
+ Util.default_options.should == {:verbose => false, :disctimeout => 2, :timeout => 5, :config => config_file, :filter => empty_filter, :collective => nil}
201
+ end
202
+ end
203
+
204
+ describe "#has_fact?" do
205
+ it "should handle missing facts correctly" do
206
+ MCollective::Facts.expects("[]").with("foo").returns(nil).once
207
+ Util.has_fact?("foo", "1", "==").should == false
208
+ end
209
+
210
+ it "should handle regex in a backward compatible way" do
211
+ MCollective::Facts.expects("[]").with("foo").returns("foo").times(6)
212
+ Util.has_fact?("foo", "foo", "=~").should == true
213
+ Util.has_fact?("foo", "/foo/", "=~").should == true
214
+ Util.has_fact?("foo", "foo", "=~").should == true
215
+ Util.has_fact?("foo", "bar", "=~").should == false
216
+ Util.has_fact?("foo", "/bar/", "=~").should == false
217
+ Util.has_fact?("foo", "bar", "=~").should == false
218
+ end
219
+
220
+ it "should evaluate equality" do
221
+ MCollective::Facts.expects("[]").with("foo").returns("foo").twice
222
+ Util.has_fact?("foo", "foo", "==").should == true
223
+ Util.has_fact?("foo", "bar", "==").should == false
224
+ end
225
+
226
+ it "should handle numeric comparisons correctly" do
227
+ MCollective::Facts.expects("[]").with("foo").returns("1").times(8)
228
+ Util.has_fact?("foo", "2", ">=").should == false
229
+ Util.has_fact?("foo", "1", ">=").should == true
230
+ Util.has_fact?("foo", "2", "<=").should == true
231
+ Util.has_fact?("foo", "1", "<=").should == true
232
+ Util.has_fact?("foo", "1", "<").should == false
233
+ Util.has_fact?("foo", "1", ">").should == false
234
+ Util.has_fact?("foo", "1", "!=").should == false
235
+ Util.has_fact?("foo", "2", "!=").should == true
236
+ end
237
+
238
+ it "should handle alphabetic comparisons correctly" do
239
+ MCollective::Facts.expects("[]").with("foo").returns("b").times(8)
240
+ Util.has_fact?("foo", "c", ">=").should == false
241
+ Util.has_fact?("foo", "a", ">=").should == true
242
+ Util.has_fact?("foo", "a", "<=").should == false
243
+ Util.has_fact?("foo", "b", "<=").should == true
244
+ Util.has_fact?("foo", "b", "<").should == false
245
+ Util.has_fact?("foo", "b", ">").should == false
246
+ Util.has_fact?("foo", "b", "!=").should == false
247
+ Util.has_fact?("foo", "a", "!=").should == true
248
+ end
249
+ end
250
+
251
+ describe "#parse_fact_string" do
252
+ it "should parse old style regex fact matches" do
253
+ Util.parse_fact_string("foo=/bar/").should == {:fact => "foo", :value => "/bar/", :operator => "=~"}
254
+ Util.parse_fact_string("foo = /bar/").should == {:fact => "foo", :value => "/bar/", :operator => "=~"}
255
+ end
256
+
257
+ it "should parse old style equality" do
258
+ Util.parse_fact_string("foo=bar").should == {:fact => "foo", :value => "bar", :operator => "=="}
259
+ Util.parse_fact_string("foo = bar").should == {:fact => "foo", :value => "bar", :operator => "=="}
260
+ end
261
+
262
+ it "should parse regex fact matches" do
263
+ Util.parse_fact_string("foo=~bar").should == {:fact => "foo", :value => "bar", :operator => "=~"}
264
+ Util.parse_fact_string("foo =~ bar").should == {:fact => "foo", :value => "bar", :operator => "=~"}
265
+ end
266
+
267
+ it "should treat => like >=" do
268
+ Util.parse_fact_string("foo=>bar").should == {:fact => "foo", :value => "bar", :operator => ">="}
269
+ Util.parse_fact_string("foo => bar").should == {:fact => "foo", :value => "bar", :operator => ">="}
270
+ end
271
+
272
+ it "should treat =< like <=" do
273
+ Util.parse_fact_string("foo=<bar").should == {:fact => "foo", :value => "bar", :operator => "<="}
274
+ Util.parse_fact_string("foo =< bar").should == {:fact => "foo", :value => "bar", :operator => "<="}
275
+ end
276
+
277
+ it "should parse less than or equal" do
278
+ Util.parse_fact_string("foo<=bar").should == {:fact => "foo", :value => "bar", :operator => "<="}
279
+ Util.parse_fact_string("foo <= bar").should == {:fact => "foo", :value => "bar", :operator => "<="}
280
+ end
281
+
282
+ it "should parse greater than or equal" do
283
+ Util.parse_fact_string("foo>=bar").should == {:fact => "foo", :value => "bar", :operator => ">="}
284
+ Util.parse_fact_string("foo >= bar").should == {:fact => "foo", :value => "bar", :operator => ">="}
285
+ end
286
+
287
+ it "should parse less than" do
288
+ Util.parse_fact_string("foo<bar").should == {:fact => "foo", :value => "bar", :operator => "<"}
289
+ Util.parse_fact_string("foo < bar").should == {:fact => "foo", :value => "bar", :operator => "<"}
290
+ end
291
+
292
+ it "should parse greater than" do
293
+ Util.parse_fact_string("foo>bar").should == {:fact => "foo", :value => "bar", :operator => ">"}
294
+ Util.parse_fact_string("foo > bar").should == {:fact => "foo", :value => "bar", :operator => ">"}
295
+ end
296
+
297
+ it "should parse greater than" do
298
+ Util.parse_fact_string("foo>bar").should == {:fact => "foo", :value => "bar", :operator => ">"}
299
+ Util.parse_fact_string("foo > bar").should == {:fact => "foo", :value => "bar", :operator => ">"}
300
+ end
301
+
302
+ it "should parse not equal" do
303
+ Util.parse_fact_string("foo!=bar").should == {:fact => "foo", :value => "bar", :operator => "!="}
304
+ Util.parse_fact_string("foo != bar").should == {:fact => "foo", :value => "bar", :operator => "!="}
305
+ end
306
+
307
+ it "should parse equal to" do
308
+ Util.parse_fact_string("foo==bar").should == {:fact => "foo", :value => "bar", :operator => "=="}
309
+ Util.parse_fact_string("foo == bar").should == {:fact => "foo", :value => "bar", :operator => "=="}
310
+ end
311
+
312
+ it "should fail for facts in the wrong format" do
313
+ expect {
314
+ Util.parse_fact_string("foo")
315
+ }.to raise_error("Could not parse fact foo it does not appear to be in a valid format")
316
+ end
317
+ end
318
+
319
+ describe "#eval_compound_statement" do
320
+ it "should return correctly on a regex class statement" do
321
+ Util.expects(:has_cf_class?).with("/foo/").returns(true)
322
+ Util.eval_compound_statement({"statement" => "/foo/"}).should == true
323
+ Util.expects(:has_cf_class?).with("/foo/").returns(false)
324
+ Util.eval_compound_statement({"statement" => "/foo/"}).should == false
325
+ end
326
+
327
+ it "should return correcly for string and regex facts" do
328
+ Util.expects(:has_fact?).with("foo", "bar", "==").returns(true)
329
+ Util.eval_compound_statement({"statement" => "foo=bar"}).should == "true"
330
+ Util.expects(:has_fact?).with("foo", "/bar/", "=~").returns(false)
331
+ Util.eval_compound_statement({"statement" => "foo=/bar/"}).should == "false"
332
+ end
333
+
334
+ it "should return correctly on a string class statement" do
335
+ Util.expects(:has_cf_class?).with("foo").returns(true)
336
+ Util.eval_compound_statement({"statement" => "foo"}).should == true
337
+ Util.expects(:has_cf_class?).with("foo").returns(false)
338
+ Util.eval_compound_statement({"statement" => "foo"}).should == false
339
+ end
340
+ end
341
+ end
342
+ end