11 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
12 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
13 check "gopkg.in/check.v1"
16 var _ = check.Suite(&IntegrationSuite{})
18 // IntegrationSuite tests need an API server
19 type IntegrationSuite struct{}
21 type SuccessHandler struct {
22 disk map[string][]byte
23 lock chan struct{} // channel with buffer==1: full when an operation is in progress.
24 ops *int // number of operations completed
27 func (h SuccessHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
30 buf, err := ioutil.ReadAll(req.Body)
35 pdh := fmt.Sprintf("%x+%d", md5.Sum(buf), len(buf))
42 resp.Write([]byte(pdh))
44 pdh := req.URL.Path[1:]
46 buf, ok := h.disk[pdh]
52 resp.WriteHeader(http.StatusNotFound)
57 resp.WriteHeader(http.StatusMethodNotAllowed)
62 mt string // manifest text
64 want interface{} // error or string to expect
67 func StubWithFakeServers(kc *KeepClient, h http.Handler) {
68 localRoots := make(map[string]string)
69 for i, k := range RunSomeFakeKeepServers(h, 4) {
70 localRoots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
72 kc.SetServiceRoots(localRoots, localRoots, nil)
75 func (s *ServerRequiredSuite) TestCollectionReaderContent(c *check.C) {
76 arv, err := arvadosclient.MakeArvadosClient()
77 c.Assert(err, check.IsNil)
78 arv.ApiToken = arvadostest.ActiveToken
80 kc, err := MakeKeepClient(&arv)
81 c.Assert(err, check.IsNil)
85 disk: make(map[string][]byte),
86 lock: make(chan struct{}, 1),
88 StubWithFakeServers(kc, h)
89 kc.PutB([]byte("foo"))
90 kc.PutB([]byte("bar"))
91 kc.PutB([]byte("Hello world\n"))
95 mt := arvadostest.PathologicalManifest
97 for _, testCase := range []rdrTest{
98 {mt: mt, f: "zzzz", want: os.ErrNotExist},
99 {mt: mt, f: "frob", want: os.ErrNotExist},
100 {mt: mt, f: "/segmented/frob", want: os.ErrNotExist},
101 {mt: mt, f: "./segmented/frob", want: os.ErrNotExist},
102 {mt: mt, f: "/f", want: os.ErrNotExist},
103 {mt: mt, f: "./f", want: os.ErrNotExist},
104 {mt: mt, f: "foo bar//baz", want: os.ErrNotExist},
105 {mt: mt, f: "foo/zero", want: ""},
106 {mt: mt, f: "zero@0", want: ""},
107 {mt: mt, f: "zero@1", want: ""},
108 {mt: mt, f: "zero@4", want: ""},
109 {mt: mt, f: "zero@9", want: ""},
110 {mt: mt, f: "f", want: "f"},
111 {mt: mt, f: "ooba", want: "ooba"},
112 {mt: mt, f: "overlapReverse/o", want: "o"},
113 {mt: mt, f: "overlapReverse/oo", want: "oo"},
114 {mt: mt, f: "overlapReverse/ofoo", want: "ofoo"},
115 {mt: mt, f: "foo bar/baz", want: "foo"},
116 {mt: mt, f: "segmented/frob", want: "frob"},
117 {mt: mt, f: "segmented/oof", want: "oof"},
119 rdr, err := kc.CollectionFileReader(map[string]interface{}{"manifest_text": testCase.mt}, testCase.f)
120 switch want := testCase.want.(type) {
122 c.Check(rdr, check.IsNil)
123 c.Check(err, check.Equals, want)
125 buf := make([]byte, len(want))
126 n, err := io.ReadFull(rdr, buf)
127 c.Check(err, check.IsNil)
128 for i := 0; i < 4; i++ {
129 c.Check(string(buf), check.Equals, want)
130 n, err = rdr.Read(buf)
131 c.Check(n, check.Equals, 0)
132 c.Check(err, check.Equals, io.EOF)
134 c.Check(rdr.Close(), check.Equals, nil)
139 func (s *ServerRequiredSuite) TestCollectionReaderCloseEarly(c *check.C) {
140 arv, err := arvadosclient.MakeArvadosClient()
141 c.Assert(err, check.IsNil)
142 arv.ApiToken = arvadostest.ActiveToken
144 kc, err := MakeKeepClient(&arv)
145 c.Assert(err, check.IsNil)
148 disk: make(map[string][]byte),
149 lock: make(chan struct{}, 1),
152 StubWithFakeServers(kc, h)
153 kc.PutB([]byte("foo"))
156 for i := 0; i < 1000; i++ {
157 mt += "acbd18db4cc2f85cedef654fccc4a4d8+3 "
159 mt += "0:3000:foo1000.txt\n"
161 // Grab the stub server's lock, ensuring our cfReader doesn't
162 // get anything back from its first call to kc.Get() before we
163 // have a chance to call Close().
165 opsBeforeRead := *h.ops
167 rdr, err := kc.CollectionFileReader(map[string]interface{}{"manifest_text": mt}, "foo1000.txt")
168 c.Assert(err, check.IsNil)
170 c.Assert(err, check.IsNil)
171 c.Assert(rdr.Error(), check.IsNil)
173 // Release the stub server's lock. The first GET operation will proceed.
176 // doGet() should close toRead before sending any more bufs to it.
177 if what, ok := <-rdr.toRead; ok {
178 c.Errorf("Got %+v, expected toRead to be closed", what)
181 // Stub should have handled exactly one GET request.
182 c.Assert(*h.ops, check.Equals, opsBeforeRead+1)