Skip to content

Commit 6ea3f5a

Browse files
Move EC2 Discovery Tests to Mock Rest API (#50605) (#52270)
Move EC2 discovery tests to using the mock REST API introduced in #50550 instead of mocking the AWS SDK classes manually. Move the trivial remaining AWS SDK mocks to the single test suit that was using them.
1 parent 26900bf commit 6ea3f5a

File tree

8 files changed

+360
-496
lines changed

8 files changed

+360
-496
lines changed

plugins/discovery-ec2/src/main/java/org/elasticsearch/discovery/ec2/AwsEc2ServiceImpl.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ private AmazonEC2 buildClient(Ec2ClientSettings clientSettings) {
5555

5656
// proxy for testing
5757
AmazonEC2 buildClient(AWSCredentialsProvider credentials, ClientConfiguration configuration) {
58-
final AmazonEC2 client = new AmazonEC2Client(credentials, configuration);
59-
return client;
58+
return new AmazonEC2Client(credentials, configuration);
6059
}
6160

6261
// pkg private for tests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.discovery.ec2;
20+
21+
import com.amazonaws.services.ec2.model.Instance;
22+
import com.amazonaws.services.ec2.model.Tag;
23+
import com.sun.net.httpserver.HttpServer;
24+
import org.elasticsearch.common.SuppressForbidden;
25+
import org.elasticsearch.common.network.InetAddresses;
26+
import org.elasticsearch.common.network.NetworkService;
27+
import org.elasticsearch.common.settings.MockSecureSettings;
28+
import org.elasticsearch.common.settings.Settings;
29+
import org.elasticsearch.core.internal.io.IOUtils;
30+
import org.elasticsearch.mocksocket.MockHttpServer;
31+
import org.elasticsearch.test.ESTestCase;
32+
import org.elasticsearch.test.transport.MockTransportService;
33+
import org.elasticsearch.threadpool.TestThreadPool;
34+
import org.elasticsearch.threadpool.ThreadPool;
35+
import org.junit.After;
36+
import org.junit.Before;
37+
38+
import javax.xml.XMLConstants;
39+
import javax.xml.stream.XMLOutputFactory;
40+
import javax.xml.stream.XMLStreamWriter;
41+
42+
import java.io.StringWriter;
43+
import java.net.InetAddress;
44+
import java.net.InetSocketAddress;
45+
import java.util.Collections;
46+
import java.util.List;
47+
import java.util.UUID;
48+
49+
import static java.nio.charset.StandardCharsets.UTF_8;
50+
51+
@SuppressForbidden(reason = "use a http server")
52+
public abstract class AbstractEC2MockAPITestCase extends ESTestCase {
53+
54+
protected HttpServer httpServer;
55+
56+
protected ThreadPool threadPool;
57+
58+
protected MockTransportService transportService;
59+
60+
protected NetworkService networkService = new NetworkService(Collections.emptyList());
61+
62+
@Before
63+
public void setUp() throws Exception {
64+
httpServer = MockHttpServer.createHttp(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0);
65+
httpServer.start();
66+
threadPool = new TestThreadPool(EC2RetriesTests.class.getName());
67+
transportService = createTransportService();
68+
super.setUp();
69+
}
70+
71+
protected abstract MockTransportService createTransportService();
72+
73+
protected Settings buildSettings(String accessKey) {
74+
final InetSocketAddress address = httpServer.getAddress();
75+
final String endpoint = "http://" + InetAddresses.toUriString(address.getAddress()) + ":" + address.getPort();
76+
final MockSecureSettings mockSecure = new MockSecureSettings();
77+
mockSecure.setString(Ec2ClientSettings.ACCESS_KEY_SETTING.getKey(), accessKey);
78+
mockSecure.setString(Ec2ClientSettings.SECRET_KEY_SETTING.getKey(), "ec2_secret");
79+
return Settings.builder().put(Ec2ClientSettings.ENDPOINT_SETTING.getKey(), endpoint).setSecureSettings(mockSecure).build();
80+
}
81+
82+
@After
83+
public void tearDown() throws Exception {
84+
try {
85+
IOUtils.close(transportService, () -> terminate(threadPool), () -> httpServer.stop(0));
86+
} finally {
87+
super.tearDown();
88+
}
89+
}
90+
91+
/**
92+
* Generates a XML response that describe the EC2 instances
93+
* TODO: org.elasticsearch.discovery.ec2.AmazonEC2Fixture uses pretty much the same code. We should dry up that test fixture.
94+
*/
95+
static byte[] generateDescribeInstancesResponse(List<Instance> instances) {
96+
final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
97+
xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
98+
99+
final StringWriter out = new StringWriter();
100+
XMLStreamWriter sw;
101+
try {
102+
sw = xmlOutputFactory.createXMLStreamWriter(out);
103+
sw.writeStartDocument();
104+
105+
String namespace = "http://ec2.amazonaws.com/doc/2013-02-01/";
106+
sw.setDefaultNamespace(namespace);
107+
sw.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, "DescribeInstancesResponse", namespace);
108+
{
109+
sw.writeStartElement("requestId");
110+
sw.writeCharacters(UUID.randomUUID().toString());
111+
sw.writeEndElement();
112+
113+
sw.writeStartElement("reservationSet");
114+
{
115+
for (Instance instance : instances) {
116+
sw.writeStartElement("item");
117+
{
118+
sw.writeStartElement("reservationId");
119+
sw.writeCharacters(UUID.randomUUID().toString());
120+
sw.writeEndElement();
121+
122+
sw.writeStartElement("instancesSet");
123+
{
124+
sw.writeStartElement("item");
125+
{
126+
sw.writeStartElement("instanceId");
127+
sw.writeCharacters(instance.getInstanceId());
128+
sw.writeEndElement();
129+
130+
sw.writeStartElement("imageId");
131+
sw.writeCharacters(instance.getImageId());
132+
sw.writeEndElement();
133+
134+
sw.writeStartElement("instanceState");
135+
{
136+
sw.writeStartElement("code");
137+
sw.writeCharacters("16");
138+
sw.writeEndElement();
139+
140+
sw.writeStartElement("name");
141+
sw.writeCharacters("running");
142+
sw.writeEndElement();
143+
}
144+
sw.writeEndElement();
145+
146+
sw.writeStartElement("privateDnsName");
147+
sw.writeCharacters(instance.getPrivateDnsName());
148+
sw.writeEndElement();
149+
150+
sw.writeStartElement("dnsName");
151+
sw.writeCharacters(instance.getPublicDnsName());
152+
sw.writeEndElement();
153+
154+
sw.writeStartElement("instanceType");
155+
sw.writeCharacters("m1.medium");
156+
sw.writeEndElement();
157+
158+
sw.writeStartElement("placement");
159+
{
160+
sw.writeStartElement("availabilityZone");
161+
sw.writeCharacters("use-east-1e");
162+
sw.writeEndElement();
163+
164+
sw.writeEmptyElement("groupName");
165+
166+
sw.writeStartElement("tenancy");
167+
sw.writeCharacters("default");
168+
sw.writeEndElement();
169+
}
170+
sw.writeEndElement();
171+
172+
sw.writeStartElement("privateIpAddress");
173+
sw.writeCharacters(instance.getPrivateIpAddress());
174+
sw.writeEndElement();
175+
176+
sw.writeStartElement("ipAddress");
177+
sw.writeCharacters(instance.getPublicIpAddress());
178+
sw.writeEndElement();
179+
180+
sw.writeStartElement("tagSet");
181+
for (Tag tag : instance.getTags()) {
182+
sw.writeStartElement("item");
183+
{
184+
sw.writeStartElement("key");
185+
sw.writeCharacters(tag.getKey());
186+
sw.writeEndElement();
187+
188+
sw.writeStartElement("value");
189+
sw.writeCharacters(tag.getValue());
190+
sw.writeEndElement();
191+
}
192+
sw.writeEndElement();
193+
}
194+
sw.writeEndElement();
195+
}
196+
sw.writeEndElement();
197+
}
198+
sw.writeEndElement();
199+
}
200+
sw.writeEndElement();
201+
}
202+
sw.writeEndElement();
203+
}
204+
sw.writeEndElement();
205+
206+
sw.writeEndDocument();
207+
sw.flush();
208+
}
209+
} catch (Exception e) {
210+
throw new RuntimeException(e);
211+
}
212+
return out.toString().getBytes(UTF_8);
213+
}
214+
}

0 commit comments

Comments
 (0)