From f77ccefad3d475c27f6d7c6cb7d3ac81ea022016 Mon Sep 17 00:00:00 2001 From: Vilius Pranckaitis Date: Tue, 19 Jan 2021 11:51:57 +0200 Subject: [PATCH] add tests --- .../storage/index/convert/convert_test.go | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/src/dbnode/storage/index/convert/convert_test.go b/src/dbnode/storage/index/convert/convert_test.go index aa72449797..5537df2f45 100644 --- a/src/dbnode/storage/index/convert/convert_test.go +++ b/src/dbnode/storage/index/convert/convert_test.go @@ -30,6 +30,7 @@ import ( "github.com/m3db/m3/src/x/checked" "github.com/m3db/m3/src/x/ident" "github.com/m3db/m3/src/x/pool" + "github.com/m3db/m3/src/x/serialize" "github.com/m3db/m3/src/x/test" "github.com/stretchr/testify/assert" @@ -171,6 +172,113 @@ func TestFromSeriesIDAndTagIterReuseBytesFromSeriesId(t *testing.T) { } } +func TestFromSeriesIDAndEncodedTagsInvalid(t *testing.T) { + var ( + id = ident.StringID("foo") + tags = ident.NewTags( + ident.StringTag(string(convert.ReservedFieldNameID), "value"), + ) + encodedTags = toEncodedTags(t, tags) + ) + _, err := convert.FromSeriesIDAndEncodedTags(id, encodedTags) + assert.Error(t, err) +} + +func TestFromSeriesIDAndEncodedTagsValid(t *testing.T) { + var ( + id = ident.StringID("foo") + tags = ident.NewTags( + ident.StringTag("bar", "baz"), + ) + encodedTags = toEncodedTags(t, tags) + ) + d, err := convert.FromSeriesIDAndEncodedTags(id, encodedTags) + assert.NoError(t, err) + assertContentsMatch(t, id, tags.Values(), d) +} + +func TestFromSeriesIDAndEncodedTagsReuseBytesFromSeriesId(t *testing.T) { + tests := []struct { + name string + id string + }{ + { + name: "tags in ID", + id: "bar=baz,quip=quix", + }, + { + name: "tags in ID with specific format", + id: `{bar="baz",quip="quix"}`, + }, + { + name: "tags in ID with specific format reverse order", + id: `{quip="quix",bar="baz"}`, + }, + { + name: "inexact tag occurrence in ID", + id: "quixquip_bazillion_barometers", + }, + } + var ( + tags = ident.NewTags( + ident.StringTag("bar", "baz"), + ident.StringTag("quip", "quix"), + ) + encodedTags = toEncodedTags(t, tags) + ) + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + seriesID := ident.StringID(tt.id) + d, err := convert.FromSeriesIDAndEncodedTags(seriesID, encodedTags) + assert.NoError(t, err) + assertContentsMatch(t, seriesID, tags.Values(), d) + for i := range d.Fields { + assertBackedBySameData(t, d.ID, d.Fields[i].Name) + assertBackedBySameData(t, d.ID, d.Fields[i].Value) + } + }) + } +} + +func TestFromSeriesIDAndEncodedTagsInvalidEncodedTags(t *testing.T) { + validEncodedTags := []byte{117, 39, 1, 0, 3, 0, 98, 97, 114, 3, 0, 98, 97, 122} + + tests := []struct { + name string + encodedTags []byte + }{ + { + name: "incomplete header", + encodedTags: validEncodedTags[:3], + }, + { + name: "incomplete tag name length", + encodedTags: validEncodedTags[:5], + }, + { + name: "incomplete tag value length", + encodedTags: validEncodedTags[:10], + }, + { + name: "invalid magic number", + encodedTags: []byte{42, 42, 0, 0}, + }, + { + name: "empty tag name", + encodedTags: []byte{117, 39, 1, 0, 0, 0, 3, 0, 98, 97, 122}, + }, + } + seriesID := ident.StringID("foo") + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := convert.FromSeriesIDAndEncodedTags(seriesID, tt.encodedTags) + assert.Error(t, err) + }) + } +} + func TestToSeriesValid(t *testing.T) { d := doc.Metadata{ ID: []byte("foo"), @@ -311,3 +419,15 @@ func assertBackedBySameData(t *testing.T, outer, inner []byte) { assert.Fail(t, "inner byte sequence wasn't found") } } + +func toEncodedTags(t *testing.T, tags ident.Tags) []byte { + pool := serialize.NewTagEncoderPool(serialize.NewTagEncoderOptions(), nil) + pool.Init() + encoder := pool.Get() + defer encoder.Finalize() + + require.NoError(t, encoder.Encode(ident.NewTagsIterator(tags))) + data, ok := encoder.Data() + require.True(t, ok) + return append([]byte(nil), data.Bytes()...) +}