fff0b7e010b22b1811991ce3b6249093c50b616b
[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
11         check "gopkg.in/check.v1"
12 )
13
14 const (
15         // Importing arvadostest would be an import cycle, so these
16         // fixtures are duplicated here [until fs moves to a separate
17         // package].
18         fixtureActiveToken             = "3kg6k6lzmp9kj5cpkcoxie963cmvjahbt2fod9zru30k1jqdmi"
19         fixtureAProjectUUID            = "zzzzz-j7d0g-v955i6s2oi1cbso"
20         fixtureFooAndBarFilesInDirUUID = "zzzzz-4zz18-foonbarfilesdir"
21         fixtureFooCollectionName       = "zzzzz-4zz18-fy296fx3hot09f7 added sometime"
22         fixtureFooCollectionPDH        = "1f4b0bc7583c2a7f9102c395f4ffc5e3+45"
23         fixtureFooCollection           = "zzzzz-4zz18-fy296fx3hot09f7"
24         fixtureNonexistentCollection   = "zzzzz-4zz18-totallynotexist"
25 )
26
27 var _ = check.Suite(&SiteFSSuite{})
28
29 type SiteFSSuite struct {
30         client *Client
31         fs     CustomFileSystem
32         kc     keepClient
33 }
34
35 func (s *SiteFSSuite) SetUpTest(c *check.C) {
36         s.client = &Client{
37                 APIHost:   os.Getenv("ARVADOS_API_HOST"),
38                 AuthToken: fixtureActiveToken,
39                 Insecure:  true,
40         }
41         s.kc = &keepClientStub{
42                 blocks: map[string][]byte{
43                         "3858f62230ac3c915f300c664312c63f": []byte("foobar"),
44                 }}
45         s.fs = s.client.SiteFileSystem(s.kc)
46 }
47
48 func (s *SiteFSSuite) TestHttpFileSystemInterface(c *check.C) {
49         _, ok := s.fs.(http.FileSystem)
50         c.Check(ok, check.Equals, true)
51 }
52
53 func (s *SiteFSSuite) TestByIDEmpty(c *check.C) {
54         f, err := s.fs.Open("/by_id")
55         c.Assert(err, check.IsNil)
56         fis, err := f.Readdir(-1)
57         c.Check(err, check.IsNil)
58         c.Check(len(fis), check.Equals, 0)
59 }
60
61 func (s *SiteFSSuite) TestByUUIDAndPDH(c *check.C) {
62         f, err := s.fs.Open("/by_id")
63         c.Assert(err, check.IsNil)
64         fis, err := f.Readdir(-1)
65         c.Check(err, check.IsNil)
66         c.Check(len(fis), check.Equals, 0)
67
68         err = s.fs.Mkdir("/by_id/"+fixtureFooCollection, 0755)
69         c.Check(err, check.Equals, os.ErrExist)
70
71         f, err = s.fs.Open("/by_id/" + fixtureNonexistentCollection)
72         c.Assert(err, check.Equals, os.ErrNotExist)
73
74         for _, path := range []string{
75                 fixtureFooCollection,
76                 fixtureFooCollectionPDH,
77                 fixtureAProjectUUID + "/" + fixtureFooCollectionName,
78         } {
79                 f, err = s.fs.Open("/by_id/" + path)
80                 c.Assert(err, check.IsNil)
81                 fis, err = f.Readdir(-1)
82                 var names []string
83                 for _, fi := range fis {
84                         names = append(names, fi.Name())
85                 }
86                 c.Check(names, check.DeepEquals, []string{"foo"})
87         }
88
89         f, err = s.fs.Open("/by_id/" + fixtureAProjectUUID + "/A Subproject/baz_file")
90         c.Assert(err, check.IsNil)
91         fis, err = f.Readdir(-1)
92         var names []string
93         for _, fi := range fis {
94                 names = append(names, fi.Name())
95         }
96         c.Check(names, check.DeepEquals, []string{"baz"})
97
98         _, err = s.fs.OpenFile("/by_id/"+fixtureNonexistentCollection, os.O_RDWR|os.O_CREATE, 0755)
99         c.Check(err, check.Equals, ErrInvalidOperation)
100         err = s.fs.Rename("/by_id/"+fixtureFooCollection, "/by_id/beep")
101         c.Check(err, check.Equals, ErrInvalidArgument)
102         err = s.fs.Rename("/by_id/"+fixtureFooCollection+"/foo", "/by_id/beep")
103         c.Check(err, check.Equals, ErrInvalidArgument)
104         _, err = s.fs.Stat("/by_id/beep")
105         c.Check(err, check.Equals, os.ErrNotExist)
106         err = s.fs.Rename("/by_id/"+fixtureFooCollection+"/foo", "/by_id/"+fixtureFooCollection+"/bar")
107         c.Check(err, check.IsNil)
108
109         err = s.fs.Rename("/by_id", "/beep")
110         c.Check(err, check.Equals, ErrInvalidArgument)
111 }