17778: Merge branch 'master' into 17778-doc-update
[arvados.git] / sdk / go / arvados / fs_site_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         "net/http"
9         "os"
10         "time"
11
12         check "gopkg.in/check.v1"
13 )
14
15 const (
16         // Importing arvadostest would be an import cycle, so these
17         // fixtures are duplicated here [until fs moves to a separate
18         // package].
19         fixtureActiveToken             = "3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi"
20         fixtureAProjectUUID            = "zzzzz-j7d0g-v955i6s2oi1cbso"
21         fixtureThisFilterGroupUUID     = "zzzzz-j7d0g-thisfiltergroup"
22         fixtureAFilterGroupTwoUUID     = "zzzzz-j7d0g-afiltergrouptwo"
23         fixtureAFilterGroupThreeUUID   = "zzzzz-j7d0g-filtergroupthre"
24         fixtureFooAndBarFilesInDirUUID = "zzzzz-4zz18-foonbarfilesdir"
25         fixtureFooCollectionName       = "zzzzz-4zz18-fy296fx3hot09f7 added sometime"
26         fixtureFooCollectionPDH        = "1f4b0bc7583c2a7f9102c395f4ffc5e3+45"
27         fixtureFooCollection           = "zzzzz-4zz18-fy296fx3hot09f7"
28         fixtureNonexistentCollection   = "zzzzz-4zz18-totallynotexist"
29         fixtureBlobSigningKey          = "zfhgfenhffzltr9dixws36j1yhksjoll2grmku38mi7yxd66h5j4q9w4jzanezacp8s6q0ro3hxakfye02152hncy6zml2ed0uc"
30         fixtureBlobSigningTTL          = 336 * time.Hour
31 )
32
33 var _ = check.Suite(&SiteFSSuite{})
34
35 func init() {
36         // Enable DebugLocksPanicMode sometimes. Don't enable it all
37         // the time, though -- it adds many calls to time.Sleep(),
38         // which could hide different bugs.
39         if time.Now().Second()&1 == 0 {
40                 DebugLocksPanicMode = true
41         }
42 }
43
44 type SiteFSSuite struct {
45         client *Client
46         fs     CustomFileSystem
47         kc     keepClient
48 }
49
50 func (s *SiteFSSuite) SetUpTest(c *check.C) {
51         s.client = &Client{
52                 APIHost:   os.Getenv("ARVADOS_API_HOST"),
53                 AuthToken: fixtureActiveToken,
54                 Insecure:  true,
55         }
56         s.kc = &keepClientStub{
57                 blocks: map[string][]byte{
58                         "3858f62230ac3c915f300c664312c63f": []byte("foobar"),
59                 },
60                 sigkey:    fixtureBlobSigningKey,
61                 sigttl:    fixtureBlobSigningTTL,
62                 authToken: fixtureActiveToken,
63         }
64         s.fs = s.client.SiteFileSystem(s.kc)
65 }
66
67 func (s *SiteFSSuite) TestHttpFileSystemInterface(c *check.C) {
68         _, ok := s.fs.(http.FileSystem)
69         c.Check(ok, check.Equals, true)
70 }
71
72 func (s *SiteFSSuite) TestByIDEmpty(c *check.C) {
73         f, err := s.fs.Open("/by_id")
74         c.Assert(err, check.IsNil)
75         fis, err := f.Readdir(-1)
76         c.Check(err, check.IsNil)
77         c.Check(len(fis), check.Equals, 0)
78 }
79
80 func (s *SiteFSSuite) TestByUUIDAndPDH(c *check.C) {
81         f, err := s.fs.Open("/by_id")
82         c.Assert(err, check.IsNil)
83         fis, err := f.Readdir(-1)
84         c.Check(err, check.IsNil)
85         c.Check(len(fis), check.Equals, 0)
86
87         err = s.fs.Mkdir("/by_id/"+fixtureFooCollection, 0755)
88         c.Check(err, check.Equals, os.ErrExist)
89
90         f, err = s.fs.Open("/by_id/" + fixtureNonexistentCollection)
91         c.Assert(err, check.Equals, os.ErrNotExist)
92
93         for _, path := range []string{
94                 fixtureFooCollection,
95                 fixtureFooCollectionPDH,
96                 fixtureAProjectUUID + "/" + fixtureFooCollectionName,
97         } {
98                 f, err = s.fs.Open("/by_id/" + path)
99                 c.Assert(err, check.IsNil)
100                 fis, err = f.Readdir(-1)
101                 c.Assert(err, check.IsNil)
102                 var names []string
103                 for _, fi := range fis {
104                         names = append(names, fi.Name())
105                 }
106                 c.Check(names, check.DeepEquals, []string{"foo"})
107         }
108
109         f, err = s.fs.Open("/by_id/" + fixtureAProjectUUID + "/A Subproject/baz_file")
110         c.Assert(err, check.IsNil)
111         fis, err = f.Readdir(-1)
112         c.Assert(err, check.IsNil)
113         var names []string
114         for _, fi := range fis {
115                 names = append(names, fi.Name())
116         }
117         c.Check(names, check.DeepEquals, []string{"baz"})
118
119         _, err = s.fs.OpenFile("/by_id/"+fixtureNonexistentCollection, os.O_RDWR|os.O_CREATE, 0755)
120         c.Check(err, check.Equals, ErrInvalidArgument)
121         err = s.fs.Rename("/by_id/"+fixtureFooCollection, "/by_id/beep")
122         c.Check(err, check.Equals, ErrInvalidArgument)
123         err = s.fs.Rename("/by_id/"+fixtureFooCollection+"/foo", "/by_id/beep")
124         c.Check(err, check.Equals, ErrInvalidArgument)
125         _, err = s.fs.Stat("/by_id/beep")
126         c.Check(err, check.Equals, os.ErrNotExist)
127         err = s.fs.Rename("/by_id/"+fixtureFooCollection+"/foo", "/by_id/"+fixtureFooCollection+"/bar")
128         c.Check(err, check.IsNil)
129
130         err = s.fs.Rename("/by_id", "/beep")
131         c.Check(err, check.Equals, ErrInvalidArgument)
132 }