10472: Handle jobs with components/subjobs
[arvados.git] / sdk / go / keepclient / collectionreader_test.go
1 package keepclient
2
3 import (
4         "crypto/md5"
5         "crypto/rand"
6         "fmt"
7         "io"
8         "io/ioutil"
9         "net/http"
10         "os"
11         "strconv"
12         "strings"
13
14         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
15         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
16         check "gopkg.in/check.v1"
17 )
18
19 var _ = check.Suite(&CollectionReaderUnit{})
20
21 type CollectionReaderUnit struct {
22         arv     *arvadosclient.ArvadosClient
23         kc      *KeepClient
24         handler SuccessHandler
25 }
26
27 func (s *CollectionReaderUnit) SetUpTest(c *check.C) {
28         var err error
29         s.arv, err = arvadosclient.MakeArvadosClient()
30         c.Assert(err, check.IsNil)
31         s.arv.ApiToken = arvadostest.ActiveToken
32
33         s.kc, err = MakeKeepClient(s.arv)
34         c.Assert(err, check.IsNil)
35
36         s.handler = SuccessHandler{
37                 disk: make(map[string][]byte),
38                 lock: make(chan struct{}, 1),
39                 ops:  new(int),
40         }
41         localRoots := make(map[string]string)
42         for i, k := range RunSomeFakeKeepServers(s.handler, 4) {
43                 localRoots[fmt.Sprintf("zzzzz-bi6l4-fakefakefake%03d", i)] = k.url
44         }
45         s.kc.SetServiceRoots(localRoots, localRoots, nil)
46 }
47
48 type SuccessHandler struct {
49         disk map[string][]byte
50         lock chan struct{} // channel with buffer==1: full when an operation is in progress.
51         ops  *int          // number of operations completed
52 }
53
54 func (h SuccessHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
55         switch req.Method {
56         case "PUT":
57                 buf, err := ioutil.ReadAll(req.Body)
58                 if err != nil {
59                         resp.WriteHeader(500)
60                         return
61                 }
62                 pdh := fmt.Sprintf("%x+%d", md5.Sum(buf), len(buf))
63                 h.lock <- struct{}{}
64                 h.disk[pdh] = buf
65                 if h.ops != nil {
66                         (*h.ops)++
67                 }
68                 <-h.lock
69                 resp.Write([]byte(pdh))
70         case "GET":
71                 pdh := req.URL.Path[1:]
72                 h.lock <- struct{}{}
73                 buf, ok := h.disk[pdh]
74                 if h.ops != nil {
75                         (*h.ops)++
76                 }
77                 <-h.lock
78                 if !ok {
79                         resp.WriteHeader(http.StatusNotFound)
80                 } else {
81                         resp.Write(buf)
82                 }
83         default:
84                 resp.WriteHeader(http.StatusMethodNotAllowed)
85         }
86 }
87
88 type rdrTest struct {
89         mt   string      // manifest text
90         f    string      // filename
91         want interface{} // error or string to expect
92 }
93
94 func (s *CollectionReaderUnit) TestCollectionReaderContent(c *check.C) {
95         s.kc.PutB([]byte("foo"))
96         s.kc.PutB([]byte("bar"))
97         s.kc.PutB([]byte("Hello world\n"))
98         s.kc.PutB([]byte(""))
99
100         mt := arvadostest.PathologicalManifest
101
102         for _, testCase := range []rdrTest{
103                 {mt: mt, f: "zzzz", want: os.ErrNotExist},
104                 {mt: mt, f: "frob", want: os.ErrNotExist},
105                 {mt: mt, f: "/segmented/frob", want: os.ErrNotExist},
106                 {mt: mt, f: "./segmented/frob", want: os.ErrNotExist},
107                 {mt: mt, f: "/f", want: os.ErrNotExist},
108                 {mt: mt, f: "./f", want: os.ErrNotExist},
109                 {mt: mt, f: "foo bar//baz", want: os.ErrNotExist},
110                 {mt: mt, f: "foo/zero", want: ""},
111                 {mt: mt, f: "zero@0", want: ""},
112                 {mt: mt, f: "zero@1", want: ""},
113                 {mt: mt, f: "zero@4", want: ""},
114                 {mt: mt, f: "zero@9", want: ""},
115                 {mt: mt, f: "f", want: "f"},
116                 {mt: mt, f: "ooba", want: "ooba"},
117                 {mt: mt, f: "overlapReverse/o", want: "o"},
118                 {mt: mt, f: "overlapReverse/oo", want: "oo"},
119                 {mt: mt, f: "overlapReverse/ofoo", want: "ofoo"},
120                 {mt: mt, f: "foo bar/baz", want: "foo"},
121                 {mt: mt, f: "segmented/frob", want: "frob"},
122                 {mt: mt, f: "segmented/oof", want: "oof"},
123         } {
124                 rdr, err := s.kc.CollectionFileReader(map[string]interface{}{"manifest_text": testCase.mt}, testCase.f)
125                 switch want := testCase.want.(type) {
126                 case error:
127                         c.Check(rdr, check.IsNil)
128                         c.Check(err, check.Equals, want)
129                 case string:
130                         buf := make([]byte, len(want))
131                         n, err := io.ReadFull(rdr, buf)
132                         c.Check(err, check.IsNil)
133                         for i := 0; i < 4; i++ {
134                                 c.Check(string(buf), check.Equals, want)
135                                 n, err = rdr.Read(buf)
136                                 c.Check(n, check.Equals, 0)
137                                 c.Check(err, check.Equals, io.EOF)
138                         }
139                         c.Check(rdr.Close(), check.Equals, nil)
140                 }
141         }
142 }
143
144 func (s *CollectionReaderUnit) TestCollectionReaderManyBlocks(c *check.C) {
145         h := md5.New()
146         buf := make([]byte, 4096)
147         locs := make([]string, len(buf))
148         filesize := 0
149         for i := 0; i < len(locs); i++ {
150                 _, err := io.ReadFull(rand.Reader, buf[:i])
151                 c.Assert(err, check.IsNil)
152                 h.Write(buf[:i])
153                 locs[i], _, err = s.kc.PutB(buf[:i])
154                 c.Assert(err, check.IsNil)
155                 filesize += i
156         }
157         manifest := "./random " + strings.Join(locs, " ") + " 0:" + strconv.Itoa(filesize) + ":bytes.bin\n"
158         dataMD5 := h.Sum(nil)
159
160         checkMD5 := md5.New()
161         rdr, err := s.kc.CollectionFileReader(map[string]interface{}{"manifest_text": manifest}, "random/bytes.bin")
162         c.Check(err, check.IsNil)
163         _, err = io.Copy(checkMD5, rdr)
164         c.Check(err, check.IsNil)
165         _, err = rdr.Read(make([]byte, 1))
166         c.Check(err, check.Equals, io.EOF)
167         c.Check(checkMD5.Sum(nil), check.DeepEquals, dataMD5)
168 }
169
170 func (s *CollectionReaderUnit) TestCollectionReaderCloseEarly(c *check.C) {
171         s.kc.PutB([]byte("foo"))
172
173         mt := ". "
174         for i := 0; i < 1000; i++ {
175                 mt += "acbd18db4cc2f85cedef654fccc4a4d8+3 "
176         }
177         mt += "0:3000:foo1000.txt\n"
178
179         // Grab the stub server's lock, ensuring our cfReader doesn't
180         // get anything back from its first call to kc.Get() before we
181         // have a chance to call Close().
182         s.handler.lock <- struct{}{}
183         opsBeforeRead := *s.handler.ops
184
185         rdr, err := s.kc.CollectionFileReader(map[string]interface{}{"manifest_text": mt}, "foo1000.txt")
186         c.Assert(err, check.IsNil)
187
188         firstReadDone := make(chan struct{})
189         go func() {
190                 rdr.Read(make([]byte, 6))
191                 firstReadDone <- struct{}{}
192         }()
193         err = rdr.Close()
194         c.Assert(err, check.IsNil)
195         c.Assert(rdr.(*cfReader).Error(), check.IsNil)
196
197         // Release the stub server's lock. The first GET operation will proceed.
198         <-s.handler.lock
199
200         // Make sure our first read operation consumes the data
201         // received from the first GET.
202         <-firstReadDone
203
204         // doGet() should close toRead before sending any more bufs to it.
205         if what, ok := <-rdr.(*cfReader).toRead; ok {
206                 c.Errorf("Got %q, expected toRead to be closed", what)
207         }
208
209         // Stub should have handled exactly one GET request.
210         c.Assert(*s.handler.ops, check.Equals, opsBeforeRead+1)
211 }
212
213 func (s *CollectionReaderUnit) TestCollectionReaderDataError(c *check.C) {
214         manifest := ". ffffffffffffffffffffffffffffffff+1 0:1:notfound.txt\n"
215         buf := make([]byte, 1)
216         rdr, err := s.kc.CollectionFileReader(map[string]interface{}{"manifest_text": manifest}, "notfound.txt")
217         c.Check(err, check.IsNil)
218         for i := 0; i < 2; i++ {
219                 _, err = io.ReadFull(rdr, buf)
220                 c.Check(err, check.NotNil)
221                 c.Check(err, check.Not(check.Equals), io.EOF)
222         }
223         c.Check(rdr.Close(), check.NotNil)
224 }