Module: ReportMonitor

Included in:
BaseMonitor, MonitoringTest
Defined in:
lib/test_case/monitoring/report_monitor.rb

Overview

All this module should do is interface with the SWORD app and includes essential components for the final reports. Will mutate the hash while the test runs.

Instance Method Summary collapse

Instance Method Details

#assign_email_params?(results_hash, legacy) ⇒ Boolean

Public: Assign the parameters for the email. Will return a boolean if it’s needed. Mutates hash instance inside method.

results_hash - Hash including all results from test so far. err_code - String of the error result found. (default: nil)

Returns boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/test_case/monitoring/report_monitor.rb', line 87

def assign_email_params?(results_hash, legacy)
  log_hash('email params 1 ', results_hash)
  # Return out if no err_code sent
  if results_hash.fetch(:result, "").downcase == 'pass'
    results_hash[:email] = 'false'
    return false
  end

  if !@test_case.nil?
    results_hash[:recipient] = @test_case.test_data['recipient']
    results_hash[:recipient] = "   "     #for jim until we set recipients or test_data
  else
    results_hash[:recipient] = test_data['recipient']
  end

  results_hash[:subject] = results_hash.fetch(:subject, "Health Check results on #{server_info['hostname']}")
  results_hash[:recipient] = results_hash.fetch(:recipient, " ") # pass comma seperated string for default ('person1@charter, person2@charter')
  results_hash[:email] = 'true'
  results_hash[:message] = generate_email_msg(results_hash, legacy ? @dut_id : nil) unless results_hash[:message]
  log_hash('email params 2 ', results_hash)
  return true
end

#assign_test_params(results_hash) ⇒ Object

Public: All sword reports need these common fields no matter what. Mutates the health check hash. Called at beginning of test script

results_hash - Hash including all results from test so far.

returns nothing.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/test_case/monitoring/report_monitor.rb', line 69

def assign_test_params(results_hash)
  log_hash('beginning ', results_hash)
  results_hash[:tmc_name] = server_info['hostname']
  results_hash[:tmc_run_id] = job
  results_hash[:tmc_start_time] = job_start_time
  results_hash[:tmc_script_name] = name
  results_hash[:slot] = "Slot #{dut.id}"
  results_hash[:username] = tmc_get('/api/login')['name']
  log_hash('assign_test_params ', results_hash)
  results_hash
end

#confirm_platform(config) ⇒ Object

Should return string with information as to failure if legacy platform.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/test_case/monitoring/report_monitor.rb', line 24

def confirm_platform(config)
  # maybe when we confirm platofrm, just let it blow up
  # if it doesn't include any of the platforms we have created?
  # (found a platform called 'adsg,' lol)
  platform = config['platform']
  name = config['name']
  legacy_plats = %w[hd dvr]
  return 'Legacy platform please update' if platform == '' || platform.nil? # if no platform at all.
  legacy = legacy_plats.select { |el| platform.downcase.include?(el) }
  return 'Legacy platform please update' unless legacy.empty? # give legacy error      
end

#generate_email_msg(hash, id = nil) ⇒ Object

Should only get called if email result is true. Adds email message to result hash.

returns nothing



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/test_case/monitoring/report_monitor.rb', line 113

def generate_email_msg(hash, id=nil)
  slot_num = nil
  platform = nil
  screenshot = nil
  if id
    slot_num = id
    platform = hash[:result]
  else
    slot_num = dut.id
    platform = dut.platform
    screenshot = hash[:vid_link]
  end
  msg = ''
  msg << "Slot #{slot_num}, Error_Code: #{hash[:error_code]}, Result: #{hash[:result]}"
  msg << ", Platform: #{platform}, Screenshot: #{screenshot}" unless id
  msg
end

#invalid_reportObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/test_case/monitoring/report_monitor.rb', line 12

def invalid_report
  @dut_id = instance_variable_get(:@primary_device_id)
  resp = tmc_get("/api/config/hardware/devices?devices=#{@dut_id}")
  @cfg = resp['devices'].first
  res = confirm_platform(@cfg)
  logger.warn('past confirm platform')
  log_hash('invalid report', res)
  send_legacy_err_hash(res, "Legacy")
  "Legacy: #{res}"
end

#log_hash(*msg, hash) ⇒ Object

TODO delete this after test development



