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