Module: ASAConsole::Test

Defined in:
lib/asa_console/test.rb,
lib/asa_console/test/script.rb

Overview

Methods to declare and execute ASAConsole test scripts in a lab environment.

Examples:

Contents of ./my_test_file.rb

ASAConsole::Test.script do |asa|
  log 'Connecting...'
  asa.connect
  log 'Executing the "show failover" command...'
  asa.show('failover')
  log 'Disconnecting...'
  asa.disconnect
end

Execute scripts in ./my_test_file.rb

require 'asa_console/test'

terminal_opts = { host: '192.0.2.1', user: 'admin', password: 'secret' }

ASAConsole::Test.color_scheme = :light
ASAConsole::Test.start('./my_test_file.rb', terminal_opts)

Defined Under Namespace

Classes: Script

Class Method Summary collapse

Class Method Details

.color_scheme=(scheme_key) ⇒ void

Set the color scheme for test script output.

Parameters:

  • scheme_key (Symbol)

    :light for light (or bold) colors, :dark for dark colors. Any other option prevents colorized output.


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/asa_console/test.rb', line 50

def self.color_scheme=(scheme_key)
  case scheme_key
  when :light
    @@colors = {
      prompt: "\e[1;36m",
      input:  "\e[1;33m",
      output: "\e[1;32m",
      log:    "\e[1;31m",
      info:   "\e[1;35m"
    }
  when :dark
    @@colors = {
      prompt: "\e[0;36m",
      input:  "\e[0;33m",
      output: "\e[0;32m",
      log:    "\e[0;31m",
      info:   "\e[0;35m"
    }
  else
    @@colors = {}
  end
end

.script {|asa| ... } ⇒ void

Declare a test script by passing a block to this method.

Yield Parameters:


39
40
41
42
43
# File 'lib/asa_console/test.rb', line 39

def self.script(&block)
  klass = Class.new(Script)
  klass.send(:define_method, 'test!', &block)
  @@scripts << klass
end

.start(test_file, terminal_opts, enable_password = nil, show_session_log = false) ⇒ Boolean

Load a ruby source file with test script declarations and execute them.

The terminal_opts and enable_password parameters will be passed to a ASAConsole factory method to generate a test object for each script run.

Parameters:

  • test_file (String)
  • terminal_opts (Hash)
  • enable_password (String) (defaults to: nil)
  • show_session_log (Boolean) (defaults to: false)

    append the session log to the end of each test output if true

Returns:

  • (Boolean)

    true if test_file can be loaded with require, or false otherwise


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/asa_console/test.rb', line 114

def self.start(test_file, terminal_opts, enable_password = nil, show_session_log = false)
  begin
    require test_file
  rescue LoadError
    puts "Unable to load #{test_file}"
    return false
  end

  @@scripts.each do |klass|
    script = klass.new(terminal_opts, enable_password)
    script.run
    script.show_session_log if show_session_log
    puts
    puts colorize('Test Complete!', :info)
    puts
  end

  true
end

.test_namesArray

A list of test names that can be passed to the command line utility.

Returns:

  • (Array)

97
98
99
100
# File 'lib/asa_console/test.rb', line 97

def self.test_names
  names = Dir.glob(File.join(test_path, 'test_*.rb'))
  names.collect { |file| file.sub(/.*test_(.*)\.rb$/, '\1') }.sort
end

.test_pathString .test_path(test_name) ⇒ String

Returns the absolute path of the test script directory or, if a test name is given, returns the path to the test file.

Overloads:

  • .test_pathString

    Returns the filesystem path of the test script directory

    Returns:

    • (String)

      the filesystem path of the test script directory

  • .test_path(test_name) ⇒ String

    Returns the filesystem path of the named test script

    Parameters:

    • test_name (String)

      the test name

    Returns:

    • (String)

      the filesystem path of the named test script

Returns:

  • (String)

    a filesystem path


88
89
90
91
92
# File 'lib/asa_console/test.rb', line 88

def self.test_path(test_name = nil)
  path = File.realpath(File.join(File.dirname(__FILE__), '..', '..', 'script'))
  path = File.join(path, "test_#{test_name}.rb") if test_name
  path
end