2018-12-17 05:27:07 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gotest.tools/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type endpoint struct {
|
|
|
|
Foo string `json:"a_very_recognizable_field_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type context struct {
|
|
|
|
Bar string `json:"another_very_recognizable_field_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var testCfg = NewConfig(func() interface{} { return &context{} },
|
|
|
|
EndpointTypeGetter("ep1", func() interface{} { return &endpoint{} }),
|
|
|
|
EndpointTypeGetter("ep2", func() interface{} { return &endpoint{} }),
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExportImport(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
s := New(testDir, testCfg)
|
2018-11-09 09:10:41 -05:00
|
|
|
err = s.CreateOrUpdateContext(
|
2018-12-17 05:27:07 -05:00
|
|
|
ContextMetadata{
|
|
|
|
Endpoints: map[string]interface{}{
|
|
|
|
"ep1": endpoint{Foo: "bar"},
|
|
|
|
},
|
|
|
|
Metadata: context{Bar: "baz"},
|
2018-11-09 09:10:41 -05:00
|
|
|
Name: "source",
|
2018-12-17 05:27:07 -05:00
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
err = s.ResetContextEndpointTLSMaterial("source", "ep1", &EndpointTLSData{
|
|
|
|
Files: map[string][]byte{
|
|
|
|
"file1": []byte("test-data"),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
r := Export("source", s)
|
|
|
|
defer r.Close()
|
|
|
|
err = Import("dest", s, r)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
srcMeta, err := s.GetContextMetadata("source")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
destMeta, err := s.GetContextMetadata("dest")
|
|
|
|
assert.NilError(t, err)
|
2018-11-09 09:10:41 -05:00
|
|
|
assert.DeepEqual(t, destMeta.Metadata, srcMeta.Metadata)
|
|
|
|
assert.DeepEqual(t, destMeta.Endpoints, srcMeta.Endpoints)
|
2018-12-17 05:27:07 -05:00
|
|
|
srcFileList, err := s.ListContextTLSFiles("source")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
destFileList, err := s.ListContextTLSFiles("dest")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, srcFileList, destFileList)
|
|
|
|
srcData, err := s.GetContextTLSData("source", "ep1", "file1")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, "test-data", string(srcData))
|
|
|
|
destData, err := s.GetContextTLSData("dest", "ep1", "file1")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, "test-data", string(destData))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemove(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
s := New(testDir, testCfg)
|
2018-11-09 09:10:41 -05:00
|
|
|
err = s.CreateOrUpdateContext(
|
2018-12-17 05:27:07 -05:00
|
|
|
ContextMetadata{
|
|
|
|
Endpoints: map[string]interface{}{
|
|
|
|
"ep1": endpoint{Foo: "bar"},
|
|
|
|
},
|
|
|
|
Metadata: context{Bar: "baz"},
|
2018-11-09 09:10:41 -05:00
|
|
|
Name: "source",
|
2018-12-17 05:27:07 -05:00
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.NilError(t, s.ResetContextEndpointTLSMaterial("source", "ep1", &EndpointTLSData{
|
|
|
|
Files: map[string][]byte{
|
|
|
|
"file1": []byte("test-data"),
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
assert.NilError(t, s.RemoveContext("source"))
|
|
|
|
_, err = s.GetContextMetadata("source")
|
|
|
|
assert.Check(t, IsErrContextDoesNotExist(err))
|
|
|
|
f, err := s.ListContextTLSFiles("source")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, 0, len(f))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListEmptyStore(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
store := New(testDir, testCfg)
|
|
|
|
result, err := store.ListContexts()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, len(result) == 0)
|
|
|
|
}
|
2018-11-09 09:10:41 -05:00
|
|
|
|
|
|
|
func TestErrHasCorrectContext(t *testing.T) {
|
|
|
|
testDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
assert.NilError(t, err)
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
store := New(testDir, testCfg)
|
|
|
|
_, err = store.GetContextMetadata("no-exists")
|
|
|
|
assert.ErrorContains(t, err, "no-exists")
|
|
|
|
assert.Check(t, IsErrContextDoesNotExist(err))
|
|
|
|
}
|