prometheus

Форк
0
/
client_test.go 
213 строк · 9.0 Кб
1
// Copyright 2020 The Prometheus Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13

14
package eureka
15

16
import (
17
	"context"
18
	"io"
19
	"net/http"
20
	"net/http/httptest"
21
	"testing"
22

23
	"github.com/stretchr/testify/require"
24
)
25

26
func TestFetchApps(t *testing.T) {
27
	appsXML := `<applications>
28
  <versions__delta>1</versions__delta>
29
  <apps__hashcode>UP_4_</apps__hashcode>
30
  <application>
31
    <name>CONFIG-SERVICE</name>
32
    <instance>
33
      <instanceId>config-service001.test.com:config-service:8080</instanceId>
34
      <hostName>config-service001.test.com</hostName>
35
      <app>CONFIG-SERVICE</app>
36
      <ipAddr>192.133.83.31</ipAddr>
37
      <status>UP</status>
38
      <overriddenstatus>UNKNOWN</overriddenstatus>
39
      <port enabled="true">8080</port>
40
      <securePort enabled="false">8080</securePort>
41
      <countryId>1</countryId>
42
      <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
43
        <name>MyOwn</name>
44
      </dataCenterInfo>
45
      <leaseInfo>
46
        <renewalIntervalInSecs>30</renewalIntervalInSecs>
47
        <durationInSecs>90</durationInSecs>
48
        <registrationTimestamp>1596003469304</registrationTimestamp>
49
        <lastRenewalTimestamp>1596110179310</lastRenewalTimestamp>
50
        <evictionTimestamp>0</evictionTimestamp>
51
        <serviceUpTimestamp>1547190033103</serviceUpTimestamp>
52
      </leaseInfo>
53
      <metadata>
54
        <instanceId>config-service001.test.com:config-service:8080</instanceId>
55
      </metadata>
56
      <homePageUrl>http://config-service001.test.com:8080/</homePageUrl>
57
      <statusPageUrl>http://config-service001.test.com:8080/info</statusPageUrl>
58
      <healthCheckUrl>http://config-service001.test.com 8080/health</healthCheckUrl>
59
      <vipAddress>config-service</vipAddress>
60
      <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
61
      <lastUpdatedTimestamp>1596003469304</lastUpdatedTimestamp>
62
      <lastDirtyTimestamp>1596003469304</lastDirtyTimestamp>
63
      <actionType>ADDED</actionType>
64
    </instance>
65
    <instance>
66
      <instanceId>config-service002.test.com:config-service:8080</instanceId>
67
      <hostName>config-service002.test.com</hostName>
68
      <app>CONFIG-SERVICE</app>
69
      <ipAddr>192.133.83.31</ipAddr>
70
      <status>UP</status>
71
      <overriddenstatus>UNKNOWN</overriddenstatus>
72
      <port enabled="true">8080</port>
73
      <securePort enabled="false">8080</securePort>
74
      <countryId>1</countryId>
75
      <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
76
        <name>MyOwn</name>
77
      </dataCenterInfo>
78
      <leaseInfo>
79
        <renewalIntervalInSecs>30</renewalIntervalInSecs>
80
        <durationInSecs>90</durationInSecs>
81
        <registrationTimestamp>1596003469304</registrationTimestamp>
82
        <lastRenewalTimestamp>1596110179310</lastRenewalTimestamp>
83
        <evictionTimestamp>0</evictionTimestamp>
84
        <serviceUpTimestamp>1547190033103</serviceUpTimestamp>
85
      </leaseInfo>
86
      <metadata>
87
        <instanceId>config-service002.test.com:config-service:8080</instanceId>
88
      </metadata>
89
      <homePageUrl>http://config-service002.test.com:8080/</homePageUrl>
90
      <statusPageUrl>http://config-service002.test.com:8080/info</statusPageUrl>
91
      <healthCheckUrl>http://config-service002.test.com:8080/health</healthCheckUrl>
92
      <vipAddress>config-service</vipAddress>
93
      <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
94
      <lastUpdatedTimestamp>1596003469304</lastUpdatedTimestamp>
95
      <lastDirtyTimestamp>1596003469304</lastDirtyTimestamp>
96
      <actionType>ADDED</actionType>
97
    </instance>
98
  </application>
99
  <application>
100
    <name>META-SERVICE</name>
101
    <instance>
102
      <instanceId>meta-service002.test.com:meta-service:8080</instanceId>
103
      <hostName>meta-service002.test.com</hostName>
104
      <app>META-SERVICE</app>
105
      <ipAddr>192.133.87.237</ipAddr>
106
      <status>UP</status>
107
      <overriddenstatus>UNKNOWN</overriddenstatus>
108
      <port enabled="true">8080</port>
109
      <securePort enabled="false">443</securePort>
110
      <countryId>1</countryId>
111
      <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
112
        <name>MyOwn</name>
113
      </dataCenterInfo>
114
      <leaseInfo>
115
        <renewalIntervalInSecs>30</renewalIntervalInSecs>
116
        <durationInSecs>90</durationInSecs>
117
        <registrationTimestamp>1535444352472</registrationTimestamp>
118
        <lastRenewalTimestamp>1596110168846</lastRenewalTimestamp>
119
        <evictionTimestamp>0</evictionTimestamp>
120
        <serviceUpTimestamp>1535444352472</serviceUpTimestamp>
121
      </leaseInfo>
122
      <metadata>
123
        <project>meta-service</project>
124
        <management.port>8090</management.port>
125
      </metadata>
126
      <homePageUrl>http://meta-service002.test.com:8080/</homePageUrl>
127
      <statusPageUrl>http://meta-service002.test.com:8080/info</statusPageUrl>
128
      <healthCheckUrl>http://meta-service002.test.com:8080/health</healthCheckUrl>
129
      <vipAddress>meta-service</vipAddress>
130
      <secureVipAddress>meta-service</secureVipAddress>
131
      <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
132
      <lastUpdatedTimestamp>1535444352472</lastUpdatedTimestamp>
133
      <lastDirtyTimestamp>1535444352398</lastDirtyTimestamp>
134
      <actionType>ADDED</actionType>
135
    </instance>
136
    <instance>
137
      <instanceId>meta-service001.test.com:meta-service:8080</instanceId>
138
      <hostName>meta-service001.test.com</hostName>
139
      <app>META-SERVICE</app>
140
      <ipAddr>192.133.87.236</ipAddr>
141
      <status>UP</status>
142
      <overriddenstatus>UNKNOWN</overriddenstatus>
143
      <port enabled="true">8080</port>
144
      <securePort enabled="false">443</securePort>
145
      <countryId>1</countryId>
146
      <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">
147
        <name>MyOwn</name>
148
      </dataCenterInfo>
149
      <leaseInfo>
150
        <renewalIntervalInSecs>30</renewalIntervalInSecs>
151
        <durationInSecs>90</durationInSecs>
152
        <registrationTimestamp>1535444352472</registrationTimestamp>
153
        <lastRenewalTimestamp>1596110168846</lastRenewalTimestamp>
154
        <evictionTimestamp>0</evictionTimestamp>
155
        <serviceUpTimestamp>1535444352472</serviceUpTimestamp>
156
      </leaseInfo>
157
      <metadata>
158
        <project>meta-service</project>
159
        <management.port>8090</management.port>
160
      </metadata>
161
      <homePageUrl>http://meta-service001.test.com:8080/</homePageUrl>
162
      <statusPageUrl>http://meta-service001.test.com:8080/info</statusPageUrl>
163
      <healthCheckUrl>http://meta-service001.test.com:8080/health</healthCheckUrl>
164
      <vipAddress>meta-service</vipAddress>
165
      <secureVipAddress>meta-service</secureVipAddress>
166
      <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
167
      <lastUpdatedTimestamp>1535444352472</lastUpdatedTimestamp>
168
      <lastDirtyTimestamp>1535444352398</lastDirtyTimestamp>
169
      <actionType>ADDED</actionType>
170
    </instance>
171
  </application>
172
</applications>`
173

174
	// Simulate apps with a valid XML response.
175
	respHandler := func(w http.ResponseWriter, r *http.Request) {
176
		w.WriteHeader(http.StatusOK)
177
		w.Header().Set("Content-Type", "application/xml")
178
		io.WriteString(w, appsXML)
179
	}
180
	// Create a test server with mock HTTP handler.
181
	ts := httptest.NewServer(http.HandlerFunc(respHandler))
182
	defer ts.Close()
183

184
	apps, err := fetchApps(context.TODO(), ts.URL, &http.Client{})
185
	require.NoError(t, err)
186

187
	require.Len(t, apps.Applications, 2)
188
	require.Equal(t, "CONFIG-SERVICE", apps.Applications[0].Name)
189
	require.Equal(t, "META-SERVICE", apps.Applications[1].Name)
190

191
	require.Len(t, apps.Applications[1].Instances, 2)
192
	require.Equal(t, "meta-service002.test.com:meta-service:8080", apps.Applications[1].Instances[0].InstanceID)
193
	require.Equal(t, "project", apps.Applications[1].Instances[0].Metadata.Items[0].XMLName.Local)
194
	require.Equal(t, "meta-service", apps.Applications[1].Instances[0].Metadata.Items[0].Content)
195
	require.Equal(t, "management.port", apps.Applications[1].Instances[0].Metadata.Items[1].XMLName.Local)
196
	require.Equal(t, "8090", apps.Applications[1].Instances[0].Metadata.Items[1].Content)
197
	require.Equal(t, "meta-service001.test.com:meta-service:8080", apps.Applications[1].Instances[1].InstanceID)
198
}
199

200
func Test500ErrorHttpResponse(t *testing.T) {
201
	// Simulate 500 error.
202
	respHandler := func(w http.ResponseWriter, r *http.Request) {
203
		w.WriteHeader(http.StatusInternalServerError)
204
		w.Header().Set("Content-Type", "application/xml")
205
		io.WriteString(w, ``)
206
	}
207
	// Create a test server with mock HTTP handler.
208
	ts := httptest.NewServer(http.HandlerFunc(respHandler))
209
	defer ts.Close()
210

211
	_, err := fetchApps(context.TODO(), ts.URL, &http.Client{})
212
	require.Error(t, err, "5xx HTTP response")
213
}
214

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.