Merge branch 'master' into 4024-pipeline-instances-scroll
[arvados.git] / apps / workbench / test / functional / pipeline_instances_controller_test.rb
1 require 'test_helper'
2
3 class PipelineInstancesControllerTest < ActionController::TestCase
4   def create_instance_long_enough_to(instance_attrs={})
5     # create 'two_part' pipeline with the given instance attributes
6     pt_fixture = api_fixture('pipeline_templates')['two_part']
7     post :create, {
8       pipeline_instance: instance_attrs.merge({
9         pipeline_template_uuid: pt_fixture['uuid']
10       }),
11       format: :json
12     }, session_for(:active)
13     assert_response :success
14     pi_uuid = assigns(:object).uuid
15     assert_not_nil assigns(:object)
16
17     # yield
18     yield pi_uuid, pt_fixture
19
20     # delete the pipeline instance
21     use_token :active
22     PipelineInstance.where(uuid: pi_uuid).first.destroy
23   end
24
25   test "pipeline instance components populated after create" do
26     create_instance_long_enough_to do |new_instance_uuid, template_fixture|
27       assert_equal(template_fixture['components'].to_json,
28                    assigns(:object).components.to_json)
29     end
30   end
31
32   test "can render pipeline instance with tagged collections" do
33     # Make sure to pass in a tagged collection to test that part of the rendering behavior.
34     get(:show,
35         {id: api_fixture("pipeline_instances")["pipeline_with_tagged_collection_input"]["uuid"]},
36         session_for(:active))
37     assert_response :success
38   end
39
40   test "update script_parameters one at a time using merge param" do
41       template_fixture = api_fixture('pipeline_templates')['two_part']
42       post :update, {
43         id: api_fixture("pipeline_instances")["pipeline_to_merge_params"]["uuid"],
44         pipeline_instance: {
45           components: {
46             "part-two" => {
47               script_parameters: {
48                 integer_with_value: {
49                   value: 9
50                 },
51                 plain_string: {
52                   value: 'quux'
53                 },
54               }
55             }
56           }
57         },
58         merge: true,
59         format: :json
60       }, session_for(:active)
61       assert_response :success
62       assert_not_nil assigns(:object)
63       orig_params = template_fixture['components']['part-two']['script_parameters']
64       new_params = assigns(:object).components[:'part-two'][:script_parameters]
65       orig_params.keys.each do |k|
66         unless %w(integer_with_value plain_string).index(k)
67           assert_equal orig_params[k].to_json, new_params[k.to_sym].to_json
68         end
69       end
70   end
71
72   test "component rendering copes with unexpeceted components format" do
73     get(:show,
74         {id: api_fixture("pipeline_instances")["components_is_jobspec"]["uuid"]},
75         session_for(:active))
76     assert_response :success
77   end
78
79   test "dates in JSON components are parsed" do
80     get(:show,
81         {id: api_fixture('pipeline_instances')['has_component_with_completed_jobs']['uuid']},
82         session_for(:active))
83     assert_response :success
84     assert_not_nil assigns(:object)
85     assert_not_nil assigns(:object).components[:foo][:job]
86     assert assigns(:object).components[:foo][:job][:started_at].is_a? Time
87     assert assigns(:object).components[:foo][:job][:finished_at].is_a? Time
88   end
89
90   # The next two tests ensure that a pipeline instance can be copied
91   # when the template has components that do not exist in the
92   # instance (ticket #4000).
93
94   test "copy pipeline instance with components=use_latest" do
95     post(:copy,
96          {
97            id: api_fixture('pipeline_instances')['pipeline_with_newer_template']['uuid'],
98            components: 'use_latest',
99            script: 'use_latest',
100            pipeline_instance: {
101              state: 'RunningOnServer'
102            }
103          },
104          session_for(:active))
105     assert_response 302
106     assert_not_nil assigns(:object)
107
108     # Component 'foo' has script parameters only in the pipeline instance.
109     # Component 'bar' is present only in the pipeline_template.
110     # Test that the copied pipeline instance includes parameters for
111     # component 'foo' from the source instance, and parameters for
112     # component 'bar' from the source template.
113     #
114     assert_not_nil assigns(:object).components[:foo]
115     foo = assigns(:object).components[:foo]
116     assert_not_nil foo[:script_parameters]
117     assert_not_nil foo[:script_parameters][:input]
118     assert_equal 'foo instance input', foo[:script_parameters][:input][:title]
119
120     assert_not_nil assigns(:object).components[:bar]
121     bar = assigns(:object).components[:bar]
122     assert_not_nil bar[:script_parameters]
123     assert_not_nil bar[:script_parameters][:input]
124     assert_equal 'bar template input', bar[:script_parameters][:input][:title]
125   end
126
127   test "copy pipeline instance on newer template works with script=use_same" do
128     post(:copy,
129          {
130            id: api_fixture('pipeline_instances')['pipeline_with_newer_template']['uuid'],
131            components: 'use_latest',
132            script: 'use_same',
133            pipeline_instance: {
134              state: 'RunningOnServer'
135            }
136          },
137          session_for(:active))
138     assert_response 302
139     assert_not_nil assigns(:object)
140
141     # Test that relevant component parameters were copied from both
142     # the source instance and source template, respectively (see
143     # previous test)
144     #
145     assert_not_nil assigns(:object).components[:foo]
146     foo = assigns(:object).components[:foo]
147     assert_not_nil foo[:script_parameters]
148     assert_not_nil foo[:script_parameters][:input]
149     assert_equal 'foo instance input', foo[:script_parameters][:input][:title]
150
151     assert_not_nil assigns(:object).components[:bar]
152     bar = assigns(:object).components[:bar]
153     assert_not_nil bar[:script_parameters]
154     assert_not_nil bar[:script_parameters][:input]
155     assert_equal 'bar template input', bar[:script_parameters][:input][:title]
156   end
157 end