Merge branch '16698-debug-ports-arvbox' into master
[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         check "gopkg.in/check.v1"
16 )
17
18 type spiedRequest struct {
19         method string
20         path   string
21         params map[string]interface{}
22 }
23
24 type spyingClient struct {
25         *Client
26         calls []spiedRequest
27 }
28
29 func (sc *spyingClient) RequestAndDecode(dst interface{}, method, path string, body io.Reader, params interface{}) error {
30         var paramsCopy map[string]interface{}
31         var buf bytes.Buffer
32         json.NewEncoder(&buf).Encode(params)
33         json.NewDecoder(&buf).Decode(&paramsCopy)
34         sc.calls = append(sc.calls, spiedRequest{
35                 method: method,
36                 path:   path,
37                 params: paramsCopy,
38         })
39         return sc.Client.RequestAndDecode(dst, method, path, body, params)
40 }
41
42 func (s *SiteFSSuite) TestCurrentUserHome(c *check.C) {
43         s.fs.MountProject("home", "")
44         s.testHomeProject(c, "/home")
45 }
46
47 func (s *SiteFSSuite) TestUsersDir(c *check.C) {
48         s.testHomeProject(c, "/users/active")
49 }
50
51 func (s *SiteFSSuite) testHomeProject(c *check.C, path string) {
52         f, err := s.fs.Open(path)
53         c.Assert(err, check.IsNil)
54         fis, err := f.Readdir(-1)
55         c.Assert(err, check.IsNil)
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         var badCollection Collection
123         err := s.client.RequestAndDecode(&badCollection, "POST", "arvados/v1/collections", nil, map[string]interface{}{
124                 "collection": map[string]string{
125                         "name":       "bad/collection",
126                         "owner_uuid": fixtureAProjectUUID,
127                 },
128         })
129         c.Assert(err, check.IsNil)
130         defer s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+badCollection.UUID, nil, nil)
131
132         var badProject Group
133         err = s.client.RequestAndDecode(&badProject, "POST", "arvados/v1/groups", nil, map[string]interface{}{
134                 "group": map[string]string{
135                         "name":        "bad/project",
136                         "group_class": "project",
137                         "owner_uuid":  fixtureAProjectUUID,
138                 },
139         })
140         c.Assert(err, check.IsNil)
141         defer s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/groups/"+badProject.UUID, nil, nil)
142
143         dir, err := s.fs.Open("/users/active/A Project")
144         c.Assert(err, check.IsNil)
145         fis, err := dir.Readdir(-1)
146         c.Check(err, check.IsNil)
147         for _, fi := range fis {
148                 c.Logf("fi.Name() == %q", fi.Name())
149                 c.Check(strings.Contains(fi.Name(), "/"), check.Equals, false)
150         }
151
152         // Make a new fs (otherwise content will still be cached from
153         // above) and enable "/" replacement string.
154         s.fs = s.client.SiteFileSystem(s.kc)
155         s.fs.ForwardSlashNameSubstitution("___")
156         dir, err = s.fs.Open("/users/active/A Project/bad___collection")
157         if c.Check(err, check.IsNil) {
158                 _, err = dir.Readdir(-1)
159                 c.Check(err, check.IsNil)
160         }
161         dir, err = s.fs.Open("/users/active/A Project/bad___project")
162         if c.Check(err, check.IsNil) {
163                 _, err = dir.Readdir(-1)
164                 c.Check(err, check.IsNil)
165         }
166 }
167
168 func (s *SiteFSSuite) TestProjectUpdatedByOther(c *check.C) {
169         s.fs.MountProject("home", "")
170
171         project, err := s.fs.OpenFile("/home/A Project", 0, 0)
172         c.Assert(err, check.IsNil)
173
174         _, err = s.fs.Open("/home/A Project/oob")
175         c.Check(err, check.NotNil)
176
177         var oob Collection
178         err = s.client.RequestAndDecode(&oob, "POST", "arvados/v1/collections", nil, map[string]interface{}{
179                 "collection": map[string]string{
180                         "name":       "oob",
181                         "owner_uuid": fixtureAProjectUUID,
182                 },
183         })
184         c.Assert(err, check.IsNil)
185         defer s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+oob.UUID, nil, nil)
186
187         err = project.Sync()
188         c.Check(err, check.IsNil)
189         f, err := s.fs.Open("/home/A Project/oob")
190         c.Assert(err, check.IsNil)
191         fi, err := f.Stat()
192         c.Assert(err, check.IsNil)
193         c.Check(fi.IsDir(), check.Equals, true)
194         f.Close()
195
196         wf, err := s.fs.OpenFile("/home/A Project/oob/test.txt", os.O_CREATE|os.O_RDWR, 0700)
197         c.Assert(err, check.IsNil)
198         _, err = wf.Write([]byte("hello oob\n"))
199         c.Check(err, check.IsNil)
200         err = wf.Close()
201         c.Check(err, check.IsNil)
202
203         err = project.Sync()
204         c.Check(err, check.IsNil)
205         _, err = s.fs.Open("/home/A Project/oob/test.txt")
206         c.Check(err, check.IsNil)
207
208         // Sync again to mark the project dir as stale, so the
209         // collection gets reloaded from the controller on next
210         // lookup.
211         err = project.Sync()
212         c.Check(err, check.IsNil)
213
214         // Ensure collection was flushed by Sync
215         var latest Collection
216         err = s.client.RequestAndDecode(&latest, "GET", "arvados/v1/collections/"+oob.UUID, nil, nil)
217         c.Check(latest.ManifestText, check.Matches, `.*:test.txt.*\n`)
218
219         // Delete test.txt behind s.fs's back by updating the
220         // collection record with an empty ManifestText.
221         err = s.client.RequestAndDecode(nil, "PATCH", "arvados/v1/collections/"+oob.UUID, nil, map[string]interface{}{
222                 "collection": map[string]string{
223                         "manifest_text":      "",
224                         "portable_data_hash": "d41d8cd98f00b204e9800998ecf8427e+0",
225                 },
226         })
227         c.Assert(err, check.IsNil)
228
229         _, err = s.fs.Open("/home/A Project/oob/test.txt")
230         c.Check(err, check.NotNil)
231         _, err = s.fs.Open("/home/A Project/oob")
232         c.Check(err, check.IsNil)
233
234         err = s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+oob.UUID, nil, nil)
235         c.Assert(err, check.IsNil)
236
237         err = project.Sync()
238         c.Check(err, check.NotNil) // can't update the deleted collection
239         _, err = s.fs.Open("/home/A Project/oob")
240         c.Check(err, check.IsNil) // parent dir still has old collection -- didn't reload, because Sync failed
241 }
242
243 func (s *SiteFSSuite) TestProjectUnsupportedOperations(c *check.C) {
244         s.fs.MountByID("by_id")
245         s.fs.MountProject("home", "")
246
247         _, err := s.fs.OpenFile("/home/A Project/newfilename", os.O_CREATE|os.O_RDWR, 0)
248         c.Check(err, check.ErrorMatches, "invalid argument")
249
250         err = s.fs.Mkdir("/home/A Project/newdirname", 0)
251         c.Check(err, check.ErrorMatches, "invalid argument")
252
253         err = s.fs.Mkdir("/by_id/newdirname", 0)
254         c.Check(err, check.ErrorMatches, "invalid argument")
255
256         err = s.fs.Mkdir("/by_id/"+fixtureAProjectUUID+"/newdirname", 0)
257         c.Check(err, check.ErrorMatches, "invalid argument")
258
259         _, err = s.fs.OpenFile("/home/A Project", 0, 0)
260         c.Check(err, check.IsNil)
261 }