Merge branch '12844-webdav-move-into-self'
[arvados.git] / services / keep-web / cadaver_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package main
6
7 import (
8         "bytes"
9         "io"
10         "io/ioutil"
11         "net/url"
12         "os"
13         "os/exec"
14         "strings"
15         "time"
16
17         "git.curoverse.com/arvados.git/sdk/go/arvados"
18         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
19         check "gopkg.in/check.v1"
20 )
21
22 func (s *IntegrationSuite) TestWebdavWithCadaver(c *check.C) {
23         testdata := []byte("the human tragedy consists in the necessity of living with the consequences of actions performed under the pressure of compulsions we do not understand")
24
25         localfile, err := ioutil.TempFile("", "localfile")
26         c.Assert(err, check.IsNil)
27         defer os.Remove(localfile.Name())
28         localfile.Write(testdata)
29
30         emptyfile, err := ioutil.TempFile("", "emptyfile")
31         c.Assert(err, check.IsNil)
32         defer os.Remove(emptyfile.Name())
33
34         checkfile, err := ioutil.TempFile("", "checkfile")
35         c.Assert(err, check.IsNil)
36         defer os.Remove(checkfile.Name())
37
38         var newCollection arvados.Collection
39         arv := arvados.NewClientFromEnv()
40         arv.AuthToken = arvadostest.ActiveToken
41         err = arv.RequestAndDecode(&newCollection, "POST", "/arvados/v1/collections", bytes.NewBufferString(url.Values{"collection": {"{}"}}.Encode()), nil)
42         c.Assert(err, check.IsNil)
43         writePath := "/c=" + newCollection.UUID + "/t=" + arv.AuthToken + "/"
44
45         pdhPath := "/c=" + strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + "/t=" + arv.AuthToken + "/"
46
47         matchToday := time.Now().Format("Jan +2")
48
49         readPath := "/c=" + arvadostest.FooAndBarFilesInDirUUID + "/t=" + arvadostest.ActiveToken + "/"
50         type testcase struct {
51                 path  string
52                 cmd   string
53                 match string
54                 data  []byte
55         }
56         for _, trial := range []testcase{
57                 {
58                         path:  readPath,
59                         cmd:   "ls\n",
60                         match: `(?ms).*dir1 *0 .*`,
61                 },
62                 {
63                         path:  readPath,
64                         cmd:   "ls dir1\n",
65                         match: `(?ms).*bar *3.*foo *3 .*`,
66                 },
67                 {
68                         path:  readPath + "_/dir1",
69                         cmd:   "ls\n",
70                         match: `(?ms).*bar *3.*foo *3 .*`,
71                 },
72                 {
73                         path:  readPath + "dir1/",
74                         cmd:   "ls\n",
75                         match: `(?ms).*bar *3.*foo +3 +Feb +\d+ +2014.*`,
76                 },
77                 {
78                         path:  writePath,
79                         cmd:   "get emptyfile '" + checkfile.Name() + "'\n",
80                         match: `(?ms).*Not Found.*`,
81                 },
82                 {
83                         path:  writePath,
84                         cmd:   "put '" + emptyfile.Name() + "' emptyfile\n",
85                         match: `(?ms).*Uploading .* succeeded.*`,
86                 },
87                 {
88                         path:  writePath,
89                         cmd:   "get emptyfile '" + checkfile.Name() + "'\n",
90                         match: `(?ms).*Downloading .* succeeded.*`,
91                         data:  []byte{},
92                 },
93                 {
94                         path:  writePath,
95                         cmd:   "put '" + localfile.Name() + "' testfile\n",
96                         match: `(?ms).*Uploading .* succeeded.*`,
97                 },
98                 {
99                         path:  writePath,
100                         cmd:   "get testfile '" + checkfile.Name() + "'\n",
101                         match: `(?ms).*succeeded.*`,
102                         data:  testdata,
103                 },
104                 {
105                         path:  writePath,
106                         cmd:   "move testfile newdir0/\n",
107                         match: `(?ms).*Moving .* succeeded.*`,
108                 },
109                 {
110                         path:  writePath,
111                         cmd:   "move testfile newdir0/\n",
112                         match: `(?ms).*Moving .* failed.*`,
113                 },
114                 {
115                         path:  writePath,
116                         cmd:   "ls\n",
117                         match: `(?ms).*newdir0.* 0 +` + matchToday + ` \d+:\d+\n.*`,
118                 },
119                 {
120                         path:  writePath,
121                         cmd:   "move newdir0/testfile emptyfile/bogus/\n",
122                         match: `(?ms).*Moving .* failed.*`,
123                 },
124                 {
125                         path:  writePath,
126                         cmd:   "mkcol newdir1\n",
127                         match: `(?ms).*Creating .* succeeded.*`,
128                 },
129                 {
130                         path:  writePath,
131                         cmd:   "move newdir0/testfile newdir1/\n",
132                         match: `(?ms).*Moving .* succeeded.*`,
133                 },
134                 {
135                         path:  writePath,
136                         cmd:   "move newdir1 newdir1/\n",
137                         match: `(?ms).*Moving .* failed.*`,
138                 },
139                 {
140                         path:  writePath,
141                         cmd:   "get newdir1/testfile '" + checkfile.Name() + "'\n",
142                         match: `(?ms).*succeeded.*`,
143                         data:  testdata,
144                 },
145                 {
146                         path:  writePath,
147                         cmd:   "put '" + localfile.Name() + "' newdir1/testfile1\n",
148                         match: `(?ms).*Uploading .* succeeded.*`,
149                 },
150                 {
151                         path:  writePath,
152                         cmd:   "mkcol newdir2\n",
153                         match: `(?ms).*Creating .* succeeded.*`,
154                 },
155                 {
156                         path:  writePath,
157                         cmd:   "put '" + localfile.Name() + "' newdir2/testfile2\n",
158                         match: `(?ms).*Uploading .* succeeded.*`,
159                 },
160                 {
161                         path:  writePath,
162                         cmd:   "copy newdir2/testfile2 testfile3\n",
163                         match: `(?ms).*succeeded.*`,
164                 },
165                 {
166                         path:  writePath,
167                         cmd:   "get testfile3 '" + checkfile.Name() + "'\n",
168                         match: `(?ms).*succeeded.*`,
169                         data:  testdata,
170                 },
171                 {
172                         path:  writePath,
173                         cmd:   "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
174                         match: `(?ms).*succeeded.*`,
175                         data:  testdata,
176                 },
177                 {
178                         path:  writePath,
179                         cmd:   "rmcol newdir2\n",
180                         match: `(?ms).*Deleting collection .* succeeded.*`,
181                 },
182                 {
183                         path:  writePath,
184                         cmd:   "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
185                         match: `(?ms).*Downloading .* failed.*`,
186                 },
187                 {
188                         path:  "/c=" + arvadostest.UserAgreementCollection + "/t=" + arv.AuthToken + "/",
189                         cmd:   "put '" + localfile.Name() + "' foo\n",
190                         match: `(?ms).*Uploading .* failed:.*403 Forbidden.*`,
191                 },
192                 {
193                         path:  pdhPath,
194                         cmd:   "put '" + localfile.Name() + "' foo\n",
195                         match: `(?ms).*Uploading .* failed:.*405 Method Not Allowed.*`,
196                 },
197                 {
198                         path:  pdhPath,
199                         cmd:   "move foo bar\n",
200                         match: `(?ms).*Moving .* failed:.*405 Method Not Allowed.*`,
201                 },
202                 {
203                         path:  pdhPath,
204                         cmd:   "copy foo bar\n",
205                         match: `(?ms).*Copying .* failed:.*405 Method Not Allowed.*`,
206                 },
207                 {
208                         path:  pdhPath,
209                         cmd:   "delete foo\n",
210                         match: `(?ms).*Deleting .* failed:.*405 Method Not Allowed.*`,
211                 },
212         } {
213                 c.Logf("%s %+v", "http://"+s.testServer.Addr, trial)
214
215                 os.Remove(checkfile.Name())
216
217                 cmd := exec.Command("cadaver", "http://"+s.testServer.Addr+trial.path)
218                 cmd.Stdin = bytes.NewBufferString(trial.cmd)
219                 stdout, err := cmd.StdoutPipe()
220                 c.Assert(err, check.Equals, nil)
221                 cmd.Stderr = cmd.Stdout
222                 go cmd.Start()
223
224                 var buf bytes.Buffer
225                 _, err = io.Copy(&buf, stdout)
226                 c.Check(err, check.Equals, nil)
227                 err = cmd.Wait()
228                 c.Check(err, check.Equals, nil)
229                 c.Check(buf.String(), check.Matches, trial.match)
230
231                 if trial.data == nil {
232                         continue
233                 }
234                 checkfile, err = os.Open(checkfile.Name())
235                 c.Assert(err, check.IsNil)
236                 checkfile.Seek(0, os.SEEK_SET)
237                 got, err := ioutil.ReadAll(checkfile)
238                 c.Check(got, check.DeepEquals, trial.data)
239                 c.Check(err, check.IsNil)
240         }
241 }