mcollective-client 2.0.0 → 2.2.0

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 (140) hide show
  1. data/lib/mcollective.rb +32 -23
  2. data/lib/mcollective/agent.rb +5 -0
  3. data/lib/mcollective/agents.rb +5 -16
  4. data/lib/mcollective/aggregate.rb +61 -0
  5. data/lib/mcollective/aggregate/base.rb +40 -0
  6. data/lib/mcollective/aggregate/result.rb +9 -0
  7. data/lib/mcollective/aggregate/result/base.rb +25 -0
  8. data/lib/mcollective/aggregate/result/collection_result.rb +19 -0
  9. data/lib/mcollective/aggregate/result/numeric_result.rb +13 -0
  10. data/lib/mcollective/application.rb +7 -4
  11. data/lib/mcollective/applications.rb +3 -14
  12. data/lib/mcollective/cache.rb +145 -0
  13. data/lib/mcollective/client.rb +10 -87
  14. data/lib/mcollective/config.rb +22 -8
  15. data/lib/mcollective/data.rb +87 -0
  16. data/lib/mcollective/data/base.rb +67 -0
  17. data/lib/mcollective/data/result.rb +40 -0
  18. data/lib/mcollective/ddl.rb +113 -0
  19. data/lib/mcollective/ddl/agentddl.rb +185 -0
  20. data/lib/mcollective/ddl/base.rb +220 -0
  21. data/lib/mcollective/ddl/dataddl.rb +56 -0
  22. data/lib/mcollective/ddl/discoveryddl.rb +52 -0
  23. data/lib/mcollective/ddl/validatorddl.rb +6 -0
  24. data/lib/mcollective/discovery.rb +143 -0
  25. data/lib/mcollective/generators.rb +7 -0
  26. data/lib/mcollective/generators/agent_generator.rb +51 -0
  27. data/lib/mcollective/generators/base.rb +46 -0
  28. data/lib/mcollective/generators/data_generator.rb +51 -0
  29. data/lib/mcollective/generators/templates/action_snippet.erb +13 -0
  30. data/lib/mcollective/generators/templates/data_input_snippet.erb +7 -0
  31. data/lib/mcollective/generators/templates/ddl.erb +8 -0
  32. data/lib/mcollective/generators/templates/plugin.erb +7 -0
  33. data/lib/mcollective/logger/console_logger.rb +15 -15
  34. data/lib/mcollective/matcher.rb +167 -0
  35. data/lib/mcollective/matcher/parser.rb +60 -25
  36. data/lib/mcollective/matcher/scanner.rb +156 -78
  37. data/lib/mcollective/message.rb +47 -6
  38. data/lib/mcollective/monkey_patches.rb +17 -0
  39. data/lib/mcollective/optionparser.rb +18 -1
  40. data/lib/mcollective/pluginmanager.rb +3 -3
  41. data/lib/mcollective/pluginpackager.rb +10 -3
  42. data/lib/mcollective/pluginpackager/agent_definition.rb +28 -20
  43. data/lib/mcollective/pluginpackager/standard_definition.rb +11 -9
  44. data/lib/mcollective/registration/base.rb +3 -1
  45. data/lib/mcollective/rpc.rb +18 -24
  46. data/lib/mcollective/rpc/agent.rb +37 -113
  47. data/lib/mcollective/rpc/client.rb +186 -64
  48. data/lib/mcollective/rpc/helpers.rb +42 -80
  49. data/lib/mcollective/rpc/progress.rb +3 -3
  50. data/lib/mcollective/rpc/reply.rb +37 -13
  51. data/lib/mcollective/rpc/request.rb +17 -6
  52. data/lib/mcollective/rpc/result.rb +9 -5
  53. data/lib/mcollective/rpc/stats.rb +71 -24
  54. data/lib/mcollective/security/base.rb +41 -34
  55. data/lib/mcollective/shell.rb +1 -1
  56. data/lib/mcollective/ssl.rb +34 -0
  57. data/lib/mcollective/util.rb +194 -23
  58. data/lib/mcollective/validator.rb +80 -0
  59. data/spec/fixtures/util/1.in +10 -0
  60. data/spec/fixtures/util/1.out +10 -0
  61. data/spec/fixtures/util/2.in +1 -0
  62. data/spec/fixtures/util/2.out +1 -0
  63. data/spec/fixtures/util/3.in +1 -0
  64. data/spec/fixtures/util/3.out +2 -0
  65. data/spec/fixtures/util/4.in +5 -0
  66. data/spec/fixtures/util/4.out +9 -0
  67. data/spec/spec.opts +1 -1
  68. data/spec/spec_helper.rb +2 -0
  69. data/spec/unit/agents_spec.rb +34 -19
  70. data/spec/unit/aggregate/base_spec.rb +57 -0
  71. data/spec/unit/aggregate/result/base_spec.rb +28 -0
  72. data/spec/unit/aggregate/result/collection_result_spec.rb +18 -0
  73. data/spec/unit/aggregate/result/numeric_result_spec.rb +22 -0
  74. data/spec/unit/aggregate_spec.rb +110 -0
  75. data/spec/unit/application_spec.rb +8 -3
  76. data/spec/unit/applications_spec.rb +2 -2
  77. data/spec/unit/cache_spec.rb +115 -0
  78. data/spec/unit/client_spec.rb +78 -0
  79. data/spec/unit/config_spec.rb +32 -34
  80. data/spec/unit/data/base_spec.rb +90 -0
  81. data/spec/unit/data/result_spec.rb +64 -0
  82. data/spec/unit/data_spec.rb +158 -0
  83. data/spec/unit/ddl/agentddl_spec.rb +217 -0
  84. data/spec/unit/{rpc/ddl_spec.rb → ddl/base_spec.rb} +238 -224
  85. data/spec/unit/ddl/dataddl_spec.rb +65 -0
  86. data/spec/unit/ddl/discoveryddl_spec.rb +58 -0
  87. data/spec/unit/ddl_spec.rb +84 -0
  88. data/spec/unit/discovery_spec.rb +196 -0
  89. data/spec/unit/facts/base_spec.rb +1 -1
  90. data/spec/unit/generators/agent_generator_spec.rb +72 -0
  91. data/spec/unit/generators/base_spec.rb +83 -0
  92. data/spec/unit/generators/data_generator_spec.rb +37 -0
  93. data/spec/unit/generators/snippets/agent_ddl +19 -0
  94. data/spec/unit/generators/snippets/data_ddl +20 -0
  95. data/spec/unit/logger/console_logger_spec.rb +76 -0
  96. data/spec/unit/logger/syslog_logger_spec.rb +2 -2
  97. data/spec/unit/matcher/parser_spec.rb +27 -10
  98. data/spec/unit/matcher/scanner_spec.rb +108 -5
  99. data/spec/unit/matcher_spec.rb +260 -0
  100. data/spec/unit/message_spec.rb +35 -13
  101. data/spec/unit/optionparser_spec.rb +2 -2
  102. data/spec/unit/pluginpackager/agent_definition_spec.rb +59 -42
  103. data/spec/unit/pluginpackager/standard_definition_spec.rb +10 -8
  104. data/spec/unit/pluginpackager_spec.rb +131 -0
  105. data/spec/unit/plugins/mcollective/aggregate/average_spec.rb +45 -0
  106. data/spec/unit/plugins/mcollective/aggregate/sum_spec.rb +31 -0
  107. data/spec/unit/plugins/mcollective/aggregate/summary_spec.rb +45 -0
  108. data/spec/unit/plugins/mcollective/connector/activemq_spec.rb +1 -1
  109. data/spec/unit/plugins/mcollective/connector/rabbitmq_spec.rb +478 -0
  110. data/spec/unit/plugins/mcollective/connector/stomp_spec.rb +2 -0
  111. data/spec/unit/plugins/mcollective/data/agent_data_spec.rb +43 -0
  112. data/spec/unit/plugins/mcollective/data/fstat_data_spec.rb +135 -0
  113. data/spec/unit/plugins/mcollective/discovery/flatfile_spec.rb +48 -0
  114. data/spec/unit/plugins/mcollective/discovery/mc_spec.rb +40 -0
  115. data/spec/unit/plugins/mcollective/packagers/debpackage_packager_spec.rb +41 -15
  116. data/spec/unit/plugins/mcollective/packagers/ospackage_spec.rb +1 -1
  117. data/spec/unit/plugins/mcollective/packagers/rpmpackage_packager_spec.rb +22 -38
  118. data/spec/unit/plugins/mcollective/validator/array_validator_spec.rb +19 -0
  119. data/spec/unit/plugins/mcollective/validator/ipv4address_validator_spec.rb +19 -0
  120. data/spec/unit/plugins/mcollective/validator/ipv6address_validator_spec.rb +19 -0
  121. data/spec/unit/plugins/mcollective/validator/length_validator_spec.rb +19 -0
  122. data/spec/unit/plugins/mcollective/validator/regex_validator_spec.rb +19 -0
  123. data/spec/unit/plugins/mcollective/validator/shellsafe_validator_spec.rb +21 -0
  124. data/spec/unit/plugins/mcollective/validator/typecheck_validator_spec.rb +23 -0
  125. data/spec/unit/registration/base_spec.rb +1 -1
  126. data/spec/unit/rpc/actionrunner_spec.rb +2 -2
  127. data/spec/unit/rpc/agent_spec.rb +41 -65
  128. data/spec/unit/rpc/client_spec.rb +430 -134
  129. data/spec/unit/rpc/reply_spec.rb +31 -1
  130. data/spec/unit/rpc/request_spec.rb +33 -12
  131. data/spec/unit/rpc/result_spec.rb +7 -0
  132. data/spec/unit/rpc/stats_spec.rb +14 -14
  133. data/spec/unit/rpc_spec.rb +16 -0
  134. data/spec/unit/security/base_spec.rb +8 -8
  135. data/spec/unit/ssl_spec.rb +20 -2
  136. data/spec/unit/string_spec.rb +15 -0
  137. data/spec/unit/util_spec.rb +141 -21
  138. data/spec/unit/validator_spec.rb +67 -0
  139. metadata +145 -7
  140. data/lib/mcollective/rpc/ddl.rb +0 -258
