1 | """ |
---|
2 | Ported to Python 3. |
---|
3 | """ |
---|
4 | |
---|
5 | import os.path |
---|
6 | from twisted.trial import unittest |
---|
7 | from allmydata.util import fileutil |
---|
8 | from ..no_network import GridTestMixin |
---|
9 | from allmydata.scripts import tahoe_mv |
---|
10 | from .common import CLITestMixin |
---|
11 | |
---|
12 | |
---|
13 | class Mv(GridTestMixin, CLITestMixin, unittest.TestCase): |
---|
14 | def test_mv_behavior(self): |
---|
15 | self.basedir = "cli/Mv/mv_behavior" |
---|
16 | self.set_up_grid(oneshare=True) |
---|
17 | fn1 = os.path.join(self.basedir, "file1") |
---|
18 | DATA1 = b"Nuclear launch codes" |
---|
19 | fileutil.write(fn1, DATA1) |
---|
20 | fn2 = os.path.join(self.basedir, "file2") |
---|
21 | DATA2 = b"UML diagrams" |
---|
22 | fileutil.write(fn2, DATA2) |
---|
23 | # copy both files to the grid |
---|
24 | d = self.do_cli("create-alias", "tahoe") |
---|
25 | d.addCallback(lambda res: |
---|
26 | self.do_cli("cp", fn1, "tahoe:")) |
---|
27 | d.addCallback(lambda res: |
---|
28 | self.do_cli("cp", fn2, "tahoe:")) |
---|
29 | |
---|
30 | # do mv file1 file3 |
---|
31 | # (we should be able to rename files) |
---|
32 | d.addCallback(lambda res: |
---|
33 | self.do_cli("mv", "tahoe:file1", "tahoe:file3")) |
---|
34 | d.addCallback(lambda rc_out_err: |
---|
35 | self.failUnlessIn("OK", rc_out_err[1], "mv didn't rename a file")) |
---|
36 | |
---|
37 | # do mv file3 file2 |
---|
38 | # (This should succeed without issue) |
---|
39 | d.addCallback(lambda res: |
---|
40 | self.do_cli("mv", "tahoe:file3", "tahoe:file2")) |
---|
41 | # Out should contain "OK" to show that the transfer worked. |
---|
42 | d.addCallback(lambda rc_out_err: |
---|
43 | self.failUnlessIn("OK", rc_out_err[1], "mv didn't output OK after mving")) |
---|
44 | |
---|
45 | # Next, make a remote directory. |
---|
46 | d.addCallback(lambda res: |
---|
47 | self.do_cli("mkdir", "tahoe:directory")) |
---|
48 | |
---|
49 | # mv file2 directory |
---|
50 | # (should fail with a descriptive error message; the CLI mv |
---|
51 | # client should support this) |
---|
52 | d.addCallback(lambda res: |
---|
53 | self.do_cli("mv", "tahoe:file2", "tahoe:directory")) |
---|
54 | d.addCallback(lambda rc_out_err: |
---|
55 | self.failUnlessIn( |
---|
56 | "Error: You can't overwrite a directory with a file", rc_out_err[2], |
---|
57 | "mv shouldn't overwrite directories" )) |
---|
58 | |
---|
59 | # mv file2 directory/ |
---|
60 | # (should succeed by making file2 a child node of directory) |
---|
61 | d.addCallback(lambda res: |
---|
62 | self.do_cli("mv", "tahoe:file2", "tahoe:directory/")) |
---|
63 | # We should see an "OK"... |
---|
64 | d.addCallback(lambda rc_out_err: |
---|
65 | self.failUnlessIn("OK", rc_out_err[1], |
---|
66 | "mv didn't mv a file into a directory")) |
---|
67 | # ... and be able to GET the file |
---|
68 | d.addCallback(lambda res: |
---|
69 | self.do_cli("get", "tahoe:directory/file2", self.basedir + "new")) |
---|
70 | d.addCallback(lambda rc_out_err: |
---|
71 | self.failUnless(os.path.exists(self.basedir + "new"), |
---|
72 | "mv didn't write the destination file")) |
---|
73 | # ... and not find the file where it was before. |
---|
74 | d.addCallback(lambda res: |
---|
75 | self.do_cli("get", "tahoe:file2", "file2")) |
---|
76 | d.addCallback(lambda rc_out_err: |
---|
77 | self.failUnlessIn("404", rc_out_err[2], |
---|
78 | "mv left the source file intact")) |
---|
79 | |
---|
80 | # Let's build: |
---|
81 | # directory/directory2/some_file |
---|
82 | # directory3 |
---|
83 | d.addCallback(lambda res: |
---|
84 | self.do_cli("mkdir", "tahoe:directory/directory2")) |
---|
85 | d.addCallback(lambda res: |
---|
86 | self.do_cli("cp", fn2, "tahoe:directory/directory2/some_file")) |
---|
87 | d.addCallback(lambda res: |
---|
88 | self.do_cli("mkdir", "tahoe:directory3")) |
---|
89 | |
---|
90 | # Let's now try to mv directory/directory2/some_file to |
---|
91 | # directory3/some_file |
---|
92 | d.addCallback(lambda res: |
---|
93 | self.do_cli("mv", "tahoe:directory/directory2/some_file", |
---|
94 | "tahoe:directory3/")) |
---|
95 | # We should have just some_file in tahoe:directory3 |
---|
96 | d.addCallback(lambda res: |
---|
97 | self.do_cli("get", "tahoe:directory3/some_file", "some_file")) |
---|
98 | d.addCallback(lambda rc_out_err: |
---|
99 | self.failUnless("404" not in rc_out_err[2], |
---|
100 | "mv didn't handle nested directories correctly")) |
---|
101 | d.addCallback(lambda res: |
---|
102 | self.do_cli("get", "tahoe:directory3/directory", "directory")) |
---|
103 | d.addCallback(lambda rc_out_err: |
---|
104 | self.failUnlessIn("404", rc_out_err[2], |
---|
105 | "mv moved the wrong thing")) |
---|
106 | return d |
---|
107 | |
---|
108 | def test_mv_error_if_DELETE_fails(self): |
---|
109 | self.basedir = "cli/Mv/mv_error_if_DELETE_fails" |
---|
110 | self.set_up_grid(oneshare=True) |
---|
111 | fn1 = os.path.join(self.basedir, "file1") |
---|
112 | DATA1 = b"Nuclear launch codes" |
---|
113 | fileutil.write(fn1, DATA1) |
---|
114 | |
---|
115 | original_do_http = tahoe_mv.do_http |
---|
116 | def mock_do_http(method, url, body=b""): |
---|
117 | if method == "DELETE": |
---|
118 | class FakeResponse(object): |
---|
119 | def read(self): |
---|
120 | return "response" |
---|
121 | resp = FakeResponse() |
---|
122 | resp.status = '500 Something Went Wrong' |
---|
123 | resp.reason = '*shrug*' |
---|
124 | return resp |
---|
125 | else: |
---|
126 | return original_do_http(method, url, body=body) |
---|
127 | tahoe_mv.do_http = mock_do_http |
---|
128 | |
---|
129 | # copy file to the grid |
---|
130 | d = self.do_cli("create-alias", "tahoe") |
---|
131 | d.addCallback(lambda res: |
---|
132 | self.do_cli("cp", fn1, "tahoe:")) |
---|
133 | |
---|
134 | # do mv file1 file2 |
---|
135 | d.addCallback(lambda res: |
---|
136 | self.do_cli("mv", "tahoe:file1", "tahoe:file2")) |
---|
137 | def _check(args ): |
---|
138 | (rc, out, err) = args |
---|
139 | self.failIfIn("OK", out, "mv printed 'OK' even though the DELETE failed") |
---|
140 | self.failUnlessEqual(rc, 2) |
---|
141 | d.addCallback(_check) |
---|
142 | |
---|
143 | def _restore_do_http(res): |
---|
144 | tahoe_mv.do_http = original_do_http |
---|
145 | return res |
---|
146 | d.addBoth(_restore_do_http) |
---|
147 | return d |
---|
148 | |
---|
149 | def test_mv_without_alias(self): |
---|
150 | # doing 'tahoe mv' without explicitly specifying an alias or |
---|
151 | # creating the default 'tahoe' alias should fail with a useful |
---|
152 | # error message. |
---|
153 | self.basedir = "cli/Mv/mv_without_alias" |
---|
154 | self.set_up_grid(oneshare=True) |
---|
155 | d = self.do_cli("mv", "afile", "anotherfile") |
---|
156 | def _check(args): |
---|
157 | (rc, out, err) = args |
---|
158 | self.failUnlessReallyEqual(rc, 1) |
---|
159 | self.failUnlessIn("error:", err) |
---|
160 | self.assertEqual(len(out), 0, out) |
---|
161 | d.addCallback(_check) |
---|
162 | # check to see that the validation extends to the |
---|
163 | # target argument by making an alias that will work with the first |
---|
164 | # one. |
---|
165 | d.addCallback(lambda ign: self.do_cli("create-alias", "havasu")) |
---|
166 | def _create_a_test_file(ign): |
---|
167 | self.test_file_path = os.path.join(self.basedir, "afile") |
---|
168 | fileutil.write(self.test_file_path, "puppies" * 100) |
---|
169 | d.addCallback(_create_a_test_file) |
---|
170 | d.addCallback(lambda ign: self.do_cli("put", self.test_file_path, |
---|
171 | "havasu:afile")) |
---|
172 | d.addCallback(lambda ign: self.do_cli("mv", "havasu:afile", |
---|
173 | "anotherfile")) |
---|
174 | d.addCallback(_check) |
---|
175 | return d |
---|
176 | |
---|
177 | def test_mv_with_nonexistent_alias(self): |
---|
178 | # doing 'tahoe mv' with an alias that doesn't exist should fail |
---|
179 | # with an informative error message. |
---|
180 | self.basedir = "cli/Mv/mv_with_nonexistent_alias" |
---|
181 | self.set_up_grid(oneshare=True) |
---|
182 | d = self.do_cli("mv", "fake:afile", "fake:anotherfile") |
---|
183 | def _check(args): |
---|
184 | (rc, out, err) = args |
---|
185 | self.failUnlessReallyEqual(rc, 1) |
---|
186 | self.failUnlessIn("error:", err) |
---|
187 | self.failUnlessIn("fake", err) |
---|
188 | self.assertEqual(len(out), 0, out) |
---|
189 | d.addCallback(_check) |
---|
190 | # check to see that the validation extends to the |
---|
191 | # target argument by making an alias that will work with the first |
---|
192 | # one. |
---|
193 | d.addCallback(lambda ign: self.do_cli("create-alias", "havasu")) |
---|
194 | def _create_a_test_file(ign): |
---|
195 | self.test_file_path = os.path.join(self.basedir, "afile") |
---|
196 | fileutil.write(self.test_file_path, "puppies" * 100) |
---|
197 | d.addCallback(_create_a_test_file) |
---|
198 | d.addCallback(lambda ign: self.do_cli("put", self.test_file_path, |
---|
199 | "havasu:afile")) |
---|
200 | d.addCallback(lambda ign: self.do_cli("mv", "havasu:afile", |
---|
201 | "fake:anotherfile")) |
---|
202 | d.addCallback(_check) |
---|
203 | return d |
---|