2018-12-17 05:27:07 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gotest.tools/assert"
|
|
|
|
"gotest.tools/assert/cmp"
|
|
|
|
)
|
|
|
|
|
2018-11-09 09:10:41 -05:00
|
|
|
func testMetadata(name string) ContextMetadata {
|
|
|
|
return ContextMetadata{
|
|
|
|
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}
|
|
|
|
expected2 := ContextMetadata{
|
|
|
|
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}
|
2018-11-09 09:10:41 -05:00
|
|
|
wholeData := []ContextMetadata{
|
|
|
|
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}
|
2018-11-09 09:10:41 -05:00
|
|
|
wholeData := []ContextMetadata{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
}
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.NilError(t, testee.createOrUpdate(ContextMetadata{Metadata: testCtxMeta, Name: "test"}))
|
|
|
|
res, err := testee.get(contextdirOf("test"))
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, testCtxMeta, res.Metadata)
|
|
|
|
}
|