// 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/prometheus/client_golang/prometheus" "github.com/prometheus/common/model" "github.com/stretchr/testify/require" "github.com/prometheus/prometheus/discovery" "github.com/prometheus/prometheus/discovery/targetgroup" ) func testUpdateServices(respHandler http.HandlerFunc) ([]*targetgroup.Group, error) { // Create a test server with mock HTTP handler. ts := httptest.NewServer(respHandler) defer ts.Close() conf := SDConfig{ Server: ts.URL, } reg := prometheus.NewRegistry() refreshMetrics := discovery.NewRefreshMetrics(reg) metrics := conf.NewDiscovererMetrics(reg, refreshMetrics) err := metrics.Register() if err != nil { return nil, err } defer metrics.Unregister() defer refreshMetrics.Unregister() md, err := NewDiscovery(&conf, nil, metrics) if err != nil { return nil, err } return md.refresh(context.Background()) } func TestEurekaSDHandleError(t *testing.T) { var ( errTesting = "non 2xx status '500' response during eureka service discovery" respHandler = func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) w.Header().Set("Content-Type", "application/xml") io.WriteString(w, ``) } ) tgs, err := testUpdateServices(respHandler) require.EqualError(t, err, errTesting) require.Empty(t, tgs) } func TestEurekaSDEmptyList(t *testing.T) { var ( appsXML = ` 1 ` respHandler = func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/xml") io.WriteString(w, appsXML) } ) tgs, err := testUpdateServices(respHandler) require.NoError(t, err) require.Len(t, tgs, 1) } func TestEurekaSDSendGroup(t *testing.T) { var ( 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 ` respHandler = func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Header().Set("Content-Type", "application/xml") io.WriteString(w, appsXML) } ) tgs, err := testUpdateServices(respHandler) require.NoError(t, err) require.Len(t, tgs, 1) tg := tgs[0] require.Equal(t, "eureka", tg.Source) require.Len(t, tg.Targets, 4) tgt := tg.Targets[0] require.Equal(t, tgt[model.AddressLabel], model.LabelValue("config-service001.test.com:8080")) tgt = tg.Targets[2] require.Equal(t, tgt[model.AddressLabel], model.LabelValue("meta-service002.test.com:8080")) }