mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-17 18:25:26 +00:00
E2e test for run with json
This commit is contained in:
@@ -30,6 +30,7 @@ func TestRunE2e(t *testing.T) {
|
||||
expectedErr string
|
||||
skipIfFalseEnv string
|
||||
}{
|
||||
|
||||
{
|
||||
name: "exec_function_no_args",
|
||||
args: func(d string) []string {
|
||||
@@ -63,6 +64,46 @@ metadata:
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
name: "exec_function_no_args_json",
|
||||
args: func(d string) []string {
|
||||
return []string{
|
||||
"--enable-exec", "--exec-path", filepath.Join(d, "e2econtainerconfig"),
|
||||
}
|
||||
},
|
||||
files: func(d string) map[string]string {
|
||||
return map[string]string{
|
||||
"deployment.json": `
|
||||
{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": {
|
||||
"name": "foo"
|
||||
}
|
||||
}
|
||||
`,
|
||||
}
|
||||
},
|
||||
expectedFiles: func(d string) map[string]string {
|
||||
return map[string]string{
|
||||
"deployment.json": `
|
||||
{
|
||||
"apiVersion": "apps/v1",
|
||||
"kind": "Deployment",
|
||||
"metadata": {
|
||||
"annotations": {
|
||||
"a-bool-value": "false",
|
||||
"a-int-value": "0",
|
||||
"a-string-value": ""
|
||||
},
|
||||
"name": "foo"
|
||||
}
|
||||
}
|
||||
`,
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
name: "exec_function_args",
|
||||
args: func(d string) []string {
|
||||
@@ -468,7 +509,6 @@ def run(r, fc):
|
||||
resource["metadata"]["annotations"]["a-string-value"] = fc["data"]["stringValue"]
|
||||
resource["metadata"]["annotations"]["a-int-value"] = fc["data"]["intValue"]
|
||||
resource["metadata"]["annotations"]["a-bool-value"] = fc["data"]["boolValue"]
|
||||
|
||||
run(ctx.resource_list["items"], ctx.resource_list["functionConfig"])
|
||||
`,
|
||||
"config.yaml": `
|
||||
@@ -545,7 +585,6 @@ def run(r, fc):
|
||||
resource["metadata"]["annotations"]["a-string-value"] = fc["data"]["stringValue"]
|
||||
resource["metadata"]["annotations"]["a-int-value"] = fc["data"]["intValue"]
|
||||
resource["metadata"]["annotations"]["a-bool-value"] = fc["data"]["boolValue"]
|
||||
|
||||
run(ctx.resource_list["items"], ctx.resource_list["functionConfig"])
|
||||
`,
|
||||
"deployment.yaml": `
|
||||
|
||||
@@ -147,18 +147,9 @@ func TestSinkCommandJSON(t *testing.T) {
|
||||
r.Command.SetIn(bytes.NewBufferString(`apiVersion: config.kubernetes.io/v1alpha1
|
||||
kind: ResourceList
|
||||
items:
|
||||
- kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx2
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/path: 'f1.json'
|
||||
config.kubernetes.io/format: 'json'
|
||||
spec:
|
||||
replicas: 1
|
||||
- {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo",
|
||||
"annotations": {"app": "nginx2", config.kubernetes.io/format: 'json', config.kubernetes.io/index: '0',
|
||||
config.kubernetes.io/path: 'f1.json'}}, "spec": {"replicas": 1}}
|
||||
`))
|
||||
r.Command.SetArgs([]string{d})
|
||||
if !assert.NoError(t, r.Command.Execute()) {
|
||||
@@ -317,17 +308,9 @@ func TestSinkCommandJSON_Stdout(t *testing.T) {
|
||||
r.Command.SetIn(bytes.NewBufferString(`apiVersion: config.kubernetes.io/v1alpha1
|
||||
kind: ResourceList
|
||||
items:
|
||||
- kind: Deployment
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx2
|
||||
name: foo
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/format: 'json'
|
||||
spec:
|
||||
replicas: 1
|
||||
- {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo",
|
||||
"annotations": {"app": "nginx2", config.kubernetes.io/format: 'json', config.kubernetes.io/index: '0',
|
||||
config.kubernetes.io/path: 'f1.json'}}, "spec": {"replicas": 1}}
|
||||
`))
|
||||
|
||||
r.Command.SetOut(out)
|
||||
|
||||
@@ -194,32 +194,14 @@ func TestSourceCommandJSON(t *testing.T) {
|
||||
if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1
|
||||
kind: ResourceList
|
||||
items:
|
||||
- kind: Deployment
|
||||
metadata:
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/format: 'json'
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/path: 'f1.json'
|
||||
labels:
|
||||
app: nginx2
|
||||
name: foo
|
||||
spec:
|
||||
replicas: 1
|
||||
- apiVersion: v1
|
||||
kind: Abstraction
|
||||
metadata:
|
||||
annotations:
|
||||
config.kubernetes.io/function: |
|
||||
container:
|
||||
image: gcr.io/example/reconciler:v1
|
||||
config.kubernetes.io/local-config: "true"
|
||||
config.kubernetes.io/format: 'json'
|
||||
config.kubernetes.io/index: '0'
|
||||
config.kubernetes.io/path: 'f2.json'
|
||||
name: foo
|
||||
spec:
|
||||
replicas: 3
|
||||
- {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo",
|
||||
"annotations": {"app": "nginx2", config.kubernetes.io/format: 'json', config.kubernetes.io/index: '0',
|
||||
config.kubernetes.io/path: 'f1.json'}}, "spec": {"replicas": 1}}
|
||||
- {"apiVersion": "v1", "kind": "Abstraction", "metadata": {"name": "foo", "annotations": {
|
||||
"config.kubernetes.io/function": "container:\n image: gcr.io/example/reconciler:v1\n",
|
||||
"config.kubernetes.io/local-config": "true", config.kubernetes.io/format: 'json',
|
||||
config.kubernetes.io/index: '0', config.kubernetes.io/path: 'f2.json'}}, "spec": {
|
||||
"replicas": 3}}
|
||||
`, b.String()) {
|
||||
return
|
||||
}
|
||||
@@ -326,17 +308,9 @@ func TestSourceCommandJSON_Stdin(t *testing.T) {
|
||||
if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1
|
||||
kind: ResourceList
|
||||
items:
|
||||
- kind: Deployment
|
||||
metadata:
|
||||
annotations:
|
||||
app: nginx2
|
||||
config.kubernetes.io/format: 'json'
|
||||
config.kubernetes.io/index: '0'
|
||||
labels:
|
||||
app: nginx2
|
||||
name: foo
|
||||
spec:
|
||||
replicas: 1
|
||||
- {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo",
|
||||
"annotations": {"app": "nginx2", config.kubernetes.io/format: 'json', config.kubernetes.io/index: '0'}},
|
||||
"spec": {"replicas": 1}}
|
||||
`, out.String()) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -130,17 +130,12 @@ func (r *ByteReader) Read() ([]*yaml.RNode, error) {
|
||||
if r.AcceptJSON && json.Valid([]byte(inputStr)) {
|
||||
// convert json to yaml string to generate resource list object
|
||||
// with appropriate format annotation
|
||||
yamlValue, err := yaml.ConvertJSONToYAMLString(input.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if r.SetAnnotations == nil {
|
||||
r.SetAnnotations = map[string]string{}
|
||||
}
|
||||
if !r.OmitReaderAnnotations {
|
||||
r.SetAnnotations[kioutil.FormatAnnotation] = JSON
|
||||
}
|
||||
inputStr = yamlValue
|
||||
}
|
||||
|
||||
values := strings.Split(inputStr, "\n---\n")
|
||||
|
||||
@@ -173,10 +173,11 @@ func TestLocalPackageReader_Read_JSON(t *testing.T) {
|
||||
}
|
||||
// TODO: Fix https://github.com/go-yaml/yaml/issues/44 so these are printed correctly
|
||||
expected := []string{
|
||||
`{"a": "b", metadata: {annotations: {config.kubernetes.io/index: '0', config.kubernetes.io/path: 'a_test.json'}}}
|
||||
`{"a": "b", metadata: {annotations: {config.kubernetes.io/format: 'json', config.kubernetes.io/index: '0',
|
||||
config.kubernetes.io/path: 'a_test.json'}}}
|
||||
`,
|
||||
`{"e": "f", "g": {"h": ["i", "j"]}, metadata: {annotations: {config.kubernetes.io/index: '0',
|
||||
config.kubernetes.io/path: 'b_test.json'}}}
|
||||
`{"e": "f", "g": {"h": ["i", "j"]}, metadata: {annotations: {config.kubernetes.io/format: 'json',
|
||||
config.kubernetes.io/index: '0', config.kubernetes.io/path: 'b_test.json'}}}
|
||||
`,
|
||||
}
|
||||
for i := range nodes {
|
||||
|
||||
@@ -110,13 +110,13 @@ func (r RunFns) getNodesAndFilters() (
|
||||
// the same one for reading must be used for writing if deleting Resources
|
||||
var outputPkg *kio.LocalPackageReadWriter
|
||||
if r.Path != "" {
|
||||
outputPkg = &kio.LocalPackageReadWriter{PackagePath: r.Path}
|
||||
outputPkg = &kio.LocalPackageReadWriter{PackagePath: r.Path, MatchFilesGlob: kio.MatchAll}
|
||||
}
|
||||
|
||||
if r.Input == nil {
|
||||
p.Inputs = []kio.Reader{outputPkg}
|
||||
} else {
|
||||
p.Inputs = []kio.Reader{&kio.ByteReader{Reader: r.Input}}
|
||||
p.Inputs = []kio.Reader{&kio.ByteReader{Reader: r.Input, AcceptJSON: true}}
|
||||
}
|
||||
if err := p.Execute(); err != nil {
|
||||
return nil, nil, outputPkg, err
|
||||
|
||||
@@ -183,7 +183,7 @@ func (sd SetterDefinition) Filter(object *yaml.RNode) (*yaml.RNode, error) {
|
||||
}
|
||||
|
||||
if sd.Schema != "" {
|
||||
schNode, err := yaml.ConvertJSONToYAMLNode(sd.Schema)
|
||||
schNode, err := yaml.ConvertJSONToYamlNode(sd.Schema)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -722,33 +722,24 @@ func (rn *RNode) UnmarshalJSON(b []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConvertJSONToYAMLNode parses input json string and returns equivalent yaml node
|
||||
func ConvertJSONToYAMLNode(jsonStr string) (*RNode, error) {
|
||||
yml, err := ConvertJSONToYAMLString(jsonStr)
|
||||
// ConvertJSONToYamlNode parses input json string and returns equivalent yaml node
|
||||
func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) {
|
||||
var body map[string]interface{}
|
||||
err := json.Unmarshal([]byte(jsonStr), &body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err := Parse(yml)
|
||||
yml, err := yaml.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
node, err := Parse(string(yml))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return node, nil
|
||||
}
|
||||
|
||||
// ConvertJSONToYAMLString parses input json string and returns equivalent yaml string
|
||||
func ConvertJSONToYAMLString(jsonStr string) (string, error) {
|
||||
var body map[string]interface{}
|
||||
err := json.Unmarshal([]byte(jsonStr), &body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
yml, err := yaml.Marshal(body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(yml), nil
|
||||
}
|
||||
|
||||
// checkKey returns true if all elems have the key
|
||||
func checkKey(key string, elems []*Node) bool {
|
||||
count := 0
|
||||
|
||||
@@ -147,7 +147,7 @@ hello: world
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertJSONToYAMLNode(t *testing.T) {
|
||||
func TestConvertJSONToYamlNode(t *testing.T) {
|
||||
inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}`
|
||||
expected := `enum:
|
||||
- allowedValue1
|
||||
@@ -156,7 +156,7 @@ maxLength: 15
|
||||
type: string
|
||||
`
|
||||
|
||||
node, err := ConvertJSONToYAMLNode(inputJSON)
|
||||
node, err := ConvertJSONToYamlNode(inputJSON)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
@@ -166,32 +166,3 @@ type: string
|
||||
}
|
||||
assert.Equal(t, expected, actual)
|
||||
}
|
||||
|
||||
func TestConvertJSONToYAMLString(t *testing.T) {
|
||||
inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}`
|
||||
expected := `enum:
|
||||
- allowedValue1
|
||||
- allowedValue2
|
||||
maxLength: 15
|
||||
type: string
|
||||
`
|
||||
|
||||
actual, err := ConvertJSONToYAMLString(inputJSON)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
assert.Equal(t, expected, actual)
|
||||
}
|
||||
|
||||
// error if there are multiple json blobs in input string
|
||||
func TestConvertJSONToYAMLStringError(t *testing.T) {
|
||||
inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}
|
||||
{"type": "string", "maxLength": 5, "enum": ["allowedValue2", "allowedValue3"]}`
|
||||
expected := `invalid character '{' after top-level value`
|
||||
|
||||
_, err := ConvertJSONToYAMLString(inputJSON)
|
||||
if !assert.Error(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
assert.Equal(t, expected, err.Error())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user