@@ -6,7 +6,14 @@ module MCollective
6
6
  module RPC
7
7
  describe Reply do
8
8
  before(:each) do
9
- @reply = Reply.new
9
+ Cache.delete!(:ddl) rescue nil
10
+
11
+ ddl = stub
12
+ ddl.stubs(:action_interface).returns({:output => {}})
13
+ ddl.stubs(:actions).returns(["rspec"])
14
+ ddl.stubs(:pluginname).returns("rspec")
15
+
16
+ @reply = Reply.new("rspec", ddl)
10
17
  end
11
18
 
12
19
  describe "#initialize" do
@@ -23,6 +30,29 @@ module MCollective
23
30
  end
24
31
  end
25
32
 
33
+ describe "#initialize_data" do
34
+ before do
35
+ Log.stubs(:warn)
36
+ @ddl = DDL.new("rspec", :agent, false)
37
+ end
38
+
39
+ it "should set defaults correctly" do
40
+ @ddl.action :rspec, :description => "testing rspec" do
41
+ @ddl.output :one, :description => "rspec test", :display_as => "rspec", :default => "default"
42
+ @ddl.output :three, :description => "rspec test", :display_as => "rspec", :default => []
43
+ @ddl.output :two, :description => "rspec test", :display_as => "rspec"
44
+ end
45
+
46
+ reply = Reply.new(:rspec, @ddl)
47
+ reply.data.should == {:one => "default", :two => nil, :three => []}
48
+ end
49
+
50
+ it "should detect missing actions" do
51
+ reply = Reply.new(:rspec, @ddl)
52
+ expect { reply.initialize_data }.to raise_error(/No action 'rspec' defined/)
53
+ end
54
+ end
55
+
26
56
  describe "#fail" do
