7307: Clarify intended failure in arv-git-httpd SplitHostPort test.
[arvados.git] / services / datamanager / keep / keep_test.go
1 package keep
2
3 import (
4         "encoding/json"
5         "git.curoverse.com/arvados.git/sdk/go/keepclient"
6         . "gopkg.in/check.v1"
7         "net/http"
8         "net/http/httptest"
9         "testing"
10 )
11
12 // Gocheck boilerplate
13 func Test(t *testing.T) {
14         TestingT(t)
15 }
16
17 type KeepSuite struct{}
18
19 var _ = Suite(&KeepSuite{})
20
21 type TestHandler struct {
22         request TrashList
23 }
24
25 func (this *TestHandler) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
26         r := json.NewDecoder(req.Body)
27         r.Decode(&this.request)
28 }
29
30 func (s *KeepSuite) TestSendTrashLists(c *C) {
31         th := TestHandler{}
32         server := httptest.NewServer(&th)
33
34         tl := map[string]TrashList{
35                 server.URL: TrashList{TrashRequest{"000000000000000000000000deadbeef", 99}}}
36
37         kc := keepclient.KeepClient{Client: &http.Client{}}
38         kc.SetServiceRoots(map[string]string{"xxxx": server.URL},
39                 map[string]string{"xxxx": server.URL},
40                 map[string]string{})
41
42         err := SendTrashLists("", &kc, tl)
43         server.Close()
44
45         c.Check(err, IsNil)
46
47         c.Check(th.request,
48                 DeepEquals,
49                 tl[server.URL])
50
51 }
52
53 type TestHandlerError struct {
54 }
55
56 func (this *TestHandlerError) ServeHTTP(writer http.ResponseWriter, req *http.Request) {
57         http.Error(writer, "I'm a teapot", 418)
58 }
59
60 func sendTrashListError(c *C, server *httptest.Server) {
61         tl := map[string]TrashList{
62                 server.URL: TrashList{TrashRequest{"000000000000000000000000deadbeef", 99}}}
63
64         kc := keepclient.KeepClient{Client: &http.Client{}}
65         kc.SetServiceRoots(map[string]string{"xxxx": server.URL},
66                 map[string]string{"xxxx": server.URL},
67                 map[string]string{})
68
69         err := SendTrashLists("", &kc, tl)
70
71         c.Check(err, NotNil)
72         c.Check(err[0], NotNil)
73 }
74
75 func (s *KeepSuite) TestSendTrashListErrorResponse(c *C) {
76         sendTrashListError(c, httptest.NewServer(&TestHandlerError{}))
77 }
78
79 func (s *KeepSuite) TestSendTrashListUnreachable(c *C) {
80         sendTrashListError(c, httptest.NewUnstartedServer(&TestHandler{}))
81 }