Merge branch '8784-dir-listings'
[arvados.git] / services / api / test / unit / pipeline_instance_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class PipelineInstanceTest < ActiveSupport::TestCase
8
9   test "check active and success for a pipeline in new state" do
10     pi = pipeline_instances :new_pipeline
11
12     assert_equal 'New', pi.state, 'expected state to be New for :new_pipeline'
13
14     # save the pipeline and expect state to be New
15     Thread.current[:user] = users(:admin)
16
17     pi.save
18     pi = PipelineInstance.find_by_uuid 'zzzzz-d1hrv-f4gneyn6br1xize'
19     assert_equal PipelineInstance::New, pi.state, 'expected state to be New for new pipeline'
20   end
21
22   test "check active and success for a newly created pipeline" do
23     set_user_from_auth :active
24
25     pi = PipelineInstance.create(state: 'Ready')
26     pi.save
27
28     assert pi.valid?, 'expected newly created empty pipeline to be valid ' + pi.errors.messages.to_s
29     assert_equal 'Ready', pi.state, 'expected state to be Ready for a new empty pipeline'
30   end
31
32   test "update attributes for pipeline" do
33     Thread.current[:user] = users(:admin)
34
35     pi = pipeline_instances :new_pipeline
36
37     # add a component with no input and expect state to be New
38     component = {'script_parameters' => {"input_not_provided" => {"required" => true}}}
39     pi.components['first'] = component
40     components = pi.components
41     pi.update_attribute 'components', pi.components
42     pi = PipelineInstance.find_by_uuid 'zzzzz-d1hrv-f4gneyn6br1xize'
43     assert_equal PipelineInstance::New, pi.state, 'expected state to be New after adding component with input'
44     assert_equal pi.components.size, 1, 'expected one component'
45     assert_nil pi.started_at, 'expected started_at to be nil on new pipeline instance'
46     assert_nil pi.finished_at, 'expected finished_at to be nil on new pipeline instance'
47
48     # add a component with no input not required
49     component = {'script_parameters' => {"input_not_provided" => {"required" => false}}}
50     pi.components['first'] = component
51     components = pi.components
52     pi.update_attribute 'components', pi.components
53     pi = PipelineInstance.find_by_uuid 'zzzzz-d1hrv-f4gneyn6br1xize'
54     assert_equal PipelineInstance::Ready, pi.state, 'expected state to be Ready after adding component with input'
55     assert_equal pi.components.size, 1, 'expected one component'
56
57     # add a component with input and expect state to become Ready
58     component = {'script_parameters' => {"input" => "yyyad4b39ca5a924e481008009d94e32+210"}}
59     pi.components['first'] = component
60     components = pi.components
61     pi.update_attribute 'components', pi.components
62     pi = PipelineInstance.find_by_uuid 'zzzzz-d1hrv-f4gneyn6br1xize'
63     assert_equal PipelineInstance::Ready, pi.state, 'expected state to be Ready after adding component with input'
64     assert_equal pi.components.size, 1, 'expected one component'
65
66     pi.state = PipelineInstance::RunningOnServer
67     pi.save
68     pi = PipelineInstance.find_by_uuid 'zzzzz-d1hrv-f4gneyn6br1xize'
69     assert_equal PipelineInstance::RunningOnServer, pi.state, 'expected state to be RunningOnServer after updating state to RunningOnServer'
70     assert_not_nil pi.started_at, 'expected started_at to have a value on a running pipeline instance'
71     assert_nil pi.finished_at, 'expected finished_at to be nil on a running pipeline instance'
72
73     pi.state = PipelineInstance::Paused
74     pi.save
75     pi = PipelineInstance.find_by_uuid 'zzzzz-d1hrv-f4gneyn6br1xize'
76     assert_equal PipelineInstance::Paused, pi.state, 'expected state to be Paused after updating state to Paused'
77
78     pi.state = PipelineInstance::Complete
79     pi.save
80     pi = PipelineInstance.find_by_uuid 'zzzzz-d1hrv-f4gneyn6br1xize'
81     assert_equal PipelineInstance::Complete, pi.state, 'expected state to be Complete after updating state to Complete'
82     assert_not_nil pi.started_at, 'expected started_at to have a value on a completed pipeline instance'
83     assert_not_nil pi.finished_at, 'expected finished_at to have a value on a completed pipeline instance'
84
85     pi.state = 'bogus'
86     pi.save
87     pi = PipelineInstance.find_by_uuid 'zzzzz-d1hrv-f4gneyn6br1xize'
88     assert_equal PipelineInstance::Complete, pi.state, 'expected state to be unchanged with set to a bogus value'
89
90     pi.state = PipelineInstance::Failed
91     pi.save
92     pi = PipelineInstance.find_by_uuid 'zzzzz-d1hrv-f4gneyn6br1xize'
93     assert_equal PipelineInstance::Failed, pi.state, 'expected state to be Failed after updating state to Failed'
94     assert_not_nil pi.started_at, 'expected started_at to have a value on a failed pipeline instance'
95     assert_not_nil pi.finished_at, 'expected finished_at to have a value on a failed pipeline instance'
96   end
97
98   test "update attributes for pipeline with two components" do
99     pi = pipeline_instances :new_pipeline
100
101     # add two components, one with input and one with no input and expect state to be New
102     component1 = {'script_parameters' => {"something" => "xxxad4b39ca5a924e481008009d94e32+210", "input" => "c1bad4b39ca5a924e481008009d94e32+210"}}
103     component2 = {'script_parameters' => {"something_else" => "xxxad4b39ca5a924e481008009d94e32+210", "input_missing" => {"required" => true}}}
104     pi.components['first'] = component1
105     pi.components['second'] = component2
106
107     Thread.current[:user] = users(:admin)
108     pi.update_attribute 'components', pi.components
109
110     pi = PipelineInstance.find_by_uuid 'zzzzz-d1hrv-f4gneyn6br1xize'
111     assert_equal PipelineInstance::New, pi.state, 'expected state to be New after adding component with input'
112     assert_equal pi.components.size, 2, 'expected two components'
113   end
114
115   [:has_component_with_no_script_parameters,
116    :has_component_with_empty_script_parameters].each do |pi_name|
117     test "update pipeline that #{pi_name}" do
118       pi = pipeline_instances pi_name
119
120       Thread.current[:user] = users(:active)
121       assert_equal PipelineInstance::Ready, pi.state
122     end
123   end
124 end