Class: Mu::Ddt

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/mu/api/ddt.rb

Constant Summary

Constants included from Helper

Helper::ESCAPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

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

Constructor Details

#initialize(host = ENV['MU_IP'], username = ENV['MU_ADMIN_USER'], password = ENV['MU_ADMIN_PASS']) ⇒ Ddt

Returns a new instance of Ddt.



7
8
9
10
11
12
13
14
15
# File 'lib/mu/api/ddt.rb', line 7

def initialize(host=ENV['MU_IP'], username=ENV['MU_ADMIN_USER'], password=ENV['MU_ADMIN_PASS'])
  @host = host
  @username  = username
  @password  = password
  @docroot = "/api/v5/ddt/"
  @session_id = nil
  @http = HttpHelper.new(@host, @username, @password, @docroot)
  msg "Created Ddt Api object to :#{@host}", Logger::DEBUG
end

Instance Attribute Details

#docrootObject

Returns the value of attribute docroot.



5
6
7
# File 'lib/mu/api/ddt.rb', line 5

def docroot
  @docroot
end

#hostObject

Returns the value of attribute host.



5
6
7
# File 'lib/mu/api/ddt.rb', line 5

def host
  @host
end

#passwordObject

Returns the value of attribute password.



5
6
7
# File 'lib/mu/api/ddt.rb', line 5

def password
  @password
end

#session_idObject

Returns the value of attribute session_id.



5
6
7
# File 'lib/mu/api/ddt.rb', line 5

def session_id
  @session_id
end

#usernameObject

Returns the value of attribute username.



5
6
7
# File 'lib/mu/api/ddt.rb', line 5

def username
  @username
end

Instance Method Details

#close_all_sessionsObject

closes all Studio Verify sessions



69
70
71
72
73
# File 'lib/mu/api/ddt.rb', line 69

def close_all_sessions
  response = @http.post("closeAllSessions")
  msg response, Logger::DEBUG
  return response
end

#close_sessionObject

closes the existing Studio Verify session



43
44
45
46
47
# File 'lib/mu/api/ddt.rb', line 43

def close_session
  response = @http.get("session/closeSession")
  msg response, Logger::DEBUG
  return response
end

#collect_results(timeout = 120) ⇒ Object

collects results until the test is done or the timeout expires

* timeout = time in seconds to wait for the test to complete


168
169
170
171
172
# File 'lib/mu/api/ddt.rb', line 168

def collect_results(timeout=120)
  wait_until_done(timeout)
  results = get_testset_results
  return results
end

#csv_export(uuid = @testset) ⇒ Object

exports a testset to a csv file

* uuid = the uuid of the testset to export


214
215
216
217
218
# File 'lib/mu/api/ddt.rb', line 214

def csv_export(uuid=@testset)
  response = @http.post("session/test/export/csv/#{uuid}")
  msg response, Logger::DEBUG
  return response
end

#csv_import(filepath) ⇒ Object

imports a csv file to the Mu

* filepath = the path to the csv file


223
224
225
226
227
228
229
# File 'lib/mu/api/ddt.rb', line 223

def csv_import(filepath)
  return "new_session required" if $cookie.nil? or $cookie["testSessionId"].nil?
  testSessionId = $cookie["testSessionId"]
  response = @http.post_form("session/test/import/csv?testSessionId=#{testSessionId}", filepath)
  msg response, Logger::DEBUG
  return response
end

#get_all_sessionsObject

returns the session id’s of all existing Studio Verify sessions



76
77
78
79
80
# File 'lib/mu/api/ddt.rb', line 76

def get_all_sessions
  response = @http.get("getAllSessions")
  msg response, Logger::DEBUG
  return response
end

#get_channelsObject

returns the channel elements of the loaded scenario



127
128
129
130
131
# File 'lib/mu/api/ddt.rb', line 127

def get_channels
  response = @http.get("session/scenario/channels")
  msg response, Logger::DEBUG
  return response
end

#get_hostsObject

returns the hosts in the loaded scenario



105
106
107
108
109
# File 'lib/mu/api/ddt.rb', line 105

def get_hosts
  response = @http.get("session/scenario/hosts")
  msg response, Logger::DEBUG
  return response
end

#get_optionsObject

returns the options from the loaded scenario



147
148
149
150
151
# File 'lib/mu/api/ddt.rb', line 147

def get_options
  response = @http.get("session/scenario/options")
  msg response, Logger::DEBUG
  return response
end

#get_sessionsObject

returns array of session_ids



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mu/api/ddt.rb', line 50

def get_sessions
  all_sessions = Array.new
  response = @http.get("")  # with no args
  sessions = response["sessions"]
  if !sessions["session"].nil?
    num = sessions["session"].size
    if num > 1
      sessions["session"].each do | s |
        all_sessions << s["id"]
      end
    else # special case if there is only one (there is no array)
      all_sessions << sessions["session"]["id"]
    end
    return all_sessions
  end
  return nil
