12483: Use real modTimes.
[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         matchToday := time.Now().Format("Jan +2")
46
47         readPath := "/c=" + arvadostest.FooAndBarFilesInDirUUID + "/t=" + arvadostest.ActiveToken + "/"
48         type testcase struct {
49                 path  string
50                 cmd   string
51                 match string
52                 data  []byte
53         }
54         for _, trial := range []testcase{
55                 {
56                         path:  readPath,
57                         cmd:   "ls\n",
58                         match: `(?ms).*dir1 *0 .*`,
59                 },
60                 {
61                         path:  readPath,
62                         cmd:   "ls dir1\n",
63                         match: `(?ms).*bar *3.*foo *3 .*`,
64                 },
65                 {
66                         path:  readPath + "_/dir1",
67                         cmd:   "ls\n",
68                         match: `(?ms).*bar *3.*foo *3 .*`,
69                 },
70                 {
71                         path:  readPath + "dir1/",
72                         cmd:   "ls\n",
73                         match: `(?ms).*bar *3.*foo +3 +Feb +\d+ +2014.*`,
74                 },
75                 {
76                         path:  writePath,
77                         cmd:   "get emptyfile '" + checkfile.Name() + "'\n",
78                         match: `(?ms).*Not Found.*`,
79                 },
80                 {
81                         path:  writePath,
82                         cmd:   "put '" + emptyfile.Name() + "' emptyfile\n",
83                         match: `(?ms).*Uploading .* succeeded.*`,
84                 },
85                 {
86                         path:  writePath,
87                         cmd:   "get emptyfile '" + checkfile.Name() + "'\n",
88                         match: `(?ms).*Downloading .* succeeded.*`,
89                         data:  []byte{},
90                 },
91                 {
92                         path:  writePath,
93                         cmd:   "put '" + localfile.Name() + "' testfile\n",
94                         match: `(?ms).*Uploading .* succeeded.*`,
95                 },
96                 {
97                         path:  writePath,
98                         cmd:   "get testfile '" + checkfile.Name() + "'\n",
99                         match: `(?ms).*succeeded.*`,
100                         data:  testdata,
101                 },
102                 {
103                         path:  writePath,
104                         cmd:   "move testfile newdir0/\n",
105                         match: `(?ms).*Moving .* succeeded.*`,
106                 },
107                 {
108                         path:  writePath,
109                         cmd:   "move testfile newdir0/\n",
110                         match: `(?ms).*Moving .* failed.*`,
111                 },
112                 {
113                         path:  writePath,
114                         cmd:   "ls\n",
115                         match: `(?ms).*newdir0.* 0 +` + matchToday + ` \d+:\d+\n.*`,
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:   "copy newdir2/testfile2 testfile3\n",
150                         match: `(?ms).*succeeded.*`,
151                 },
152                 {
153                         path:  writePath,
154                         cmd:   "get testfile3 '" + checkfile.Name() + "'\n",
155                         match: `(?ms).*succeeded.*`,
156                         data:  testdata,
157                 },
158                 {
159                         path:  writePath,
160                         cmd:   "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
161                         match: `(?ms).*succeeded.*`,
162                         data:  testdata,
163                 },
164                 {
165                         path:  writePath,
166                         cmd:   "rmcol newdir2\n",
167                         match: `(?ms).*Deleting collection .* succeeded.*`,
168                 },
169                 {
170                         path:  writePath,
171                         cmd:   "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
172                         match: `(?ms).*Downloading .* failed.*`,
173                 },
174                 {
175                         path:  "/c=" + arvadostest.UserAgreementCollection + "/t=" + arv.AuthToken + "/",
176                         cmd:   "put '" + localfile.Name() + "' foo\n",
177                         match: `(?ms).*Uploading .* failed:.*403 Forbidden.*`,
178                 },
179                 {
180                         path:  "/c=" + strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + "/t=" + arv.AuthToken + "/",
181                         cmd:   "put '" + localfile.Name() + "' foo\n",
182                         match: `(?ms).*Uploading .* failed:.*405 Method Not Allowed.*`,
183                 },
184         } {
185                 c.Logf("%s %+v", "http://"+s.testServer.Addr, trial)
186
187                 os.Remove(checkfile.Name())
188
189                 cmd := exec.Command("cadaver", "http://"+s.testServer.Addr+trial.path)
190                 cmd.Stdin = bytes.NewBufferString(trial.cmd)
191                 stdout, err := cmd.StdoutPipe()
192                 c.Assert(err, check.Equals, nil)
193                 cmd.Stderr = cmd.Stdout
194                 go cmd.Start()
195
196                 var buf bytes.Buffer
197                 _, err = io.Copy(&buf, stdout)
198                 c.Check(err, check.Equals, nil)
199                 err = cmd.Wait()
200                 c.Check(err, check.Equals, nil)
201                 c.Check(buf.String(), check.Matches, trial.match)
202
203                 if trial.data == nil {
204                         continue
205                 }
206                 checkfile, err = os.Open(checkfile.Name())
207                 c.Assert(err, check.IsNil)
208                 checkfile.Seek(0, os.SEEK_SET)
209                 got, err := ioutil.ReadAll(checkfile)
210                 c.Check(got, check.DeepEquals, trial.data)
211                 c.Check(err, check.IsNil)
212         }
213 }