/
HttpMetricsIngestionServerShutdownIntegrationTest.java
141 lines (118 loc) · 5.47 KB
/
HttpMetricsIngestionServerShutdownIntegrationTest.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
/*
* Copyright 2015 Rackspace
*
* 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
*
* http://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 com.rackspacecloud.blueflood.inputs.handlers;
import com.github.tlrx.elasticsearch.test.EsSetup;
import com.rackspacecloud.blueflood.http.HttpClientVendor;
import com.rackspacecloud.blueflood.io.*;
import com.rackspacecloud.blueflood.service.Configuration;
import com.rackspacecloud.blueflood.service.CoreConfig;
import com.rackspacecloud.blueflood.service.HttpConfig;
import com.rackspacecloud.blueflood.service.ScheduleContext;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.*;
import java.net.ConnectException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashSet;
import static org.mockito.Mockito.*;
import static com.rackspacecloud.blueflood.TestUtils.*;
public class HttpMetricsIngestionServerShutdownIntegrationTest {
private static HttpMetricsIngestionServer server;
private static HttpClientVendor vendor;
private static DefaultHttpClient client;
private static Collection<Integer> manageShards = new HashSet<Integer>();
private static int httpPort;
private static ScheduleContext context;
private static EventsIO eventsSearchIO;
private static EsSetup esSetup;
//A time stamp 2 days ago
private final long baseMillis = Calendar.getInstance().getTimeInMillis() - 172800000;
@BeforeClass
public static void setUp() throws Exception{
System.setProperty(CoreConfig.EVENTS_MODULES.name(), "com.rackspacecloud.blueflood.io.EventElasticSearchIO");
Configuration.getInstance().init();
httpPort = Configuration.getInstance().getIntegerProperty(HttpConfig.HTTP_INGESTION_PORT);
manageShards.add(1); manageShards.add(5); manageShards.add(6);
context = spy(new ScheduleContext(System.currentTimeMillis(), manageShards));
esSetup = new EsSetup();
esSetup.execute(EsSetup.deleteAll());
esSetup.execute(EsSetup.createIndex(EventElasticSearchIO.EVENT_INDEX)
.withSettings(EsSetup.fromClassPath("index_settings.json"))
.withMapping("graphite_event", EsSetup.fromClassPath("events_mapping.json")));
eventsSearchIO = new EventElasticSearchIO(esSetup.client());
server = new HttpMetricsIngestionServer(context);
server.setHttpEventsIngestionHandler(new HttpEventsIngestionHandler(eventsSearchIO));
server.startServer();
vendor = new HttpClientVendor();
client = vendor.getClient();
}
@Test
public void testHttpIngestionHappyCase() throws Exception {
// given
HttpPost post = new HttpPost(getMetricsURI());
HttpEntity entity = new StringEntity( generateJSONMetricsData(),
ContentType.APPLICATION_JSON);
post.setEntity(entity);
HttpResponse response = client.execute(post);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
HttpPost post2 = new HttpPost(getMetricsURI());
HttpEntity entity2 = new StringEntity( generateJSONMetricsData(),
ContentType.APPLICATION_JSON);
post2.setEntity(entity2);
// when
server.shutdownServer();
// then
try {
HttpResponse response2 = client.execute(post2);
Assert.fail("We should have received a Connect exception");
} catch (ConnectException ex) {
// NOTE: ideally, one would simply use jUnit's `ExpectedException`
// rule or `expected` param to indicate that we expected a
// ConnectException to be thrown. However, ConnectException can be
// thrown for a number of different reasons, and the only way to
// know for sure that the connection was refused (and, thus, that
// the port is no longer open) is to catch the exception object and
// check its message. Hence, this try/catch.
Assert.assertTrue("Connection refused", ex.getMessage().contains("Connection refused"));
}
}
private URI getMetricsURI() throws URISyntaxException {
return getMetricsURIBuilder().build();
}
private URIBuilder getMetricsURIBuilder() throws URISyntaxException {
return new URIBuilder().setScheme("http").setHost("127.0.0.1")
.setPort(httpPort).setPath("/v2.0/acTEST/ingest");
}
@AfterClass
public static void shutdown() {
Configuration.getInstance().setProperty(CoreConfig.EVENTS_MODULES.name(), "");
System.clearProperty(CoreConfig.EVENTS_MODULES.name());
if (esSetup != null) {
esSetup.terminate();
}
if (vendor != null) {
vendor.shutdown();
}
}
}