end

#get_testset_resultsObject

returns the results of a running test set. can be called repeatedly. the test is done when the results include the ‘END’ keyword



206
207
208
209
210
# File 'lib/mu/api/ddt.rb', line 206

def get_testset_results
  response = @http.get("session/test/runSuite/results")
  msg response, Logger::DEBUG
  return response
end

#get_testset_statusObject

returns the status of a running test set



198
199
200
201
202
# File 'lib/mu/api/ddt.rb', line 198

def get_testset_status
  response = @http.get("session/test/runSuite/status")
  msg response, Logger::DEBUG
  return response
end

#load_scenario(uuid) ⇒ Object

loads a scenario

* the uuid of a scenario present on the Mu


98
99
100
101
102
# File 'lib/mu/api/ddt.rb', line 98

def load_scenario(uuid)
  response = @http.post("session/loadScenario/#{uuid}")
  msg response, Logger::DEBUG
  return response
end

#new_sessionObject

must be called first to establish a new Studio Verify session



33
34
35
36
37
38
39
40
# File 'lib/mu/api/ddt.rb', line 33

def new_session
  reply = @http.post("newSession") # with no args
  response = reply["response"]
  msg response, Logger::DEBUG
  @session_id = response["sessionId"]
  msg @session_id, Logger::DEBUG
  return @session_id  
end

#runObject

verifies the loaded scenario



18
19
20
21
22
# File 'lib/mu/api/ddt.rb', line 18

def run
  response = @http.post("session/test/run")
  msg response, Logger::DEBUG
  return response
end

#run_testset(uuid) ⇒ Object

sets and executes test suite

* uuid = the uuid of a testset loaded on the Mu


26
27
28
29
30
# File 'lib/mu/api/ddt.rb', line 26

def run_testset(uuid)
  response = @http.post("session/test/runSuite/#{uuid}")
  msg response, Logger::DEBUG
  return response
end

#set_channels(roles = [], names = []) ⇒ Object

sets the channel elements of the loaded scenario

* roles = an array of the roles defined for the channels in the scenario ('channel')
* names = an array of host names to be mapped to the roles


136
137
138
139
140
141
142
143
144
# File 'lib/mu/api/ddt.rb', line 136

def set_channels(roles=[], names=[])
  responses = Array.new
  roles.length.times do | i |
      response = @http.post("session/scenario/channels/#{roles[i]}/#{names[i]}")
      responses << response
  end
  msg responses, Logger::DEBUG
  return responses
end

#set_hosts(roles = [], names = [], type = "v4") ⇒ Object

sets the hosts in the loaded scenario

* roles = an array of the host roles defined in the scenario
* names = an array of host names to be mapped to the roles
* type = network layer type (v4, v6 or l2) matching the defined hosts


115
116
117
118
119
120
121
122
123
124
# File 'lib/mu/api/ddt.rb', line 115

def set_hosts(roles=[], names=[], type="v4")
  responses = Array.new
  hosts = roles.length.to_i
  hosts.times do | i |
     response = @http.post("session/scenario/hosts/#{roles[i]}/#{names[i]}/#{type}")
     responses << response
  end
  msg responses, Logger::DEBUG
  return responses
end

#set_options(names = [], values = []) ⇒ Object

sets the options for the loaded scenario

* an array of option names
* an array of option values


156
157
158
159
160
161
162
163
164
# File 'lib/mu/api/ddt.rb', line 156

def set_options(names=[], values=[])
  responses = Array.new
  names.length.times do | i |
     response = @http.post("session/scenario/options/#{names[i]}/#{values[i]}")
     responses << response
  end
  msg responses, Logger::DEBUG
  return responses
end

#setup_testObject

sets up a test for run



83
84
85
86
87
# File 'lib/mu/api/ddt.rb', line 83

def setup_test
  response = @http.post("session/setupTest")
  msg response, Logger::DEBUG
  return response
end

#teardown_testObject

tears down a test



90
91
92
93
94
# File 'lib/mu/api/ddt.rb', line 90

def teardown_test
  response = @http.post("session/test/tearDownTest")
  msg response, Logger::DEBUG
  return response
end

#wait_until_done(timeout = 120) ⇒ Object

waits until the test is done or the timeout expires

* timeout = the time in seconds to wait for the test to complete


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/mu/api/ddt.rb', line 176

def wait_until_done(timeout=120)
  finished = false
  interval = timeout / 10
  10.times do
    begin
      response = get_testset_status
      #msg "wait_until_done, response = #{response}", Logger::DEBUG
      if !response.nil?
        if response.to_s.include?("Done")
          finished = true
          return finished
        end
      end
    rescue Exception => e
      puts e, Logger::DEBUG # status may not be ready right away. may return a 500
    end
    sleep interval
  end
  return finished
end