12483: Support file create/write via webdav.
[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                 c.Logf("%s %+v", "http://"+s.testServer.Addr, trial)
100
101                 os.Remove(checkfile.Name())
102
103                 cmd := exec.Command("cadaver", "http://"+s.testServer.Addr+trial.path)
104                 cmd.Stdin = bytes.NewBufferString(trial.cmd)
105                 stdout, err := cmd.StdoutPipe()
106                 c.Assert(err, check.Equals, nil)
107                 cmd.Stderr = cmd.Stdout
108                 go cmd.Start()
109
110                 var buf bytes.Buffer
111                 _, err = io.Copy(&buf, stdout)
112                 c.Check(err, check.Equals, nil)
113                 err = cmd.Wait()
114                 c.Check(err, check.Equals, nil)
115                 c.Check(buf.String(), check.Matches, trial.match)
116
117                 if trial.data == nil {
118                         continue
119                 }
120                 checkfile, err = os.Open(checkfile.Name())
121                 c.Assert(err, check.IsNil)
122                 checkfile.Seek(0, os.SEEK_SET)
123                 got, err := ioutil.ReadAll(checkfile)
124                 c.Check(got, check.DeepEquals, trial.data)
125                 c.Check(err, check.IsNil)
126         }
127 }