Add 'apps/arv-web/' from commit 'f9732ad8460d013c2f28363655d0d1b91894dca5'
[arvados.git] / sdk / cli / test / test_arv-put.rb
1 require 'minitest/autorun'
2 require 'digest/md5'
3
4 class TestArvPut < 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     skip "Waiting unitl #4534 is implemented"
26
27     out, err = capture_subprocess_io do
28       r,w = IO.pipe
29       wpid = fork do
30         r.close
31         w << 'foo'
32       end
33       w.close
34       assert arv_put('--raw', {in: r})
35       r.close
36       Process.waitpid wpid
37     end
38     $stderr.write err
39     assert_match '', err
40     assert_equal "acbd18db4cc2f85cedef654fccc4a4d8+3\n", out
41   end
42
43   def test_raw_file
44     skip "Waiting unitl #4534 is implemented"
45
46     out, err = capture_subprocess_io do
47       assert arv_put('--raw', './tmp/foo')
48     end
49     $stderr.write err
50     assert_match '', err
51     assert_equal "acbd18db4cc2f85cedef654fccc4a4d8+3\n", out
52   end
53
54   def test_raw_empty_file
55     skip "Waiting unitl #4534 is implemented"
56
57     out, err = capture_subprocess_io do
58       assert arv_put('--raw', './tmp/empty_file')
59     end
60     $stderr.write err
61     assert_match '', err
62     assert_equal "d41d8cd98f00b204e9800998ecf8427e+0\n", out
63   end
64
65   def test_filename_arg_with_directory
66     out, err = capture_subprocess_io do
67       assert_equal(false, arv_put('--filename', 'foo', './tmp/empty_dir/.'),
68                    'arv-put --filename refuses directory')
69     end
70     assert_match /^usage:.*error:/m, err
71     assert_empty out
72   end
73
74   def test_filename_arg_with_multiple_files
75     out, err = capture_subprocess_io do
76       assert_equal(false, arv_put('--filename', 'foo',
77                                   './tmp/empty_file',
78                                   './tmp/empty_file'),
79                    'arv-put --filename refuses directory')
80     end
81     assert_match /^usage:.*error:/m, err
82     assert_empty out
83   end
84
85   def test_filename_arg_with_empty_file
86     skip "Waiting unitl #4534 is implemented"
87
88     out, err = capture_subprocess_io do
89       assert arv_put('--filename', 'foo', './tmp/empty_file')
90     end
91     $stderr.write err
92     assert_match '', err
93     assert match_collection_uuid(out)
94   end
95
96   def test_as_stream
97     skip "Waiting unitl #4534 is implemented"
98
99     out, err = capture_subprocess_io do
100       assert arv_put('--as-stream', './tmp/foo')
101     end
102     $stderr.write err
103     assert_match '', err
104     assert_equal foo_manifest, out
105   end
106
107   def test_progress
108     skip "Waiting unitl #4534 is implemented"
109
110     out, err = capture_subprocess_io do
111       assert arv_put('--manifest', '--progress', './tmp/foo')
112     end
113     assert_match /%/, err
114     assert match_collection_uuid(out)
115   end
116
117   def test_batch_progress
118     skip "Waiting unitl #4534 is implemented"
119
120     out, err = capture_subprocess_io do
121       assert arv_put('--manifest', '--batch-progress', './tmp/foo')
122     end
123     assert_match /: 0 written 3 total/, err
124     assert_match /: 3 written 3 total/, err
125     assert match_collection_uuid(out)
126   end
127
128   def test_progress_and_batch_progress
129     out, err = capture_subprocess_io do
130       assert_equal(false,
131                    arv_put('--progress', '--batch-progress', './tmp/foo'),
132                    'arv-put --progress --batch-progress is contradictory')
133     end
134     assert_match /^usage:.*error:/m, err
135     assert_empty out
136   end
137
138   def test_read_from_implicit_stdin
139     skip "Waiting unitl #4534 is implemented"
140
141     test_read_from_stdin(specify_stdin_as='--manifest')
142   end
143
144   def test_read_from_dev_stdin
145     skip "Waiting unitl #4534 is implemented"
146
147     test_read_from_stdin(specify_stdin_as='/dev/stdin')
148   end
149
150   def test_read_from_stdin(specify_stdin_as='-')
151     skip "Waiting unitl #4534 is implemented"
152
153     out, err = capture_subprocess_io do
154       r,w = IO.pipe
155       wpid = fork do
156         r.close
157         w << 'foo'
158       end
159       w.close
160       assert arv_put('--filename', 'foo', specify_stdin_as,
161                                  { in: r })
162       r.close
163       Process.waitpid wpid
164     end
165     $stderr.write err
166     assert_match '', err
167     assert match_collection_uuid(out)
168   end
169
170   def test_read_from_implicit_stdin_implicit_manifest
171     skip "Waiting unitl #4534 is implemented"
172
173     test_read_from_stdin_implicit_manifest(specify_stdin_as=nil,
174                                            expect_filename='stdin')
175   end
176
177   def test_read_from_dev_stdin_implicit_manifest
178     skip "Waiting unitl #4534 is implemented"
179
180     test_read_from_stdin_implicit_manifest(specify_stdin_as='/dev/stdin')
181   end
182
183   def test_read_from_stdin_implicit_manifest(specify_stdin_as='-',
184                                              expect_filename=nil)
185     skip "Waiting unitl #4534 is implemented"
186
187     expect_filename = expect_filename || specify_stdin_as.split('/').last
188     out, err = capture_subprocess_io do
189       r,w = IO.pipe
190       wpid = fork do
191         r.close
192         w << 'foo'
193       end
194       w.close
195       args = []
196       args.push specify_stdin_as if specify_stdin_as
197       assert arv_put(*args, { in: r })
198       r.close
199       Process.waitpid wpid
200     end
201     $stderr.write err
202     assert_match '', err
203     assert match_collection_uuid(out)
204   end
205
206   protected
207   def arv_put(*args)
208     system ['./bin/arv-put', 'arv-put'], *args
209   end
210
211   def foo_manifest(filename='foo')
212     ". #{Digest::MD5.hexdigest('foo')}+3 0:3:#{filename}\n"
213   end
214
215   def foo_manifest_locator(filename='foo')
216     Digest::MD5.hexdigest(foo_manifest(filename)) +
217       "+#{foo_manifest(filename).length}"
218   end
219
220   def match_collection_uuid(uuid)
221     /^([0-9a-z]{5}-4zz18-[0-9a-z]{15})?$/.match(uuid)
222   end
223 end