func TestKubePodMetricsFull(t *testing.T) { nodeList := nodes.NodeList{ Items: map[nodes.Host]nodes.Info{ nodes.Host("test-machine-b"): {InternalIP: "10.10.10.1"}, nodes.Host("test-machine-1"): {InternalIP: "10.10.10.0"}, }, } podList := []api.Pod{ { PodMetadata: api.PodMetadata{ Name: "blah", }, }, { PodMetadata: api.PodMetadata{ Name: "blah1", }, }, } container := &api.Container{ Name: "test", } nodesApi := &fakeNodesApi{nodeList} podsApi := &fakePodsApi{podList} kubeletApi := &fakeKubeletApi{container: container} source := NewKubePodMetrics(10250, kubeletApi, nodesApi, podsApi) data, err := source.GetInfo(time.Now(), time.Now().Add(time.Minute), time.Second, false) require.NoError(t, err) require.NotEmpty(t, data) }
func TestWorkflowSuccess(t *testing.T) { type cadvisorData struct { subcontainers []*api.Container root *api.Container } hostA := datasource.Host{IP: "1.1.1.1", Port: "8080", Resource: ""} hostB := datasource.Host{IP: "1.1.1.2", Port: "8080", Resource: ""} expectedData := map[datasource.Host]cadvisorData{ hostA: { subcontainers: []*api.Container{{Name: "/a"}}, root: &api.Container{Name: "/"}, }, hostB: { subcontainers: []*api.Container{{Name: "/b"}}, root: &api.Container{Name: "/"}, }, } cadvisorApi := &fakeDataSource{ f: func(host datasource.Host, numStats int) (subcontainers []*api.Container, root *api.Container, err error) { data, exists := expectedData[host] if !exists { return nil, nil, fmt.Errorf("unexpected host: %+v", host) } return data.subcontainers, data.root, nil }, } nodeList := nodes.NodeList{ Items: map[nodes.Host]nodes.Info{ nodes.Host("a"): {InternalIP: "1.1.1.1"}, nodes.Host("b"): {InternalIP: "1.1.1.2"}, }, } source := &cadvisorSource{ nodesApi: &fakeNodesApi{nodeList}, pollDuration: 1 * time.Second, cadvisorPort: "8080", lastQuery: time.Now(), cadvisorApi: cadvisorApi, } data, err := source.GetInfo() require.NoError(t, err) assert.Len(t, data.Containers, 2) assert.Len(t, data.Machine, 2) assert.Equal(t, data.Machine[0].Name, expectedData[hostA].root.Name) assert.Equal(t, data.Machine[1].Name, expectedData[hostB].root.Name) containerNames := []string{ data.Containers[0].Name, data.Containers[1].Name, } assert.Contains(t, containerNames, expectedData[hostA].subcontainers[0].Name) assert.Contains(t, containerNames, expectedData[hostB].subcontainers[0].Name) }
// Returns a map of minion hostnames to the Pods running in them. func (self *realPodsApi) List(nodeList *nodes.NodeList) ([]api.Pod, error) { pods, err := self.podLister.List(labels.Everything()) if err != nil { return []api.Pod{}, err } glog.V(5).Infof("got pods from api server %+v", pods) selectedPods := []podNodePair{} // TODO(vishh): Avoid this loop by setting a node selector on the watcher. for i, pod := range pods { if nodeInfo, ok := nodeList.Items[nodes.Host(pod.Spec.NodeName)]; ok { nsObj, exists, err := self.namespaceStore.GetByKey(pod.Namespace) if err != nil { return []api.Pod{}, err } if !exists { glog.V(2).Infof("Ignoring pod %s with namespace %s since namespace object was not found", pod.Name, pod.Namespace) continue } ns, ok := nsObj.(*kapi.Namespace) if !ok { glog.V(2).Infof("Ignoring pod %s with namespace %s since casting to namespace object failed - %T, %v", pod.Name, pod.Namespace, ns, ns) continue } selectedPods = append(selectedPods, podNodePair{pods[i], &nodeInfo, ns}) } else { glog.V(2).Infof("pod %q with host %q and hostip %q not found in nodeList", pod.Name, pod.Spec.NodeName, pod.Status.HostIP) } } glog.V(4).Infof("selected pods from api server %+v", selectedPods) return self.parseAllPods(selectedPods), nil }
func TestKubeSourceDetail(t *testing.T) { nodeList := nodes.NodeList{ Items: map[nodes.Host]nodes.Info{ nodes.Host("test-machine-b"): {InternalIP: "10.10.10.1"}, nodes.Host("test-machine-1"): {InternalIP: "10.10.10.0"}, }, } podList := []api.Pod{ { Name: "blah", }, { Name: "blah1", }, } container := &api.Container{ Name: "test", } nodesApi := &fakeNodesApi{nodeList} podsApi := &fakePodsApi{podList} kubeletApi := &fakeKubeletApi{container} eventsList := []kubeapi.Event{ { Reason: "event 1", }, { Reason: "event 2", }, } eventsApi := &fakeEventsApi{eventsList} kubeSource := &kubeSource{ lastQuery: time.Now(), kubeletPort: "10250", nodesApi: nodesApi, podsApi: podsApi, kubeletApi: kubeletApi, eventsApi: eventsApi, } data, err := kubeSource.GetInfo() require.NoError(t, err) require.NotEmpty(t, data) }
// Returns a map of minion hostnames to the Pods running in them. func (self *realPodsApi) List(nodeList *nodes.NodeList) ([]api.Pod, error) { pods, err := self.podLister.List(labels.Everything()) if err != nil { return []api.Pod{}, err } glog.V(3).Infof("got pods from api server %+v", pods) selectedPods := []podNodePair{} // TODO(vishh): Avoid this loop by setting a node selector on the watcher. for i, pod := range pods { if nodeInfo, ok := nodeList.Items[nodes.Host(pod.Status.Host)]; ok { selectedPods = append(selectedPods, podNodePair{&pods[i], &nodeInfo}) } else { glog.V(2).Infof("pod %q with host %q and hostip %q not found in nodeList", pod.Name, pod.Status.Host, pod.Status.HostIP) } } glog.V(3).Infof("selected pods from api server %+v", selectedPods) return self.parseAllPods(selectedPods), nil }
func TestPodsParsing(t *testing.T) { podList := &kube_api.PodList{ Items: []kube_api.Pod{ { Status: kube_api.PodStatus{ Phase: kube_api.PodRunning, PodIP: "1.2.3.4", Host: "test-machine-a", HostIP: "10.10.10.0", }, ObjectMeta: kube_api.ObjectMeta{ Name: "pod1", Namespace: "test", UID: "x-y-z", Labels: map[string]string{ "foo": "bar", "name": "baz", }, }, Spec: kube_api.PodSpec{ Containers: []kube_api.Container{ {Name: "test1"}, {Name: "test2"}, }, }, }, { Status: kube_api.PodStatus{ Phase: kube_api.PodRunning, PodIP: "1.2.3.5", Host: "test-machine-b", HostIP: "10.10.10.1", }, ObjectMeta: kube_api.ObjectMeta{ Name: "pod2", Namespace: "test", UID: "x-y-a", Labels: map[string]string{ "foo": "bar", "name": "baz", }, }, Spec: kube_api.PodSpec{ Containers: []kube_api.Container{ {Name: "test1"}, {Name: "test2"}, }, }, }, }, } handler := util.FakeHandler{ StatusCode: 200, RequestBody: "something", ResponseBody: body(podList), T: t, } server := httptest.NewServer(&handler) defer server.Close() client := client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()}) podsApi := newPodsApi(client) nodeList := &nodes.NodeList{ Items: map[nodes.Host]nodes.Info{ nodes.Host("test-machine-b"): {PublicIP: "10.10.10.1"}, nodes.Host("test-machine-1"): {PublicIP: "10.10.10.0"}, }, } pods, err := podsApi.List(nodeList) require.NoError(t, err) for i, pod := range pods { assert.Equal(t, pod.Name, podList.Items[i].Name) assert.Equal(t, pod.Namespace, podList.Items[i].Namespace) assert.Equal(t, pod.ID, podList.Items[i].UID) assert.Equal(t, pod.Hostname, podList.Items[i].Status.Host) assert.Equal(t, pod.PodIP, podList.Items[i].Status.PodIP) assert.Equal(t, pod.Status, podList.Items[i].Status.Phase) assert.Equal(t, pod.Labels, podList.Items[i].Labels) for idx, container := range pod.Containers { assert.Equal(t, container.Name, podList.Items[i].Spec.Containers[idx].Name) } } }