Class: Mu::Command::Cmd_runscenario

Inherits:
Mu::Command show all
Defined in:
lib/mu/command/cmd_runscenario.rb

Constant Summary

Constants inherited from Mu::Command

Api

Constants included from Helper

Helper::ESCAPES

Instance Attribute Summary collapse

Attributes inherited from Mu::Command

#options, #opts

Instance Method Summary collapse

Methods inherited from Mu::Command

#initialize

Methods included from Helper

#ask, #bin2hex, #error, #escape, #format_float, #get_file_as_string_array, #make_xml, #msg, #shift, #to_boolean

Constructor Details

This class inherits a constructor from Mu::Command

Instance Attribute Details

#ddt_apiObject

Returns the value of attribute ddt_api.



7
8
9
# File 'lib/mu/command/cmd_runscenario.rb', line 7

def ddt_api
  @ddt_api
end

#errorsObject

Returns the value of attribute errors.



7
8
9
# File 'lib/mu/command/cmd_runscenario.rb', line 7

def errors
  @errors
end

#hostObject

Returns the value of attribute host.



7
8
9
# File 'lib/mu/command/cmd_runscenario.rb', line 7

def host
  @host
end

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/mu/command/cmd_runscenario.rb', line 7

def password
  @password
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/mu/command/cmd_runscenario.rb', line 7

def username
  @username
end

Instance Method Details

#cmd_help(argv) ⇒ Object

displays command-line help

* argv = command-line arguments


11
12
13
# File 'lib/mu/command/cmd_runscenario.rb', line 11

def cmd_help argv
   help
end

#cmd_run(argv) ⇒ Object

verifies a Mu Studio scenario

* argv = command-line arguments, requires a scenario (-s) argument, which must be an xml scenario template to be loaded onto the Mu


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/mu/command/cmd_runscenario.rb', line 17

def cmd_run argv
  args = parse_cli argv
  setup

  if not args['scenario']
    msg "scenario required"
    return help
  else
    if args['scenario'].include?(".xml")
       scenario = args['scenario']
    else # TODO: eventually, msl files may be supported by this api
      msg "only .xml files are currently supported"
      return help
    end
  end

  if not args['dir']
    dir = "."
  else
    dir = args['dir']
  end

  if scenario.include?("/")
    filename = scenario
  else
    filename = dir + "/" + scenario
  end

  if not args['interfaces']
    @interfaces = Array.new
  else
    @interfaces = args['interfaces'].split(",")
  end

  @errors = Array.new
  hosts_array = Array.new
  roles_array = Array.new
      
  begin
 msg "new session"
    @ddt_api.new_session
    msg filename, Logger::DEBUG
    f = File.open(filename)
    doc = Nokogiri::XML(f)
    response = @http.post_xml("templates/import", doc)
    msg "response from post(#{filename}):\n#{response}", Logger::DEBUG
    scenario_name = doc.xpath("//scenario")[0].attribute('name')
    hosts = doc.xpath("//hosts/host")
    uuid = doc.xpath("//scenario")[0].attribute('uuid')
    roles = doc.xpath("//hosts/host/role")
    ids = doc.xpath("//hosts/host/id")
    channels = doc.xpath("//steps/channel")
    type = doc.xpath("//hosts/host/type")[0].text

    if !@interfaces.empty?
      if @interfaces.size != hosts.size
        msg "Error. The number of hosts/interfaces specified on the command-line does not equal the number
             of hosts in the template. #{@interfaces.size} != #{hosts.size}"
        return
      end
    end
    
    msg @ddt_api.load_scenario(uuid)

    roles.each_with_index do | r, i |
       content = r.text
       if @interfaces.empty?
         begin
           host = content.match(/\(.*\)/).to_s
           host = host[1, host.index(".") - 1].downcase
           hosts_array << host
         rescue => e
           msg e
           msg "expected to find host name embedded in role, e.g. 'client (A1.V4)'"
         end
       else
         hosts_array << @interfaces[i]
       end
       roles_array << content # use the whole name for the role
    end

    msg @ddt_api.set_hosts(roles_array, hosts_array, type)

    # if there are channels in the scenario, bind them
    if channels.size > 0
      channel_roles = Array.new
      channel_names = Array.new
      arg_channels = args['channel'].split(",")
      arg_channels.each do | c |
        channel_roles << "channel" # role is always "channel"
        channel_names << c
      end
      msg @ddt_api.set_channels(channel_roles, channel_names)
    end

    # verify the scenario
    msg @ddt_api.setup_test
    response = @ddt_api.run
    if response == ""
      msg "==> #{scenario_name} run returned without status"
      @errors << "#{scenario_name}: status = #{response}"
    else
      doc = Nokogiri::XML(response)
      status = doc.xpath("//status")[0].content
      msg "==> #{scenario_name}: status = #{status}"
      if status == 'failed'
      # on error, add to the errors array
        @errors << "#{scenario_name}: status = #{status}"
      end
    end

    @ddt_api.teardown_test
    @ddt_api.close_session

    # clear the hosts and roles array for the next iteration
    roles_array.clear
    hosts_array.clear
  end # if !exclude_list.include? and hosts.length
  # if there were errors, print them out and fail the test
  estr = ""
  if @errors.size > 0
    @errors.each do | e |
      estr = estr + e + "\n"
      msg estr
    end
 end
ensure
  teardown
end