Skip to content

Commit c2989cc

Browse files
authored
Merge pull request #4298 from chaosi-zju/add-test
add test for static weight random remainder
2 parents 51169ba + 6b2101a commit c2989cc

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Copyright 2023 The Karmada Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package core
18+
19+
import (
20+
"testing"
21+
22+
clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
23+
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
24+
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
25+
"github.com/karmada-io/karmada/test/helper"
26+
)
27+
28+
// Test Case of even distribution of replicas, case 1
29+
// 1. create deployment (replicas=3), weight=1:1
30+
// 2. check two member cluster replicas, should be 2:1 or 1:2
31+
func Test_genericScheduler_AssignReplicas(t *testing.T) {
32+
tests := []struct {
33+
name string
34+
clusters []*clusterv1alpha1.Cluster
35+
placement *policyv1alpha1.Placement
36+
object *workv1alpha2.ResourceBindingSpec
37+
wants [][]workv1alpha2.TargetCluster
38+
wantErr bool
39+
}{
40+
{
41+
name: "replica 3, static weighted 1:1",
42+
clusters: []*clusterv1alpha1.Cluster{
43+
helper.NewCluster(ClusterMember1),
44+
helper.NewCluster(ClusterMember2),
45+
},
46+
object: &workv1alpha2.ResourceBindingSpec{
47+
Replicas: 3,
48+
},
49+
placement: &policyv1alpha1.Placement{
50+
ReplicaScheduling: &policyv1alpha1.ReplicaSchedulingStrategy{
51+
ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDivided,
52+
ReplicaDivisionPreference: policyv1alpha1.ReplicaDivisionPreferenceWeighted,
53+
WeightPreference: &policyv1alpha1.ClusterPreferences{
54+
StaticWeightList: []policyv1alpha1.StaticClusterWeight{
55+
{TargetCluster: policyv1alpha1.ClusterAffinity{ClusterNames: []string{ClusterMember1}}, Weight: 1},
56+
{TargetCluster: policyv1alpha1.ClusterAffinity{ClusterNames: []string{ClusterMember2}}, Weight: 1},
57+
},
58+
},
59+
},
60+
},
61+
wants: [][]workv1alpha2.TargetCluster{
62+
{
63+
{Name: ClusterMember1, Replicas: 1},
64+
{Name: ClusterMember2, Replicas: 2},
65+
},
66+
{
67+
{Name: ClusterMember1, Replicas: 2},
68+
{Name: ClusterMember2, Replicas: 1},
69+
},
70+
},
71+
wantErr: false,
72+
},
73+
}
74+
for _, tt := range tests {
75+
t.Run(tt.name, func(t *testing.T) {
76+
g := &genericScheduler{}
77+
got, err := g.assignReplicas(tt.clusters, tt.placement, tt.object)
78+
if (err != nil) != tt.wantErr {
79+
t.Errorf("AssignReplicas() error = %v, wantErr %v", err, tt.wantErr)
80+
return
81+
}
82+
if tt.wantErr {
83+
return
84+
}
85+
for _, want := range tt.wants {
86+
if helper.IsScheduleResultEqual(got, want) {
87+
return
88+
}
89+
}
90+
t.Errorf("AssignReplicas() got = %v, wants %v", got, tt.wants)
91+
})
92+
}
93+
}

0 commit comments

Comments
 (0)