11644: Accept index/pull/trash requests for a specific mount.
[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         }
44         err := json.Unmarshal(resp.Body.Bytes(), &mntList)
45         c.Check(err, check.IsNil)
46         c.Check(len(mntList), check.Equals, 2)
47
48         // Bad auth
49         for _, tok := range []string{"", "xyzzy"} {
50                 resp = s.call("GET", "/mounts/"+mntList[1].UUID+"/blocks", tok)
51                 c.Check(resp.Code, check.Equals, http.StatusUnauthorized)
52                 c.Check(resp.Body.String(), check.Equals, "Unauthorized\n")
53         }
54
55         tok := arvadostest.DataManagerToken
56
57         // Nonexistent mount UUID
58         resp = s.call("GET", "/mounts/X/blocks", tok)
59         c.Check(resp.Code, check.Equals, http.StatusNotFound)
60         c.Check(resp.Body.String(), check.Equals, "mount not found\n")
61
62         // Complete index of first mount
63         resp = s.call("GET", "/mounts/"+mntList[0].UUID+"/blocks", tok)
64         c.Check(resp.Code, check.Equals, http.StatusOK)
65         c.Check(resp.Body.String(), check.Matches, TestHash+`\+[0-9]+ [0-9]+\n\n`)
66
67         // Partial index of first mount (one block matches prefix)
68         resp = s.call("GET", "/mounts/"+mntList[0].UUID+"/blocks/"+TestHash[:2], tok)
69         c.Check(resp.Code, check.Equals, http.StatusOK)
70         c.Check(resp.Body.String(), check.Matches, TestHash+`\+[0-9]+ [0-9]+\n\n`)
71
72         // Complete index of second mount (note trailing slash)
73         resp = s.call("GET", "/mounts/"+mntList[1].UUID+"/blocks/", tok)
74         c.Check(resp.Code, check.Equals, http.StatusOK)
75         c.Check(resp.Body.String(), check.Matches, TestHash2+`\+[0-9]+ [0-9]+\n\n`)
76
77         // Partial index of second mount (no blocks match prefix)
78         resp = s.call("GET", "/mounts/"+mntList[1].UUID+"/blocks/"+TestHash[:2], tok)
79         c.Check(resp.Code, check.Equals, http.StatusOK)
80         c.Check(resp.Body.String(), check.Equals, "\n")
81 }
82
83 func (s *MountsSuite) call(method, path, tok string) *httptest.ResponseRecorder {
84         resp := httptest.NewRecorder()
85         req, _ := http.NewRequest(method, path, nil)
86         if tok != "" {
87                 req.Header.Set("Authorization", "OAuth2 "+tok)
88         }
89         s.rtr.ServeHTTP(resp, req)
90         return resp
91 }