Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests in pkg/binding for condition coverage #5388

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions pkg/controllers/binding/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package binding

import (
"reflect"
"sort"
"testing"

v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -379,3 +380,122 @@ func Test_shouldSuspendDispatching(t *testing.T) {
})
}
}

func Test_needReviseReplicas(t *testing.T) {
tests := []struct {
name string
replicas int32
placement *policyv1alpha1.Placement
want bool
}{
{
name: "replicas is zero",
yashpandey06 marked this conversation as resolved.
Show resolved Hide resolved
replicas: 0,
placement: &policyv1alpha1.Placement{
ReplicaScheduling: &policyv1alpha1.ReplicaSchedulingStrategy{
ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDivided,
},
},
want: false,
},
{
name: "placement is nil",
replicas: 1,
placement: nil,
want: false,
},
{
name: "replica scheduling type is not divided",
replicas: 1,
placement: &policyv1alpha1.Placement{
ReplicaScheduling: &policyv1alpha1.ReplicaSchedulingStrategy{
ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDuplicated,
},
},
want: false,
},
{
name: "replica scheduling type is divided",
replicas: 1,
placement: &policyv1alpha1.Placement{
ReplicaScheduling: &policyv1alpha1.ReplicaSchedulingStrategy{
ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDivided,
},
},
want: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := needReviseReplicas(tt.replicas, tt.placement); got != tt.want {
t.Errorf("needReviseReplicas() = %v, want %v", got, tt.want)
}
})
}
}

func Test_divideReplicasByJobCompletions(t *testing.T) {
tests := []struct {
name string
workload *unstructured.Unstructured
clusters []workv1alpha2.TargetCluster
want []workv1alpha2.TargetCluster
wantErr bool
}{
{
name: "completions found",
workload: &unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{
yashpandey06 marked this conversation as resolved.
Show resolved Hide resolved
"completions": int64(10),
},
},
},
clusters: []workv1alpha2.TargetCluster{
{Name: "cluster1", Replicas: 5},
{Name: "cluster2", Replicas: 5},
},
want: []workv1alpha2.TargetCluster{
{Name: "cluster1", Replicas: 5},
{Name: "cluster2", Replicas: 5},
},
wantErr: false,
},
{
name: "error in NestedInt64",
workload: &unstructured.Unstructured{
Object: map[string]interface{}{
"spec": "invalid",
},
},
clusters: []workv1alpha2.TargetCluster{
{Name: "cluster1", Replicas: 5},
{Name: "cluster2", Replicas: 5},
},
want: nil,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := divideReplicasByJobCompletions(tt.workload, tt.clusters)
if (err != nil) != tt.wantErr {
t.Errorf("divideReplicasByJobCompletions() error = %v, wantErr %v", err, tt.wantErr)
return
}

sort.Slice(got, func(i, j int) bool {
return got[i].Name < got[j].Name
})
sort.Slice(tt.want, func(i, j int) bool {
return tt.want[i].Name < tt.want[j].Name
})

if !reflect.DeepEqual(got, tt.want) {
t.Errorf("divideReplicasByJobCompletions() = %v, want %v", got, tt.want)
}
})
}
}