Class: Mutx::Results::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/mutx/results/result.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_for_result) ⇒ Result



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
68
69
70
71
# File 'lib/mutx/results/result.rb', line 37

def initialize data_for_result
  Mutx::Support::Log.debug "Result.new => data_for_result => #{data_for_result}" if Mutx::Support::Log
  if data_for_result["_id"]
    # It comes from mongo because there is a result id
    @id                       = data_for_result["_id"]
    load_values(data_for_result)

  else # It comes from a new execution request
    @id = Mutx::Database::MongoConnector.generate_id
    @task                     = data_for_result['task']
    @execution_name           = data_for_result["execution_name"] || ""
    @custom_params            = data_for_result["custom_params"]
    @git_log                  = data_for_result["git_log"]
    @started_message          = data_for_result["started_message"]
    @started_at               = now_in_seconds
    @finished_at              = nil
    @status = @summary        = "started"
    @timeout                  = nil
    @show_as                  = "pending"
    @html_report              = ""
    @results_details          = {"duration" => 0, "passed"=>[], "failed" =>[], "undefined" => [], "unknown"=>[], "skipped" => [], "pending" => []}

    # Save task info
    task_data                 = Mutx::Database::MongoConnector.task_data_for(@task["id"])
    @command                  = data_for_result['command']
    @task_name                = task_data["name"]
    @command                  = task_data["command"]
    @application              = task_data["application"]
    @console_output           = ""
    @last_check_time          = now_in_seconds
    @execution_data           = {}
    @configuration_values     = Mutx::Database::MongoConnector.configuration
    create_output_dir
  end
end

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def application
  @application
end

#commandObject

Returns the value of attribute command.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def command
  @command
end

#configuration_valuesObject

Returns the value of attribute configuration_values.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def configuration_values
  @configuration_values
end

#console_outputObject

Returns the value of attribute console_output.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def console_output
  @console_output
end

#console_output_file_nameObject

Returns the value of attribute console_output_file_name.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def console_output_file_name
  @console_output_file_name
end

#custom_paramsObject

Returns the value of attribute custom_params.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def custom_params
  @custom_params
end

#execution_dataObject

Returns the value of attribute execution_data.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def execution_data
  @execution_data
end

#execution_nameObject

Returns the value of attribute execution_name.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def execution_name
  @execution_name
end

#finished_atObject

Returns the value of attribute finished_at.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def finished_at
  @finished_at
end

#git_logObject

Returns the value of attribute git_log.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def git_log
  @git_log
end

#html_reportObject

Returns the value of attribute html_report.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def html_report
  @html_report
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def id
  @id
end

#last_check_timeObject

Returns the value of attribute last_check_time.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def last_check_time
  @last_check_time
end

#mutx_commandObject

Returns the value of attribute mutx_command.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def mutx_command
  @mutx_command
end

#mutx_report_file_nameObject

Returns the value of attribute mutx_report_file_name.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def mutx_report_file_name
  @mutx_report_file_name
end

#pidObject

Returns the value of attribute pid.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def pid
  @pid
end

#results_detailsObject

Returns the value of attribute results_details.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def results_details
  @results_details
end

#sawObject

Returns the value of attribute saw.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def saw
  @saw
end

#show_asObject

Returns the value of attribute show_as.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def show_as
  @show_as
end

#started_atObject

Returns the value of attribute started_at.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def started_at
  @started_at
end

#started_messageObject

Returns the value of attribute started_message.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def started_message
  @started_message
end

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def status
  @status
end

#summaryObject

Returns the value of attribute summary.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def summary
  @summary
end

#taskObject

Returns the value of attribute task.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def task
  @task
end

#task_nameObject

Returns the value of attribute task_name.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def task_name
  @task_name
end

#timeoutObject

Returns the value of attribute timeout.



7
8
9
# File 'lib/mutx/results/result.rb', line 7

def timeout
  @timeout
end

Class Method Details

.get(result_id) ⇒ Object



81
82
83
84
# File 'lib/mutx/results/result.rb', line 81

def self.get(result_id)
  result_data = Mutx::Database::MongoConnector.result_data_for_id(result_id)
  new(result_data) if result_data
end

Instance Method Details

#add_execution_data(key, value) ⇒ Object



158
159
160
161
# File 'lib/mutx/results/result.rb', line 158

def add_execution_data key, value
  @execution_data.store(key, value)
  self.save!
end

#add_scenario_result(details) ⇒ Object



