X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/8a504ad561c1ffbafee8a7bc8da551f9d4b9a29e..8e1e7e6bf355eb0f1defc7278f1434c393a75a75:/sdk/go/manifest/manifest_test.go diff --git a/sdk/go/manifest/manifest_test.go b/sdk/go/manifest/manifest_test.go index c1bfb14e14..364648d643 100644 --- a/sdk/go/manifest/manifest_test.go +++ b/sdk/go/manifest/manifest_test.go @@ -1,20 +1,23 @@ package manifest import ( - "git.curoverse.com/arvados.git/sdk/go/blockdigest" "io/ioutil" + "reflect" "runtime" "testing" + + "git.curoverse.com/arvados.git/sdk/go/arvadostest" + "git.curoverse.com/arvados.git/sdk/go/blockdigest" ) -func getStackTrace() (string) { +func getStackTrace() string { buf := make([]byte, 1000) bytes_written := runtime.Stack(buf, false) return "Stack Trace:\n" + string(buf[:bytes_written]) } func expectFromChannel(t *testing.T, c <-chan string, expected string) { - actual, ok := <- c + actual, ok := <-c if !ok { t.Fatalf("Expected to receive %s but channel was closed. %s", expected, @@ -29,7 +32,7 @@ func expectFromChannel(t *testing.T, c <-chan string, expected string) { } func expectChannelClosed(t *testing.T, c <-chan interface{}) { - received, ok := <- c + received, ok := <-c if ok { t.Fatalf("Expected channel to be closed, but received %v instead. %s", received, @@ -60,59 +63,20 @@ func expectStringSlicesEqual(t *testing.T, actual []string, expected []string) { func expectManifestStream(t *testing.T, actual ManifestStream, expected ManifestStream) { expectEqual(t, actual.StreamName, expected.StreamName) expectStringSlicesEqual(t, actual.Blocks, expected.Blocks) - expectStringSlicesEqual(t, actual.Files, expected.Files) + expectStringSlicesEqual(t, actual.FileTokens, expected.FileTokens) } -func expectBlockLocator(t *testing.T, actual BlockLocator, expected BlockLocator) { +func expectBlockLocator(t *testing.T, actual blockdigest.BlockLocator, expected blockdigest.BlockLocator) { expectEqual(t, actual.Digest, expected.Digest) expectEqual(t, actual.Size, expected.Size) expectStringSlicesEqual(t, actual.Hints, expected.Hints) } -func expectLocatorPatternMatch(t *testing.T, s string) { - if !LocatorPattern.MatchString(s) { - t.Fatalf("Expected \"%s\" to match locator pattern but it did not.", - s) - } -} - -func expectLocatorPatternFail(t *testing.T, s string) { - if LocatorPattern.MatchString(s) { - t.Fatalf("Expected \"%s\" to fail locator pattern but it passed.", - s) - } -} - -func TestLocatorPatternBasic(t *testing.T) { - expectLocatorPatternMatch(t, "12345678901234567890123456789012+12345") - expectLocatorPatternMatch(t, "A2345678901234abcdefababdeffdfdf+12345") - expectLocatorPatternMatch(t, "12345678901234567890123456789012+12345+A1") - expectLocatorPatternMatch(t, - "12345678901234567890123456789012+12345+A1+B123wxyz@_-") - expectLocatorPatternMatch(t, - "12345678901234567890123456789012+12345+A1+B123wxyz@_-+C@") - - expectLocatorPatternFail(t, "12345678901234567890123456789012") - expectLocatorPatternFail(t, "12345678901234567890123456789012+") - expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+") - expectLocatorPatternFail(t, "1234567890123456789012345678901+12345") - expectLocatorPatternFail(t, "123456789012345678901234567890123+12345") - expectLocatorPatternFail(t, "g2345678901234abcdefababdeffdfdf+12345") - expectLocatorPatternFail(t, "12345678901234567890123456789012+12345 ") - expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+1") - expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+1A") - expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+A") - expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+a1") - expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+A1+") - expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+A1+B") - expectLocatorPatternFail(t, "12345678901234567890123456789012+12345+A+B2") -} - func TestParseManifestStreamSimple(t *testing.T) { m := parseManifestStream(". 365f83f5f808896ec834c8b595288735+2310+K@qr1hi+Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf 0:2310:qr1hi-8i9sb-ienvmpve1a0vpoi.log.txt") expectManifestStream(t, m, ManifestStream{StreamName: ".", - Blocks: []string{"365f83f5f808896ec834c8b595288735+2310+K@qr1hi+Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf"}, - Files: []string{"0:2310:qr1hi-8i9sb-ienvmpve1a0vpoi.log.txt"}}) + Blocks: []string{"365f83f5f808896ec834c8b595288735+2310+K@qr1hi+Af0c9a66381f3b028677411926f0be1c6282fe67c@542b5ddf"}, + FileTokens: []string{"0:2310:qr1hi-8i9sb-ienvmpve1a0vpoi.log.txt"}}) } func TestParseBlockLocatorSimple(t *testing.T) { @@ -138,10 +102,10 @@ func TestStreamIterShortManifestWithBlankStreams(t *testing.T) { expectManifestStream(t, firstStream, ManifestStream{StreamName: ".", - Blocks: []string{"b746e3d2104645f2f64cd3cc69dd895d+15693477+E2866e643690156651c03d876e638e674dcd79475@5441920c"}, - Files: []string{"0:15893477:chr10_band0_s0_e3000000.fj"}}) + Blocks: []string{"b746e3d2104645f2f64cd3cc69dd895d+15693477+E2866e643690156651c03d876e638e674dcd79475@5441920c"}, + FileTokens: []string{"0:15893477:chr10_band0_s0_e3000000.fj"}}) - received, ok := <- streamIter + received, ok := <-streamIter if ok { t.Fatalf("Expected streamIter to be closed, but received %v instead.", received) @@ -159,20 +123,75 @@ func TestBlockIterLongManifest(t *testing.T) { firstBlock := <-blockChannel expectBlockLocator(t, firstBlock, - BlockLocator{Digest: blockdigest.AssertFromString("b746e3d2104645f2f64cd3cc69dd895d"), - Size: 15693477, + blockdigest.BlockLocator{Digest: blockdigest.AssertFromString("b746e3d2104645f2f64cd3cc69dd895d"), + Size: 15693477, Hints: []string{"E2866e643690156651c03d876e638e674dcd79475@5441920c"}}) blocksRead := 1 - var lastBlock BlockLocator + var lastBlock blockdigest.BlockLocator for lastBlock = range blockChannel { //log.Printf("Blocks Read: %d", blocksRead) - blocksRead++ + blocksRead++ } expectEqual(t, blocksRead, 853) expectBlockLocator(t, lastBlock, - BlockLocator{Digest: blockdigest.AssertFromString("f9ce82f59e5908d2d70e18df9679b469"), - Size: 31367794, + blockdigest.BlockLocator{Digest: blockdigest.AssertFromString("f9ce82f59e5908d2d70e18df9679b469"), + Size: 31367794, Hints: []string{"E53f903684239bcc114f7bf8ff9bd6089f33058db@5441920c"}}) } + +func TestUnescape(t *testing.T) { + for _, testCase := range [][]string{ + {`\040`, ` `}, + {`\009`, `\009`}, + {`\\\040\\`, `\ \`}, + {`\\040\`, `\040\`}, + } { + in := testCase[0] + expect := testCase[1] + got := UnescapeName(in) + if expect != got { + t.Errorf("For '%s' got '%s' instead of '%s'", in, got, expect) + } + } +} + +type fsegtest struct { + mt string // manifest text + f string // filename + want []FileSegment // segments should be received on channel +} + +func TestFileSegmentIterByName(t *testing.T) { + mt := arvadostest.PathologicalManifest + for _, testCase := range []fsegtest{ + {mt: mt, f: "zzzz", want: nil}, + // This case is too sensitive: it would be acceptable + // (even preferable) to return only one empty segment. + {mt: mt, f: "foo/zero", want: []FileSegment{{"d41d8cd98f00b204e9800998ecf8427e+0", 0, 0}, {"d41d8cd98f00b204e9800998ecf8427e+0", 0, 0}}}, + {mt: mt, f: "zero@0", want: []FileSegment{{"d41d8cd98f00b204e9800998ecf8427e+0", 0, 0}}}, + {mt: mt, f: "zero@1", want: []FileSegment{{"d41d8cd98f00b204e9800998ecf8427e+0", 0, 0}}}, + {mt: mt, f: "zero@4", want: []FileSegment{{"d41d8cd98f00b204e9800998ecf8427e+0", 0, 0}}}, + {mt: mt, f: "zero@9", want: []FileSegment{{"d41d8cd98f00b204e9800998ecf8427e+0", 0, 0}}}, + {mt: mt, f: "f", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 0, 1}}}, + {mt: mt, f: "ooba", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 1, 2}, {"37b51d194a7513e45b56f6524f2d51f2+3", 0, 2}}}, + {mt: mt, f: "overlapReverse/o", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 2, 1}}}, + {mt: mt, f: "overlapReverse/oo", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 1, 2}}}, + {mt: mt, f: "overlapReverse/ofoo", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 2, 1}, {"acbd18db4cc2f85cedef654fccc4a4d8+3", 0, 3}}}, + {mt: mt, f: "foo bar/baz", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 0, 3}}}, + // This case is too sensitive: it would be better to + // omit the empty segment. + {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}}}, + {mt: mt, f: "segmented/oof", want: []FileSegment{{"acbd18db4cc2f85cedef654fccc4a4d8+3", 1, 2}, {"acbd18db4cc2f85cedef654fccc4a4d8+3", 0, 1}}}, + } { + m := Manifest{Text: testCase.mt} + var got []FileSegment + for fs := range m.FileSegmentIterByName(testCase.f) { + got = append(got, *fs) + } + if !reflect.DeepEqual(got, testCase.want) { + t.Errorf("For %#v:\n got %#v\n want %#v", testCase.f, got, testCase.want) + } + } +}