27
57
  it "should set statusmsg" do
28
58
  @reply.fail "foo"
@@ -7,15 +7,24 @@ module MCollective
7
7
  describe Request do
8
8
  before(:each) do
9
9
  @req = {:msgtime => Time.now,
10
- :senderid => "spec test",
11
- :requestid => "12345",
12
- :callerid => "rip"}
10
+ :senderid => "spec test",
11
+ :requestid => "12345",
12
+ :callerid => "rip"}
13
13
 
14
14
  @req[:body] = {:action => "test",
15
- :data => {:foo => "bar", :process_results => true},
16
- :agent => "tester"}
15
+ :data => {:foo => "bar", :process_results => true},
16
+ :agent => "tester"}
17
17
 
18
- @request = Request.new(@req)
18
+ @ddl = DDL.new("rspec", :agent, false)
19
+
20
+ @request = Request.new(@req, @ddl)
21
+ end
22
+
23
+ describe "#validate!" do
24
+ it "should validate the request using the supplied DDL" do
25
+ @ddl.expects(:validate_rpc_request).with("test", {:foo => "bar", :process_results => true})
26
+ @request.validate!
27
+ end
19
28
  end
20
29
 
21
30
  describe "#initialize" do
@@ -49,7 +58,7 @@ module MCollective
49
58
 
