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