Merge branch 'master' into 4156-cli-tests
[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     skip "TBD"
23
24     # Add a single tag.
25     tag_uuid, err = capture_subprocess_io do
26       assert arv_tag '--short', 'add', 'test_tag1', '--object', 'uuid1'
27     end
28     assert_empty err
29
30     out, err = capture_subprocess_io do
31       assert arv 'link', 'show', '--uuid', tag_uuid.rstrip
32     end
33
34     assert_empty err
35     link = JSON.parse out
36     assert_tag link, 'test_tag1', 'uuid1'
37
38     # Remove the tag.
39     out, err = capture_subprocess_io do
40       assert arv_tag 'remove', 'test_tag1', '--object', 'uuid1'
41     end
42
43     assert_empty err
44     links = JSON.parse out
45     assert_equal 1, links.length
46     assert_tag links[0], 'test_tag1', 'uuid1'
47
48     # Verify that the link no longer exists.
49     out, err = capture_subprocess_io do
50       assert_equal false, arv('link', 'show', '--uuid', links[0]['uuid'])
51     end
52
53     assert_equal "Error: Path not found\n", err
54   end
55
56   # Test adding and removing a single tag with multiple objects.
57   def test_single_tag_multi_objects
58     skip "TBD"
59
60     out, err = capture_subprocess_io do
61       assert arv_tag('add', 'test_tag1',
62                      '--object', 'uuid1',
63                      '--object', 'uuid2',
64                      '--object', 'uuid3')
65     end
66     assert_empty err
67
68     out, err = capture_subprocess_io do
69       assert arv 'link', 'list', '--where', '{"link_class":"tag","name":"test_tag1"}'
70     end
71
72     assert_empty err
73     json_out = JSON.parse out
74     links = json_out['items'].sort { |a,b| a['head_uuid'] <=> b['head_uuid'] }
75     assert_equal 3, links.length
76     assert_tag links[0], 'test_tag1', 'uuid1'
77     assert_tag links[1], 'test_tag1', 'uuid2'
78     assert_tag links[2], 'test_tag1', 'uuid3'
79
80     out, err = capture_subprocess_io do
81       assert arv_tag('remove', 'test_tag1',
82                      '--object', 'uuid1',
83                      '--object', 'uuid2',
84                      '--object', 'uuid3')
85     end
86     assert_empty err
87
88     out, err = capture_subprocess_io do
89       assert arv 'link', 'list', '--where', '{"link_class":"tag","name":"test_tag1"}'
90     end
91
92     assert_empty err
93     assert_empty out
94   end
95
96   protected
97   def arv_tag(*args)
98     system ['./bin/arv-tag', 'arv-tag'], *args
99   end
100
101   def arv(*args)
102     system ['./bin/arv', 'arv'], *args
103   end
104
105   def assert_tag(link, name, head_uuid)
106     assert_equal 'tag',     link['link_class']
107     assert_equal name,      link['name']
108     assert_equal head_uuid, link['head_uuid']
109   end
110 end