50
59
  it "should set unknown caller if none is supplied" do
51
60
  @req.delete(:callerid)
52
- Request.new(@req).caller.should == "unknown"
61
+ Request.new(@req, @ddl).caller.should == "unknown"
53
62
  end
54
63
  end
55
64
 
@@ -60,26 +69,26 @@ module MCollective
60
69
 
61
70
  it "should return false for non hash data" do
62
71
  @req[:body][:data] = "foo"
63
- Request.new(@req).include?(:foo).should == false
72
+ Request.new(@req, @ddl).include?(:foo).should == false
64
73
  end
65
74
  end
66
75
 
67
76
  describe "#should_respond?" do
68
77
  it "should return true if the header is absent" do
69
78
  @req[:body][:data].delete(:process_results)
70
- Request.new(@req).should_respond?.should == true
79
+ Request.new(@req, @ddl).should_respond?.should == true
71
80
  end
72
81
 
73
82
  it "should return correct value" do
74
83
  @req[:body][:data][:process_results] = false
75
- Request.new(@req).should_respond?.should == false
84
+ Request.new(@req, @ddl).should_respond?.should == false
76
85
  end
77
86
  end
78
87
 
79
88
  describe "#[]" do
80
89
  it "should return nil for non hash data" do
81
90
  @req[:body][:data] = "foo"
82
- Request.new(@req)["foo"].should == nil
91
+ Request.new(@req, @ddl)["foo"].should == nil
83
92
  end
84
93
 
85
94
  it "should return correct data" do
@@ -91,6 +100,18 @@ module MCollective
91
100
  end
92
101
  end
93
102
 
103
+ describe "#fetch" do
104
+ it "should return nil for non hash data" do
105
+ @req[:body][:data] = "foo"
106
+ Request.new(@req, @ddl)["foo"].should == nil
107
+ end
108
+
109
+ it "should fetch data with the correct default behavior" do
110
+ @request.fetch(:foo, "default").should == "bar"
111
+ @request.fetch(:rspec, "default").should == "default"
112
+ end
113
+ end
114
+
94
115
  describe "#to_hash" do
95
116
  it "should have the correct keys" do
96
117
  @request.to_hash.keys.sort.should == [:action, :agent, :data]
@@ -106,7 +127,7 @@ module MCollective
106
127
 
107
128
  it "should return the correct data" do
108
129
  @request.to_hash[:data].should == {:foo => "bar",
109
- :process_results => true}
130
+ :process_results => true}
110
131
  end
111
132
  end
112
133
  end
@@ -44,6 +44,13 @@ module MCollective
44
44
  end
