mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 03:41:45 +00:00
Merge pull request #59367 from colemickens/ptr-flake
Automatic merge from submit-queue (batch tested with PRs 59367, 60007). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. podtolerationrestriction: fix informer race in test **What this PR does / why we need it**: This fixes test flakes in the PodTolerationRestriction admission controller unit tests. They seem to pass most of the time currently, but modifications I was making for #58818 changed timing and caused it to constantly break. **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: n/a **Special notes for your reviewer**: n/a Sending this as a one-off because the changes for both of the admin controllers in #58818 require additional discussion. Thanks to @ericchiang for finding it and authoring the commit; I just rebased and sent the PR. ```release-note NONE ```
This commit is contained in:
commit
becee4c12e
@ -37,21 +37,6 @@ import (
|
|||||||
|
|
||||||
// TestPodAdmission verifies various scenarios involving pod/namespace tolerations
|
// TestPodAdmission verifies various scenarios involving pod/namespace tolerations
|
||||||
func TestPodAdmission(t *testing.T) {
|
func TestPodAdmission(t *testing.T) {
|
||||||
namespace := &api.Namespace{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: "testNamespace",
|
|
||||||
Namespace: "",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
mockClient := &fake.Clientset{}
|
|
||||||
handler, informerFactory, err := newHandlerForTest(mockClient)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("unexpected error initializing handler: %v", err)
|
|
||||||
}
|
|
||||||
stopCh := make(chan struct{})
|
|
||||||
defer close(stopCh)
|
|
||||||
informerFactory.Start(stopCh)
|
|
||||||
|
|
||||||
CPU1000m := resource.MustParse("1000m")
|
CPU1000m := resource.MustParse("1000m")
|
||||||
CPU500m := resource.MustParse("500m")
|
CPU500m := resource.MustParse("500m")
|
||||||
@ -230,6 +215,15 @@ func TestPodAdmission(t *testing.T) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
t.Run(test.testName, func(t *testing.T) {
|
||||||
|
namespace := &api.Namespace{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "testNamespace",
|
||||||
|
Namespace: "",
|
||||||
|
Annotations: map[string]string{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
if test.namespaceTolerations != nil {
|
if test.namespaceTolerations != nil {
|
||||||
tolerationStr, err := json.Marshal(test.namespaceTolerations)
|
tolerationStr, err := json.Marshal(test.namespaceTolerations)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -246,7 +240,14 @@ func TestPodAdmission(t *testing.T) {
|
|||||||
namespace.Annotations[NSWLTolerations] = string(tolerationStr)
|
namespace.Annotations[NSWLTolerations] = string(tolerationStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
informerFactory.Core().InternalVersion().Namespaces().Informer().GetStore().Update(namespace)
|
mockClient := fake.NewSimpleClientset(namespace)
|
||||||
|
handler, informerFactory, err := newHandlerForTest(mockClient)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error initializing handler: %v", err)
|
||||||
|
}
|
||||||
|
stopCh := make(chan struct{})
|
||||||
|
defer close(stopCh)
|
||||||
|
informerFactory.Start(stopCh)
|
||||||
|
|
||||||
handler.pluginConfig = &pluginapi.Configuration{Default: test.defaultClusterTolerations, Whitelist: test.clusterWhitelist}
|
handler.pluginConfig = &pluginapi.Configuration{Default: test.defaultClusterTolerations, Whitelist: test.clusterWhitelist}
|
||||||
pod := test.pod
|
pod := test.pod
|
||||||
@ -257,7 +258,7 @@ func TestPodAdmission(t *testing.T) {
|
|||||||
oldPod.Initializers = &metav1.Initializers{Pending: []metav1.Initializer{{Name: "init"}}}
|
oldPod.Initializers = &metav1.Initializers{Pending: []metav1.Initializer{{Name: "init"}}}
|
||||||
oldPod.Spec.Tolerations = []api.Toleration{{Key: "testKey", Operator: "Equal", Value: "testValue1", Effect: "NoSchedule", TolerationSeconds: nil}}
|
oldPod.Spec.Tolerations = []api.Toleration{{Key: "testKey", Operator: "Equal", Value: "testValue1", Effect: "NoSchedule", TolerationSeconds: nil}}
|
||||||
|
|
||||||
err := handler.Admit(admission.NewAttributesRecord(pod, nil, api.Kind("Pod").WithVersion("version"), "testNamespace", namespace.ObjectMeta.Name, api.Resource("pods").WithVersion("version"), "", admission.Create, nil))
|
err = handler.Admit(admission.NewAttributesRecord(pod, nil, api.Kind("Pod").WithVersion("version"), "testNamespace", namespace.ObjectMeta.Name, api.Resource("pods").WithVersion("version"), "", admission.Create, nil))
|
||||||
if test.admit && err != nil {
|
if test.admit && err != nil {
|
||||||
t.Errorf("Test: %s, expected no error but got: %s", test.testName, err)
|
t.Errorf("Test: %s, expected no error but got: %s", test.testName, err)
|
||||||
} else if !test.admit && err == nil {
|
} else if !test.admit && err == nil {
|
||||||
@ -281,6 +282,7 @@ func TestPodAdmission(t *testing.T) {
|
|||||||
if test.admit && !tolerations.EqualTolerations(updatedPodTolerations, test.mergedTolerations) {
|
if test.admit && !tolerations.EqualTolerations(updatedPodTolerations, test.mergedTolerations) {
|
||||||
t.Errorf("Test: %s, expected: %#v but got: %#v", test.testName, test.mergedTolerations, updatedPodTolerations)
|
t.Errorf("Test: %s, expected: %#v but got: %#v", test.testName, test.mergedTolerations, updatedPodTolerations)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user