Пример #1
0
// instance:delete
func TestInstanceDelete(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(instances)
	handler, err := setupServer(c)
	assert.Nil(t, err)

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

		req, err := http.NewRequest("DELETE", serverURL+amalgam8.InstanceURL(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+amalgam8.ServiceInstancesURL("http"), nil)
	assert.Nil(t, err)
	req.Header.Set("Content-Type", "application/json")
	handler.ServeHTTP(recorder, req)
	assert.Equal(t, http.StatusNotFound, recorder.Code)
}
Пример #2
0
//----------
// services
//----------
// services:methods
func TestServiceInstancesMethods(t *testing.T) {
	var methods = []string{"CONNECT", "HEAD", "OPTIONS", "PATCH", "POST", "PUT", "TRACE"}

	url := serverURL + amalgam8.ServiceInstancesURL("fakeservice")
	c := defaultServerConfig()
	handler, err := setupServer(c)
	assert.Nil(t, err)

	for _, method := range methods {
		recorder := httptest.NewRecorder()
		req, err := http.NewRequest(method, url, nil)
		assert.Nil(t, err)
		handler.ServeHTTP(recorder, req)
		assert.Equal(t, http.StatusMethodNotAllowed, recorder.Code, method+":"+url)
	}
}
Пример #3
0
// services/<name>:list
func TestServiceInstances(t *testing.T) {
	cases := []struct {
		sname    string // input service name
		expected int    // expected result
	}{
		{"http", http.StatusOK},
		{"", http.StatusNotFound},      // empty service name
		{"https", http.StatusNotFound}, // non-existing service
	}

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

	for _, tc := range cases {
		recorder := httptest.NewRecorder()
		req, err := http.NewRequest("GET", serverURL+amalgam8.ServiceInstancesURL(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 {
			svc := amalgam8.InstanceList{}

			err = json.Unmarshal(recorder.Body.Bytes(), &svc)
			assert.NoError(t, err)
			assert.Equal(t, svc.ServiceName, tc.sname)
			assert.NotNil(t, svc.Instances)
			assert.Equal(t, len(instances), len(svc.Instances))
			for _, inst := range svc.Instances {
				assert.EqualValues(t, "UP", inst.Status)
				assert.NotNil(t, inst.LastHeartbeat)
				assert.EqualValues(t, metadata, inst.Metadata)
			}
		}
	}
}
Пример #4
0
func lookup(t *testing.T, port uint16, sname string) int {
	url := fmt.Sprintf("http://localhost:%d%s", port, amalgam8.ServiceInstancesURL(sname))
	req, err1 := http.NewRequest("GET", url, nil)
	assert.NoError(t, err1)
	assert.NotNil(t, req)

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("X-Forwarded-Proto", "https")

	res, err2 := http.DefaultClient.Do(req)
	assert.NoError(t, err2)
	assert.NotNil(t, res)
	assert.EqualValues(t, 200, res.StatusCode)

	data, err3 := ioutil.ReadAll(res.Body)
	assert.NoError(t, err3)
	res.Body.Close()

	var slist amalgam8.InstanceList
	err1 = json.Unmarshal(data, &slist)
	assert.NoError(t, err1)
	return len(slist.Instances)
}