|
19 | 19 |
|
20 | 20 | package org.elasticsearch.discovery.ec2;
|
21 | 21 |
|
| 22 | +import com.sun.net.httpserver.HttpServer; |
| 23 | +import org.elasticsearch.common.Strings; |
| 24 | +import org.elasticsearch.common.SuppressForbidden; |
22 | 25 | import org.elasticsearch.common.network.NetworkService;
|
23 | 26 | import org.elasticsearch.common.settings.Settings;
|
| 27 | +import org.elasticsearch.mocksocket.MockHttpServer; |
| 28 | +import org.elasticsearch.rest.RestStatus; |
24 | 29 | import org.elasticsearch.test.ESTestCase;
|
| 30 | +import org.junit.AfterClass; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.BeforeClass; |
25 | 33 |
|
26 | 34 | import java.io.IOException;
|
| 35 | +import java.io.OutputStream; |
27 | 36 | import java.net.InetAddress;
|
| 37 | +import java.net.InetSocketAddress; |
| 38 | +import java.security.AccessController; |
| 39 | +import java.security.PrivilegedAction; |
| 40 | +import java.util.Arrays; |
28 | 41 | import java.util.Collections;
|
| 42 | +import java.util.function.BiConsumer; |
29 | 43 |
|
| 44 | +import static com.amazonaws.SDKGlobalConfiguration.EC2_METADATA_SERVICE_OVERRIDE_SYSTEM_PROPERTY; |
| 45 | +import static java.nio.charset.StandardCharsets.UTF_8; |
30 | 46 | import static org.hamcrest.Matchers.arrayContaining;
|
31 |
| -import static org.hamcrest.Matchers.containsString; |
| 47 | +import static org.hamcrest.Matchers.equalTo; |
32 | 48 |
|
33 | 49 | /**
|
34 | 50 | * Test for EC2 network.host settings.
|
35 | 51 | * <p>
|
36 | 52 | * Warning: This test doesn't assert that the exceptions are thrown.
|
37 | 53 | * They aren't.
|
38 | 54 | */
|
| 55 | +@SuppressForbidden(reason = "use http server") |
39 | 56 | public class Ec2NetworkTests extends ESTestCase {
|
| 57 | + |
| 58 | + private static HttpServer httpServer; |
| 59 | + |
| 60 | + @BeforeClass |
| 61 | + public static void startHttp() throws Exception { |
| 62 | + httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress().getHostAddress(), 0), 0); |
| 63 | + |
| 64 | + BiConsumer<String, String> registerContext = (path, v) ->{ |
| 65 | + final byte[] message = v.getBytes(UTF_8); |
| 66 | + httpServer.createContext(path, (s) -> { |
| 67 | + s.sendResponseHeaders(RestStatus.OK.getStatus(), message.length); |
| 68 | + OutputStream responseBody = s.getResponseBody(); |
| 69 | + responseBody.write(message); |
| 70 | + responseBody.close(); |
| 71 | + }); |
| 72 | + }; |
| 73 | + registerContext.accept("/latest/meta-data/local-ipv4","127.0.0.1"); |
| 74 | + registerContext.accept("/latest/meta-data/public-ipv4","165.168.10.2"); |
| 75 | + registerContext.accept("/latest/meta-data/public-hostname","165.168.10.3"); |
| 76 | + registerContext.accept("/latest/meta-data/local-hostname","10.10.10.5"); |
| 77 | + |
| 78 | + httpServer.start(); |
| 79 | + } |
| 80 | + |
| 81 | + @Before |
| 82 | + public void setup() { |
| 83 | + // redirect EC2 metadata service to httpServer |
| 84 | + AccessController.doPrivileged((PrivilegedAction<String>) () -> System.setProperty(EC2_METADATA_SERVICE_OVERRIDE_SYSTEM_PROPERTY, |
| 85 | + "http://" + httpServer.getAddress().getHostName() + ":" + httpServer.getAddress().getPort())); |
| 86 | + } |
| 87 | + |
| 88 | + @AfterClass |
| 89 | + public static void stopHttp() { |
| 90 | + httpServer.stop(0); |
| 91 | + httpServer = null; |
| 92 | + } |
| 93 | + |
40 | 94 | /**
|
41 | 95 | * Test for network.host: _ec2_
|
42 | 96 | */
|
43 | 97 | public void testNetworkHostEc2() throws IOException {
|
44 |
| - Settings nodeSettings = Settings.builder() |
45 |
| - .put("network.host", "_ec2_") |
46 |
| - .build(); |
| 98 | + resolveEc2("_ec2_", InetAddress.getByName("127.0.0.1")); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Test for network.host: _ec2_ |
| 103 | + */ |
| 104 | + public void testNetworkHostUnableToResolveEc2() { |
| 105 | + // redirect EC2 metadata service to unknown location |
| 106 | + AccessController.doPrivileged((PrivilegedAction<String>) () -> System.setProperty(EC2_METADATA_SERVICE_OVERRIDE_SYSTEM_PROPERTY, |
| 107 | + "http://127.0.0.1/")); |
47 | 108 |
|
48 |
| - NetworkService networkService = new NetworkService(Collections.singletonList(new Ec2NameResolver())); |
49 |
| - // TODO we need to replace that with a mock. For now we check the URL we are supposed to reach. |
50 | 109 | try {
|
51 |
| - networkService.resolveBindHostAddresses(null); |
52 |
| - // note: this can succeed and the test can pass |
| 110 | + resolveEc2("_ec2_", (InetAddress[]) null); |
53 | 111 | } catch (IOException e) {
|
54 |
| - assertThat(e.getMessage(), containsString("local-ipv4")); |
| 112 | + assertThat(e.getMessage(), |
| 113 | + equalTo("IOException caught when fetching InetAddress from [http://127.0.0.1//latest/meta-data/local-ipv4]")); |
55 | 114 | }
|
56 | 115 | }
|
57 | 116 |
|
58 | 117 | /**
|
59 | 118 | * Test for network.host: _ec2:publicIp_
|
60 | 119 | */
|
61 | 120 | public void testNetworkHostEc2PublicIp() throws IOException {
|
62 |
| - Settings nodeSettings = Settings.builder() |
63 |
| - .put("network.host", "_ec2:publicIp_") |
64 |
| - .build(); |
65 |
| - |
66 |
| - NetworkService networkService = new NetworkService(Collections.singletonList(new Ec2NameResolver())); |
67 |
| - // TODO we need to replace that with a mock. For now we check the URL we are supposed to reach. |
68 |
| - try { |
69 |
| - networkService.resolveBindHostAddresses(null); |
70 |
| - // note: this can succeed and the test can pass |
71 |
| - } catch (IOException e) { |
72 |
| - assertThat(e.getMessage(), containsString("public-ipv4")); |
73 |
| - } |
| 121 | + resolveEc2("_ec2:publicIp_", InetAddress.getByName("165.168.10.2")); |
74 | 122 | }
|
75 | 123 |
|
76 | 124 | /**
|
77 | 125 | * Test for network.host: _ec2:privateIp_
|
78 | 126 | */
|
79 | 127 | public void testNetworkHostEc2PrivateIp() throws IOException {
|
80 |
| - Settings nodeSettings = Settings.builder() |
81 |
| - .put("network.host", "_ec2:privateIp_") |
82 |
| - .build(); |
83 |
| - |
84 |
| - NetworkService networkService = new NetworkService(Collections.singletonList(new Ec2NameResolver())); |
85 |
| - // TODO we need to replace that with a mock. For now we check the URL we are supposed to reach. |
86 |
| - try { |
87 |
| - networkService.resolveBindHostAddresses(null); |
88 |
| - // note: this can succeed and the test can pass |
89 |
| - } catch (IOException e) { |
90 |
| - assertThat(e.getMessage(), containsString("local-ipv4")); |
91 |
| - } |
| 128 | + resolveEc2("_ec2:privateIp_", InetAddress.getByName("127.0.0.1")); |
92 | 129 | }
|
93 | 130 |
|
94 | 131 | /**
|
95 | 132 | * Test for network.host: _ec2:privateIpv4_
|
96 | 133 | */
|
97 | 134 | public void testNetworkHostEc2PrivateIpv4() throws IOException {
|
98 |
| - Settings nodeSettings = Settings.builder() |
99 |
| - .put("network.host", "_ec2:privateIpv4_") |
100 |
| - .build(); |
101 |
| - |
102 |
| - NetworkService networkService = new NetworkService(Collections.singletonList(new Ec2NameResolver())); |
103 |
| - // TODO we need to replace that with a mock. For now we check the URL we are supposed to reach. |
104 |
| - try { |
105 |
| - networkService.resolveBindHostAddresses(null); |
106 |
| - // note: this can succeed and the test can pass |
107 |
| - } catch (IOException e) { |
108 |
| - assertThat(e.getMessage(), containsString("local-ipv4")); |
109 |
| - } |
| 135 | + resolveEc2("_ec2:privateIpv4_", InetAddress.getByName("127.0.0.1")); |
110 | 136 | }
|
111 | 137 |
|
112 | 138 | /**
|
113 | 139 | * Test for network.host: _ec2:privateDns_
|
114 | 140 | */
|
115 | 141 | public void testNetworkHostEc2PrivateDns() throws IOException {
|
116 |
| - Settings nodeSettings = Settings.builder() |
117 |
| - .put("network.host", "_ec2:privateDns_") |
118 |
| - .build(); |
119 |
| - |
120 |
| - NetworkService networkService = new NetworkService(Collections.singletonList(new Ec2NameResolver())); |
121 |
| - // TODO we need to replace that with a mock. For now we check the URL we are supposed to reach. |
122 |
| - try { |
123 |
| - networkService.resolveBindHostAddresses(null); |
124 |
| - // note: this can succeed and the test can pass |
125 |
| - } catch (IOException e) { |
126 |
| - assertThat(e.getMessage(), containsString("local-hostname")); |
127 |
| - } |
| 142 | + resolveEc2("_ec2:privateDns_", InetAddress.getByName("10.10.10.5")); |
128 | 143 | }
|
129 | 144 |
|
130 | 145 | /**
|
131 | 146 | * Test for network.host: _ec2:publicIpv4_
|
132 | 147 | */
|
133 | 148 | public void testNetworkHostEc2PublicIpv4() throws IOException {
|
134 |
| - Settings nodeSettings = Settings.builder() |
135 |
| - .put("network.host", "_ec2:publicIpv4_") |
136 |
| - .build(); |
137 |
| - |
138 |
| - NetworkService networkService = new NetworkService(Collections.singletonList(new Ec2NameResolver())); |
139 |
| - // TODO we need to replace that with a mock. For now we check the URL we are supposed to reach. |
140 |
| - try { |
141 |
| - networkService.resolveBindHostAddresses(null); |
142 |
| - // note: this can succeed and the test can pass |
143 |
| - } catch (IOException e) { |
144 |
| - assertThat(e.getMessage(), containsString("public-ipv4")); |
145 |
| - } |
| 149 | + resolveEc2("_ec2:publicIpv4_", InetAddress.getByName("165.168.10.2")); |
146 | 150 | }
|
147 | 151 |
|
148 | 152 | /**
|
149 | 153 | * Test for network.host: _ec2:publicDns_
|
150 | 154 | */
|
151 | 155 | public void testNetworkHostEc2PublicDns() throws IOException {
|
| 156 | + resolveEc2("_ec2:publicDns_", InetAddress.getByName("165.168.10.3")); |
| 157 | + } |
| 158 | + |
| 159 | + private InetAddress[] resolveEc2(String host, InetAddress ... expected) throws IOException { |
152 | 160 | Settings nodeSettings = Settings.builder()
|
153 |
| - .put("network.host", "_ec2:publicDns_") |
154 |
| - .build(); |
| 161 | + .put("network.host", host) |
| 162 | + .build(); |
155 | 163 |
|
156 | 164 | NetworkService networkService = new NetworkService(Collections.singletonList(new Ec2NameResolver()));
|
157 |
| - // TODO we need to replace that with a mock. For now we check the URL we are supposed to reach. |
158 |
| - try { |
159 |
| - networkService.resolveBindHostAddresses(null); |
160 |
| - // note: this can succeed and the test can pass |
161 |
| - } catch (IOException e) { |
162 |
| - assertThat(e.getMessage(), containsString("public-hostname")); |
| 165 | + |
| 166 | + InetAddress[] addresses = networkService.resolveBindHostAddresses( |
| 167 | + NetworkService.GLOBAL_NETWORK_BINDHOST_SETTING.get(nodeSettings).toArray(Strings.EMPTY_ARRAY)); |
| 168 | + if (expected == null) { |
| 169 | + fail("We should get an IOException, resolved addressed:" + Arrays.toString(addresses)); |
163 | 170 | }
|
| 171 | + assertThat(addresses, arrayContaining(expected)); |
| 172 | + return addresses; |
164 | 173 | }
|
165 | 174 |
|
166 | 175 | /**
|
|
0 commit comments