Class: Inferno::Web::Controllers::TestRuns::Create

Inherits:
Controller
  • Object
show all
Defined in:
lib/inferno/apps/web/controllers/test_runs/create.rb

Constant Summary collapse

PARAMS =
[:test_session_id, :test_suite_id, :test_group_id, :test_id].freeze

Instance Method Summary collapse

Methods inherited from Controller

call, resource_class, resource_name

Instance Method Details

#create_params(params) ⇒ Object



75
76
77
# File 'lib/inferno/apps/web/controllers/test_runs/create.rb', line 75

def create_params(params)
  params.to_h.slice(*PARAMS)
end

#handle(req, res) ⇒ Object



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
# File 'lib/inferno/apps/web/controllers/test_runs/create.rb', line 45

def handle(req, res)
  test_session = test_sessions_repo.find(req.params[:test_session_id])

  # if testsession.nil?
  if test_runs_repo.active_test_run_for_session?(test_session.id)
    halt 409, { error: 'Cannot run new test while another test run is in progress' }.to_json
  end

  verify_runnable(
    repo.build_entity(create_params(req.params)).runnable,
    req.params[:inputs],
    test_session.suite_options
  )

  test_run = repo.create(create_params(req.params).merge(status: 'queued'))

  res.body = serialize(test_run, suite_options: test_session.suite_options)

  persist_inputs(req.params, test_run)

  Jobs.perform(Jobs::ExecuteTestRun, test_run.id)
rescue Sequel::ValidationFailed, Sequel::ForeignKeyConstraintViolation,
       Inferno::Exceptions::RequiredInputsNotFound,
       Inferno::Exceptions::NotUserRunnableException => e
  halt 422, { errors: e.message }.to_json
rescue StandardError => e
  Application['logger'].error(e.full_message)
  halt 500, { errors: e.message }.to_json
end

#persist_inputs(params, test_run) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/inferno/apps/web/controllers/test_runs/create.rb', line 21

def persist_inputs(params, test_run)
  available_inputs = test_run.runnable.available_inputs
  params[:inputs]&.each do |input_params|
    input =
      available_inputs
        .find { |_, runnable_input| runnable_input.name == input_params[:name] }
        &.last

    if input.nil?
      Inferno::Application['logger'].warn(
        "Unknown input `#{input_params[:name]}` for #{test_run.runnable.id}: #{test_run.runnable.title}"
      )
      next
    end

    session_data_repo.save(
      test_session_id: test_run.test_session_id,
      name: input.name,
      value: input_params[:value],
      type: input.type
    )
  end
end

#verify_runnable(runnable, inputs, selected_suite_options) ⇒ Object



14
15
16
17
18
19
# File 'lib/inferno/apps/web/controllers/test_runs/create.rb', line 14

def verify_runnable(runnable, inputs, selected_suite_options)
  missing_inputs = runnable&.missing_inputs(inputs, selected_suite_options)
  user_runnable = runnable&.user_runnable?
  raise Inferno::Exceptions::RequiredInputsNotFound, missing_inputs if missing_inputs&.any?
  raise Inferno::Exceptions::NotUserRunnableException unless user_runnable
end