github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/storage/chunk/client/object_client_test.go (about) 1 package client 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/prometheus/common/model" 8 "github.com/stretchr/testify/require" 9 10 "github.com/grafana/loki/pkg/logproto" 11 "github.com/grafana/loki/pkg/storage/chunk" 12 "github.com/grafana/loki/pkg/storage/config" 13 ) 14 15 func MustParseDayTime(s string) config.DayTime { 16 t, err := time.Parse("2006-01-02", s) 17 if err != nil { 18 panic(err) 19 } 20 return config.DayTime{ 21 Time: model.TimeFromUnix(t.Unix()), 22 } 23 } 24 25 func TestFSEncoder(t *testing.T) { 26 schema := config.SchemaConfig{ 27 Configs: []config.PeriodConfig{ 28 { 29 From: MustParseDayTime("2020-01-01"), 30 Schema: "v11", 31 }, 32 { 33 From: MustParseDayTime("2022-01-01"), 34 Schema: "v12", 35 }, 36 }, 37 } 38 39 // chunk that resolves to v11 40 oldChunk := chunk.Chunk{ 41 ChunkRef: logproto.ChunkRef{ 42 UserID: "fake", 43 From: MustParseDayTime("2020-01-02").Time, 44 Through: MustParseDayTime("2020-01-03").Time, 45 Fingerprint: uint64(456), 46 Checksum: 123, 47 }, 48 } 49 50 // chunk that resolves to v12 51 newChunk := chunk.Chunk{ 52 ChunkRef: logproto.ChunkRef{ 53 UserID: "fake", 54 From: MustParseDayTime("2022-01-02").Time, 55 Through: MustParseDayTime("2022-01-03").Time, 56 Fingerprint: uint64(456), 57 Checksum: 123, 58 }, 59 } 60 61 for _, tc := range []struct { 62 desc string 63 from string 64 exp string 65 }{ 66 { 67 desc: "before v12 encodes entire chunk", 68 from: schema.ExternalKey(oldChunk.ChunkRef), 69 exp: "ZmFrZS8xYzg6MTZmNjM4ZDQ0MDA6MTZmNjhiM2EwMDA6N2I=", 70 }, 71 { 72 desc: "v12+ encodes encodes the non-directory trail", 73 from: schema.ExternalKey(newChunk.ChunkRef), 74 exp: "fake/1c8/MTdlMTgxNWY4MDA6MTdlMWQzYzU0MDA6N2I=", 75 }, 76 } { 77 t.Run(tc.desc, func(t *testing.T) { 78 chk, err := chunk.ParseExternalKey("fake", tc.from) 79 require.Nil(t, err) 80 require.Equal(t, tc.exp, FSEncoder(schema, chk)) 81 }) 82 } 83 }