Skip to content

Commit

Permalink
fix UT
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Zhang committed Oct 24, 2024
1 parent 176f75d commit ddb950a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
76 changes: 62 additions & 14 deletions pkg/controllers/workgenerator/controller_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Licensed under the MIT license.
package workgenerator

import (
"crypto/sha256"
"encoding/json"
"fmt"
"strconv"
"time"
Expand Down Expand Up @@ -64,13 +66,14 @@ const (
)

var _ = Describe("Test Work Generator Controller", func() {

Context("Test Bound ClusterResourceBinding", func() {
var binding *placementv1beta1.ClusterResourceBinding
ignoreTypeMeta := cmpopts.IgnoreFields(metav1.TypeMeta{}, "Kind", "APIVersion")
ignoreWorkOption := cmpopts.IgnoreFields(metav1.ObjectMeta{},
"UID", "ResourceVersion", "ManagedFields", "CreationTimestamp", "Generation")

var emptyArray []string
jsonBytes, _ := json.Marshal(emptyArray)
emptyHash := fmt.Sprintf("%x", sha256.Sum256(jsonBytes))
BeforeEach(func() {
memberClusterName = "cluster-" + utils.RandStr()
testCRPName = "crp" + utils.RandStr()
Expand Down Expand Up @@ -306,6 +309,10 @@ var _ = Describe("Test Work Generator Controller", func() {
placementv1beta1.ParentBindingLabel: binding.Name,
placementv1beta1.ParentResourceSnapshotIndexLabel: "1",
},
Annotations: map[string]string{
placementv1beta1.ParentClusterResourceOverrideSnapshotHashAnnotation: emptyHash,
placementv1beta1.ParentResourceOverrideSnapshotHashAnnotation: emptyHash,
},
},
Spec: placementv1beta1.WorkSpec{
Workload: placementv1beta1.WorkloadTemplate{
Expand Down Expand Up @@ -395,6 +402,10 @@ var _ = Describe("Test Work Generator Controller", func() {
placementv1beta1.ParentBindingLabel: binding.Name,
placementv1beta1.ParentResourceSnapshotIndexLabel: "1",
},
Annotations: map[string]string{
placementv1beta1.ParentClusterResourceOverrideSnapshotHashAnnotation: emptyHash,
placementv1beta1.ParentResourceOverrideSnapshotHashAnnotation: emptyHash,
},
},
Spec: placementv1beta1.WorkSpec{
Workload: placementv1beta1.WorkloadTemplate{
Expand Down Expand Up @@ -492,6 +503,10 @@ var _ = Describe("Test Work Generator Controller", func() {
placementv1beta1.ParentBindingLabel: binding.Name,
placementv1beta1.ParentResourceSnapshotIndexLabel: "1",
},
Annotations: map[string]string{
placementv1beta1.ParentClusterResourceOverrideSnapshotHashAnnotation: emptyHash,
placementv1beta1.ParentResourceOverrideSnapshotHashAnnotation: emptyHash,
},
},
Spec: placementv1beta1.WorkSpec{
Workload: placementv1beta1.WorkloadTemplate{
Expand Down Expand Up @@ -531,6 +546,10 @@ var _ = Describe("Test Work Generator Controller", func() {
placementv1beta1.EnvelopeNameLabel: "envelop-configmap",
placementv1beta1.EnvelopeNamespaceLabel: "app",
},
Annotations: map[string]string{
placementv1beta1.ParentClusterResourceOverrideSnapshotHashAnnotation: emptyHash,
placementv1beta1.ParentResourceOverrideSnapshotHashAnnotation: emptyHash,
},
},
Spec: placementv1beta1.WorkSpec{
Workload: placementv1beta1.WorkloadTemplate{
Expand Down Expand Up @@ -609,6 +628,10 @@ var _ = Describe("Test Work Generator Controller", func() {
placementv1beta1.ParentBindingLabel: binding.Name,
placementv1beta1.ParentResourceSnapshotIndexLabel: "2",
},
Annotations: map[string]string{
placementv1beta1.ParentClusterResourceOverrideSnapshotHashAnnotation: emptyHash,
placementv1beta1.ParentResourceOverrideSnapshotHashAnnotation: emptyHash,
},
},
Spec: placementv1beta1.WorkSpec{
Workload: placementv1beta1.WorkloadTemplate{
Expand Down Expand Up @@ -647,6 +670,10 @@ var _ = Describe("Test Work Generator Controller", func() {
placementv1beta1.EnvelopeNameLabel: "envelop-configmap",
placementv1beta1.EnvelopeNamespaceLabel: "app",
},
Annotations: map[string]string{
placementv1beta1.ParentClusterResourceOverrideSnapshotHashAnnotation: emptyHash,
placementv1beta1.ParentResourceOverrideSnapshotHashAnnotation: emptyHash,
},
},
Spec: placementv1beta1.WorkSpec{
Workload: placementv1beta1.WorkloadTemplate{
Expand Down Expand Up @@ -787,6 +814,10 @@ var _ = Describe("Test Work Generator Controller", func() {
placementv1beta1.ParentResourceSnapshotIndexLabel: "2",
placementv1beta1.ParentBindingLabel: binding.Name,
},
Annotations: map[string]string{
placementv1beta1.ParentClusterResourceOverrideSnapshotHashAnnotation: emptyHash,
placementv1beta1.ParentResourceOverrideSnapshotHashAnnotation: emptyHash,
},
},
Spec: placementv1beta1.WorkSpec{
Workload: placementv1beta1.WorkloadTemplate{
Expand Down Expand Up @@ -857,6 +888,10 @@ var _ = Describe("Test Work Generator Controller", func() {
placementv1beta1.ParentResourceSnapshotIndexLabel: "2",
placementv1beta1.ParentBindingLabel: binding.Name,
},
Annotations: map[string]string{
placementv1beta1.ParentClusterResourceOverrideSnapshotHashAnnotation: emptyHash,
placementv1beta1.ParentResourceOverrideSnapshotHashAnnotation: emptyHash,
},
},
Spec: placementv1beta1.WorkSpec{
Workload: placementv1beta1.WorkloadTemplate{
Expand Down Expand Up @@ -1082,28 +1117,33 @@ var _ = Describe("Test Work Generator Controller", func() {

Context("Test Bound ClusterResourceBinding with a single resource snapshot and valid overrides", func() {
var masterSnapshot *placementv1beta1.ClusterResourceSnapshot
var roHash, croHash string

BeforeEach(func() {
masterSnapshot = generateResourceSnapshot(1, 1, 0, [][]byte{
testResourceCRD, testNameSpace, testResource,
})
Expect(k8sClient.Create(ctx, masterSnapshot)).Should(Succeed())
By(fmt.Sprintf("master resource snapshot %s created", masterSnapshot.Name))
spec := placementv1beta1.ResourceBindingSpec{
State: placementv1beta1.BindingStateBound,
ResourceSnapshotName: masterSnapshot.Name,
TargetCluster: memberClusterName,
ClusterResourceOverrideSnapshots: []string{
validClusterResourceOverrideSnapshotName,
},
ResourceOverrideSnapshots: []placementv1beta1.NamespacedName{
{
Name: validResourceOverrideSnapshotName,
Namespace: appNamespaceName,
},
crolist := []string{validClusterResourceOverrideSnapshotName}
roList := []placementv1beta1.NamespacedName{
{
Name: validResourceOverrideSnapshotName,
Namespace: appNamespaceName,
},
}
spec := placementv1beta1.ResourceBindingSpec{
State: placementv1beta1.BindingStateBound,
ResourceSnapshotName: masterSnapshot.Name,
TargetCluster: memberClusterName,
ClusterResourceOverrideSnapshots: crolist,
ResourceOverrideSnapshots: roList,
}
createClusterResourceBinding(&binding, spec)
jsonBytes, _ := json.Marshal(roList)
roHash = fmt.Sprintf("%x", sha256.Sum256(jsonBytes))
jsonBytes, _ = json.Marshal(crolist)
croHash = fmt.Sprintf("%x", sha256.Sum256(jsonBytes))
})

AfterEach(func() {
Expand Down Expand Up @@ -1146,6 +1186,10 @@ var _ = Describe("Test Work Generator Controller", func() {
placementv1beta1.ParentBindingLabel: binding.Name,
placementv1beta1.ParentResourceSnapshotIndexLabel: "1",
},
Annotations: map[string]string{
placementv1beta1.ParentClusterResourceOverrideSnapshotHashAnnotation: croHash,
placementv1beta1.ParentResourceOverrideSnapshotHashAnnotation: roHash,
},
},
Spec: placementv1beta1.WorkSpec{
Workload: placementv1beta1.WorkloadTemplate{
Expand Down Expand Up @@ -1374,6 +1418,10 @@ var _ = Describe("Test Work Generator Controller", func() {
placementv1beta1.ParentBindingLabel: binding.Name,
placementv1beta1.ParentResourceSnapshotIndexLabel: "1",
},
Annotations: map[string]string{
placementv1beta1.ParentClusterResourceOverrideSnapshotHashAnnotation: emptyHash,
placementv1beta1.ParentResourceOverrideSnapshotHashAnnotation: emptyHash,
},
},
Spec: placementv1beta1.WorkSpec{
Workload: placementv1beta1.WorkloadTemplate{
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/rollout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ var _ = Describe("placing wrapped resources using a CRP", Ordered, func() {
})
})

Context("Test a CRP place custom resource successfully, should wait to update resource on multiple member clusters", Ordered, func() {
FContext("Test a CRP place custom resource successfully, should wait to update resource on multiple member clusters", Ordered, func() {
crpName := fmt.Sprintf(crpNameTemplate, GinkgoParallelProcess())
workNamespace := appNamespace()
var wantSelectedResources []placementv1beta1.ResourceIdentifier
Expand Down

0 comments on commit ddb950a

Please sign in to comment.