12483: Fix cadaver tests.
[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                         path:  writePath,
105                         cmd:   "move testfile newdir0/\n",
106                         match: `(?ms).*Moving .* failed.*`,
107                 },
108                 {
109                         path:  writePath,
110                         cmd:   "ls\n",
111                         match: `(?ms).*newdir0.* 0 .*`,
112                 },
113                 {
114                         path:  writePath,
115                         cmd:   "move newdir0/testfile emptyfile/bogus/\n",
116                         match: `(?ms).*Moving .* failed.*`,
117                 },
118                 {
119                         path:  writePath,
120                         cmd:   "mkcol newdir1\n",
121                         match: `(?ms).*Creating .* succeeded.*`,
122                 },
123                 {
124                         path:  writePath,
125                         cmd:   "move newdir0/testfile newdir1/\n",
126                         match: `(?ms).*Moving .* succeeded.*`,
127                 },
128                 {
129                         path:  writePath,
130                         cmd:   "put '" + localfile.Name() + "' newdir1/testfile1\n",
131                         match: `(?ms).*Uploading .* succeeded.*`,
132                 },
133                 {
134                         path:  writePath,
135                         cmd:   "mkcol newdir2\n",
136                         match: `(?ms).*Creating .* succeeded.*`,
137                 },
138                 {
139                         path:  writePath,
140                         cmd:   "put '" + localfile.Name() + "' newdir2/testfile2\n",
141                         match: `(?ms).*Uploading .* succeeded.*`,
142                 },
143                 {
144                         path:  writePath,
145                         cmd:   "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
146                         match: `(?ms).*succeeded.*`,
147                         data:  testdata,
148                 },
149                 {
150                         path:  writePath,
151                         cmd:   "rmcol newdir2\n",
152                         match: `(?ms).*Deleting collection .* succeeded.*`,
153                 },
154                 {
155                         path:  writePath,
156                         cmd:   "get newdir2/testfile2 '" + checkfile.Name() + "'\n",
157                         match: `(?ms).*Downloading .* failed.*`,
158                 },
159         } {
160                 c.Logf("%s %+v", "http://"+s.testServer.Addr, trial)
161
162                 os.Remove(checkfile.Name())
163
164                 cmd := exec.Command("cadaver", "http://"+s.testServer.Addr+trial.path)
165                 cmd.Stdin = bytes.NewBufferString(trial.cmd)
166                 stdout, err := cmd.StdoutPipe()
167                 c.Assert(err, check.Equals, nil)
168                 cmd.Stderr = cmd.Stdout
169                 go cmd.Start()
170
171                 var buf bytes.Buffer
172                 _, err = io.Copy(&buf, stdout)
173                 c.Check(err, check.Equals, nil)
174                 err = cmd.Wait()
175                 c.Check(err, check.Equals, nil)
176                 c.Check(buf.String(), check.Matches, trial.match)
177
178                 if trial.data == nil {
179                         continue
180                 }
181                 checkfile, err = os.Open(checkfile.Name())
182                 c.Assert(err, check.IsNil)
183                 checkfile.Seek(0, os.SEEK_SET)
184                 got, err := ioutil.ReadAll(checkfile)
185                 c.Check(got, check.DeepEquals, trial.data)
186                 c.Check(err, check.IsNil)
187         }
188 }