From c7c66c99a6763c69fcbd56a15b42f7793bd4495a Mon Sep 17 00:00:00 2001 From: hulizhe Date: Mon, 15 Apr 2024 22:27:14 +0800 Subject: [PATCH] add e2e test for lazy propagation policy Signed-off-by: hulizhe --- test/e2e/lazy_activation_policy_test.go | 103 +++++++++++++++++++++++- test/helper/policy.go | 18 +++++ 2 files changed, 119 insertions(+), 2 deletions(-) diff --git a/test/e2e/lazy_activation_policy_test.go b/test/e2e/lazy_activation_policy_test.go index 401e353740c8..73eac0620732 100644 --- a/test/e2e/lazy_activation_policy_test.go +++ b/test/e2e/lazy_activation_policy_test.go @@ -25,6 +25,7 @@ import ( "k8s.io/apimachinery/pkg/util/rand" policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" + "github.com/karmada-io/karmada/pkg/util" "github.com/karmada-io/karmada/test/e2e/framework" testhelper "github.com/karmada-io/karmada/test/helper" ) @@ -34,17 +35,18 @@ const waitIntervalForLazyPolicyTest = 3 * time.Second // e2e test for https://github.com/karmada-io/karmada/blob/master/docs/proposals/scheduling/activation-preference/lazy-activation-preference.md#test-plan var _ = ginkgo.Describe("Lazy activation policy testing", func() { var namespace string - var deploymentName, configMapName, policyName string + var deploymentName, configMapName, policyName, policyHigherPriorityName string var originalCluster, modifiedCluster string var deployment *appsv1.Deployment var configMap *corev1.ConfigMap - var policy *policyv1alpha1.PropagationPolicy + var policy, policyHigherPriority *policyv1alpha1.PropagationPolicy ginkgo.BeforeEach(func() { namespace = testNamespace deploymentName = deploymentNamePrefix + rand.String(RandomStrLength) configMapName = deploymentName policyName = deploymentName + policyHigherPriorityName = deploymentName + "higherpriority" originalCluster = framework.ClusterNames()[0] modifiedCluster = framework.ClusterNames()[1] @@ -60,6 +62,16 @@ var _ = ginkgo.Describe("Lazy activation policy testing", func() { ClusterNames: []string{originalCluster}, }, }) + policyHigherPriority = testhelper.NewLazyExplicitPriorityPropagationPolicy(namespace, policyHigherPriorityName, []policyv1alpha1.ResourceSelector{ + { + APIVersion: deployment.APIVersion, + Kind: deployment.Kind, + Name: deploymentName, + }}, policyv1alpha1.Placement{ + ClusterAffinity: &policyv1alpha1.ClusterAffinity{ + ClusterNames: []string{modifiedCluster}, + }, + }, 2, policyv1alpha1.PreemptAlways) }) ginkgo.Context("1. Policy created before resource", func() { @@ -72,6 +84,7 @@ var _ = ginkgo.Describe("Lazy activation policy testing", func() { framework.RemovePropagationPolicyIfExist(karmadaClient, namespace, policyName) framework.RemoveDeployment(kubeClient, namespace, deploymentName) framework.WaitDeploymentDisappearOnCluster(originalCluster, namespace, deploymentName) + framework.WaitDeploymentDisappearOnCluster(modifiedCluster, namespace, deploymentName) }) }) @@ -90,6 +103,92 @@ var _ = ginkgo.Describe("Lazy activation policy testing", func() { }) }) + // Simple Case 3 (Lazy to immediate) + // refer: https://github.com/karmada-io/karmada/blob/master/docs/proposals/scheduling/activation-preference/lazy-activation-preference.md#simple-case-3-lazy-to-immediate + ginkgo.It("Simple Case 3 (Lazy to immediate)", func() { + ginkgo.By("step 1: deployment propagate success when policy created before it", func() { + waitDeploymentPresentOnCluster(originalCluster, namespace, deploymentName) + }) + + ginkgo.By("step 2: after policy updated (cluster=member2, remove lazy activationPreference field), deployment's propagation status changes", func() { + // 1. remove lazy activationPreference field + policy.Spec.ActivationPreference = "" + // 2. update policy placement with clusterAffinities + changePlacementTargetCluster(policy, modifiedCluster) + // 3. the propagation of deployment changed + framework.WaitDeploymentDisappearOnCluster(originalCluster, namespace, deploymentName) + waitDeploymentPresentOnCluster(modifiedCluster, namespace, deploymentName) + }) + }) + + ginkgo.Context("Immediate to lazy", func() { + ginkgo.BeforeEach(func() { + // remove lazy activationPreference field + policy.Spec.ActivationPreference = "" + }) + + // Simple Case 4 (Immediate to lazy) + // refer: https://github.com/karmada-io/karmada/blob/master/docs/proposals/scheduling/activation-preference/lazy-activation-preference.md#simple-case-4-immediate-to-lazy + ginkgo.It("Simple Case 4 (Immediate to lazy)", func() { + ginkgo.By("step 1: deployment propagate success", func() { + waitDeploymentPresentOnCluster(originalCluster, namespace, deploymentName) + }) + + ginkgo.By("step 2: after policy updated (cluster=member2, activationPreference=lazy), deployment's propagation status unchanged", func() { + // 1. activationPreference=lazy + policy.Spec.ActivationPreference = policyv1alpha1.LazyActivation + // 2. update policy placement with clusterAffinities + changePlacementTargetCluster(policy, modifiedCluster) + // 3. wait to distinguish whether the state will not change or have no time to change + time.Sleep(pollInterval) + waitDeploymentPresentOnCluster(originalCluster, namespace, deploymentName) + framework.WaitDeploymentDisappearOnCluster(modifiedCluster, namespace, deploymentName) + }) + + ginkgo.By("step3: resource would propagate when itself updated", func() { + // 1. update annotation of the deployment + updateDeploymentManually(deployment) + // 2. the propagation of deployment changed + framework.WaitDeploymentDisappearOnCluster(originalCluster, namespace, deploymentName) + waitDeploymentPresentOnCluster(modifiedCluster, namespace, deploymentName) + }) + }) + }) + + // Combined Case 4 (Policy preemption) + // refer: https://github.com/chaosi-zju/karmada/blob/lazy-proposal/docs/proposals/scheduling/activation-preference/lazy-activation-preference.md#combined-case-4-policy-preemption + ginkgo.It("Policy preemption", func() { + ginkgo.By("step 1: deployment propagate success when policy created before it", func() { + waitDeploymentPresentOnCluster(originalCluster, namespace, deploymentName) + }) + + ginkgo.By("step 2: create PP2 (match nginx, cluster=member2, lazy, priority=2, preemption=true)", func() { + framework.CreatePropagationPolicy(karmadaClient, policyHigherPriority) + // 1. label of policy name changed + framework.WaitDeploymentFitWith(kubeClient, namespace, deploymentName, func(deployment *appsv1.Deployment) bool { + policyNameLabel := util.GetLabelValue(deployment.GetLabels(), policyv1alpha1.PropagationPolicyNameLabel) + return policyNameLabel == policyHigherPriorityName + }) + // 2. wait to distinguish whether the state will not change or have no time to change + time.Sleep(waitIntervalForLazyPolicyTest) + // propagation unchanged + waitDeploymentPresentOnCluster(originalCluster, namespace, deploymentName) + framework.WaitDeploymentDisappearOnCluster(modifiedCluster, namespace, deploymentName) + }) + + ginkgo.By("step 3: update deployment", func() { + // 1. update annotation of the deployment + updateDeploymentManually(deployment) + // 2. the propagation of deployment changed + waitDeploymentPresentOnCluster(modifiedCluster, namespace, deploymentName) + framework.WaitDeploymentDisappearOnCluster(originalCluster, namespace, deploymentName) + }) + + ginkgo.By("step 4: clean up", func() { + framework.RemovePropagationPolicyIfExist(karmadaClient, namespace, policyHigherPriorityName) + }) + }) + ginkgo.Context("Propagate dependencies", func() { ginkgo.BeforeEach(func() { policy.Spec.PropagateDeps = true diff --git a/test/helper/policy.go b/test/helper/policy.go index 5e56901c3f8c..345ba3293708 100644 --- a/test/helper/policy.go +++ b/test/helper/policy.go @@ -70,6 +70,24 @@ func NewExplicitPriorityPropagationPolicy(ns, name string, rsSelectors []policyv } } +// NewLazyExplicitPriorityPropagationPolicy will build a PropagationPolicy object with explicit priority. +func NewLazyExplicitPriorityPropagationPolicy(ns, name string, rsSelectors []policyv1alpha1.ResourceSelector, + placement policyv1alpha1.Placement, priority int32, preemption policyv1alpha1.PreemptionBehavior) *policyv1alpha1.PropagationPolicy { + return &policyv1alpha1.PropagationPolicy{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: ns, + Name: name, + }, + Spec: policyv1alpha1.PropagationSpec{ + ActivationPreference: policyv1alpha1.LazyActivation, + ResourceSelectors: rsSelectors, + Priority: &priority, + Placement: placement, + Preemption: preemption, + }, + } +} + // NewClusterPropagationPolicy will build a ClusterPropagationPolicy object. func NewClusterPropagationPolicy(policyName string, rsSelectors []policyv1alpha1.ResourceSelector, placement policyv1alpha1.Placement) *policyv1alpha1.ClusterPropagationPolicy { return &policyv1alpha1.ClusterPropagationPolicy{