Esempio n. 1
0
func TestContainer(t *testing.T) {
	fw := newServerTest()
	expected := []api.ContainerManifest{
		{
			ID:   "test_manifest",
			UUID: "value",
			Containers: []api.Container{
				{
					Name: "container",
				},
			},
			Volumes: []api.Volume{
				{
					Name: "test",
				},
			},
			RestartPolicy: api.RestartPolicy{
				Never: &api.RestartPolicyNever{},
			},
		},
	}
	body := bytes.NewBuffer([]byte(util.EncodeJSON(expected[0]))) // Only send a single ContainerManifest
	resp, err := http.Post(fw.testHTTPServer.URL+"/container", "application/json", body)
	if err != nil {
		t.Errorf("Post returned: %v", err)
	}
	resp.Body.Close()
	close(fw.updateChan)
	received := fw.updateReader.GetList()
	if len(received) != 1 {
		t.Errorf("Expected 1 manifest, but got %v", len(received))
	}
	expectedPods := []api.BoundPod{
		{
			ObjectMeta: api.ObjectMeta{
				Name: "test_manifest",
				UID:  "value",
			},
			Spec: api.PodSpec{
				Containers: []api.Container{
					{
						Name: "container",
					},
				},
				Volumes: []api.Volume{
					{
						Name: "test",
					},
				},
				RestartPolicy: api.RestartPolicy{
					Never: &api.RestartPolicyNever{},
				},
			},
		},
	}
	if !reflect.DeepEqual(expectedPods, received[0]) {
		t.Errorf("Expected %#v, but got %#v", expectedPods, received[0])
	}
}
func makeTestServer(t *testing.T, podResponse serverResponse, serviceResponse serverResponse) *httptest.Server {
	fakePodHandler := util.FakeHandler{
		StatusCode:   podResponse.statusCode,
		ResponseBody: util.EncodeJSON(podResponse.obj),
	}
	fakeServiceHandler := util.FakeHandler{
		StatusCode:   serviceResponse.statusCode,
		ResponseBody: util.EncodeJSON(serviceResponse.obj),
	}
	mux := http.NewServeMux()
	mux.Handle("/api/v1beta1/pods", &fakePodHandler)
	mux.Handle("/api/v1beta1/services", &fakeServiceHandler)
	mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
		t.Errorf("unexpected request: %v", req.RequestURI)
		res.WriteHeader(http.StatusNotFound)
	})
	return httptest.NewServer(mux)
}
Esempio n. 3
0
func TestExtractObj(t *testing.T) {
	fakeClient := NewFakeEtcdClient(t)
	expect := api.Pod{JSONBase: api.JSONBase{ID: "foo"}}
	fakeClient.Set("/some/key", util.EncodeJSON(expect), 0)
	helper := EtcdHelper{fakeClient, codec, versioner}
	var got api.Pod
	err := helper.ExtractObj("/some/key", &got, false)
	if err != nil {
		t.Errorf("Unexpected error %#v", err)
	}
	if !reflect.DeepEqual(got, expect) {
		t.Errorf("Wanted %#v, got %#v", expect, got)
	}
}
func makeTestServer(t *testing.T, minionResponse serverResponse) (*httptest.Server, *util.FakeHandler) {
	fakeMinionHandler := util.FakeHandler{
		StatusCode:   minionResponse.statusCode,
		ResponseBody: util.EncodeJSON(minionResponse.obj),
	}
	mux := http.NewServeMux()
	mux.Handle("/api/"+testapi.Version()+"/minions", &fakeMinionHandler)
	mux.Handle("/api/"+testapi.Version()+"/minions/", &fakeMinionHandler)
	mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
		t.Errorf("unexpected request: %v", req.RequestURI)
		res.WriteHeader(http.StatusNotFound)
	})
	return httptest.NewServer(mux), &fakeMinionHandler
}
Esempio n. 5
0
func TestContainer(t *testing.T) {
	fw := newServerTest()
	expected := []api.ContainerManifest{
		{ID: "test_manifest"},
	}
	body := bytes.NewBuffer([]byte(util.EncodeJSON(expected[0]))) // Only send a single ContainerManifest
	resp, err := http.Post(fw.testHTTPServer.URL+"/container", "application/json", body)
	if err != nil {
		t.Errorf("Post returned: %v", err)
	}
	resp.Body.Close()
	close(fw.updateChan)
	received := fw.updateReader.GetList()
	if len(received) != 1 {
		t.Errorf("Expected 1 manifest, but got %v", len(received))
	}
	expectedPods := []Pod{{Name: "1", Manifest: expected[0]}}
	if !reflect.DeepEqual(expectedPods, received[0]) {
		t.Errorf("Expected %#v, but got %#v", expectedPods, received[0])
	}
}
func TestSyncronize(t *testing.T) {
	controllerSpec1 := api.ReplicationController{
		JSONBase: api.JSONBase{APIVersion: "v1beta1"},
		DesiredState: api.ReplicationControllerState{
			Replicas: 4,
			PodTemplate: api.PodTemplate{
				DesiredState: api.PodState{
					Manifest: api.ContainerManifest{
						Containers: []api.Container{
							{
								Image: "foo/bar",
							},
						},
					},
				},
				Labels: map[string]string{
					"name": "foo",
					"type": "production",
				},
			},
		},
	}
	controllerSpec2 := api.ReplicationController{
		JSONBase: api.JSONBase{APIVersion: "v1beta1"},
		DesiredState: api.ReplicationControllerState{
			Replicas: 3,
			PodTemplate: api.PodTemplate{
				DesiredState: api.PodState{
					Manifest: api.ContainerManifest{
						Containers: []api.Container{
							{
								Image: "bar/baz",
							},
						},
					},
				},
				Labels: map[string]string{
					"name": "bar",
					"type": "production",
				},
			},
		},
	}

	fakeEtcd := tools.NewFakeEtcdClient(t)
	fakeEtcd.Data["/registry/controllers"] = tools.EtcdResponseWithError{
		R: &etcd.Response{
			Node: &etcd.Node{
				Nodes: []*etcd.Node{
					{
						Value: util.EncodeJSON(controllerSpec1),
					},
					{
						Value: util.EncodeJSON(controllerSpec2),
					},
				},
			},
		},
	}

	fakePodHandler := util.FakeHandler{
		StatusCode:   200,
		ResponseBody: "{\"apiVersion\": \"v1beta1\", \"kind\": \"PodList\"}",
		T:            t,
	}
	fakeControllerHandler := util.FakeHandler{
		StatusCode: 200,
		ResponseBody: runtime.DefaultScheme.EncodeOrDie(&api.ReplicationControllerList{
			Items: []api.ReplicationController{
				controllerSpec1,
				controllerSpec2,
			},
		}),
		T: t,
	}
	mux := http.NewServeMux()
	mux.Handle("/api/v1beta1/pods/", &fakePodHandler)
	mux.Handle("/api/v1beta1/replicationControllers/", &fakeControllerHandler)
	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		w.WriteHeader(http.StatusNotFound)
		t.Errorf("Unexpected request for %v", req.RequestURI)
	})
	testServer := httptest.NewServer(mux)
	client := client.NewOrDie(testServer.URL, nil)
	manager := NewReplicationManager(client)
	fakePodControl := FakePodControl{}
	manager.podControl = &fakePodControl

	manager.synchronize()

	validateSyncReplication(t, &fakePodControl, 7, 0)
}