Merge pull request #2546 from phanimarupaka/FnSourceJson

Include json files for fn source and sink
This commit is contained in:
Kubernetes Prow Robot
2020-06-22 11:00:40 -07:00
committed by GitHub
9 changed files with 275 additions and 12 deletions

View File

@@ -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,36 @@ 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": {"name": "foo", annotations: {
a-string-value: '', a-int-value: '0', a-bool-value: 'false'}}}
`,
}
},
},
{
name: "exec_function_args",
args: func(d string) []string {
@@ -468,7 +499,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 +575,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": `

View File

@@ -136,6 +136,51 @@ spec:
}
}
func TestSinkCommandJSON(t *testing.T) {
d, err := ioutil.TempDir("", "kustomize-source-test")
if !assert.NoError(t, err) {
t.FailNow()
}
defer os.RemoveAll(d)
r := commands.GetSinkRunner("")
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'}}, "spec": {"replicas": 1}}
`))
r.Command.SetArgs([]string{d})
if !assert.NoError(t, r.Command.Execute()) {
t.FailNow()
}
actual, err := ioutil.ReadFile(filepath.Join(d, "f1.json"))
if !assert.NoError(t, err) {
t.FailNow()
}
expected := `{
"kind": "Deployment",
"metadata": {
"annotations": {
"app": "nginx2"
},
"labels": {
"app": "nginx2"
},
"name": "foo"
},
"spec": {
"replicas": 1
}
}
`
if !assert.Equal(t, expected, string(actual)) {
t.FailNow()
}
}
func TestSinkCommand_Stdout(t *testing.T) {
d, err := ioutil.TempDir("", "kustomize-source-test")
if !assert.NoError(t, err) {
@@ -249,3 +294,49 @@ spec:
t.FailNow()
}
}
func TestSinkCommandJSON_Stdout(t *testing.T) {
d, err := ioutil.TempDir("", "kustomize-source-test")
if !assert.NoError(t, err) {
t.FailNow()
}
defer os.RemoveAll(d)
// fmt the files
out := &bytes.Buffer{}
r := commands.GetSinkRunner("")
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'}}, "spec": {"replicas": 1}}
`))
r.Command.SetOut(out)
r.Command.SetArgs([]string{})
if !assert.NoError(t, r.Command.Execute()) {
t.FailNow()
}
expected := `{
"kind": "Deployment",
"metadata": {
"annotations": {
"app": "nginx2"
},
"labels": {
"app": "nginx2"
},
"name": "foo"
},
"spec": {
"replicas": 1
}
}
`
if !assert.Equal(t, expected, out.String()) {
println(out.String())
t.FailNow()
}
}

View File

@@ -71,7 +71,7 @@ func (r *SourceRunner) runE(c *cobra.Command, args []string) error {
var inputs []kio.Reader
for _, a := range args {
inputs = append(inputs, kio.LocalPackageReader{PackagePath: a})
inputs = append(inputs, kio.LocalPackageReader{PackagePath: a, MatchFilesGlob: kio.MatchAll})
}
if len(inputs) == 0 {
inputs = []kio.Reader{&kio.ByteReader{Reader: c.InOrStdin()}}

View File

@@ -135,6 +135,77 @@ items:
}
}
func TestSourceCommandJSON(t *testing.T) {
d, err := ioutil.TempDir("", "kustomize-source-test")
if !assert.NoError(t, err) {
return
}
defer os.RemoveAll(d)
err = ioutil.WriteFile(filepath.Join(d, "f1.json"), []byte(`
{
"kind": "Deployment",
"metadata": {
"labels": {
"app": "nginx2"
},
"name": "foo",
"annotations": {
"app": "nginx2"
}
},
"spec": {
"replicas": 1
}
}
`), 0600)
if !assert.NoError(t, err) {
return
}
err = ioutil.WriteFile(filepath.Join(d, "f2.json"), []byte(`
{
"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"
}
},
"spec": {
"replicas": 3
}
}
`), 0600)
if !assert.NoError(t, err) {
return
}
// fmt the files
b := &bytes.Buffer{}
r := commands.GetSourceRunner("")
r.Command.SetArgs([]string{d})
r.Command.SetOut(b)
if !assert.NoError(t, r.Command.Execute()) {
return
}
if !assert.Equal(t, `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'}},
"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/index: '0',
config.kubernetes.io/path: 'f2.json'}}, "spec": {"replicas": 3}}
`, b.String()) {
return
}
}
func TestSourceCommand_Stdin(t *testing.T) {
d, err := ioutil.TempDir("", "kustomize-source-test")
if !assert.NoError(t, err) {
@@ -198,3 +269,48 @@ items:
return
}
}
func TestSourceCommandJSON_Stdin(t *testing.T) {
d, err := ioutil.TempDir("", "kustomize-source-test")
if !assert.NoError(t, err) {
return
}
defer os.RemoveAll(d)
in := bytes.NewBufferString(`
{
"kind": "Deployment",
"metadata": {
"labels": {
"app": "nginx2"
},
"name": "foo",
"annotations": {
"app": "nginx2"
}
},
"spec": {
"replicas": 1
}
}
`)
out := &bytes.Buffer{}
r := commands.GetSourceRunner("")
r.Command.SetArgs([]string{})
r.Command.SetIn(in)
r.Command.SetOut(out)
if !assert.NoError(t, r.Command.Execute()) {
return
}
if !assert.Equal(t, `apiVersion: config.kubernetes.io/v1alpha1
kind: ResourceList
items:
- {"kind": "Deployment", "metadata": {"labels": {"app": "nginx2"}, "name": "foo",
"annotations": {"app": "nginx2", config.kubernetes.io/index: '0'}}, "spec": {
"replicas": 1}}
`, out.String()) {
return
}
}