Files
open-cluster-management/pkg/placement/plugins/addon/addon_test.go
Jian Zhu 7332a585c0 🌱 add a verify rule for golang files import order (#177)
* 🌱 add a verify rule for golang files import order

This PR uses the [gci tool](https://github.com/daixiang0/gci) to make all go files' import section with a specific order, it will organize import with group with order:
1. standard library modules
2. 3rd party modules
3. modules in OCM org, like the `open-cluster-management.io/api`
4. current project `open-cluster-management.io/ocm` modules

developers can use the `make fmt-imports` to format the import automatically and the `make verify-fmt-imports` to check for any violation.

Signed-off-by: zhujian <jiazhu@redhat.com>

* 🌱 format the go files import

Signed-off-by: zhujian <jiazhu@redhat.com>

---------

Signed-off-by: zhujian <jiazhu@redhat.com>
2023-06-12 10:23:04 -04:00

111 lines
4.3 KiB
Go

package addon
import (
"context"
"errors"
"testing"
"time"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime"
testingclock "k8s.io/utils/clock/testing"
clusterapiv1 "open-cluster-management.io/api/cluster/v1"
clusterapivbeta1 "open-cluster-management.io/api/cluster/v1beta1"
testinghelpers "open-cluster-management.io/ocm/pkg/placement/helpers/testing"
)
var fakeTime = time.Date(2022, time.January, 01, 0, 0, 0, 0, time.UTC)
var expiredTime = fakeTime.Add(-30 * time.Second)
func TestScoreClusterWithAddOn(t *testing.T) {
cases := []struct {
name string
placement *clusterapivbeta1.Placement
clusters []*clusterapiv1.ManagedCluster
existingAddOnScores []runtime.Object
expectedScores map[string]int64
expectedErr error
}{
{
name: "no addon scores",
placement: testinghelpers.NewPlacement("test", "test").WithScoreCoordinateAddOn("test", "score1", 1).Build(),
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").Build(),
testinghelpers.NewManagedCluster("cluster2").Build(),
testinghelpers.NewManagedCluster("cluster3").Build(),
},
existingAddOnScores: []runtime.Object{},
expectedScores: map[string]int64{"cluster1": 0, "cluster2": 0, "cluster3": 0},
},
{
name: "part of addon scores generated",
placement: testinghelpers.NewPlacement("test", "test").WithScoreCoordinateAddOn("test", "score1", 1).Build(),
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").Build(),
testinghelpers.NewManagedCluster("cluster2").Build(),
testinghelpers.NewManagedCluster("cluster3").Build(),
},
existingAddOnScores: []runtime.Object{
testinghelpers.NewAddOnPlacementScore("cluster1", "test").WithScore("score1", 30).Build(),
},
expectedScores: map[string]int64{"cluster1": 30, "cluster2": 0, "cluster3": 0},
},
{
name: "part of addon scores expired",
placement: testinghelpers.NewPlacement("test", "test").WithScoreCoordinateAddOn("test", "score1", 1).Build(),
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").Build(),
testinghelpers.NewManagedCluster("cluster2").Build(),
testinghelpers.NewManagedCluster("cluster3").Build(),
},
existingAddOnScores: []runtime.Object{
testinghelpers.NewAddOnPlacementScore("cluster1", "test").WithScore("score1", 30).WithValidUntil(expiredTime).Build(),
testinghelpers.NewAddOnPlacementScore("cluster2", "test").WithScore("score1", 40).Build(),
testinghelpers.NewAddOnPlacementScore("cluster3", "test").WithScore("score1", 50).Build(),
},
expectedScores: map[string]int64{"cluster1": 0, "cluster2": 40, "cluster3": 50},
expectedErr: errors.New("AddOnPlacementScores cluster1/test expired"),
},
{
name: "all the addon scores generated",
placement: testinghelpers.NewPlacement("test", "test").WithScoreCoordinateAddOn("test", "score1", 1).Build(),
clusters: []*clusterapiv1.ManagedCluster{
testinghelpers.NewManagedCluster("cluster1").Build(),
testinghelpers.NewManagedCluster("cluster2").Build(),
testinghelpers.NewManagedCluster("cluster3").Build(),
},
existingAddOnScores: []runtime.Object{
testinghelpers.NewAddOnPlacementScore("cluster1", "test").WithScore("score1", 30).Build(),
testinghelpers.NewAddOnPlacementScore("cluster2", "test").WithScore("score1", 40).Build(),
testinghelpers.NewAddOnPlacementScore("cluster3", "test").WithScore("score1", 50).Build(),
},
expectedScores: map[string]int64{"cluster1": 30, "cluster2": 40, "cluster3": 50},
},
}
AddOnClock = testingclock.NewFakeClock(fakeTime)
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
addon := &AddOn{
handle: testinghelpers.NewFakePluginHandle(t, nil, c.existingAddOnScores...),
prioritizerName: "AddOn/test/score1",
resourceName: "test",
scoreName: "score1",
}
scoreResult, status := addon.Score(context.TODO(), c.placement, c.clusters)
scores := scoreResult.Scores
err := status.AsError()
if err != nil && err.Error() != c.expectedErr.Error() {
t.Errorf("expect err %v but get %v", c.expectedErr, err)
}
if !apiequality.Semantic.DeepEqual(scores, c.expectedScores) {
t.Errorf("Expect score %v, but got %v", c.expectedScores, scores)
}
})
}
}