Class: Patir::CommandSequenceStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/patir/command.rb

Overview

CommandSequenceStatus represents the status of a CommandSequence, including the status of all the steps for this sequence.

In order to extract the status from steps, classes should quack to the rythm of Command. CommandSequenceStatus does this, so you can nest Stati

The status of an action sequence is :not_executed, :running, :success, :warning or :error and represents the overall status :not_executed is set when all steps are :not_executed :running is set while the sequence is running. Upon completion or interruption one of :success, :error or :warning will be set. :success is set when all steps are succesfull. :warning is set when at least one step generates warnings and there are no failures. :error is set when after execution at least one step has the :error status

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sequence_name, steps = nil) ⇒ CommandSequenceStatus

You can pass an array of Commands to initialize CommandSequenceStatus



342
343
344
345
346
347
348
349
350
351
352
# File 'lib/patir/command.rb', line 342

def initialize sequence_name,steps=nil
  @sequence_name=sequence_name
  @sequence_runner=""
  @sequence_id=nil
  @step_states||=Hash.new
  #not run yet
  @status=:not_executed
  #translate the array of steps as we need it in number=>state form
  steps.each{|step| self.step=step } if steps
  @start_time=Time.now
end

Instance Attribute Details

#sequence_idObject

Returns the value of attribute sequence_id.



340
341
342
# File 'lib/patir/command.rb', line 340

def sequence_id
  @sequence_id
end

#sequence_nameObject

Returns the value of attribute sequence_name.



340
341
342
# File 'lib/patir/command.rb', line 340

def sequence_name
  @sequence_name
end

#sequence_runnerObject

Returns the value of attribute sequence_runner.



340
341
342
# File 'lib/patir/command.rb', line 340

def sequence_runner
  @sequence_runner
end

#start_timeObject

Returns the value of attribute start_time.



340
341
342
# File 'lib/patir/command.rb', line 340

def start_time
  @start_time
end

#statusObject

Returns the value of attribute status.



340
341
342
# File 'lib/patir/command.rb', line 340

def status
  @status
end

#step_statesObject

Returns the value of attribute step_states.



340
341
342
# File 'lib/patir/command.rb', line 340

def step_states
  @step_states
end

#stop_timeObject

Returns the value of attribute stop_time.



340
341
342
# File 'lib/patir/command.rb', line 340

def stop_time
  @stop_time
end

#strategyObject

Returns the value of attribute strategy.



340
341
342
# File 'lib/patir/command.rb', line 340

def strategy
  @strategy
end

Instance Method Details

#completed?Boolean

A sequence is considered completed when:

a step has errors and the :fail_on_error strategy is used

a step has warnings and the :fail_on_warning strategy is used

in all other cases if none of the steps has a :not_executed or :running status

Returns:

  • (Boolean)


370
371
372
373
374
375
376
377
378
379
# File 'lib/patir/command.rb', line 370

def completed?
  #this saves us iterating once+1 when no execution took place
  return false if !self.executed?
  @step_states.each do |state|
    return true if state[1][:status]==:error && state[1][:strategy]==:fail_on_error
    return true if state[1][:status]==:warning && state[1][:strategy]==:fail_on_warning
  end
  @step_states.each{|state| return false if state[1][:status]==:not_executed || state[1][:status]==:running }
  return true
end

#errorObject



448
449
450
# File 'lib/patir/command.rb', line 448

def error
  return ""
end

#exec_timeObject



435
436
437
438
# File 'lib/patir/command.rb', line 435

def exec_time
  return @stop_time-@start_time if @stop_time
  return 0
end

#executed?Boolean

Returns:

  • (Boolean)


451
452
453
454
# File 'lib/patir/command.rb', line 451

def executed?
  return true unless @status==:not_executed
  return false
end

#nameObject



439
440
441
# File 'lib/patir/command.rb', line 439

def name
  return @sequence_name
end

#numberObject



442
443
444
# File 'lib/patir/command.rb', line 442

def number
  return @sequence_id
end

#outputObject



445
446
447
# File 'lib/patir/command.rb', line 445

def output
  return self.summary
end

#running?Boolean

Returns:

  • (Boolean)


353
354
355
356
# File 'lib/patir/command.rb', line 353

def running?
  return true if :running==@status
  return false
end

#step=(step) ⇒ Object

Adds a step to the state. The step state is inferred from the Command instance __step__



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/patir/command.rb', line 386

def step=step
  @step_states[step.number]={:name=>step.name,
    :status=>step.status,
    :output=>step.output,
    :duration=>step.exec_time,
    :error=>step.error,
    :strategy=>step.strategy
  }
  #this way we don't have to compare all the step states we always get the worst last stable state
  #:not_executed<:success<:warning<:success
  unless @status==:running
    @previous_status=@status 
    case step.status
    when :running
      @status=:running
    when :warning
      @status=:warning unless @status==:error
      @status=:error if @previous_status==:error
    when :error
      @status=:error
    when :success
      @status=:success unless @status==:error || @status==:warning
      @status=:warning if @previous_status==:warning
      @status=:error if @previous_status==:error
    when :not_executed
      @status=@previous_status
    end
  end#unless running
end

#step_state(number) ⇒ Object

A nil means there is no step with that number



381
382
383
384
# File 'lib/patir/command.rb', line 381

def step_state number
  s=@step_states[number] if @step_states[number]
  return s
end

#success?Boolean

true is returned when all steps were succesfull.

Returns:

  • (Boolean)


358
359
360
361
# File 'lib/patir/command.rb', line 358

def success?
  return true if :success==@status
  return false
end

#summaryObject

produces a brief text summary for this status



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/patir/command.rb', line 416

def summary
  sum=""
  sum<<"#{@sequence_id}:" if @sequence_id
  sum<<"#{@sequence_name}. " unless @sequence_name.empty?
  sum<<"Status - #{@status}" 
  if !@step_states.empty? && @status!=:not_executed
    sum<<". States #{@step_states.size}\nStep status summary:"
    sorter=Hash.new
    @step_states.each do |number,state|
      #sort them by number
      sorter[number]="\n\t#{number}:'#{state[:name]}' - #{state[:status]}"
    end
    1.upto(sorter.size) {|i| sum<<sorter[i] if sorter[i]}
  end 
  return sum
end

#to_sObject



432
433
434
# File 'lib/patir/command.rb', line 432

def to_s
  "'#{sequence_id}':'#{@sequence_name}' on '#{@sequence_runner}' started at #{@start_time}.#{@step_states.size} steps"
end