178
179
180
# File 'lib/mutx/results/result.rb', line 178

def add_scenario_result details
  self.add_test_result details
end

#add_test_result(details) ⇒ Object

details =

"name" => scenario.name,
"status" => self.get_status(scenario),
"location" => "#{scenario.location.file:#Mutx::Results::Result.scenarioscenario.locationscenario.location.line"}


168
169
170
171
172
173
174
175
176
# File 'lib/mutx/results/result.rb', line 168

def add_test_result details
  return false unless @results_details.keys.include? details["status"]
  begin
    self.send("@results_details[#{details['status']}]<<#{details}") and self.save!
    true
  rescue
    false
  end
end

#api_responseObject



128
129
130
131
132
133
134
135
# File 'lib/mutx/results/result.rb', line 128

def api_response
  data = result_data_structure
  data["has_report"] = self.has_report?
  data["elapsed_time"] = self.elapsed_time
  # List of fields to omit in api response
  ["html_report","console_output_file_name","mutx_command","mutx_report_file_name","pid","last_check_time","console_output","git_log"].each{|field| data.delete(field)}
  data
end

#append_output(output) ⇒ Object



442
443
444
445
446
# File 'lib/mutx/results/result.rb', line 442

def append_output output
  @console_output += output
  mark_last_check_time
  self.save!
end

#check_if_finished!Object

def check_progress!

if is_running?
  collect_console_output!
  if reached_timeout?
    finished_by_timeout!
  elsif is_process_dead?
    finish!
  end
else
  collect_console_output!
end

end



246
247
248
249
250
251
252
253
254
255
256
# File 'lib/mutx/results/result.rb', line 246

def check_if_finished!
  if is_running?
    if reached_timeout?
      finished_by_timeout!
    elsif is_process_dead?
      finish!
    end
  else
    ensure_finished!
  end
end

#console_output_contentObject



384
385
386
387
388
389
390
391
392
393
# File 'lib/mutx/results/result.rb', line 384

def console_output_content
  begin
    FileUtils.cp("#{Dir.pwd}/mutx/temp/#{console_output_file_name}", "#{Dir.pwd}/mutx/temp/#{console_output_file_name}~")
    file_content = File.open "#{Dir.pwd}/mutx/temp/#{console_output_file_name}~", "r"
    File.delete("#{Dir.pwd}/mutx/temp/#{console_output_file_name}~")
  rescue Errno::ENOENT
    false
  end
  file_content
end

#console_output_file_textObject



372
373
374
375
376
377
378
# File 'lib/mutx/results/result.rb', line 372

def console_output_file_text
  text = ""
  console_output_content.each_line do |line|
    text += line + "\n"
  end
  text
end

#create_output_dirObject



227
228
229
# File 'lib/mutx/results/result.rb', line 227

def create_output_dir
  Dir.mkdir "#{Dir.pwd}/mutx/out/#{@id}"
end

#custom_params_valuesString

Returns the string of custom params values



139
140
141
# File 'lib/mutx/results/result.rb', line 139

def custom_params_values
  "mutx_custom_params='#{validate_params(@custom_params).to_json}'".gsub(',', ', ')
end

#delete_asociated_files!Object



537
538
539
540
541
# File 'lib/mutx/results/result.rb', line 537

def delete_asociated_files!
  delete_console_output_file!
  delete_mutx_report_file!
  delete_copy_mutx_report_file!
end

#delete_console_output_file!Object



508
509
510
511
512
513
514
515
516
# File 'lib/mutx/results/result.rb', line 508

def delete_console_output_file!
  begin
    File.delete("#{Dir.pwd}/mutx/temp/#{console_output_file_name}")
    Mutx::Support::Log.debug "[#{@id}] Console output files deleted" if Mutx::Support::Log
    true
  rescue => e
    false
  end
end

#delete_copy_mutx_report_file!Object



528
529
530
531
532
533
534
535
# File 'lib/mutx/results/result.rb', line 528

def delete_copy_mutx_report_file!
  begin
    File.delete("#{Dir.pwd}/mutx/temp/#{mutx_report_file_name}~")
    true
  rescue => e
    false
  end
end

#delete_mutx_report_file!Object



518
519
520
521
522
523
524
525
526
# File 'lib/mutx/results/result.rb', line 518

def delete_mutx_report_file!
  begin
    File.delete("#{Dir.pwd}/mutx/temp/#{mutx_report_file_name}")
    Mutx::Support::Log.debug "[#{@id}] Report files deleted" if Mutx::Support::Log
    true
  rescue => e
    false
  end
