From a8c21e1d0fbd2e1240e97e0ecd494c19718cebb3 Mon Sep 17 00:00:00 2001 From: charlie Date: Mon, 4 Sep 2023 21:31:32 +0800 Subject: [PATCH] test: add unit test Signed-off-by: charlie --- .../internal/utils/controller_utils.go | 81 ------------------- .../internal/utils/controller_utils_test.go | 30 +++++++ 2 files changed, 30 insertions(+), 81 deletions(-) create mode 100644 module-controller/internal/utils/controller_utils_test.go diff --git a/module-controller/internal/utils/controller_utils.go b/module-controller/internal/utils/controller_utils.go index 4df708c05..f4290abe0 100644 --- a/module-controller/internal/utils/controller_utils.go +++ b/module-controller/internal/utils/controller_utils.go @@ -2,7 +2,6 @@ package utils import ( "fmt" - "sort" "strings" "time" @@ -92,86 +91,6 @@ func GetNextReconcileTime(currentTime time.Time) time.Duration { return nextDuration } -type PodWithModuleCount struct { - Pod *corev1.Pod - Count int -} - -func ScaleUp(pods *corev1.PodList, existedModuleList *moduledeploymentv1alpha1.ModuleList, - delta int, limit int, strategy moduledeploymentv1alpha1.ModuleSchedulingStrategy) []corev1.Pod { - - // get allocated pod - usedPodNames := make(map[string]bool) - for _, module := range existedModuleList.Items { - usedPodNames[module.Labels[label.BaseInstanceNameLabel]] = true - } - - // allocate pod - var toAllocatePod []PodWithModuleCount - - for i := 0; i < len(pods.Items); i++ { - pod := pods.Items[i] - if _, ok := usedPodNames[pod.Name]; !ok { - if count := GetModuleCountFromPod(&pod); count < limit { - toAllocatePod = append(toAllocatePod, PodWithModuleCount{ - Pod: &pod, - Count: count, - }) - } - } - } - - if strategy == moduledeploymentv1alpha1.Scatter { - sort.Slice(toAllocatePod, func(i, j int) bool { - return toAllocatePod[i].Count < toAllocatePod[j].Count - }) - } else if strategy == moduledeploymentv1alpha1.Stacking { - sort.Slice(toAllocatePod, func(i, j int) bool { - return toAllocatePod[i].Count > toAllocatePod[j].Count - }) - } - - var res []corev1.Pod - for i := 0; i < len(toAllocatePod) && i < delta; i++ { - res = append(res, *toAllocatePod[i].Pod) - } - return res -} - -func ScaleDown(pods *corev1.PodList, existedModuleList *moduledeploymentv1alpha1.ModuleList, delta int, strategy moduledeploymentv1alpha1.ModuleSchedulingStrategy) []moduledeploymentv1alpha1.Module { - usedPodNames := make(map[string]int) - for idx, module := range existedModuleList.Items { - usedPodNames[module.Labels[label.BaseInstanceNameLabel]] = idx - } - - var filteredPods []PodWithModuleCount - for i := 0; i < len(pods.Items); i++ { - pod := pods.Items[i] - if _, ok := usedPodNames[pod.Name]; !ok { - filteredPods = append(filteredPods, PodWithModuleCount{ - Pod: &pod, - Count: GetModuleCountFromPod(&pod), - }) - } - } - - if strategy == moduledeploymentv1alpha1.Scatter { - sort.Slice(filteredPods, func(i, j int) bool { - return filteredPods[i].Count > filteredPods[j].Count - }) - } else if strategy == moduledeploymentv1alpha1.Stacking { - sort.Slice(filteredPods, func(i, j int) bool { - return filteredPods[i].Count < filteredPods[j].Count - }) - } - - var res []moduledeploymentv1alpha1.Module - for i := 0; i < len(filteredPods) && i < delta; i++ { - res = append(res, existedModuleList.Items[usedPodNames[filteredPods[i].Pod.Name]]) - } - return res -} - func GetModuleCountFromPod(pod *corev1.Pod) (count int) { for k, _ := range pod.Labels { if strings.HasPrefix(k, label.ModuleNameLabel) { diff --git a/module-controller/internal/utils/controller_utils_test.go b/module-controller/internal/utils/controller_utils_test.go new file mode 100644 index 000000000..e32afc1d2 --- /dev/null +++ b/module-controller/internal/utils/controller_utils_test.go @@ -0,0 +1,30 @@ +package utils + +import ( + "fmt" + "strconv" + "testing" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + "github.com/sofastack/sofa-serverless/internal/constants/label" +) + +func TestGetModuleCountFromPod(t *testing.T) { + pod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Labels: map[string]string{}, + }, + } + count := 5 + + for i := 0; i < count; i++ { + pod.Labels[fmt.Sprintf("%s-%s", label.ModuleNameLabel, "module-"+strconv.Itoa(i))] = "1.0.0" + } + + actual := GetModuleCountFromPod(pod) + if count != actual { + t.Errorf("the expected count is %v, but got %v", count, actual) + } +}