// Copyright 2020 The Prometheus Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package eureka import ( "context" "io" "net/http" "net/http/httptest" "testing" "github.com/stretchr/testify/assert" ) func TestFetchApps(t *testing.T) { appsXML := ` 1 UP_4_ CONFIG-SERVICE config-service001.test.com:config-service:8080 config-service001.test.com CONFIG-SERVICE 192.133.83.31 UP UNKNOWN 8080 8080 1 MyOwn 30 90 1596003469304 1596110179310 0 1547190033103 config-service001.test.com:config-service:8080 http://config-service001.test.com:8080/ http://config-service001.test.com:8080/info http://config-service001.test.com 8080/health config-service false 1596003469304 1596003469304 ADDED config-service002.test.com:config-service:8080 config-service002.test.com CONFIG-SERVICE 192.133.83.31 UP UNKNOWN 8080 8080 1 MyOwn 30 90 1596003469304 1596110179310 0 1547190033103 config-service002.test.com:config-service:8080 http://config-service002.test.com:8080/ http://config-service002.test.com:8080/info http://config-service002.test.com:8080/health config-service false 1596003469304 1596003469304 ADDED META-SERVICE meta-service002.test.com:meta-service:8080 meta-service002.test.com META-SERVICE 192.133.87.237 UP UNKNOWN 8080 443 1 MyOwn 30 90 1535444352472 1596110168846 0 1535444352472 meta-service 8090 http://meta-service002.test.com:8080/ http://meta-service002.test.com:8080/info http://meta-service002.test.com:8080/health meta-service meta-service false 1535444352472 1535444352398 ADDED meta-service001.test.com:meta-service:8080 meta-service001.test.com META-SERVICE 192.133.87.236 UP UNKNOWN 8080 443 1 MyOwn 30 90 1535444352472 1596110168846 0 1535444352472 meta-service 8090 http://meta-service001.test.com:8080/ http://meta-service001.test.com:8080/info http://meta-service001.test.com:8080/health meta-service meta-service false 1535444352472 1535444352398 ADDED ` // Simulate apps with a valid XML response. respHandler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/xml") io.WriteString(w, appsXML) } // Create a test server with mock HTTP handler. ts := httptest.NewServer(http.HandlerFunc(respHandler)) defer ts.Close() apps, err := fetchApps(context.TODO(), ts.URL, &http.Client{}) assert.NoError(t, err) assert.Equal(t, len(apps.Applications), 2) assert.Equal(t, apps.Applications[0].Name, "CONFIG-SERVICE") assert.Equal(t, apps.Applications[1].Name, "META-SERVICE") assert.Equal(t, len(apps.Applications[1].Instances), 2) assert.Equal(t, apps.Applications[1].Instances[0].InstanceID, "meta-service002.test.com:meta-service:8080") assert.Equal(t, apps.Applications[1].Instances[0].Metadata.Items[0].XMLName.Local, "project") assert.Equal(t, apps.Applications[1].Instances[0].Metadata.Items[0].Content, "meta-service") assert.Equal(t, apps.Applications[1].Instances[0].Metadata.Items[1].XMLName.Local, "management.port") assert.Equal(t, apps.Applications[1].Instances[0].Metadata.Items[1].Content, "8090") assert.Equal(t, apps.Applications[1].Instances[1].InstanceID, "meta-service001.test.com:meta-service:8080") } func Test500ErrorHttpResponse(t *testing.T) { // Simulate 500 error. respHandler := func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) w.Header().Set("Content-Type", "application/xml") io.WriteString(w, ``) } // Create a test server with mock HTTP handler. ts := httptest.NewServer(http.HandlerFunc(respHandler)) defer ts.Close() _, err := fetchApps(context.TODO(), ts.URL, &http.Client{}) assert.Error(t, err, "5xx HTTP response") }