end

#elapsed_timeObject



549
550
551
# File 'lib/mutx/results/result.rb', line 549

def elapsed_time
  (finished_at || now_in_seconds) - started_at
end

#ensure_complete_console_output(output) ⇒ Object



417
418
419
420
421
422
# File 'lib/mutx/results/result.rb', line 417

def ensure_complete_console_output output
  if output.size > @console_output.size
    @console_output = output
    self.save!
  end
end

#ensure_finished!Object



258
259
260
# File 'lib/mutx/results/result.rb', line 258

def ensure_finished!
  finish! unless finished?
end

#finish!Object



206
207
208
209
210
211
212
213
214
# File 'lib/mutx/results/result.rb', line 206

def finish!
  @finished_at= now_in_seconds
  @status = @summary = "finished"
  save_report
  self.save!
  delete_asociated_files!
  Mutx::Support::Log.debug "[#{@id}] Executuion finished" if Mutx::Support::Log
  true
end

#finished?Boolean



457
458
459
# File 'lib/mutx/results/result.rb', line 457

def finished?
  @status =~ /(finished|stopped)/i
end

#finished_at_formattedString

Returns the finished at time attribute in a format way (configured on mutx info)



567
568
569
# File 'lib/mutx/results/result.rb', line 567

def finished_at_formatted
  Mutx::Support::TimeHelper.formatted_time_for @finished_at
end

#finished_by_timeout!Object



216
217
218
219
220
221
222
223
224
225
# File 'lib/mutx/results/result.rb', line 216

def finished_by_timeout!

  @timeout = "#{Mutx::Support::Configuration.execution_time_to_live}"
  reset!("Timeout reached '#{@timeout}'")
  # save_report
  # save_report_summary_and_status!
  Mutx::Support::Log.debug "[#{@id}] Finished by timeout (#{Mutx::Support::Configuration.execution_time_to_live} sec)" if Mutx::Support::Log
  Mutx::Workers::EmailSender.perform_async(self.id)
  true
end

#generate_cucumber_report?Boolean



202
203
204
# File 'lib/mutx/results/result.rb', line 202

def generate_cucumber_report?
  @task["cucumber_report"]
end

#get_started_message!Object



121
122
123
124
125
126
# File 'lib/mutx/results/result.rb', line 121

def get_started_message!
  text = self.started_message
  @started_message = nil
  self.save!
  text
end

#get_status!Object

tries to get status



359
360
361
362
363
364
365
366
# File 'lib/mutx/results/result.rb', line 359

def get_status!
  if is_cucumber?

    value = Mutx::View::Parser.get_status(read_report) if is_there_a_report_file?

    @status = @show_as = value if value
  end
end

#get_summary!Object



304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/mutx/results/result.rb', line 304

def get_summary!
  if is_cucumber?
    report = if is_there_a_report_file?
      read_report
    else
      @html_report
    end
    @summary = Mutx::View::Parser.extract_summary(report) unless summary?
    self.save!
  else

  end
end

#has_report?Boolean



300
301
302
# File 'lib/mutx/results/result.rb', line 300

def has_report?
  !@html_report.empty?
end

#has_scenario_executed?Boolean



498
499
500
# File 'lib/mutx/results/result.rb', line 498

def has_scenario_executed?
  Mutx::View::Parser.has_scenarios_executed? @html_report
end

#has_started_message?Boolean



117
118
119
# File 'lib/mutx/results/result.rb', line 117

def has_started_message?
  !@started_message.nil?
end

#has_summary?Boolean



490
491
492
# File 'lib/mutx/results/result.rb', line 490

def has_summary?
  report_has_summary?
end

#is_cucumber?Boolean



194
195
196
# File 'lib/mutx/results/result.rb', line 194

def is_cucumber?
  @task["cucumber"]
end

#is_finished?Boolean



469
# File 'lib/mutx/results/result.rb', line 469

def is_finished?; self.finished?; end

#is_process_dead?Boolean



266
267
268
# File 'lib/mutx/results/result.rb', line 266

def is_process_dead?
  Mutx::Support::Processes.alive? @pid
end

#is_ruby_platform?Boolean



198
199
200
# File 'lib/mutx/results/result.rb', line 198

def is_ruby_platform?
  @task["platform"]
end

#is_running?Boolean



448
449
450
# File 'lib/mutx/results/result.rb', line 448

def is_running?
  @status =~ /running|stated/i
end

