Refactor openAPI

Refactor global openapi
This commit is contained in:
Phani Teja Marupaka
2020-11-10 12:50:24 -08:00
parent b2ba82a0bd
commit b6a4dd446a
27 changed files with 178 additions and 503 deletions

View File

@@ -4,7 +4,9 @@
package setters2
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
@@ -736,7 +738,6 @@ metadata:
t.Run(test.name, func(t *testing.T) {
// reset the openAPI afterward
defer openapi.ResetOpenAPI()
initSchema(t, test.openapi)
// parse the input to be modified
r, err := yaml.Parse(test.input)
@@ -745,7 +746,7 @@ metadata:
}
// invoke the setter
instance := &Set{Name: test.setter}
instance := &Set{Name: test.setter, SettersSchema: SettersSchema(t, test.openapi)}
result, err := instance.Filter(r)
if !assert.NoError(t, err) {
t.FailNow()
@@ -880,7 +881,6 @@ spec:
t.Run(test.name, func(t *testing.T) {
// reset the openAPI afterward
defer openapi.ResetOpenAPI()
initSchema(t, test.openapi)
// parse the input to be modified
var inputNodes []*yaml.RNode
@@ -893,7 +893,7 @@ spec:
}
// invoke the setter
instance := &Set{Name: test.setter}
instance := &Set{Name: test.setter, SettersSchema: SettersSchema(t, test.openapi)}
result, err := SetAll(instance).Filter(inputNodes)
if !assert.NoError(t, err) {
t.FailNow()
@@ -920,44 +920,19 @@ spec:
}
// initSchema initializes the openAPI with the definitions from s
func initSchema(t *testing.T, s string) {
// parse out the schema from the input openAPI
y, err := yaml.Parse(s)
func SettersSchema(t *testing.T, s string) *spec.Schema {
dir, err := ioutil.TempDir("", "")
assert.NoError(t, err)
defer os.RemoveAll(dir)
err = ioutil.WriteFile(filepath.Join(dir, "Krmfile"), []byte(s), 0600)
if !assert.NoError(t, err) {
t.FailNow()
}
// get the field containing the openAPI
f := y.Field("openAPI")
if !assert.NotNil(t, f) {
t.FailNow()
}
defs, err := f.Value.String()
if !assert.NoError(t, err) {
t.FailNow()
}
// convert the yaml openAPI to an interface{}
// which can be marshalled into json
var o interface{}
err = yaml.Unmarshal([]byte(defs), &o)
if !assert.NoError(t, err) {
t.FailNow()
}
// convert the interface{} into a json string
j, err := json.Marshal(o)
if !assert.NoError(t, err) {
t.FailNow()
}
// reset the openAPI to clear existing definitions
openapi.ResetOpenAPI()
// add the json schema to the global schema
err = openapi.AddSchema(j)
sc, err := openapi.SchemaFromFile(filepath.Join(dir, "Krmfile"))
if !assert.NoError(t, err) {
t.FailNow()
}
return sc
}
func TestSetOpenAPI_Filter(t *testing.T) {