45
45
  end
46
46
 
47
+ describe "#fetch" do
48
+ it "should fetch data with the correct default behavior" do
49
+ @result.fetch(:foo, "default").should == "bar"
50
+ @result.fetch(:rspec, "default").should == "default"
51
+ end
52
+ end
53
+
47
54
  describe "#each" do
48
55
  it "should itterate all the pairs" do
49
56
  data = {}
@@ -6,16 +6,18 @@ module MCollective
6
6
  module RPC
7
7
  describe Stats do
8
8
  before(:each) do
9
- @expected = {:discoverytime=>0,
10
- :okcount=>0,
11
- :blocktime=>0,
12
- :failcount=>0,
13
- :noresponsefrom=>[],
14
- :responses=>0,
15
- :totaltime=>0,
16
- :discovered=>0,
17
- :starttime=>1300031826.0,
18
- :discovered_nodes=>[]}
9
+ @expected = {:discoverytime => 0,
10
+ :okcount => 0,
11
+ :blocktime => 0,
12
+ :failcount => 0,
13
+ :noresponsefrom => [],
14
+ :responses => 0,
15
+ :totaltime => 0,
16
+ :discovered => 0,
17
+ :starttime => 1300031826.0,
18
+ :requestid => nil,
19
+ :aggregate_summary => [],
20
+ :discovered_nodes => []}
19
21
 
20
22
  @stats = Stats.new
21
23
  end
@@ -43,9 +45,7 @@ module MCollective
43
45
  Time.stubs(:now).returns(Time.at(1300031826))
44
46
  s = Stats.new
45
47
 
46
- @expected.keys.each do |k|
47
- @expected[k].should == s.to_hash[k]
48
- end
48
+ s.to_hash.should == @expected
49
49
  end
50
50
  end
51
51
 
@@ -280,7 +280,7 @@ module MCollective
280
280
  @stats.discovered_agents ["foo", "bar"]
281
281
  @stats.finish_request
282
282
 
283
- @stats.no_response_report.should match(Regexp.new(/No response from.+foo.+bar/m))
283
+ @stats.no_response_report.should match(Regexp.new(/No response from.+bar\s+foo/m))
284
284
  end
285
285
  end
286
286
  end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env rspec
2
+
3
+ require 'spec_helper'
4
+
5
+ module MCollective
6
+ describe RPC do
7
+ describe "#const_missing" do
8
+ it "should deprecate only the DDL class" do
9
+ Log.expects(:warn).with("MCollective::RPC::DDL is deprecatd, please use MCollective::DDL instead")
10
+ MCollective::RPC::DDL.should == MCollective::DDL
11
+
12
+ expect { MCollective::RPC::Foo }.to raise_error(NameError)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -248,29 +248,29 @@ module MCollective
248
248
  end
249
249
 
250
250
  describe "#validrequest?" do
251
- it "should log an error when not implimented" do
252
- MCollective::Log.expects(:error).with("validrequest? is not implimented in MCollective::Security::Base")
251
+ it "should log an error when not implemented" do
252
+ MCollective::Log.expects(:error).with("validrequest? is not implemented in MCollective::Security::Base")
253
253
  @plugin.validrequest?(nil)
254
254
  end
255
255
  end
256
256
 
257
257
  describe "#encoderequest" do
258
- it "should log an error when not implimented" do
259
- MCollective::Log.expects(:error).with("encoderequest is not implimented in MCollective::Security::Base")
258
+ it "should log an error when not implemented" do
259
+ MCollective::Log.expects(:error).with("encoderequest is not implemented in MCollective::Security::Base")
260
260
  @plugin.encoderequest(nil, nil, nil)
261
261
  end
262
262
  end
263
263
 
264
264
  describe "#encodereply" do
265
- it "should log an error when not implimented" do
266
- MCollective::Log.expects(:error).with("encodereply is not implimented in MCollective::Security::Base")
265
+ it "should log an error when not implemented" do
266
+ MCollective::Log.expects(:error).with("encodereply is not implemented in MCollective::Security::Base")
267
267
  @plugin.encodereply(nil, nil, nil)
