Class: Res::Reporters::Lion

Inherits:
Object
  • Object
show all
Defined in:
lib/res/reporters/lion.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Lion

Returns a new instance of Lion.



9
10
11
12
13
14
15
16
17
18
# File 'lib/res/reporters/lion.rb', line 9

def initialize(args)
  @url = args[:url]
  # :device_type is optional for backward compatibility
  # At the next major version update this should be moved into the
  # required list.
  @config = Res::Config.new([:url, :tag, :description, :app_name, :target],
                            :optional => [:hive_job_id, :queue, :cert, :cacert, :ssl_verify_mode, :device_type, :version],
                            :pre_env  => 'LION_')
  config.process(args)
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/res/reporters/lion.rb', line 7

def config
  @config
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/res/reporters/lion.rb', line 7

def url
  @url
end

Instance Method Details

#submit_results(ir, args = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/res/reporters/lion.rb', line 20

def submit_results(ir, args = nil)
  status = "failed"
  status = "passed" if ir.tests.count == ir.count(:passed)
  results = Hash[ir.tests.map { |t| [t[:name], t[:status]] }]

  if ir.values.has_key?('#type') && ir.values['#type'] == 'multiple'
    measures = {}
    ir.values.select { |k, v| k !~ /^[#_]/ }.each do |k, v|
      measures[k] = { :count => v, :total => ir.values['_total'] }
    end
  else
    measures = ir.values
  end

  # Set Lion Data
  lion_data = {
    :app_name => config.app_name,
    :app_version => config.version || 'Unknown',
    :hive_job_id => config.hive_job_id || 0,
    :tag => config.tag,
    :device_type => config.device_type || 'Unknown',
    :description => config.description,
    :queue_name => config.target,
    :type => ir.type,
    :started => ir.results.first[:started],
    :finished => ir.results.last[:finished],
    :status => status,
    :results => results,
    :measures => measures
  }
  
  uri = URI.parse(config.url)
  @http = Net::HTTP.new(uri.host, uri.port)
  if config.cert
    pem = File.read(config.cert)
    @http.use_ssl = true if uri.scheme == 'https'
    @http.cert = OpenSSL::X509::Certificate.new(pem)
    @http.key = OpenSSL::PKey::RSA.new(pem)
    @http.ca_file = config.cacert if config.cacert
    @http.verify_mode = config.ssl_verify_mode if config.ssl_verify_mode
  end
  
  request = Net::HTTP::Post.new(config.url + "/import")
  request.content_type = 'application/json'
  request.body = lion_data.to_json

  @http.request(request)
end