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

refactor: add reset method to all test plugins #79424

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 51 additions & 31 deletions test/integration/scheduler/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,46 @@ import (
"testing"
"time"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove the added "v1". The problem is usually that the editor you are using relies on goimports rather than gofmt to do formatting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept asking people to remove these aliases, but now I feel it is not worth fighting the tool. goimport does this and many PRs have this king of aliasing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: to eliminate the v1 in editor settings (e.g. vscode): https://groups.google.com/d/msg/kubernetes-dev/Y2ShVRrU4xM/LKfaiFqAAwAJ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: to eliminate the v1 in editor settings (e.g. vscode): https://groups.google.com/d/msg/kubernetes-dev/Y2ShVRrU4xM/LKfaiFqAAwAJ

Thanks for the information, but I used emacs for development, this does not work for me 😢

However, I do find my way to solve this problem through magit which could help me to commit changes by chunk, and these changes are accidentally committed. 🐱

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
schedulerconfig "k8s.io/kubernetes/pkg/scheduler/apis/config"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
)

// TesterPlugin is common ancestor for a test plugin that allows injection of
// failures and some other test functionalities.
type TesterPlugin struct {
numPrefilterCalled int
numReserveCalled int
numPrebindCalled int
numPostbindCalled int
numUnreserveCalled int
failPrefilter bool
rejectPrefilter bool
failReserve bool
failPrebind bool
rejectPrebind bool
numPermitCalled int
failPermit bool
rejectPermit bool
timeoutPermit bool
waitAndRejectPermit bool
waitAndAllowPermit bool
}

type PrefilterPlugin struct {
TesterPlugin
numPrefilterCalled int
failPrefilter bool
rejectPrefilter bool
}

type ReservePlugin struct {
TesterPlugin
numReserveCalled int
failReserve bool
}

type PrebindPlugin struct {
TesterPlugin
numPrebindCalled int
failPrebind bool
rejectPrebind bool
}

type PostbindPlugin struct {
TesterPlugin
numPostbindCalled int
}

type UnreservePlugin struct {
TesterPlugin
numUnreserveCalled int
}

type PermitPlugin struct {
TesterPlugin
fh framework.FrameworkHandle
numPermitCalled int
failPermit bool
rejectPermit bool
timeoutPermit bool
waitAndRejectPermit bool
waitAndAllowPermit bool
fh framework.FrameworkHandle
}

const (
Expand Down Expand Up @@ -107,6 +96,11 @@ func (rp *ReservePlugin) Reserve(pc *framework.PluginContext, pod *v1.Pod, nodeN
return nil
}

// reset used to reset reserve plugin.
func (rp *ReservePlugin) reset() {
rp.numReserveCalled = 0
}

// NewReservePlugin is the factory for reserve plugin.
func NewReservePlugin(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
return resPlugin, nil
Expand All @@ -131,9 +125,11 @@ func (pp *PrebindPlugin) Prebind(pc *framework.PluginContext, pod *v1.Pod, nodeN
return nil
}

// reset used to reset numPrebindCalled.
// reset used to reset prebind plugin.
func (pp *PrebindPlugin) reset() {
pp.numPrebindCalled = 0
pp.failPrebind = false
pp.rejectPrebind = false
}

// NewPrebindPlugin is the factory for prebind plugin.
Expand All @@ -153,7 +149,7 @@ func (pp *PostbindPlugin) Postbind(pc *framework.PluginContext, pod *v1.Pod, nod
pp.numPostbindCalled++
}

// reset used to reset numPostbindCalled.
// reset used to reset postbind plugin.
func (pp *PostbindPlugin) reset() {
pp.numPostbindCalled = 0
}
Expand Down Expand Up @@ -182,6 +178,13 @@ func (pp *PrefilterPlugin) Prefilter(pc *framework.PluginContext, pod *v1.Pod) *
return nil
}

// reset used to reset prefilter plugin.
func (pp *PrefilterPlugin) reset() {
pp.numPrefilterCalled = 0
pp.failPrefilter = false
pp.rejectPrefilter = false
}

// NewPrebindPlugin is the factory for prebind plugin.
func NewPrefilterPlugin(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
return pfPlugin, nil
Expand Down Expand Up @@ -253,6 +256,16 @@ func (pp *PermitPlugin) Permit(pc *framework.PluginContext, pod *v1.Pod, nodeNam
return nil, 0
}

// reset used to reset permit plugin.
func (pp *PermitPlugin) reset() {
pp.numPermitCalled = 0
pp.failPermit = false
pp.rejectPermit = false
pp.timeoutPermit = false
pp.waitAndRejectPermit = false
pp.waitAndAllowPermit = false
}

// NewPermitPlugin is the factory for permit plugin.
func NewPermitPlugin(_ *runtime.Unknown, fh framework.FrameworkHandle) (framework.Plugin, error) {
perPlugin.fh = fh
Expand Down Expand Up @@ -333,6 +346,7 @@ func TestPrefilterPlugin(t *testing.T) {
t.Errorf("Expected the prefilter plugin to be called.")
}

pfPlugin.reset()
cleanupPods(cs, t, []*v1.Pod{pod})
}
}
Expand Down Expand Up @@ -391,6 +405,7 @@ func TestReservePlugin(t *testing.T) {
t.Errorf("Expected the reserve plugin to be called.")
}

resPlugin.reset()
cleanupPods(cs, t, []*v1.Pod{pod})
}
}
Expand Down Expand Up @@ -482,6 +497,7 @@ func TestPrebindPlugin(t *testing.T) {
t.Errorf("Expected the prebind plugin to be called.")
}

pbdPlugin.reset()
cleanupPods(cs, t, []*v1.Pod{pod})
}
}
Expand Down Expand Up @@ -593,6 +609,7 @@ func TestUnreservePlugin(t *testing.T) {
}
}
}

unresPlugin.reset()
pbdPlugin.reset()
cleanupPods(cs, t, []*v1.Pod{pod})
Expand Down Expand Up @@ -792,6 +809,7 @@ func TestPermitPlugin(t *testing.T) {
perPlugin.timeoutPermit = test.timeout
perPlugin.waitAndRejectPermit = false
perPlugin.waitAndAllowPermit = false

// Create a best effort pod.
pod, err := createPausePod(cs,
initPausePod(cs, &pausePodConfig{Name: "test-pod", Namespace: context.ns.Name}))
Expand All @@ -818,6 +836,7 @@ func TestPermitPlugin(t *testing.T) {
t.Errorf("Expected the permit plugin to be called.")
}

perPlugin.reset()
cleanupPods(cs, t, []*v1.Pod{pod})
}
}
Expand Down Expand Up @@ -911,6 +930,7 @@ func TestCoSchedulingWithPermitPlugin(t *testing.T) {
t.Errorf("Expected the permit plugin to be called.")
}

perPlugin.reset()
cleanupPods(cs, t, []*v1.Pod{waitingPod, signallingPod})
}
}