Merge remote-tracking branch 'origin/master' into 4031-fix-graph-connections
[arvados.git] / apps / workbench / test / controllers / pipeline_instances_controller_test.rb
1 require 'test_helper'
2
3 class PipelineInstancesControllerTest < ActionController::TestCase
4   include PipelineInstancesHelper
5
6   test "one" do
7     r = [{started_at: 1, finished_at: 3}]
8     assert_equal 2, determine_wallclock_runtime(r)
9
10     r = [{started_at: 1, finished_at: 5}]
11     assert_equal 4, determine_wallclock_runtime(r)
12
13     r = [{started_at: 1, finished_at: 2}, {started_at: 3, finished_at: 5}]
14     assert_equal 3, determine_wallclock_runtime(r)
15
16     r = [{started_at: 3, finished_at: 5}, {started_at: 1, finished_at: 2}]
17     assert_equal 3, determine_wallclock_runtime(r)
18
19     r = [{started_at: 3, finished_at: 5}, {started_at: 1, finished_at: 2},
20          {started_at: 2, finished_at: 4}]
21     assert_equal 4, determine_wallclock_runtime(r)
22
23     r = [{started_at: 1, finished_at: 5}, {started_at: 2, finished_at: 3}]
24     assert_equal 4, determine_wallclock_runtime(r)
25
26     r = [{started_at: 3, finished_at: 5}, {started_at: 1, finished_at: 4}]
27     assert_equal 4, determine_wallclock_runtime(r)
28
29     r = [{started_at: 1, finished_at: 4}, {started_at: 3, finished_at: 5}]
30     assert_equal 4, determine_wallclock_runtime(r)
31
32     r = [{started_at: 1, finished_at: 4}, {started_at: 3, finished_at: 5},
33          {started_at: 5, finished_at: 8}]
34     assert_equal 7, determine_wallclock_runtime(r)
35
36     r = [{started_at: 1, finished_at: 4}, {started_at: 3, finished_at: 5},
37          {started_at: 6, finished_at: 8}]
38     assert_equal 6, determine_wallclock_runtime(r)
39   end
40
41
42   class RequestDuck
43     def self.host
44       "localhost"
45     end
46
47     def self.port
48       8080
49     end
50
51     def self.protocol
52       "http"
53     end
54   end
55
56   test "generate graph" do
57
58     use_token 'admin'
59
60     pipeline_for_graph = {
61       state: 'Complete',
62       uuid: 'zzzzz-d1hrv-9fm8l10i9z2kqc9',
63       components: {
64         stage1: {
65           repository: 'foo',
66           script: 'hash',
67           script_version: 'master',
68           job: {uuid: 'zzzzz-8i9sb-graphstage10000'},
69           output_uuid: 'zzzzz-4zz18-bv31uwvy3neko22'
70         },
71         stage2: {
72           repository: 'foo',
73           script: 'hash2',
74           script_version: 'master',
75           script_parameters: {
76             input: 'fa7aeb5140e2848d39b416daeef4ffc5+45'
77           },
78           job: {uuid: 'zzzzz-8i9sb-graphstage20000'},
79           output_uuid: 'zzzzz-4zz18-uukreo9rbgwsujx'
80         }
81       }
82     }
83
84     @controller.params['tab_pane'] = "Graph"
85     provenance, pips = @controller.graph([pipeline_for_graph])
86
87     graph_test_collection1 = find_fixture Collection, "graph_test_collection1"
88     stage1 = find_fixture Job, "graph_stage1"
89     stage2 = find_fixture Job, "graph_stage2"
90
91     ['component_zzzzz-d1hrv-9fm8l10i9z2kqc9_stage1',
92      'component_zzzzz-d1hrv-9fm8l10i9z2kqc9_stage2',
93      stage1.uuid,
94      stage2.uuid,
95      stage1.output,
96      stage2.output,
97      pipeline_for_graph[:components][:stage1][:output_uuid],
98      pipeline_for_graph[:components][:stage2][:output_uuid]
99     ].each do |k|
100
101       assert_not_nil provenance[k], "Expected key #{k} in provenance set"
102       assert_equal 1, pips[k], "Expected key #{k} in pips set" if !k.start_with? "component_"
103     end
104
105     prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
106         :request => RequestDuck,
107         :all_script_parameters => true,
108         :combine_jobs => :script_and_version,
109         :pips => pips,
110         :only_components => true }
111
112     stage1_id = "#{stage1[:script]}_#{stage1[:script_version]}_#{Digest::MD5.hexdigest(stage1[:script_parameters].to_json)}"
113     stage2_id = "#{stage2[:script]}_#{stage2[:script_version]}_#{Digest::MD5.hexdigest(stage2[:script_parameters].to_json)}"
114
115     stage1_out = stage1[:output].gsub('+','\\\+')
116
117     assert_match /#{stage1_id}&#45;&gt;#{stage1_out}/, prov_svg
118
119     assert_match /#{stage1_out}&#45;&gt;#{stage2_id}/, prov_svg
120
121   end
122
123   test "generate graph compare" do
124
125     use_token 'admin'
126
127     pipeline_for_graph1 = {
128       state: 'Complete',
129       uuid: 'zzzzz-d1hrv-9fm8l10i9z2kqc9',
130       components: {
131         stage1: {
132           repository: 'foo',
133           script: 'hash',
134           script_version: 'master',
135           job: {uuid: 'zzzzz-8i9sb-graphstage10000'},
136           output_uuid: 'zzzzz-4zz18-bv31uwvy3neko22'
137         },
138         stage2: {
139           repository: 'foo',
140           script: 'hash2',
141           script_version: 'master',
142           script_parameters: {
143             input: 'fa7aeb5140e2848d39b416daeef4ffc5+45'
144           },
145           job: {uuid: 'zzzzz-8i9sb-graphstage20000'},
146           output_uuid: 'zzzzz-4zz18-uukreo9rbgwsujx'
147         }
148       }
149     }
150
151     pipeline_for_graph2 = {
152       state: 'Complete',
153       uuid: 'zzzzz-d1hrv-9fm8l10i9z2kqc0',
154       components: {
155         stage1: {
156           repository: 'foo',
157           script: 'hash',
158           script_version: 'master',
159           job: {uuid: 'zzzzz-8i9sb-graphstage10000'},
160           output_uuid: 'zzzzz-4zz18-bv31uwvy3neko22'
161         },
162         stage2: {
163           repository: 'foo',
164           script: 'hash2',
165           script_version: 'master',
166           script_parameters: {
167           },
168           job: {uuid: 'zzzzz-8i9sb-graphstage30000'},
169           output_uuid: 'zzzzz-4zz18-uukreo9rbgwsujj'
170         }
171       }
172     }
173
174     @controller.params['tab_pane'] = "Graph"
175     provenance, pips = @controller.graph([pipeline_for_graph1, pipeline_for_graph2])
176
177     collection1 = find_fixture Collection, "graph_test_collection1"
178
179     stage1 = find_fixture Job, "graph_stage1"
180     stage2 = find_fixture Job, "graph_stage2"
181     stage3 = find_fixture Job, "graph_stage3"
182
183     [['component_zzzzz-d1hrv-9fm8l10i9z2kqc9_stage1', nil],
184      ['component_zzzzz-d1hrv-9fm8l10i9z2kqc9_stage2', nil],
185      ['component_zzzzz-d1hrv-9fm8l10i9z2kqc0_stage1', nil],
186      ['component_zzzzz-d1hrv-9fm8l10i9z2kqc0_stage2', nil],
187      [stage1.uuid, 3],
188      [stage2.uuid, 1],
189      [stage3.uuid, 2],
190      [stage1.output, 3],
191      [stage2.output, 1],
192      [stage3.output, 2],
193      [pipeline_for_graph1[:components][:stage1][:output_uuid], 3],
194      [pipeline_for_graph1[:components][:stage2][:output_uuid], 1],
195      [pipeline_for_graph2[:components][:stage2][:output_uuid], 2]
196     ].each do |k|
197       assert_not_nil provenance[k[0]], "Expected key #{k[0]} in provenance set"
198       assert_equal k[1], pips[k[0]], "Expected key #{k} in pips" if !k[0].start_with? "component_"
199     end
200
201     prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
202         :request => RequestDuck,
203         :all_script_parameters => true,
204         :combine_jobs => :script_and_version,
205         :pips => pips,
206         :only_components => true }
207
208     collection1_id = collection1.portable_data_hash.gsub('+','\\\+')
209
210     stage2_id = "#{stage2[:script]}_#{stage2[:script_version]}_#{Digest::MD5.hexdigest(stage2[:script_parameters].to_json)}"
211     stage3_id = "#{stage3[:script]}_#{stage3[:script_version]}_#{Digest::MD5.hexdigest(stage3[:script_parameters].to_json)}"
212
213     stage2_out = stage2[:output].gsub('+','\\\+')
214     stage3_out = stage3[:output].gsub('+','\\\+')
215
216     assert_match /#{collection1_id}&#45;&gt;#{stage2_id}/, prov_svg
217     assert_match /#{collection1_id}&#45;&gt;#{stage3_id}/, prov_svg
218
219     assert_match /#{stage2_id}&#45;&gt;#{stage2_out}/, prov_svg
220     assert_match /#{stage3_id}&#45;&gt;#{stage3_out}/, prov_svg
221
222   end
223
224 end