52
52
53
53
public class FileBasedUnicastHostsProviderTests extends ESTestCase {
54
54
55
+ private boolean legacyLocation ;
55
56
private ThreadPool threadPool ;
56
57
private ExecutorService executorService ;
57
58
private MockTransportService transportService ;
59
+ private Path configPath ;
58
60
59
61
@ Before
60
62
public void setUp () throws Exception {
@@ -78,23 +80,20 @@ public void tearDown() throws Exception {
78
80
79
81
@ Before
80
82
public void createTransportSvc () {
81
- MockTcpTransport transport =
82
- new MockTcpTransport (Settings .EMPTY ,
83
- threadPool ,
84
- BigArrays .NON_RECYCLING_INSTANCE ,
85
- new NoneCircuitBreakerService (),
86
- new NamedWriteableRegistry (Collections .emptyList ()),
87
- new NetworkService (Collections .emptyList ())) {
88
- @ Override
89
- public BoundTransportAddress boundAddress () {
90
- return new BoundTransportAddress (
91
- new TransportAddress []{new TransportAddress (InetAddress .getLoopbackAddress (), 9300 )},
92
- new TransportAddress (InetAddress .getLoopbackAddress (), 9300 )
93
- );
94
- }
95
- };
83
+ final MockTcpTransport transport = new MockTcpTransport (Settings .EMPTY , threadPool , BigArrays .NON_RECYCLING_INSTANCE ,
84
+ new NoneCircuitBreakerService (),
85
+ new NamedWriteableRegistry (Collections .emptyList ()),
86
+ new NetworkService (Collections .emptyList ())) {
87
+ @ Override
88
+ public BoundTransportAddress boundAddress () {
89
+ return new BoundTransportAddress (
90
+ new TransportAddress []{new TransportAddress (InetAddress .getLoopbackAddress (), 9300 )},
91
+ new TransportAddress (InetAddress .getLoopbackAddress (), 9300 )
92
+ );
93
+ }
94
+ };
96
95
transportService = new MockTransportService (Settings .EMPTY , transport , threadPool , TransportService .NOOP_TRANSPORT_INTERCEPTOR ,
97
- null );
96
+ null );
98
97
}
99
98
100
99
public void testBuildDynamicNodes () throws Exception {
@@ -109,16 +108,26 @@ public void testBuildDynamicNodes() throws Exception {
109
108
assertEquals (9300 , nodes .get (2 ).getPort ());
110
109
}
111
110
111
+ public void testBuildDynamicNodesLegacyLocation () throws Exception {
112
+ legacyLocation = true ;
113
+ testBuildDynamicNodes ();
114
+ assertDeprecatedLocationWarning ();
115
+ }
116
+
112
117
public void testEmptyUnicastHostsFile () throws Exception {
113
118
final List <String > hostEntries = Collections .emptyList ();
114
119
final List <TransportAddress > addresses = setupAndRunHostProvider (hostEntries );
115
120
assertEquals (0 , addresses .size ());
116
121
}
117
122
118
- public void testUnicastHostsDoesNotExist () throws Exception {
119
- final Settings settings = Settings .builder ()
120
- .put (Environment .PATH_HOME_SETTING .getKey (), createTempDir ())
121
- .build ();
123
+ public void testEmptyUnicastHostsFileLegacyLocation () throws Exception {
124
+ legacyLocation = true ;
125
+ testEmptyUnicastHostsFile ();
126
+ assertDeprecatedLocationWarning ();
127
+ }
128
+
129
+ public void testUnicastHostsDoesNotExist () {
130
+ final Settings settings = Settings .builder ().put (Environment .PATH_HOME_SETTING .getKey (), createTempDir ()).build ();
122
131
final FileBasedUnicastHostsProvider provider = new FileBasedUnicastHostsProvider (settings , createTempDir ().toAbsolutePath ());
123
132
final List <TransportAddress > addresses = provider .buildDynamicHosts ((hosts , limitPortCounts ) ->
124
133
UnicastZenPing .resolveHostsLists (executorService , logger , hosts , limitPortCounts , transportService ,
@@ -127,41 +136,60 @@ public void testUnicastHostsDoesNotExist() throws Exception {
127
136
}
128
137
129
138
public void testInvalidHostEntries () throws Exception {
130
- List <String > hostEntries = Arrays .asList ("192.168.0.1:9300:9300" );
131
- List <TransportAddress > addresses = setupAndRunHostProvider (hostEntries );
139
+ final List <String > hostEntries = Arrays .asList ("192.168.0.1:9300:9300" );
140
+ final List <TransportAddress > addresses = setupAndRunHostProvider (hostEntries );
132
141
assertEquals (0 , addresses .size ());
133
142
}
134
143
144
+ public void testInvalidHostEntriesLegacyLocation () throws Exception {
145
+ legacyLocation = true ;
146
+ testInvalidHostEntries ();
147
+ assertDeprecatedLocationWarning ();
148
+ }
149
+
135
150
public void testSomeInvalidHostEntries () throws Exception {
136
- List <String > hostEntries = Arrays .asList ("192.168.0.1:9300:9300" , "192.168.0.1:9301" );
137
- List <TransportAddress > addresses = setupAndRunHostProvider (hostEntries );
151
+ final List <String > hostEntries = Arrays .asList ("192.168.0.1:9300:9300" , "192.168.0.1:9301" );
152
+ final List <TransportAddress > addresses = setupAndRunHostProvider (hostEntries );
138
153
assertEquals (1 , addresses .size ()); // only one of the two is valid and will be used
139
154
assertEquals ("192.168.0.1" , addresses .get (0 ).getAddress ());
140
155
assertEquals (9301 , addresses .get (0 ).getPort ());
141
156
}
142
157
158
+ public void testSomeInvalidHostEntriesLegacyLocation () throws Exception {
159
+ legacyLocation = true ;
160
+ testSomeInvalidHostEntries ();
161
+ assertDeprecatedLocationWarning ();
162
+ }
163
+
143
164
// sets up the config dir, writes to the unicast hosts file in the config dir,
144
165
// and then runs the file-based unicast host provider to get the list of discovery nodes
145
166
private List <TransportAddress > setupAndRunHostProvider (final List <String > hostEntries ) throws IOException {
146
167
final Path homeDir = createTempDir ();
147
168
final Settings settings = Settings .builder ()
148
- .put (Environment .PATH_HOME_SETTING .getKey (), homeDir )
149
- .build ();
150
- final Path configPath ;
169
+ .put (Environment .PATH_HOME_SETTING .getKey (), homeDir )
170
+ .build ();
151
171
if (randomBoolean ()) {
152
172
configPath = homeDir .resolve ("config" );
153
173
} else {
154
174
configPath = createTempDir ();
155
175
}
156
- final Path discoveryFilePath = configPath .resolve ("discovery-file" );
176
+ final Path discoveryFilePath = legacyLocation ? configPath .resolve ("discovery-file" ) : configPath ;
157
177
Files .createDirectories (discoveryFilePath );
158
178
final Path unicastHostsPath = discoveryFilePath .resolve (UNICAST_HOSTS_FILE );
159
179
try (BufferedWriter writer = Files .newBufferedWriter (unicastHostsPath )) {
160
180
writer .write (String .join ("\n " , hostEntries ));
161
181
}
162
182
163
183
return new FileBasedUnicastHostsProvider (settings , configPath ).buildDynamicHosts ((hosts , limitPortCounts ) ->
164
- UnicastZenPing .resolveHostsLists (executorService , logger , hosts , limitPortCounts , transportService ,
165
- TimeValue .timeValueSeconds (10 )));
184
+ UnicastZenPing .resolveHostsLists (executorService , logger , hosts , limitPortCounts , transportService ,
185
+ TimeValue .timeValueSeconds (10 )));
186
+ }
187
+
188
+ private void assertDeprecatedLocationWarning () {
189
+ assertWarnings ("Found dynamic hosts list at [" +
190
+ configPath .resolve ("discovery-file" ).resolve (UNICAST_HOSTS_FILE ) +
191
+ "] but this path is deprecated. This list should be at [" +
192
+ configPath .resolve (UNICAST_HOSTS_FILE ) +
193
+ "] instead. Support for the deprecated path will be removed in future." );
166
194
}
167
195
}
0 commit comments