Permalink
Browse files

Documenting EC test script

  • Loading branch information...
1 parent ba2f343 commit 04210307b18781ff89f87958d81283eabc4ffba7 Jack C Hong committed May 24, 2012
Showing with 135 additions and 107 deletions.
  1. +0 −107 omf_ec/bin/omf
  2. +135 −0 omf_ec/bin/omf_test
View
107 omf_ec/bin/omf
@@ -1,107 +0,0 @@
-#!/usr/bin/env ruby
-
-require "optparse"
-require 'omf_common'
-$stdout.sync = true
-
-options = {
- dsl: 'xmpp_blather',
- pubsub_host: 'pubsub',
- node: 'interlagos'
-}
-
-executable_name = File.basename($PROGRAM_NAME)
-
-option_parser = OptionParser.new do |opts|
- opts.banner = "usage: #{executable_name} [options]"
-
- opts.on("-u USER", "Username") do |user|
- options[:user] = user
- end
-
- opts.on("-p PASSWORD", "Password") do |password|
- options[:password] = password
- end
-
- opts.on("-s SERVER", "XMPP server") do |server|
- options[:server] = server
- end
-
- opts.on("-d", "--debug", "Debug mode") do
- options[:debug] = true
- end
-end
-
-begin
- option_parser.parse!
-rescue => e
- puts e.message
- puts ""
- puts option_parser.help
- exit(1)
-end
-
-unless options[:server] && options[:user] && options[:server]
- puts option_parser.help
- exit(1)
-end
-
-Blather.logger = logger
-Blather.logger.level = Logger::DEBUG if options[:debug]
-
-comm = OmfCommon::Comm.new(options[:dsl])
-host = nil
-
-create_a_mock = OmfCommon::Message.create { |v| v.property('type', 'mock') }.sign
-request_mock_property = OmfCommon::Message.request { |v| v.property('cpu_model') }.sign
-
-comm.when_ready do
- logger.info "CONNECTED: #{comm.jid.inspect}"
- host = "#{options[:pubsub_host]}.#{comm.jid.domain}"
-
- #EM.add_periodic_timer(1) do
- comm.subscribe(options[:node], host) do
- comm.publish(options[:node], create_a_mock, host)
- end
- #end
-end
-
-comm.node_item_event do |e|
- e.items.each do |item|
- m = OmfCommon::Message.parse(item.payload)
- logger.error "Invalid Message\n#{m.to_xml}" unless m.valid?
- context_id = m.read_element("//context_id").first.content
- #logger.info "RECEIVED: #{m.operation.to_s} <Context ID> #{context_id}"
-
- begin
- case m.operation
- when :inform
- inform_type = m.read_element("//inform_type").first.content
- case inform_type
- when 'CREATED'
- resource_id = m.read_element("//resource_id").first.content
- logger.info "Resource #{resource_id} CREATED"
- comm.subscribe(resource_id, host) do
- EM.add_periodic_timer(2) do
- comm.publish(resource_id, request_mock_property, host)
- end
- end
- when 'STATUS'
- logger.info m.read_element("//property")
- end
- end
- rescue => e
- logger.error "#{e.message}\n#{e.backtrace.join("\n")}"
- end
- end
-end
-
-# Generic pubsub event
-comm.pubsub_event do |e|
- logger.debug "PUBSUB GENERIC EVENT: #{e}"
-end
-
-trap(:INT) { comm.disconnect(host) }
-trap(:TERM) { comm.disconnect(host) }
-
-EM.run { comm.connect(options[:user], options[:password], options[:server]) }
View
135 omf_ec/bin/omf_test
@@ -0,0 +1,135 @@
+#!/usr/bin/env ruby
+
+require "optparse"
+require 'omf_common'
+$stdout.sync = true
+
+options = {
+ dsl: 'xmpp_blather',
+ pubsub_host: 'pubsub',
+ node: 'interlagos'
+}
+
+executable_name = File.basename($PROGRAM_NAME)
+
+option_parser = OptionParser.new do |opts|
+ opts.banner = "usage: #{executable_name} [options]"
+
+ opts.on("-u USER", "Username") do |user|
+ options[:user] = user
+ end
+
+ opts.on("-p PASSWORD", "Password") do |password|
+ options[:password] = password
+ end
+
+ opts.on("-s SERVER", "XMPP server") do |server|
+ options[:server] = server
+ end
+
+ opts.on("-d", "--debug", "Debug mode") do
+ options[:debug] = true
+ end
+end
+
+begin
+ option_parser.parse!
+rescue => e
+ puts e.message
+ puts ""
+ puts option_parser.help
+ exit(1)
+end
+
+unless options[:server] && options[:user] && options[:server]
+ puts option_parser.help
+ exit(1)
+end
+
+Logging.logger.root.level = :debug if options[:debug]
+Blather.logger = logger
+
+comm = OmfCommon::Comm.new(options[:dsl])
+host = nil
+
+# Create a resource of type mock
+create_msg = OmfCommon::Message.create { |v| v.property('type', 'mock') }.sign
+
+# A request message to be sent to mock
+request_mock_property = OmfCommon::Message.request do |v|
+ v.property('kernel_version')
+ v.property('resource_proxy_list')
+ v.property('resource_utility_list')
+end.sign
+
+# A configure message to be sent to mock
+configure_mock_property = OmfCommon::Message.configure do |v|
+ v.property('bob', 'test')
+end.sign
+
+# Simple release message
+release_message = OmfCommon::Message.release.sign
+
+# For simplicity, use comm instance directly
+comm.when_ready do
+ logger.info "CONNECTED: #{comm.jid.inspect}"
+ host = "#{options[:pubsub_host]}.#{comm.jid.domain}"
+
+ # We assume the node where RC runs started already
+ comm.subscribe(options[:node], host) do |e|
+ if e.error?
+ comm.disconnect(host)
+ else
+ # Publish the create message to RC's pubsub node
+ comm.publish(options[:node], create_msg, host)
+ end
+ end
+end
+
+# Fired when messages published to the nodes I subscribed to
+comm.node_event do |e|
+ e.items.each do |item|
+ begin
+ # Parse the message (pubsub item payload)
+ message = OmfCommon::Message.parse(item.payload)
+ context_id = message.read_content("context_id")
+
+ # We are only interested in inform messages for the moment
+ if message.operation == :inform
+ inform_type = message.read_content("inform_type")
+
+ case inform_type
+ when 'CREATED'
+ resource_id = message.read_content("resource_id")
+ logger.info "Resource #{resource_id} created"
+
+ comm.subscribe(resource_id, host) do |m|
+ EM.add_periodic_timer(2) do
+ comm.publish(resource_id, request_mock_property, host)
+ comm.publish(resource_id, configure_mock_property, host)
+ end
+ end
+
+ EM.add_timer(5) do
+ comm.publish(resource_id, release_message, host)
+ end
+ when 'STATUS'
+ message.read_element("//property").each do |p|
+ logger.info "#{p.attr('key')} => #{p.content.strip}"
+ end
+ when 'RELEASED'
+ logger.info "Resource #{message.read_content("resource_id")} released"
+ when 'FAILED'
+ logger.error message.read_content("error_message")
+ end
+ end
+ rescue => e
+ logger.error "#{e.message}\n#{e.backtrace.join("\n")}"
+ end
+ end
+end
+
+trap(:INT) { comm.disconnect(host) }
+trap(:TERM) { comm.disconnect(host) }
+
+EM.run { comm.connect(options[:user], options[:password], options[:server]) }

0 comments on commit 0421030

Please sign in to comment.