268
268
  end
269
269
  end
270
270
 
271
271
  describe "#decodemsg" do
272
- it "should log an error when not implimented" do
273
- MCollective::Log.expects(:error).with("decodemsg is not implimented in MCollective::Security::Base")
272
+ it "should log an error when not implemented" do
273
+ MCollective::Log.expects(:error).with("decodemsg is not implemented in MCollective::Security::Base")
274
274
  @plugin.decodemsg(nil)
275
275
  end
276
276
  end
@@ -50,7 +50,7 @@ module MCollective
50
50
  end
51
51
 
52
52
  it "should take the configured value when present" do
53
- Config.any_instance.stubs("ssl_cipher").returns("aes-128-cbc")
53
+ Config.instance.stubs("ssl_cipher").returns("aes-128-cbc")
54
54
  @ssl = SSL.new("#{@rootdir}/../fixtures/test-public.pem", "#{@rootdir}/../fixtures/test-private.pem")
55
55
 
56
56
  @ssl.ssl_cipher.should == "aes-128-cbc"
@@ -62,7 +62,7 @@ module MCollective
62
62
  end
63
63
 
64
64
  it "should prefer the supplied cipher over configured cipher" do
65
- Config.any_instance.stubs("aes_key_size").returns("foo-foo-foo")
65
+ Config.instance.stubs("aes_key_size").returns("foo-foo-foo")
66
66
  @ssl = SSL.new("#{@rootdir}/../fixtures/test-public.pem", "#{@rootdir}/../fixtures/test-private.pem", nil, "aes-128-cbc")
67
67
 
68
68
  @ssl.ssl_cipher.should == "aes-128-cbc"
@@ -82,6 +82,14 @@ module MCollective
82
82
  }.to raise_error("Could not find key /nonexisting")
83
83
  end
84
84
 
85
+ it "should fail on existing, empty files" do
86
+ File.expects(:exist?).with('key').returns(true)
87
+ File.expects(:zero?).with('key').returns(true)
88
+ expect{
89
+ @ssl.read_key(:public, 'key')
90
+ }.to raise_error("public key file 'key' is empty")
91
+ end
92
+
85
93
  it "should fail on unknown key types" do
86
94
  expect {
87
95
  @ssl.read_key(:unknown, @ssl.public_key_file)
@@ -240,5 +248,15 @@ module MCollective
240
248
  }.to raise_error("Crypted data should include data")
241
249
  end
242
250
  end
251
+
252
+ describe "#uuid" do
253
+ it "should produce repeatable uuids" do
254
+ SSL.uuid("hello world").should == SSL.uuid("hello world")
255
+ end
256
+
257
+ it "should not always produce the same uuid" do
258
+ SSL.uuid.should_not == SSL.uuid
259
+ end
260
+ end
243
261
  end
244
262
  end
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env rspec
2
+
3
+ require 'spec_helper'
4
+
5
+ class String
6
+ describe "#start_with?" do
7
+ it "should return true for matches" do
8
+ "hello world".start_with?("hello").should == true
9
+ end
10
+
11
+ it "should return false for non matches" do
12
+ "hello world".start_with?("world").should == false
13
+ end
14
+ end
15
+ end
@@ -107,12 +107,12 @@ module MCollective
107
107
  Util.make_subscriptions("test", "test", "test")
108
108
  }.to raise_error("Unknown target type test")
109
109
 
110
- Config.any_instance.stubs(:collectives).returns(["test"])
110
+ Config.instance.stubs(:collectives).returns(["test"])
111
111
  Util.make_subscriptions("test", :broadcast, "test")
112
112
  end
113
113
 
114
114
  it "should return a subscription for each collective" do
