Merge branch '11807-yaml-to-json'
[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         "context"
9         "encoding/json"
10         "net/http"
11         "net/http/httptest"
12
13         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
14         check "gopkg.in/check.v1"
15 )
16
17 var _ = check.Suite(&MountsSuite{})
18
19 type MountsSuite struct {
20         vm  VolumeManager
21         rtr http.Handler
22 }
23
24 func (s *MountsSuite) SetUpTest(c *check.C) {
25         s.vm = MakeTestVolumeManager(2)
26         KeepVM = s.vm
27         s.rtr = MakeRESTRouter()
28         theConfig.systemAuthToken = arvadostest.DataManagerToken
29 }
30
31 func (s *MountsSuite) TearDownTest(c *check.C) {
32         s.vm.Close()
33         KeepVM = nil
34         theConfig = DefaultConfig()
35         theConfig.Start()
36 }
37
38 func (s *MountsSuite) TestMounts(c *check.C) {
39         vols := s.vm.AllWritable()
40         vols[0].Put(context.Background(), TestHash, TestBlock)
41         vols[1].Put(context.Background(), TestHash2, TestBlock2)
42
43         resp := s.call("GET", "/mounts", "")
44         c.Check(resp.Code, check.Equals, http.StatusOK)
45         var mntList []struct {
46                 UUID        string
47                 DeviceID    string
48                 ReadOnly    bool
49                 Replication int
50                 Tier        int
51         }
52         err := json.Unmarshal(resp.Body.Bytes(), &mntList)
53         c.Assert(err, check.IsNil)
54         c.Assert(len(mntList), check.Equals, 2)
55         for _, m := range mntList {
56                 c.Check(len(m.UUID), check.Equals, 27)
57                 c.Check(m.UUID[:12], check.Equals, "zzzzz-ivpuk-")
58                 c.Check(m.DeviceID, check.Equals, "mock-device-id")
59                 c.Check(m.ReadOnly, check.Equals, false)
60                 c.Check(m.Replication, check.Equals, 1)
61                 c.Check(m.Tier, check.Equals, 1)
62         }
63         c.Check(mntList[0].UUID, check.Not(check.Equals), mntList[1].UUID)
64
65         // Bad auth
66         for _, tok := range []string{"", "xyzzy"} {
67                 resp = s.call("GET", "/mounts/"+mntList[1].UUID+"/blocks", tok)
68                 c.Check(resp.Code, check.Equals, http.StatusUnauthorized)
69                 c.Check(resp.Body.String(), check.Equals, "Unauthorized\n")
70         }
71
72         tok := arvadostest.DataManagerToken
73
74         // Nonexistent mount UUID
75         resp = s.call("GET", "/mounts/X/blocks", tok)
76         c.Check(resp.Code, check.Equals, http.StatusNotFound)
77         c.Check(resp.Body.String(), check.Equals, "mount not found\n")
78
79         // Complete index of first mount
80         resp = s.call("GET", "/mounts/"+mntList[0].UUID+"/blocks", tok)
81         c.Check(resp.Code, check.Equals, http.StatusOK)
82         c.Check(resp.Body.String(), check.Matches, TestHash+`\+[0-9]+ [0-9]+\n\n`)
83
84         // Partial index of first mount (one block matches prefix)
85         resp = s.call("GET", "/mounts/"+mntList[0].UUID+"/blocks?prefix="+TestHash[:2], tok)
86         c.Check(resp.Code, check.Equals, http.StatusOK)
87         c.Check(resp.Body.String(), check.Matches, TestHash+`\+[0-9]+ [0-9]+\n\n`)
88
89         // Complete index of second mount (note trailing slash)
90         resp = s.call("GET", "/mounts/"+mntList[1].UUID+"/blocks/", tok)
91         c.Check(resp.Code, check.Equals, http.StatusOK)
92         c.Check(resp.Body.String(), check.Matches, TestHash2+`\+[0-9]+ [0-9]+\n\n`)
93
94         // Partial index of second mount (no blocks match prefix)
95         resp = s.call("GET", "/mounts/"+mntList[1].UUID+"/blocks/?prefix="+TestHash[:2], tok)
96         c.Check(resp.Code, check.Equals, http.StatusOK)
97         c.Check(resp.Body.String(), check.Equals, "\n")
98 }
99
100 func (s *MountsSuite) call(method, path, tok string) *httptest.ResponseRecorder {
101         resp := httptest.NewRecorder()
102         req, _ := http.NewRequest(method, path, nil)
103         if tok != "" {
104                 req.Header.Set("Authorization", "OAuth2 "+tok)
105         }
106         s.rtr.ServeHTTP(resp, req)
107         return resp
108 }