add e2e for federatedresourcequota status collection

Signed-off-by: changzhen <changzhen5@huawei.com>
This commit is contained in:
changzhen 2022-03-21 17:07:42 +08:00
parent 82ff421c57
commit decc5e1123
2 changed files with 80 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/tools/clientcmd"
@ -131,3 +132,35 @@ var _ = ginkgo.Describe("[FederatedResourceQuota] auto-provision testing", func(
})
})
})
var _ = ginkgo.Describe("[FederatedResourceQuota] status collection testing", func() {
ginkgo.Context("collect federatedResourceQuota status", func() {
frqNamespace := testNamespace
frqName := federatedResourceQuotaPrefix + rand.String(RandomStrLength)
federatedResourceQuota := helper.NewFederatedResourceQuota(frqNamespace, frqName)
ginkgo.AfterEach(func() {
framework.RemoveFederatedResourceQuota(karmadaClient, frqNamespace, frqName)
})
ginkgo.It("federatedResourceQuota status should be collect correctly", func() {
framework.CreateFederatedResourceQuota(karmadaClient, federatedResourceQuota)
framework.WaitFederatedResourceQuotaCollectStatus(karmadaClient, frqNamespace, frqName)
patch := []map[string]interface{}{
{
"op": "replace",
"path": "/spec/staticAssignments/0/hard/cpu",
"value": "2",
},
{
"op": "replace",
"path": "/spec/staticAssignments/1/hard/memory",
"value": "4Gi",
},
}
framework.UpdateFederatedResourceQuotaWithPatch(karmadaClient, frqNamespace, frqName, patch, types.JSONPatchType)
framework.WaitFederatedResourceQuotaCollectStatus(karmadaClient, frqNamespace, frqName)
})
})
})

View File

@ -2,11 +2,14 @@ package framework
import (
"context"
"encoding/json"
"fmt"
"reflect"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
karmada "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
@ -27,3 +30,47 @@ func RemoveFederatedResourceQuota(client karmada.Interface, namespace, name stri
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
})
}
// UpdateFederatedResourceQuotaWithPatch update FederatedResourceQuota with patch bytes.
func UpdateFederatedResourceQuotaWithPatch(client karmada.Interface, namespace, name string, patch []map[string]interface{}, patchType types.PatchType) {
ginkgo.By(fmt.Sprintf("Updating FederatedResourceQuota(%s/%s)", namespace, name), func() {
bytes, err := json.Marshal(patch)
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
_, err = client.PolicyV1alpha1().FederatedResourceQuotas(namespace).Patch(context.TODO(), name, patchType, bytes, metav1.PatchOptions{})
gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
})
}
// WaitFederatedResourceQuotaCollectStatus wait FederatedResourceQuota collect status successfully.
func WaitFederatedResourceQuotaCollectStatus(client karmada.Interface, namespace, name string) {
ginkgo.By("wait status collect correctly", func() {
gomega.Eventually(func(g gomega.Gomega) (bool, error) {
frq, err := client.PolicyV1alpha1().FederatedResourceQuotas(namespace).Get(context.TODO(), name, metav1.GetOptions{})
g.Expect(err).NotTo(gomega.HaveOccurred())
staticAssignments := frq.Spec.StaticAssignments
aggregatedStatus := frq.Status.AggregatedStatus
for _, assign := range staticAssignments {
matched := false
for _, aggregated := range aggregatedStatus {
if assign.ClusterName != aggregated.ClusterName {
continue
}
matched = true
if reflect.DeepEqual(assign.Hard, aggregated.Hard) {
break
} else {
return false, nil
}
}
if !matched {
return false, nil
}
}
return true, nil
}, pollTimeout, pollInterval).Should(gomega.Equal(true))
})
}