Skip to content

Commit

Permalink
add e2e test for lazy propagation policy
Browse files Browse the repository at this point in the history
Signed-off-by: hulizhe <[email protected]>
  • Loading branch information
hulizhe committed Apr 10, 2024
1 parent 4898c0f commit 214171e
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 2 deletions.
131 changes: 129 additions & 2 deletions test/e2e/lazy_activation_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -34,17 +35,19 @@ 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, immediatePolicyName, policyHigherPriorityName string
var originalCluster, modifiedCluster string
var deployment *appsv1.Deployment
var configMap *corev1.ConfigMap
var policy *policyv1alpha1.PropagationPolicy
var policy, immediatePolicy, policyHigherPriority *policyv1alpha1.PropagationPolicy

ginkgo.BeforeEach(func() {
namespace = testNamespace
deploymentName = deploymentNamePrefix + rand.String(RandomStrLength)
configMapName = deploymentName
policyName = deploymentName
immediatePolicyName = deploymentName + "immediate"
policyHigherPriorityName = deploymentName + "higherpriority"
originalCluster = framework.ClusterNames()[0]
modifiedCluster = framework.ClusterNames()[1]

Expand All @@ -60,6 +63,26 @@ var _ = ginkgo.Describe("Lazy activation policy testing", func() {
ClusterNames: []string{originalCluster},
},
})
immediatePolicy = testhelper.NewPropagationPolicy(namespace, immediatePolicyName, []policyv1alpha1.ResourceSelector{
{
APIVersion: deployment.APIVersion,
Kind: deployment.Kind,
Name: deploymentName,
}}, policyv1alpha1.Placement{
ClusterAffinity: &policyv1alpha1.ClusterAffinity{
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() {
Expand All @@ -72,6 +95,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)
})
})

Expand All @@ -90,6 +114,64 @@ 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. update policy placement with clusterAffinities
// 2. remove lazy activationPreference field
policy.Spec.Placement.ClusterAffinity = nil
policy.Spec.Placement.ClusterAffinities = []policyv1alpha1.ClusterAffinityTerm{{
AffinityName: "group1",
ClusterAffinity: policyv1alpha1.ClusterAffinity{ClusterNames: []string{modifiedCluster}},
}}
policy.Spec.ActivationPreference = ""
framework.UpdatePropagationPolicyWithSpec(karmadaClient, policy.Namespace, policy.Name, policy.Spec)
// wait to distinguish whether the state will not change or have no time to change
time.Sleep(waitIntervalForLazyPolicyTest)
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)
// wait to distinguish whether the state will not change or have no time to change
time.Sleep(waitIntervalForLazyPolicyTest)
// 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
})
// propagation unchanged
waitDeploymentPresentOnCluster(originalCluster, namespace, deploymentName)
framework.WaitDeploymentDisappearOnCluster(modifiedCluster, namespace, deploymentName)
})

ginkgo.By("step 3: update deployment", func() {
updateDeploymentManually(deployment)
// wait to distinguish whether the state will not change or have no time to change
time.Sleep(waitIntervalForLazyPolicyTest)
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
Expand Down Expand Up @@ -197,6 +279,51 @@ var _ = ginkgo.Describe("Lazy activation policy testing", func() {
})
})
})

ginkgo.Context("3. Immediate to lazy activation policy testing", func() {
ginkgo.JustBeforeEach(func() {
framework.CreatePropagationPolicy(karmadaClient, immediatePolicy)
waitPropagatePolicyReconciled(namespace, immediatePolicyName)
framework.CreateDeployment(kubeClient, deployment)

ginkgo.DeferCleanup(func() {
framework.RemovePropagationPolicyIfExist(karmadaClient, namespace, immediatePolicyName)
framework.RemoveDeployment(kubeClient, namespace, deploymentName)
framework.WaitDeploymentDisappearOnCluster(originalCluster, namespace, deploymentName)
framework.WaitDeploymentDisappearOnCluster(modifiedCluster, namespace, deploymentName)
})
})

// 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. update policy placement with clusterAffinities
// 2. activationPreference=lazy
immediatePolicy.Spec.Placement.ClusterAffinity = nil
immediatePolicy.Spec.Placement.ClusterAffinities = []policyv1alpha1.ClusterAffinityTerm{{
AffinityName: "group1",
ClusterAffinity: policyv1alpha1.ClusterAffinity{ClusterNames: []string{modifiedCluster}},
}}
immediatePolicy.Spec.ActivationPreference = policyv1alpha1.LazyActivation
framework.UpdatePropagationPolicyWithSpec(karmadaClient, immediatePolicy.Namespace, immediatePolicy.Name, immediatePolicy.Spec)
// 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() {
updateDeploymentManually(deployment)
framework.WaitDeploymentDisappearOnCluster(originalCluster, namespace, deploymentName)
waitDeploymentPresentOnCluster(modifiedCluster, namespace, deploymentName)
})
})
})
})

// updateDeploymentManually manually update deployment
Expand Down
18 changes: 18 additions & 0 deletions test/helper/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 214171e

Please sign in to comment.