Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceralon committed Nov 3, 2023
1 parent 533ece1 commit 6bde692
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions kustomize/commands/edit/fix/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"

testutils_test "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/testutils"
"sigs.k8s.io/kustomize/kyaml/filesys"
)
Expand Down Expand Up @@ -1143,3 +1144,76 @@ metadata:
a.b.c: SOME_SECRET_NAME_PLACEHOLDER
`, string(content))
}

func TestFixVarsWithPatch(t *testing.T) {
kustomization := []byte(`
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
patchesStrategicMerge:
- patch.yaml
vars:
- name: CERTIFICATE_NAMESPACE
objref:
name: system
fieldref:
fieldpath: metadata.namespace
`)
patch := []byte(`
apiVersion: apps/v1
kapiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: $(CERTIFICATE_NAMESPACE)
`)

fSys := filesys.MakeFsInMemory()
testutils_test.WriteTestKustomizationWith(fSys, kustomization)
fSys.WriteFile("patch.yaml", patch)

Check failure on line 1179 in kustomize/commands/edit/fix/convert_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `fSys.WriteFile` is not checked (errcheck)
cmd := NewCmdFix(fSys, os.Stdout)
assert.NoError(t, cmd.Flags().Set("vars", "true"))
assert.NoError(t, cmd.RunE(cmd, nil))
content, err := testutils_test.ReadTestKustomization(fSys)
assert.NoError(t, err)

assert.Equal(t, `
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
patches:
- path: patch.yaml
replacements:
- source:
fieldPath: metadata.namespace
name: system
targets:
- fieldPaths:
- spec.template.spec.containers.0.name
select:
namespace: system
`, string(content))

content, err = fSys.ReadFile("patch.yaml")
assert.NoError(t, err)
assert.Equal(t, `
apiVersion: apps/v1
kapiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: CERTIFICATE_NAMESPACE_PLACEHOLDER
`, string(content))
}

0 comments on commit 6bde692

Please sign in to comment.