13111: Merge branch 'master' into 13111-webdav-projects
[arvados.git] / sdk / go / arvados / fs_project_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 package arvados
6
7 import (
8         "bytes"
9         "encoding/json"
10         "io"
11         "os"
12         "path/filepath"
13         "strings"
14
15         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
16         check "gopkg.in/check.v1"
17 )
18
19 type spiedRequest struct {
20         method string
21         path   string
22         params map[string]interface{}
23 }
24
25 type spyingClient struct {
26         *Client
27         calls []spiedRequest
28 }
29
30 func (sc *spyingClient) RequestAndDecode(dst interface{}, method, path string, body io.Reader, params interface{}) error {
31         var paramsCopy map[string]interface{}
32         var buf bytes.Buffer
33         json.NewEncoder(&buf).Encode(params)
34         json.NewDecoder(&buf).Decode(&paramsCopy)
35         sc.calls = append(sc.calls, spiedRequest{
36                 method: method,
37                 path:   path,
38                 params: paramsCopy,
39         })
40         return sc.Client.RequestAndDecode(dst, method, path, body, params)
41 }
42
43 func (s *SiteFSSuite) TestCurrentUserHome(c *check.C) {
44         s.fs.MountProject("home", "")
45         s.testHomeProject(c, "/home")
46 }
47
48 func (s *SiteFSSuite) TestUsersDir(c *check.C) {
49         s.testHomeProject(c, "/users/active")
50 }
51
52 func (s *SiteFSSuite) testHomeProject(c *check.C, path string) {
53         f, err := s.fs.Open(path)
54         c.Assert(err, check.IsNil)
55         fis, err := f.Readdir(-1)
56         c.Check(len(fis), check.Not(check.Equals), 0)
57
58         ok := false
59         for _, fi := range fis {
60                 c.Check(fi.Name(), check.Not(check.Equals), "")
61                 if fi.Name() == "A Project" {
62                         ok = true
63                 }
64         }
65         c.Check(ok, check.Equals, true)
66
67         f, err = s.fs.Open(path + "/A Project/..")
68         c.Assert(err, check.IsNil)
69         fi, err := f.Stat()
70         c.Assert(err, check.IsNil)
71         c.Check(fi.IsDir(), check.Equals, true)
72         _, basename := filepath.Split(path)
73         c.Check(fi.Name(), check.Equals, basename)
74
75         f, err = s.fs.Open(path + "/A Project/A Subproject")
76         c.Assert(err, check.IsNil)
77         fi, err = f.Stat()
78         c.Assert(err, check.IsNil)
79         c.Check(fi.IsDir(), check.Equals, true)
80
81         for _, nx := range []string{
82                 path + "/Unrestricted public data",
83                 path + "/Unrestricted public data/does not exist",
84                 path + "/A Project/does not exist",
85         } {
86                 c.Log(nx)
87                 f, err = s.fs.Open(nx)
88                 c.Check(err, check.NotNil)
89                 c.Check(os.IsNotExist(err), check.Equals, true)
90         }
91 }
92
93 func (s *SiteFSSuite) TestProjectReaddirAfterLoadOne(c *check.C) {
94         f, err := s.fs.Open("/users/active/A Project/A Subproject")
95         c.Assert(err, check.IsNil)
96         defer f.Close()
97         f, err = s.fs.Open("/users/active/A Project/Project does not exist")
98         c.Assert(err, check.NotNil)
99         f, err = s.fs.Open("/users/active/A Project/A Subproject")
100         c.Assert(err, check.IsNil)
101         defer f.Close()
102         f, err = s.fs.Open("/users/active/A Project")
103         c.Assert(err, check.IsNil)
104         defer f.Close()
105         fis, err := f.Readdir(-1)
106         c.Assert(err, check.IsNil)
107         c.Logf("%#v", fis)
108         var foundSubproject, foundCollection bool
109         for _, fi := range fis {
110                 switch fi.Name() {
111                 case "A Subproject":
112                         foundSubproject = true
113                 case "collection_to_move_around":
114                         foundCollection = true
115                 }
116         }
117         c.Check(foundSubproject, check.Equals, true)
118         c.Check(foundCollection, check.Equals, true)
119 }
120
121 func (s *SiteFSSuite) TestSlashInName(c *check.C) {
122         badCollection := Collection{
123                 Name:      "bad/collection",
124                 OwnerUUID: arvadostest.AProjectUUID,
125         }
126         err := s.client.RequestAndDecode(&badCollection, "POST", "arvados/v1/collections", s.client.UpdateBody(&badCollection), nil)
127         c.Assert(err, check.IsNil)
128         defer s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+badCollection.UUID, nil, nil)
129
130         badProject := Group{
131                 Name:       "bad/project",
132                 GroupClass: "project",
133                 OwnerUUID:  arvadostest.AProjectUUID,
134         }
135         err = s.client.RequestAndDecode(&badProject, "POST", "arvados/v1/groups", s.client.UpdateBody(&badProject), nil)
136         c.Assert(err, check.IsNil)
137         defer s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/groups/"+badProject.UUID, nil, nil)
138
139         dir, err := s.fs.Open("/users/active/A Project")
140         c.Assert(err, check.IsNil)
141         fis, err := dir.Readdir(-1)
142         c.Check(err, check.IsNil)
143         for _, fi := range fis {
144                 c.Logf("fi.Name() == %q", fi.Name())
145                 c.Check(strings.Contains(fi.Name(), "/"), check.Equals, false)
146         }
147 }
148
149 func (s *SiteFSSuite) TestProjectUpdatedByOther(c *check.C) {
150         s.fs.MountProject("home", "")
151
152         project, err := s.fs.OpenFile("/home/A Project", 0, 0)
153         c.Assert(err, check.IsNil)
154
155         _, err = s.fs.Open("/home/A Project/oob")
156         c.Check(err, check.NotNil)
157
158         oob := Collection{
159                 Name:      "oob",
160                 OwnerUUID: arvadostest.AProjectUUID,
161         }
162         err = s.client.RequestAndDecode(&oob, "POST", "arvados/v1/collections", s.client.UpdateBody(&oob), nil)
163         c.Assert(err, check.IsNil)
164         defer s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+oob.UUID, nil, nil)
165
166         err = project.Sync()
167         c.Check(err, check.IsNil)
168         f, err := s.fs.Open("/home/A Project/oob")
169         c.Assert(err, check.IsNil)
170         fi, err := f.Stat()
171         c.Assert(err, check.IsNil)
172         c.Check(fi.IsDir(), check.Equals, true)
173         f.Close()
174
175         wf, err := s.fs.OpenFile("/home/A Project/oob/test.txt", os.O_CREATE|os.O_RDWR, 0700)
176         c.Assert(err, check.IsNil)
177         _, err = wf.Write([]byte("hello oob\n"))
178         c.Check(err, check.IsNil)
179         err = wf.Close()
180         c.Check(err, check.IsNil)
181
182         // Delete test.txt behind s.fs's back by updating the
183         // collection record with the old (empty) ManifestText.
184         err = s.client.RequestAndDecode(nil, "PATCH", "arvados/v1/collections/"+oob.UUID, s.client.UpdateBody(&oob), nil)
185         c.Assert(err, check.IsNil)
186
187         err = project.Sync()
188         c.Check(err, check.IsNil)
189         _, err = s.fs.Open("/home/A Project/oob/test.txt")
190         c.Check(err, check.NotNil)
191         _, err = s.fs.Open("/home/A Project/oob")
192         c.Check(err, check.IsNil)
193
194         err = s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+oob.UUID, nil, nil)
195         c.Assert(err, check.IsNil)
196
197         err = project.Sync()
198         c.Check(err, check.IsNil)
199         _, err = s.fs.Open("/home/A Project/oob")
200         c.Check(err, check.NotNil)
201 }