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