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