Class: Inferno::CLI::Session::StartRun

Inherits:
Object
  • Object
show all
Includes:
Connection, Errors
Defined in:
lib/inferno/apps/cli/session/start_run.rb

Constant Summary collapse

COMMAND_OPTIONS =
{
  inputs: {
    aliases: ['-i'],
    type: :hash,
    desc: 'Inputs (i.e: --inputs=foo:bar goo:baz); will merge and override current session inputs ' \
          '(from preset or previous runs)'
  }
}.freeze
AUTH_INFO_COMPONENT_AUTH_MODE_DEFAULTS =
{
  'use_discovery' => 'true',
  'pkce_support' => 'enabled',
  'pkce_code_challenge_method' => 'S256',
  'auth_request_method' => 'GET'
}.freeze
AUTH_INFO_COMPONENT_ACCESS_MODE_DEFAULTS =
{
  'access_token' => '',
  'refresh_token' => '',
  'issue_time' => '',
  'expires_in' => ''
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Errors

#handle_web_api_error, #not_found_error_message, #parse_error_response, #test_run_not_found_message, #text_error_message

Methods included from Connection

#base_url, #check_session_exists, #connection, #delete, #get, #handle_connection_error, #post

Constructor Details

#initialize(session_id, options) ⇒ StartRun

Returns a new instance of StartRun.



25
26
27
28
# File 'lib/inferno/apps/cli/session/start_run.rb', line 25

def initialize(session_id, options)
  self.session_id = session_id
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



23
24
25
# File 'lib/inferno/apps/cli/session/start_run.rb', line 23

def options
  @options
end

#session_idObject

Returns the value of attribute session_id.



23
24
25
# File 'lib/inferno/apps/cli/session/start_run.rb', line 23

def session_id
  @session_id
end

Instance Method Details

#add_auth_info_component_defaults(component_object, runnable_input) ⇒ Object



164
165
166
167
# File 'lib/inferno/apps/cli/session/start_run.rb', line 164

def add_auth_info_component_defaults(component_object, runnable_input)
  default_from_runnable_components(component_object, runnable_input)
  default_component_from_definitions(component_object, auth_info_mode_from_runnable_input(runnable_input))
end

#auth_info_mode_from_runnable_input(runnable_input) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/inferno/apps/cli/session/start_run.rb', line 169

def auth_info_mode_from_runnable_input(runnable_input)
  mode = runnable_input.dig('options', 'mode')
  if mode.nil?
    'access'
  elsif ['access', 'auth'].include?(mode)
    mode
  else
    puts JSON.pretty_generate({ errors: "Failed to create run: unknown auth_info mode '#{mode}'." })
    exit(3)
  end
end

#calculate_inputsObject

trying to replicate the process used in the UI



139
140
141
142
143
# File 'lib/inferno/apps/cli/session/start_run.rb', line 139

def calculate_inputs
  target_runnable_details['inputs'].map do |runnable_input|
    input_run_value(runnable_input)
  end
end

#components_to_default(mode) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/inferno/apps/cli/session/start_run.rb', line 217

def components_to_default(mode)
  components_to_default =
    case mode
    when 'access'
      AUTH_INFO_COMPONENT_ACCESS_MODE_DEFAULTS.keys
    when 'auth'
      AUTH_INFO_COMPONENT_AUTH_MODE_DEFAULTS.keys
    end
  components_to_default << 'encryption_algorithm'

  components_to_default
end

#default_component_from_definitions(component_object, mode) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/inferno/apps/cli/session/start_run.rb', line 181

def default_component_from_definitions(component_object, mode)
  component_object['auth_type'] = 'public' if component_object['auth_type'].blank?
  auth_type = component_object['auth_type']
  components_to_default = components_to_default(mode)

  components_to_default.each do |component|
    default_value = default_from_auth_info_component_definition(component, mode, auth_type)
    if (component_object[component].blank? || component_object[component] == '') && !default_value.blank?
      component_object[component] = default_value
    end
  end
end

#default_from_auth_info_component_definition(component, mode, auth_type) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/inferno/apps/cli/session/start_run.rb', line 230

def default_from_auth_info_component_definition(component, mode, auth_type)
  return 'public' if component == 'auth_type'
  return 'ES384' if component == 'encryption_algorithm' && ['backend_services',
                                                            'asymmetric'].include?(auth_type)

  case mode
  when 'auth'
    AUTH_INFO_COMPONENT_AUTH_MODE_DEFAULTS[component]
  when 'access'
    AUTH_INFO_COMPONENT_ACCESS_MODE_DEFAULTS[component]
  end
end

#default_from_runnable_components(component_object, runnable_input) ⇒ Object



194
195
196
197
198
199
200
201
# File 'lib/inferno/apps/cli/session/start_run.rb', line 194

def default_from_runnable_components(component_object, runnable_input)
  runnable_input.dig('options', 'components')&.each do |component|
    component_name = component['name']
    unless component_object.key?(component_name) || component['default'].blank?
      component_object[component_name] = component['default']
    end
  end
end

#find_target_runnableObject

runnable_to_find can be a complete internal id, an internal id suffix, or short id displayed in the UI. Use ‘suite’ to run the whole suite.



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
# File 'lib/inferno/apps/cli/session/start_run.rb', line 99

def find_target_runnable
  if options[:runnable].blank?
    error_object = { errors: 'No runnable specified. Use a group/test id or "suite" to run the whole suite.' }
    puts error_object.to_json
    exit(3)
  end

  target_runnable = options[:runnable] == 'suite' ? session_details['test_suite_id'] : options[:runnable]
  target_runnable = target_runnable.to_s unless target_runnable.is_a?(String)

  candidates = []
  runnable_search(session_details['test_suite'], target_runnable, candidates)
  if candidates.blank?
    error_object =
      { errors: "Runnable '#{target_runnable}' not found in suite '#{session_details['test_suite_id']}'" }
    puts error_object.to_json
    exit(3)
  elsif candidates.size > 1
    error_object =
      { errors: "Runnable '#{target_runnable}' not unique in suite '#{session_details['test_suite_id']}'" }
    puts error_object.to_json
    exit(3)
  end

  candidates.first
end

#input_run_value(runnable_input) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/inferno/apps/cli/session/start_run.rb', line 145

def input_run_value(runnable_input)
  input_name = runnable_input['name']
  value = session_value_for_input(session_inputs, input_name)
  value = user_inputs[input_name] if user_inputs[input_name].present?
  value = runnable_input['default'] if value == '' && runnable_input['default'].present?
  if runnable_input['type'] == 'auth_info'
    component_object = value == '' ? {} : JSON.parse(value)
    add_auth_info_component_defaults(component_object, runnable_input)
    value = component_object.to_json
  end

  { 'name' => input_name, 'value' => value }
end

#normalize_inputs(inputs) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/inferno/apps/cli/session/start_run.rb', line 79

def normalize_inputs(inputs)
  inputs.transform_values do |value|
    next value.to_json if value.is_a?(Array) || value.is_a?(Hash)
    next value unless value.to_s.start_with?('@')

    path = File.expand_path(value[1..])
    unless File.exist?(path)
      puts JSON.pretty_generate({ errors: "File input not found: #{path}" })
      exit(3)
    end
    File.read(path)
  end
end

#runObject



30
31
32
33
# File 'lib/inferno/apps/cli/session/start_run.rb', line 30

def run
  puts JSON.pretty_generate(start_run)
  exit(0)
end

#runnable_inputsObject



93
94
95
# File 'lib/inferno/apps/cli/session/start_run.rb', line 93

def runnable_inputs
  @runnable_inputs ||= calculate_inputs
end

#runnable_matches?(runnable_details, runnable_to_find) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
# File 'lib/inferno/apps/cli/session/start_run.rb', line 132

def runnable_matches?(runnable_details, runnable_to_find)
  runnable_details['id'] == runnable_to_find ||
    runnable_details['id']&.ends_with?("-#{runnable_to_find}") ||
    runnable_details['short_id'] == runnable_to_find
end

#runnable_search(runnable_details, runnable_to_find, matches) ⇒ Object



126
127
128
129
130
# File 'lib/inferno/apps/cli/session/start_run.rb', line 126

def runnable_search(runnable_details, runnable_to_find, matches)
  matches << runnable_details if runnable_matches?(runnable_details, runnable_to_find)
  runnable_details['test_groups']&.each { |group| runnable_search(group, runnable_to_find, matches) }
  runnable_details['tests']&.each { |test| runnable_search(test, runnable_to_find, matches) }
end

#session_detailsObject



49
50
51
# File 'lib/inferno/apps/cli/session/start_run.rb', line 49

def session_details
  @session_details ||= SessionDetails.new(session_id, options).details_for_session
end

#session_inputsObject



71
72
73
# File 'lib/inferno/apps/cli/session/start_run.rb', line 71

def session_inputs
  @session_inputs ||= SessionData.new(session_id, options).data_for_session(session_id)
end

#session_value_for_input(session_inputs, input_name) ⇒ Object



159
160
161
162
# File 'lib/inferno/apps/cli/session/start_run.rb', line 159

def session_value_for_input(session_inputs, input_name)
  session_input = session_inputs.find { |input| input['name'] == input_name }
  session_input&.dig('value') || ''
end

#start_runObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/inferno/apps/cli/session/start_run.rb', line 35

def start_run
  request_body = {
    test_session_id: session_id,
    "#{target_runnable_key}": target_runnable_id,
    inputs: runnable_inputs
  }

  response = post('api/test_runs', request_body.to_json, content_type: 'application/json')

  handle_web_api_error(response, :start_run) if response.status != 200

  JSON.parse(response.body)
end

#target_runnable_detailsObject



53
54
55
# File 'lib/inferno/apps/cli/session/start_run.rb', line 53

def target_runnable_details
  @target_runnable_details ||= find_target_runnable
end

#target_runnable_idObject



67
68
69
# File 'lib/inferno/apps/cli/session/start_run.rb', line 67

def target_runnable_id
  target_runnable_details['id']
end

#target_runnable_keyObject



57
58
59
60
61
62
63
64
65
# File 'lib/inferno/apps/cli/session/start_run.rb', line 57

def target_runnable_key
  if target_runnable_details.key?('suite_summary')
    'test_suite_id'
  elsif target_runnable_details.key?('run_as_group')
    'test_group_id'
  else
    'test_id'
  end
end

#user_inputsObject



75
76
77
# File 'lib/inferno/apps/cli/session/start_run.rb', line 75

def user_inputs
  @user_inputs ||= normalize_inputs(options[:inputs] || {})
end