Exemplo n.º 1
0
// instance:delete
func TestEurekaInstanceDelete(t *testing.T) {
	cases := []struct {
		iid      string // input service identifier
		expected int    // expected result
	}{
		{"http-1", http.StatusOK},
		{"http-2", http.StatusOK},
		{"http-2", http.StatusGone}, // duplicate delete should fail
		{"http-3", http.StatusGone}, // unknown instance id should fail
	}

	c := defaultServerConfig()
	c.Registry.(*mockCatalog).prepopulateInstances(eurekaInstances)
	handler, err := setupServer(c)
	assert.Nil(t, err)

	for _, tc := range cases {
		recorder := httptest.NewRecorder()

		req, err := http.NewRequest("DELETE", serverURL+eureka.InstanceURL("", "http", tc.iid), nil)
		assert.Nil(t, err)
		req.Header.Set("Content-Type", "application/json")
		handler.ServeHTTP(recorder, req)
		assert.Equal(t, tc.expected, recorder.Code, string(tc.iid))
	}

	recorder := httptest.NewRecorder()

	req, err := http.NewRequest("GET", serverURL+eureka.ApplicationURL("", "http"), nil)
	assert.Nil(t, err)
	req.Header.Set("Content-Type", "application/json")
	handler.ServeHTTP(recorder, req)
	assert.Equal(t, http.StatusOK, recorder.Code)
}
Exemplo n.º 2
0
// apps/<name>
func TestEurekaAppInstances(t *testing.T) {
	cases := []struct {
		sname    string // input service name
		nInsts   int    // number of returned instances
		expected int    // expected result
	}{
		{"http", len(eurekaInstances), http.StatusOK},
		{"https", 0, http.StatusOK}, // non-existing service
	}

	c := defaultServerConfig()
	c.Registry.(*mockCatalog).prepopulateInstances(eurekaInstances)
	handler, err := setupServer(c)
	assert.Nil(t, err)

	for _, tc := range cases {
		recorder := httptest.NewRecorder()
		req, err := http.NewRequest("GET", serverURL+eureka.ApplicationURL("", tc.sname), nil)
		assert.Nil(t, err)
		req.Header.Set("Content-Type", "application/json")
		handler.ServeHTTP(recorder, req)
		assert.Equal(t, tc.expected, recorder.Code, string(tc.sname))
		if tc.expected == http.StatusOK {
			var m map[string]eureka.Application

			err = json.Unmarshal(recorder.Body.Bytes(), &m)
			assert.NoError(t, err)
			app := m["application"]
			assert.NotNil(t, app)
			assert.Equal(t, app.Name, tc.sname)
			if tc.nInsts > 0 {
				assert.NotNil(t, app.Instances)
				assert.Equal(t, len(instances), len(app.Instances))
				for _, inst := range app.Instances {
					assert.EqualValues(t, "STARTING", inst.Status)
					assert.EqualValues(t, metadata, inst.Metadata)
				}
			} else {
				assert.Nil(t, app.Instances)
			}

		}
	}
}
Exemplo n.º 3
0
func TestEurekaInstancesCreate(t *testing.T) {
	invalidMetadata := json.RawMessage("{\"INVALID\":\"INVALID\"}")

	cases := []createEurekaTestCase{
		newCreateEurekaTestCase("", "http", "192.168.1.1", "http-vip", "8080", metadata, http.StatusBadRequest),                 // empty hostname
		newCreateEurekaTestCase("localhost", "http", "", "http-vip", "8080", metadata, http.StatusBadRequest),                   // empty IP address
		newCreateEurekaTestCase("localhost", "http", "192.168.1.1", "", "8080", metadata, http.StatusBadRequest),                // empty VIPaddr
		newCreateEurekaTestCase("localhost", "http", "192.168.1.1", "http-vip", "8080", invalidMetadata, http.StatusBadRequest), // invalid metadata
		newCreateEurekaTestCase("localhost", "http", "192.168.1.1", "http-vip", "8080", metadata, http.StatusNoContent),         // valid
	}

	c := defaultServerConfig()
	handler, err := setupServer(c)
	assert.Nil(t, err)

	url := serverURL + eureka.ApplicationURL("", "http")

	for _, tc := range cases {
		recorder := httptest.NewRecorder()
		b, err := json.Marshal(&eureka.InstanceWrapper{&tc.instance})
		assert.NoError(t, err)

		if reflect.DeepEqual(tc.instance.Metadata, invalidMetadata) {
			b = tc.toByteswithFaultyMetadata()
		}

		req, err := http.NewRequest("POST", url, bytes.NewReader(b))

		assert.Nil(t, err)
		req.Header.Set("Content-Type", "application/json")
		handler.ServeHTTP(recorder, req)
		assert.Equal(t, tc.expected, recorder.Code, string(b), "\nResponse:", string(recorder.Body.Bytes()))
		if recorder.Code == http.StatusCreated { // verify links
			reply := &eureka.Instance{}

			err = json.Unmarshal(recorder.Body.Bytes(), &reply)
			assert.NoError(t, err)
		}
	}
}