github.com/thanos-io/thanos@v0.32.5/pkg/block/metadata/markers_test.go (about) 1 // Copyright (c) The Thanos Authors. 2 // Licensed under the Apache License 2.0. 3 4 package metadata 5 6 import ( 7 "bytes" 8 "context" 9 "encoding/json" 10 "path" 11 "testing" 12 "time" 13 14 "github.com/go-kit/log" 15 "github.com/oklog/ulid" 16 "github.com/pkg/errors" 17 "github.com/thanos-io/objstore" 18 "go.uber.org/goleak" 19 20 "github.com/efficientgo/core/testutil" 21 ) 22 23 func TestMain(m *testing.M) { 24 goleak.VerifyTestMain(m) 25 } 26 27 func TestReadMarker(t *testing.T) { 28 ctx := context.Background() 29 30 tmpDir := t.TempDir() 31 32 bkt := objstore.WithNoopInstr(objstore.NewInMemBucket()) 33 t.Run(DeletionMarkFilename, func(t *testing.T) { 34 blockWithoutMark := ulid.MustNew(uint64(1), nil) 35 d := DeletionMark{} 36 err := ReadMarker(ctx, log.NewNopLogger(), bkt, path.Join(tmpDir, blockWithoutMark.String()), &d) 37 testutil.NotOk(t, err) 38 testutil.Equals(t, ErrorMarkerNotFound, err) 39 40 blockWithPartialMark := ulid.MustNew(uint64(2), nil) 41 testutil.Ok(t, bkt.Upload(ctx, path.Join(tmpDir, blockWithPartialMark.String(), DeletionMarkFilename), bytes.NewBufferString("not a valid deletion-mark.json"))) 42 err = ReadMarker(ctx, log.NewNopLogger(), bkt, path.Join(tmpDir, blockWithPartialMark.String()), &d) 43 testutil.NotOk(t, err) 44 testutil.Equals(t, ErrorUnmarshalMarker, errors.Cause(err)) 45 46 blockWithDifferentVersionMark := ulid.MustNew(uint64(3), nil) 47 var buf bytes.Buffer 48 testutil.Ok(t, json.NewEncoder(&buf).Encode(&DeletionMark{ 49 ID: blockWithDifferentVersionMark, 50 DeletionTime: time.Now().Unix(), 51 Version: 2, 52 })) 53 testutil.Ok(t, bkt.Upload(ctx, path.Join(tmpDir, blockWithDifferentVersionMark.String(), DeletionMarkFilename), &buf)) 54 err = ReadMarker(ctx, log.NewNopLogger(), bkt, path.Join(tmpDir, blockWithDifferentVersionMark.String()), &d) 55 testutil.NotOk(t, err) 56 testutil.Equals(t, "unexpected deletion-mark file version 2, expected 1", err.Error()) 57 58 blockWithValidMark := ulid.MustNew(uint64(3), nil) 59 buf.Reset() 60 expected := &DeletionMark{ 61 ID: blockWithValidMark, 62 DeletionTime: time.Now().Unix(), 63 Version: 1, 64 } 65 testutil.Ok(t, json.NewEncoder(&buf).Encode(expected)) 66 testutil.Ok(t, bkt.Upload(ctx, path.Join(tmpDir, blockWithValidMark.String(), DeletionMarkFilename), &buf)) 67 err = ReadMarker(ctx, log.NewNopLogger(), bkt, path.Join(tmpDir, blockWithValidMark.String()), &d) 68 testutil.Ok(t, err) 69 testutil.Equals(t, *expected, d) 70 }) 71 t.Run(NoCompactMarkFilename, func(t *testing.T) { 72 blockWithoutMark := ulid.MustNew(uint64(1), nil) 73 n := NoCompactMark{} 74 err := ReadMarker(ctx, log.NewNopLogger(), bkt, path.Join(tmpDir, blockWithoutMark.String()), &n) 75 testutil.NotOk(t, err) 76 testutil.Equals(t, ErrorMarkerNotFound, err) 77 78 blockWithPartialMark := ulid.MustNew(uint64(2), nil) 79 testutil.Ok(t, bkt.Upload(ctx, path.Join(tmpDir, blockWithPartialMark.String(), NoCompactMarkFilename), bytes.NewBufferString("not a valid no-compact-mark.json"))) 80 err = ReadMarker(ctx, log.NewNopLogger(), bkt, path.Join(tmpDir, blockWithPartialMark.String()), &n) 81 testutil.NotOk(t, err) 82 testutil.Equals(t, ErrorUnmarshalMarker, errors.Cause(err)) 83 84 blockWithDifferentVersionMark := ulid.MustNew(uint64(3), nil) 85 var buf bytes.Buffer 86 testutil.Ok(t, json.NewEncoder(&buf).Encode(&NoCompactMark{ 87 ID: blockWithDifferentVersionMark, 88 Version: 2, 89 Reason: IndexSizeExceedingNoCompactReason, 90 Details: "yolo", 91 })) 92 testutil.Ok(t, bkt.Upload(ctx, path.Join(tmpDir, blockWithDifferentVersionMark.String(), NoCompactMarkFilename), &buf)) 93 err = ReadMarker(ctx, log.NewNopLogger(), bkt, path.Join(tmpDir, blockWithDifferentVersionMark.String()), &n) 94 testutil.NotOk(t, err) 95 testutil.Equals(t, "unexpected no-compact-mark file version 2, expected 1", err.Error()) 96 97 blockWithValidMark := ulid.MustNew(uint64(3), nil) 98 buf.Reset() 99 expected := &NoCompactMark{ 100 ID: blockWithValidMark, 101 Version: 1, 102 Reason: IndexSizeExceedingNoCompactReason, 103 Details: "yolo", 104 } 105 testutil.Ok(t, json.NewEncoder(&buf).Encode(expected)) 106 testutil.Ok(t, bkt.Upload(ctx, path.Join(tmpDir, blockWithValidMark.String(), NoCompactMarkFilename), &buf)) 107 err = ReadMarker(ctx, log.NewNopLogger(), bkt, path.Join(tmpDir, blockWithValidMark.String()), &n) 108 testutil.Ok(t, err) 109 testutil.Equals(t, *expected, n) 110 }) 111 }