Merge branch '12483-writable-fs'
[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:   "put '" + localfile.Name() + "' newdir1/testfile1\n",
137                         match: `(?ms).*Uploading .* succeeded.*`,
138                 },
139                 {
140                         path:  writePath,
141                         cmd:   "mkcol newdir2\n",
142                         match: `(?ms).*Creating .* succeeded.*`,
143                 },
144                 {
145                         path:  writePath,
146                         cmd:   "put '" + localfile.Name() + "' newdir2/testfile2\n",
147                         match: `(?ms).*Uploading .* succeeded.*`,
148                 },
149                 {
150                         path:  writePath,
151                         cmd:   "copy newdir2/testfile2 testfile3\n",
152                         match: `(?ms).*succeeded.*`,
153                 },
154                 {
155                         path:  writePath,
156                         cmd:   "get testfile3 '" + checkfile.Name() + "'\n",
157                         match: `(?ms).*succeeded.*`,
158                         data:  testdata,
159                 },
160                 {
161                         path:  writePath,
162                         cmd:   "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
163                         match: `(?ms).*succeeded.*`,
164                         data:  testdata,
165                 },
166                 {
167                         path:  writePath,
168                         cmd:   "rmcol newdir2\n",
169                         match: `(?ms).*Deleting collection .* succeeded.*`,
170                 },
171                 {
172                         path:  writePath,
173                         cmd:   "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
174                         match: `(?ms).*Downloading .* failed.*`,
175                 },
176                 {
177                         path:  "/c=" + arvadostest.UserAgreementCollection + "/t=" + arv.AuthToken + "/",
178                         cmd:   "put '" + localfile.Name() + "' foo\n",
179                         match: `(?ms).*Uploading .* failed:.*403 Forbidden.*`,
180                 },
181                 {
182                         path:  pdhPath,
183                         cmd:   "put '" + localfile.Name() + "' foo\n",
184                         match: `(?ms).*Uploading .* failed:.*405 Method Not Allowed.*`,
185                 },
186                 {
187                         path:  pdhPath,
188                         cmd:   "move foo bar\n",
189                         match: `(?ms).*Moving .* failed:.*405 Method Not Allowed.*`,
190                 },
191                 {
192                         path:  pdhPath,
193                         cmd:   "copy foo bar\n",
194                         match: `(?ms).*Copying .* failed:.*405 Method Not Allowed.*`,
195                 },
196                 {
197                         path:  pdhPath,
198                         cmd:   "delete foo\n",
199                         match: `(?ms).*Deleting .* failed:.*405 Method Not Allowed.*`,
200                 },
201         } {
202                 c.Logf("%s %+v", "http://"+s.testServer.Addr, trial)
203
204                 os.Remove(checkfile.Name())
205
206                 cmd := exec.Command("cadaver", "http://"+s.testServer.Addr+trial.path)
207                 cmd.Stdin = bytes.NewBufferString(trial.cmd)
208                 stdout, err := cmd.StdoutPipe()
209                 c.Assert(err, check.Equals, nil)
210                 cmd.Stderr = cmd.Stdout
211                 go cmd.Start()
212
213                 var buf bytes.Buffer
214                 _, err = io.Copy(&buf, stdout)
215                 c.Check(err, check.Equals, nil)
216                 err = cmd.Wait()
217                 c.Check(err, check.Equals, nil)
218                 c.Check(buf.String(), check.Matches, trial.match)
219
220                 if trial.data == nil {
221                         continue
222                 }
223                 checkfile, err = os.Open(checkfile.Name())
224                 c.Assert(err, check.IsNil)
225                 checkfile.Seek(0, os.SEEK_SET)
226                 got, err := ioutil.ReadAll(checkfile)
227                 c.Check(got, check.DeepEquals, trial.data)
228                 c.Check(err, check.IsNil)
229         }
230 }