1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
15 check "gopkg.in/check.v1"
18 type spiedRequest struct {
21 params map[string]interface{}
24 type spyingClient struct {
29 func (sc *spyingClient) RequestAndDecode(dst interface{}, method, path string, body io.Reader, params interface{}) error {
30 var paramsCopy map[string]interface{}
32 json.NewEncoder(&buf).Encode(params)
33 json.NewDecoder(&buf).Decode(¶msCopy)
34 sc.calls = append(sc.calls, spiedRequest{
39 return sc.Client.RequestAndDecode(dst, method, path, body, params)
42 func (s *SiteFSSuite) TestCurrentUserHome(c *check.C) {
43 s.fs.MountProject("home", "")
44 s.testHomeProject(c, "/home")
47 func (s *SiteFSSuite) TestUsersDir(c *check.C) {
48 s.testHomeProject(c, "/users/active")
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)
59 for _, fi := range fis {
60 c.Check(fi.Name(), check.Not(check.Equals), "")
61 if fi.Name() == "A Project" {
65 c.Check(ok, check.Equals, true)
67 f, err = s.fs.Open(path + "/A Project/..")
68 c.Assert(err, check.IsNil)
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)
75 f, err = s.fs.Open(path + "/A Project/A Subproject")
76 c.Assert(err, check.IsNil)
78 c.Assert(err, check.IsNil)
79 c.Check(fi.IsDir(), check.Equals, true)
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",
87 f, err = s.fs.Open(nx)
88 c.Check(err, check.NotNil)
89 c.Check(os.IsNotExist(err), check.Equals, true)
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)
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)
102 f, err = s.fs.Open("/users/active/A Project")
103 c.Assert(err, check.IsNil)
105 fis, err := f.Readdir(-1)
106 c.Assert(err, check.IsNil)
108 var foundSubproject, foundCollection bool
109 for _, fi := range fis {
112 foundSubproject = true
113 case "collection_to_move_around":
114 foundCollection = true
117 c.Check(foundSubproject, check.Equals, true)
118 c.Check(foundCollection, check.Equals, true)
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,
129 c.Assert(err, check.IsNil)
130 defer s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+badCollection.UUID, nil, nil)
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,
140 c.Assert(err, check.IsNil)
141 defer s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/groups/"+badProject.UUID, nil, nil)
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)
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)
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)
168 func (s *SiteFSSuite) TestProjectUpdatedByOther(c *check.C) {
169 s.fs.MountProject("home", "")
171 project, err := s.fs.OpenFile("/home/A Project", 0, 0)
172 c.Assert(err, check.IsNil)
174 _, err = s.fs.Open("/home/A Project/oob")
175 c.Check(err, check.NotNil)
178 err = s.client.RequestAndDecode(&oob, "POST", "arvados/v1/collections", nil, map[string]interface{}{
179 "collection": map[string]string{
181 "owner_uuid": fixtureAProjectUUID,
184 c.Assert(err, check.IsNil)
185 defer s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+oob.UUID, nil, nil)
188 c.Check(err, check.IsNil)
189 f, err := s.fs.Open("/home/A Project/oob")
190 c.Assert(err, check.IsNil)
192 c.Assert(err, check.IsNil)
193 c.Check(fi.IsDir(), check.Equals, true)
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)
201 c.Check(err, check.IsNil)
204 c.Check(err, check.IsNil)
205 _, err = s.fs.Open("/home/A Project/oob/test.txt")
206 c.Check(err, check.IsNil)
208 // Sync again to mark the project dir as stale, so the
209 // collection gets reloaded from the controller on next
212 c.Check(err, check.IsNil)
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`)
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{
224 "portable_data_hash": "d41d8cd98f00b204e9800998ecf8427e+0",
227 c.Assert(err, check.IsNil)
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)
234 err = s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+oob.UUID, nil, nil)
235 c.Assert(err, check.IsNil)
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
243 func (s *SiteFSSuite) TestProjectUnsupportedOperations(c *check.C) {
244 s.fs.MountByID("by_id")
245 s.fs.MountProject("home", "")
247 _, err := s.fs.OpenFile("/home/A Project/newfilename", os.O_CREATE|os.O_RDWR, 0)
248 c.Check(err, check.ErrorMatches, "invalid argument")
250 err = s.fs.Mkdir("/home/A Project/newdirname", 0)
251 c.Check(err, check.ErrorMatches, "invalid argument")
253 err = s.fs.Mkdir("/by_id/newdirname", 0)
254 c.Check(err, check.ErrorMatches, "invalid argument")
256 err = s.fs.Mkdir("/by_id/"+fixtureAProjectUUID+"/newdirname", 0)
257 c.Check(err, check.ErrorMatches, "invalid argument")
259 _, err = s.fs.OpenFile("/home/A Project", 0, 0)
260 c.Check(err, check.IsNil)