Skip to content

Commit

Permalink
test: add testcase for util
Browse files Browse the repository at this point in the history
Signed-off-by: qiuming520 <qiuming_yewu@cmss.chinamobile.com>
  • Loading branch information
qiuming520 committed Sep 25, 2024
1 parent afb3cab commit 81e05e3
Show file tree
Hide file tree
Showing 9 changed files with 1,815 additions and 0 deletions.
170 changes: 170 additions & 0 deletions pkg/utils/convertpolicy/pod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
// nolint:dupl
package convertpolicy

import (
"testing"

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

kosmosv1alpha1 "github.com/kosmos.io/kosmos/pkg/apis/kosmos/v1alpha1"
)

func TestGetMatchPodConvertPolicy(t *testing.T) {
t.Run("Test with empty policies", func(t *testing.T) {
policies := kosmosv1alpha1.PodConvertPolicyList{}
podLabels := map[string]string{
"app": "test",
}
nodeLabels := map[string]string{
"node": "test",
}

matched, err := GetMatchPodConvertPolicy(policies, podLabels, nodeLabels)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(matched) != 0 {
t.Errorf("Expected no matched policies, got %v", matched)
}
})

t.Run("Test with policies that do not match", func(t *testing.T) {
policies := kosmosv1alpha1.PodConvertPolicyList{
Items: []kosmosv1alpha1.PodConvertPolicy{
{
Spec: kosmosv1alpha1.PodConvertPolicySpec{
LabelSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "not-test",
},
},
},
},
},
}

podLabels := map[string]string{
"app": "test",
}
nodeLabels := map[string]string{
"node": "test",
}
matched, err := GetMatchPodConvertPolicy(policies, podLabels, nodeLabels)

if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(matched) != 0 {
t.Errorf("Expected no matched policies, got %v", matched)
}
})

t.Run("Test with policies that match", func(t *testing.T) {
policies := kosmosv1alpha1.PodConvertPolicyList{
Items: []kosmosv1alpha1.PodConvertPolicy{
{
Spec: kosmosv1alpha1.PodConvertPolicySpec{
LabelSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "test",
},
},
},
},
},
}

podLabels := map[string]string{
"app": "test",
}
nodeLabels := map[string]string{
"node": "test",
}
matched, err := GetMatchPodConvertPolicy(policies, podLabels, nodeLabels)

if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(matched) != 1 {
t.Errorf("Expected 1 matched policy, got %v", len(matched))
}
})
}

func TestGetMatchClusterPodConvertPolicy(t *testing.T) {
t.Run("Test with empty policies", func(t *testing.T) {
policies := kosmosv1alpha1.ClusterPodConvertPolicyList{}
podLabels := map[string]string{
"app": "test",
}
nodeLabels := map[string]string{
"node": "test",
}

matched, err := GetMatchClusterPodConvertPolicy(policies, podLabels, nodeLabels)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(matched) != 0 {
t.Errorf("Expected no matched policies, got %v", matched)
}
})

t.Run("Test with policies that do not match", func(t *testing.T) {
policies := kosmosv1alpha1.ClusterPodConvertPolicyList{
Items: []kosmosv1alpha1.ClusterPodConvertPolicy{
{
Spec: kosmosv1alpha1.ClusterPodConvertPolicySpec{
LabelSelector: metav1.LabelSelector{
MatchLabels: map[string]string{"app": "not-test"},
},
},
},
},
}

podLabels := map[string]string{
"app": "test",
}
nodeLabels := map[string]string{
"node": "test",
}
matched, err := GetMatchClusterPodConvertPolicy(policies, podLabels, nodeLabels)

if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(matched) != 0 {
t.Errorf("Expected no matched policies, got %v", matched)
}
})

t.Run("Test with policies that match", func(t *testing.T) {
policies := kosmosv1alpha1.ClusterPodConvertPolicyList{
Items: []kosmosv1alpha1.ClusterPodConvertPolicy{
{
Spec: kosmosv1alpha1.ClusterPodConvertPolicySpec{
LabelSelector: metav1.LabelSelector{
MatchLabels: map[string]string{"app": "test"},
},
},
},
},
}

podLabels := map[string]string{
"app": "test",
}
nodeLabels := map[string]string{
"node": "test",
}
matched, err := GetMatchClusterPodConvertPolicy(policies, podLabels, nodeLabels)

if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(matched) != 1 {
t.Errorf("more than one matched policies, got %v", matched)
}
})
}
168 changes: 168 additions & 0 deletions pkg/utils/flags/backoffflag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// nolint:dupl
package flags

import (
"testing"
"time"

"github.com/spf13/pflag"
)