6
7
8
9
10
# File 'lib/test_case/monitoring/report_monitor.rb', line 6

def log_hash(*msg, hash)
  logger.warn("#{msg[0]}: #{hash}", tag: "HASH")
  logger.warn("hash id is #{hash.object_id} ")
  logger.warn("hash id is #{hash}")
end

#send_legacy_err_hash(result, err_code) ⇒ Object

This function grabs what we need w/o calling dut, which hasn’t been initialized yet.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/test_case/monitoring/report_monitor.rb', line 37

def send_legacy_err_hash(result, err_code)
  res_hash = {}
  res_hash[:tmc_name] = server_info['hostname']
  res_hash[:tmc_start_time] = Time.now
  res_hash[:tmc_run_id] = job_name
  res_hash[:error_code] = err_code
  res_hash[:slot] = @dut_id
  res_hash[:result] = result
  res_hash[:tmc_script_name] = name
  res_hash[:script_error_time] = Time.now + 500
  res_hash[:username] = tmc_get('/api/login')['name']
  sword_send_results(res_hash, true)
end

#set_err_result(results_hash, err_msg, err_code) ⇒ Object

Public Function used to determine throughout checks if there is a fail.

Returns nothing.



53
54
55
56
57
58
59
60
61
62
# File 'lib/test_case/monitoring/report_monitor.rb', line 53

def set_err_result(results_hash, err_msg, err_code)
  logger.warn("#{results_hash}")
  results_hash[:result] = err_msg
  results_hash[:error_code] = err_code
  results_hash[:script_error_time] = Time.now
  log_hash("set err ", results_hash)
  results_hash[:vid_link] = take_screenshot
  sword_send_results(results_hash)
  @test_case.fail "Device is not healthy: #{err_code} : #{err_msg}"
end

#sword_send_results(result_hash, legacy = false) ⇒ Object

Returns nothing.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/test_case/monitoring/report_monitor.rb', line 149

def sword_send_results(result_hash, legacy = false)
  logger.warn('sword send')
  url = nil
  # If true, send email. Else endpoint just adds to SWORD db.
  if assign_email_params?(result_hash, legacy)
    url = "https://sword-prod.enwd.co.sa.charterlab.com:443/automation_units/send_email_for_rack?"
  else
    url = "https://sword-prod.enwd.co.sa.charterlab.com:443/automation_units/tmc_stb_healthcheck?"
  end
  result_hash[:script_error_time] = Time.now
  log_hash('sword send results ', result_hash) # Remove once testing is complete TODO
  # new hash with SWORD friendly params. We could just standardize the keys as well
  # then we won't need a new hash.
  params = {
    tmc_name: result_hash[:tmc_name], slot: result_hash[:slot], tmc_run_id: result_hash[:tmc_run_id],
    tmc_start_time: result_hash[:tmc_start_time], tmc_script_name: result_hash[:tmc_script_name],
    email_to_list: result_hash[:recipient], send_email_tf: result_hash[:email], message_to_send: result_hash[:message],
    subject_line: result_hash[:subject], link_to_video: result_hash[:vid_link], script_error_time: result_hash[:script_error_time],
    mac_address: result_hash[:mac], resolution: result_hash[:resolution], model: result_hash[:model], serial_number: result_hash[:serial_number],
    device_id: result_hash[:device_id], account: result_hash[:account], screenshot_1: result_hash[:screenshot_1], screenshot_2: result_hash[:screenshot_2],
    ip: result_hash[:ip], username: result_hash[:username], result: result_hash[:result], error_code: result_hash[:error_code]
  }
  log_hash('final params ', params)
  url = url.concat(params.collect { |k,v| "#{k}=#{URI::escape(v.to_s)}" }.join('&'))
  logger.debug("Reporting URL: #{url}")
  # response = Net::HTTP.get_response(URI.parse(url))
  response = web_request(:GET, url)
  logger.debug("---#{response.body}--")
end

#take_screenshotObject



131
132
133
134
135
136
# File 'lib/test_case/monitoring/report_monitor.rb', line 131

def take_screenshot
  ss = dut.capture_screen(format: :raw)
  aws_write_s3(
    'charter-testworkbench-com',
    "health_check/#{server_name.gsub(' ', '')}/#{dut.id}/#{dut.platform}",ss)
end