2018-12-17 05:27:07 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
2019-03-06 09:01:12 -05:00
|
|
|
"crypto/rand"
|
2018-12-17 05:27:07 -05:00
|
|
|
"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)
|
2019-03-06 09:01:12 -05:00
|
|
|
file1 := make([]byte, 1500)
|
|
|
|
rand.Read(file1)
|
|
|
|
file2 := make([]byte, 3700)
|
|
|
|
rand.Read(file2)
|
2018-12-17 05:27:07 -05:00
|
|
|
err = s.ResetContextEndpointTLSMaterial("source", "ep1", &EndpointTLSData{
|
|
|
|
Files: map[string][]byte{
|
2019-03-06 09:01:12 -05:00
|
|
|
"file1": file1,
|
|
|
|
"file2": file2,
|
2018-12-17 05:27:07 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
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)
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.Equal(t, 1, len(destFileList))
|
|
|
|
assert.Equal(t, 1, len(srcFileList))
|
|
|
|
assert.Equal(t, 2, len(destFileList["ep1"]))
|
|
|
|
assert.Equal(t, 2, len(srcFileList["ep1"]))
|
|
|
|
srcData1, err := s.GetContextTLSData("source", "ep1", "file1")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.DeepEqual(t, file1, srcData1)
|
|
|
|
srcData2, err := s.GetContextTLSData("source", "ep1", "file2")
|
2018-12-17 05:27:07 -05:00
|
|
|
assert.NilError(t, err)
|
2019-03-06 09:01:12 -05:00
|
|
|
assert.DeepEqual(t, file2, srcData2)
|
|
|
|
destData1, err := s.GetContextTLSData("dest", "ep1", "file1")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, file1, destData1)
|
|
|
|
destData2, err := s.GetContextTLSData("dest", "ep1", "file2")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, file2, destData2)
|
2018-12-17 05:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|