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