1- // Copyright 2019 The Kubernetes Authors.
1+ // Copyright 2023 The Kubernetes Authors.
22// SPDX-License-Identifier: Apache-2.0
33
44package remove //nolint:testpackage
55
66import (
77 "fmt"
8- "strings"
98 "testing"
109
11- "github.com/stretchr/testify/assert "
12- testutils_test "sigs.k8s.io/kustomize/kustomize/v4 /commands/internal/testutils"
10+ "github.com/stretchr/testify/require "
11+ testutils_test "sigs.k8s.io/kustomize/kustomize/v5 /commands/internal/testutils"
1312 "sigs.k8s.io/kustomize/kyaml/filesys"
1413)
1514
@@ -18,9 +17,10 @@ func TestRemoveSecret(t *testing.T) {
1817 const secretName02 = "example-secret-02"
1918
2019 tests := map [string ]struct {
21- input string
22- args []string
23- expectedErr string
20+ input string
21+ args []string
22+ expectedOutput string
23+ expectedErr string
2424 }{
2525 "happy path" : {
2626 input : fmt .Sprintf (`
@@ -32,6 +32,10 @@ secretGenerator:
3232 - longsecret.txt
3333` , secretName01 ),
3434 args : []string {secretName01 },
35+ expectedOutput : `
36+ apiVersion: kustomize.config.k8s.io/v1beta1
37+ kind: Kustomization
38+ ` ,
3539 },
3640 "multiple" : {
3741 input : fmt .Sprintf (`
@@ -48,6 +52,10 @@ secretGenerator:
4852 args : []string {
4953 fmt .Sprintf ("%s,%s" , secretName01 , secretName02 ),
5054 },
55+ expectedOutput : `
56+ apiVersion: kustomize.config.k8s.io/v1beta1
57+ kind: Kustomization
58+ ` ,
5159 },
5260 "miss" : {
5361 input : fmt .Sprintf (`
@@ -58,7 +66,31 @@ secretGenerator:
5866 files:
5967 - longsecret.txt
6068` , secretName01 ),
61- args : []string {"foo" },
69+ args : []string {"foo" },
70+ expectedErr : "no specified secret(s) were found" ,
71+ },
72+ "no secret name specified" : {
73+ args : []string {},
74+ expectedErr : "at least one secret name must be specified" ,
75+ },
76+ "too many secret names specified" : {
77+ args : []string {"test1" , "test2" },
78+ expectedErr : "too many arguments" ,
79+ },
80+ "one existing and one non-existing" : {
81+ input : fmt .Sprintf (`
82+ apiVersion: kustomize.config.k8s.io/v1beta1
83+ kind: Kustomization
84+ secretGenerator:
85+ - name: %s
86+ files:
87+ - application.properties
88+ ` , secretName01 ),
89+ args : []string {fmt .Sprintf ("%s,%s" , secretName01 , "foo" )},
90+ expectedOutput : `
91+ apiVersion: kustomize.config.k8s.io/v1beta1
92+ kind: Kustomization
93+ ` ,
6294 },
6395 }
6496
@@ -68,17 +100,17 @@ secretGenerator:
68100 testutils_test .WriteTestKustomizationWith (fSys , []byte (tc .input ))
69101 cmd := newCmdRemoveSecret (fSys )
70102 err := cmd .RunE (cmd , tc .args )
103+
71104 if tc .expectedErr != "" {
72- assert .Error (t , err )
73- assert .Contains (t , err .Error (), tc .expectedErr )
74- } else {
75- assert .NoError (t , err )
76- content , err := testutils_test .ReadTestKustomization (fSys )
77- assert .NoError (t , err )
78- for _ , opt := range strings .Split (tc .args [0 ], "," ) {
79- assert .NotContains (t , string (content ), opt )
80- }
105+ require .Error (t , err )
106+ require .Contains (t , err .Error (), tc .expectedErr )
107+ return
81108 }
109+
110+ require .NoError (t , err )
111+ content , err := testutils_test .ReadTestKustomization (fSys )
112+ require .NoError (t , err )
113+ require .Equal (t , tc .expectedOutput , string (content ))
82114 })
83115 }
84116}
0 commit comments