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,53 @@
1
+ # Copyright (c) 2009-2011 RightScale, Inc, All Rights Reserved Worldwide.
2
+ #
3
+ # THIS PROGRAM IS CONFIDENTIAL AND PROPRIETARY TO RIGHTSCALE
4
+ # AND CONSTITUTES A VALUABLE TRADE SECRET. Any unauthorized use,
5
+ # reproduction, modification, or disclosure of this program is
6
+ # strictly prohibited. Any use of this program by an authorized
7
+ # licensee is strictly subject to the terms and conditions,
8
+ # including confidentiality obligations, set forth in the applicable
9
+ # License Agreement between RightScale.com, Inc. and
10
+ # the licensee.
11
+
12
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
13
+ require File.expand_path(File.join(File.dirname(__FILE__), '..' , 'lib', 'right_agent', 'enrollment_result'))
14
+
15
+ describe RightScale::EnrollmentResult do
16
+ before(:each) do
17
+ @key = 'topsecret'
18
+ @result = RightScale::EnrollmentResult.new(6, Time.now, 'router cert', 'my cert', 'my private key', @key)
19
+ @message = RightScale::EnrollmentResult.dump(@result)
20
+ end
21
+
22
+ it 'should serialize and unserialize correctly' do
23
+ r2 = RightScale::EnrollmentResult.load(@message, @key)
24
+ @result.should == r2
25
+ end
26
+
27
+ context "supporting different versions" do
28
+ RightScale::EnrollmentResult::SUPPORTED_VERSIONS.each do |v|
29
+ it "should support version #{v}" do
30
+ @result = RightScale::EnrollmentResult.new(v, Time.now, 'router cert', 'my cert', 'my private key', @key)
31
+ serialized = RightScale::EnrollmentResult.dump(@result)
32
+ @result2 = RightScale::EnrollmentResult.load(serialized, @key)
33
+ @result.should == @result2
34
+ end
35
+ end
36
+ end
37
+
38
+ it 'should fail to decrypt if tampered with' do
39
+ #Simulate some ciphertext tampering.
40
+ @message.gsub! /[0-9]/, '1'
41
+ @message.gsub! /"r_s_version":"[0-9]+"/, '"r_s_version":"6"'
42
+
43
+ lambda do
44
+ RightScale::EnrollmentResult.load(@message, @key)
45
+ end.should raise_error(RightScale::EnrollmentResult::IntegrityFailure)
46
+ end
47
+
48
+ it 'should fail to decrypt if the key is wrong' do
49
+ lambda do
50
+ RightScale::EnrollmentResult.load(@message, @key + "evil")
51
+ end.should raise_error(RightScale::EnrollmentResult::IntegrityFailure)
52
+ end
53
+ end
@@ -0,0 +1,246 @@
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
+ require 'tmpdir'
25
+
26
+ describe RightScale::History do
27
+
28
+ before(:each) do
29
+ @identity = "rs-agent-1-1"
30
+ @pid = Process.pid
31
+ now = Time.at(1000000)
32
+ flexmock(Time).should_receive(:now).and_return { now += 10 }
33
+ FileUtils.mkdir_p(@test_dir = File.join(RightScale::Platform.filesystem.temp_dir, 'history_test'))
34
+ RightScale::AgentConfig.pid_dir = @test_dir
35
+ @history = RightScale::History.new(@identity)
36
+ end
37
+
38
+ after(:each) do
39
+ FileUtils.remove_dir(@test_dir)
40
+ end
41
+
42
+ describe "update" do
43
+
44
+ it "should store event in history file" do
45
+ @history.update("some event").should be_true
46
+ @history.load.should == [{"time" => 1000010, "pid" => @pid, "event" => "some event"}]
47
+ end
48
+
49
+ it "should store event in history file following previous event" do
50
+ @history.update("some event").should be_true
51
+ @history.update("another event").should be_true
52
+ @history.load.should == [{"time" => 1000010, "pid" => @pid, "event" => "some event"},
53
+ {"time" => 1000020, "pid" => @pid, "event" => "another event"}]
54
+ end
55
+
56
+ end
57
+
58
+ describe "load" do
59
+
60
+ it "should load no events if there is no history" do
61
+ @history.load.should == []
62
+ end
63
+
64
+ it "should load events from history file" do
65
+ @history.update("some event").should be_true
66
+ @history.load.should == [{"time" => 1000010, "pid" => @pid, "event" => "some event"}]
67
+ end
68
+
69
+ end
70
+
71
+ describe "analyze_service" do
72
+
73
+ it "should indicate no uptime if history empty" do
74
+ @history.analyze_service.should == {:uptime => 0, :total_uptime => 0}
75
+ end
76
+
77
+ it "should indicate no uptime if not yet running" do
78
+ @history.update("start")
79
+ @history.analyze_service.should == {:uptime => 0, :total_uptime => 0}
80
+ end
81
+
82
+ it "should measure uptime starting from last run time" do
83
+ @history.update("start")
84
+ @history.update("run")
85
+ @history.analyze_service.should == {:uptime => 10, :total_uptime => 10}
86
+ end
87
+
88
+ it "should count restarts" do
89
+ @history.update("start")
90
+ @history.update("stop")
91
+ @history.update("start")
92
+ @history.analyze_service[:restarts].should == 1
93
+ end
94
+
95
+ it "should not count initial start as a restart" do
96
+ @history.update("start")
97
+ @history.update("run")
98
+ @history.analyze_service[:restarts].should be_nil
99
+ end
100
+
101
+ it "should ignore repeated stops when counting restarts" do
102
+ @history.update("start")
103
+ @history.update("stop")
104
+ @history.update("stop")
105
+ @history.update("start")
106
+ @history.analyze_service[:restarts].should == 1
107
+ end
108
+
109
+ it "should record number of graceful exits if there are restarts" do
110
+ @history.update("start")
111
+ @history.update("run")
112
+ @history.update("stop")
113
+ @history.update("graceful exit")
114
+ @history.update("start")
115
+ @history.update("stop")
116
+ @history.update("start")
117
+ @history.update("run")
118
+ @history.update("stop")
119
+ @history.update("graceful exit")
120
+ @history.update("start")
121
+ @history.update("run")
122
+ @history.analyze_service[:graceful_exits].should == 2
123
+ @history.analyze_service[:restarts].should == 3
124
+ end
125
+
126
+ it "should measure total uptime across restarts and include current uptime" do
127
+ @history.update("start")
128
+ @history.update("run")
129
+ @history.update("stop")
130
+ @history.update("graceful exit")
131
+ @history.update("start")
132
+ @history.update("stop")
133
+ @history.update("start")
134
+ @history.update("run")
135
+ @history.update("stop")
136
+ @history.update("graceful exit")
137
+ @history.update("start")
138
+ @history.update("run")
139
+ @history.analyze_service.should == {:uptime => 10, :total_uptime => 30, :restarts => 3, :graceful_exits => 2}
140
+ end
141
+
142
+ it "should count crashes" do
143
+ @history.update("start")
144
+ @history.update("start")
145
+ @history.analyze_service[:crashes].should == 1
146
+ @history.analyze_service[:last_crash_time].should == 1000020
147
+ @history.analyze_service[:crashed_last].should be_true
148
+ end
149
+
150
+ it "should record last crash age if there are crashes" do
151
+ @history.update("start")
152
+ @history.update("start")
153
+ @history.analyze_service[:crashed_last].should be_true
154
+ end
155
+
156
+ it "should determine whether crashed last time was started" do
157
+ @history.update("start")
158
+ @history.update("start")
159
+ @history.analyze_service[:crashed_last].should be_true
160
+ @history.update("run")
161
+ @history.analyze_service[:crashed_last].should be_true
162
+ @history.update("stop")
163
+ @history.analyze_service[:crashed_last].should be_false
164
+ @history.update("start")
165
+ @history.analyze_service[:crashed_last].should be_false
166
+ @history.update("stop")
167
+ @history.update("start")
168
+ @history.analyze_service[:crashed_last].should be_false
169
+ @history.update("run")
170
+ @history.update("stop")
171
+ @history.update("graceful exit")
172
+ @history.analyze_service[:crashed_last].should be_false
173
+ @history.update("start")
174
+ @history.analyze_service[:crashed_last].should be_false
175
+ @history.update("start")
176
+ @history.analyze_service[:crashed_last].should be_true
177
+ end
178
+
179
+ it "should count restarts and crashes" do
180
+ @history.update("start")
181
+ @history.update("run")
182
+ @history.update("stop")
183
+ @history.update("graceful exit")
184
+ @history.update("start")
185
+ @history.update("start")
186
+ @history.update("stop")
187
+ @history.update("start")
188
+ @history.update("run")
189
+ @history.update("stop")
190
+ @history.update("graceful exit")
191
+ @history.update("start")
192
+ @history.update("run")
193
+ @history.update("start")
194
+ @history.update("run")
195
+ @history.analyze_service[:crashes].should == 2
196
+ @history.analyze_service[:restarts].should == 3
197
+ @history.analyze_service[:last_crash_time].should == 1000140
198
+ @history.analyze_service[:crashed_last].should be_true
199
+ end
200
+
201
+ it "should ignore unrecognized events" do
202
+ @history.update("other event")
203
+ @history.update("start")
204
+ @history.update("other event")
205
+ @history.update("stop")
206
+ @history.update("start")
207
+ @history.update("other event")
208
+ @history.analyze_service.should == {:uptime => 0, :total_uptime => 0, :restarts => 1, :graceful_exits => 0}
209
+ end
210
+
211
+ it "should not re-analyze if there are no new events" do
212
+ @history.update("start")
213
+ @history.update("run")
214
+ @history.analyze_service.should == {:uptime => 10, :total_uptime => 10}
215
+ flexmock(@history).should_receive(:load).never
216
+ @history.analyze_service
217
+ end
218
+
219
+ it "should update uptime and total_uptime even if do not do re-analyze" do
220
+ @history.update("start")
221
+ @history.update("run")
222
+ @history.update("start")
223
+ @history.update("run")
224
+ @history.update("stop")
225
+ @history.update("graceful exit")
226
+ @history.update("start")
227
+ @history.update("run")
228
+ @history.analyze_service.should == {:uptime => 10, :total_uptime => 30, :restarts => 1, :graceful_exits => 1,
229
+ :crashes => 1, :last_crash_time => 1000030, :crashed_last => false}
230
+ @history.analyze_service.should == {:uptime => 20, :total_uptime => 40, :restarts => 1, :graceful_exits => 1,
231
+ :crashes => 1, :last_crash_time => 1000030, :crashed_last => false}
232
+ end
233
+
234
+ it "should re-analyze if there was a new event since last analysis" do
235
+ @history.update("start")
236
+ @history.update("run")
237
+ @history.analyze_service.should == {:uptime => 10, :total_uptime => 10}
238
+ @history.instance_variable_set(:@pid, -1) # Simulate new process id following crash
239
+ @history.update("start")
240
+ @history.analyze_service.should == {:uptime => 0, :total_uptime => 20, :crashes => 1, :last_crash_time => 1000040,
241
+ :crashed_last => true}
242
+ end
243
+
244
+ end
245
+
246
+ end
@@ -0,0 +1,192 @@
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::Log do
26
+
27
+ # Count number of lines logged with the specified text
28
+ # Search is case sensitive and regular expression sensitive
29
+ #
30
+ # === Parameters
31
+ # text(String):: Text to search for
32
+ #
33
+ # === Return
34
+ # (Integer):: Number of lines where text found
35
+ def log_count(text)
36
+ # no such animal as egrep in windows, so hacked up an equivalent here.
37
+ pattern = Regexp.compile(text)
38
+ File.read(@log_file).each_line.select { |x| x =~ pattern }.count
39
+ end
40
+
41
+ before(:all) do
42
+ ENV['RS_LOG'] = 'true'
43
+
44
+ class RightScale::Platform::Filesystem
45
+ alias original_log_dir log_dir
46
+ def log_dir
47
+ # For specs, write all logs to temp_dir
48
+ # since log_dir may not be writable
49
+ temp_dir
50
+ end
51
+ end
52
+ end
53
+
54
+ after(:all) do
55
+ class RightScale::Platform::Filesystem
56
+ alias log_dir original_log_dir
57
+ end
58
+ end
59
+
60
+ before(:each) do
61
+ Singleton.__init__(RightScale::Log)
62
+ end
63
+
64
+ after(:all) do
65
+ Singleton.__init__(RightScale::Log)
66
+ ENV['RS_LOG'] = nil
67
+ end
68
+
69
+ it 'should set default level to info' do
70
+ RightScale::Log.level.should == :info
71
+ end
72
+
73
+ it 'should change level to debug when force_debug' do
74
+ RightScale::Log.level.should == :info
75
+ RightScale::Log.force_debug
76
+ RightScale::Log.level.should == :debug
77
+ end
78
+
79
+ it 'should notify of log level change' do
80
+ RightScale::Log.level.should == :info
81
+ notified = nil
82
+ RightScale::Log.notify(lambda{ |l| notified = l })
83
+ RightScale::Log.level = :debug
84
+ notified.should == Logger::DEBUG
85
+ end
86
+
87
+ context "logging" do
88
+
89
+ before(:each) do
90
+ # note that log directory doesn't necessarily exist in Windows dev/test
91
+ # environment.
92
+ log_dir = RightScale::Platform.filesystem.log_dir
93
+ FileUtils.mkdir_p(log_dir) unless File.directory?(log_dir)
94
+
95
+ # use a unique name for the test because the file cannot be deleted after
96
+ # the test in the Windows case (until the process exits) and we need to
97
+ # avoid any potential conflicts with other tests, etc.
98
+ @log_name = "log_test-9D9A9CB7-24A1-4093-9F75-D462D373A0D8"
99
+ @log_file = File.join(log_dir, "#{@log_name}.log")
100
+ RightScale::Log.program_name = "tester"
101
+ RightScale::Log.log_to_file_only(true)
102
+ RightScale::Log.init(@log_name, log_dir)
103
+ end
104
+
105
+ after(:each) do
106
+ # note that the log is held open in the Windows case by Log so we
107
+ # cannot delete it after each test. on the other hand, we can truncate the
108
+ # log to zero size and continue to the next test (ultimately leaving an
109
+ # empty file after the test, which is acceptable).
110
+ File.delete(@log_file) if File.file?(@log_file) rescue File.truncate(@log_file, 0)
111
+ end
112
+
113
+ it 'should log info but not debug by default' do
114
+ RightScale::Log.debug("Test debug")
115
+ RightScale::Log.info("Test info")
116
+ log_count("Test debug$").should == 0
117
+ log_count("Test info$").should == 1
118
+ end
119
+
120
+ it 'should log debug after adjust level to :debug' do
121
+ RightScale::Log.level = :debug
122
+ RightScale::Log.debug("Test debug")
123
+ RightScale::Log.info("Test info")
124
+ log_count("Test debug$").should == 1
125
+ log_count("Test info$").should == 1
126
+ end
127
+
128
+ it 'should log additional error string when given' do
129
+ RightScale::Log.warning("Test warning", "Error")
130
+ RightScale::Log.error("Test error", "Error")
131
+ log_count("Test warning \\\(Error\\\)$").should == 1
132
+ log_count("Test error \\\(Error\\\)$").should == 1
133
+ end
134
+
135
+ it 'should log without exception when none given' do
136
+ begin
137
+ nil + "string"
138
+ rescue Exception => e
139
+ RightScale::Log.warning("Test warning")
140
+ RightScale::Log.error("Test error")
141
+ end
142
+ log_count("Test warning$").should == 1
143
+ log_count("Test error$").should == 1
144
+ end
145
+
146
+ it 'should log with exception class, message, and caller appended by default when exception given' do
147
+ begin
148
+ nil + "string"
149
+ rescue Exception => e
150
+ RightScale::Log.warning("Test warning", e)
151
+ RightScale::Log.error("Test error", e)
152
+ end
153
+ log_count("Test warning \\\(NoMethodError: undefined method \\\`\\\+\' for nil:NilClass in .*log_spec.*\\\)$").should == 1
154
+ log_count("Test error \\\(NoMethodError: undefined method \\\`\\\+\' for nil:NilClass in .*log_spec.*\\\)$").should == 1
155
+ end
156
+
157
+ it 'should log with exception class, message, and caller appended when use :caller' do
158
+ begin
159
+ nil + "string"
160
+ rescue Exception => e
161
+ RightScale::Log.warning("Test warning", e, :caller)
162
+ RightScale::Log.error("Test error", e, :caller)
163
+ end
164
+ log_count("Test warning \\\(NoMethodError: undefined method \\\`\\\+\' for nil:NilClass in .*log_spec.*\\\)$").should == 1
165
+ log_count("Test error \\\(NoMethodError: undefined method \\\`\\\+\' for nil:NilClass in .*log_spec.*\\\)$").should == 1
166
+ end
167
+
168
+ it 'should log with exception class and message appended when use :no_trace' do
169
+ begin
170
+ nil + "string"
171
+ rescue Exception => e
172
+ RightScale::Log.warning("Test warning", e, :no_trace)
173
+ RightScale::Log.error("Test error", e, :no_trace)
174
+ end
175
+ log_count("Test warning \\\(NoMethodError: undefined method \\\`\\\+\' for nil:NilClass\\\)$").should == 1
176
+ log_count("Test error \\\(NoMethodError: undefined method \\\`\\\+\' for nil:NilClass\\\)$").should == 1
177
+ end
178
+
179
+ it 'should log with exception class, message, and full backtrace appended when use :trace' do
180
+ begin
181
+ nil + "string"
182
+ rescue Exception => e
183
+ RightScale::Log.warning("Test warning", e, :trace)
184
+ RightScale::Log.error("Test error", e, :trace)
185
+ end
186
+ log_count("Test warning \\\(NoMethodError: undefined method \\\`\\\+\' for nil:NilClass in$").should == 1
187
+ log_count("Test error \\\(NoMethodError: undefined method \\\`\\\+\' for nil:NilClass in$").should == 1
188
+ end
189
+
190
+ end
191
+
192
+ end