#is_there_a_report_file?Boolean



292
293
294
295
296
297
298
# File 'lib/mutx/results/result.rb', line 292

def is_there_a_report_file?
  begin
    !open_report_file.nil?
  rescue
    false
  end
end

#is_there_console_output_file?Boolean



380
381
382
# File 'lib/mutx/results/result.rb', line 380

def is_there_console_output_file?
  File.exist? "#{Dir.pwd}/mutx/temp/#{console_output_file_name}"
end

#is_there_new_lines_to_save?(text) ⇒ Boolean



368
369
370
# File 'lib/mutx/results/result.rb', line 368

def is_there_new_lines_to_save? text
  text.size > @console_output.size
end

#load_values(data) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/mutx/results/result.rb', line 73

def load_values data
  data.each_pair do |var, value|
    begin
      send("#{var}=",value) if send("#{var}").nil?
    rescue; end
  end
end

#mark_as_saw!Object



502
503
504
505
506
# File 'lib/mutx/results/result.rb', line 502

def mark_as_saw!
  @saw = true
  Mutx::Support::Log.debug "[#{@id}] Marked as saw" if Mutx::Support::Log
  self.save!
end

#mark_last_check_timeObject

# Append text to console output # @param [String] text = the text to be appended def append_to_console_output text

@console_output += text
mark_last_check_time
self.save!

end



403
404
405
# File 'lib/mutx/results/result.rb', line 403

def mark_last_check_time
  @last_check_time = now_in_seconds
end

#nowTime

Returns actal timestamp



555
556
557
# File 'lib/mutx/results/result.rb', line 555

def now
  Time.now.localtime
end

#now_in_secondsObject

Returns the timestamp in seconds



573
574
575
# File 'lib/mutx/results/result.rb', line 573

def now_in_seconds
  now.to_i
end

#open_report_fileObject



335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/mutx/results/result.rb', line 335

def open_report_file
  begin
    FileUtils.cp("#{Dir.pwd}/mutx/temp/#{mutx_report_file_name}", "#{Dir.pwd}/mutx/temp/#{mutx_report_file_name}~")
    file_content = File.open "#{Dir.pwd}/mutx/temp/#{mutx_report_file_name}~", "r"

    File.delete("#{Dir.pwd}/mutx/temp/#{mutx_report_file_name}~")

  rescue
    false
  end
  file_content
end

#process_finished?Boolean



461
462
463
# File 'lib/mutx/results/result.rb', line 461

def process_finished?
  ! process_running?
end

#process_running?Boolean



465
466
467
# File 'lib/mutx/results/result.rb', line 465

def process_running?
  Mutx::Support::Processes.process_running? pid
end

#reached_timeout?Boolean



262
263
264
# File 'lib/mutx/results/result.rb', line 262

def reached_timeout?
  self.seconds_without_changes > Mutx::Support::Configuration.execution_time_to_live
end

#read_reportString

Returns the html report file created by MuTX



325
326
327
328
329
330
331
332
333
# File 'lib/mutx/results/result.rb', line 325

def read_report
  begin
    content = ''
    open_report_file.each_line do |line|
      content+= line
    end
  rescue; end
  content
end

#report_has_summary?Boolean



494
495
496
# File 'lib/mutx/results/result.rb', line 494

def report_has_summary?
  @summary.include? "scenario"
end

#report_says_finished?Boolean



471
472
473
# File 'lib/mutx/results/result.rb', line 471

def report_says_finished?
  Mutx::View::Parser.finished_statement? @html_report
end

#reset!(reason = nil) ⇒ Object



479
480
481
482
483
484
485
486
487
488
# File 'lib/mutx/results/result.rb', line 479

def reset! reason=nil
  status_text = "stopped"
  status_text += " (#{reason})" if reason
  @status = @summary = status_text
  @finished_at= now_in_seconds
  save_report
  delete_asociated_files!
  self.save!
  Mutx::Support::Log.debug "[#{@id}] Execution stoppped (reset)" if Mutx::Support::Log
end

#result_data_structureObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mutx/results/result.rb', line 87

