8784: Fix test for latest firefox.
[arvados.git] / sdk / cli / test / test_arv-keep-put.rb
1 require 'minitest/autorun'
2 require 'digest/md5'
3
4 class TestArvKeepPut < Minitest::Test
5   def setup
6     begin Dir.mkdir './tmp' rescue Errno::EEXIST end
7     begin Dir.mkdir './tmp/empty_dir' rescue Errno::EEXIST end
8     File.open './tmp/empty_file', 'wb' do
9     end
10     File.open './tmp/foo', 'wb' do |f|
11       f.write 'foo'
12     end
13   end
14
15   def test_help
16     out, err = capture_subprocess_io do
17       assert arv_put('-h'), 'arv-put -h exits zero'
18     end
19     $stderr.write err
20     assert_empty err
21     assert_match /^usage:/, out
22   end
23
24   def test_raw_stdin
25     out, err = capture_subprocess_io do
26       r,w = IO.pipe
27       wpid = fork do
28         r.close
29         w << 'foo'
30       end
31       w.close
32       assert arv_put('--raw', {in: r})
33       r.close
34       Process.waitpid wpid
35     end
36     $stderr.write err
37     assert_match '', err
38     assert_equal "acbd18db4cc2f85cedef654fccc4a4d8+3\n", out
39   end
40
41   def test_raw_file
42     out, err = capture_subprocess_io do
43       assert arv_put('--no-cache', '--raw', './tmp/foo')
44     end
45     $stderr.write err
46     assert_match '', err
47     assert_equal "acbd18db4cc2f85cedef654fccc4a4d8+3\n", out
48   end
49
50   def test_raw_empty_file
51     out, err = capture_subprocess_io do
52       assert arv_put('--raw', './tmp/empty_file')
53     end
54     $stderr.write err
55     assert_match '', err
56     assert_equal "d41d8cd98f00b204e9800998ecf8427e+0\n", out
57   end
58
59   def test_filename_arg_with_directory
60     out, err = capture_subprocess_io do
61       assert_equal(false, arv_put('--filename', 'foo', './tmp/empty_dir/.'),
62                    'arv-put --filename refuses directory')
63     end
64     assert_match /^usage:.*error:/m, err
65     assert_empty out
66   end
67
68   def test_filename_arg_with_multiple_files
69     out, err = capture_subprocess_io do
70       assert_equal(false, arv_put('--filename', 'foo',
71                                   './tmp/empty_file',
72                                   './tmp/empty_file'),
73                    'arv-put --filename refuses directory')
74     end
75     assert_match /^usage:.*error:/m, err
76     assert_empty out
77   end
78
79   def test_filename_arg_with_empty_file
80     out, err = capture_subprocess_io do
81       assert arv_put('--filename', 'foo', './tmp/empty_file')
82     end
83     $stderr.write err
84     assert_match '', err
85     assert match_collection_uuid(out)
86   end
87
88   def test_as_stream
89     out, err = capture_subprocess_io do
90       assert arv_put('--no-cache', '--as-stream', './tmp/foo')
91     end
92     $stderr.write err
93     assert_match '', err
94     assert_equal foo_manifest, out
95   end
96
97   def test_progress
98     out, err = capture_subprocess_io do
99       assert arv_put('--no-cache', '--manifest', '--progress', './tmp/foo')
100     end
101     assert_match /%/, err
102     assert match_collection_uuid(out)
103   end
104
105   def test_batch_progress
106     out, err = capture_subprocess_io do
107       assert arv_put('--no-cache', '--manifest', '--batch-progress', './tmp/foo')
108     end
109     assert_match /: 0 written 3 total/, err
110     assert_match /: 3 written 3 total/, err
111     assert match_collection_uuid(out)
112   end
113
114   def test_progress_and_batch_progress
115     out, err = capture_subprocess_io do
116       assert_equal(false,
117                    arv_put('--progress', '--batch-progress', './tmp/foo'),
118                    'arv-put --progress --batch-progress is contradictory')
119     end
120     assert_match /^usage:.*error:/m, err
121     assert_empty out
122   end
123
124   def test_read_from_implicit_stdin
125     test_read_from_stdin(specify_stdin_as='--manifest')
126   end
127
128   def test_read_from_dev_stdin
129     test_read_from_stdin(specify_stdin_as='/dev/stdin')
130   end
131
132   def test_read_from_stdin(specify_stdin_as='-')
133     out, err = capture_subprocess_io do
134       r,w = IO.pipe
135       wpid = fork do
136         r.close
137         w << 'foo'
138       end
139       w.close
140       assert arv_put('--filename', 'foo', specify_stdin_as,
141                                  { in: r })
142       r.close
143       Process.waitpid wpid
144     end
145     $stderr.write err
146     assert_match '', err
147     assert match_collection_uuid(out)
148   end
149
150   def test_read_from_implicit_stdin_implicit_manifest
151     test_read_from_stdin_implicit_manifest(specify_stdin_as=nil,
152                                            expect_filename='stdin')
153   end
154
155   def test_read_from_dev_stdin_implicit_manifest
156     test_read_from_stdin_implicit_manifest(specify_stdin_as='/dev/stdin')
157   end
158
159   def test_read_from_stdin_implicit_manifest(specify_stdin_as='-',
160                                              expect_filename=nil)
161     expect_filename = expect_filename || specify_stdin_as.split('/').last
162     out, err = capture_subprocess_io do
163       r,w = IO.pipe
164       wpid = fork do
165         r.close
166         w << 'foo'
167       end
168       w.close
169       args = []
170       args.push specify_stdin_as if specify_stdin_as
171       assert arv_put(*args, { in: r })
172       r.close
173       Process.waitpid wpid
174     end
175     $stderr.write err
176     assert_match '', err
177     assert match_collection_uuid(out)
178   end
179
180   protected
181   def arv_put(*args)
182     system ['./bin/arv-put', 'arv-put'], *args
183   end
184
185   def foo_manifest(filename='foo')
186     ". #{Digest::MD5.hexdigest('foo')}+3 0:3:#{filename}\n"
187   end
188
189   def foo_manifest_locator(filename='foo')
190     Digest::MD5.hexdigest(foo_manifest(filename)) +
191       "+#{foo_manifest(filename).length}"
192   end
193
194   def match_collection_uuid(uuid)
195     /^([0-9a-z]{5}-4zz18-[0-9a-z]{15})?$/.match(uuid)
196   end
197 end