Merge remote-tracking branch 'origin/master' into 2884-improved-picker
[arvados.git] / sdk / cli / test / test_arv-tag.rb
1 require 'minitest/autorun'
2 require 'digest/md5'
3 require 'json'
4
5 def assert_failure *args
6   assert_equal false, *args
7 end
8
9 class TestArvTag < Minitest::Test
10
11   def test_no_args
12     # arv-tag exits with failure if run with no args
13     out, err = capture_subprocess_io do
14       assert_equal false, arv_tag
15     end
16     assert_empty out
17     assert_match /^usage:/i, err
18   end
19
20   # Test adding and removing a single tag on a single object.
21   def test_single_tag_single_obj
22     # Add a single tag.
23     tag_uuid, err = capture_subprocess_io do
24       assert arv_tag '--short', 'add', 'test_tag1', '--object', 'uuid1'
25     end
26     assert_empty err
27
28     out, err = capture_subprocess_io do
29       assert arv 'link', 'show', '--uuid', tag_uuid.rstrip
30     end
31
32     assert_empty err
33     link = JSON.parse out
34     assert_tag link, 'test_tag1', 'uuid1'
35
36     # Remove the tag.
37     out, err = capture_subprocess_io do
38       assert arv_tag 'remove', 'test_tag1', '--object', 'uuid1'
39     end
40
41     assert_empty err
42     links = JSON.parse out
43     assert_equal 1, links.length
44     assert_tag links[0], 'test_tag1', 'uuid1'
45
46     # Verify that the link no longer exists.
47     out, err = capture_subprocess_io do
48       assert_equal false, arv('link', 'show', '--uuid', links[0]['uuid'])
49     end
50
51     assert_equal "Error: Path not found\n", err
52   end
53
54   # Test adding and removing a single tag with multiple objects.
55   def test_single_tag_multi_objects
56     out, err = capture_subprocess_io do
57       assert arv_tag('add', 'test_tag1',
58                      '--object', 'uuid1',
59                      '--object', 'uuid2',
60                      '--object', 'uuid3')
61     end
62     assert_empty err
63
64     out, err = capture_subprocess_io do
65       assert arv 'link', 'list', '--where', '{"link_class":"tag","name":"test_tag1"}'
66     end
67
68     assert_empty err
69     json_out = JSON.parse out
70     links = json_out['items'].sort { |a,b| a['head_uuid'] <=> b['head_uuid'] }
71     assert_equal 3, links.length
72     assert_tag links[0], 'test_tag1', 'uuid1'
73     assert_tag links[1], 'test_tag1', 'uuid2'
74     assert_tag links[2], 'test_tag1', 'uuid3'
75
76     out, err = capture_subprocess_io do
77       assert arv_tag('remove', 'test_tag1',
78                      '--object', 'uuid1',
79                      '--object', 'uuid2',
80                      '--object', 'uuid3')
81     end
82     assert_empty err
83
84     out, err = capture_subprocess_io do
85       assert arv 'link', 'list', '--where', '{"link_class":"tag","name":"test_tag1"}'
86     end
87
88     assert_empty err
89     assert_empty out
90   end
91
92   protected
93   def arv_tag(*args)
94     system ['./bin/arv-tag', 'arv-tag'], *args
95   end
96
97   def arv(*args)
98     system ['./bin/arv', 'arv'], *args
99   end
100
101   def assert_tag(link, name, head_uuid)
102     assert_equal 'tag',     link['link_class']
103     assert_equal name,      link['name']
104     assert_equal head_uuid, link['head_uuid']
105   end
106 end