class Object

Public Instance Methods

die(line) click to toggle source
# File lib/restest.rb, line 37
def die(line)
  puts "[ERROR] #{line}"
  exit(1)
end
get(name) click to toggle source
# File lib/restest.rb, line 54
def get(name)
  return $GLOBAL_STATE.get(name)
end
load_test(name, state) click to toggle source
# File lib/restest.rb, line 137
def load_test(name, state)
  if (!File.exists?("tests/#{name}"))
    die("No test file #{name}")
  end

  state.set_in_test('filename', name)
  file = File.new("tests/#{name}", "r")
  doc = false
  out(3, "Reading test file tests/#{name}")

  # line 1: service-class-name api-name
  line = file.gets
  line =~ /(\S*)\s*(\S*)/
  if (!$1 || !$2)
    die("Test #{name} missing service and/or API")
  end
  state.set_in_test('service', $1)
  state.set_in_test('api', $2)

  # then process 'set' or 'validate' directives
  while ((line = file.gets) != nil)
    next if line =~ /^\s*$/
    next if line =~ /^#/

    if (line =~ /^set (\S*)\s+=\s+(.*)/)
      state.set_in_test($1, $2)

    elsif (line =~ /^validate (\S+)\s+(\S+)\s+(.*)/)
      state.set_validate($1, "#{$2} #{$3}")

    elsif (line =~ /^doc (.*)/)
      state.set_in_test('doc', $1)
      doc = true

    else
      die("Unknown directive [#{line}] in #{name}")
    end
  end

  if (!doc)
    die("Test #{name} has no documentation (doc line)")
  end
end
method_missing(method_name, *args) click to toggle source
Calls superclass method
# File lib/restest.rb, line 368
def method_missing(method_name, *args)
  if ($TEST_FILES[method_name.to_s])
    run_test(method_name)
  elsif (method_name == :ignore) && $TEST_FILES[args[0].to_s]
    run_test(args[0], true)
  else
    super
  end
end
out(level, line) click to toggle source
# File lib/restest.rb, line 45
def out(level, line)
  if ($LOG_LEVEL >= level)
    puts line
  end
end
respond_to?(method_name, include_private = false) click to toggle source
Calls superclass method
# File lib/restest.rb, line 378
def respond_to?(method_name, include_private = false)
  if ($TEST_FILES[method_name.to_s])
    return true
  else
    super
  end
end
restore_state(tag) click to toggle source
# File lib/restest.rb, line 82
def restore_state(tag)
  $GLOBAL_STATE.vars = $SAVED_STATES[tag]
  if ($GLOBAL_STATE.vars == nil)
    die("Attempted to restore to a saved stage [#{tag}] which does not exist!")
  end
end
run_suite(name) click to toggle source
# File lib/restest.rb, line 126
def run_suite(name)
  out(1, "\n\n--Running included test suite #{name}")
  save_state("_pre_run_suite")
  load(name)
  restore_state("_pre_run_suite")
  out(1, "\n--Completed included test suite #{name}")
end
run_test(name, ignore=false) click to toggle source
# File lib/restest.rb, line 184
def run_test(name, ignore=false)
  out(1, "\n\n\nRunning test #{name}")

  if ($LOG_LEVEL > 0)
    save_state('_internal_trace')
  end

  test_state = State.new($GLOBAL_STATE)
  load_test(name, test_state)

  service = test_state.get('service')
  api = test_state.get('api')
  out(1, "Service class: [#{service}] Calling API: [#{api}]")

  if ($INTERACTIVE)
    print "Interactive mode... <enter> to run this test now, <c>ontinue: "
    STDOUT.flush
    input = gets.chomp!
    $INTERACTIVE = false if (input == "c")
  end

  before = Time.now()

  allow_retries = test_state.get('allow_retries')
  if (allow_retries != nil)
    times = allow_retries.to_i
    sleep_sec = test_state.get('retry_sleep').to_i
    if (sleep_sec < 1)
      die("allow_retries is set but retry_sleep is not")
    end
    out(1, "Test allow retries: allow_retries: #{times}  retry_sleep: #{sleep_sec}")
    begin
      out(1, "Tries left: #{times}")
      test_state = State.new($GLOBAL_STATE)
      load_test(name, test_state)
      testobj = Object::const_get(service).new
      result = testobj.send(api, test_state)
      times -= 1
      sleep(sleep_sec) if !result.is_ok
    end while (times > 0 && !result.is_ok && result.allow_retry)

  else
    testobj = Object::const_get(service).new
    result = testobj.send(api, test_state)
  end

  if ($LOG_LEVEL > 0)
    show_state_diff('_internal_trace')
  end

  duration = Integer((Time.now() - before) * 1000)

  out(1, "Duration of test: #{duration} ms")

  prefix = ""
  if (ignore)
    prefix="IGNORE-"
  end

  if (result.is_ok)
    out(0, "[#{prefix}OK] (#{duration}ms) #{service}.#{api}: #{test_state.get('doc')}")
    $TESTS_OK += 1 if !ignore
  else
    out(0, "[#{prefix}FAIL] (#{duration}ms) #{service}.#{api}: #{test_state.get('doc')} #{result.message}")
    $TESTS_FAIL += 1 if !ignore
  end

  if ($REPORT != nil)
    if (result.is_ok)
      status = "#{prefix}OK"
    else
      status = "#{prefix}FAIL"
    end
    $REPORT.log(status, duration, service, api, name, result.message)
  end

  if (result.abort_suite_run)
    die("Test suite run has been aborted by previous test!")
  end
end
save_state(tag) click to toggle source
# File lib/restest.rb, line 75
def save_state(tag)
  $SAVED_STATES[tag] = $GLOBAL_STATE.vars.clone()
end
set(name, value) click to toggle source
# File lib/restest.rb, line 61
def set(name, value)
  $GLOBAL_STATE.set(name, value)
end
show_state_diff(tag_a, tag_b = nil) click to toggle source
# File lib/restest.rb, line 93
def show_state_diff(tag_a, tag_b = nil)

  a = $SAVED_STATES[tag_a]
  if (tag_b == nil)
    b = $GLOBAL_STATE.vars
  else
    b = $SAVED_STATES[tag_b]
  end

  if (a == nil || b == nil)
    puts "[ERROR] Unable to show diff from state #{tag_a} to state #{tag_b}"
    return
  end

  b.each { |k,v|
    if (a[k] == nil)
      puts "  ADDED:   '#{k}' => '#{v}'"
    elsif (a[k] != v)
      puts "  CHANGED: '#{k}' from '#{a[k]}' to '#{v}'"
    end
  }

  a.each { |k,v|
    if (b[k] == nil)
      puts "  REMOVED: '#{k}' was '#{a[k]}'"
    end
  }
end
show_usage(opts) click to toggle source
# File lib/restest.rb, line 268
def show_usage(opts)
  puts opts
  puts <<EOF

Running a test suite
====================

To run a test suite, simply run the test suite file as it is an
executable ruby script. A configuration file argument must be
provided.

The configuration file is responsible for setting state values which
are specific to a test environment, and thus not suitable to set
elsewhere. These include values such as hostnames, user names, test
domains, etc.

(TODO: Until the framework packaging is organized, you need to run it
from the test suite directory. So for the share tests for example, "cd
share-tests" first. This is a temporary limitation.)

The initial share test script is called "suite1" so run it by:

% ./suite1 -c config.ENV

(Where ENV is the share environment to run it against. There are
separate config files for each share env.)

EOF
end
unset(name) click to toggle source
# File lib/restest.rb, line 68
def unset(name)
  $GLOBAL_STATE.unset(name)
end