2018-12-17 05:27:07 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/assert/cmp"
|
2018-12-17 05:27:07 -05:00
|
|
|
)
|
|
|
|
|
2019-04-18 09:12:30 -04:00
|
|
|
func testMetadata(name string) Metadata {
|
|
|
|
return Metadata{
|
2018-11-09 09:10:41 -05:00
|
|
|
Endpoints: map[string]interface{}{
|
|
|
|
"ep1": endpoint{Foo: "bar"},
|
|
|
|
},
|
|
|
|
Metadata: context{Bar: "baz"},
|
|
|
|
Name: name,
|
|
|
|
}
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetadataGetNotExisting(t *testing.T) {
|
2022-02-25 08:37:07 -05:00
|
|
|
testee := metadataStore{root: t.TempDir(), config: testCfg}
|
|
|
|
_, err := testee.get("noexist")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.Assert(t, IsErrContextDoesNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetadataCreateGetRemove(t *testing.T) {
|
2022-02-25 08:37:07 -05:00
|
|
|
testDir := t.TempDir()
|
2018-12-17 05:27:07 -05:00
|
|
|
testee := metadataStore{root: testDir, config: testCfg}
|
2019-04-18 09:12:30 -04:00
|
|
|
expected2 := Metadata{
|
2018-12-17 05:27:07 -05:00
|
|
|
Endpoints: map[string]interface{}{
|
|
|
|
"ep1": endpoint{Foo: "baz"},
|
|
|
|
"ep2": endpoint{Foo: "bee"},
|
|
|
|
},
|
|
|
|
Metadata: context{Bar: "foo"},
|
2018-11-09 09:10:41 -05:00
|
|
|
Name: "test-context",
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
2018-11-09 09:10:41 -05:00
|
|
|
testMeta := testMetadata("test-context")
|
2022-02-25 08:37:07 -05:00
|
|
|
err := testee.createOrUpdate(testMeta)
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
// create a new instance to check it does not depend on some sort of state
|
|
|
|
testee = metadataStore{root: testDir, config: testCfg}
|
2022-09-28 10:33:18 -04:00
|
|
|
meta, err := testee.get("test-context")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.DeepEqual(t, meta, testMeta)
|
2018-12-17 05:27:07 -05:00
|
|
|
|
|
|
|
// update
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
err = testee.createOrUpdate(expected2)
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2022-09-28 10:33:18 -04:00
|
|
|
meta, err = testee.get("test-context")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, meta, expected2)
|
|
|
|
|
2022-09-28 10:33:18 -04:00
|
|
|
assert.NilError(t, testee.remove("test-context"))
|
|
|
|
assert.NilError(t, testee.remove("test-context")) // support duplicate remove
|
|
|
|
_, err = testee.get("test-context")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.Assert(t, IsErrContextDoesNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetadataRespectJsonAnnotation(t *testing.T) {
|
2022-02-25 08:37:07 -05:00
|
|
|
testDir := t.TempDir()
|
2018-12-17 05:27:07 -05:00
|
|
|
testee := metadataStore{root: testDir, config: testCfg}
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.NilError(t, testee.createOrUpdate(testMetadata("test")))
|
2022-02-25 08:37:07 -05:00
|
|
|
bytes, err := os.ReadFile(filepath.Join(testDir, string(contextdirOf("test")), "meta.json"))
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, cmp.Contains(string(bytes), "a_very_recognizable_field_name"))
|
|
|
|
assert.Assert(t, cmp.Contains(string(bytes), "another_very_recognizable_field_name"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetadataList(t *testing.T) {
|
2022-02-25 08:37:07 -05:00
|
|
|
testee := metadataStore{root: t.TempDir(), config: testCfg}
|
2019-04-18 09:12:30 -04:00
|
|
|
wholeData := []Metadata{
|
2018-11-09 09:10:41 -05:00
|
|
|
testMetadata("context1"),
|
|
|
|
testMetadata("context2"),
|
|
|
|
testMetadata("context3"),
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
for _, s := range wholeData {
|
2022-02-25 08:37:07 -05:00
|
|
|
assert.NilError(t, testee.createOrUpdate(s))
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := testee.list()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, data, wholeData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyConfig(t *testing.T) {
|
2022-02-25 08:37:07 -05:00
|
|
|
testee := metadataStore{root: t.TempDir()}
|
2019-04-18 09:12:30 -04:00
|
|
|
wholeData := []Metadata{
|
2018-11-09 09:10:41 -05:00
|
|
|
testMetadata("context1"),
|
|
|
|
testMetadata("context2"),
|
|
|
|
testMetadata("context3"),
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
for _, s := range wholeData {
|
2022-02-25 08:37:07 -05:00
|
|
|
assert.NilError(t, testee.createOrUpdate(s))
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := testee.list()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, len(data), len(wholeData))
|
|
|
|
}
|
|
|
|
|
|
|
|
type contextWithEmbedding struct {
|
|
|
|
embeddedStruct
|
|
|
|
}
|
|
|
|
type embeddedStruct struct {
|
|
|
|
Val string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithEmbedding(t *testing.T) {
|
2022-02-25 08:37:07 -05:00
|
|
|
testee := metadataStore{
|
|
|
|
root: t.TempDir(),
|
|
|
|
config: NewConfig(func() interface{} { return &contextWithEmbedding{} }),
|
|
|
|
}
|
2018-12-17 05:27:07 -05:00
|
|
|
testCtxMeta := contextWithEmbedding{
|
|
|
|
embeddedStruct: embeddedStruct{
|
|
|
|
Val: "Hello",
|
|
|
|
},
|
|
|
|
}
|
2019-04-18 09:12:30 -04:00
|
|
|
assert.NilError(t, testee.createOrUpdate(Metadata{Metadata: testCtxMeta, Name: "test"}))
|
2022-09-28 10:33:18 -04:00
|
|
|
res, err := testee.get("test")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, testCtxMeta, res.Metadata)
|
|
|
|
}
|