21700: Install Bundler system-wide in Rails postinst
[arvados.git] / services / keepstore / mounts_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package keepstore
6
7 import (
8         "context"
9         "encoding/json"
10         "net/http"
11
12         . "gopkg.in/check.v1"
13 )
14
15 func (s *routerSuite) TestMounts(c *C) {
16         router, cancel := testRouter(c, s.cluster, nil)
17         defer cancel()
18
19         router.keepstore.mountsW[0].BlockWrite(context.Background(), fooHash, []byte("foo"))
20         router.keepstore.mountsW[1].BlockWrite(context.Background(), barHash, []byte("bar"))
21
22         resp := call(router, "GET", "/mounts", s.cluster.SystemRootToken, nil, nil)
23         c.Check(resp.Code, Equals, http.StatusOK)
24         c.Log(resp.Body.String())
25
26         var mntList []struct {
27                 UUID           string          `json:"uuid"`
28                 DeviceID       string          `json:"device_id"`
29                 ReadOnly       bool            `json:"read_only"`
30                 Replication    int             `json:"replication"`
31                 StorageClasses map[string]bool `json:"storage_classes"`
32         }
33         err := json.Unmarshal(resp.Body.Bytes(), &mntList)
34         c.Assert(err, IsNil)
35         c.Assert(mntList, HasLen, 2)
36
37         for _, m := range mntList {
38                 c.Check(len(m.UUID), Equals, 27)
39                 c.Check(m.UUID[:12], Equals, "zzzzz-nyw5e-")
40                 c.Check(m.DeviceID, Matches, "0x[0-9a-f]+")
41                 c.Check(m.ReadOnly, Equals, false)
42                 c.Check(m.Replication, Equals, 1)
43                 c.Check(m.StorageClasses, HasLen, 1)
44                 for k := range m.StorageClasses {
45                         c.Check(k, Matches, "testclass.*")
46                 }
47         }
48         c.Check(mntList[0].UUID, Not(Equals), mntList[1].UUID)
49
50         c.Logf("=== bad auth")
51         for _, tok := range []string{"", "xyzzy"} {
52                 resp = call(router, "GET", "/mounts/"+mntList[1].UUID+"/blocks", tok, nil, nil)
53                 if tok == "" {
54                         c.Check(resp.Code, Equals, http.StatusUnauthorized)
55                         c.Check(resp.Body.String(), Equals, "Unauthorized\n")
56                 } else {
57                         c.Check(resp.Code, Equals, http.StatusForbidden)
58                         c.Check(resp.Body.String(), Equals, "Forbidden\n")
59                 }
60         }
61
62         c.Logf("=== nonexistent mount UUID")
63         resp = call(router, "GET", "/mounts/X/blocks", s.cluster.SystemRootToken, nil, nil)
64         c.Check(resp.Code, Equals, http.StatusNotFound)
65
66         c.Logf("=== complete index of first mount")
67         resp = call(router, "GET", "/mounts/"+mntList[0].UUID+"/blocks", s.cluster.SystemRootToken, nil, nil)
68         c.Check(resp.Code, Equals, http.StatusOK)
69         c.Check(resp.Body.String(), Matches, fooHash+`\+[0-9]+ [0-9]+\n\n`)
70
71         c.Logf("=== partial index of first mount (one block matches prefix)")
72         resp = call(router, "GET", "/mounts/"+mntList[0].UUID+"/blocks?prefix="+fooHash[:2], s.cluster.SystemRootToken, nil, nil)
73         c.Check(resp.Code, Equals, http.StatusOK)
74         c.Check(resp.Body.String(), Matches, fooHash+`\+[0-9]+ [0-9]+\n\n`)
75
76         c.Logf("=== complete index of second mount (note trailing slash)")
77         resp = call(router, "GET", "/mounts/"+mntList[1].UUID+"/blocks/", s.cluster.SystemRootToken, nil, nil)
78         c.Check(resp.Code, Equals, http.StatusOK)
79         c.Check(resp.Body.String(), Matches, barHash+`\+[0-9]+ [0-9]+\n\n`)
80
81         c.Logf("=== partial index of second mount (no blocks match prefix)")
82         resp = call(router, "GET", "/mounts/"+mntList[1].UUID+"/blocks/?prefix="+fooHash[:2], s.cluster.SystemRootToken, nil, nil)
83         c.Check(resp.Code, Equals, http.StatusOK)
84         c.Check(resp.Body.String(), Equals, "\n")
85 }