Skip to content

Commit

Permalink
test:before/after score plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
196Ikuchil committed Mar 4, 2022
1 parent e052074 commit b99ad8b
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions scheduler/plugin/wrappedplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,119 @@ func Test_wrappedPlugin_Score(t *testing.T) {
}
}

func Test_wrappedPlugin_Score_WithPluginExtender(t *testing.T) {
t.Parallel()

type args struct {
pod *v1.Pod
nodename string
}
tests := []struct {
name string
prepareEachMockFn func(ctx context.Context, m *mock_plugin.MockStore, p *mock_plugin.MockScorePlugin, se *mock_plugin.MockScorePluginExtender, as args)
args args
want int64
wantstatus *framework.Status
}{
{
name: "return AfterScore's results when Score is successful",
prepareEachMockFn: func(ctx context.Context, s *mock_plugin.MockStore, p *mock_plugin.MockScorePlugin, se *mock_plugin.MockScorePluginExtender, as args) {
success1 := framework.NewStatus(framework.Success, "BeforeScore returned")
success2 := framework.NewStatus(framework.Error, "Score returned")
success3 := framework.NewStatus(framework.Success, "AfterScore returned")
se.EXPECT().BeforeScore(ctx, nil, as.pod, "node1").Return(int64(1111), success1)
p.EXPECT().Score(ctx, nil, as.pod, "node1").Return(int64(2222), success2)
se.EXPECT().AfterScore(ctx, nil, as.pod, "node1", int64(2222), success2).Return(int64(3333), success3)
p.EXPECT().Name().Return("fakeScorePlugin").AnyTimes()
s.EXPECT().AddScoreResult("default", "pod1", "node1", "BeforefakeScorePlugin", int64(1111))
s.EXPECT().AddScoreResult("default", "pod1", "node1", "fakeScorePlugin", int64(2222))
s.EXPECT().AddScoreResult("default", "pod1", "node1", "AfterfakeScorePlugin", int64(3333))
},
args: args{
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "default"}},
nodename: "node1",
},
want: 3333,
wantstatus: framework.NewStatus(framework.Success, "AfterScore returned"),
},
{
name: "return AfterScore's results & does not call AddScoreResult after Score, if Score fails",
prepareEachMockFn: func(ctx context.Context, s *mock_plugin.MockStore, p *mock_plugin.MockScorePlugin, se *mock_plugin.MockScorePluginExtender, as args) {
success1 := framework.NewStatus(framework.Success, "BeforeScore returned")
failure := framework.NewStatus(framework.Error, "Score returned")
success3 := framework.NewStatus(framework.Success, "AfterScore returned")
se.EXPECT().BeforeScore(ctx, nil, as.pod, "node1").Return(int64(1111), success1)
p.EXPECT().Score(ctx, nil, as.pod, "node1").Return(int64(2222), failure)
se.EXPECT().AfterScore(ctx, nil, as.pod, "node1", int64(2222), failure).Return(int64(3333), success3)
p.EXPECT().Name().Return("fakeScorePlugin").AnyTimes()
s.EXPECT().AddScoreResult("default", "pod1", "node1", "BeforefakeScorePlugin", int64(1111))
s.EXPECT().AddScoreResult("default", "pod1", "node1", "AfterfakeScorePlugin", int64(3333))
},
args: args{
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "default"}},
nodename: "node1",
},
want: 3333,
wantstatus: framework.NewStatus(framework.Success, "AfterScore returned"),
},
{
name: "return Before's results & does not call Score, if BeforeScore fails",
prepareEachMockFn: func(ctx context.Context, s *mock_plugin.MockStore, p *mock_plugin.MockScorePlugin, se *mock_plugin.MockScorePluginExtender, as args) {
failure := framework.NewStatus(framework.Error, "BeforeScore returned")
se.EXPECT().BeforeScore(ctx, nil, as.pod, "node1").Return(int64(1111), failure)
p.EXPECT().Name().Return("fakeScorePlugin").AnyTimes()
s.EXPECT().AddScoreResult("default", "pod1", "node1", "BeforefakeScorePlugin", int64(1111))
},
args: args{
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "default"}},
nodename: "node1",
},
want: 1111,
wantstatus: framework.NewStatus(framework.Error, "BeforeScore returned"),
},
{
name: "return AfterScore's results when AfterScore is fails",
prepareEachMockFn: func(ctx context.Context, s *mock_plugin.MockStore, p *mock_plugin.MockScorePlugin, se *mock_plugin.MockScorePluginExtender, as args) {
success1 := framework.NewStatus(framework.Success, "BeforeScore returned")
success2 := framework.NewStatus(framework.Error, "Score returned")
failure := framework.NewStatus(framework.Error, "AfterScore returned")
se.EXPECT().BeforeScore(ctx, nil, as.pod, "node1").Return(int64(1111), success1)
p.EXPECT().Score(ctx, nil, as.pod, "node1").Return(int64(2222), success2)
se.EXPECT().AfterScore(ctx, nil, as.pod, "node1", int64(2222), success2).Return(int64(3333), failure)
p.EXPECT().Name().Return("fakeScorePlugin").AnyTimes()
s.EXPECT().AddScoreResult("default", "pod1", "node1", "BeforefakeScorePlugin", int64(1111))
s.EXPECT().AddScoreResult("default", "pod1", "node1", "fakeScorePlugin", int64(2222))
s.EXPECT().AddScoreResult("default", "pod1", "node1", "AfterfakeScorePlugin", int64(3333))
},
args: args{
pod: &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "default"}},
nodename: "node1",
},
want: 3333,
wantstatus: framework.NewStatus(framework.Error, "AfterScore returned"),
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
s := mock_plugin.NewMockStore(ctrl)
p := mock_plugin.NewMockScorePlugin(ctrl)
se := mock_plugin.NewMockScorePluginExtender(ctrl)
e := &Extenders{
scorePluginExtender: se,
}
ctx := context.Background()
tt.prepareEachMockFn(ctx, s, p, se, tt.args)
pl := newWrappedPlugin(s, p, 0, WithExtendersOption(e)).(*wrappedPlugin)
gotscore, gotstatus := pl.Score(ctx, nil, tt.args.pod, tt.args.nodename)
assert.Equal(t, tt.want, gotscore)
assert.Equal(t, tt.wantstatus, gotstatus)
})
}
}

func Test_wrappedPlugin_ScoreExtensions(t *testing.T) {
t.Parallel()
tests := []struct {
Expand Down

0 comments on commit b99ad8b

Please sign in to comment.