Skip to content

Commit

Permalink
add e2e for resource interpreter customization
Browse files Browse the repository at this point in the history
Signed-off-by: changzhen <changzhen5@huawei.com>
  • Loading branch information
XiShanYongYe-Chang committed Nov 18, 2022
1 parent 594ad9f commit 4309b9d
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/e2e/framework/resourceinterpretercustomization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package framework

import (
"context"
"fmt"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
karmada "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
)

// CreateResourceInterpreterCustomization create ResourceInterpreterCustomization with karmada client.
func CreateResourceInterpreterCustomization(client karmada.Interface, customization *configv1alpha1.ResourceInterpreterCustomization) {
ginkgo.By(fmt.Sprintf("Creating ResourceInterpreterCustomization(%s)", customization.Name), func() {
_, err := client.ConfigV1alpha1().ResourceInterpreterCustomizations().Create(context.TODO(), customization, metav1.CreateOptions{})
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
})
}

// DeleteResourceInterpreterCustomization delete ResourceInterpreterCustomization with karmada client.
func DeleteResourceInterpreterCustomization(client karmada.Interface, name string) {
ginkgo.By(fmt.Sprintf("Deleting ResourceInterpreterCustomization(%s)", name), func() {
err := client.ConfigV1alpha1().ResourceInterpreterCustomizations().Delete(context.TODO(), name, metav1.DeleteOptions{})
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
})
}
68 changes: 68 additions & 0 deletions test/e2e/resourceinterpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"k8s.io/utils/pointer"

workloadv1alpha1 "github.com/karmada-io/karmada/examples/customresourceinterpreter/apis/workload/v1alpha1"
configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/util/names"
Expand Down Expand Up @@ -272,3 +273,70 @@ var _ = ginkgo.Describe("Resource interpreter webhook testing", func() {
})
})
})

var _ = framework.SerialDescribe("Resource interpreter customization testing", func() {
var resourceInterpreterCustomization *configv1alpha1.ResourceInterpreterCustomization

var policyNamespace, policyName string
var workloadNamespace, workloadName string
var workload *workloadv1alpha1.Workload
var policy *policyv1alpha1.PropagationPolicy
// We only need to test any one of the member clusters.
var targetCluster string

ginkgo.BeforeEach(func() {
resourceInterpreterCustomization = testhelper.NewResourceInterpreterCustomization("interpreter-customization" + rand.String(RandomStrLength))

policyNamespace = testNamespace
policyName = workloadNamePrefix + rand.String(RandomStrLength)
workloadNamespace = testNamespace
workloadName = policyName
targetCluster = framework.ClusterNames()[rand.Intn(len(framework.ClusterNames()))]

workload = testhelper.NewWorkload(workloadNamespace, workloadName)
policy = testhelper.NewPropagationPolicy(policyNamespace, policyName, []policyv1alpha1.ResourceSelector{
{
APIVersion: workload.APIVersion,
Kind: workload.Kind,
Name: workload.Name,
},
}, policyv1alpha1.Placement{
ClusterAffinity: &policyv1alpha1.ClusterAffinity{
ClusterNames: []string{targetCluster},
},
})
})

ginkgo.JustBeforeEach(func() {
framework.CreateResourceInterpreterCustomization(karmadaClient, resourceInterpreterCustomization)
// Wait for resource interpreter informer synced.
time.Sleep(time.Second * 3)

framework.CreatePropagationPolicy(karmadaClient, policy)
framework.CreateWorkload(dynamicClient, workload)
ginkgo.DeferCleanup(func() {
framework.RemoveWorkload(dynamicClient, workload.Namespace, workload.Name)
framework.RemovePropagationPolicy(karmadaClient, policy.Namespace, policy.Name)
framework.DeleteResourceInterpreterCustomization(karmadaClient, resourceInterpreterCustomization.Name)
})
})

ginkgo.Context("InterpreterOperation InterpretReplica testing", func() {
ginkgo.It("InterpretReplica testing", func() {
ginkgo.By("check if workload's replica is interpreted", func() {
resourceBindingName := names.GenerateBindingName(workload.Kind, workload.Name)
// Just for the current test case to distinguish the build-in logic.
expectedReplicas := *workload.Spec.Replicas + 1

gomega.Eventually(func(g gomega.Gomega) (int32, error) {
resourceBinding, err := karmadaClient.WorkV1alpha2().ResourceBindings(workload.Namespace).Get(context.TODO(), resourceBindingName, metav1.GetOptions{})
g.Expect(err).NotTo(gomega.HaveOccurred())

klog.Infof(fmt.Sprintf("ResourceBinding(%s/%s)'s replicas is %d, expected: %d.",
resourceBinding.Namespace, resourceBinding.Name, resourceBinding.Spec.Replicas, expectedReplicas))
return resourceBinding.Spec.Replicas, nil
}, pollTimeout, pollInterval).Should(gomega.Equal(expectedReplicas))
})
})
})
})
38 changes: 38 additions & 0 deletions test/helper/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package helper

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

configv1alpha1 "github.com/karmada-io/karmada/pkg/apis/config/v1alpha1"
)

// NewResourceInterpreterCustomization will build a ResourceInterpreterCustomization object.
func NewResourceInterpreterCustomization(name string) *configv1alpha1.ResourceInterpreterCustomization {
return &configv1alpha1.ResourceInterpreterCustomization{
ObjectMeta: metav1.ObjectMeta{Name: name},
Spec: configv1alpha1.ResourceInterpreterCustomizationSpec{
Target: configv1alpha1.CustomizationTarget{
APIVersion: "apps/v1",
Kind: "Deployment",
},
Customizations: configv1alpha1.CustomizationRules{
ReplicaResource: &configv1alpha1.ReplicaResourceRequirement{
LuaScript: `
function GetReplicas(desiredObj)
nodeClaim = {}
resourceRequest = {}
result = {}
replica = desiredObj.spec.replicas + 1
result.resourceRequest = desiredObj.spec.template.spec.containers[1].resources.limits
nodeClaim.nodeSelector = desiredObj.spec.template.spec.nodeSelector
nodeClaim.tolerations = desiredObj.spec.template.spec.tolerations
result.nodeClaim = {}
result.nodeClaim = nil
return replica, {}
end`,
},
},
},
}
}

0 comments on commit 4309b9d

Please sign in to comment.