Example #1
0
func TestGetEtcd(t *testing.T) {
	fakeClient := tools.MakeFakeEtcdClient(t)
	ch := make(chan interface{}, 1)
	manifest := api.ContainerManifest{ID: "foo", Version: "v1beta1", Containers: []api.Container{{Name: "1", Image: "foo"}}}
	fakeClient.Data["/registry/hosts/machine/kubelet"] = tools.EtcdResponseWithError{
		R: &etcd.Response{
			Node: &etcd.Node{
				Value: api.EncodeOrDie(&api.ContainerManifestList{
					Items: []api.ContainerManifest{manifest},
				}),
				ModifiedIndex: 1,
			},
		},
		E: nil,
	}
	c := SourceEtcd{"/registry/hosts/machine/kubelet", fakeClient, ch, time.Millisecond}
	lastIndex, err := c.fetchNextState(0)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}
	if lastIndex != 2 {
		t.Errorf("Expected %#v, Got %#v", 2, lastIndex)
	}
	update := (<-ch).(kubelet.PodUpdate)
	expected := CreatePodUpdate(kubelet.SET, kubelet.Pod{Name: "foo", Manifest: manifest})
	if !reflect.DeepEqual(expected, update) {
		t.Errorf("Expected %#v, Got %#v", expected, update)
	}
	for i := range update.Pods {
		if errs := kubelet.ValidatePod(&update.Pods[i]); len(errs) != 0 {
			t.Errorf("Expected no validation errors on %#v, Got %#v", update.Pods[i], errs)
		}
	}
}
func TestExtractFromHttpMultiple(t *testing.T) {
	manifests := []api.ContainerManifest{
		{Version: "v1beta1", ID: "", Containers: []api.Container{{Name: "1", Image: "foo"}}},
		{Version: "v1beta1", ID: "bar", Containers: []api.Container{{Name: "1", Image: "foo"}}},
	}
	data, err := json.Marshal(manifests)
	if err != nil {
		t.Fatalf("Some weird json problem: %v", err)
	}

	fakeHandler := util.FakeHandler{
		StatusCode:   200,
		ResponseBody: string(data),
	}
	testServer := httptest.NewServer(&fakeHandler)
	ch := make(chan interface{}, 1)
	c := SourceURL{testServer.URL, ch}

	err = c.extractFromURL()
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	update := (<-ch).(kubelet.PodUpdate)
	expected := CreatePodUpdate(kubelet.SET, kubelet.Pod{Name: "1", Manifest: manifests[0]}, kubelet.Pod{Name: "bar", Manifest: manifests[1]})
	if !reflect.DeepEqual(expected, update) {
		t.Errorf("Expected: %#v, Got: %#v", expected, update)
	}
	for i := range update.Pods {
		if errs := kubelet.ValidatePod(&update.Pods[i]); len(errs) != 0 {
			t.Errorf("Expected no validation errors on %#v, Got %#v", update.Pods[i], errs)
		}
	}
}
func TestExtractFromDir(t *testing.T) {
	manifests := []api.ContainerManifest{
		{Version: "v1beta1", Containers: []api.Container{{Name: "1", Image: "foo"}}},
		{Version: "v1beta1", Containers: []api.Container{{Name: "2", Image: "bar"}}},
	}
	files := make([]*os.File, len(manifests))

	dirName, err := ioutil.TempDir("", "foo")
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	for i, manifest := range manifests {
		data, err := json.Marshal(manifest)
		if err != nil {
			t.Errorf("Unexpected error: %v", err)
		}
		file, err := ioutil.TempFile(dirName, manifest.ID)
		if err != nil {
			t.Errorf("Unexpected error: %v", err)
		}
		name := file.Name()
		if err := file.Close(); err != nil {
			t.Errorf("Unexpected error: %v", err)
		}
		ioutil.WriteFile(name, data, 0755)
		files[i] = file
	}

	ch := make(chan interface{}, 1)
	c := SourceFile{dirName, ch}
	err = c.extractFromPath()
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	update := (<-ch).(kubelet.PodUpdate)
	expected := CreatePodUpdate(
		kubelet.SET,
		kubelet.Pod{Name: simpleSubdomainSafeHash(files[0].Name()), Manifest: manifests[0]},
		kubelet.Pod{Name: simpleSubdomainSafeHash(files[1].Name()), Manifest: manifests[1]},
	)
	sort.Sort(sortedPods(update.Pods))
	sort.Sort(sortedPods(expected.Pods))
	if !reflect.DeepEqual(expected, update) {
		t.Errorf("Expected %#v, Got %#v", expected, update)
	}
	for i := range update.Pods {
		if errs := kubelet.ValidatePod(&update.Pods[i]); len(errs) != 0 {
			t.Errorf("Expected no validation errors on %#v, Got %#v", update.Pods[i], errs)
		}
	}
}
Example #4
0
func filterInvalidPods(pods []kubelet.Pod, source string) (filtered []*kubelet.Pod) {
	names := util.StringSet{}
	for i := range pods {
		var errors []error
		if names.Has(pods[i].Name) {
			errors = append(errors, api.ValidationError{api.ErrTypeDuplicate, "Pod.Name", pods[i].Name})
		} else {
			names.Insert(pods[i].Name)
		}
		if errs := kubelet.ValidatePod(&pods[i]); len(errs) != 0 {
			errors = append(errors, errs...)
		}
		if len(errors) > 0 {
			glog.Warningf("Pod %d from %s failed validation, ignoring: %v", i+1, source, errors)
			continue
		}
		filtered = append(filtered, &pods[i])
	}
	return
}
Example #5
0
func TestExtractFromHTTP(t *testing.T) {
	var testCases = []struct {
		desc      string
		manifests interface{}
		expected  kubelet.PodUpdate
	}{
		{
			desc:      "Single manifest",
			manifests: api.ContainerManifest{Version: "v1beta1", ID: "foo"},
			expected: CreatePodUpdate(kubelet.SET,
				kubelet.Pod{
					Name: "foo",
					Manifest: api.ContainerManifest{
						Version:       "v1beta1",
						ID:            "foo",
						RestartPolicy: api.RestartPolicy{Always: &api.RestartPolicyAlways{}},
					},
				}),
		},
		{
			desc: "Multiple manifests",
			manifests: []api.ContainerManifest{
				{Version: "v1beta1", ID: "", Containers: []api.Container{{Name: "1", Image: "foo"}}},
				{Version: "v1beta1", ID: "bar", Containers: []api.Container{{Name: "1", Image: "foo"}}},
			},
			expected: CreatePodUpdate(kubelet.SET,
				kubelet.Pod{
					Name:     "1",
					Manifest: api.ContainerManifest{Version: "v1beta1", ID: "", Containers: []api.Container{{Name: "1", Image: "foo"}}},
				},
				kubelet.Pod{
					Name:     "bar",
					Manifest: api.ContainerManifest{Version: "v1beta1", ID: "bar", Containers: []api.Container{{Name: "1", Image: "foo"}}},
				}),
		},
		{
			desc:      "Empty Array",
			manifests: []api.ContainerManifest{},
			expected:  CreatePodUpdate(kubelet.SET),
		},
	}
	for _, testCase := range testCases {
		data, err := json.Marshal(testCase.manifests)
		if err != nil {
			t.Fatalf("%s: Some weird json problem: %v", testCase.desc, err)
		}
		fakeHandler := util.FakeHandler{
			StatusCode:   200,
			ResponseBody: string(data),
		}
		testServer := httptest.NewServer(&fakeHandler)
		ch := make(chan interface{}, 1)
		c := SourceURL{testServer.URL, ch, nil}
		if err := c.extractFromURL(); err != nil {
			t.Errorf("%s: Unexpected error: %v", testCase.desc, err)
		}
		update := (<-ch).(kubelet.PodUpdate)
		if !reflect.DeepEqual(testCase.expected, update) {
			t.Errorf("%s: Expected: %#v, Got: %#v", testCase.desc, testCase.expected, update)
		}
		for i := range update.Pods {
			if errs := kubelet.ValidatePod(&update.Pods[i]); len(errs) != 0 {
				t.Errorf("%s: Expected no validation errors on %#v, Got %#v", testCase.desc, update.Pods[i], errs)
			}
		}
	}
}