Ejemplo n.º 1
0
func createFakeRuntimeCache(fakeRecorder *record.FakeRecorder) qingcontainer.RuntimeCache {
	fakeDocker := &dockertools.FakeDockerClient{}
	np, _ := network.InitNetworkPlugin([]network.NetworkPlugin{}, "", network.NewFakeHost(nil))
	dockerManager := dockertools.NewFakeDockerManager(fakeDocker, fakeRecorder, nil, nil, dockertools.PodInfraContainerImage, 0, 0, "", qingcontainer.FakeOS{}, np, nil, nil, newQingletRuntimeHooks(fakeRecorder))
	return qingcontainer.NewFakeRuntimeCache(dockerManager)
}
Ejemplo n.º 2
0
func TestRunOnce(t *testing.T) {
	cadvisor := &cadvisor.Mock{}
	cadvisor.On("MachineInfo").Return(&cadvisorApi.MachineInfo{}, nil)

	podManager, _ := newFakePodManager()

	kb := &Qinglet{
		rootDirectory:       "/tmp/qinglet",
		recorder:            &record.FakeRecorder{},
		cadvisor:            cadvisor,
		nodeLister:          testNodeLister{},
		statusManager:       newStatusManager(nil),
		containerRefManager: qingcontainer.NewRefManager(),
		readinessManager:    qingcontainer.NewReadinessManager(),
		podManager:          podManager,
		os:                  qingcontainer.FakeOS{},
		volumeManager:       newVolumeManager(),
	}
	kb.containerManager, _ = newContainerManager(cadvisor, "", "", "")

	kb.networkPlugin, _ = network.InitNetworkPlugin([]network.NetworkPlugin{}, "", network.NewFakeHost(nil))
	if err := kb.setupDataDirs(); err != nil {
		t.Errorf("Failed to init data dirs: %v", err)
	}
	podContainers := []docker.APIContainers{
		{
			Names:  []string{"/k8s_bar." + strconv.FormatUint(qingcontainer.HashContainer(&api.Container{Name: "bar"}), 16) + "_foo_new_12345678_42"},
			ID:     "1234",
			Status: "running",
		},
		{
			Names:  []string{"/k8s_net_foo.new.test_abcdefgh_42"},
			ID:     "9876",
			Status: "running",
		},
	}
	kb.dockerClient = &testDocker{
		listContainersResults: []listContainersResult{
			{label: "list pod container", containers: []docker.APIContainers{}},
			{label: "syncPod", containers: []docker.APIContainers{}},
			{label: "list pod container", containers: []docker.APIContainers{}},
			{label: "syncPod", containers: podContainers},
			{label: "list pod container", containers: podContainers},
			{label: "list pod container", containers: podContainers},
		},
		inspectContainersResults: []inspectContainersResult{
			{
				label: "syncPod",
				container: docker.Container{
					Config: &docker.Config{Image: "someimage"},
					State:  docker.State{Running: true, Pid: 42},
				},
			},
			{
				label: "syncPod",
				container: docker.Container{
					Config: &docker.Config{Image: "someimage"},
					State:  docker.State{Running: true, Pid: 42},
				},
			},
			{
				label: "syncPod",
				container: docker.Container{
					Config: &docker.Config{Image: "someimage"},
					State:  docker.State{Running: true, Pid: 42},
				},
			},
			{
				label: "syncPod",
				container: docker.Container{
					Config: &docker.Config{Image: "someimage"},
					State:  docker.State{Running: true, Pid: 42},
				},
			},
		},
		t: t,
	}

	kb.containerRuntime = dockertools.NewFakeDockerManager(
		kb.dockerClient,
		kb.recorder,
		kb.readinessManager,
		kb.containerRefManager,
		dockertools.PodInfraContainerImage,
		0,
		0,
		"",
		qingcontainer.FakeOS{},
		kb.networkPlugin,
		kb,
		nil,
		newQingletRuntimeHooks(kb.recorder))

	pods := []*api.Pod{
		{
			ObjectMeta: api.ObjectMeta{
				UID:       "12345678",
				Name:      "foo",
				Namespace: "new",
			},
			Spec: api.PodSpec{
				Containers: []api.Container{
					{Name: "bar"},
				},
			},
		},
	}
	podManager.SetPods(pods)
	results, err := kb.runOnce(pods, time.Millisecond)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}
	if results[0].Err != nil {
		t.Errorf("unexpected run pod error: %v", results[0].Err)
	}
	if results[0].Pod.Name != "foo" {
		t.Errorf("unexpected pod: %q", results[0].Pod.Name)
	}
}
Ejemplo n.º 3
0
// TestFakePodWorkers verifies that the fakePodWorkers behaves the same way as the real podWorkers
// for their invocation of the syncPodFn.
func TestFakePodWorkers(t *testing.T) {
	// Create components for pod workers.
	fakeDocker := &dockertools.FakeDockerClient{}
	fakeRecorder := &record.FakeRecorder{}
	np, _ := network.InitNetworkPlugin([]network.NetworkPlugin{}, "", network.NewFakeHost(nil))
	dockerManager := dockertools.NewFakeDockerManager(fakeDocker, fakeRecorder, nil, nil, dockertools.PodInfraContainerImage, 0, 0, "", qingcontainer.FakeOS{}, np, nil, nil, newQingletRuntimeHooks(fakeRecorder))
	fakeRuntimeCache := qingcontainer.NewFakeRuntimeCache(dockerManager)

	qingletForRealWorkers := &simpleFakeQinglet{}
	qingletForFakeWorkers := &simpleFakeQinglet{}

	realPodWorkers := newPodWorkers(fakeRuntimeCache, qingletForRealWorkers.syncPodWithWaitGroup, fakeRecorder)
	fakePodWorkers := &fakePodWorkers{qingletForFakeWorkers.syncPod, fakeRuntimeCache, t}

	tests := []struct {
		pod                    *api.Pod
		mirrorPod              *api.Pod
		containerList          []docker.APIContainers
		containersInRunningPod int
	}{
		{
			&api.Pod{},
			&api.Pod{},
			[]docker.APIContainers{},
			0,
		},

		{
			&api.Pod{
				ObjectMeta: api.ObjectMeta{
					UID:       "12345678",
					Name:      "foo",
					Namespace: "new",
				},
				Spec: api.PodSpec{
					Containers: []api.Container{
						{
							Name: "fooContainer",
						},
					},
				},
			},
			&api.Pod{
				ObjectMeta: api.ObjectMeta{
					UID:       "12345678",
					Name:      "fooMirror",
					Namespace: "new",
				},
				Spec: api.PodSpec{
					Containers: []api.Container{
						{
							Name: "fooContainerMirror",
						},
					},
				},
			},
			[]docker.APIContainers{
				{
					// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>_<random>
					Names: []string{"/k8s_bar.hash123_foo_new_12345678_0"},
					ID:    "1234",
				},
				{
					// pod infra container
					Names: []string{"/k8s_POD.hash123_foo_new_12345678_0"},
					ID:    "9876",
				},
			},
			2,
		},

		{
			&api.Pod{
				ObjectMeta: api.ObjectMeta{
					UID:       "98765",
					Name:      "bar",
					Namespace: "new",
				},
				Spec: api.PodSpec{
					Containers: []api.Container{
						{
							Name: "fooContainer",
						},
					},
				},
			},
			&api.Pod{
				ObjectMeta: api.ObjectMeta{
					UID:       "98765",
					Name:      "fooMirror",
					Namespace: "new",
				},
				Spec: api.PodSpec{
					Containers: []api.Container{
						{
							Name: "fooContainerMirror",
						},
					},
				},
			},
			[]docker.APIContainers{
				{
					// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>_<random>
					Names: []string{"/k8s_bar.hash123_bar_new_98765_0"},
					ID:    "1234",
				},
				{
					// pod infra container
					Names: []string{"/k8s_POD.hash123_foo_new_12345678_0"},
					ID:    "9876",
				},
			},
			1,
		},

		// Empty running pod.
		{
			&api.Pod{
				ObjectMeta: api.ObjectMeta{
					UID:       "98765",
					Name:      "baz",
					Namespace: "new",
				},
				Spec: api.PodSpec{
					Containers: []api.Container{
						{
							Name: "bazContainer",
						},
					},
				},
			},
			&api.Pod{
				ObjectMeta: api.ObjectMeta{
					UID:       "98765",
					Name:      "bazMirror",
					Namespace: "new",
				},
				Spec: api.PodSpec{
					Containers: []api.Container{
						{
							Name: "bazContainerMirror",
						},
					},
				},
			},
			[]docker.APIContainers{
				{
					// format is // k8s_<container-id>_<pod-fullname>_<pod-uid>_<random>
					Names: []string{"/k8s_bar.hash123_bar_new_12345678_0"},
					ID:    "1234",
				},
				{
					// pod infra container
					Names: []string{"/k8s_POD.hash123_foo_new_12345678_0"},
					ID:    "9876",
				},
			},
			0,
		},
	}

	for i, tt := range tests {
		qingletForRealWorkers.wg.Add(1)

		fakeDocker.ContainerList = tt.containerList
		realPodWorkers.UpdatePod(tt.pod, tt.mirrorPod, func() {})
		fakePodWorkers.UpdatePod(tt.pod, tt.mirrorPod, func() {})

		qingletForRealWorkers.wg.Wait()

		if !reflect.DeepEqual(qingletForRealWorkers.pod, qingletForFakeWorkers.pod) {
			t.Errorf("%d: Expected: %#v, Actual: %#v", i, qingletForRealWorkers.pod, qingletForFakeWorkers.pod)
		}

		if !reflect.DeepEqual(qingletForRealWorkers.mirrorPod, qingletForFakeWorkers.mirrorPod) {
			t.Errorf("%d: Expected: %#v, Actual: %#v", i, qingletForRealWorkers.mirrorPod, qingletForFakeWorkers.mirrorPod)
		}

		if tt.containersInRunningPod != len(qingletForFakeWorkers.runningPod.Containers) {
			t.Errorf("%d: Expected: %#v, Actual: %#v", i, tt.containersInRunningPod, len(qingletForFakeWorkers.runningPod.Containers))
		}

		sort.Sort(byContainerName(qingletForRealWorkers.runningPod))
		sort.Sort(byContainerName(qingletForFakeWorkers.runningPod))
		if !reflect.DeepEqual(qingletForRealWorkers.runningPod, qingletForFakeWorkers.runningPod) {
			t.Errorf("%d: Expected: %#v, Actual: %#v", i, qingletForRealWorkers.runningPod, qingletForFakeWorkers.runningPod)
		}
	}
}