-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
RefreshablePeerEurekaNodesTests.java
252 lines (214 loc) · 10.9 KB
/
RefreshablePeerEurekaNodesTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright 2013-2020 the original author or 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
*
* https://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 org.springframework.cloud.netflix.eureka.server;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.discovery.EurekaClientConfig;
import com.netflix.eureka.EurekaServerConfig;
import com.netflix.eureka.cluster.PeerEurekaNodes;
import com.netflix.eureka.registry.PeerAwareInstanceRegistry;
import com.netflix.eureka.resources.ServerCodecs;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
import org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration.RefreshablePeerEurekaNodes;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author Fahim Farook
*/
@SpringBootTest(classes = RefreshablePeerEurekaNodesTests.Application.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, value = { "spring.application.name=eureka-server",
"eureka.client.service-url.defaultZone=http://localhost:8678/eureka/" })
class RefreshablePeerEurekaNodesTests {
@Autowired
private ConfigurableApplicationContext context;
@Autowired
private PeerEurekaNodes peerEurekaNodes;
@Value("${local.server.port}")
private int port = 0;
private static final String DEFAULT_ZONE = "eureka.client.service-url.defaultZone";
private static final String REGION = "eureka.client.region";
private static final String USE_DNS = "eureka.client.use-dns-for-fetching-service-urls";
@Test
void notUpdatedWhenDnsIsTrue() {
changeProperty("eureka.client.use-dns-for-fetching-service-urls=true",
"eureka.client.region=unavailable-region", // to force defaultZone
"eureka.client.service-url.defaultZone=https://default-host1:8678/eureka/");
this.context.publishEvent(new EnvironmentChangeEvent(new HashSet<>(Arrays.asList(USE_DNS, DEFAULT_ZONE))));
assertThat(serviceUrlMatches("https://default-host1:8678/eureka/"))
.as("PeerEurekaNodes' are updated when eureka.client.use-dns-for-fetching-service-urls is true")
.isFalse();
}
@Test
@Disabled("boot env ordering see https://github.com/spring-cloud/spring-cloud-netflix/issues/4048")
void updatedWhenDnsIsFalse() {
changeProperty("eureka.client.use-dns-for-fetching-service-urls=false",
"eureka.client.region=unavailable-region", // to force defaultZone
"eureka.client.service-url.defaultZone=https://default-host2:8678/eureka/");
this.context.publishEvent(new EnvironmentChangeEvent(new HashSet<>(Arrays.asList(USE_DNS, DEFAULT_ZONE))));
assertThat(serviceUrlMatches("https://default-host2:8678/eureka/"))
.as("PeerEurekaNodes' are not updated when eureka.client.use-dns-for-fetching-service-urls is false")
.isTrue();
}
@Test
void updatedWhenRegionChanged() {
changeProperty("eureka.client.use-dns-for-fetching-service-urls=false", "eureka.client.region=region1",
"eureka.client.availability-zones.region1=region1-zone",
"eureka.client.availability-zones.region2=region2-zone",
"eureka.client.service-url.region1-zone=https://region1-zone-host:8678/eureka/",
"eureka.client.service-url.region2-zone=https://region2-zone-host:8678/eureka/");
this.context.publishEvent(new EnvironmentChangeEvent(Collections.singleton(REGION)));
assertThat(serviceUrlMatches("https://region1-zone-host:8678/eureka/"))
.as("PeerEurekaNodes' are not updated when eureka.client.region is changed").isTrue();
changeProperty("eureka.client.region=region2");
this.context.publishEvent(new EnvironmentChangeEvent(Collections.singleton(REGION)));
assertThat(serviceUrlMatches("https://region2-zone-host:8678/eureka/"))
.as("PeerEurekaNodes' are not updated when eureka.client.region is changed").isTrue();
}
@Test
@Disabled("boot env ordering see https://github.com/spring-cloud/spring-cloud-netflix/issues/4048")
void updatedWhenAvailabilityZoneChanged() {
changeProperty("eureka.client.use-dns-for-fetching-service-urls=false", "eureka.client.region=region4",
"eureka.client.availability-zones.region3=region3-zone",
"eureka.client.service-url.region4-zone=https://region4-zone-host:8678/eureka/",
"eureka.client.service-url.defaultZone=https://default-host3:8678/eureka/");
this.context.publishEvent(
new EnvironmentChangeEvent(Collections.singleton("eureka.client.availability-zones.region3")));
assertThat(this.peerEurekaNodes.getPeerEurekaNodes().get(0).getServiceUrl()
.equals("https://default-host3:8678/eureka/")).isTrue();
changeProperty("eureka.client.availability-zones.region4=region4-zone");
this.context.publishEvent(
new EnvironmentChangeEvent(Collections.singleton("eureka.client.availability-zones.region4")));
assertThat(serviceUrlMatches("https://region4-zone-host:8678/eureka/"))
.as("PeerEurekaNodes' are not updated when eureka.client.availability-zones are changed").isTrue();
}
@Test
void notUpdatedWhenIrrelevantPropertiesChanged() {
// Only way to test this is verifying whether updatePeerEurekaNodes() is invoked.
// PeerEurekaNodes.updatePeerEurekaNodes() is not public, hence cannot verify with
// Mockito.
class VerifyablePeerEurekNode extends RefreshablePeerEurekaNodes {
VerifyablePeerEurekNode(PeerAwareInstanceRegistry registry, EurekaServerConfig serverConfig,
EurekaClientConfig clientConfig, ServerCodecs serverCodecs,
ApplicationInfoManager applicationInfoManager) {
super(registry, serverConfig, clientConfig, serverCodecs, applicationInfoManager,
new ReplicationClientAdditionalFilters(Collections.emptySet()));
}
protected void updatePeerEurekaNodes(List<String> newPeerUrls) {
super.updatePeerEurekaNodes(newPeerUrls);
}
}
// Create stubs.
final EurekaClientConfigBean configClientBean = mock(EurekaClientConfigBean.class);
when(configClientBean.isUseDnsForFetchingServiceUrls()).thenReturn(false);
final VerifyablePeerEurekNode mock = spy(new VerifyablePeerEurekNode(null, null, configClientBean, null, null));
mock.onApplicationEvent(new EnvironmentChangeEvent(Collections.singleton("some.irrelevant.property")));
verify(mock, never()).updatePeerEurekaNodes(anyList());
}
@Test
void peerEurekaNodesIsRefreshablePeerEurekaNodes() {
assertThat(this.peerEurekaNodes).isNotNull();
assertThat(this.peerEurekaNodes instanceof RefreshablePeerEurekaNodes)
.as("PeerEurekaNodes should be an instance of RefreshablePeerEurekaNodes").isTrue();
}
@Test
@Disabled("boot env ordering see https://github.com/spring-cloud/spring-cloud-netflix/issues/4048")
void serviceUrlsCountAsSoonAsRefreshed() {
changeProperty(
"eureka.client.service-url.defaultZone=https://defaul-host3:8678/eureka/,http://defaul-host4:8678/eureka/");
forceUpdate();
assertThat(this.peerEurekaNodes.getPeerEurekaNodes().size()).as("PeerEurekaNodes' peer count is incorrect.")
.isEqualTo(2);
}
@Test
@Disabled("boot env ordering see https://github.com/spring-cloud/spring-cloud-netflix/issues/4048")
void serviceUrlsValueAsSoonAsRefreshed() {
changeProperty("eureka.client.service-url.defaultZone=https://defaul-host4:8678/eureka/");
forceUpdate();
assertThat(serviceUrlMatches("https://defaul-host4:8678/eureka/"))
.as("PeerEurekaNodes' new peer[0] is incorrect").isTrue();
}
@Test
@Disabled("boot env ordering see https://github.com/spring-cloud/spring-cloud-netflix/issues/4048")
void dashboardUpdatedAsSoonAsRefreshed() {
changeProperty("eureka.client.service-url.defaultZone=https://defaul-host5:8678/eureka/");
forceUpdate();
final ResponseEntity<String> entity = new TestRestTemplate().getForEntity("http://localhost:" + this.port + "/",
String.class);
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
final String body = entity.getBody();
assertThat(body).isNotNull();
assertThat(body.contains("https://defaul-host5:8678/eureka/"))
.as("DS Replicas not updated in the Eureka Server dashboard").isTrue();
}
@Test
void notUpdatedForRelaxedKeys() {
changeProperty("eureka.client.use-dns-for-fetching-service-urls=false",
"eureka.client.region=unavailable-region", // to force defaultZone
"eureka.client.service-url.defaultZone=https://defaul-host6:8678/eureka/");
this.context.publishEvent(
new EnvironmentChangeEvent(Collections.singleton("eureka.client.serviceUrl.defaultZone")));
assertThat(serviceUrlMatches("https://defaul-host6:8678/eureka/"))
.as("PeerEurekaNodes' are updated for keys with relaxed binding").isFalse();
}
/*
* Changes the value of given key in the environment.
*/
private void changeProperty(final String... pairs) {
TestPropertyValues.of(pairs).applyTo(this.context);
}
/*
* Refreshes the context with properties satisfying to invoke update.
*/
private void forceUpdate() {
changeProperty("eureka.client.use-dns-for-fetching-service-urls=false",
"eureka.client.region=unavailable-region"); // to force defaultZone
this.context.publishEvent(
new EnvironmentChangeEvent(Collections.singleton("eureka.client.service-url.defaultZone")));
}
/*
* Whether the first element in PeerEurekaNodes matches the given url.
*/
private boolean serviceUrlMatches(final String serviceUrl) {
return this.peerEurekaNodes.getPeerEurekaNodes().get(0).getServiceUrl().equals(serviceUrl);
}
@EnableEurekaServer
@Configuration(proxyBeanMethods = false)
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class })
protected static class Application {
}
}