wakame 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. data/History.txt +20 -0
  2. data/README.rdoc +63 -0
  3. data/Rakefile +86 -0
  4. data/VERSION +1 -0
  5. data/app_generators/wakame/templates/README +0 -0
  6. data/app_generators/wakame/templates/Rakefile +18 -0
  7. data/app_generators/wakame/templates/bin/wakame-agent +9 -0
  8. data/app_generators/wakame/templates/bin/wakame-master +9 -0
  9. data/app_generators/wakame/templates/bin/wakameadm +9 -0
  10. data/app_generators/wakame/templates/cluster/resources/apache_app/apache_app.rb +54 -0
  11. data/app_generators/wakame/templates/cluster/resources/apache_app/conf/apache2.conf +46 -0
  12. data/app_generators/wakame/templates/cluster/resources/apache_app/conf/envvars-app +7 -0
  13. data/app_generators/wakame/templates/cluster/resources/apache_app/conf/sites-app.conf +23 -0
  14. data/app_generators/wakame/templates/cluster/resources/apache_app/conf/system-app.conf +67 -0
  15. data/app_generators/wakame/templates/cluster/resources/apache_app/init.d/apache2-app +192 -0
  16. data/app_generators/wakame/templates/cluster/resources/apache_lb/apache_lb.rb +56 -0
  17. data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/apache2.conf +46 -0
  18. data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/envvars-lb +6 -0
  19. data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/sites-lb.conf +54 -0
  20. data/app_generators/wakame/templates/cluster/resources/apache_lb/conf/system-lb.conf +75 -0
  21. data/app_generators/wakame/templates/cluster/resources/apache_lb/init.d/apache2-lb +192 -0
  22. data/app_generators/wakame/templates/cluster/resources/apache_www/apache_www.rb +50 -0
  23. data/app_generators/wakame/templates/cluster/resources/apache_www/conf/apache2.conf +47 -0
  24. data/app_generators/wakame/templates/cluster/resources/apache_www/conf/envvars-www +7 -0
  25. data/app_generators/wakame/templates/cluster/resources/apache_www/conf/sites-www.conf +23 -0
  26. data/app_generators/wakame/templates/cluster/resources/apache_www/conf/system-www.conf +63 -0
  27. data/app_generators/wakame/templates/cluster/resources/apache_www/init.d/apache2-www +192 -0
  28. data/app_generators/wakame/templates/cluster/resources/ec2_elastic_ip/ec2_elastic_ip.rb +39 -0
  29. data/app_generators/wakame/templates/cluster/resources/mysql_master/conf/my.cnf +154 -0
  30. data/app_generators/wakame/templates/cluster/resources/mysql_master/init.d/mysql +185 -0
  31. data/app_generators/wakame/templates/cluster/resources/mysql_master/mysql_master.rb +174 -0
  32. data/app_generators/wakame/templates/config/boot.rb +85 -0
  33. data/app_generators/wakame/templates/config/cluster.rb +64 -0
  34. data/app_generators/wakame/templates/config/environments/common.rb +0 -0
  35. data/app_generators/wakame/templates/config/environments/ec2.rb +3 -0
  36. data/app_generators/wakame/templates/config/environments/stand_alone.rb +0 -0
  37. data/app_generators/wakame/templates/config/init.d/wakame-agent +72 -0
  38. data/app_generators/wakame/templates/config/init.d/wakame-master +73 -0
  39. data/app_generators/wakame/wakame_generator.rb +124 -0
  40. data/bin/wakame +18 -0
  41. data/contrib/imagesetup.sh +77 -0
  42. data/lib/ext/eventmachine.rb +86 -0
  43. data/lib/ext/shellwords.rb +172 -0
  44. data/lib/ext/uri.rb +15 -0
  45. data/lib/wakame/action.rb +156 -0
  46. data/lib/wakame/actions/destroy_instances.rb +39 -0
  47. data/lib/wakame/actions/launch_cluster.rb +31 -0
  48. data/lib/wakame/actions/migrate_service.rb +65 -0
  49. data/lib/wakame/actions/propagate_instances.rb +95 -0
  50. data/lib/wakame/actions/reload_service.rb +21 -0
  51. data/lib/wakame/actions/scaleout_when_high_load.rb +44 -0
  52. data/lib/wakame/actions/shutdown_cluster.rb +22 -0
  53. data/lib/wakame/actions/shutdown_vm.rb +19 -0
  54. data/lib/wakame/actions/start_service.rb +64 -0
  55. data/lib/wakame/actions/stop_service.rb +49 -0
  56. data/lib/wakame/actions/util.rb +71 -0
  57. data/lib/wakame/actor/daemon.rb +37 -0
  58. data/lib/wakame/actor/service_monitor.rb +21 -0
  59. data/lib/wakame/actor/system.rb +46 -0
  60. data/lib/wakame/actor.rb +33 -0
  61. data/lib/wakame/agent.rb +226 -0
  62. data/lib/wakame/amqp_client.rb +219 -0
  63. data/lib/wakame/command/action_status.rb +62 -0
  64. data/lib/wakame/command/actor.rb +23 -0
  65. data/lib/wakame/command/clone_service.rb +12 -0
  66. data/lib/wakame/command/launch_cluster.rb +15 -0
  67. data/lib/wakame/command/migrate_service.rb +21 -0
  68. data/lib/wakame/command/propagate_service.rb +24 -0
  69. data/lib/wakame/command/shutdown_cluster.rb +15 -0
  70. data/lib/wakame/command/status.rb +81 -0
  71. data/lib/wakame/command.rb +31 -0
  72. data/lib/wakame/command_queue.rb +44 -0
  73. data/lib/wakame/configuration.rb +93 -0
  74. data/lib/wakame/daemonize.rb +96 -0
  75. data/lib/wakame/event.rb +232 -0
  76. data/lib/wakame/event_dispatcher.rb +154 -0
  77. data/lib/wakame/graph.rb +79 -0
  78. data/lib/wakame/initializer.rb +162 -0
  79. data/lib/wakame/instance_counter.rb +78 -0
  80. data/lib/wakame/logger.rb +12 -0
  81. data/lib/wakame/manager/commands.rb +134 -0
  82. data/lib/wakame/master.rb +369 -0
  83. data/lib/wakame/monitor/agent.rb +50 -0
  84. data/lib/wakame/monitor/service.rb +183 -0
  85. data/lib/wakame/monitor.rb +69 -0
  86. data/lib/wakame/packets.rb +160 -0
  87. data/lib/wakame/queue_declare.rb +14 -0
  88. data/lib/wakame/rule.rb +116 -0
  89. data/lib/wakame/rule_engine.rb +202 -0
  90. data/lib/wakame/runner/administrator_command.rb +112 -0
  91. data/lib/wakame/runner/agent.rb +81 -0
  92. data/lib/wakame/runner/master.rb +93 -0
  93. data/lib/wakame/scheduler.rb +251 -0
  94. data/lib/wakame/service.rb +914 -0
  95. data/lib/wakame/template.rb +189 -0
  96. data/lib/wakame/trigger.rb +66 -0
  97. data/lib/wakame/triggers/instance_count_update.rb +45 -0
  98. data/lib/wakame/triggers/load_history.rb +107 -0
  99. data/lib/wakame/triggers/maintain_ssh_known_hosts.rb +43 -0
  100. data/lib/wakame/triggers/process_command.rb +34 -0
  101. data/lib/wakame/triggers/shutdown_unused_vm.rb +16 -0
  102. data/lib/wakame/util.rb +569 -0
  103. data/lib/wakame/vm_manipulator.rb +186 -0
  104. data/lib/wakame.rb +59 -0
  105. data/tasks/ec2.rake +127 -0
  106. data/tests/cluster.json +3 -0
  107. data/tests/conf/a +1 -0
  108. data/tests/conf/b +1 -0
  109. data/tests/conf/c +1 -0
  110. data/tests/setup_agent.rb +39 -0
  111. data/tests/setup_master.rb +28 -0
  112. data/tests/test_actor.rb +54 -0
  113. data/tests/test_agent.rb +218 -0
  114. data/tests/test_amqp_client.rb +94 -0
  115. data/tests/test_graph.rb +36 -0
  116. data/tests/test_master.rb +167 -0
  117. data/tests/test_monitor.rb +47 -0
  118. data/tests/test_rule_engine.rb +127 -0
  119. data/tests/test_scheduler.rb +123 -0
  120. data/tests/test_service.rb +60 -0
  121. data/tests/test_template.rb +67 -0
  122. data/tests/test_uri_amqp.rb +19 -0
  123. data/tests/test_util.rb +71 -0
  124. data/wakame_generators/resource/resource_generator.rb +54 -0
  125. data/wakame_generators/resource/templates/apache_app/apache_app.rb +60 -0
  126. data/wakame_generators/resource/templates/apache_app/conf/apache2.conf +46 -0
  127. data/wakame_generators/resource/templates/apache_app/conf/envvars-app +7 -0
  128. data/wakame_generators/resource/templates/apache_app/conf/sites-app.conf +23 -0
  129. data/wakame_generators/resource/templates/apache_app/conf/system-app.conf +67 -0
  130. data/wakame_generators/resource/templates/apache_app/init.d/apache2-app +192 -0
  131. data/wakame_generators/resource/templates/apache_lb/apache_lb.rb +67 -0
  132. data/wakame_generators/resource/templates/apache_lb/conf/apache2.conf +46 -0
  133. data/wakame_generators/resource/templates/apache_lb/conf/envvars-lb +6 -0
  134. data/wakame_generators/resource/templates/apache_lb/conf/sites-lb.conf +54 -0
  135. data/wakame_generators/resource/templates/apache_lb/conf/system-lb.conf +75 -0
  136. data/wakame_generators/resource/templates/apache_lb/init.d/apache2-lb +192 -0
  137. data/wakame_generators/resource/templates/apache_www/apache_www.rb +56 -0
  138. data/wakame_generators/resource/templates/apache_www/conf/apache2.conf +47 -0
  139. data/wakame_generators/resource/templates/apache_www/conf/envvars-www +7 -0
  140. data/wakame_generators/resource/templates/apache_www/conf/sites-www.conf +23 -0
  141. data/wakame_generators/resource/templates/apache_www/conf/system-www.conf +63 -0
  142. data/wakame_generators/resource/templates/apache_www/init.d/apache2-www +192 -0
  143. data/wakame_generators/resource/templates/ec2_elastic_ip/ec2_elastic_ip.rb +39 -0
  144. data/wakame_generators/resource/templates/mysql_master/conf/my.cnf +154 -0
  145. data/wakame_generators/resource/templates/mysql_master/init.d/mysql +185 -0
  146. data/wakame_generators/resource/templates/mysql_master/mysql_master.rb +119 -0
  147. metadata +289 -0
