mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 07:47:56 +00:00
Merge pull request #41563 from gyliu513/kubelet-util
Automatic merge from submit-queue Improved code coverage for pkg/kubelet/util. The test coverage for pkg/kubelet/util.go increased from 45.1% to 84.3%. **What this PR does / why we need it**: **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes # **Special notes for your reviewer**: **Release note**: ```release-note ```
This commit is contained in:
commit
b68b4aeb20
@ -1042,6 +1042,136 @@ func TestHostNetworkDisallowed(t *testing.T) {
|
|||||||
assert.Error(t, err, "expected pod infra creation to fail")
|
assert.Error(t, err, "expected pod infra creation to fail")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHostPIDAllowed(t *testing.T) {
|
||||||
|
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
||||||
|
defer testKubelet.Cleanup()
|
||||||
|
testKubelet.fakeCadvisor.On("Start").Return(nil)
|
||||||
|
testKubelet.fakeCadvisor.On("VersionInfo").Return(&cadvisorapi.VersionInfo{}, nil)
|
||||||
|
testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorapi.MachineInfo{}, nil)
|
||||||
|
testKubelet.fakeCadvisor.On("ImagesFsInfo").Return(cadvisorapiv2.FsInfo{}, nil)
|
||||||
|
testKubelet.fakeCadvisor.On("RootFsInfo").Return(cadvisorapiv2.FsInfo{}, nil)
|
||||||
|
|
||||||
|
kubelet := testKubelet.kubelet
|
||||||
|
|
||||||
|
capabilities.SetForTests(capabilities.Capabilities{
|
||||||
|
PrivilegedSources: capabilities.PrivilegedSources{
|
||||||
|
HostPIDSources: []string{kubetypes.ApiserverSource, kubetypes.FileSource},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
pod := podWithUidNameNsSpec("12345678", "foo", "new", v1.PodSpec{
|
||||||
|
Containers: []v1.Container{
|
||||||
|
{Name: "foo"},
|
||||||
|
},
|
||||||
|
HostPID: true,
|
||||||
|
})
|
||||||
|
pod.Annotations[kubetypes.ConfigSourceAnnotationKey] = kubetypes.FileSource
|
||||||
|
|
||||||
|
kubelet.podManager.SetPods([]*v1.Pod{pod})
|
||||||
|
err := kubelet.syncPod(syncPodOptions{
|
||||||
|
pod: pod,
|
||||||
|
podStatus: &kubecontainer.PodStatus{},
|
||||||
|
updateType: kubetypes.SyncPodUpdate,
|
||||||
|
})
|
||||||
|
assert.NoError(t, err, "expected pod infra creation to succeed")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHostPIDDisallowed(t *testing.T) {
|
||||||
|
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
||||||
|
defer testKubelet.Cleanup()
|
||||||
|
testKubelet.fakeCadvisor.On("Start").Return(nil)
|
||||||
|
testKubelet.fakeCadvisor.On("VersionInfo").Return(&cadvisorapi.VersionInfo{}, nil)
|
||||||
|
testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorapi.MachineInfo{}, nil)
|
||||||
|
testKubelet.fakeCadvisor.On("ImagesFsInfo").Return(cadvisorapiv2.FsInfo{}, nil)
|
||||||
|
testKubelet.fakeCadvisor.On("RootFsInfo").Return(cadvisorapiv2.FsInfo{}, nil)
|
||||||
|
|
||||||
|
kubelet := testKubelet.kubelet
|
||||||
|
|
||||||
|
capabilities.SetForTests(capabilities.Capabilities{
|
||||||
|
PrivilegedSources: capabilities.PrivilegedSources{
|
||||||
|
HostPIDSources: []string{},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
pod := podWithUidNameNsSpec("12345678", "foo", "new", v1.PodSpec{
|
||||||
|
Containers: []v1.Container{
|
||||||
|
{Name: "foo"},
|
||||||
|
},
|
||||||
|
HostPID: true,
|
||||||
|
})
|
||||||
|
pod.Annotations[kubetypes.ConfigSourceAnnotationKey] = kubetypes.FileSource
|
||||||
|
|
||||||
|
err := kubelet.syncPod(syncPodOptions{
|
||||||
|
pod: pod,
|
||||||
|
podStatus: &kubecontainer.PodStatus{},
|
||||||
|
updateType: kubetypes.SyncPodUpdate,
|
||||||
|
})
|
||||||
|
assert.Error(t, err, "expected pod infra creation to fail")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHostIPCAllowed(t *testing.T) {
|
||||||
|
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
||||||
|
defer testKubelet.Cleanup()
|
||||||
|
testKubelet.fakeCadvisor.On("Start").Return(nil)
|
||||||
|
testKubelet.fakeCadvisor.On("VersionInfo").Return(&cadvisorapi.VersionInfo{}, nil)
|
||||||
|
testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorapi.MachineInfo{}, nil)
|
||||||
|
testKubelet.fakeCadvisor.On("ImagesFsInfo").Return(cadvisorapiv2.FsInfo{}, nil)
|
||||||
|
testKubelet.fakeCadvisor.On("RootFsInfo").Return(cadvisorapiv2.FsInfo{}, nil)
|
||||||
|
|
||||||
|
kubelet := testKubelet.kubelet
|
||||||
|
|
||||||
|
capabilities.SetForTests(capabilities.Capabilities{
|
||||||
|
PrivilegedSources: capabilities.PrivilegedSources{
|
||||||
|
HostIPCSources: []string{kubetypes.ApiserverSource, kubetypes.FileSource},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
pod := podWithUidNameNsSpec("12345678", "foo", "new", v1.PodSpec{
|
||||||
|
Containers: []v1.Container{
|
||||||
|
{Name: "foo"},
|
||||||
|
},
|
||||||
|
HostIPC: true,
|
||||||
|
})
|
||||||
|
pod.Annotations[kubetypes.ConfigSourceAnnotationKey] = kubetypes.FileSource
|
||||||
|
|
||||||
|
kubelet.podManager.SetPods([]*v1.Pod{pod})
|
||||||
|
err := kubelet.syncPod(syncPodOptions{
|
||||||
|
pod: pod,
|
||||||
|
podStatus: &kubecontainer.PodStatus{},
|
||||||
|
updateType: kubetypes.SyncPodUpdate,
|
||||||
|
})
|
||||||
|
assert.NoError(t, err, "expected pod infra creation to succeed")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHostIPCDisallowed(t *testing.T) {
|
||||||
|
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
||||||
|
defer testKubelet.Cleanup()
|
||||||
|
testKubelet.fakeCadvisor.On("Start").Return(nil)
|
||||||
|
testKubelet.fakeCadvisor.On("VersionInfo").Return(&cadvisorapi.VersionInfo{}, nil)
|
||||||
|
testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorapi.MachineInfo{}, nil)
|
||||||
|
testKubelet.fakeCadvisor.On("ImagesFsInfo").Return(cadvisorapiv2.FsInfo{}, nil)
|
||||||
|
testKubelet.fakeCadvisor.On("RootFsInfo").Return(cadvisorapiv2.FsInfo{}, nil)
|
||||||
|
|
||||||
|
kubelet := testKubelet.kubelet
|
||||||
|
|
||||||
|
capabilities.SetForTests(capabilities.Capabilities{
|
||||||
|
PrivilegedSources: capabilities.PrivilegedSources{
|
||||||
|
HostIPCSources: []string{},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
pod := podWithUidNameNsSpec("12345678", "foo", "new", v1.PodSpec{
|
||||||
|
Containers: []v1.Container{
|
||||||
|
{Name: "foo"},
|
||||||
|
},
|
||||||
|
HostIPC: true,
|
||||||
|
})
|
||||||
|
pod.Annotations[kubetypes.ConfigSourceAnnotationKey] = kubetypes.FileSource
|
||||||
|
|
||||||
|
err := kubelet.syncPod(syncPodOptions{
|
||||||
|
pod: pod,
|
||||||
|
podStatus: &kubecontainer.PodStatus{},
|
||||||
|
updateType: kubetypes.SyncPodUpdate,
|
||||||
|
})
|
||||||
|
assert.Error(t, err, "expected pod infra creation to fail")
|
||||||
|
}
|
||||||
|
|
||||||
func TestPrivilegeContainerAllowed(t *testing.T) {
|
func TestPrivilegeContainerAllowed(t *testing.T) {
|
||||||
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
testKubelet := newTestKubelet(t, false /* controllerAttachDetachEnabled */)
|
||||||
defer testKubelet.Cleanup()
|
defer testKubelet.Cleanup()
|
||||||
|
Loading…
Reference in New Issue
Block a user