def result_data_structure
  {
    "_id"                       => id,
    "task"                      => @task,
    "execution_name"            => execution_name,
    "command"                   => command,
    "application"               => application,
    "custom_params"             => custom_params,
    "mutx_command"              => mutx_command,
    "started_message"           => started_message,
    "mutx_report_file_name"     => mutx_report_file_name,
    "html_report"               => html_report,
    "started_at"                => started_at,
    "finished_at"               => finished_at,
    "status"                    => status,
    "timeout"                   => timeout,
    "summary"                   => summary,
    "results_details"           => results_details,
    "show_as"                   => show_as,
    "console_output_file_name"  => console_output_file_name,
    "console_output"            => console_output,
    "git_log"                   => git_log,
    "saw"                       => saw,
    "pid"                       => pid,
    "last_check_time"           => last_check_time,
    "execution_data"            => execution_data,
    "configuration_values"      => configuration_values
  }
end

#running!Object



348
349
350
351
352
# File 'lib/mutx/results/result.rb', line 348

def running!
  @status = "running"
  Mutx::Support::Log.debug "[#{@id}] Setted as running" if Mutx::Support::Log
  self.save!
end

#running?Boolean



354
355
356
# File 'lib/mutx/results/result.rb', line 354

def running?
  @status == "running"
end

#save!Boolean

Persists the status of the object on mongo. If result exist update it, else creates it



580
581
582
583
584
585
586
587
588
# File 'lib/mutx/results/result.rb', line 580

def save!
  if Mutx::Database::MongoConnector.result_data_for_id(id)
    
    Mutx::Database::MongoConnector.update_result(result_data_structure)
  else
    Mutx::Database::MongoConnector.insert_result(result_data_structure)
  end
  Mutx::Support::Log.debug "[#{@id}] Result saved" if Mutx::Support::Log
end

#save_reportObject

Reads, copy html report from cucumber output file and saves it on a instance variable Persists changes on mongo and then deletes the html reporte file



282
283
284
285
286
287
288
289
290
# File 'lib/mutx/results/result.rb', line 282

def save_report
  if is_cucumber? and is_there_a_report_file?
    new_content = Mutx::View::Parser.adapt_to_mutx(read_report, self)
    if new_content.size > @html_report.size
      @html_report= new_content
      Mutx::Support::Log.debug "[#{@id}] Report saved" if Mutx::Support::Log
    end
  end
end

#save_report_summary_and_status!Object



271
272
273
274
275
276
277
# File 'lib/mutx/results/result.rb', line 271

def save_report_summary_and_status!
  if is_cucumber?
    self.save_report
    self.get_summary!
    self.get_status!
  end
end

#seconds_without_changesObject

Returns the seconds that there is no console output changes only if it is not finished, else returns 0 This is aimed to help to detect if execution is freezed (or has a debugger statement)



545
546
547
# File 'lib/mutx/results/result.rb', line 545

def seconds_without_changes
  (self.finished? or self.stopped?) ? 0 : (now_in_seconds - @last_check_time)
end

#started?Boolean



453
454
455
# File 'lib/mutx/results/result.rb', line 453

def started?
  @status == "started"
end

#started_at_formattedString

Returns the started at time attribute in a format way (configured on mutx info)



561
562
563
# File 'lib/mutx/results/result.rb', line 561

def started_at_formatted
  Mutx::Support::TimeHelper.formatted_time_for @started_at
end

#stopped?Boolean



475
476
477
# File 'lib/mutx/results/result.rb', line 475

def stopped?
  @status =~ /(reset|stopped)/i
end

#summary?Boolean



318
319
320
# File 'lib/mutx/results/result.rb', line 318

def summary?
  !(["Not available yet", "running"].include? @summary)
end

#task_idObject



182
183
184
# File 'lib/mutx/results/result.rb', line 182

def task_id
  @task["id"]
end

#task_typeObject



190
191
192
# File 'lib/mutx/results/result.rb', line 190

def task_type
  @task["type"]
end

#update_console_output(output) ⇒ Object



407
408
409
410
411
412
413
414
415
# File 'lib/mutx/results/result.rb', line 407

def update_console_output output
  if output.size > @console_output.size
    @console_output = output
    mark_last_check_time
    self.save!
  else
    raise ExecutionTimeoutError.new("Execution Timeout reached") if (now_in_seconds - @last_check_time) > Mutx::Support::Configuration.execution_time_to_live
  end
end

#validate_params(custom_params = {}) ⇒ hash

Returns a hash with valid parameters. This is to prevent command line command with could cause problems



147
148
149
150
151
152
153
154
155
156
# File 'lib/mutx/results/result.rb', line 147

def validate_params custom_params={}
  unless custom_params.nil?
    validated = custom_params.select do |key, value|
      unless value.nil?
        Mutx::Support::Risk.secure? value
      end
    end
  end
  validated || {}
end