@@ -17,6 +17,7 @@ limitations under the License.
17
17
package common
18
18
19
19
import (
20
+ "encoding/json"
20
21
"fmt"
21
22
22
23
"k8s.io/api/core/v1"
@@ -25,7 +26,9 @@ import (
25
26
"k8s.io/kubernetes/test/e2e/framework"
26
27
imageutils "k8s.io/kubernetes/test/utils/image"
27
28
29
+ "encoding/base64"
28
30
"github.com/onsi/ginkgo"
31
+ "k8s.io/apimachinery/pkg/types"
29
32
)
30
33
31
34
var _ = ginkgo .Describe ("[sig-api-machinery] Secrets" , func () {
@@ -134,6 +137,89 @@ var _ = ginkgo.Describe("[sig-api-machinery] Secrets", func() {
134
137
secret , err := createEmptyKeySecretForTest (f )
135
138
framework .ExpectError (err , "created secret %q with empty key in namespace %q" , secret .Name , f .Namespace .Name )
136
139
})
140
+
141
+ ginkgo .It ("should patch a secret" , func () {
142
+ ginkgo .By ("creating a secret" )
143
+
144
+ secretTestName := "test-secret-" + string (uuid .NewUUID ())
145
+
146
+ // create a secret in the test namespace
147
+ _ , err := f .ClientSet .CoreV1 ().Secrets (f .Namespace .Name ).Create (& v1.Secret {
148
+ ObjectMeta : metav1.ObjectMeta {
149
+ Name : secretTestName ,
150
+ Labels : map [string ]string {
151
+ "testsecret-constant" : "true" ,
152
+ },
153
+ },
154
+ Data : map [string ][]byte {
155
+ "key" : []byte ("value" ),
156
+ },
157
+ Type : "Opaque" ,
158
+ })
159
+ framework .ExpectNoError (err , "failed to create secret" )
160
+
161
+ ginkgo .By ("listing secrets in all namespaces to ensure that there are more than zero" )
162
+ // list all secrets in all namespaces to ensure endpoint coverage
163
+ secretsList , err := f .ClientSet .CoreV1 ().Secrets ("" ).List (metav1.ListOptions {
164
+ LabelSelector : "testsecret-constant=true" ,
165
+ })
166
+ framework .ExpectNoError (err , "failed to list secrets" )
167
+ framework .ExpectNotEqual (len (secretsList .Items ), 0 , "no secrets found" )
168
+
169
+ foundCreatedSecret := false
170
+ var secretCreatedName string
171
+ for _ , val := range secretsList .Items {
172
+ if val .ObjectMeta .Name == secretTestName && val .ObjectMeta .Namespace == f .Namespace .Name {
173
+ foundCreatedSecret = true
174
+ secretCreatedName = val .ObjectMeta .Name
175
+ break
176
+ }
177
+ }
178
+ framework .ExpectEqual (foundCreatedSecret , true , "unable to find secret by its value" )
179
+
180
+ ginkgo .By ("patching the secret" )
181
+ // patch the secret in the test namespace
182
+ secretPatchNewData := base64 .StdEncoding .EncodeToString ([]byte ("value1" ))
183
+ secretPatch , err := json .Marshal (map [string ]interface {}{
184
+ "metadata" : map [string ]interface {}{
185
+ "labels" : map [string ]string {"testsecret" : "true" },
186
+ },
187
+ "data" : map [string ][]byte {"key" : []byte (secretPatchNewData )},
188
+ })
189
+ framework .ExpectNoError (err , "failed to marshal JSON" )
190
+ _ , err = f .ClientSet .CoreV1 ().Secrets (f .Namespace .Name ).Patch (secretCreatedName , types .StrategicMergePatchType , []byte (secretPatch ))
191
+ framework .ExpectNoError (err , "failed to patch secret" )
192
+
193
+ secret , err := f .ClientSet .CoreV1 ().Secrets (f .Namespace .Name ).Get (secretCreatedName , metav1.GetOptions {})
194
+ framework .ExpectNoError (err , "failed to get secret" )
195
+
196
+ secretDecodedstring , err := base64 .StdEncoding .DecodeString (string (secret .Data ["key" ]))
197
+ framework .ExpectNoError (err , "failed to decode secret from Base64" )
198
+
199
+ framework .ExpectEqual (string (secretDecodedstring ), "value1" , "found secret, but the data wasn't updated from the patch" )
200
+
201
+ ginkgo .By ("deleting the secret using a LabelSelector" )
202
+ err = f .ClientSet .CoreV1 ().Secrets (f .Namespace .Name ).DeleteCollection (& metav1.DeleteOptions {}, metav1.ListOptions {
203
+ LabelSelector : "testsecret=true" ,
204
+ })
205
+ framework .ExpectNoError (err , "failed to delete patched secret" )
206
+
207
+ ginkgo .By ("listing secrets in all namespaces, searching for label name and value in patch" )
208
+ // list all secrets in all namespaces
209
+ secretsList , err = f .ClientSet .CoreV1 ().Secrets ("" ).List (metav1.ListOptions {
210
+ LabelSelector : "testsecret-constant=true" ,
211
+ })
212
+ framework .ExpectNoError (err , "failed to list secrets" )
213
+
214
+ foundCreatedSecret = false
215
+ for _ , val := range secretsList .Items {
216
+ if val .ObjectMeta .Name == secretTestName && val .ObjectMeta .Namespace == f .Namespace .Name {
217
+ foundCreatedSecret = true
218
+ break
219
+ }
220
+ }
221
+ framework .ExpectEqual (foundCreatedSecret , false , "secret was not deleted successfully" )
222
+ })
137
223
})
138
224
139
225
func newEnvFromSecret (namespace , name string ) * v1.Secret {
0 commit comments