1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
15 check "gopkg.in/check.v1"
18 var _ = check.Suite(&MountsSuite{})
20 type MountsSuite struct {
25 func (s *MountsSuite) SetUpTest(c *check.C) {
26 s.vm = MakeTestVolumeManager(2)
28 theConfig = DefaultConfig()
29 theConfig.systemAuthToken = arvadostest.DataManagerToken
30 theConfig.ManagementToken = arvadostest.ManagementToken
32 s.rtr = MakeRESTRouter(testCluster)
35 func (s *MountsSuite) TearDownTest(c *check.C) {
38 theConfig = DefaultConfig()
42 func (s *MountsSuite) TestMounts(c *check.C) {
43 vols := s.vm.AllWritable()
44 vols[0].Put(context.Background(), TestHash, TestBlock)
45 vols[1].Put(context.Background(), TestHash2, TestBlock2)
47 resp := s.call("GET", "/mounts", "", nil)
48 c.Check(resp.Code, check.Equals, http.StatusOK)
49 var mntList []struct {
50 UUID string `json:"uuid"`
51 DeviceID string `json:"device_id"`
52 ReadOnly bool `json:"read_only"`
53 Replication int `json:"replication"`
54 StorageClasses []string `json:"storage_classes"`
56 err := json.Unmarshal(resp.Body.Bytes(), &mntList)
57 c.Assert(err, check.IsNil)
58 c.Assert(len(mntList), check.Equals, 2)
59 for _, m := range mntList {
60 c.Check(len(m.UUID), check.Equals, 27)
61 c.Check(m.UUID[:12], check.Equals, "zzzzz-ivpuk-")
62 c.Check(m.DeviceID, check.Equals, "mock-device-id")
63 c.Check(m.ReadOnly, check.Equals, false)
64 c.Check(m.Replication, check.Equals, 1)
65 c.Check(m.StorageClasses, check.DeepEquals, []string{"default"})
67 c.Check(mntList[0].UUID, check.Not(check.Equals), mntList[1].UUID)
70 for _, tok := range []string{"", "xyzzy"} {
71 resp = s.call("GET", "/mounts/"+mntList[1].UUID+"/blocks", tok, nil)
72 c.Check(resp.Code, check.Equals, http.StatusUnauthorized)
73 c.Check(resp.Body.String(), check.Equals, "Unauthorized\n")
76 tok := arvadostest.DataManagerToken
78 // Nonexistent mount UUID
79 resp = s.call("GET", "/mounts/X/blocks", tok, nil)
80 c.Check(resp.Code, check.Equals, http.StatusNotFound)
81 c.Check(resp.Body.String(), check.Equals, "mount not found\n")
83 // Complete index of first mount
84 resp = s.call("GET", "/mounts/"+mntList[0].UUID+"/blocks", tok, nil)
85 c.Check(resp.Code, check.Equals, http.StatusOK)
86 c.Check(resp.Body.String(), check.Matches, TestHash+`\+[0-9]+ [0-9]+\n\n`)
88 // Partial index of first mount (one block matches prefix)
89 resp = s.call("GET", "/mounts/"+mntList[0].UUID+"/blocks?prefix="+TestHash[:2], tok, nil)
90 c.Check(resp.Code, check.Equals, http.StatusOK)
91 c.Check(resp.Body.String(), check.Matches, TestHash+`\+[0-9]+ [0-9]+\n\n`)
93 // Complete index of second mount (note trailing slash)
94 resp = s.call("GET", "/mounts/"+mntList[1].UUID+"/blocks/", tok, nil)
95 c.Check(resp.Code, check.Equals, http.StatusOK)
96 c.Check(resp.Body.String(), check.Matches, TestHash2+`\+[0-9]+ [0-9]+\n\n`)
98 // Partial index of second mount (no blocks match prefix)
99 resp = s.call("GET", "/mounts/"+mntList[1].UUID+"/blocks/?prefix="+TestHash[:2], tok, nil)
100 c.Check(resp.Code, check.Equals, http.StatusOK)
101 c.Check(resp.Body.String(), check.Equals, "\n")
104 func (s *MountsSuite) TestMetrics(c *check.C) {
105 s.call("PUT", "/"+TestHash, "", TestBlock)
106 s.call("PUT", "/"+TestHash2, "", TestBlock2)
107 resp := s.call("GET", "/metrics.json", "", nil)
108 c.Check(resp.Code, check.Equals, http.StatusUnauthorized)
109 resp = s.call("GET", "/metrics.json", "foobar", nil)
110 c.Check(resp.Code, check.Equals, http.StatusForbidden)
111 resp = s.call("GET", "/metrics.json", arvadostest.ManagementToken, nil)
112 c.Check(resp.Code, check.Equals, http.StatusOK)
123 SampleCount string `json:"sample_count"`
124 SampleSum float64 `json:"sample_sum"`
132 json.NewDecoder(resp.Body).Decode(&j)
133 found := make(map[string]bool)
134 for _, g := range j {
135 for _, m := range g.Metric {
136 if len(m.Label) == 2 && m.Label[0].Name == "code" && m.Label[0].Value == "200" && m.Label[1].Name == "method" && m.Label[1].Value == "put" {
137 c.Check(m.Summary.SampleCount, check.Equals, "2")
138 c.Check(len(m.Summary.Quantile), check.Not(check.Equals), 0)
139 c.Check(m.Summary.Quantile[0].Value, check.Not(check.Equals), float64(0))
144 c.Check(found["request_duration_seconds"], check.Equals, true)
145 c.Check(found["time_to_status_seconds"], check.Equals, true)
148 func (s *MountsSuite) call(method, path, tok string, body []byte) *httptest.ResponseRecorder {
149 resp := httptest.NewRecorder()
150 req, _ := http.NewRequest(method, path, bytes.NewReader(body))
152 req.Header.Set("Authorization", "Bearer "+tok)
154 s.rtr.ServeHTTP(resp, req)