13100: Merge branch 'master' into 13100-crunch-run-output
[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 main
6
7 import (
8         "bytes"
9         "context"
10         "encoding/json"
11         "net/http"
12         "net/http/httptest"
13
14         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
15         check "gopkg.in/check.v1"
16 )
17
18 var _ = check.Suite(&MountsSuite{})
19
20 type MountsSuite struct {
21         vm  VolumeManager
22         rtr http.Handler
23 }
24
25 func (s *MountsSuite) SetUpTest(c *check.C) {
26         s.vm = MakeTestVolumeManager(2)
27         KeepVM = s.vm
28         theConfig = DefaultConfig()
29         theConfig.systemAuthToken = arvadostest.DataManagerToken
30         theConfig.Start()
31         s.rtr = MakeRESTRouter()
32 }
33
34 func (s *MountsSuite) TearDownTest(c *check.C) {
35         s.vm.Close()
36         KeepVM = nil
37         theConfig = DefaultConfig()
38         theConfig.Start()
39 }
40
41 func (s *MountsSuite) TestMounts(c *check.C) {
42         vols := s.vm.AllWritable()
43         vols[0].Put(context.Background(), TestHash, TestBlock)
44         vols[1].Put(context.Background(), TestHash2, TestBlock2)
45
46         resp := s.call("GET", "/mounts", "", nil)
47         c.Check(resp.Code, check.Equals, http.StatusOK)
48         var mntList []struct {
49                 UUID           string   `json:"uuid"`
50                 DeviceID       string   `json:"device_id"`
51                 ReadOnly       bool     `json:"read_only"`
52                 Replication    int      `json:"replication"`
53                 StorageClasses []string `json:"storage_classes"`
54         }
55         err := json.Unmarshal(resp.Body.Bytes(), &mntList)
56         c.Assert(err, check.IsNil)
57         c.Assert(len(mntList), check.Equals, 2)
58         for _, m := range mntList {
59                 c.Check(len(m.UUID), check.Equals, 27)
60                 c.Check(m.UUID[:12], check.Equals, "zzzzz-ivpuk-")
61                 c.Check(m.DeviceID, check.Equals, "mock-device-id")
62                 c.Check(m.ReadOnly, check.Equals, false)
63                 c.Check(m.Replication, check.Equals, 1)
64                 c.Check(m.StorageClasses, check.DeepEquals, []string{"default"})
65         }
66         c.Check(mntList[0].UUID, check.Not(check.Equals), mntList[1].UUID)
67
68         // Bad auth
69         for _, tok := range []string{"", "xyzzy"} {
70                 resp = s.call("GET", "/mounts/"+mntList[1].UUID+"/blocks", tok, nil)
71                 c.Check(resp.Code, check.Equals, http.StatusUnauthorized)
72                 c.Check(resp.Body.String(), check.Equals, "Unauthorized\n")
73         }
74
75         tok := arvadostest.DataManagerToken
76
77         // Nonexistent mount UUID
78         resp = s.call("GET", "/mounts/X/blocks", tok, nil)
79         c.Check(resp.Code, check.Equals, http.StatusNotFound)
80         c.Check(resp.Body.String(), check.Equals, "mount not found\n")
81
82         // Complete index of first mount
83         resp = s.call("GET", "/mounts/"+mntList[0].UUID+"/blocks", tok, nil)
84         c.Check(resp.Code, check.Equals, http.StatusOK)
85         c.Check(resp.Body.String(), check.Matches, TestHash+`\+[0-9]+ [0-9]+\n\n`)
86
87         // Partial index of first mount (one block matches prefix)
88         resp = s.call("GET", "/mounts/"+mntList[0].UUID+"/blocks?prefix="+TestHash[:2], tok, nil)
89         c.Check(resp.Code, check.Equals, http.StatusOK)
90         c.Check(resp.Body.String(), check.Matches, TestHash+`\+[0-9]+ [0-9]+\n\n`)
91
92         // Complete index of second mount (note trailing slash)
93         resp = s.call("GET", "/mounts/"+mntList[1].UUID+"/blocks/", tok, nil)
94         c.Check(resp.Code, check.Equals, http.StatusOK)
95         c.Check(resp.Body.String(), check.Matches, TestHash2+`\+[0-9]+ [0-9]+\n\n`)
96
97         // Partial index of second mount (no blocks match prefix)
98         resp = s.call("GET", "/mounts/"+mntList[1].UUID+"/blocks/?prefix="+TestHash[:2], tok, nil)
99         c.Check(resp.Code, check.Equals, http.StatusOK)
100         c.Check(resp.Body.String(), check.Equals, "\n")
101 }
102
103 func (s *MountsSuite) TestMetrics(c *check.C) {
104         s.call("PUT", "/"+TestHash, "", TestBlock)
105         s.call("PUT", "/"+TestHash2, "", TestBlock2)
106         resp := s.call("GET", "/metrics.json", "", nil)
107         c.Check(resp.Code, check.Equals, http.StatusOK)
108         var j []struct {
109                 Name   string
110                 Help   string
111                 Type   string
112                 Metric []struct {
113                         Label []struct {
114                                 Name  string
115                                 Value string
116                         }
117                         Summary struct {
118                                 SampleCount string  `json:"sample_count"`
119                                 SampleSum   float64 `json:"sample_sum"`
120                                 Quantile    []struct {
121                                         Quantile float64
122                                         Value    float64
123                                 }
124                         }
125                 }
126         }
127         json.NewDecoder(resp.Body).Decode(&j)
128         found := make(map[string]bool)
129         for _, g := range j {
130                 for _, m := range g.Metric {
131                         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" {
132                                 c.Check(m.Summary.SampleCount, check.Equals, "2")
133                                 c.Check(len(m.Summary.Quantile), check.Not(check.Equals), 0)
134                                 c.Check(m.Summary.Quantile[0].Value, check.Not(check.Equals), float64(0))
135                                 found[g.Name] = true
136                         }
137                 }
138         }
139         c.Check(found["request_duration_seconds"], check.Equals, true)
140         c.Check(found["time_to_status_seconds"], check.Equals, true)
141 }
142
143 func (s *MountsSuite) call(method, path, tok string, body []byte) *httptest.ResponseRecorder {
144         resp := httptest.NewRecorder()
145         req, _ := http.NewRequest(method, path, bytes.NewReader(body))
146         if tok != "" {
147                 req.Header.Set("Authorization", "OAuth2 "+tok)
148         }
149         s.rtr.ServeHTTP(resp, req)
150         return resp
151 }