Merge branch '15964-fix-docs' refs #15964
[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         fixtureFooAndBarFilesInDirUUID = "zzzzz-4zz18-foonbarfilesdir"
22         fixtureFooCollectionName       = "zzzzz-4zz18-fy296fx3hot09f7 added sometime"
23         fixtureFooCollectionPDH        = "1f4b0bc7583c2a7f9102c395f4ffc5e3+45"
24         fixtureFooCollection           = "zzzzz-4zz18-fy296fx3hot09f7"
25         fixtureNonexistentCollection   = "zzzzz-4zz18-totallynotexist"
26         fixtureBlobSigningKey          = "zfhgfenhffzltr9dixws36j1yhksjoll2grmku38mi7yxd66h5j4q9w4jzanezacp8s6q0ro3hxakfye02152hncy6zml2ed0uc"
27         fixtureBlobSigningTTL          = 336 * time.Hour
28 )
29
30 var _ = check.Suite(&SiteFSSuite{})
31
32 type SiteFSSuite struct {
33         client *Client
34         fs     CustomFileSystem
35         kc     keepClient
36 }
37
38 func (s *SiteFSSuite) SetUpTest(c *check.C) {
39         s.client = &Client{
40                 APIHost:   os.Getenv("ARVADOS_API_HOST"),
41                 AuthToken: fixtureActiveToken,
42                 Insecure:  true,
43         }
44         s.kc = &keepClientStub{
45                 blocks: map[string][]byte{
46                         "3858f62230ac3c915f300c664312c63f": []byte("foobar"),
47                 },
48                 sigkey:    fixtureBlobSigningKey,
49                 sigttl:    fixtureBlobSigningTTL,
50                 authToken: fixtureActiveToken,
51         }
52         s.fs = s.client.SiteFileSystem(s.kc)
53 }
54
55 func (s *SiteFSSuite) TestHttpFileSystemInterface(c *check.C) {
56         _, ok := s.fs.(http.FileSystem)
57         c.Check(ok, check.Equals, true)
58 }
59
60 func (s *SiteFSSuite) TestByIDEmpty(c *check.C) {
61         f, err := s.fs.Open("/by_id")
62         c.Assert(err, check.IsNil)
63         fis, err := f.Readdir(-1)
64         c.Check(err, check.IsNil)
65         c.Check(len(fis), check.Equals, 0)
66 }
67
68 func (s *SiteFSSuite) TestByUUIDAndPDH(c *check.C) {
69         f, err := s.fs.Open("/by_id")
70         c.Assert(err, check.IsNil)
71         fis, err := f.Readdir(-1)
72         c.Check(err, check.IsNil)
73         c.Check(len(fis), check.Equals, 0)
74
75         err = s.fs.Mkdir("/by_id/"+fixtureFooCollection, 0755)
76         c.Check(err, check.Equals, os.ErrExist)
77
78         f, err = s.fs.Open("/by_id/" + fixtureNonexistentCollection)
79         c.Assert(err, check.Equals, os.ErrNotExist)
80
81         for _, path := range []string{
82                 fixtureFooCollection,
83                 fixtureFooCollectionPDH,
84                 fixtureAProjectUUID + "/" + fixtureFooCollectionName,
85         } {
86                 f, err = s.fs.Open("/by_id/" + path)
87                 c.Assert(err, check.IsNil)
88                 fis, err = f.Readdir(-1)
89                 c.Assert(err, check.IsNil)
90                 var names []string
91                 for _, fi := range fis {
92                         names = append(names, fi.Name())
93                 }
94                 c.Check(names, check.DeepEquals, []string{"foo"})
95         }
96
97         f, err = s.fs.Open("/by_id/" + fixtureAProjectUUID + "/A Subproject/baz_file")
98         c.Assert(err, check.IsNil)
99         fis, err = f.Readdir(-1)
100         c.Assert(err, check.IsNil)
101         var names []string
102         for _, fi := range fis {
103                 names = append(names, fi.Name())
104         }
105         c.Check(names, check.DeepEquals, []string{"baz"})
106
107         _, err = s.fs.OpenFile("/by_id/"+fixtureNonexistentCollection, os.O_RDWR|os.O_CREATE, 0755)
108         c.Check(err, check.Equals, ErrInvalidArgument)
109         err = s.fs.Rename("/by_id/"+fixtureFooCollection, "/by_id/beep")
110         c.Check(err, check.Equals, ErrInvalidArgument)
111         err = s.fs.Rename("/by_id/"+fixtureFooCollection+"/foo", "/by_id/beep")
112         c.Check(err, check.Equals, ErrInvalidArgument)
113         _, err = s.fs.Stat("/by_id/beep")
114         c.Check(err, check.Equals, os.ErrNotExist)
115         err = s.fs.Rename("/by_id/"+fixtureFooCollection+"/foo", "/by_id/"+fixtureFooCollection+"/bar")
116         c.Check(err, check.IsNil)
117
118         err = s.fs.Rename("/by_id", "/beep")
119         c.Check(err, check.Equals, ErrInvalidArgument)
120 }