@@ -0,0 +1,218 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
4
+ $:.unshift(File.dirname(__FILE__))
5
+
6
+ require 'setup_agent.rb'
7
+
8
+ require 'rubygems'
9
+ require 'test/unit'
10
+
11
+ require 'wakame'
12
+ require 'wakame/agent'
13
+
14
+ class TestAgent < Test::Unit::TestCase
15
+
16
+ def setup
17
+ puts "Start #{@method_name}"
18
+ end
19
+
20
+ def teardown
21
+ puts "End #{@method_name}"
22
+ end
23
+
24
+ def test_agent_stop
25
+ 5.times {
26
+ EM.run{
27
+ Wakame::Agent.stop
28
+ }
29
+ }
30
+ 5.times {
31
+ EM.run{
32
+ Wakame::Agent.start
33
+ assert_equal(Wakame::Agent, Wakame::Agent.start.class) # Get cached instance returned
34
+ EM.add_timer(1) { Wakame::Agent.stop }
35
+ }
36
+ }
37
+ end
38
+
39
+ DUMMY_INSTANCE_ID='dummy_instance_id'
40
+ class DummyService < Wakame::Service::Property
41
+ attr_reader :check_count
42
+ def initialize(check_time=0.3)
43
+ super(check_time)
44
+ @check_count = 0
45
+ end
46
+
47
+ def start
48
+ @start_time ||= Time.now
49
+ #log.debug "/etc/init.d/apache start"
50
+ puts "/etc/init.d/apache start"
51
+ sleep 2
52
+ end
53
+
54
+ def check
55
+ return false if @start_time.nil?
56
+ @check_count += 1
57
+ #log.debug "checking... #{Time.now - @start_time} : #{Thread.current.inspect}"
58
+ puts "checking... #{Time.now - @start_time} : #{Thread.current.inspect}"
59
+
60
+ sleep 0.5
61
+
62
+ return Time.now - @start_time > 4.5 ? true : false
63
+ end
64
+
65
+ def stop
66
+ #log.debug "/etc/init.d/apache stop"
67
+ puts "/etc/init.d/apache stop"
68
+ @start_time = nil
69
+ sleep 2
70
+ end
71
+ end
72
+
73
+ ## This test dies when the test ran after another test.
74
+ # The test process stops at sleep() in DummyService#check(). The hang seems to be occured in EM's C backend though check() method runs in EM.defer thread which is Ruby thread.
75
+ def test_service_monitor
76
+ status_changed_flag=0
77
+ Wakame::EH.reset
78
+ EM.run {
79
+ monitor = Wakame::ServiceMonitor.new
80
+ Wakame::EH.subscribe(Wakame::Event::ServiceStatusChanged) { |event|
81
+ assert_equal(DUMMY_INSTANCE_ID, event.instance_id)
82
+ status_changed_flag=1
83
+ }
84
+ dummy = Wakame::ServiceRunner.new(DUMMY_INSTANCE_ID, DummyService.new(2))
85
+ monitor.register(dummy)
86
+
87
+ Wakame::EH.subscribe(Wakame::Event::ServiceOnline) { |event|
88
+
89
+ count = 0
90
+ monitor.monitors {|i|
91
+ count+=1
92
+ }
93
+
94
+ assert(dummy.property.check_count > 0)
95
+ assert_equal(1, count)
96
+ assert_equal(DUMMY_INSTANCE_ID, event.instance_id)
97
+ EM.next_tick {
98
+ EM.stop
99
+ }
100
+ }
101
+
102
+
103
+ EM.defer proc { dummy.start }
104
+ # Do not use EM.next_tick{ sleep 5 }. This holds EM's main thread up.
105
+ # Run DummyService#check() for 5 secs
106
+ }
107
+ end
108
+
109
+
110
+ def test_send_cmd
111
+ EM.run {
112
+ agent = Wakame::Agent.start
113
+ EM.next_tick {
114
+ agent.send_cmd(Wakame::Packets::Agent::Nop.new)
115
+ Wakame::Agent.stop
116
+ }
117
+ }
118
+ end
119
+
120
+
121
+ class DummyMaster
122
+ include Wakame::AMQPClient
123
+ include Wakame::QueueDeclare
124
+
125
+ define_queue 'registry', 'registry'
126
+ define_queue 'ping', 'ping'
127
+ define_queue 'agent_event', 'agent_event'
128
+
129
+ def initialize()
130
+ connect()
131
+ @counters = {'registry'=>0,'ping'=>0,'agent_event'=>0, }
132
+ add_subscriber('registry') { |data|
133
+ p data
134
+ @counters['registry'] += 1
135
+ }
136
+
137
+ add_subscriber('ping') { |data|
138
+ p data
139
+ @counters['ping'] += 1
140
+ }
141
+
142
+ add_subscriber('agent_event') { |data|
143
+ p data
144
+ @counters['agent_event'] += 1
145
+ }
146
+ end
147
+
148
+ def debug_counters
149
+ @counters
150
+ end
151
+
152
+
153
+ def send_actor_request(agent_id, path, *args)
154
+ publish_to('agent_command', "agent_id.#{agent_id}", Wakame::Packets::Agent::ActorRequest.new(agent_id, path, *args).marshal)
155
+ end
156
+
157
+
158
+ end
159
+
160
+ def test_agent_monitor
161
+ EM.run {
162
+ DummyMaster.start
163
+ agent = Wakame::Agent.start
164
+ EM.add_timer(5){ EM.stop }
165
+ }
166
+ end
167
+
168
+
169
+ def test_service_monitor
170
+ EM.run {
171
+ master = DummyMaster.start
172
+ agent = Wakame::Agent.start
173
+
174
+ svcmon = agent.find_monitor('/service')
175
+ svcmon.register('aaaaa', 'ls -l /usr')
176
+ svcmon.register('bbbbb', 'ls -l /dev')
177
+
178
+ assert(svcmon.checkers.keys.member?('aaaaa') && svcmon.checkers.keys.member?('bbbbb'))
179
+
180
+ EM.add_timer(8) {
181
+ svcmon.unregister('aaaaa')
182
+ svcmon.unregister('bbbbb')
183
+
184
+ svcmon.register('ccc', 'ls -l /var')
185
+
186
+ assert_equal(['ccc'], svcmon.checkers.keys)
187
+ }
188
+
189
+ EM.add_timer(15){
190
+ assert(master.debug_counters['agent_event'] > 2)
191
+ EM.stop
192
+ }
193
+ }
194
+ end
195
+
196
+
197
+ def test_actor
198
+ EM.run {
199
+ master = DummyMaster.start
200
+ agent = Wakame::Agent.start
201
+ EM.next_tick {
202
+ master.send_actor_request(agent.agent_id, '/service_monitor/register', ['12345', 'pidof ls'])
203
+ EM.add_timer(1) {
204
+ p agent.monitor_registry.find_monitor('/service').checkers.keys
205
+ assert_not_nil(agent.monitor_registry.find_monitor('/service').checkers['12345'])
206
+ EM.add_timer(4.5){
207
+ master.send_actor_request(agent.agent_id, '/service_monitor/unregister', ['12345'])
208
+ EM.add_timer(1) {
209
+ assert_nil(agent.monitor_registry.find_monitor('/service').checkers['12345'])
210
+ EM.stop
211
+ }
212
+ }
213
+ }
214
+ }
215
+ }
216
+ end
217
+
218
+ end
@@ -0,0 +1,94 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $:.unshift File.dirname(__FILE__) + '/../lib'
4
+ require 'rubygems'
5
+
6
+ require 'test/unit'
7
+ require 'eventmachine'
8
+ require 'wakame/amqp_client'
9
+
10
+ class TestAMQPClient < Test::Unit::TestCase
11
+
12
+ def setup
13
+ puts "Start #{@method_name}"
14
+
15
+
16
+ end
17
+
18
+ def teardown
19
+ puts "End #{@method_name}"
20
+ end
21
+
22
+
23
+ class TestClient
24
+ include Wakame::AMQPClient
25
+
26
+ define_exchange('ex1', :direct)
27
+ define_exchange('ex2')
28
+ define_exchange('ex3')
29
+
30
+ def initialize
31
+ super
32
+ connect
33
+
34
+ end
35
+ end
36
+
37
+
38
+ class TestClientRecv
39
+ include Wakame::AMQPClient
40
+
41
+ define_queue('recv1', 'ex1', {:exclusive=>true})
42
+ define_queue('recv2', 'ex2', {:exclusive=>true})
43
+
44
+ def initialize
45
+ super
46
+ connect
47
+
48
+ add_subscriber('recv1') { |data|
49
+ puts "From recv1 queue : #{data}"
50
+ }
51
+ add_subscriber('recv2') { |data|
52
+ puts "From recv2 queue : #{data}"
53
+ }
54
+ end
55
+ end
56
+
57
+
58
+ def test_stop
59
+ 5.times {
60
+ EM.run{
61
+ TestClient.stop
62
+ }
63
+ }
64
+ 5.times {
65
+ EM.run{
66
+ TestClient.start
67
+ assert_equal(TestClient, TestClient.start.class) # Get cached instance returned
68
+ EM.next_tick { TestClient.stop }
69
+ }
70
+ }
71
+ end
72
+
73
+
74
+ def test_publish_to
75
+ EM.run{
76
+ TestClientRecv.start
77
+ c = TestClient.start
78
+ EM.next_tick {
79
+ c.publish_to('ex2', 'aaa')
80
+
81
+ EM.add_periodic_timer(1) {
82
+ c.publish_to('ex1', 'test message')
83
+ }
84
+
85
+ EM.add_timer(10) {
86
+ TestClientRecv.stop
87
+ TestClient.stop
88
+ }
89
+ }
90
+ }
91
+ end
92
+
93
+ end
94
+
@@ -0,0 +1,36 @@
1
+
2
+ $:.unshift File.dirname(__FILE__) + '/../lib'
3
+ require 'rubygems'
4
+
5
+ require 'test/unit'
6
+ require 'wakame/graph'
7
+
8
+ WAKAME_ROOT="#{File.dirname(__FILE__)}/.."
9
+
10
+ class TestGraph < Test::Unit::TestCase
11
+ def test_graph1
12
+ g = Wakame::Graph.new
13
+ g.add_edge(0, 1) # LB
14
+ g.add_edge(0, 2) # APP
15
+ g.add_edge(0, 3) # WWW
16
+ g.add_edge(0, 4) # MySQL
17
+ g.add_edge(0, 5) # LB0
18
+ g.add_edge(0, 6) # MySQL_Slave
19
+
20
+ g.remove_edge(0, 1)
21
+ g.add_edge(2, 1) # APP -> LB
22
+ g.add_edge(3, 1) # WWW -> LB
23
+ g.remove_edge(0, 2)
24
+ g.add_edge(4, 2) # MySQL -> APP
25
+ g.remove_edge(0, 5)
26
+ g.add_edge(1, 5) # LB -> LB0
27
+ g.add_edge(6, 2) # MySQL_Slave -> APP
28
+ g.remove_edge(0, 4)
29
+ g.add_edge(6, 4) # MySQL_Slave -> MySQL
30
+
31
+
32
+ assert_equal([[3], [1], [5]], g.level_layout(3))
33
+ assert_equal([[0], [3, 6], [2], [3], [1], [5]], g.level_layout(0))
34
+
35
+ end
36
+ end
@@ -0,0 +1,167 @@
1
+ #!/usr/bin/ruby
2
+
3
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
4
+ $:.unshift(File.dirname(__FILE__))
5
+
6
+ require 'setup_master.rb'
7
+
8
+ require 'wakame'
9
+ #require 'wakame/master'
10
+ require 'wakame/service'
11
+ require 'wakame/queue_declare'
12
+ #require 'wakame/packets'
13
+
14
+ class TestMaster < Test::Unit::TestCase
15
+
16
+ def setup
17
+ puts "Start #{@method_name}"
18
+ end
19
+
20
+ def teardown
21
+ puts "End #{@method_name}"
22
+ end
23
+
24
+ def test_master_stop
25
+ 5.times {
26
+ EM.run{
27
+ Wakame::Master.stop
28
+ }
29
+ }
30
+ 5.times {
31
+ EM.run{
32
+ Wakame::Master.start
33
+ EM.add_timer(1) { Wakame::Master.stop }
34
+ }
35
+ }
36
+ end
37
+
38
+ def test_command_queue
39
+ end
40
+
41
+ def test_service_cluster
42
+ end
43
+
44
+
45
+ class MockAgent
46
+ def initialize
47
+ # Do nothing
48
+ end
49
+
50
+ def agent_id
51
+ 'asdfasdfasdfasdfasdf'
52
+ end
53
+ def agent_ip
54
+ '127.0.0.1'
55
+ end
56
+ end
57
+
58
+
59
+ class DummyResponder2
60
+ include Wakame::AMQPClient
61
+ include Wakame::QueueDeclare
62
+
63
+ AGENT_ID='__standalone__'
64
+
65
+ def self.create_register_hash()
66
+ {:agent_id=>AGENT_ID, :responded_at=>Time.now.to_s, :type=>Wakame::Packets::Register.to_s}
67
+ end
68
+ def self.create_unregister_hash()
69
+ {:agent_id=>AGENT_ID, :responded_at=>Time.now.to_s, :type=>Wakame::Packets::UnRegister.to_s}
70
+ end
71
+ def self.create_ping_hash()
72
+ {:agent_id=>AGENT_ID, :responded_at=>Time.now.to_s, :type=>Wakame::Packets::Ping.to_s, :attrs=>{}, :monitors=>{}, :actors=>{}, :services=>{}}
73
+ end
74
+
75
+ define_queue 'agent_actor.%{agent_id}', 'agent_command', {:key=>'agent_id.%{agent_id}', :auto_delete=>true}
76
+
77
+ def agent_id
78
+ AGENT_ID
79
+ end
80
+
81
+ def initialize()
82
+ connect
83
+
84
+ add_subscriber("agent_actor.#{agent_id}") { |data|
85
+ data = eval(data)
86
+ p data
87
+
88
+ if data[:path] == '/test/echo'
89
+ sleep 5
90
+ publish_to('agent_event', Wakame::Packets::ActorResponse.new(self, data[:token], Wakame::Actor::STATUS_SUCCESS).marshal)
91
+ end
92
+ }
93
+ end
94
+
95
+ def test_registered_agent
96
+ self.publish_to('registry', self.class.create_register_hash.inspect)
97
+ EM.add_periodic_timer(1) {
98
+ self.publish_to('ping', self.class.create_ping_hash.inspect)
99
+ }
100
+ end
101
+
102
+ def test_unregistered_agent
103
+ EM.add_periodic_timer(1) {
104
+ self.publish_to('ping', self.class.create_ping_hash.inspect)
105
+ }
106
+ end
107
+
108
+ end
109
+
110
+
111
+ def test_agent_monitor
112
+ flag_statchanged=false
113
+ flag_monitored=false
114
+
115
+ EM.run {
116
+ master = Wakame::Master.start
117
+ dr2 = DummyResponder2.start
118
+
119
+ dr2.test_registered_agent
120
+
121
+ EM.next_tick {
122
+ Wakame::ED.subscribe(Wakame::Event::AgentStatusChanged) { |event|
123
+ flag_statchanged = true
124
+ }
125
+ Wakame::ED.subscribe(Wakame::Event::AgentMonitored) { |event|
126
+ flag_monitored = true
127
+ }
128
+
129
+ Wakame::ED.subscribe(Wakame::Event::AgentPong) { |event|
130
+ puts "#{event.class.to_s} has been received from #{event.agent.agent_id}"
131
+ assert_equal(1, master.agent_monitor.registered_agents.size)
132
+ assert_equal(0, master.agent_monitor.unregistered_agents.size)
133
+
134
+ EM.add_timer(1) {
135
+ Wakame::Master.stop
136
+ DummyResponder2.stop
137
+ }
138
+ }
139
+ }
140
+
141
+ }
142
+
143
+ assert(flag_monitored)
144
+ assert(flag_statchanged)
145
+ end
146
+
147
+
148
+
149
+ def test_actor_request
150
+ EM.run {
151
+ master = Wakame::Master.start
152
+ dr2 = DummyResponder2.start
153
+
154
+ dr2.test_registered_agent
155
+
156
+ EM.next_tick {
157
+ req = master.actor_request('__standalone__', '/test/echo', 'a', 'b', 'c').request
158
+ EM.defer proc {
159
+ req.wait_completion
160
+ }, proc {
161
+ EM.stop
162
+ }
163
+ }
164
+ }
165
+ end
166
+
167
+ end
@@ -0,0 +1,47 @@
1
+
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
3
+ $:.unshift(File.dirname(__FILE__))
4
+
5
+ require 'setup_agent.rb'
6
+
7
+ require 'rubygems'
8
+
9
+ require 'test/unit'
10
+
11
+ require 'wakame'
12
+ require 'wakame/packets'
13
+ require 'wakame/monitor'
14
+ require 'wakame/monitor/agent'
15
+ require 'wakame/monitor/service'
16
+
17
+ class TestMonitor < Test::Unit::TestCase
18
+ include Wakame
19
+
20
+ def test_agent_monitor
21
+ EM.run {
22
+ agent = DummyAgent.new
23
+ a = Monitor::Agent.new(agent)
24
+ a.setup
25
+ a.check
26
+
27
+ EM.add_timer(21) {
28
+ assert_equal(2, agent.publish_count)
29
+ EM.stop
30
+ }
31
+ }
32
+ end
33
+
34
+ def test_checker_timer
35
+ EM.run {
36
+ count = 0
37
+ timer = Monitor::CheckerTimer.new(1) {
38
+ count += 1
39
+ puts "a"
40
+ }
41
+
42
+ timer.start
43
+ EM.add_timer(5) { puts count; assert((4..5).include?(count) ); EM.stop}
44
+ }
45
+ end
46
+
47
+ end
@@ -0,0 +1,127 @@
1
+
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
3
+ $:.unshift(File.dirname(__FILE__))
4
+
5
+ require 'setup_master.rb'
6
+
7
+ require 'rubygems'
8
+ require 'eventmachine'
9
+
10
+ require 'test/unit'
11
+ require 'wakame'
12
+ require 'wakame/rule.rb'
13
+
14
+ WAKAME_ROOT="#{File.dirname(__FILE__)}/.."
15
+
16
+ Wakame::EventDispatcher
17
+
18
+ class TestRuleEngine < Test::Unit::TestCase
19
+ include Wakame::Rule
20
+
21
+ class Action1 < Action
22
+ def run
23
+ trigger_action(Action2.new)
24
+ flush_subactions
25
+ end
26
+ end
27
+ class Action2 < Action
28
+ def run
29
+ act3 = Action3.new
30
+ trigger_action(act3)
31
+ flush_subactions
32
+ end
33
+ end
34
+ class Action3 < Action
35
+ def run
36
+ puts "sleeping(2)..."
37
+ sleep 2
38
+ end
39
+ end
40
+
41
+ class Rule1 < Rule
42
+ def register_hooks
43
+ trigger_action(Action1.new)
44
+ end
45
+ end
46
+
47
+ class DummyMaster
48
+ end
49
+
50
+ def test_nested_actions
51
+ EM.run {
52
+ engine = RuleEngine.new(Wakame::Service::ServiceCluster.new(Object.new))
53
+ engine.register_rule(Rule1.new)
54
+ EM.add_timer(5) { EM.stop }
55
+ }
56
+ end
57
+
58
+ def test_each_subaction
59
+ EM.run {
60
+ engine = RuleEngine.new(Wakame::Service::ServiceCluster.new(Object.new))
61
+ engine.register_rule(Rule1.new)
62
+ EM.add_timer(1) {
63
+ engine.active_jobs.each { |k, v|
64
+ v[:root_action].walk_subactions {|a|
65
+ puts a
66
+ }
67
+ }
68
+
69
+ }
70
+ EM.add_timer(5) { EM.stop }
71
+ }
72
+ end
73
+
74
+
75
+ class Rule2 < Rule
76
+ def register_hooks
77
+ act1 = Action1.new
78
+ job_id = trigger_action(act1)
79
+ puts job_id
80
+
81
+ EM.add_timer(1){
82
+ rule_engine.cancel_action(job_id)
83
+ }
84
+ end
85
+ end
86
+
87
+ def test_cancel_action
88
+ EM.run {
89
+ engine = RuleEngine.new(Wakame::Service::ServiceCluster.new(Object.new))
90
+ engine.register_rule(Rule2.new)
91
+ EM.add_timer(5) { EM.stop }
92
+ }
93
+ end
94
+
95
+
96
+ class Action4 < Action
97
+ def run
98
+ trigger_action(Action1.new)
99
+
100
+ trigger_action(FailAction1.new)
101
+
102
+ flush_subactions
103
+ end
104
+ end
105
+
106
+ class FailAction1 < Action
107
+ def run
108
+ trigger_action(Action1.new)
109
+ raise StandardError
110
+ end
111
+ end
112
+
113
+ class Rule3 < Rule
114
+ def register_hooks
115
+ trigger_action(Action4.new)
116
+ end
117
+ end
118
+
119
+ def test_cancel_escalation
120
+ EM.run {
121
+ engine = RuleEngine.new(Wakame::Service::ServiceCluster.new(Object.new))
122
+ engine.register_rule(Rule3.new)
123
+ EM.add_timer(10) { EM.stop }
124
+ }
125
+ end
126
+
127
+ end