Skip to content

Commit

Permalink
utils: improve unittest coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Cyclinder Kuo <[email protected]>
  • Loading branch information
cyclinder committed Dec 31, 2024
1 parent 415359e commit 5348a3e
Show file tree
Hide file tree
Showing 9 changed files with 402 additions and 218 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ unittest-tests: check_test_label
@echo "run unittest-tests"
$(QUIET) $(ROOT_DIR)/tools/scripts/ginkgo.sh \
--cover --coverprofile=./coverage.out --covermode set \
--json-report unittestreport.json --label-filter $(E2E_GINKGO_UTLABELS) \
--json-report unittestreport.json \
-randomize-suites -randomize-all --keep-going --timeout=1h -p \
-vv -r $(ROOT_DIR)/pkg $(ROOT_DIR)/cmd
$(QUIET) go tool cover -html=./coverage.out -o coverage-all.html
Expand Down
102 changes: 2 additions & 100 deletions pkg/coordinatormanager/coordinator_informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"net"
"reflect"
"regexp"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -473,7 +472,7 @@ func (cc *CoordinatorController) updatePodAndServerCIDR(ctx context.Context, log

if err == nil {
logger.Sugar().Info("Trying to fetch the ClusterCIDR from kube-system/kubeadm-config")
k8sPodCIDR, k8sServiceCIDR, err = ExtractK8sCIDRFromKubeadmConfigMap(&cm)
k8sPodCIDR, k8sServiceCIDR, err = utils.ExtractK8sCIDRFromKubeadmConfigMap(&cm)
if err == nil {
// Success to get ClusterCIDR from kubeadm-config
logger.Sugar().Infof("Success get CIDR from kubeadm-config: PodCIDR=%v, ServiceCIDR=%v", k8sPodCIDR, k8sServiceCIDR)
Expand Down Expand Up @@ -507,7 +506,7 @@ func (cc *CoordinatorController) updatePodAndServerCIDR(ctx context.Context, log
return coordCopy
}

k8sPodCIDR, k8sServiceCIDR = ExtractK8sCIDRFromKCMPod(&podList.Items[0])
k8sPodCIDR, k8sServiceCIDR = utils.ExtractK8sCIDRFromKCMPod(&podList.Items[0])
logger.Sugar().Infof("kube-controller-manager k8sPodCIDR %v, k8sServiceCIDR %v", k8sPodCIDR, k8sServiceCIDR)
}

Expand Down Expand Up @@ -778,103 +777,6 @@ func (cc *CoordinatorController) updateServiceCIDR(logger *zap.Logger, coordCopy
return nil
}

func ExtractK8sCIDRFromKubeadmConfigMap(cm *corev1.ConfigMap) ([]string, []string, error) {
if cm == nil {
return nil, nil, fmt.Errorf("kubeadm configmap is unexpected to nil")
}
var podCIDR, serviceCIDR []string

clusterConfig, exists := cm.Data["ClusterConfiguration"]
if !exists {
return podCIDR, serviceCIDR, fmt.Errorf("unable to get kubeadm configmap ClusterConfiguration")
}

podReg := regexp.MustCompile(`podSubnet:\s*(\S+)`)
serviceReg := regexp.MustCompile(`serviceSubnet:\s*(\S+)`)

podSubnets := podReg.FindStringSubmatch(clusterConfig)
serviceSubnets := serviceReg.FindStringSubmatch(clusterConfig)

if len(podSubnets) > 1 {
for _, cidr := range strings.Split(podSubnets[1], ",") {
cidr = strings.TrimSpace(cidr)
_, _, err := net.ParseCIDR(cidr)
if err != nil {
continue
}
podCIDR = append(podCIDR, cidr)
}
}

if len(serviceSubnets) > 1 {
for _, cidr := range strings.Split(serviceSubnets[1], ",") {
cidr = strings.TrimSpace(cidr)
_, _, err := net.ParseCIDR(cidr)
if err != nil {
continue
}
serviceCIDR = append(serviceCIDR, cidr)
}
}

return podCIDR, serviceCIDR, nil
}

func ExtractK8sCIDRFromKCMPod(kcm *corev1.Pod) ([]string, []string) {
var podCIDR, serviceCIDR []string

podReg := regexp.MustCompile(`--cluster-cidr=(.*)`)
serviceReg := regexp.MustCompile(`--service-cluster-ip-range=(.*)`)

var podSubnets, serviceSubnets []string
findSubnets := func(l string) {
if len(podSubnets) == 0 {
podSubnets = podReg.FindStringSubmatch(l)
}
if len(serviceSubnets) == 0 {
serviceSubnets = serviceReg.FindStringSubmatch(l)
}
}

for _, l := range kcm.Spec.Containers[0].Command {
findSubnets(l)
if len(podSubnets) != 0 && len(serviceSubnets) != 0 {
break
}
}

if len(podSubnets) == 0 || len(serviceSubnets) == 0 {
for _, l := range kcm.Spec.Containers[0].Args {
findSubnets(l)
if len(podSubnets) != 0 && len(serviceSubnets) != 0 {
break
}
}
}

if len(podSubnets) != 0 {
for _, cidr := range strings.Split(podSubnets[1], ",") {
_, _, err := net.ParseCIDR(cidr)
if err != nil {
continue
}
podCIDR = append(podCIDR, cidr)
}
}

if len(serviceSubnets) != 0 {
for _, cidr := range strings.Split(serviceSubnets[1], ",") {
_, _, err := net.ParseCIDR(cidr)
if err != nil {
continue
}
serviceCIDR = append(serviceCIDR, cidr)
}
}

return podCIDR, serviceCIDR
}

func fetchType(cniDir string) (string, error) {
defaultCniName, err := utils.GetDefaultCniName(cniDir)
if err != nil {
Expand Down
54 changes: 0 additions & 54 deletions pkg/coordinatormanager/coordinator_informer_test.go

This file was deleted.

61 changes: 0 additions & 61 deletions pkg/coordinatormanager/coordinatormanager_suite_test.go

This file was deleted.

77 changes: 77 additions & 0 deletions pkg/ippoolmanager/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2024 Authors of spidernet-io
// SPDX-License-Identifier: Apache-2.0
package ippoolmanager

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("IPPoolManagerConfig", Label("ippool_manager_test"), func() {
var config IPPoolManagerConfig

Describe("setDefaultsForIPPoolManagerConfig", func() {
Context("when MaxAllocatedIPs is nil", func() {
BeforeEach(func() {
config = IPPoolManagerConfig{
MaxAllocatedIPs: nil,
EnableKubevirtStaticIP: false,
}
})

It("should set MaxAllocatedIPs to default value", func() {
result := setDefaultsForIPPoolManagerConfig(config)
Expect(result.MaxAllocatedIPs).NotTo(BeNil())
Expect(*result.MaxAllocatedIPs).To(Equal(defaultMaxAllocatedIPs))
})
})

Context("when MaxAllocatedIPs is set", func() {
BeforeEach(func() {
maxIPs := 3000
config = IPPoolManagerConfig{
MaxAllocatedIPs: &maxIPs,
EnableKubevirtStaticIP: true,
}
})

It("should retain the provided MaxAllocatedIPs value", func() {
result := setDefaultsForIPPoolManagerConfig(config)
Expect(result.MaxAllocatedIPs).NotTo(BeNil())
Expect(*result.MaxAllocatedIPs).To(Equal(3000))
})
})

Context("when EnableKubevirtStaticIP is true", func() {
BeforeEach(func() {
config = IPPoolManagerConfig{
MaxAllocatedIPs: nil,
EnableKubevirtStaticIP: true,
}
})

It("should set MaxAllocatedIPs to default value", func() {
result := setDefaultsForIPPoolManagerConfig(config)
Expect(result.MaxAllocatedIPs).NotTo(BeNil())
Expect(*result.MaxAllocatedIPs).To(Equal(defaultMaxAllocatedIPs))
Expect(result.EnableKubevirtStaticIP).To(BeTrue())
})
})

Context("when EnableKubevirtStaticIP is false", func() {
BeforeEach(func() {
config = IPPoolManagerConfig{
MaxAllocatedIPs: nil,
EnableKubevirtStaticIP: false,
}
})

It("should set MaxAllocatedIPs to default value", func() {
result := setDefaultsForIPPoolManagerConfig(config)
Expect(result.MaxAllocatedIPs).NotTo(BeNil())
Expect(*result.MaxAllocatedIPs).To(Equal(defaultMaxAllocatedIPs))
Expect(result.EnableKubevirtStaticIP).To(BeFalse())
})
})
})
})
Loading

0 comments on commit 5348a3e

Please sign in to comment.