Merge branch '1499-workbench-tags'
[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_equal(true, arv_put('-h'),
18                    'arv-put -h exits zero')
19     end
20     $stderr.write err
21     assert_equal '', err
22     assert_match /^usage:/, out
23   end
24
25   def test_raw_stdin
26     out, err = capture_subprocess_io do
27       r,w = IO.pipe
28       wpid = fork do
29         r.close
30         w << 'foo'
31       end
32       w.close
33       assert_equal true, arv_put('--raw', {in: r})
34       r.close
35       Process.waitpid wpid
36     end
37     $stderr.write err
38     assert_match '', err
39     assert_equal "acbd18db4cc2f85cedef654fccc4a4d8+3\n", out
40   end
41
42   def test_raw_file
43     out, err = capture_subprocess_io do
44       assert_equal true, arv_put('--raw', './tmp/foo')
45     end
46     $stderr.write err
47     assert_match '', err
48     assert_equal "acbd18db4cc2f85cedef654fccc4a4d8+3\n", out
49   end
50
51   def test_raw_empty_file
52     out, err = capture_subprocess_io do
53       assert_equal true, arv_put('--raw', './tmp/empty_file')
54     end
55     $stderr.write err
56     assert_match '', err
57     assert_equal "d41d8cd98f00b204e9800998ecf8427e+0\n", out
58   end
59
60   def test_filename_arg_with_directory
61     out, err = capture_subprocess_io do
62       assert_equal(false, arv_put('--filename', 'foo', './tmp/empty_dir/.'),
63                    'arv-put --filename refuses directory')
64     end
65     assert_match /^usage:.*error:/m, err
66     assert_equal '', out
67   end
68
69   def test_filename_arg_with_multiple_files
70     out, err = capture_subprocess_io do
71       assert_equal(false, arv_put('--filename', 'foo',
72                                   './tmp/empty_file',
73                                   './tmp/empty_file'),
74                    'arv-put --filename refuses directory')
75     end
76     assert_match /^usage:.*error:/m, err
77     assert_equal '', out
78   end
79
80   def test_filename_arg_with_empty_file
81     out, err = capture_subprocess_io do
82       assert_equal true, arv_put('--filename', 'foo', './tmp/empty_file')
83     end
84     $stderr.write err
85     assert_match '', err
86     assert_equal "aa4f15cbf013142a7d98b1e273f9c661+45\n", out
87   end
88
89   def test_as_stream
90     out, err = capture_subprocess_io do
91       assert_equal true, arv_put('--as-stream', './tmp/foo')
92     end
93     $stderr.write err
94     assert_match '', err
95     assert_equal foo_manifest, out
96   end
97
98   def test_progress
99     out, err = capture_subprocess_io do
100       assert_equal true, arv_put('--manifest', '--progress', './tmp/foo')
101     end
102     assert_match /%/, err
103     assert_equal foo_manifest_locator+"\n", out
104   end
105
106   def test_batch_progress
107     out, err = capture_subprocess_io do
108       assert_equal true, arv_put('--manifest', '--batch-progress', './tmp/foo')
109     end
110     assert_match /: 0 written 3 total/, err
111     assert_match /: 3 written 3 total/, err
112     assert_equal foo_manifest_locator+"\n", out
113   end
114
115   def test_progress_and_batch_progress
116     out, err = capture_subprocess_io do
117       assert_equal(false,
118                    arv_put('--progress', '--batch-progress', './tmp/foo'),
119                    'arv-put --progress --batch-progress is contradictory')
120     end
121     assert_match /^usage:.*error:/m, err
122     assert_equal '', out
123   end
124
125   def test_read_from_implicit_stdin
126     test_read_from_stdin(specify_stdin_as='--manifest')
127   end
128
129   def test_read_from_dev_stdin
130     test_read_from_stdin(specify_stdin_as='/dev/stdin')
131   end
132
133   def test_read_from_stdin(specify_stdin_as='-')
134     out, err = capture_subprocess_io do
135       r,w = IO.pipe
136       wpid = fork do
137         r.close
138         w << 'foo'
139       end
140       w.close
141       assert_equal true, arv_put('--filename', 'foo', specify_stdin_as,
142                                  { in: r })
143       r.close
144       Process.waitpid wpid
145     end
146     $stderr.write err
147     assert_match '', err
148     assert_equal foo_manifest_locator+"\n", out
149   end
150
151   def test_read_from_implicit_stdin_implicit_manifest
152     test_read_from_stdin_implicit_manifest(specify_stdin_as=nil,
153                                            expect_filename='stdin')
154   end
155
156   def test_read_from_dev_stdin_implicit_manifest
157     test_read_from_stdin_implicit_manifest(specify_stdin_as='/dev/stdin')
158   end
159
160   def test_read_from_stdin_implicit_manifest(specify_stdin_as='-',
161                                              expect_filename=nil)
162     expect_filename = expect_filename || specify_stdin_as.split('/').last
163     out, err = capture_subprocess_io do
164       r,w = IO.pipe
165       wpid = fork do
166         r.close
167         w << 'foo'
168       end
169       w.close
170       args = []
171       args.push specify_stdin_as if specify_stdin_as
172       assert_equal true, arv_put(*args, { in: r })
173       r.close
174       Process.waitpid wpid
175     end
176     $stderr.write err
177     assert_match '', err
178     assert_equal(foo_manifest_locator(expect_filename)+"\n",
179                  out)
180   end
181
182   protected
183   def arv_put(*args)
184     system ['./bin/arv-put', 'arv-put'], *args
185   end
186
187   def foo_manifest(filename='foo')
188     ". #{Digest::MD5.hexdigest('foo')}+3 0:3:#{filename}\n"
189   end
190
191   def foo_manifest_locator(filename='foo')
192     Digest::MD5.hexdigest(foo_manifest(filename)) +
193       "+#{foo_manifest(filename).length}"
194   end
195 end