mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-22 10:06:15 +00:00
kubelet: Move TestSyncPodWithTerminationLog() to dockertools package.
Update the fakeOptionGenerator to create the pod container directory, so that fake docker client can see and record the termination log path.
This commit is contained in:
parent
6ddffdd736
commit
91e198cb70
@ -19,7 +19,9 @@ package dockertools
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
@ -84,8 +86,19 @@ type fakeOptionGenerator struct{}
|
||||
|
||||
var _ kubecontainer.RunContainerOptionsGenerator = &fakeOptionGenerator{}
|
||||
|
||||
var testPodContainerDir string
|
||||
|
||||
func (*fakeOptionGenerator) GenerateRunContainerOptions(pod *api.Pod, container *api.Container) (*kubecontainer.RunContainerOptions, error) {
|
||||
return &kubecontainer.RunContainerOptions{}, nil
|
||||
var opts kubecontainer.RunContainerOptions
|
||||
var err error
|
||||
if len(container.TerminationMessagePath) != 0 {
|
||||
testPodContainerDir, err = ioutil.TempDir("", "fooPodContainerDir")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opts.PodContainerDir = testPodContainerDir
|
||||
}
|
||||
return &opts, nil
|
||||
}
|
||||
|
||||
func newTestDockerManagerWithHTTPClient(fakeHTTPClient *fakeHTTP) (*DockerManager, *FakeDockerClient) {
|
||||
@ -1937,3 +1950,45 @@ func TestPortForwardNoSuchContainer(t *testing.T) {
|
||||
t.Fatalf("expected %v, but saw %v", expectedErr, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncPodWithTerminationLog(t *testing.T) {
|
||||
dm, fakeDocker := newTestDockerManager()
|
||||
container := api.Container{
|
||||
Name: "bar",
|
||||
TerminationMessagePath: "/dev/somepath",
|
||||
}
|
||||
fakeDocker.ContainerList = []docker.APIContainers{}
|
||||
pod := &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
UID: "12345678",
|
||||
Name: "foo",
|
||||
Namespace: "new",
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
container,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
runSyncPod(t, dm, fakeDocker, pod)
|
||||
verifyCalls(t, fakeDocker, []string{
|
||||
// Create pod infra container.
|
||||
"create", "start", "inspect_container",
|
||||
// Create container.
|
||||
"create", "start", "inspect_container",
|
||||
})
|
||||
|
||||
defer os.Remove(testPodContainerDir)
|
||||
|
||||
fakeDocker.Lock()
|
||||
defer fakeDocker.Unlock()
|
||||
|
||||
parts := strings.Split(fakeDocker.Container.HostConfig.Binds[0], ":")
|
||||
if !matchString(t, testPodContainerDir+"/k8s_bar\\.[a-f0-9]", parts[0]) {
|
||||
t.Errorf("Unexpected host path: %s", parts[0])
|
||||
}
|
||||
if parts[1] != "/dev/somepath" {
|
||||
t.Errorf("Unexpected container path: %s", parts[1])
|
||||
}
|
||||
}
|
||||
|
@ -448,62 +448,6 @@ func generatePodInfraContainerHash(pod *api.Pod) uint64 {
|
||||
return kubecontainer.HashContainer(container)
|
||||
}
|
||||
|
||||
func TestSyncPodsWithTerminationLog(t *testing.T) {
|
||||
testKubelet := newTestKubelet(t)
|
||||
testKubelet.fakeCadvisor.On("MachineInfo").Return(&cadvisorApi.MachineInfo{}, nil)
|
||||
testKubelet.fakeCadvisor.On("DockerImagesFsInfo").Return(cadvisorApiv2.FsInfo{}, nil)
|
||||
testKubelet.fakeCadvisor.On("RootFsInfo").Return(cadvisorApiv2.FsInfo{}, nil)
|
||||
kubelet := testKubelet.kubelet
|
||||
fakeDocker := testKubelet.fakeDocker
|
||||
container := api.Container{
|
||||
Name: "bar",
|
||||
TerminationMessagePath: "/dev/somepath",
|
||||
}
|
||||
fakeDocker.ContainerList = []docker.APIContainers{}
|
||||
pods := []*api.Pod{
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
UID: "12345678",
|
||||
Name: "foo",
|
||||
Namespace: "new",
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
container,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
kubelet.podManager.SetPods(pods)
|
||||
err := kubelet.SyncPods(pods, emptyPodUIDs, map[string]*api.Pod{}, time.Now())
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
verifyCalls(t, fakeDocker, []string{
|
||||
"list", "list",
|
||||
// Get pod status.
|
||||
"list", "inspect_image",
|
||||
// Create pod infra container.
|
||||
"create", "start", "inspect_container",
|
||||
// Create container.
|
||||
"create", "start", "inspect_container",
|
||||
// Get pod status.
|
||||
"list", "inspect_container", "inspect_container",
|
||||
// Get pods for deleting orphaned volumes.
|
||||
"list",
|
||||
})
|
||||
|
||||
fakeDocker.Lock()
|
||||
parts := strings.Split(fakeDocker.Container.HostConfig.Binds[0], ":")
|
||||
if !matchString(t, kubelet.getPodContainerDir("12345678", "bar")+"/k8s_bar\\.[a-f0-9]", parts[0]) {
|
||||
t.Errorf("Unexpected host path: %s", parts[0])
|
||||
}
|
||||
if parts[1] != "/dev/somepath" {
|
||||
t.Errorf("Unexpected container path: %s", parts[1])
|
||||
}
|
||||
fakeDocker.Unlock()
|
||||
}
|
||||
|
||||
func matchString(t *testing.T, pattern, str string) bool {
|
||||
match, err := regexp.MatchString(pattern, str)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user