Merge pull request #120250 from liyuerich/apimachinery

e2e_apimachinery:stop using deprecated framework.ExpectError
This commit is contained in:
Kubernetes Prow Robot 2023-08-30 23:40:47 -07:00 committed by GitHub
commit 887ac275a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 29 deletions

View File

@ -21,6 +21,8 @@ import (
"strings"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/runtime/schema"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@ -149,7 +151,7 @@ var _ = SIGDescribe("CustomResourceValidationRules [Privileged:ClusterAdmin]", f
"y": int64(0),
},
}}, metav1.CreateOptions{})
framework.ExpectError(err, "validation rules not satisfied")
gomega.Expect(err).To(gomega.HaveOccurred(), "validation rules not satisfied")
expectedErrMsg := "failed rule"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg, err.Error())
@ -174,7 +176,7 @@ var _ = SIGDescribe("CustomResourceValidationRules [Privileged:ClusterAdmin]", f
}`))
crd := fixtures.NewRandomNameV1CustomResourceDefinitionWithSchema(v1.NamespaceScoped, schemaWithInvalidValidationRule, false)
_, err := fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
framework.ExpectError(err, "creating CustomResourceDefinition with a validation rule that refers to a property that do not exist")
gomega.Expect(err).To(gomega.HaveOccurred(), "creating CustomResourceDefinition with a validation rule that refers to a property that do not exist")
expectedErrMsg := "undefined field 'z'"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg, err.Error())
@ -196,7 +198,7 @@ var _ = SIGDescribe("CustomResourceValidationRules [Privileged:ClusterAdmin]", f
}`))
crd := fixtures.NewRandomNameV1CustomResourceDefinitionWithSchema(v1.NamespaceScoped, schemaWithSyntaxErrorRule, false)
_, err := fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
framework.ExpectError(err, "creating a CustomResourceDefinition with a validation rule that contains a syntax error")
gomega.Expect(err).To(gomega.HaveOccurred(), "creating a CustomResourceDefinition with a validation rule that contains a syntax error")
expectedErrMsg := "Syntax error"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expected error message to contain %q, got %q", expectedErrMsg, err.Error())
@ -229,7 +231,7 @@ var _ = SIGDescribe("CustomResourceValidationRules [Privileged:ClusterAdmin]", f
}`))
crd := fixtures.NewRandomNameV1CustomResourceDefinitionWithSchema(v1.NamespaceScoped, schemaWithExpensiveRule, false)
_, err := fixtures.CreateNewV1CustomResourceDefinitionWatchUnsafe(crd, apiExtensionClient)
framework.ExpectError(err, "creating a CustomResourceDefinition with a validation rule that exceeds the cost limit")
gomega.Expect(err).To(gomega.HaveOccurred(), "creating a CustomResourceDefinition with a validation rule that exceeds the cost limit")
expectedErrMsg := "exceeds budget"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expected error message to contain %q, got %q", expectedErrMsg, err.Error())
@ -259,7 +261,7 @@ var _ = SIGDescribe("CustomResourceValidationRules [Privileged:ClusterAdmin]", f
"largeArray": genLargeArray(725, 20),
},
}}, metav1.CreateOptions{})
framework.ExpectError(err, "custom resource creation should be prohibited by runtime cost limit")
gomega.Expect(err).To(gomega.HaveOccurred(), "custom resource creation should be prohibited by runtime cost limit")
expectedErrMsg := "call cost exceeds limit"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg, err.Error())
@ -319,7 +321,7 @@ var _ = SIGDescribe("CustomResourceValidationRules [Privileged:ClusterAdmin]", f
"num": int64(9),
},
}}, metav1.UpdateOptions{})
framework.ExpectError(err, "custom resource update should be prohibited by transition rule")
gomega.Expect(err).To(gomega.HaveOccurred(), "custom resource update should be prohibited by transition rule")
expectedErrMsg := "failed rule"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg, err.Error())

View File

@ -41,6 +41,8 @@ import (
admissionapi "k8s.io/pod-security-admission/api"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/types"
)
@ -141,7 +143,7 @@ func ensurePodsAreRemovedWhenNamespaceIsDeleted(ctx context.Context, f *framewor
ginkgo.By("Verifying there are no pods in the namespace")
_, err = f.ClientSet.CoreV1().Pods(namespace.Name).Get(ctx, pod.Name, metav1.GetOptions{})
framework.ExpectError(err, "failed to get pod %s in namespace: %s", pod.Name, namespace.Name)
gomega.Expect(err).To(gomega.HaveOccurred(), "failed to get pod %s in namespace: %s", pod.Name, namespace.Name)
}
func ensureServicesAreRemovedWhenNamespaceIsDeleted(ctx context.Context, f *framework.Framework) {
@ -198,7 +200,7 @@ func ensureServicesAreRemovedWhenNamespaceIsDeleted(ctx context.Context, f *fram
ginkgo.By("Verifying there is no service in the namespace")
_, err = f.ClientSet.CoreV1().Services(namespace.Name).Get(ctx, service.Name, metav1.GetOptions{})
framework.ExpectError(err, "failed to get service %s in namespace: %s", service.Name, namespace.Name)
gomega.Expect(err).To(gomega.HaveOccurred(), "failed to get service %s in namespace: %s", service.Name, namespace.Name)
}
// This test must run [Serial] due to the impact of running other parallel

View File

@ -53,6 +53,7 @@ import (
"k8s.io/utils/pointer"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)
const (
@ -128,7 +129,7 @@ var _ = SIGDescribe("ResourceQuota", func() {
ginkgo.By("Not allowing a LoadBalancer Service with NodePort to be created that exceeds remaining quota")
loadbalancer := newTestServiceForQuota("test-service-lb", v1.ServiceTypeLoadBalancer, true)
_, err = f.ClientSet.CoreV1().Services(f.Namespace.Name).Create(ctx, loadbalancer, metav1.CreateOptions{})
framework.ExpectError(err)
gomega.Expect(err).To(gomega.HaveOccurred())
ginkgo.By("Ensuring resource quota status captures service creation")
usedResources = v1.ResourceList{}
@ -276,7 +277,7 @@ var _ = SIGDescribe("ResourceQuota", func() {
requests[v1.ResourceMemory] = resource.MustParse("100Mi")
pod = newTestPodForQuota(f, "fail-pod", requests, v1.ResourceList{})
_, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(ctx, pod, metav1.CreateOptions{})
framework.ExpectError(err)
gomega.Expect(err).To(gomega.HaveOccurred())
ginkgo.By("Not allowing a pod to be created that exceeds remaining quota(validation on extended resources)")
requests = v1.ResourceList{}
@ -288,7 +289,7 @@ var _ = SIGDescribe("ResourceQuota", func() {
limits[v1.ResourceName(extendedResourceName)] = resource.MustParse("2")
pod = newTestPodForQuota(f, "fail-pod-for-extended-resource", requests, limits)
_, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(ctx, pod, metav1.CreateOptions{})
framework.ExpectError(err)
gomega.Expect(err).To(gomega.HaveOccurred())
ginkgo.By("Ensuring a pod cannot update its resource requirements")
// a pod cannot dynamically update its resource requirements.
@ -298,7 +299,7 @@ var _ = SIGDescribe("ResourceQuota", func() {
requests[v1.ResourceEphemeralStorage] = resource.MustParse("10Gi")
podToUpdate.Spec.Containers[0].Resources.Requests = requests
_, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Update(ctx, podToUpdate, metav1.UpdateOptions{})
framework.ExpectError(err)
gomega.Expect(err).To(gomega.HaveOccurred())
ginkgo.By("Ensuring attempts to update pod resource requirements did not change quota usage")
err = waitForResourceQuota(ctx, f.ClientSet, f.Namespace.Name, quotaName, usedResources)
@ -669,7 +670,7 @@ var _ = SIGDescribe("ResourceQuota", func() {
},
}, resourceClient, testcrd.Crd)
// since we only give one quota, this creation should fail.
framework.ExpectError(err)
gomega.Expect(err).To(gomega.HaveOccurred())
ginkgo.By("Deleting a custom resource")
err = deleteCustomResource(ctx, resourceClient, testcr.GetName())
@ -1463,7 +1464,7 @@ var _ = SIGDescribe("ResourceQuota [Feature:PodPriority]", func() {
podName2 := "testpod-pclass2-2"
pod2 := newTestPodForQuotaWithPriority(f, podName2, v1.ResourceList{}, v1.ResourceList{}, "pclass2")
_, err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Create(ctx, pod2, metav1.CreateOptions{})
framework.ExpectError(err)
gomega.Expect(err).To(gomega.HaveOccurred())
ginkgo.By("Deleting first pod")
err = f.ClientSet.CoreV1().Pods(f.Namespace.Name).Delete(ctx, pod.Name, *metav1.NewDeleteOptions(0))

View File

@ -164,7 +164,7 @@ var _ = SIGDescribe("Servers with support for Table transformation", func() {
},
}
err := c.AuthorizationV1().RESTClient().Post().Resource("selfsubjectaccessreviews").SetHeader("Accept", "application/json;as=Table;v=v1;g=meta.k8s.io").Body(sar).Do(ctx).Into(table)
framework.ExpectError(err, "failed to return error when posting self subject access review: %+v, to a backend that does not implement metadata", sar)
gomega.Expect(err).To(gomega.HaveOccurred(), "failed to return error when posting self subject access review: %+v, to a backend that does not implement metadata", sar)
framework.ExpectEqual(err.(apierrors.APIStatus).Status().Code, int32(406))
})
})

View File

@ -826,7 +826,7 @@ var _ = SIGDescribe("AdmissionWebhook [Privileged:ClusterAdmin]", func() {
validatingWebhookConfiguration := newValidatingWebhookWithMatchConditions(f, servicePort, certCtx, initalMatchConditions)
_, err := createValidatingWebhookConfiguration(ctx, f, validatingWebhookConfiguration)
framework.ExpectError(err, "create validatingwebhookconfiguration should have been denied by the api-server")
gomega.Expect(err).To(gomega.HaveOccurred(), "create validatingwebhookconfiguration should have been denied by the api-server")
expectedErrMsg := "compilation failed"
gomega.Expect(strings.Contains(err.Error(), expectedErrMsg)).To(gomega.BeTrue())
})
@ -850,7 +850,7 @@ var _ = SIGDescribe("AdmissionWebhook [Privileged:ClusterAdmin]", func() {
mutatingWebhookConfiguration := newMutatingWebhookWithMatchConditions(f, servicePort, certCtx, initalMatchConditions)
_, err := createMutatingWebhookConfiguration(ctx, f, mutatingWebhookConfiguration)
framework.ExpectError(err, "create mutatingwebhookconfiguration should have been denied by the api-server")
gomega.Expect(err).To(gomega.HaveOccurred(), "create mutatingwebhookconfiguration should have been denied by the api-server")
expectedErrMsg := "compilation failed"
gomega.Expect(strings.Contains(err.Error(), expectedErrMsg)).To(gomega.BeTrue())
})
@ -1369,7 +1369,7 @@ func testWebhook(ctx context.Context, f *framework.Framework) {
// Creating the pod, the request should be rejected
pod := nonCompliantPod(f)
_, err := client.CoreV1().Pods(f.Namespace.Name).Create(ctx, pod, metav1.CreateOptions{})
framework.ExpectError(err, "create pod %s in namespace %s should have been denied by webhook", pod.Name, f.Namespace.Name)
gomega.Expect(err).To(gomega.HaveOccurred(), "create pod %s in namespace %s should have been denied by webhook", pod.Name, f.Namespace.Name)
expectedErrMsg1 := "the pod contains unwanted container name"
if !strings.Contains(err.Error(), expectedErrMsg1) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg1, err.Error())
@ -1384,7 +1384,7 @@ func testWebhook(ctx context.Context, f *framework.Framework) {
// Creating the pod, the request should be rejected
pod = hangingPod(f)
_, err = client.CoreV1().Pods(f.Namespace.Name).Create(ctx, pod, metav1.CreateOptions{})
framework.ExpectError(err, "create pod %s in namespace %s should have caused webhook to hang", pod.Name, f.Namespace.Name)
gomega.Expect(err).To(gomega.HaveOccurred(), "create pod %s in namespace %s should have caused webhook to hang", pod.Name, f.Namespace.Name)
// ensure the error is webhook-related, not client-side
if !strings.Contains(err.Error(), "webhook") {
framework.Failf("expect error %q, got %q", "webhook", err.Error())
@ -1402,7 +1402,7 @@ func testWebhook(ctx context.Context, f *framework.Framework) {
// Creating the configmap, the request should be rejected
configmap := nonCompliantConfigMap(f)
_, err = client.CoreV1().ConfigMaps(f.Namespace.Name).Create(ctx, configmap, metav1.CreateOptions{})
framework.ExpectError(err, "create configmap %s in namespace %s should have been denied by the webhook", configmap.Name, f.Namespace.Name)
gomega.Expect(err).To(gomega.HaveOccurred(), "create configmap %s in namespace %s should have been denied by the webhook", configmap.Name, f.Namespace.Name)
expectedErrMsg := "the configmap contains unwanted key and value"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg, err.Error())
@ -1429,7 +1429,7 @@ func testWebhook(ctx context.Context, f *framework.Framework) {
cm.Data["webhook-e2e-test"] = "webhook-disallow"
}
_, err = updateConfigMap(ctx, client, f.Namespace.Name, allowedConfigMapName, toNonCompliantFn)
framework.ExpectError(err, "update (PUT) admitted configmap %s in namespace %s to a non-compliant one should be rejected by webhook", allowedConfigMapName, f.Namespace.Name)
gomega.Expect(err).To(gomega.HaveOccurred(), "update (PUT) admitted configmap %s in namespace %s to a non-compliant one should be rejected by webhook", allowedConfigMapName, f.Namespace.Name)
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg, err.Error())
}
@ -1437,7 +1437,7 @@ func testWebhook(ctx context.Context, f *framework.Framework) {
ginkgo.By("update (PATCH) the admitted configmap to a non-compliant one should be rejected by the webhook")
patch := nonCompliantConfigMapPatch()
_, err = client.CoreV1().ConfigMaps(f.Namespace.Name).Patch(ctx, allowedConfigMapName, types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{})
framework.ExpectError(err, "update admitted configmap %s in namespace %s by strategic merge patch to a non-compliant one should be rejected by webhook. Patch: %+v", allowedConfigMapName, f.Namespace.Name, patch)
gomega.Expect(err).To(gomega.HaveOccurred(), "update admitted configmap %s in namespace %s by strategic merge patch to a non-compliant one should be rejected by webhook. Patch: %+v", allowedConfigMapName, f.Namespace.Name, patch)
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg, err.Error())
}
@ -1470,7 +1470,7 @@ func testAttachingPodWebhook(ctx context.Context, f *framework.Framework) {
timer := time.NewTimer(30 * time.Second)
defer timer.Stop()
_, err = e2ekubectl.NewKubectlCommand(f.Namespace.Name, "attach", fmt.Sprintf("--namespace=%v", f.Namespace.Name), pod.Name, "-i", "-c=container1").WithTimeout(timer.C).Exec()
framework.ExpectError(err, "'kubectl attach' the pod, should be denied by the webhook")
gomega.Expect(err).To(gomega.HaveOccurred(), "'kubectl attach' the pod, should be denied by the webhook")
if e, a := "attaching to pod 'to-be-attached-pod' is not allowed", err.Error(); !strings.Contains(a, e) {
framework.Failf("unexpected 'kubectl attach' error message. expected to contain %q, got %q", e, a)
}
@ -1562,7 +1562,7 @@ func testFailClosedWebhook(ctx context.Context, f *framework.Framework) {
},
}
_, err = client.CoreV1().ConfigMaps(failNamespaceName).Create(ctx, configmap, metav1.CreateOptions{})
framework.ExpectError(err, "create configmap in namespace: %s should be unconditionally rejected by the webhook", failNamespaceName)
gomega.Expect(err).To(gomega.HaveOccurred(), "create configmap in namespace: %s should be unconditionally rejected by the webhook", failNamespaceName)
if !apierrors.IsInternalError(err) {
framework.Failf("expect an internal error, got %#v", err)
}
@ -2088,7 +2088,7 @@ func testCustomResourceWebhook(ctx context.Context, f *framework.Framework, crd
},
}
_, err := customResourceClient.Create(ctx, crInstance, metav1.CreateOptions{})
framework.ExpectError(err, "create custom resource %s in namespace %s should be denied by webhook", crInstanceName, f.Namespace.Name)
gomega.Expect(err).To(gomega.HaveOccurred(), "create custom resource %s in namespace %s should be denied by webhook", crInstanceName, f.Namespace.Name)
expectedErrMsg := "the custom resource contains unwanted data"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg, err.Error())
@ -2123,7 +2123,7 @@ func testBlockingCustomResourceUpdateDeletion(ctx context.Context, f *framework.
data["webhook-e2e-test"] = "webhook-disallow"
}
_, err = updateCustomResource(ctx, customResourceClient, f.Namespace.Name, crInstanceName, toNonCompliantFn)
framework.ExpectError(err, "updating custom resource %s in namespace: %s should be denied", crInstanceName, f.Namespace.Name)
gomega.Expect(err).To(gomega.HaveOccurred(), "updating custom resource %s in namespace: %s should be denied", crInstanceName, f.Namespace.Name)
expectedErrMsg := "the custom resource contains unwanted data"
if !strings.Contains(err.Error(), expectedErrMsg) {
@ -2132,7 +2132,7 @@ func testBlockingCustomResourceUpdateDeletion(ctx context.Context, f *framework.
ginkgo.By("Deleting the custom resource should be denied")
err = customResourceClient.Delete(ctx, crInstanceName, metav1.DeleteOptions{})
framework.ExpectError(err, "deleting custom resource %s in namespace: %s should be denied", crInstanceName, f.Namespace.Name)
gomega.Expect(err).To(gomega.HaveOccurred(), "deleting custom resource %s in namespace: %s should be denied", crInstanceName, f.Namespace.Name)
expectedErrMsg1 := "the custom resource cannot be deleted because it contains unwanted key and value"
if !strings.Contains(err.Error(), expectedErrMsg1) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg1, err.Error())
@ -2356,7 +2356,7 @@ func testCRDDenyWebhook(ctx context.Context, f *framework.Framework) {
// create CRD
_, err = apiExtensionClient.ApiextensionsV1().CustomResourceDefinitions().Create(ctx, crd, metav1.CreateOptions{})
framework.ExpectError(err, "create custom resource definition %s should be denied by webhook", crd.Name)
gomega.Expect(err).To(gomega.HaveOccurred(), "create custom resource definition %s should be denied by webhook", crd.Name)
expectedErrMsg := "the crd contains unwanted label"
if !strings.Contains(err.Error(), expectedErrMsg) {
framework.Failf("expect error contains %q, got %q", expectedErrMsg, err.Error())
@ -2444,7 +2444,7 @@ func testSlowWebhookTimeoutFailEarly(ctx context.Context, f *framework.Framework
client := f.ClientSet
name := "e2e-test-slow-webhook-configmap"
_, err := client.CoreV1().ConfigMaps(f.Namespace.Name).Create(ctx, &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: name}}, metav1.CreateOptions{})
framework.ExpectError(err, "create configmap in namespace %s should have timed-out reaching slow webhook", f.Namespace.Name)
gomega.Expect(err).To(gomega.HaveOccurred(), "create configmap in namespace %s should have timed-out reaching slow webhook", f.Namespace.Name)
// http timeout message: context deadline exceeded
// dial timeout message: dial tcp {address}: i/o timeout
isTimeoutError := strings.Contains(err.Error(), `context deadline exceeded`) || strings.Contains(err.Error(), `timeout`)