Skip to content

Commit

Permalink
add test cases for generators and kustomize.config.k8s.io annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
yutaroyamanaka committed Apr 8, 2023
1 parent ff894ed commit 695b941
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions api/krusty/generatorplugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package krusty_test

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
"sigs.k8s.io/kustomize/kyaml/filesys"
)

func TestGeneratorHashSuffixWithMergeBehavior(t *testing.T) {
fSys := filesys.MakeFsOnDisk()
generatorFilename := "generateWithHashRequest.sh"

th := kusttest_test.MakeHarnessWithFs(t, fSys)
o := th.MakeOptionsPluginsEnabled()
o.PluginConfig.FnpLoadingOptions.EnableExec = true

tmpDir, err := filesys.NewTmpConfirmedDir()
assert.NoError(t, err)
th.WriteK(tmpDir.String(), `
resources:
- configmap.yaml
generators:
- |-
kind: Executable
metadata:
name: demo
annotations:
config.kubernetes.io/function: |
exec:
path: ./`+generatorFilename+`
`)

th.WriteF(filepath.Join(tmpDir.String(), "configmap.yaml"), `
apiVersion: v1
kind: ConfigMap
metadata:
name: cmap
data:
a: b
`)
th.WriteF(filepath.Join(tmpDir.String(), generatorFilename), `#!/bin/sh
cat <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: cmap
annotations:
kustomize.config.k8s.io/needs-hash: "true"
kustomize.config.k8s.io/behavior: "merge"
data:
c: d
EOF
`)
assert.NoError(t, os.Chmod(filepath.Join(tmpDir.String(), generatorFilename), 0777))
m := th.Run(tmpDir.String(), o)
assert.NoError(t, err)
yml, err := m.AsYaml()
assert.NoError(t, err)
assert.Equal(t, `apiVersion: v1
data:
a: b
c: d
kind: ConfigMap
metadata:
name: cmap-fh478f99mk
`, string(yml))
}

func TestGeneratorHashSuffixWithReplaceBehavior(t *testing.T) {
fSys := filesys.MakeFsOnDisk()
generatorFilename := "generateWithHashRequest.sh"

th := kusttest_test.MakeHarnessWithFs(t, fSys)
o := th.MakeOptionsPluginsEnabled()
o.PluginConfig.FnpLoadingOptions.EnableExec = true

tmpDir, err := filesys.NewTmpConfirmedDir()
assert.NoError(t, err)
th.WriteK(tmpDir.String(), `
resources:
- configmap.yaml
generators:
- |-
kind: Executable
metadata:
name: demo
annotations:
config.kubernetes.io/function: |
exec:
path: ./`+generatorFilename+`
`)

th.WriteF(filepath.Join(tmpDir.String(), "configmap.yaml"), `
apiVersion: v1
kind: ConfigMap
metadata:
name: cmap
data:
a: b
`)
th.WriteF(filepath.Join(tmpDir.String(), generatorFilename), `#!/bin/sh
cat <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: cmap
annotations:
kustomize.config.k8s.io/needs-hash: "true"
kustomize.config.k8s.io/behavior: "replace"
data:
c: d
EOF
`)
assert.NoError(t, os.Chmod(filepath.Join(tmpDir.String(), generatorFilename), 0777))
m := th.Run(tmpDir.String(), o)
assert.NoError(t, err)
yml, err := m.AsYaml()
assert.NoError(t, err)
assert.Equal(t, `apiVersion: v1
data:
c: d
kind: ConfigMap
metadata:
name: cmap-gbdtcf54mt
`, string(yml))
}

0 comments on commit 695b941

Please sign in to comment.