Class: Nugget::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/nugget/service.rb

Class Method Summary collapse

Class Method Details

.config_converter(definition) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nugget/service.rb', line 76

def self.config_converter(definition)
  options = definition[:request]

  if options[:method]
    sym_method = options[:method].to_sym
    options.store(:method, sym_method)
  end

  if options[:url]
    url = options[:url]
    options.delete(:url)
  end

  {
    :url => url,
    :type => definition[:type],
    :options => options
  }
end

.run(test_name = 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
# File 'lib/nugget/service.rb', line 20

def self.run(test_name = nil)
  config_file = open(Nugget::Config.config)
  parser = Yajl::Parser.new(:symbolize_keys => true)
  config = parser.parse(config_file)

  results = Hash.new

  if test_name
    if definition = config[test_name.to_s.to_sym]
      run_test(results, test_name, definition)
    else
      raise "No test name #{test_name.inspect} found."
    end
  else
    config.each do |test, definition|
      run_test(results, test, definition)
    end
  end

  if Nugget::Config.daemon
    Nugget::Service.write_results(results)
  end
end

.run_daemon(test_name = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/nugget/service.rb', line 4

def self.run_daemon(test_name = nil)
  Nugget::Log.info("Starting up Nugget in daemon mode ...")

  loop do
    run(test_name)

    # chill
    sleep(Nugget::Config.interval.to_i)
  end
end

.run_once(test_name = nil) ⇒ Object



15
16
17
18
# File 'lib/nugget/service.rb', line 15

def self.run_once(test_name = nil)
  Nugget::Log.info("Starting up Nugget in run-once mode ...")
  run(test_name)
end

.run_test(results, test, definition) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nugget/service.rb', line 44

def self.run_test(results, test, definition)
  result = nil
  response = nil

  begin
    request_definition = config_converter(definition)
    response_definition = definition[:response]

    response = Turd.run(request_definition, response_definition)
    result = "PASS"
  rescue Exception => e
    Nugget::Log.error("#{definition[:type]} test #{test} failed due to #{e.response[:failed]}!")
    Nugget::Log.error(e)

    result = "FAIL"
    response = e.response
  end

  Nugget::Log.info("Test #{test} complete with status #{result}")

  results.store(test, {
    :config => request_definition,
    :result => result,
    :response => response,
    :timestamp => Time.now.to_i
  })

  if Nugget::Config.backstop_url
    Nugget::Backstop.send_metrics(test, result, response)
  end
end

.write_results(results) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/nugget/service.rb', line 96

def self.write_results(results)
  begin
    file = File.open(Nugget::Config.resultsfile, "w")
    file.puts(results.to_json)
    file.close
  rescue Exception => e
    Nugget::Log.error("Something went wrong with writing out the results file!")
    Nugget::Log.error(e)
  end
end