2018-12-17 05:27:07 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"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) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
testee := metadataStore{root: testDir, config: testCfg}
|
|
|
|
_, err = testee.get("noexist")
|
|
|
|
assert.Assert(t, IsErrContextDoesNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetadataCreateGetRemove(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
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")
|
|
|
|
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}
|
2018-11-09 09:10:41 -05:00
|
|
|
meta, err := testee.get(contextdirOf("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)
|
2018-11-09 09:10:41 -05:00
|
|
|
meta, err = testee.get(contextdirOf("test-context"))
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, meta, expected2)
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.NilError(t, testee.remove(contextdirOf("test-context")))
|
|
|
|
assert.NilError(t, testee.remove(contextdirOf("test-context"))) // support duplicate remove
|
|
|
|
_, err = testee.get(contextdirOf("test-context"))
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.Assert(t, IsErrContextDoesNotExist(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetadataRespectJsonAnnotation(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
testee := metadataStore{root: testDir, config: testCfg}
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.NilError(t, testee.createOrUpdate(testMetadata("test")))
|
|
|
|
bytes, err := ioutil.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) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
testee := metadataStore{root: testDir, 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 {
|
|
|
|
err = testee.createOrUpdate(s)
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := testee.list()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, data, wholeData)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyConfig(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
testee := metadataStore{root: testDir}
|
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 {
|
|
|
|
err = testee.createOrUpdate(s)
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
testee := metadataStore{root: testDir, config: NewConfig(func() interface{} { return &contextWithEmbedding{} })}
|
|
|
|
testCtxMeta := contextWithEmbedding{
|
|
|
|
embeddedStruct: embeddedStruct{
|
|
|
|
Val: "Hello",
|
|
|
|
},
|
|
|
|
}
|
2019-04-18 09:12:30 -04:00
|
|
|
assert.NilError(t, testee.createOrUpdate(Metadata{Metadata: testCtxMeta, Name: "test"}))
|
2018-11-09 09:10:41 -05:00
|
|
|
res, err := testee.get(contextdirOf("test"))
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, testCtxMeta, res.Metadata)
|
|
|
|
}
|