115
- Config.any_instance.stubs(:collectives).returns(["collective1", "collective2"])
115
+ Config.instance.stubs(:collectives).returns(["collective1", "collective2"])
116
116
  Util.make_subscriptions("test", :broadcast).should == [{:type=>:broadcast,
117
117
  :agent=>"test",
118
118
  :collective=>"collective1"},
@@ -122,7 +122,7 @@ module MCollective
122
122
  end
123
123
 
124
124
  it "should validate given collective" do
125
- Config.any_instance.stubs(:collectives).returns(["collective1", "collective2"])
125
+ Config.instance.stubs(:collectives).returns(["collective1", "collective2"])
126
126
 
127
127
  expect {
128
128
  Util.make_subscriptions("test", :broadcast, "test")
@@ -130,7 +130,7 @@ module MCollective
130
130
  end
131
131
 
132
132
  it "should return a single subscription array given a collective" do
133
- Config.any_instance.stubs(:collectives).returns(["collective1", "collective2"])
133
+ Config.instance.stubs(:collectives).returns(["collective1", "collective2"])
134
134
  Util.make_subscriptions("test", :broadcast, "collective1").should == [{:type=>:broadcast, :agent=>"test", :collective=>"collective1"}]
135
135
  end
136
136
  end
@@ -194,10 +194,11 @@ module MCollective
194
194
 
195
195
  describe "#default_options" do
196
196
  it "should supply correct default options" do
197
+ Config.instance.stubs(:default_discovery_options).returns([])
197
198
  empty_filter = Util.empty_filter
198
199
  config_file = Util.config_file_for_user
199
200
 
200
- Util.default_options.should == {:verbose => false, :disctimeout => 2, :timeout => 5, :config => config_file, :filter => empty_filter, :collective => nil}
201
+ Util.default_options.should == {:verbose => false, :disctimeout => nil, :timeout => 5, :config => config_file, :filter => empty_filter, :collective => nil, :discovery_method => nil, :discovery_options => []}
201
202
  end
202
203
  end
203
204
 
@@ -316,26 +317,145 @@ module MCollective
316
317
  end
317
318
  end
318
319
 
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
320
+ describe "#colorize" do
321
+ it "should not add color codes when color is disabled" do
322
+ Config.instance.stubs(:color).returns(false)
323
+ Util.colorize(:red, "hello world").should == "hello world"
325
324
  end
326
325
 
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"
326
+ it "should add color when color is enabled" do
327
+ Config.instance.stubs(:color).returns(true)
328
+ Util.colorize(:red, "hello world").should == "hello world"
332
329
  end
330
+ end
331
+
332
+ describe "#align_text" do
333
+ it "should default to 80 if the terminal dimensions are unknown" do
334
+ Util.stubs(:terminal_dimensions).returns([0,0])
335
+
336
+ rootdir = File.dirname(__FILE__)
337
+ input = File.read("#{rootdir}/../fixtures/util/4.in")
338
+ output = File.read("#{rootdir}/../fixtures/util/4.out")
339
+
340
+ (Util.align_text(input, nil, 3) + "\n").should == output
341
+ end
342
+
343
+ it "should return the origional string if console lines are 0" do
344
+ result = Util.align_text("test", 0)
345
+ result.should == "test"
346
+ end
347
+
348
+ it "should return the origional string if preamble is greater than console lines" do
349
+ result = Util.align_text("test", 5, 6)
350
+ result.should == "test"
351
+ end
352
+
353
+ it "should return a string prefixed by the preamble" do
354
+ result = Util.align_text("test")
355
+ result.should == " test"
356
+ end
357
+
358
+ it "should correctly align strings" do
359
+ rootdir = File.dirname(__FILE__)
360
+ (1..2).each do |i|
361
+ input = File.read("#{rootdir}/../fixtures/util/#{i}.in")
362
+ output = File.read("#{rootdir}/../fixtures/util/#{i}.out")
363
+
364
+ (Util.align_text(input, 158 , 5) + "\n").should == output
365
+ end
366
+
367
+ input = File.read("#{rootdir}/../fixtures/util/3.in")
368
+ output = File.read("#{rootdir}/../fixtures/util/3.out")
369
+
370
+ (Util.align_text(input, 30, 0) + "\n").should == output
371
+ end
372
+ end
373
+
374
+ describe "#terminal_dimensions" do
375
+ it "should return 0 if there is no tty" do
376
+ stdout = mock()
377
+ stdout.expects(:tty?).returns(false)
378
+ result = Util.terminal_dimensions(stdout)
379
+ result.should == [0,0]
380
+ end
381
+
382
+ it "should return the default dimensions for a windows terminal" do
383
+ stdout = mock()
384
+ stdout.expects(:tty?).returns(true)
385
+ Util.expects(:windows?).returns(true)
386
+ result = Util.terminal_dimensions(stdout)
387
+ result.should == [80, 40]
388
+ end
389
+
390
+ it "should return 0 if an exception was raised" do
391
+ stdout = mock()
392
+ stdout.expects(:tty?).raises("error")
393
+ result = Util.terminal_dimensions(stdout)
394
+ result.should == [0, 0]
395
+ end
396
+
397
+ it "should return the correct dimensions if ENV columns and lines are set" do
398
+
399
+ stdout = mock()
400
+ stdout.expects(:tty?).returns(true)
401
+ environment = mock()
402
+ environment.expects(:[]).with("COLUMNS").returns(5).twice
403
+ environment.expects(:[]).with("LINES").returns(5).twice
404
+ result = Util.terminal_dimensions(stdout, environment)
405
+ result.should == [5,5]
406
+ end
407
+
408
+ it "should return the correct dimensions if ENV term is set and tput is present" do
409
+ stdout = mock()
410
+ stdout.expects(:tty?).returns(true)
411
+ environment = mock()
412
+ environment.expects(:[]).with("COLUMNS").returns(false)
413
+ environment.expects(:[]).with("TERM").returns(true)
414
+
415
+ Util.expects(:command_in_path?).with("tput").returns(true)
416
+ Util.stubs(:`).returns("5")
417
+
418
+ result = Util.terminal_dimensions(stdout, environment)
419
+ result.should == [5,5]
420
+ end
421
+
422
+ it "should return the correct dimensions if stty is present" do
423
+ stdout = mock()
424
+ stdout.expects(:tty?).returns(true)
425
+
426
+ environment = mock()
427
+ environment.expects(:[]).with("COLUMNS").returns(false)
428
+ environment.expects(:[]).with("TERM").returns(false)
429
+
430
+ Util.expects(:command_in_path?).with("stty").returns(true)
431
+ Util.stubs(:`).returns("5 5")
432
+
433
+ result = Util.terminal_dimensions(stdout, environment)
434
+ result.should == [5,5]
435
+ end
436
+ end
437
+
438
+ describe "#command_in_path?" do
439
+ it "should return true if the command is found" do
440
+ File.stubs(:exist?).returns(true)
441
+ result = Util.command_in_path?("test")
442
+ result.should == true
443
+ end
444
+
445
+ it "should return false if the command cannot be found" do
446
+ File.stubs(:exist?).returns(false)
447
+ result = Util.command_in_path?("test")
448
+ result.should == false
449
+ end
450
+ end
451
+
452
+ describe "#versioncmp" do
453
+ it "should be able to sort a long set of various unordered versions" do
454
+ ary = %w{ 1.1.6 2.3 1.1a 3.0 1.5 1 2.4 1.1-4 2.3.1 1.2 2.3.0 1.1-3 2.4b 2.4 2.40.2 2.3a.1 3.1 0002 1.1-5 1.1.a 1.06}
455
+
456
+ newary = ary.sort {|a, b| Util.versioncmp(a,b) }
333
457
 
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
458
+ newary.should == ["0002", "1", "1.06", "1.1-3", "1.1-4", "1.1-5", "1.1.6", "1.1.a", "1.1a", "1.2", "1.5", "2.3", "2.3.0", "2.3.1", "2.3a.1", "2.4", "2.4", "2.4b", "2.40.2", "3.0", "3.1"]
339
459
  end
340
460
  end
341
461
  end