-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
add dswp unit test case #41189
Merged
k8s-github-robot
merged 1 commit into
kubernetes:master
from
NickrenREN:kubelet-dswp-test
Apr 6, 2017
Merged
add dswp unit test case #41189
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
pkg/kubelet/volumemanager/populator/desired_state_of_world_populator_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* | ||
Copyright 2017 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package populator | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/kubernetes/pkg/api/v1" | ||
"k8s.io/kubernetes/pkg/client/clientset_generated/clientset/fake" | ||
containertest "k8s.io/kubernetes/pkg/kubelet/container/testing" | ||
kubepod "k8s.io/kubernetes/pkg/kubelet/pod" | ||
podtest "k8s.io/kubernetes/pkg/kubelet/pod/testing" | ||
"k8s.io/kubernetes/pkg/kubelet/secret" | ||
"k8s.io/kubernetes/pkg/kubelet/status" | ||
statustest "k8s.io/kubernetes/pkg/kubelet/status/testing" | ||
"k8s.io/kubernetes/pkg/kubelet/volumemanager/cache" | ||
volumetesting "k8s.io/kubernetes/pkg/volume/testing" | ||
"k8s.io/kubernetes/pkg/volume/util/types" | ||
"k8s.io/kubernetes/pkg/volume/util/volumehelper" | ||
) | ||
|
||
func TestFindAndAddNewPods_FindAndRemoveDeletedPods(t *testing.T) { | ||
fakeVolumePluginMgr, _ := volumetesting.GetTestVolumePluginMgr(t) | ||
fakeClient := &fake.Clientset{} | ||
|
||
fakeSecretManager := secret.NewFakeManager() | ||
fakePodManager := kubepod.NewBasicPodManager( | ||
podtest.NewFakeMirrorClient(), fakeSecretManager) | ||
|
||
fakesDSW := cache.NewDesiredStateOfWorld(fakeVolumePluginMgr) | ||
fakeRuntime := &containertest.FakeRuntime{} | ||
|
||
fakeStatusManager := status.NewManager(fakeClient, fakePodManager, &statustest.FakePodDeletionSafetyProvider{}) | ||
|
||
dswp := &desiredStateOfWorldPopulator{ | ||
kubeClient: fakeClient, | ||
loopSleepDuration: 100 * time.Millisecond, | ||
getPodStatusRetryDuration: 2 * time.Second, | ||
podManager: fakePodManager, | ||
podStatusProvider: fakeStatusManager, | ||
desiredStateOfWorld: fakesDSW, | ||
pods: processedPods{ | ||
processedPods: make(map[types.UniquePodName]bool)}, | ||
kubeContainerRuntime: fakeRuntime, | ||
keepTerminatedPodVolumes: false, | ||
} | ||
|
||
pod := &v1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "dswp-test-pod", | ||
UID: "dswp-test-pod-uid", | ||
Namespace: "dswp-test", | ||
}, | ||
Spec: v1.PodSpec{ | ||
Volumes: []v1.Volume{ | ||
{ | ||
Name: "dswp-test-volume-name", | ||
VolumeSource: v1.VolumeSource{ | ||
GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{ | ||
PDName: "dswp-test-fake-device", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Status: v1.PodStatus{ | ||
Phase: v1.PodPhase("Running"), | ||
}, | ||
} | ||
|
||
fakePodManager.AddPod(pod) | ||
|
||
podName := volumehelper.GetUniquePodName(pod) | ||
|
||
generatedVolumeName := "fake-plugin/" + pod.Spec.Volumes[0].Name | ||
|
||
dswp.findAndAddNewPods() | ||
|
||
if !dswp.pods.processedPods[podName] { | ||
t.Fatalf("Failed to record that the volumes for the specified pod: %s have been processed by the populator", podName) | ||
} | ||
|
||
expectedVolumeName := v1.UniqueVolumeName(generatedVolumeName) | ||
|
||
volumeExists := fakesDSW.VolumeExists(expectedVolumeName) | ||
if !volumeExists { | ||
t.Fatalf( | ||
"VolumeExists(%q) failed. Expected: <true> Actual: <%v>", | ||
expectedVolumeName, | ||
volumeExists) | ||
} | ||
|
||
if podExistsInVolume := fakesDSW.PodExistsInVolume( | ||
podName, expectedVolumeName); !podExistsInVolume { | ||
t.Fatalf( | ||
"DSW PodExistsInVolume returned incorrect value. Expected: <true> Actual: <%v>", | ||
podExistsInVolume) | ||
} | ||
|
||
verifyVolumeExistsInVolumesToMount( | ||
t, v1.UniqueVolumeName(generatedVolumeName), false /* expectReportedInUse */, fakesDSW) | ||
|
||
//let the pod be terminated | ||
podGet, exist := fakePodManager.GetPodByName(pod.Namespace, pod.Name) | ||
if !exist { | ||
t.Fatalf("Failed to get pod by pod name: %s and namespace: %s", pod.Name, pod.Namespace) | ||
} | ||
podGet.Status.Phase = v1.PodFailed | ||
|
||
//pod is added to fakePodManager but fakeRuntime can not get the pod,so here findAndRemoveDeletedPods() will remove the pod and volumes it is mounted | ||
dswp.findAndRemoveDeletedPods() | ||
|
||
if dswp.pods.processedPods[podName] { | ||
t.Fatalf("Failed to remove pods from desired state of world since they no longer exist") | ||
} | ||
|
||
volumeExists = fakesDSW.VolumeExists(expectedVolumeName) | ||
if volumeExists { | ||
t.Fatalf( | ||
"VolumeExists(%q) failed. Expected: <false> Actual: <%v>", | ||
expectedVolumeName, | ||
volumeExists) | ||
} | ||
|
||
if podExistsInVolume := fakesDSW.PodExistsInVolume( | ||
podName, expectedVolumeName); podExistsInVolume { | ||
t.Fatalf( | ||
"DSW PodExistsInVolume returned incorrect value. Expected: <false> Actual: <%v>", | ||
podExistsInVolume) | ||
} | ||
|
||
volumesToMount := fakesDSW.GetVolumesToMount() | ||
for _, volume := range volumesToMount { | ||
if volume.VolumeName == expectedVolumeName { | ||
t.Fatalf( | ||
"Found volume %v in the list of desired state of world volumes to mount. Expected not", | ||
expectedVolumeName) | ||
} | ||
} | ||
|
||
} | ||
|
||
func verifyVolumeExistsInVolumesToMount(t *testing.T, expectedVolumeName v1.UniqueVolumeName, expectReportedInUse bool, dsw cache.DesiredStateOfWorld) { | ||
volumesToMount := dsw.GetVolumesToMount() | ||
for _, volume := range volumesToMount { | ||
if volume.VolumeName == expectedVolumeName { | ||
if volume.ReportedInUse != expectReportedInUse { | ||
t.Fatalf( | ||
"Found volume %v in the list of VolumesToMount, but ReportedInUse incorrect. Expected: <%v> Actual: <%v>", | ||
expectedVolumeName, | ||
expectReportedInUse, | ||
volume.ReportedInUse) | ||
} | ||
|
||
return | ||
} | ||
} | ||
|
||
t.Fatalf( | ||
"Could not find volume %v in the list of desired state of world volumes to mount %+v", | ||
expectedVolumeName, | ||
volumesToMount) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: go convention is to avoid use of _ in function names and just use camelCase throughout.