10795: Remove test-selector code (use -check.f instead).
[arvados.git] / sdk / go / manifest / manifest_test.go
1 package manifest
2
3 import (
4         "io/ioutil"
5         "reflect"
6         "regexp"
7         "runtime"
8         "testing"
9
10         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
11         "git.curoverse.com/arvados.git/sdk/go/blockdigest"
12 )
13
14 func getStackTrace() string {
15         buf := make([]byte, 1000)
16         bytesWritten := runtime.Stack(buf, false)
17         return "Stack Trace:\n" + string(buf[:bytesWritten])
18 }
19
20 func expectFromChannel(t *testing.T, c <-chan string, expected string) {
21         actual, ok := <-c
22         if !ok {
23                 t.Fatalf("Expected to receive %s but channel was closed. %s",
24                         expected,
25                         getStackTrace())
26         }
27         if actual != expected {
28                 t.Fatalf("Expected %s but got %s instead. %s",
29                         expected,
30                         actual,
31                         getStackTrace())
32         }
33 }
34
35 func expectChannelClosed(t *testing.T, c <-chan interface{}) {
36         received, ok := <-c
37         if ok {
38                 t.Fatalf("Expected channel to be closed, but received %v instead. %s",
39                         received,
40                         getStackTrace())
41         }
42 }
43
44 func expectEqual(t *testing.T, actual interface{}, expected interface{}) {
45         if actual != expected {
46                 t.Fatalf("Expected %v but received %v instead. %s",
47                         expected,
48                         actual,
49                         getStackTrace())
50         }
51 }
52
53 func expectStringSlicesEqual(t *testing.T, actual []string, expected []string) {
54         if len(actual) != len(expected) {
55                 t.Fatalf("Expected %v (length %d), but received %v (length %d) instead. %s", expected, len(expected), actual, len(actual), getStackTrace())
56         }
57         for i := range actual {
58                 if actual[i] != expected[i] {
59                         t.Fatalf("Expected %v but received %v instead (first disagreement at position %d). %s", expected, actual, i, getStackTrace())
60                 }
61         }
62 }
63
64 func expectFileStreamSegmentsEqual(t *testing.T, actual []FileStreamSegment, expected []FileStreamSegment) {
65         if !reflect.DeepEqual(actual, expected) {
66                 t.Fatalf("Expected %v but received %v instead. %s", expected, actual, getStackTrace())
67         }
68 }
69
70 func expectManifestStream(t *testing.T, actual ManifestStream, expected ManifestStream) {
71         expectEqual(t, actual.StreamName, expected.StreamName)
72         expectStringSlicesEqual(t, actual.Blocks, expected.Blocks)
73         expectFileStreamSegmentsEqual(t, actual.FileStreamSegments, expected.FileStreamSegments)
74 }
75
76 func expectBlockLocator(t *testing.T, actual blockdigest.BlockLocator, expected blockdigest.BlockLocator) {
77         expectEqual(t, actual.Digest, expected.Digest)
78         expectEqual(t, actual.Size, expected.Size)
79         expectStringSlicesEqual(t, actual.Hints, expected.Hints)
80 }
81
82 func TestParseManifestStreamSimple(t *testing.T) {
83         m := parseManifestStream(". 365f83f5f808896ec834c8b595288735+2310+K@qr1hi+Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf 0:2310:qr1hi-8i9sb-ienvmpve1a0vpoi.log.txt")
84         expectManifestStream(t, m, ManifestStream{StreamName: ".",
85                 Blocks:             []string{"365f83f5f808896ec834c8b595288735+2310+K@qr1hi+Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf"},
86                 FileStreamSegments: []FileStreamSegment{{0, 2310, "qr1hi-8i9sb-ienvmpve1a0vpoi.log.txt"}}})
87 }
88
89 func TestParseBlockLocatorSimple(t *testing.T) {
90         b, err := ParseBlockLocator("365f83f5f808896ec834c8b595288735+2310+K@qr1hi+Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf")
91         if err != nil {
92                 t.Fatalf("Unexpected error parsing block locator: %v", err)
93         }
94         d, err := blockdigest.FromString("365f83f5f808896ec834c8b595288735")
95         if err != nil {
96                 t.Fatalf("Unexpected error during FromString for block locator: %v", err)
97         }
98         expectBlockLocator(t, blockdigest.BlockLocator{b.Digest, b.Size, b.Hints},
99                 blockdigest.BlockLocator{Digest: d,
100                         Size: 2310,
101                         Hints: []string{"K@qr1hi",
102                                 "Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf"}})
103 }
104
105 func TestStreamIterShortManifestWithBlankStreams(t *testing.T) {
106         content, err := ioutil.ReadFile("testdata/short_manifest")
107         if err != nil {
108                 t.Fatalf("Unexpected error reading manifest from file: %v", err)
109         }
110         manifest := Manifest{Text: string(content)}
111         streamIter := manifest.StreamIter()
112
113         firstStream := <-streamIter
114         expectManifestStream(t,
115                 firstStream,
116                 ManifestStream{StreamName: ".",
117                         Blocks:             []string{"b746e3d2104645f2f64cd3cc69dd895d+15693477+E2866e643690156651c03d876e638e674dcd79475@5441920c"},
118                         FileStreamSegments: []FileStreamSegment{{0, 15893477, "chr10_band0_s0_e3000000.fj"}}})
119
120         received, ok := <-streamIter
121         if ok {
122                 t.Fatalf("Expected streamIter to be closed, but received %v instead.",
123                         received)
124         }
125 }
126
127 func TestBlockIterLongManifest(t *testing.T) {
128         content, err := ioutil.ReadFile("testdata/long_manifest")
129         if err != nil {
130                 t.Fatalf("Unexpected error reading manifest from file: %v", err)
131         }
132         manifest := Manifest{Text: string(content)}
133         blockChannel := manifest.BlockIterWithDuplicates()
134
135         firstBlock := <-blockChannel
136         d, err := blockdigest.FromString("b746e3d2104645f2f64cd3cc69dd895d")
137         if err != nil {
138                 t.Fatalf("Unexpected error during FromString for block: %v", err)
139         }
140         expectBlockLocator(t,
141                 firstBlock,
142                 blockdigest.BlockLocator{Digest: d,
143                         Size:  15693477,
144                         Hints: []string{"E2866e643690156651c03d876e638e674dcd79475@5441920c"}})
145         blocksRead := 1
146         var lastBlock blockdigest.BlockLocator
147         for lastBlock = range blockChannel {
148                 blocksRead++
149         }
150         expectEqual(t, blocksRead, 853)
151
152         d, err = blockdigest.FromString("f9ce82f59e5908d2d70e18df9679b469")
153         if err != nil {
154                 t.Fatalf("Unexpected error during FromString for block: %v", err)
155         }
156         expectBlockLocator(t,
157                 lastBlock,
158                 blockdigest.BlockLocator{Digest: d,
159                         Size:  31367794,
160                         Hints: []string{"E53f903684239bcc114f7bf8ff9bd6089f33058db@5441920c"}})
161 }
162
163 func TestUnescape(t *testing.T) {
164         for _, testCase := range [][]string{
165                 {`\040`, ` `},
166                 {`\009`, `\009`},
167                 {`\\\040\\`, `\ \`},
168                 {`\\040\`, `\040\`},
169         } {
170                 in := testCase[0]
171                 expect := testCase[1]
172                 got := UnescapeName(in)
173                 if expect != got {
174                         t.Errorf("For '%s' got '%s' instead of '%s'", in, got, expect)
175                 }
176         }
177 }
178
179 type fsegtest struct {
180         mt   string        // manifest text
181         f    string        // filename
182         want []FileSegment // segments should be received on channel
183 }
184
185 func TestFileSegmentIterByName(t *testing.T) {
186         mt := arvadostest.PathologicalManifest
187         for _, testCase := range []fsegtest{
188                 {mt: mt, f: "zzzz", want: nil},
189                 // This case is too sensitive: it would be acceptable
190                 // (even preferable) to return only one empty segment.
191                 {mt: mt, f: "foo/zero", want: []FileSegment{{"d41d8cd98f00b204e9800998ecf8427e+0", 0, 0}, {"d41d8cd98f00b204e9800998ecf8427e+0", 0, 0}}},
192                 {mt: mt, f: "zero@0", want: []FileSegment{{"d41d8cd98f00b204e9800998ecf8427e+0", 0, 0}}},
193                 {mt: mt, f: "zero@1", want: []FileSegment{{"d41d8cd98f00b204e9800998ecf8427e+0", 0, 0}}},
194                 {mt: mt, f: "zero@4", want: []FileSegment{{"d41d8cd98f00b204e9800998ecf8427e+0", 0, 0}}},
195                 {mt: mt, f: "zero@9", want: []FileSegment{{"d41d8cd98f00b204e9800998ecf8427e+0", 0, 0}}},
196                 {mt: mt, f: "f", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 0, 1}}},
197                 {mt: mt, f: "ooba", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 1, 2}, {"37b51d194a7513e45b56f6524f2d51f2+3", 0, 2}}},
198                 {mt: mt, f: "overlapReverse/o", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 2, 1}}},
199                 {mt: mt, f: "overlapReverse/oo", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 1, 2}}},
200                 {mt: mt, f: "overlapReverse/ofoo", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 2, 1}, {"acbd18db4cc2f85cedef654fccc4a4d8+3", 0, 3}}},
201                 {mt: mt, f: "foo bar/baz", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 0, 3}}},
202                 // This case is too sensitive: it would be better to
203                 // omit the empty segment.
204                 {mt: mt, f: "segmented/frob", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 0, 1}, {"37b51d194a7513e45b56f6524f2d51f2+3", 2, 1}, {"acbd18db4cc2f85cedef654fccc4a4d8+3", 1, 1}, {"d41d8cd98f00b204e9800998ecf8427e+0", 0, 0}, {"37b51d194a7513e45b56f6524f2d51f2+3", 0, 1}}},
205                 {mt: mt, f: "segmented/oof", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 1, 2}, {"acbd18db4cc2f85cedef654fccc4a4d8+3", 0, 1}}},
206         } {
207                 m := Manifest{Text: testCase.mt}
208                 var got []FileSegment
209                 for fs := range m.FileSegmentIterByName(testCase.f) {
210                         got = append(got, *fs)
211                 }
212                 if !reflect.DeepEqual(got, testCase.want) {
213                         t.Errorf("For %#v:\n got  %#v\n want %#v", testCase.f, got, testCase.want)
214                 }
215         }
216 }
217
218 func TestBlockIterWithBadManifest(t *testing.T) {
219         testCases := [][]string{
220                 {"badstream acbd18db4cc2f85cedef654fccc4a4d8+3 0:1:file1.txt", "Invalid stream name: badstream"},
221                 {"/badstream acbd18db4cc2f85cedef654fccc4a4d8+3 0:1:file1.txt", "Invalid stream name: /badstream"},
222                 {". acbd18db4cc2f85cedef654fccc4a4d8+3 file1.txt", "Invalid file token: file1.txt"},
223                 {". acbd18db4cc2f85cedef654fccc4a4+3 0:1:file1.txt", "No block locators found"},
224                 {". acbd18db4cc2f85cedef654fccc4a4d8 0:1:file1.txt", "No block locators found"},
225                 {". acbd18db4cc2f85cedef654fccc4a4d8+3 0:1:file1.txt file2.txt 1:2:file3.txt", "Invalid file token: file2.txt"},
226                 {". acbd18db4cc2f85cedef654fccc4a4d8+3 0:1:file1.txt. bcde18db4cc2f85cedef654fccc4a4d8+3 1:2:file3.txt", "Invalid file token: bcde18db4cc2f85cedef654fccc4a4d8.*"},
227                 {". acbd18db4cc2f85cedef654fccc4a4d8+3 0:1:file1.txt\n. acbd18db4cc2f85cedef654fccc4a4d8+3 ::file2.txt\n", "Invalid file token: ::file2.txt"},
228                 {". acbd18db4cc2f85cedef654fccc4a4d8+3 bcde18db4cc2f85cedef654fccc4a4d8+3\n", "No file tokens found"},
229                 {". acbd18db4cc2f85cedef654fccc4a4d8+3 ", "Invalid file token"},
230                 {". acbd18db4cc2f85cedef654fccc4a4d8+3", "No file tokens found"},
231                 {". 0:1:file1.txt\n", "No block locators found"},
232                 {".\n", "No block locators found"},
233         }
234
235         for _, testCase := range testCases {
236                 manifest := Manifest{Text: string(testCase[0])}
237                 blockChannel := manifest.BlockIterWithDuplicates()
238
239                 for block := range blockChannel {
240                         _ = block
241                 }
242
243                 // completed reading from blockChannel; now check for errors
244                 if manifest.Err == nil {
245                         t.Fatalf("Expected error")
246                 }
247
248                 matched, _ := regexp.MatchString(testCase[1], manifest.Err.Error())
249                 if !matched {
250                         t.Fatalf("Expected error not found. Expected: %v; Found: %v", testCase[1], manifest.Err.Error())
251                 }
252         }
253 }