func TestAddFlags(t *testing.T) {
t.Run("AddFlags with default values", func(t *testing.T) {
o := &BackoffOptions{}
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
o.AddFlags(fs)

// Check default values
if o.Duration != 5*time.Millisecond {
t.Errorf("Expected default Duration to be 5ms, got %v", o.Duration)
}
if o.Factor != 1.0 {
t.Errorf("Expected default Factor to be 1.0, got %v", o.Factor)
}
if o.Jitter != 0.1 {
t.Errorf("Expected default Jitter to be 0.1, got %v", o.Jitter)
}
if o.Steps != 5 {
t.Errorf("Expected default Steps to be 5, got %v", o.Steps)
}
})

t.Run("AddFlags with custom values", func(t *testing.T) {
o := &BackoffOptions{}
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
o.AddFlags(fs)

// Check setting values
err := fs.Parse([]string{"--retry-duration=1s", "--retry-factor=2.0", "--retry-jitter=0.2", "--retry-steps=10"})
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}

if o.Duration != 1*time.Second {
t.Errorf("Expected Duration to be 1s, got %v", o.Duration)
}
if o.Factor != 2.0 {
t.Errorf("Expected Factor to be 2.0, got %v", o.Factor)
}
if o.Jitter != 0.2 {
t.Errorf("Expected Jitter to be 0.2, got %v", o.Jitter)
}
if o.Steps != 10 {
t.Errorf("Expected Steps to be 10, got %v", o.Steps)
}
})

t.Run("AddFlags with zero values", func(t *testing.T) {
o := &BackoffOptions{}
fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
o.AddFlags(fs)

// Check setting values
err := fs.Parse([]string{"--retry-duration=0", "--retry-factor=0.0", "--retry-jitter=0.0", "--retry-steps=0"})
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}

if o.Duration != 0 {
t.Errorf("Expected Duration to be 0, got %v", o.Duration)
}
if o.Factor != 0.0 {
t.Errorf("Expected Factor to be 0.0, got %v", o.Factor)
}
if o.Jitter != 0.0 {
t.Errorf("Expected Jitter to be 0.0, got %v", o.Jitter)
}
if o.Steps != 0 {
t.Errorf("Expected Steps to be 0, got %v", o.Steps)
}
})
}

func TestDefaultUpdateRetryBackoff(t *testing.T) {
t.Run("Test with zero values", func(t *testing.T) {
opts := BackoffOptions{}
backoff := DefaultUpdateRetryBackoff(opts)

if backoff.Duration != 5*time.Millisecond {
t.Errorf("Expected duration to be 5ms, got %v", backoff.Duration)
}
if backoff.Factor != 1.0 {
t.Errorf("Expected factor to be 1.0, got %v", backoff.Factor)
}
if backoff.Jitter != 0.1 {
t.Errorf("Expected jitter to be 0.1, got %v", backoff.Jitter)
}
if backoff.Steps != 5 {
t.Errorf("Expected steps to be 5, got %v", backoff.Steps)
}
})

t.Run("Test with custom values", func(t *testing.T) {
opts := BackoffOptions{
Duration: 10 * time.Millisecond,
Factor: 2.0,
Jitter: 0.2,
Steps: 10,
}
backoff := DefaultUpdateRetryBackoff(opts)

if backoff.Duration != 10*time.Millisecond {
t.Errorf("Expected duration to be 10ms, got %v", backoff.Duration)
}
if backoff.Factor != 2.0 {
t.Errorf("Expected factor to be 2.0, got %v", backoff.Factor)
}
if backoff.Jitter != 0.2 {
t.Errorf("Expected jitter to be 0.2, got %v", backoff.Jitter)
}
if backoff.Steps != 10 {
t.Errorf("Expected steps to be 10, got %v", backoff.Steps)
}
})

t.Run("Test with negative values", func(t *testing.T) {
opts := BackoffOptions{
Duration: -1 * time.Millisecond,
Factor: -2.0,
Jitter: -0.2,
Steps: -10,
}
backoff := DefaultUpdateRetryBackoff(opts)

if backoff.Duration != 5*time.Millisecond {
t.Errorf("Expected duration to be 5ms, got %v", backoff.Duration)
}
if backoff.Factor != 1.0 {
t.Errorf("Expected factor to be 1.0, got %v", backoff.Factor)
}
if backoff.Jitter != 0.1 {
t.Errorf("Expected jitter to be 0.1, got %v", backoff.Jitter)
}
if backoff.Steps != 5 {
t.Errorf("Expected steps to be 5, got %v", backoff.Steps)
}
})

t.Run("Test with part values", func(t *testing.T) {
opts := BackoffOptions{
Jitter: 0.2,
Steps: 10,
}
backoff := DefaultUpdateRetryBackoff(opts)

if backoff.Duration != 5*time.Millisecond {
t.Errorf("Expected duration to be 5ms, got %v", backoff.Duration)
}
if backoff.Factor != 1.0 {
t.Errorf("Expected factor to be 1.0, got %v", backoff.Factor)
}
if backoff.Jitter != 0.2 {
t.Errorf("Expected jitter to be 0.2, got %v", backoff.Jitter)
}
if backoff.Steps != 10 {
t.Errorf("Expected steps to be 10, got %v", backoff.Steps)
}
})
}
Loading

0 comments on commit 81e05e3

Please sign in to comment.