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) TestFilterGroup(c *check.C) {
43 // Make sure that a collection and group that match the filter are present,
44 // and that a group that does not match the filter is not present.
45 s.fs.MountProject("fg", fixtureThisFilterGroupUUID)
47 _, err := s.fs.OpenFile("/fg/baz_file", 0, 0)
48 c.Assert(err, check.IsNil)
50 _, err = s.fs.OpenFile("/fg/A Subproject", 0, 0)
51 c.Assert(err, check.IsNil)
53 _, err = s.fs.OpenFile("/fg/A Project", 0, 0)
54 c.Assert(err, check.Not(check.IsNil))
56 // An empty filter means everything that is visible should be returned.
57 s.fs.MountProject("fg2", fixtureAFilterGroupTwoUUID)
59 _, err = s.fs.OpenFile("/fg2/baz_file", 0, 0)
60 c.Assert(err, check.IsNil)
62 _, err = s.fs.OpenFile("/fg2/A Subproject", 0, 0)
63 c.Assert(err, check.IsNil)
65 _, err = s.fs.OpenFile("/fg2/A Project", 0, 0)
66 c.Assert(err, check.IsNil)
68 // An 'is_a' 'arvados#collection' filter means only collections should be returned.
69 s.fs.MountProject("fg3", fixtureAFilterGroupThreeUUID)
71 _, err = s.fs.OpenFile("/fg3/baz_file", 0, 0)
72 c.Assert(err, check.IsNil)
74 _, err = s.fs.OpenFile("/fg3/A Subproject", 0, 0)
75 c.Assert(err, check.Not(check.IsNil))
77 // An 'exists' 'arvados#collection' filter means only collections with certain properties should be returned.
78 s.fs.MountProject("fg4", fixtureAFilterGroupFourUUID)
80 _, err = s.fs.Stat("/fg4/collection with list property with odd values")
81 c.Assert(err, check.IsNil)
83 _, err = s.fs.Stat("/fg4/collection with list property with even values")
84 c.Assert(err, check.IsNil)
86 // A 'contains' 'arvados#collection' filter means only collections with certain properties should be returned.
87 s.fs.MountProject("fg5", fixtureAFilterGroupFiveUUID)
89 _, err = s.fs.Stat("/fg5/collection with list property with odd values")
90 c.Assert(err, check.IsNil)
92 _, err = s.fs.Stat("/fg5/collection with list property with string value")
93 c.Assert(err, check.IsNil)
95 _, err = s.fs.Stat("/fg5/collection with prop2 5")
96 c.Assert(err, check.Not(check.IsNil))
98 _, err = s.fs.Stat("/fg5/collection with list property with even values")
99 c.Assert(err, check.Not(check.IsNil))
102 func (s *SiteFSSuite) TestCurrentUserHome(c *check.C) {
103 s.fs.MountProject("home", "")
104 s.testHomeProject(c, "/home", "home")
107 func (s *SiteFSSuite) TestUsersDir(c *check.C) {
108 // /users/active is a hardlink to a dir whose name is the UUID
109 // of the active user
110 s.testHomeProject(c, "/users/active", fixtureActiveUserUUID)
113 func (s *SiteFSSuite) testHomeProject(c *check.C, path, expectRealName string) {
114 f, err := s.fs.Open(path)
115 c.Assert(err, check.IsNil)
116 fis, err := f.Readdir(-1)
117 c.Assert(err, check.IsNil)
118 c.Check(len(fis), check.Not(check.Equals), 0)
121 for _, fi := range fis {
122 c.Check(fi.Name(), check.Not(check.Equals), "")
123 if fi.Name() == "A Project" {
127 c.Check(ok, check.Equals, true)
129 f, err = s.fs.Open(path + "/A Project/..")
130 c.Assert(err, check.IsNil)
132 c.Assert(err, check.IsNil)
133 c.Check(fi.IsDir(), check.Equals, true)
134 c.Check(fi.Name(), check.Equals, expectRealName)
136 f, err = s.fs.Open(path + "/A Project/A Subproject")
137 c.Assert(err, check.IsNil)
139 c.Assert(err, check.IsNil)
140 c.Check(fi.IsDir(), check.Equals, true)
142 for _, nx := range []string{
143 path + "/Unrestricted public data",
144 path + "/Unrestricted public data/does not exist",
145 path + "/A Project/does not exist",
148 f, err = s.fs.Open(nx)
149 c.Check(err, check.NotNil)
150 c.Check(os.IsNotExist(err), check.Equals, true)
154 func (s *SiteFSSuite) TestProjectReaddirAfterLoadOne(c *check.C) {
155 f, err := s.fs.Open("/users/active/A Project/A Subproject")
156 c.Assert(err, check.IsNil)
158 f, err = s.fs.Open("/users/active/A Project/Project does not exist")
159 c.Assert(err, check.NotNil)
160 f, err = s.fs.Open("/users/active/A Project/A Subproject")
161 c.Assert(err, check.IsNil)
163 f, err = s.fs.Open("/users/active/A Project")
164 c.Assert(err, check.IsNil)
166 fis, err := f.Readdir(-1)
167 c.Assert(err, check.IsNil)
169 var foundSubproject, foundCollection bool
170 for _, fi := range fis {
173 foundSubproject = true
174 case "collection_to_move_around":
175 foundCollection = true
178 c.Check(foundSubproject, check.Equals, true)
179 c.Check(foundCollection, check.Equals, true)
182 func (s *SiteFSSuite) TestSlashInName(c *check.C) {
183 var badCollection Collection
184 err := s.client.RequestAndDecode(&badCollection, "POST", "arvados/v1/collections", nil, map[string]interface{}{
185 "collection": map[string]string{
186 "name": "bad/collection",
187 "owner_uuid": fixtureAProjectUUID,
190 c.Assert(err, check.IsNil)
191 defer s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+badCollection.UUID, nil, nil)
194 err = s.client.RequestAndDecode(&badProject, "POST", "arvados/v1/groups", nil, map[string]interface{}{
195 "group": map[string]string{
196 "name": "bad/project",
197 "group_class": "project",
198 "owner_uuid": fixtureAProjectUUID,
201 c.Assert(err, check.IsNil)
202 defer s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/groups/"+badProject.UUID, nil, nil)
204 dir, err := s.fs.Open("/users/active/A Project")
205 c.Assert(err, check.IsNil)
206 fis, err := dir.Readdir(-1)
207 c.Check(err, check.IsNil)
208 for _, fi := range fis {
209 c.Logf("fi.Name() == %q", fi.Name())
210 c.Check(strings.Contains(fi.Name(), "/"), check.Equals, false)
213 // Make a new fs (otherwise content will still be cached from
214 // above) and enable "/" replacement string.
215 s.fs = s.client.SiteFileSystem(s.kc)
216 s.fs.ForwardSlashNameSubstitution("___")
217 dir, err = s.fs.Open("/users/active/A Project/bad___collection")
218 if c.Check(err, check.IsNil) {
219 _, err = dir.Readdir(-1)
220 c.Check(err, check.IsNil)
222 dir, err = s.fs.Open("/users/active/A Project/bad___project")
223 if c.Check(err, check.IsNil) {
224 _, err = dir.Readdir(-1)
225 c.Check(err, check.IsNil)
229 func (s *SiteFSSuite) TestProjectUpdatedByOther(c *check.C) {
230 s.fs.MountProject("home", "")
232 project, err := s.fs.OpenFile("/home/A Project", 0, 0)
233 c.Assert(err, check.IsNil)
235 _, err = s.fs.Open("/home/A Project/oob")
236 c.Check(err, check.NotNil)
239 err = s.client.RequestAndDecode(&oob, "POST", "arvados/v1/collections", nil, map[string]interface{}{
240 "collection": map[string]string{
242 "owner_uuid": fixtureAProjectUUID,
245 c.Assert(err, check.IsNil)
246 defer s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+oob.UUID, nil, nil)
249 c.Check(err, check.IsNil)
250 f, err := s.fs.Open("/home/A Project/oob")
251 c.Assert(err, check.IsNil)
253 c.Assert(err, check.IsNil)
254 c.Check(fi.IsDir(), check.Equals, true)
257 wf, err := s.fs.OpenFile("/home/A Project/oob/test.txt", os.O_CREATE|os.O_RDWR, 0700)
258 c.Assert(err, check.IsNil)
259 _, err = wf.Write([]byte("hello oob\n"))
260 c.Check(err, check.IsNil)
262 c.Check(err, check.IsNil)
265 c.Check(err, check.IsNil)
266 f, err = s.fs.Open("/home/A Project/oob/test.txt")
267 if c.Check(err, check.IsNil) {
271 // Ensure collection was flushed by Sync
272 var latest Collection
273 err = s.client.RequestAndDecode(&latest, "GET", "arvados/v1/collections/"+oob.UUID, nil, nil)
274 c.Check(err, check.IsNil)
275 c.Check(latest.ManifestText, check.Matches, `.*:test.txt.*\n`)
277 // Delete test.txt behind s.fs's back by updating the
278 // collection record with an empty ManifestText.
279 err = s.client.RequestAndDecode(nil, "PATCH", "arvados/v1/collections/"+oob.UUID, nil, map[string]interface{}{
280 "collection": map[string]string{
282 "portable_data_hash": "d41d8cd98f00b204e9800998ecf8427e+0",
285 c.Assert(err, check.IsNil)
287 // Sync again to reload collection.
289 c.Check(err, check.IsNil)
291 // Check test.txt deletion is reflected in fs.
292 _, err = s.fs.Open("/home/A Project/oob/test.txt")
293 c.Check(err, check.NotNil)
294 f, err = s.fs.Open("/home/A Project/oob")
295 if c.Check(err, check.IsNil) {
299 err = s.client.RequestAndDecode(nil, "DELETE", "arvados/v1/collections/"+oob.UUID, nil, nil)
300 c.Assert(err, check.IsNil)
302 wf, err = s.fs.OpenFile("/home/A Project/oob/test.txt", os.O_CREATE|os.O_RDWR, 0700)
303 c.Assert(err, check.IsNil)
305 c.Check(err, check.IsNil)
308 c.Check(err, check.NotNil) // can't update the deleted collection
309 _, err = s.fs.Open("/home/A Project/oob")
310 c.Check(err, check.IsNil) // parent dir still has old collection -- didn't reload, because Sync failed
313 func (s *SiteFSSuite) TestProjectUnsupportedOperations(c *check.C) {
314 s.fs.MountByID("by_id")
315 s.fs.MountProject("home", "")
317 _, err := s.fs.OpenFile("/home/A Project/newfilename", os.O_CREATE|os.O_RDWR, 0)
318 c.Check(err, ErrorIs, ErrInvalidOperation)
320 err = s.fs.Mkdir("/home/A Project/newdirname", 0)
321 c.Check(err, ErrorIs, ErrInvalidOperation)
323 err = s.fs.Mkdir("/by_id/newdirname", 0)
324 c.Check(err, ErrorIs, ErrInvalidOperation)
326 err = s.fs.Mkdir("/by_id/"+fixtureAProjectUUID+"/newdirname", 0)
327 c.Check(err, ErrorIs, ErrInvalidOperation)
329 _, err = s.fs.OpenFile("/home/A Project", 0, 0)
330 c.Check(err, check.IsNil)
333 type errorIsChecker struct {
337 var ErrorIs check.Checker = errorIsChecker{
338 &check.CheckerInfo{Name: "ErrorIs", Params: []string{"value", "target"}},
341 func (checker errorIsChecker) Check(params []interface{}, names []string) (result bool, errStr string) {
342 err, ok := params[0].(error)
346 target, ok := params[1].(error)
350 return errors.Is(err, target), ""