Initial (temporary) implementation of search doc.

Document describing how to convert a kustomization file into a
searchable document on appengine (will be changed to elasticsearch)
soon.
This commit is contained in:
Damien Robichaud
2019-07-09 13:04:10 -07:00
parent c464fb0a81
commit c02b4f3a11
5 changed files with 356 additions and 0 deletions

View File

@@ -0,0 +1,153 @@
package doc
import (
"fmt"
"reflect"
"sort"
"strings"
"testing"
"time"
"google.golang.org/appengine/search"
)
func TestLoadFailures(t *testing.T) {
type sentinelType struct{}
sentinel := sentinelType{}
testCases := [][]search.Field{
{{Name: identifierStr, Value: sentinel}},
{{Name: documentStr, Value: sentinel}},
{{Name: repoURLStr, Value: sentinel}},
{{Name: filePathStr, Value: sentinel}},
{{Name: creationTimeStr, Value: sentinel}},
}
for _, test := range testCases {
var k KustomizationDocument
err := k.Load(test, nil)
if err == nil {
t.Errorf("Type missmatch %#v should not be loadable", test)
}
}
}
func TestFieldLoadSaver(t *testing.T) {
commonTestCases := []KustomizationDocument{
{
identifiers: []Atom{"namePrefix", "metadata.name", "kind"},
FilePath: "some/path/kustomization.yaml",
RepositoryURL: "https://example.com/kustomize",
CreationTime: time.Now(),
DocumentData: `
namePrefix: dev-
metadata:
name: app
kind: Deployment
`,
},
}
for _, test := range commonTestCases {
fields, metadata, err := test.Save()
if err != nil {
t.Errorf("Error calling Save(): %s\n", err)
}
doc := KustomizationDocument{}
err = doc.Load(fields, metadata)
if err != nil {
t.Errorf("Doc failed to load: %s\n", err)
}
if !reflect.DeepEqual(test, doc) {
t.Errorf("Expected loaded document (%+v) to be equal to (%+v)\n", doc, test)
}
}
}
func TestParseYAML(t *testing.T) {
testCases := []struct {
identifiers []Atom
yaml string
}{
{
identifiers: []Atom{
"namePrefix",
"metadata",
"metadata name",
"kind",
},
yaml: `
namePrefix: dev-
metadata:
name: app
kind: Deployment
`,
},
{
identifiers: []Atom{
"namePrefix",
"metadata",
"metadata name",
"metadata spec",
"metadata spec replicas",
"kind",
"replicas",
"replicas name",
"replicas count",
"resource",
},
yaml: `
namePrefix: dev-
# map of map
metadata:
name: n1
spec:
replicas: 3
kind: Deployment
#list of map
replicas:
- name: n1
count: 3
- name: n2
count: 3
# list
resource:
- file1.yaml
- file2.yaml
`,
},
}
atomStrs := func(atoms []Atom) []string {
strs := make([]string, 0, len(atoms))
for _, val := range atoms {
strs = append(strs, fmt.Sprintf("%v", val))
}
return strs
}
for _, test := range testCases {
doc := KustomizationDocument{
DocumentData: test.yaml,
FilePath: "example/path/kustomization.yaml",
}
err := doc.ParseYAML()
if err != nil {
t.Errorf("Document error error: %s", err)
}
docIDs := atomStrs(doc.identifiers)
expectedIDs := atomStrs(test.identifiers)
sort.Strings(docIDs)
sort.Strings(expectedIDs)
if !reflect.DeepEqual(docIDs, expectedIDs) {
t.Errorf("Expected loaded document (%v) to be equal to (%v)\n",
strings.Join(docIDs, ","), strings.Join(expectedIDs, ","))
}
}
}