right_agent 2.0.7-x86-mingw32

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 (176) hide show
  1. data/LICENSE +20 -0
  2. data/README.rdoc +82 -0
  3. data/Rakefile +113 -0
  4. data/lib/right_agent.rb +59 -0
  5. data/lib/right_agent/actor.rb +182 -0
  6. data/lib/right_agent/actor_registry.rb +76 -0
  7. data/lib/right_agent/actors/agent_manager.rb +232 -0
  8. data/lib/right_agent/agent.rb +1149 -0
  9. data/lib/right_agent/agent_config.rb +480 -0
  10. data/lib/right_agent/agent_identity.rb +210 -0
  11. data/lib/right_agent/agent_tag_manager.rb +237 -0
  12. data/lib/right_agent/audit_formatter.rb +107 -0
  13. data/lib/right_agent/clients.rb +31 -0
  14. data/lib/right_agent/clients/api_client.rb +383 -0
  15. data/lib/right_agent/clients/auth_client.rb +247 -0
  16. data/lib/right_agent/clients/balanced_http_client.rb +369 -0
  17. data/lib/right_agent/clients/base_retry_client.rb +495 -0
  18. data/lib/right_agent/clients/right_http_client.rb +279 -0
  19. data/lib/right_agent/clients/router_client.rb +493 -0
  20. data/lib/right_agent/command.rb +30 -0
  21. data/lib/right_agent/command/agent_manager_commands.rb +150 -0
  22. data/lib/right_agent/command/command_client.rb +136 -0
  23. data/lib/right_agent/command/command_constants.rb +33 -0
  24. data/lib/right_agent/command/command_io.rb +126 -0
  25. data/lib/right_agent/command/command_parser.rb +87 -0
  26. data/lib/right_agent/command/command_runner.rb +118 -0
  27. data/lib/right_agent/command/command_serializer.rb +63 -0
  28. data/lib/right_agent/connectivity_checker.rb +179 -0
  29. data/lib/right_agent/console.rb +65 -0
  30. data/lib/right_agent/core_payload_types.rb +44 -0
  31. data/lib/right_agent/core_payload_types/cookbook.rb +61 -0
  32. data/lib/right_agent/core_payload_types/cookbook_position.rb +46 -0
  33. data/lib/right_agent/core_payload_types/cookbook_repository.rb +116 -0
  34. data/lib/right_agent/core_payload_types/cookbook_sequence.rb +70 -0
  35. data/lib/right_agent/core_payload_types/dev_repositories.rb +100 -0
  36. data/lib/right_agent/core_payload_types/dev_repository.rb +76 -0
  37. data/lib/right_agent/core_payload_types/event_categories.rb +38 -0
  38. data/lib/right_agent/core_payload_types/executable_bundle.rb +130 -0
  39. data/lib/right_agent/core_payload_types/login_policy.rb +72 -0
  40. data/lib/right_agent/core_payload_types/login_user.rb +79 -0
  41. data/lib/right_agent/core_payload_types/planned_volume.rb +94 -0
  42. data/lib/right_agent/core_payload_types/recipe_instantiation.rb +73 -0
  43. data/lib/right_agent/core_payload_types/repositories_bundle.rb +50 -0
  44. data/lib/right_agent/core_payload_types/right_script_attachment.rb +95 -0
  45. data/lib/right_agent/core_payload_types/right_script_instantiation.rb +94 -0
  46. data/lib/right_agent/core_payload_types/runlist_policy.rb +44 -0
  47. data/lib/right_agent/core_payload_types/secure_document.rb +66 -0
  48. data/lib/right_agent/core_payload_types/secure_document_location.rb +63 -0
  49. data/lib/right_agent/core_payload_types/software_repository_instantiation.rb +61 -0
  50. data/lib/right_agent/daemonize.rb +35 -0
  51. data/lib/right_agent/dispatched_cache.rb +109 -0
  52. data/lib/right_agent/dispatcher.rb +272 -0
  53. data/lib/right_agent/enrollment_result.rb +221 -0
  54. data/lib/right_agent/exceptions.rb +87 -0
  55. data/lib/right_agent/history.rb +145 -0
  56. data/lib/right_agent/log.rb +460 -0
  57. data/lib/right_agent/minimal.rb +46 -0
  58. data/lib/right_agent/monkey_patches.rb +30 -0
  59. data/lib/right_agent/monkey_patches/ruby_patch.rb +55 -0
  60. data/lib/right_agent/monkey_patches/ruby_patch/array_patch.rb +29 -0
  61. data/lib/right_agent/monkey_patches/ruby_patch/darwin_patch.rb +24 -0
  62. data/lib/right_agent/monkey_patches/ruby_patch/linux_patch.rb +24 -0
  63. data/lib/right_agent/monkey_patches/ruby_patch/linux_patch/file_patch.rb +30 -0
  64. data/lib/right_agent/monkey_patches/ruby_patch/object_patch.rb +49 -0
  65. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch.rb +32 -0
  66. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/file_patch.rb +60 -0
  67. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/process_patch.rb +63 -0
  68. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/stdio_patch.rb +27 -0
  69. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/time_patch.rb +55 -0
  70. data/lib/right_agent/monkey_patches/ruby_patch/windows_patch/win32ole_patch.rb +34 -0
  71. data/lib/right_agent/multiplexer.rb +102 -0
  72. data/lib/right_agent/offline_handler.rb +270 -0
  73. data/lib/right_agent/operation_result.rb +300 -0
  74. data/lib/right_agent/packets.rb +673 -0
  75. data/lib/right_agent/payload_formatter.rb +104 -0
  76. data/lib/right_agent/pending_requests.rb +128 -0
  77. data/lib/right_agent/pid_file.rb +159 -0
  78. data/lib/right_agent/platform.rb +770 -0
  79. data/lib/right_agent/platform/unix/darwin/platform.rb +102 -0
  80. data/lib/right_agent/platform/unix/linux/platform.rb +305 -0
  81. data/lib/right_agent/platform/unix/platform.rb +226 -0
  82. data/lib/right_agent/platform/windows/mingw/platform.rb +447 -0
  83. data/lib/right_agent/platform/windows/mswin/platform.rb +236 -0
  84. data/lib/right_agent/platform/windows/platform.rb +1808 -0
  85. data/lib/right_agent/protocol_version_mixin.rb +69 -0
  86. data/lib/right_agent/retryable_request.rb +195 -0
  87. data/lib/right_agent/scripts/agent_controller.rb +543 -0
  88. data/lib/right_agent/scripts/agent_deployer.rb +400 -0
  89. data/lib/right_agent/scripts/common_parser.rb +160 -0
  90. data/lib/right_agent/scripts/log_level_manager.rb +192 -0
  91. data/lib/right_agent/scripts/stats_manager.rb +268 -0
  92. data/lib/right_agent/scripts/usage.rb +58 -0
  93. data/lib/right_agent/secure_identity.rb +92 -0
  94. data/lib/right_agent/security.rb +32 -0
  95. data/lib/right_agent/security/cached_certificate_store_proxy.rb +77 -0
  96. data/lib/right_agent/security/certificate.rb +102 -0
  97. data/lib/right_agent/security/certificate_cache.rb +89 -0
  98. data/lib/right_agent/security/distinguished_name.rb +56 -0
  99. data/lib/right_agent/security/encrypted_document.rb +83 -0
  100. data/lib/right_agent/security/rsa_key_pair.rb +76 -0
  101. data/lib/right_agent/security/signature.rb +86 -0
  102. data/lib/right_agent/security/static_certificate_store.rb +85 -0
  103. data/lib/right_agent/sender.rb +792 -0
  104. data/lib/right_agent/serialize.rb +29 -0
  105. data/lib/right_agent/serialize/message_pack.rb +107 -0
  106. data/lib/right_agent/serialize/secure_serializer.rb +151 -0
  107. data/lib/right_agent/serialize/secure_serializer_initializer.rb +47 -0
  108. data/lib/right_agent/serialize/serializable.rb +151 -0
  109. data/lib/right_agent/serialize/serializer.rb +159 -0
  110. data/lib/right_agent/subprocess.rb +38 -0
  111. data/lib/right_agent/tracer.rb +124 -0
  112. data/right_agent.gemspec +101 -0
  113. data/spec/actor_registry_spec.rb +80 -0
  114. data/spec/actor_spec.rb +162 -0
  115. data/spec/agent_config_spec.rb +235 -0
  116. data/spec/agent_identity_spec.rb +78 -0
  117. data/spec/agent_spec.rb +734 -0
  118. data/spec/agent_tag_manager_spec.rb +319 -0
  119. data/spec/clients/api_client_spec.rb +423 -0
  120. data/spec/clients/auth_client_spec.rb +272 -0
  121. data/spec/clients/balanced_http_client_spec.rb +576 -0
  122. data/spec/clients/base_retry_client_spec.rb +635 -0
  123. data/spec/clients/router_client_spec.rb +594 -0
  124. data/spec/clients/spec_helper.rb +111 -0
  125. data/spec/command/agent_manager_commands_spec.rb +51 -0
  126. data/spec/command/command_io_spec.rb +93 -0
  127. data/spec/command/command_parser_spec.rb +79 -0
  128. data/spec/command/command_runner_spec.rb +107 -0
  129. data/spec/command/command_serializer_spec.rb +51 -0
  130. data/spec/connectivity_checker_spec.rb +83 -0
  131. data/spec/core_payload_types/dev_repositories_spec.rb +64 -0
  132. data/spec/core_payload_types/dev_repository_spec.rb +33 -0
  133. data/spec/core_payload_types/executable_bundle_spec.rb +67 -0
  134. data/spec/core_payload_types/login_user_spec.rb +102 -0
  135. data/spec/core_payload_types/recipe_instantiation_spec.rb +81 -0
  136. data/spec/core_payload_types/right_script_attachment_spec.rb +65 -0
  137. data/spec/core_payload_types/right_script_instantiation_spec.rb +79 -0
  138. data/spec/core_payload_types/spec_helper.rb +23 -0
  139. data/spec/dispatched_cache_spec.rb +136 -0
  140. data/spec/dispatcher_spec.rb +324 -0
  141. data/spec/enrollment_result_spec.rb +53 -0
  142. data/spec/history_spec.rb +246 -0
  143. data/spec/log_spec.rb +192 -0
  144. data/spec/monkey_patches/eventmachine_spec.rb +62 -0
  145. data/spec/multiplexer_spec.rb +48 -0
  146. data/spec/offline_handler_spec.rb +340 -0
  147. data/spec/operation_result_spec.rb +208 -0
  148. data/spec/packets_spec.rb +461 -0
  149. data/spec/pending_requests_spec.rb +136 -0
  150. data/spec/platform/spec_helper.rb +216 -0
  151. data/spec/platform/unix/darwin/platform_spec.rb +181 -0
  152. data/spec/platform/unix/linux/platform_spec.rb +540 -0
  153. data/spec/platform/unix/spec_helper.rb +149 -0
  154. data/spec/platform/windows/mingw/platform_spec.rb +222 -0
  155. data/spec/platform/windows/mswin/platform_spec.rb +259 -0
  156. data/spec/platform/windows/spec_helper.rb +720 -0
  157. data/spec/retryable_request_spec.rb +306 -0
  158. data/spec/secure_identity_spec.rb +50 -0
  159. data/spec/security/cached_certificate_store_proxy_spec.rb +62 -0
  160. data/spec/security/certificate_cache_spec.rb +71 -0
  161. data/spec/security/certificate_spec.rb +49 -0
  162. data/spec/security/distinguished_name_spec.rb +46 -0
  163. data/spec/security/encrypted_document_spec.rb +55 -0
  164. data/spec/security/rsa_key_pair_spec.rb +55 -0
  165. data/spec/security/signature_spec.rb +66 -0
  166. data/spec/security/static_certificate_store_spec.rb +58 -0
  167. data/spec/sender_spec.rb +1045 -0
  168. data/spec/serialize/message_pack_spec.rb +131 -0
  169. data/spec/serialize/secure_serializer_spec.rb +132 -0
  170. data/spec/serialize/serializable_spec.rb +90 -0
  171. data/spec/serialize/serializer_spec.rb +197 -0
  172. data/spec/spec.opts +2 -0
  173. data/spec/spec.win32.opts +1 -0
  174. data/spec/spec_helper.rb +130 -0
  175. data/spec/tracer_spec.rb +114 -0
  176. metadata +447 -0
