17989: Passes the given timeout value to api_from_config()
[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         fixtureStorageClassesDesiredArchive = "zzzzz-4zz18-3t236wr12769qqa"
30         fixtureBlobSigningKey               = "zfhgfenhffzltr9dixws36j1yhksjoll2grmku38mi7yxd66h5j4q9w4jzanezacp8s6q0ro3hxakfye02152hncy6zml2ed0uc"
31         fixtureBlobSigningTTL               = 336 * time.Hour
32 )
33
34 var _ = check.Suite(&SiteFSSuite{})
35
36 func init() {
37         // Enable DebugLocksPanicMode sometimes. Don't enable it all
38         // the time, though -- it adds many calls to time.Sleep(),
39         // which could hide different bugs.
40         if time.Now().Second()&1 == 0 {
41                 DebugLocksPanicMode = true
42         }
43 }
44
45 type SiteFSSuite struct {
46         client *Client
47         fs     CustomFileSystem
48         kc     keepClient
49 }
50
51 func (s *SiteFSSuite) SetUpTest(c *check.C) {
52         s.client = &Client{
53                 APIHost:   os.Getenv("ARVADOS_API_HOST"),
54                 AuthToken: fixtureActiveToken,
55                 Insecure:  true,
56         }
57         s.kc = &keepClientStub{
58                 blocks: map[string][]byte{
59                         "3858f62230ac3c915f300c664312c63f": []byte("foobar"),
60                 },
61                 sigkey:    fixtureBlobSigningKey,
62                 sigttl:    fixtureBlobSigningTTL,
63                 authToken: fixtureActiveToken,
64         }
65         s.fs = s.client.SiteFileSystem(s.kc)
66 }
67
68 func (s *SiteFSSuite) TestHttpFileSystemInterface(c *check.C) {
69         _, ok := s.fs.(http.FileSystem)
70         c.Check(ok, check.Equals, true)
71 }
72
73 func (s *SiteFSSuite) TestByIDEmpty(c *check.C) {
74         f, err := s.fs.Open("/by_id")
75         c.Assert(err, check.IsNil)
76         fis, err := f.Readdir(-1)
77         c.Check(err, check.IsNil)
78         c.Check(len(fis), check.Equals, 0)
79 }
80
81 func (s *SiteFSSuite) TestUpdateStorageClasses(c *check.C) {
82         f, err := s.fs.OpenFile("/by_id/"+fixtureStorageClassesDesiredArchive+"/newfile", os.O_CREATE|os.O_RDWR, 0777)
83         c.Assert(err, check.IsNil)
84         _, err = f.Write([]byte("nope"))
85         c.Assert(err, check.IsNil)
86         err = f.Close()
87         c.Assert(err, check.IsNil)
88         err = s.fs.Sync()
89         c.Assert(err, check.ErrorMatches, `.*stub does not write storage class "archive"`)
90 }
91
92 func (s *SiteFSSuite) TestByUUIDAndPDH(c *check.C) {
93         f, err := s.fs.Open("/by_id")
94         c.Assert(err, check.IsNil)
95         fis, err := f.Readdir(-1)
96         c.Check(err, check.IsNil)
97         c.Check(len(fis), check.Equals, 0)
98
99         err = s.fs.Mkdir("/by_id/"+fixtureFooCollection, 0755)
100         c.Check(err, check.Equals, os.ErrExist)
101
102         f, err = s.fs.Open("/by_id/" + fixtureNonexistentCollection)
103         c.Assert(err, check.Equals, os.ErrNotExist)
104
105         for _, path := range []string{
106                 fixtureFooCollection,
107                 fixtureFooCollectionPDH,
108                 fixtureAProjectUUID + "/" + fixtureFooCollectionName,
109         } {
110                 f, err = s.fs.Open("/by_id/" + path)
111                 c.Assert(err, check.IsNil)
112                 fis, err = f.Readdir(-1)
113                 c.Assert(err, check.IsNil)
114                 var names []string
115                 for _, fi := range fis {
116                         names = append(names, fi.Name())
117                 }
118                 c.Check(names, check.DeepEquals, []string{"foo"})
119         }
120
121         f, err = s.fs.Open("/by_id/" + fixtureAProjectUUID + "/A Subproject/baz_file")
122         c.Assert(err, check.IsNil)
123         fis, err = f.Readdir(-1)
124         c.Assert(err, check.IsNil)
125         var names []string
126         for _, fi := range fis {
127                 names = append(names, fi.Name())
128         }
129         c.Check(names, check.DeepEquals, []string{"baz"})
130
131         _, err = s.fs.OpenFile("/by_id/"+fixtureNonexistentCollection, os.O_RDWR|os.O_CREATE, 0755)
132         c.Check(err, check.Equals, ErrInvalidArgument)
133         err = s.fs.Rename("/by_id/"+fixtureFooCollection, "/by_id/beep")
134         c.Check(err, check.Equals, ErrInvalidArgument)
135         err = s.fs.Rename("/by_id/"+fixtureFooCollection+"/foo", "/by_id/beep")
136         c.Check(err, check.Equals, ErrInvalidArgument)
137         _, err = s.fs.Stat("/by_id/beep")
138         c.Check(err, check.Equals, os.ErrNotExist)
139         err = s.fs.Rename("/by_id/"+fixtureFooCollection+"/foo", "/by_id/"+fixtureFooCollection+"/bar")
140         c.Check(err, check.IsNil)
141
142         err = s.fs.Rename("/by_id", "/beep")
143         c.Check(err, check.Equals, ErrInvalidArgument)
144 }