Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create test to patch a secret #86857

Merged
merged 6 commits into from Jan 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 86 additions & 0 deletions test/e2e/common/secrets.go
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package common

import (
"encoding/json"
"fmt"

"k8s.io/api/core/v1"
Expand All @@ -25,7 +26,9 @@ import (
"k8s.io/kubernetes/test/e2e/framework"
imageutils "k8s.io/kubernetes/test/utils/image"

"encoding/base64"
"github.com/onsi/ginkgo"
"k8s.io/apimachinery/pkg/types"
)

var _ = ginkgo.Describe("[sig-api-machinery] Secrets", func() {
Expand Down Expand Up @@ -134,6 +137,89 @@ var _ = ginkgo.Describe("[sig-api-machinery] Secrets", func() {
secret, err := createEmptyKeySecretForTest(f)
framework.ExpectError(err, "created secret %q with empty key in namespace %q", secret.Name, f.Namespace.Name)
})

ginkgo.It("should patch a secret", func() {
ginkgo.By("creating a secret")

secretTestName := "test-secret-" + string(uuid.NewUUID())

// create a secret in the test namespace
_, err := f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Create(&v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretTestName,
Labels: map[string]string{
"testsecret-constant": "true",
},
},
Data: map[string][]byte{
"key": []byte("value"),
},
Type: "Opaque",
})
framework.ExpectNoError(err, "failed to create secret")

ginkgo.By("listing secrets in all namespaces to ensure that there are more than zero")
// list all secrets in all namespaces to ensure endpoint coverage
secretsList, err := f.ClientSet.CoreV1().Secrets("").List(metav1.ListOptions{
LabelSelector: "testsecret-constant=true",
})
framework.ExpectNoError(err, "failed to list secrets")
framework.ExpectNotEqual(len(secretsList.Items), 0, "no secrets found")

foundCreatedSecret := false
var secretCreatedName string
for _, val := range secretsList.Items {
if val.ObjectMeta.Name == secretTestName && val.ObjectMeta.Namespace == f.Namespace.Name {
foundCreatedSecret = true
secretCreatedName = val.ObjectMeta.Name
break
}
}
framework.ExpectEqual(foundCreatedSecret, true, "unable to find secret by its value")

ginkgo.By("patching the secret")
// patch the secret in the test namespace
secretPatchNewData := base64.StdEncoding.EncodeToString([]byte("value1"))
secretPatch, err := json.Marshal(map[string]interface{}{
"metadata": map[string]interface{}{
"labels": map[string]string{"testsecret": "true"},
},
"data": map[string][]byte{"key": []byte(secretPatchNewData)},
})
framework.ExpectNoError(err, "failed to marshal JSON")
_, err = f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Patch(secretCreatedName, types.StrategicMergePatchType, []byte(secretPatch))
framework.ExpectNoError(err, "failed to patch secret")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO once we've patched the secret, we should get the secret back and confirm the patch has taken effect. As written this test could be satisfied by an endpoint that does nothing but return 200 OK. So here I would once again locate the secret by name/namespace (might want to pull out into a helper func), and then verify its data

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with this idea, I've updated the test to include the check in 07e3c46.


secret, err := f.ClientSet.CoreV1().Secrets(f.Namespace.Name).Get(secretCreatedName, metav1.GetOptions{})
framework.ExpectNoError(err, "failed to get secret")

secretDecodedstring, err := base64.StdEncoding.DecodeString(string(secret.Data["key"]))
framework.ExpectNoError(err, "failed to decode secret from Base64")

framework.ExpectEqual(string(secretDecodedstring), "value1", "found secret, but the data wasn't updated from the patch")

ginkgo.By("deleting the secret using a LabelSelector")
err = f.ClientSet.CoreV1().Secrets(f.Namespace.Name).DeleteCollection(&metav1.DeleteOptions{}, metav1.ListOptions{
LabelSelector: "testsecret=true",
})
framework.ExpectNoError(err, "failed to delete patched secret")

ginkgo.By("listing secrets in all namespaces, searching for label name and value in patch")
// list all secrets in all namespaces
secretsList, err = f.ClientSet.CoreV1().Secrets("").List(metav1.ListOptions{
LabelSelector: "testsecret-constant=true",
})
framework.ExpectNoError(err, "failed to list secrets")

foundCreatedSecret = false
for _, val := range secretsList.Items {
if val.ObjectMeta.Name == secretTestName && val.ObjectMeta.Namespace == f.Namespace.Name {
foundCreatedSecret = true
break
}
}
framework.ExpectEqual(foundCreatedSecret, false, "secret was not deleted successfully")
})
})

func newEnvFromSecret(namespace, name string) *v1.Secret {
Expand Down