4031: Refresh provenance helper graph generation to fix bugs and make better
[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     ['component_zzzzz-d1hrv-9fm8l10i9z2kqc9_stage1',
88      'component_zzzzz-d1hrv-9fm8l10i9z2kqc9_stage2',
89      'zzzzz-8i9sb-graphstage10000',
90      'zzzzz-8i9sb-graphstage20000',
91      'b519d9cb706a29fc7ea24dbea2f05851+93',
92      'fa7aeb5140e2848d39b416daeef4ffc5+45',
93      'zzzzz-4zz18-bv31uwvy3neko22',
94      'zzzzz-4zz18-uukreo9rbgwsujx'].each do |k|
95
96       assert_not_nil provenance[k], "Expected key #{k} in provenance set"
97       assert_equal 1, pips[k], "Expected key #{k} in pips set" if !k.start_with? "component_"
98     end
99
100     prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
101         :request => RequestDuck,
102         :all_script_parameters => true,
103         :combine_jobs => :script_and_version,
104         :pips => pips,
105         :only_components => true }
106
107     # hash -> owned_by_active
108     assert /hash_4fe459abe02d9b365932b8f5dc419439ab4e2577_99914b932bd37a50b983c5e7c90ae93b&#45;&gt;fa7aeb5140e2848d39b416daeef4ffc5\+45/.match(prov_svg)
109
110     # owned_by_active -> hash2
111     assert /fa7aeb5140e2848d39b416daeef4ffc5\+45&#45;&gt;hash2_4fe459abe02d9b365932b8f5dc419439ab4e2577_4900033ec5cfaf8a63566f3664aeaa70/.match(prov_svg)
112
113     #File::open "./tmp/stuff1.svg", "w" do |f|
114     #  f.write "<?xml version=\"1.0\" ?>\n"
115     #  f.write prov_svg
116     #end
117
118   end
119
120   test "generate graph compare" do
121
122     use_token 'admin'
123
124     pipeline_for_graph1 = {
125       state: 'Complete',
126       uuid: 'zzzzz-d1hrv-9fm8l10i9z2kqc9',
127       components: {
128         stage1: {
129           repository: 'foo',
130           script: 'hash',
131           script_version: 'master',
132           job: {uuid: 'zzzzz-8i9sb-graphstage10000'},
133           output_uuid: 'zzzzz-4zz18-bv31uwvy3neko22'
134         },
135         stage2: {
136           repository: 'foo',
137           script: 'hash2',
138           script_version: 'master',
139           script_parameters: {
140             input: 'fa7aeb5140e2848d39b416daeef4ffc5+45'
141           },
142           job: {uuid: 'zzzzz-8i9sb-graphstage20000'},
143           output_uuid: 'zzzzz-4zz18-uukreo9rbgwsujx'
144         }
145       }
146     }
147
148     pipeline_for_graph2 = {
149       state: 'Complete',
150       uuid: 'zzzzz-d1hrv-9fm8l10i9z2kqc0',
151       components: {
152         stage1: {
153           repository: 'foo',
154           script: 'hash',
155           script_version: 'master',
156           job: {uuid: 'zzzzz-8i9sb-graphstage10000'},
157           output_uuid: 'zzzzz-4zz18-bv31uwvy3neko22'
158         },
159         stage2: {
160           repository: 'foo',
161           script: 'hash2',
162           script_version: 'master',
163           script_parameters: {
164           },
165           job: {uuid: 'zzzzz-8i9sb-graphstage30000'},
166           output_uuid: 'zzzzz-4zz18-uukreo9rbgwsujj'
167         }
168       }
169     }
170
171     @controller.params['tab_pane'] = "Graph"
172     provenance, pips = @controller.graph([pipeline_for_graph1, pipeline_for_graph2])
173
174     [['component_zzzzz-d1hrv-9fm8l10i9z2kqc9_stage1', nil],
175      ['component_zzzzz-d1hrv-9fm8l10i9z2kqc9_stage2', nil],
176      ['component_zzzzz-d1hrv-9fm8l10i9z2kqc0_stage1', nil],
177      ['component_zzzzz-d1hrv-9fm8l10i9z2kqc0_stage2', nil],
178      ['zzzzz-8i9sb-graphstage10000', 3],
179      ['zzzzz-8i9sb-graphstage20000', 1],
180      ['zzzzz-8i9sb-graphstage30000', 2],
181      ['b519d9cb706a29fc7ea24dbea2f05851+93', 1],
182      ['fa7aeb5140e2848d39b416daeef4ffc5+45', 3],
183      ['ea10d51bcf88862dbcc36eb292017dfd+45', 2],
184      ['zzzzz-4zz18-bv31uwvy3neko22', 3],
185      ['zzzzz-4zz18-uukreo9rbgwsujx', 1],
186      ['zzzzz-4zz18-uukreo9rbgwsujj', 2]
187     ].each do |k|
188       assert_not_nil provenance[k[0]], "Expected key #{k[0]} in provenance set"
189       assert_equal k[1], pips[k[0]], "Expected key #{k} in pips" if !k[0].start_with? "component_"
190     end
191
192     prov_svg = ProvenanceHelper::create_provenance_graph provenance, "provenance_svg", {
193         :request => RequestDuck,
194         :all_script_parameters => true,
195         :combine_jobs => :script_and_version,
196         :pips => pips,
197         :only_components => true }
198
199     # owned_by_active -> hash2 (stuff)
200     assert /fa7aeb5140e2848d39b416daeef4ffc5\+45&#45;&gt;hash2_4fe459abe02d9b365932b8f5dc419439ab4e2577_4900033ec5cfaf8a63566f3664aeaa70/.match(prov_svg)
201
202     # owned_by_active -> hash2 (stuff2)
203     assert /fa7aeb5140e2848d39b416daeef4ffc5\+45&#45;&gt;hash2_4fe459abe02d9b365932b8f5dc419439ab4e2577_02a085407e751d00b5dc88f1bd5e8247/.match(prov_svg)
204
205     # hash2 (stuff) -> GPL
206     assert /hash2_4fe459abe02d9b365932b8f5dc419439ab4e2577_4900033ec5cfaf8a63566f3664aeaa70&#45;&gt;b519d9cb706a29fc7ea24dbea2f05851\+93/.match(prov_svg)
207
208     # hash2 (stuff2) -> baz file
209     assert /hash2_4fe459abe02d9b365932b8f5dc419439ab4e2577_02a085407e751d00b5dc88f1bd5e8247&#45;&gt;ea10d51bcf88862dbcc36eb292017dfd\+45/.match(prov_svg)
210
211     # File::open "./tmp/stuff2.svg", "w" do |f|
212     #   f.write "<?xml version=\"1.0\" ?>\n"
213     #   f.write prov_svg
214     # end
215
216   end
217
218 end