12483: Make webdav rename/remove work. Tidy up code.
[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
15         "git.curoverse.com/arvados.git/sdk/go/arvados"
16         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
17         check "gopkg.in/check.v1"
18 )
19
20 func (s *IntegrationSuite) TestWebdavWithCadaver(c *check.C) {
21         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")
22
23         localfile, err := ioutil.TempFile("", "localfile")
24         c.Assert(err, check.IsNil)
25         defer os.Remove(localfile.Name())
26         localfile.Write(testdata)
27
28         emptyfile, err := ioutil.TempFile("", "emptyfile")
29         c.Assert(err, check.IsNil)
30         defer os.Remove(emptyfile.Name())
31
32         checkfile, err := ioutil.TempFile("", "checkfile")
33         c.Assert(err, check.IsNil)
34         defer os.Remove(checkfile.Name())
35
36         var newCollection arvados.Collection
37         arv := arvados.NewClientFromEnv()
38         arv.AuthToken = arvadostest.ActiveToken
39         err = arv.RequestAndDecode(&newCollection, "POST", "/arvados/v1/collections", bytes.NewBufferString(url.Values{"collection": {"{}"}}.Encode()), nil)
40         c.Assert(err, check.IsNil)
41         writePath := "/c=" + newCollection.UUID + "/t=" + arv.AuthToken + "/"
42
43         readPath := "/c=" + arvadostest.FooAndBarFilesInDirUUID + "/t=" + arvadostest.ActiveToken + "/"
44         type testcase struct {
45                 path  string
46                 cmd   string
47                 match string
48                 data  []byte
49         }
50         for _, trial := range []testcase{
51                 {
52                         path:  readPath,
53                         cmd:   "ls\n",
54                         match: `(?ms).*dir1 *0 .*`,
55                 },
56                 {
57                         path:  readPath,
58                         cmd:   "ls dir1\n",
59                         match: `(?ms).*bar *3.*foo *3 .*`,
60                 },
61                 {
62                         path:  readPath + "_/dir1",
63                         cmd:   "ls\n",
64                         match: `(?ms).*bar *3.*foo *3 .*`,
65                 },
66                 {
67                         path:  readPath + "dir1/",
68                         cmd:   "ls\n",
69                         match: `(?ms).*bar *3.*foo *3 .*`,
70                 },
71                 {
72                         path:  writePath,
73                         cmd:   "get emptyfile '" + checkfile.Name() + "'\n",
74                         match: `(?ms).*Not Found.*`,
75                 },
76                 {
77                         path:  writePath,
78                         cmd:   "put '" + emptyfile.Name() + "' emptyfile\n",
79                         match: `(?ms).*Uploading .* succeeded.*`,
80                 },
81                 {
82                         path:  writePath,
83                         cmd:   "get emptyfile '" + checkfile.Name() + "'\n",
84                         match: `(?ms).*Downloading .* succeeded.*`,
85                         data:  []byte{},
86                 },
87                 {
88                         path:  writePath,
89                         cmd:   "put '" + localfile.Name() + "' testfile\n",
90                         match: `(?ms).*Uploading .* succeeded.*`,
91                 },
92                 {
93                         path:  writePath,
94                         cmd:   "get testfile '" + checkfile.Name() + "'\n",
95                         match: `(?ms).*succeeded.*`,
96                         data:  testdata,
97                 },
98                 {
99                         path:  writePath,
100                         cmd:   "move testfile newdir0\n",
101                         match: `(?ms).*Moving .* succeeded.*`,
102                 },
103                 {
104                         // Strangely, webdav deletes dst if you do
105                         // "move nonexistent dst" -- otherwise we
106                         // would repeat the above "move testfile
107                         // newdir0" here.
108                         path:  writePath,
109                         cmd:   "move testfile nonexistentdir\n",
110                         match: `(?ms).*Moving .* failed.*`,
111                 },
112                 {
113                         path:  writePath,
114                         cmd:   "ls\n",
115                         match: `(?ms).*newdir0.* 0 .*`,
116                 },
117                 {
118                         path:  writePath,
119                         cmd:   "move newdir0/testfile emptyfile/bogus/\n",
120                         match: `(?ms).*Moving .* failed.*`,
121                 },
122                 {
123                         path:  writePath,
124                         cmd:   "mkcol newdir1\n",
125                         match: `(?ms).*Creating .* succeeded.*`,
126                 },
127                 {
128                         path:  writePath,
129                         cmd:   "move newdir0/testfile newdir1\n",
130                         match: `(?ms).*Moving .* succeeded.*`,
131                 },
132                 {
133                         path:  writePath,
134                         cmd:   "put '" + localfile.Name() + "' newdir1/testfile1\n",
135                         match: `(?ms).*Uploading .* succeeded.*`,
136                 },
137                 {
138                         path:  writePath,
139                         cmd:   "mkcol newdir2\n",
140                         match: `(?ms).*Creating .* succeeded.*`,
141                 },
142                 {
143                         path:  writePath,
144                         cmd:   "put '" + localfile.Name() + "' newdir2/testfile2\n",
145                         match: `(?ms).*Uploading .* succeeded.*`,
146                 },
147                 {
148                         path:  writePath,
149                         cmd:   "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
150                         match: `(?ms).*succeeded.*`,
151                         data:  testdata,
152                 },
153                 {
154                         path:  writePath,
155                         cmd:   "rmcol newdir2\n",
156                         match: `(?ms).*Deleting collection .* succeeded.*`,
157                 },
158                 {
159                         path:  writePath,
160                         cmd:   "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
161                         match: `(?ms).*Downloading .* failed.*`,
162                 },
163         } {
164                 c.Logf("%s %+v", "http://"+s.testServer.Addr, trial)
165
166                 os.Remove(checkfile.Name())
167
168                 cmd := exec.Command("cadaver", "http://"+s.testServer.Addr+trial.path)
169                 cmd.Stdin = bytes.NewBufferString(trial.cmd)
170                 stdout, err := cmd.StdoutPipe()
171                 c.Assert(err, check.Equals, nil)
172                 cmd.Stderr = cmd.Stdout
173                 go cmd.Start()
174
175                 var buf bytes.Buffer
176                 _, err = io.Copy(&buf, stdout)
177                 c.Check(err, check.Equals, nil)
178                 err = cmd.Wait()
179                 c.Check(err, check.Equals, nil)
180                 c.Check(buf.String(), check.Matches, trial.match)
181
182                 if trial.data == nil {
183                         continue
184                 }
185                 checkfile, err = os.Open(checkfile.Name())
186                 c.Assert(err, check.IsNil)
187                 checkfile.Seek(0, os.SEEK_SET)
188                 got, err := ioutil.ReadAll(checkfile)
189                 c.Check(got, check.DeepEquals, trial.data)
190                 c.Check(err, check.IsNil)
191         }
192 }