@@ -0,0 +1,208 @@
1
+ #
2
+ # Copyright (c) 2009-2011 RightScale Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
24
+
25
+ describe RightScale::OperationResult do
26
+
27
+ describe "when fetching results" do
28
+
29
+ before do
30
+ @success1 = RightScale::OperationResult.success(1)
31
+ @success2 = RightScale::OperationResult.success(2)
32
+ @simple_result = RightScale::Result.new("token", "to", @success1, "from")
33
+ @hash = {"from1" => @success1, "from2" => @success2}
34
+ @hash_result = RightScale::Result.new("token", "to", @hash, "from")
35
+ @nil_result = RightScale::Result.new("token", "to", nil, "from")
36
+ @error_result = RightScale::Result.new("token", "to", RightScale::OperationResult.error("Error"), "from")
37
+ end
38
+
39
+ it "should handle results hash and return only first value as an OperationResult" do
40
+ result = RightScale::OperationResult.from_results(@hash)
41
+ result.kind_of?(RightScale::OperationResult).should be_true
42
+ result.status_code.should == RightScale::OperationResult::SUCCESS
43
+ result.content.should == @hash.values[0].content
44
+ end
45
+
46
+ it "should handle individual result and return it as an OperationResult" do
47
+ result = RightScale::OperationResult.from_results(@simple_result)
48
+ result.kind_of?(RightScale::OperationResult).should be_true
49
+ result.status_code.should == RightScale::OperationResult::SUCCESS
50
+ result.content.should == 1
51
+ end
52
+
53
+ it "should handle non-success result and return it as an OperationResult" do
54
+ result = RightScale::OperationResult.from_results(@error_result)
55
+ result.kind_of?(RightScale::OperationResult).should be_true
56
+ result.status_code.should == RightScale::OperationResult::ERROR
57
+ result.content.should == "Error"
58
+ end
59
+
60
+ it "should convert unexpected string result into an error result" do
61
+ result = RightScale::OperationResult.from_results(nil)
62
+ result.kind_of?(RightScale::OperationResult).should be_true
63
+ result.status_code.should == RightScale::OperationResult::ERROR
64
+ result.content.should == "No results"
65
+ end
66
+
67
+ it "should return error OperationResult if results is nil" do
68
+ string_result = RightScale::Result.new("token", "to", "Unexpected exception", "from")
69
+ result = RightScale::OperationResult.from_results(string_result)
70
+ result.kind_of?(RightScale::OperationResult).should be_true
71
+ result.status_code.should == RightScale::OperationResult::ERROR
72
+ result.content.should == "Unexpected exception"
73
+ end
74
+
75
+ it "should return error OperationResult including Result results if the results value is not recognized" do
76
+ array_result = RightScale::Result.new("token", "to", ["some data"], "from")
77
+ result = RightScale::OperationResult.from_results(array_result)
78
+ result.kind_of?(RightScale::OperationResult).should be_true
79
+ result.status_code.should == RightScale::OperationResult::ERROR
80
+ result.content.should == 'Invalid results in Result from from: ["some data"]'
81
+ end
82
+
83
+ it "should return error OperationResult if the results value is not recognized" do
84
+ result = RightScale::OperationResult.from_results(0)
85
+ result.kind_of?(RightScale::OperationResult).should be_true
86
+ result.status_code.should == RightScale::OperationResult::ERROR
87
+ result.content.should == "Invalid operation result type: 0"
88
+ end
89
+ end
90
+
91
+ describe "when handling each type of result" do
92
+
93
+ it "should store success content value and respond to success query" do
94
+ result = RightScale::OperationResult.success({})
95
+ result.kind_of?(RightScale::OperationResult).should be_true
96
+ result.success?.should be_true
97
+ result.error?.should be_false
98
+ result.continue?.should be_false
99
+ result.retry?.should be_false
100
+ result.non_delivery?.should be_false
101
+ result.content.should == {}
102
+ end
103
+
104
+ it "should treat non-delivery as error" do
105
+ RightScale::OperationResult.non_delivery(0).error?.should be_true
106
+ end
107
+
108
+ it "should store error content value and respond to error query" do
109
+ result = RightScale::OperationResult.error("Error")
110
+ result.kind_of?(RightScale::OperationResult).should be_true
111
+ result.error?.should be_true
112
+ result.content.should == "Error"
113
+ end
114
+
115
+ it 'should store additional error string when given' do
116
+ result = RightScale::OperationResult.error("Error", "details")
117
+ result.kind_of?(RightScale::OperationResult).should be_true
118
+ result.error?.should be_true
119
+ result.content.should == "Error (details)"
120
+ end
121
+
122
+ it "should store exception info in error content value when given" do
123
+ begin
124
+ nil + "string"
125
+ rescue Exception => e
126
+ result = RightScale::OperationResult.error("Error", e)
127
+ end
128
+ result.kind_of?(RightScale::OperationResult).should be_true
129
+ result.error?.should be_true
130
+ (result.content =~ /Error \(NoMethodError: undefined method \`\+' for nil:NilClass in .*operation_result_spec.*\)$/).should be_true
131
+ end
132
+
133
+ it "should store continue content value and respond to continue query" do
134
+ result = RightScale::OperationResult.continue("Continue")
135
+ result.kind_of?(RightScale::OperationResult).should be_true
136
+ result.continue?.should be_true
137
+ result.content.should == "Continue"
138
+ end
139
+
140
+ it "should store retry content value and respond to retry query" do
141
+ result = RightScale::OperationResult.retry("Retry")
142
+ result.kind_of?(RightScale::OperationResult).should be_true
143
+ result.retry?.should be_true
144
+ result.content.should == "Retry"
145
+ end
146
+
147
+ it "should store multicast targets" do
148
+ result = RightScale::OperationResult.multicast(["target"])
149
+ result.kind_of?(RightScale::OperationResult).should be_true
150
+ result.content.should == ["target"]
151
+ end
152
+
153
+ it "should store non-delivery content value and respond to non-delivery query" do
154
+ result = RightScale::OperationResult.non_delivery("Non-delivery")
155
+ result.kind_of?(RightScale::OperationResult).should be_true
156
+ result.non_delivery?.should be_true
157
+ result.content.should == "Non-delivery"
158
+ end
159
+
160
+ it "should store cancel content value and respond to cancel query" do
161
+ result = RightScale::OperationResult.cancel("Cancel")
162
+ result.kind_of?(RightScale::OperationResult).should be_true
163
+ result.cancel?.should be_true
164
+ result.content.should == "Cancel"
165
+ end
166
+ end
167
+
168
+ describe "when converting result to string" do
169
+
170
+ it "should display error message" do
171
+ result = RightScale::OperationResult.error("some problem")
172
+ result.to_s.should == "error (some problem)"
173
+ end
174
+
175
+ it "should convert error content to string if necessary" do
176
+ result = RightScale::OperationResult.error({"data" => "some data", "message" => "some problem"})
177
+ result.to_s.should start_with ("error (")
178
+ result.to_s.should include("\"data\"=>\"some data\"")
179
+ result.to_s.should include("\"message\"=>\"some problem\"")
180
+ end
181
+
182
+ it "should truncate error message if too long" do
183
+ result = RightScale::OperationResult.error("123456789012345678901234567890123456789012345678901234567890")
184
+ result.to_s.should == "error (123456789012345678901234567890123456789012345678901234567...)"
185
+ result.status.should == "error"
186
+ end
187
+
188
+ it "should display retry reason" do
189
+ result = RightScale::OperationResult.retry("because")
190
+ result.to_s.should == "retry (because)"
191
+ result.status.should == "retry"
192
+ end
193
+
194
+ it "should display non-delivery reason" do
195
+ result = RightScale::OperationResult.non_delivery(RightScale::OperationResult::TTL_EXPIRATION)
196
+ result.to_s.should == "non-delivery (TTL expiration)"
197
+ result.status.should == "non-delivery"
198
+ end
199
+
200
+ it "should display cancel reason" do
201
+ result = RightScale::OperationResult.cancel("enough already")
202
+ result.to_s.should == "cancel (enough already)"
203
+ result.status.should == "cancel"
204
+ end
205
+
206
+ end
207
+
208
+ end
@@ -0,0 +1,461 @@
1
+ #
2
+ # Copyright (c) 2009-2011 RightScale Inc
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+
23
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
24
+
25
+ class TestPacket < RightScale::Packet
26
+ @@cls_attr = "ignore"
27
+ def initialize(attr1, duration = nil, size = nil)
28
+ @attr1 = attr1
29
+ @duration = duration
30
+ @size = size
31
+ @version = [RightScale::Packet::VERSION, RightScale::Packet::VERSION]
32
+ end
33
+ end
34
+
35
+ describe "Packet: Base class" do
36
+
37
+ it "should be an abstract class" do
38
+ lambda { RightScale::Packet.new }.should raise_error(NotImplementedError, "RightScale::Packet is an abstract class.")
39
+ end
40
+
41
+ it "should retrieve version" do
42
+ packet = TestPacket.new(1)
43
+ packet.recv_version.should == RightScale::Packet::VERSION
44
+ packet.send_version.should == RightScale::Packet::VERSION
45
+ end
46
+
47
+ it "should store version" do
48
+ packet = TestPacket.new(1)
49
+ packet.send_version.should == RightScale::Packet::VERSION
50
+ packet.send_version = 5
51
+ packet.send_version.should == 5
52
+ lambda { packet.recv_version = 5 }.should raise_error
53
+ end
54
+
55
+ it "should convert to string with packet name in lower snake case" do
56
+ packet = TestPacket.new(1)
57
+ packet.name.should == "test_packet"
58
+ packet.to_s.should == "[test_packet]"
59
+ end
60
+
61
+ it "should convert to string including version if requested" do
62
+ packet = TestPacket.new(1)
63
+ packet.send_version = 5
64
+ packet.to_s(filter = nil, version = :recv_version).should == "[test_packet v#{RightScale::Packet::VERSION}]"
65
+ packet.to_s(filter = nil, version = :send_version).should == "[test_packet v5]"
66
+ end
67
+
68
+ it "should convert to string including duration if filtered and size known" do
69
+ packet = TestPacket.new(1, 0.042122, 323)
70
+ packet.to_s(filter = nil).should == "[test_packet] (323 bytes, 0.042 sec)"
71
+ packet.to_s(filter = []).should == "[test_packet] (323 bytes)"
72
+ packet.to_s(filter = [:duration]).should == "[test_packet] (323 bytes, 0.042 sec)"
73
+ packet = TestPacket.new(1, 0.042122)
74
+ packet.to_s(filter = [:duration]).should == "[test_packet]"
75
+ end
76
+
77
+ it "should convert to string including local duration if filtered and size known" do
78
+ flexmock(Time).should_receive(:now).and_return(Time.at(1000000), Time.at(1000000.012))
79
+ packet = TestPacket.new(1, nil, 323)
80
+ packet.received_at = Time.now.to_f
81
+ packet.to_s(filter = nil).should == "[test_packet] (323 bytes, 0.012 sec)"
82
+ packet.to_s(filter = []).should == "[test_packet] (323 bytes)"
83
+ packet.to_s(filter = [:local_duration]).should == "[test_packet] (323 bytes, 0.012 sec)"
84
+ packet = TestPacket.new(1)
85
+ packet.to_s(filter = [:local_duration]).should == "[test_packet]"
86
+ end
87
+
88
+ it "should convert floating point values to decimal digit string with at least two digit precision" do
89
+ packet = TestPacket.new(1)
90
+ packet.enough_precision(100.5).should == "101"
91
+ packet.enough_precision(100.4).should == "100"
92
+ packet.enough_precision(99.0).should == "99"
93
+ packet.enough_precision(10.5).should == "11"
94
+ packet.enough_precision(10.4).should == "10"
95
+ packet.enough_precision(9.15).should == "9.2"
96
+ packet.enough_precision(9.1).should == "9.1"
97
+ packet.enough_precision(1.05).should == "1.1"
98
+ packet.enough_precision(1.01).should == "1.0"
99
+ packet.enough_precision(1.0).should == "1.0"
100
+ packet.enough_precision(0.995).should == "1.00"
101
+ packet.enough_precision(0.991).should == "0.99"
102
+ packet.enough_precision(0.0995).should == "0.100"
103
+ packet.enough_precision(0.0991).should == "0.099"
104
+ packet.enough_precision(0.00995).should == "0.0100"
105
+ packet.enough_precision(0.00991).should == "0.0099"
106
+ packet.enough_precision(0.000995).should == "0.00100"
107
+ packet.enough_precision(0.000991).should == "0.00099"
108
+ packet.enough_precision(0.000005).should == "0.00001"
109
+ packet.enough_precision(0.000001).should == "0.00000"
110
+ packet.enough_precision(0.0).should == "0"
111
+ packet.enough_precision(55).should == "55"
112
+ end
113
+
114
+ it "should be one-way by default" do
115
+ TestPacket.new(1).one_way.should be_true
116
+ end
117
+
118
+ describe "using MessagePack" do
119
+ it "should know how to dump itself to MessagePack" do
120
+ packet = TestPacket.new(1)
121
+ packet.should respond_to(:to_msgpack)
122
+ end
123
+
124
+ it "should dump the class name in 'msgpack_class' key" do
125
+ packet = TestPacket.new(42)
126
+ packet.to_msgpack.should =~ /msgpack_class.*TestPacket/
127
+ end
128
+
129
+ it "should dump instance variables in 'data' key" do
130
+ packet = TestPacket.new(188)
131
+ packet.to_msgpack.should =~ /data.*attr1/
132
+ end
133
+
134
+ it "should not dump class variables" do
135
+ packet = TestPacket.new(382)
136
+ packet.to_msgpack.should_not =~ /cls_attr/
137
+ end
138
+
139
+ it "should not dump excluded instance variables" do
140
+ packet = TestPacket.new(382)
141
+ packet.received_at = Time.now.to_f
142
+ packet.to_msgpack.should_not =~ /received_at/
143
+ end
144
+
145
+ it "should remove '@' from instance variables" do
146
+ packet = TestPacket.new(2)
147
+ packet.to_msgpack.should_not =~ /@attr1/
148
+ packet.to_msgpack.should =~ /attr1/
149
+ end
150
+ end
151
+
152
+ describe "using JSON" do
153
+ it "should know how to dump itself to JSON" do
154
+ packet = TestPacket.new(1)
155
+ packet.should respond_to(:to_json)
156
+ end
157
+
158
+ it "should dump the class name in 'json_class' key" do
159
+ packet = TestPacket.new(42)
160
+ packet.to_json.should =~ /\"json_class\":\"TestPacket\"/
161
+ end
162
+
163
+ it "should dump instance variables in 'data' key" do
164
+ packet = TestPacket.new(188, nil, 323)
165
+ json = packet.to_json
166
+ json.should =~ /\"data\":\{.*\"attr1\":188.*\}/
167
+ json.should =~ /\"data\":\{.*\"duration\":null.*\}/
168
+ json.should =~ /\"data\":\{.*\"size\":323.*\}/
169
+ json.should =~ /\"data\":\{.*\"version\":\[\d*,\d*\].*\}/
170
+ end
171
+
172
+ it "should not dump class variables" do
173
+ packet = TestPacket.new(382)
174
+ packet.to_json.should_not =~ /cls_attr/
175
+ end
176
+
177
+ it "should not dump excluded instance variables" do
178
+ packet = TestPacket.new(382)
179
+ packet.received_at = Time.now.to_f
180
+ packet.to_json.should_not =~ /received_at/
181
+ end
182
+
183
+ it "should remove '@' from instance variables" do
184
+ packet = TestPacket.new(2)
185
+ packet.to_json.should_not =~ /@attr1/
186
+ packet.to_json.should =~ /attr1/
187
+ end
188
+ end
189
+ end
190
+
191
+
192
+ describe "Packet: Request" do
193
+ it "should dump/load as MessagePack objects" do
194
+ packet = RightScale::Request.new('/some/foo', 'payload', :from => 'from', :token => '0xdeadbeef',
195
+ :reply_to => 'reply_to', :tries => ['try'])
196
+ packet2 = MessagePack.load(packet.to_msgpack)
197
+ packet.type.should == packet2.type
198
+ packet.payload.should == packet2.payload
199
+ packet.from.should == packet2.from
200
+ packet.scope.should == packet2.scope
201
+ packet.token.should == packet2.token
202
+ packet.reply_to.should == packet2.reply_to
203
+ packet.tries.should == packet2.tries
204
+ packet.expires_at.should == packet2.expires_at
205
+ packet.recv_version.should == packet2.recv_version
206
+ packet.send_version.should == packet2.send_version
207
+ end
208
+
209
+ it "should dump/load as JSON objects" do
210
+ packet = RightScale::Request.new('/some/foo', 'payload', :from => 'from', :token => '0xdeadbeef',
211
+ :reply_to => 'reply_to', :tries => ['try'])
212
+ packet2 = JSON.load(packet.to_json)
213
+ packet.type.should == packet2.type
214
+ packet.payload.should == packet2.payload
215
+ packet.from.should == packet2.from
216
+ packet.scope.should == packet2.scope
217
+ packet.token.should == packet2.token
218
+ packet.reply_to.should == packet2.reply_to
219
+ packet.tries.should == packet2.tries
220
+ packet.expires_at.should == packet2.expires_at
221
+ packet.recv_version.should == packet2.recv_version
222
+ packet.send_version.should == packet2.send_version
223
+ end
224
+
225
+ it "should default selector to :any" do
226
+ packet = RightScale::Request.new('/some/foo', 'payload')
227
+ packet.selector.should == :any
228
+ end
229
+
230
+ it "should convert selector from :least_loaded or :random to :any" do
231
+ packet = RightScale::Request.new('/some/foo', 'payload', :selector => :least_loaded)
232
+ packet.selector.should == :any
233
+ packet = RightScale::Request.new('/some/foo', 'payload', :selector => "least_loaded")
234
+ packet.selector.should == :any
235
+ packet = RightScale::Request.new('/some/foo', 'payload', :selector => :random)
236
+ packet.selector.should == :any
237
+ packet = RightScale::Request.new('/some/foo', 'payload', :selector => "random")
238
+ packet.selector.should == :any
239
+ packet = RightScale::Request.new('/some/foo', 'payload', :selector => :any)
240
+ packet.selector.should == :any
241
+ packet = RightScale::Request.new('/some/foo', 'payload')
242
+ packet.selector.should == :any
243
+ end
244
+
245
+ it "should distinguish fanout request" do
246
+ RightScale::Request.new('/some/foo', 'payload').fanout?.should be_false
247
+ RightScale::Request.new('/some/foo', 'payload', :selector => 'all').fanout?.should be_true
248
+ end
249
+
250
+ it "should convert created_at to expires_at" do
251
+ packet = RightScale::Request.new('/some/foo', 'payload', :expires_at => 100000)
252
+ packet2 = JSON.parse(packet.to_json.sub("expires_at", "created_at"))
253
+ packet2.expires_at.should == 100900
254
+ packet = RightScale::Request.new('/some/foo', 'payload', :expires_at => 0)
255
+ packet2 = JSON.parse(packet.to_json.sub("expires_at", "created_at"))
256
+ packet2.expires_at.should == 0
257
+ end
258
+
259
+ it "should not be one-way" do
260
+ RightScale::Request.new('/some/foo', 'payload').one_way.should be_false
261
+ end
262
+
263
+ it "should use current version by default when constructing" do
264
+ packet = RightScale::Request.new('/some/foo', 'payload', :from => 'from', :token => '0xdeadbeef',
265
+ :reply_to => 'reply_to', :tries => ['try'])
266
+ packet.recv_version.should == RightScale::Packet::VERSION
267
+ packet.send_version.should == RightScale::Packet::VERSION
268
+ end
269
+
270
+ it "should use default version if none supplied when unmarshalling" do
271
+ packet = RightScale::Request.new('/some/foo', 'payload', :from => 'from', :token => '0xdeadbeef',
272
+ :reply_to => 'reply_to', :tries => ['try'])
273
+ packet.instance_variable_set(:@version, nil)
274
+ MessagePack.load(packet.to_msgpack).send_version.should == RightScale::Packet::DEFAULT_VERSION
275
+ JSON.load(packet.to_json).send_version.should == RightScale::Packet::DEFAULT_VERSION
276
+ end
277
+
278
+ it "should handle either a single target or an array" do
279
+ packet = RightScale::Request.new('/some/foo', 'payload', :target => "target1")
280
+ packet.token = 123
281
+ packet.to_s([:target]).should == '[request] <> <123> /some/foo, target target1'
282
+ packet = RightScale::Request.new('/some/foo', 'payload', :target => ["target1", "target2"])
283
+ packet.token = 123
284
+ packet.to_s([:target]).should == "[request] <> <123> /some/foo, target [target1, target2]"
285
+ end
286
+ end
287
+
288
+
289
+ describe "Packet: Push" do
290
+ it "should dump/load as MessagePack objects" do
291
+ packet = RightScale::Push.new('/some/foo', 'payload', :from => 'from', :token => '0xdeadbeef', :tries => ['try'])
292
+ packet2 = MessagePack.load(packet.to_msgpack)
293
+ packet.type.should == packet2.type
294
+ packet.payload.should == packet2.payload
295
+ packet.from.should == packet2.from
296
+ packet.token.should == packet2.token
297
+ packet.tries.should == packet2.tries
298
+ packet.confirm.should == packet2.confirm
299
+ packet.expires_at.should == packet2.expires_at
300
+ packet.recv_version.should == packet2.recv_version
301
+ packet.send_version.should == packet2.send_version
302
+ end
303
+
304
+ it "should dump/load as JSON objects" do
305
+ packet = RightScale::Push.new('/some/foo', 'payload', :from => 'from', :token => '0xdeadbeef', :tries => ['try'])
306
+ packet2 = JSON.load(packet.to_json)
307
+ packet.type.should == packet2.type
308
+ packet.payload.should == packet2.payload
309
+ packet.from.should == packet2.from
310
+ packet.token.should == packet2.token
311
+ packet.tries.should == packet2.tries
312
+ packet.confirm.should == packet2.confirm
313
+ packet.expires_at.should == packet2.expires_at
314
+ packet.recv_version.should == packet2.recv_version
315
+ packet.send_version.should == packet2.send_version
316
+ end
317
+
318
+ it "should default selector to :any" do
319
+ packet = RightScale::Push.new('/some/foo', 'payload')
320
+ packet.selector.should == :any
321
+ end
322
+
323
+ it "should convert selector from :least_loaded or :random to :any" do
324
+ packet = RightScale::Push.new('/some/foo', 'payload', :selector => :least_loaded)
325
+ packet.selector.should == :any
326
+ packet = RightScale::Push.new('/some/foo', 'payload', :selector => "least_loaded")
327
+ packet.selector.should == :any
328
+ packet = RightScale::Push.new('/some/foo', 'payload', :selector => :random)
329
+ packet.selector.should == :any
330
+ packet = RightScale::Push.new('/some/foo', 'payload', :selector => "random")
331
+ packet.selector.should == :any
332
+ packet = RightScale::Push.new('/some/foo', 'payload', :selector => :any)
333
+ packet.selector.should == :any
334
+ end
335
+
336
+ it "should distinguish fanout request" do
337
+ RightScale::Push.new('/some/foo', 'payload').fanout?.should be_false
338
+ RightScale::Push.new('/some/foo', 'payload', :selector => 'all').fanout?.should be_true
339
+ end
340
+
341
+ it "should not convert created_at to expires_at" do
342
+ packet = RightScale::Push.new('/some/foo', 'payload', :expires_at => 100000)
343
+ packet2 = JSON.parse(packet.to_json.sub("expires_at", "created_at"))
344
+ packet2.expires_at.should == 0
345
+ packet = RightScale::Push.new('/some/foo', 'payload', :expires_at => 0)
346
+ packet2 = JSON.parse(packet.to_json.sub("expires_at", "created_at"))
347
+ packet2.expires_at.should == 0
348
+ end
349
+
350
+ it "should be one-way" do
351
+ RightScale::Push.new('/some/foo', 'payload').one_way.should be_true
352
+ end
353
+
354
+ it "should use current version by default when constructing" do
355
+ packet = RightScale::Push.new('/some/foo', 'payload', :from => 'from', :token => '0xdeadbeef', :tries => ['try'])
356
+ packet.recv_version.should == RightScale::Packet::VERSION
357
+ packet.send_version.should == RightScale::Packet::VERSION
358
+ end
359
+
360
+ it "should use default version if none supplied when unmarshalling" do
361
+ packet = RightScale::Push.new('/some/foo', 'payload', :from => 'from', :token => '0xdeadbeef', :tries => ['try'])
362
+ packet.instance_variable_set(:@version, nil)
363
+ MessagePack.load(packet.to_msgpack).send_version.should == RightScale::Packet::DEFAULT_VERSION
364
+ JSON.load(packet.to_json).send_version.should == RightScale::Packet::DEFAULT_VERSION
365
+ end
366
+
367
+ it "should handle either a single target or an array" do
368
+ packet = RightScale::Push.new('/some/foo', 'payload', :target => "target1")
369
+ packet.token = 123
370
+ packet.to_s([:target]).should == '[push] <> <123> /some/foo, target target1'
371
+ packet = RightScale::Push.new('/some/foo', 'payload', :target => ["target1", "target2"])
372
+ packet.token = 123
373
+ packet.to_s([:target]).should == "[push] <> <123> /some/foo, target [target1, target2]"
374
+ end
375
+ end
376
+
377
+
378
+ describe "Packet: Result" do
379
+ it "should dump/load as MessagePack objects" do
380
+ packet = RightScale::Result.new('0xdeadbeef', 'to', 'results', 'from', 'request_from', ['try'], true)
381
+ packet2 = MessagePack.load(packet.to_msgpack)
382
+ packet.token.should == packet2.token
383
+ packet.to.should == packet2.to
384
+ packet.results.should == packet2.results
385
+ packet.from.should == packet2.from
386
+ packet.request_from.should == packet2.request_from
387
+ packet.tries.should == packet2.tries
388
+ packet.persistent.should == packet2.persistent
389
+ packet.recv_version.should == packet2.recv_version
390
+ packet.send_version.should == packet2.send_version
391
+ end
392
+
393
+ it "should dump/load as JSON objects" do
394
+ packet = RightScale::Result.new('0xdeadbeef', 'to', 'results', 'from', 'request_from', ['try'], true)
395
+ packet2 = JSON.load(packet.to_json)
396
+ packet.token.should == packet2.token
397
+ packet.to.should == packet2.to
398
+ packet.results.should == packet2.results
399
+ packet.from.should == packet2.from
400
+ packet.request_from.should == packet2.request_from
401
+ packet.tries.should == packet2.tries
402
+ packet.persistent.should == packet2.persistent
403
+ packet.recv_version.should == packet2.recv_version
404
+ packet.send_version.should == packet2.send_version
405
+ end
406
+
407
+ it "should use current version by default when constructing" do
408
+ packet = RightScale::Result.new('0xdeadbeef', 'to', 'results', 'from', 'request_from', ['try'], true)
409
+ packet.recv_version.should == RightScale::Packet::VERSION
410
+ packet.send_version.should == RightScale::Packet::VERSION
411
+ end
412
+
413
+ it "should use default version if none supplied when unmarshalling" do
414
+ packet = RightScale::Result.new('0xdeadbeef', 'to', 'results', 'from', 'request_from', ['try'], true)
415
+ packet.instance_variable_set(:@version, nil)
416
+ MessagePack.load(packet.to_msgpack).send_version.should == RightScale::Packet::DEFAULT_VERSION
417
+ JSON.load(packet.to_json).send_version.should == RightScale::Packet::DEFAULT_VERSION
418
+ end
419
+
420
+ it "should be one-way" do
421
+ RightScale::Result.new('0xdeadbeef', 'to', 'results', 'from').one_way.should be_true
422
+ end
423
+ end
424
+
425
+
426
+ describe "Packet: Stats" do
427
+ it "should dump/load as MessagePack objects" do
428
+ packet = RightScale::Stats.new(['data'], 'from')
429
+ packet2 = MessagePack.load(packet.to_msgpack)
430
+ packet.data.should == packet2.data
431
+ packet.from.should == packet2.from
432
+ packet.recv_version.should == packet2.recv_version
433
+ packet.send_version.should == packet2.send_version
434
+ end
435
+
436
+ it "should dump/load as JSON objects" do
437
+ packet = RightScale::Stats.new(['data'], 'from')
438
+ packet2 = JSON.load(packet.to_json)
439
+ packet.data.should == packet2.data
440
+ packet.from.should == packet2.from
441
+ packet.recv_version.should == packet2.recv_version
442
+ packet.send_version.should == packet2.send_version
443
+ end
444
+
445
+ it "should use current version by default when constructing" do
446
+ packet = RightScale::Stats.new(['data'], 'from')
447
+ packet.recv_version.should == RightScale::Packet::VERSION
448
+ packet.send_version.should == RightScale::Packet::VERSION
449
+ end
450
+
451
+ it "should use default version if none supplied when unmarshalling" do
452
+ packet = RightScale::Stats.new(['data'], 'from')
453
+ packet.instance_variable_set(:@version, nil)
454
+ MessagePack.load(packet.to_msgpack).send_version.should == RightScale::Packet::DEFAULT_VERSION
455
+ JSON.load(packet.to_json).send_version.should == RightScale::Packet::DEFAULT_VERSION
456
+ end
457
+
458
+ it "should be one-way" do
459
+ RightScale::Stats.new(['data'], 'from').one_way.should be_true
460
+ end
461
+ end