PageRenderTime 921ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/galaxy-agent/src/test/java/com/proofpoint/galaxy/agent/TestServer.java

http://github.com/dain/galaxy-server
Java | 491 lines | 414 code | 63 blank | 14 comment | 11 complexity | 1c8fc0724e70d41e5e263f6694f7b962 MD5 | raw file
  1. /**
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package com.proofpoint.galaxy.agent;
  15. import com.google.common.collect.ImmutableMap;
  16. import com.google.common.io.Resources;
  17. import com.google.inject.Guice;
  18. import com.google.inject.Injector;
  19. import com.proofpoint.configuration.ConfigurationFactory;
  20. import com.proofpoint.configuration.ConfigurationModule;
  21. import com.proofpoint.discovery.client.testing.TestingDiscoveryModule;
  22. import com.proofpoint.event.client.NullEventModule;
  23. import com.proofpoint.galaxy.shared.HttpUriBuilder;
  24. import com.proofpoint.galaxy.shared.Installation;
  25. import com.proofpoint.galaxy.shared.InstallationHelper;
  26. import com.proofpoint.galaxy.shared.InstallationRepresentation;
  27. import com.proofpoint.galaxy.shared.SlotStatus;
  28. import com.proofpoint.galaxy.shared.VersionsUtil;
  29. import com.proofpoint.http.client.ApacheHttpClient;
  30. import com.proofpoint.http.client.FullJsonResponseHandler.JsonResponse;
  31. import com.proofpoint.http.client.HttpClient;
  32. import com.proofpoint.http.client.JsonBodyGenerator;
  33. import com.proofpoint.http.client.Request;
  34. import com.proofpoint.http.client.RequestBuilder;
  35. import com.proofpoint.http.client.StatusResponseHandler.StatusResponse;
  36. import com.proofpoint.http.server.testing.TestingHttpServer;
  37. import com.proofpoint.http.server.testing.TestingHttpServerModule;
  38. import com.proofpoint.jaxrs.JaxrsModule;
  39. import com.proofpoint.json.JsonCodec;
  40. import com.proofpoint.json.JsonModule;
  41. import com.proofpoint.node.testing.TestingNodeModule;
  42. import org.testng.annotations.AfterClass;
  43. import org.testng.annotations.BeforeClass;
  44. import org.testng.annotations.BeforeMethod;
  45. import org.testng.annotations.Test;
  46. import javax.ws.rs.core.HttpHeaders;
  47. import javax.ws.rs.core.MediaType;
  48. import java.io.File;
  49. import java.net.URI;
  50. import java.util.Collections;
  51. import java.util.List;
  52. import java.util.Map;
  53. import java.util.UUID;
  54. import static com.google.common.base.Charsets.UTF_8;
  55. import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
  56. import static com.proofpoint.galaxy.shared.ExtraAssertions.assertEqualsNoOrder;
  57. import static com.proofpoint.galaxy.shared.FileUtils.createTempDir;
  58. import static com.proofpoint.galaxy.shared.FileUtils.deleteRecursively;
  59. import static com.proofpoint.galaxy.shared.HttpUriBuilder.uriBuilderFrom;
  60. import static com.proofpoint.galaxy.shared.SlotLifecycleState.RUNNING;
  61. import static com.proofpoint.galaxy.shared.SlotLifecycleState.STOPPED;
  62. import static com.proofpoint.galaxy.shared.SlotLifecycleState.TERMINATED;
  63. import static com.proofpoint.http.client.FullJsonResponseHandler.createFullJsonResponseHandler;
  64. import static com.proofpoint.http.client.JsonResponseHandler.createJsonResponseHandler;
  65. import static com.proofpoint.http.client.StaticBodyGenerator.createStaticBodyGenerator;
  66. import static com.proofpoint.http.client.StatusResponseHandler.createStatusResponseHandler;
  67. import static com.proofpoint.json.JsonCodec.jsonCodec;
  68. import static com.proofpoint.json.JsonCodec.listJsonCodec;
  69. import static com.proofpoint.json.JsonCodec.mapJsonCodec;
  70. import static javax.ws.rs.core.Response.Status;
  71. import static org.testng.Assert.assertEquals;
  72. import static org.testng.Assert.assertNull;
  73. import static org.testng.Assert.assertTrue;
  74. public class TestServer
  75. {
  76. private static final int NOT_ALLOWED = 405;
  77. private HttpClient client;
  78. private TestingHttpServer server;
  79. private Agent agent;
  80. private final JsonCodec<InstallationRepresentation> installationCodec = jsonCodec(InstallationRepresentation.class);
  81. private final JsonCodec<Map<String, Object>> mapCodec = mapJsonCodec(String.class, Object.class);
  82. private final JsonCodec<List<Map<String, Object>>> listCodec = listJsonCodec(mapCodec);
  83. private InstallationHelper installationHelper;
  84. private Installation appleInstallation;
  85. private Installation bananaInstallation;
  86. private File tempDir;
  87. @BeforeClass
  88. public void startServer()
  89. throws Exception
  90. {
  91. tempDir = createTempDir("agent");
  92. Map<String, String> properties = ImmutableMap.<String, String>builder()
  93. .put("agent.id", UUID.randomUUID().toString())
  94. .put("agent.coordinator-uri", "http://localhost:9999/")
  95. .put("agent.slots-dir", tempDir.getAbsolutePath())
  96. .put("discovery.uri", "fake://server")
  97. .build();
  98. Injector injector = Guice.createInjector(
  99. new TestingDiscoveryModule(),
  100. new TestingNodeModule(),
  101. new JsonModule(),
  102. new TestingHttpServerModule(),
  103. new JaxrsModule(),
  104. new NullEventModule(),
  105. new AgentMainModule(),
  106. new ConfigurationModule(new ConfigurationFactory(properties)));
  107. server = injector.getInstance(TestingHttpServer.class);
  108. agent = injector.getInstance(Agent.class);
  109. server.start();
  110. client = new ApacheHttpClient();
  111. installationHelper = new InstallationHelper();
  112. appleInstallation = installationHelper.getAppleInstallation();
  113. bananaInstallation = installationHelper.getBananaInstallation();
  114. }
  115. @BeforeMethod
  116. public void resetState()
  117. {
  118. for (Slot slot : agent.getAllSlots()) {
  119. if (slot.status().getAssignment() != null) {
  120. slot.stop();
  121. }
  122. agent.terminateSlot(slot.getId());
  123. }
  124. assertTrue(agent.getAllSlots().isEmpty());
  125. }
  126. @AfterClass
  127. public void stopServer()
  128. throws Exception
  129. {
  130. if (server != null) {
  131. server.stop();
  132. }
  133. if (tempDir != null) {
  134. deleteRecursively(tempDir);
  135. }
  136. if (installationHelper != null) {
  137. installationHelper.destroy();
  138. }
  139. }
  140. @Test
  141. public void testGetSlotStatus()
  142. throws Exception
  143. {
  144. SlotStatus slotStatus = agent.install(appleInstallation);
  145. Request request = RequestBuilder.prepareGet().setUri(urlFor("/v1/agent/slot", slotStatus.getId().toString())).build();
  146. Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode()));
  147. Map<String, Object> expected = mapCodec.fromJson(Resources.toString(Resources.getResource("slot-status.json"), UTF_8));
  148. expected.put("id", slotStatus.getId().toString());
  149. expected.put("shortId", slotStatus.getId().toString());
  150. expected.put("self", urlFor(slotStatus).toASCIIString());
  151. expected.put("externalUri", urlFor(slotStatus).toASCIIString());
  152. expected.put("version", slotStatus.getVersion());
  153. expected.put("location", slotStatus.getLocation());
  154. expected.put("shortLocation", slotStatus.getLocation());
  155. expected.put("installPath", slotStatus.getInstallPath());
  156. // agent does not return instance id or expected status
  157. expected.remove("instanceId");
  158. expected.remove("expectedBinary");
  159. expected.remove("expectedConfig");
  160. expected.remove("expectedStatus");
  161. assertEquals(response, expected);
  162. }
  163. @Test
  164. public void testGetAllSlotStatusEmpty()
  165. throws Exception
  166. {
  167. Request request = RequestBuilder.prepareGet().setUri(urlFor("/v1/agent/slot")).build();
  168. List<Map<String, Object>> response = client.execute(request, createJsonResponseHandler(listCodec, Status.OK.getStatusCode()));
  169. assertEquals(response, Collections.<Object>emptyList());
  170. }
  171. @Test
  172. public void testGetAllSlotStatus()
  173. throws Exception
  174. {
  175. SlotStatus appleSlotStatus = agent.install(appleInstallation);
  176. SlotStatus bananaSlotStatus = agent.install(bananaInstallation);
  177. Request request = RequestBuilder.prepareGet().setUri(urlFor("/v1/agent/slot")).build();
  178. List<Map<String, Object>> response = client.execute(request, createJsonResponseHandler(listCodec, Status.OK.getStatusCode()));
  179. List<Map<String, Object>> expected = listCodec.fromJson(Resources.toString(Resources.getResource("slot-status-list.json"), UTF_8));
  180. expected.get(0).put("id", appleSlotStatus.getId().toString());
  181. expected.get(0).put("shortId", appleSlotStatus.getId().toString());
  182. expected.get(0).put("version", appleSlotStatus.getVersion());
  183. expected.get(0).put("self", urlFor(appleSlotStatus).toASCIIString());
  184. expected.get(0).put("externalUri", urlFor(appleSlotStatus).toASCIIString());
  185. expected.get(0).put("location", appleSlotStatus.getLocation());
  186. expected.get(0).put("shortLocation", appleSlotStatus.getLocation());
  187. expected.get(0).put("installPath", appleSlotStatus.getInstallPath());
  188. expected.get(0).put("resources", ImmutableMap.<String, Integer>of("memory", 512));
  189. expected.get(1).put("id", bananaSlotStatus.getId().toString());
  190. expected.get(1).put("shortId", bananaSlotStatus.getId().toString());
  191. expected.get(1).put("version", bananaSlotStatus.getVersion());
  192. expected.get(1).put("self", urlFor(bananaSlotStatus).toASCIIString());
  193. expected.get(1).put("externalUri", urlFor(bananaSlotStatus).toASCIIString());
  194. expected.get(1).put("location", bananaSlotStatus.getLocation());
  195. expected.get(1).put("shortLocation", bananaSlotStatus.getLocation());
  196. expected.get(1).put("installPath", bananaSlotStatus.getInstallPath());
  197. expected.get(1).put("resources", ImmutableMap.<String, Integer>of("cpu", 1));
  198. assertEqualsNoOrder(response, expected);
  199. }
  200. @Test
  201. public void testInstallSlot()
  202. throws Exception
  203. {
  204. Request request = RequestBuilder.preparePost()
  205. .setUri(urlFor("/v1/agent/slot"))
  206. .setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON)
  207. .setBodyGenerator(JsonBodyGenerator.jsonBodyGenerator(installationCodec, InstallationRepresentation.from(appleInstallation)))
  208. .build();
  209. JsonResponse<Map<String, Object>> response = client.execute(request, createFullJsonResponseHandler(mapCodec));
  210. assertEquals(response.getStatusCode(), Status.CREATED.getStatusCode());
  211. Slot slot = agent.getAllSlots().iterator().next();
  212. assertEquals(response.getHeader(HttpHeaders.LOCATION), uriBuilderFrom(server.getBaseUrl()).appendPath("/v1/agent/slot/").appendPath(slot.getId().toString()).toString());
  213. assertEquals(response.getHeader(CONTENT_TYPE), MediaType.APPLICATION_JSON);
  214. Map<String, Object> expected = ImmutableMap.<String, Object>builder()
  215. .put("id", slot.getId().toString())
  216. .put("shortId", slot.getId().toString())
  217. .put("binary", appleInstallation.getAssignment().getBinary())
  218. .put("shortBinary", appleInstallation.getAssignment().getBinary())
  219. .put("config", appleInstallation.getAssignment().getConfig())
  220. .put("shortConfig", appleInstallation.getAssignment().getConfig())
  221. .put("self", urlFor(slot).toASCIIString())
  222. .put("externalUri", urlFor(slot).toASCIIString())
  223. .put("location", slot.status().getLocation())
  224. .put("shortLocation", slot.status().getLocation())
  225. .put("status", STOPPED.toString())
  226. .put("version", slot.status().getVersion())
  227. .put("installPath", slot.status().getInstallPath())
  228. .put("resources", ImmutableMap.<String, Integer>of("memory", 512))
  229. .build();
  230. assertEquals(response.getValue(), expected);
  231. }
  232. @Test
  233. public void testTerminateSlot()
  234. throws Exception
  235. {
  236. SlotStatus slotStatus = agent.install(appleInstallation);
  237. Request request = RequestBuilder.prepareDelete()
  238. .setUri(urlFor("/v1/agent/slot", slotStatus.getId().toString()))
  239. .build();
  240. Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode()));
  241. assertNull(agent.getSlot(slotStatus.getId()));
  242. Map<String, Object> expected = ImmutableMap.<String, Object>builder()
  243. .put("id", slotStatus.getId().toString())
  244. .put("shortId", slotStatus.getId().toString())
  245. .put("self", urlFor(slotStatus).toASCIIString())
  246. .put("externalUri", urlFor(slotStatus).toASCIIString())
  247. .put("location", slotStatus.getLocation())
  248. .put("shortLocation", slotStatus.getLocation())
  249. .put("status", TERMINATED.toString())
  250. .put("version", VersionsUtil.createSlotVersion(slotStatus.getId(), TERMINATED, null))
  251. .put("resources", ImmutableMap.<String, Integer>of())
  252. .build();
  253. assertEquals(response, expected);
  254. }
  255. @Test
  256. public void testTerminateUnknownSlot()
  257. throws Exception
  258. {
  259. Request request = RequestBuilder.prepareDelete()
  260. .setUri(urlFor("/v1/agent/slot/unknown"))
  261. .build();
  262. StatusResponse response = client.execute(request, createStatusResponseHandler());
  263. assertEquals(response.getStatusCode(), Status.NOT_FOUND.getStatusCode());
  264. }
  265. @Test
  266. public void testPutNotAllowed()
  267. throws Exception
  268. {
  269. String json = Resources.toString(Resources.getResource("slot-status.json"), UTF_8);
  270. Request request = RequestBuilder.preparePut()
  271. .setUri(urlFor("/v1/agent/slot"))
  272. .setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON)
  273. .setBodyGenerator(createStaticBodyGenerator(json, UTF_8))
  274. .build();
  275. StatusResponse response = client.execute(request, createStatusResponseHandler());
  276. assertEquals(response.getStatusCode(), NOT_ALLOWED);
  277. assertNull(agent.getSlot(UUID.randomUUID()));
  278. }
  279. @Test
  280. public void testAssign()
  281. throws Exception
  282. {
  283. SlotStatus slotStatus = agent.install(appleInstallation);
  284. String json = installationCodec.toJson(InstallationRepresentation.from(appleInstallation));
  285. Request request = RequestBuilder.preparePut()
  286. .setUri(urlFor(slotStatus, "assignment"))
  287. .setHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON)
  288. .setBodyGenerator(createStaticBodyGenerator(json, UTF_8))
  289. .build();
  290. Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode()));
  291. Map<String, Object> expected = ImmutableMap.<String, Object>builder()
  292. .put("id", slotStatus.getId().toString())
  293. .put("shortId", slotStatus.getId().toString())
  294. .put("binary", appleInstallation.getAssignment().getBinary())
  295. .put("shortBinary", appleInstallation.getAssignment().getBinary())
  296. .put("config", appleInstallation.getAssignment().getConfig())
  297. .put("shortConfig", appleInstallation.getAssignment().getConfig())
  298. .put("self", urlFor(slotStatus).toASCIIString())
  299. .put("externalUri", urlFor(slotStatus).toASCIIString())
  300. .put("location", slotStatus.getLocation())
  301. .put("shortLocation", slotStatus.getLocation())
  302. .put("status", STOPPED.toString())
  303. .put("version", slotStatus.getVersion())
  304. .put("installPath", slotStatus.getInstallPath())
  305. .put("resources", ImmutableMap.<String, Integer>of("memory", 512))
  306. .build();
  307. assertEquals(response, expected);
  308. }
  309. @Test
  310. public void testStart()
  311. throws Exception
  312. {
  313. SlotStatus slotStatus = agent.install(appleInstallation);
  314. Request request = RequestBuilder.preparePut()
  315. .setUri(urlFor(slotStatus, "lifecycle"))
  316. .setBodyGenerator(createStaticBodyGenerator("running", UTF_8))
  317. .build();
  318. Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode()));
  319. Map<String, Object> expected = ImmutableMap.<String, Object>builder()
  320. .put("id", slotStatus.getId().toString())
  321. .put("shortId", slotStatus.getId().toString())
  322. .put("binary", appleInstallation.getAssignment().getBinary())
  323. .put("shortBinary", appleInstallation.getAssignment().getBinary())
  324. .put("config", appleInstallation.getAssignment().getConfig())
  325. .put("shortConfig", appleInstallation.getAssignment().getConfig())
  326. .put("self", urlFor(slotStatus).toASCIIString())
  327. .put("externalUri", urlFor(slotStatus).toASCIIString())
  328. .put("location", slotStatus.getLocation())
  329. .put("shortLocation", slotStatus.getLocation())
  330. .put("status", RUNNING.toString())
  331. .put("version", VersionsUtil.createSlotVersion(slotStatus.getId(), RUNNING, appleInstallation.getAssignment()))
  332. .put("installPath", slotStatus.getInstallPath())
  333. .put("resources", ImmutableMap.<String, Integer>of("memory", 512))
  334. .build();
  335. assertEquals(response, expected);
  336. }
  337. @Test
  338. public void testStop()
  339. throws Exception
  340. {
  341. SlotStatus slotStatus = agent.install(appleInstallation);
  342. agent.getSlot(slotStatus.getId()).start();
  343. Request request = RequestBuilder.preparePut()
  344. .setUri(urlFor(slotStatus, "lifecycle"))
  345. .setBodyGenerator(createStaticBodyGenerator("stopped", UTF_8))
  346. .build();
  347. Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode()));
  348. Map<String, Object> expected = ImmutableMap.<String, Object>builder()
  349. .put("id", slotStatus.getId().toString())
  350. .put("shortId", slotStatus.getId().toString())
  351. .put("binary", appleInstallation.getAssignment().getBinary())
  352. .put("shortBinary", appleInstallation.getAssignment().getBinary())
  353. .put("config", appleInstallation.getAssignment().getConfig())
  354. .put("shortConfig", appleInstallation.getAssignment().getConfig())
  355. .put("self", urlFor(slotStatus).toASCIIString())
  356. .put("externalUri", urlFor(slotStatus).toASCIIString())
  357. .put("location", slotStatus.getLocation())
  358. .put("shortLocation", slotStatus.getLocation())
  359. .put("status", STOPPED.toString())
  360. .put("version", slotStatus.getVersion())
  361. .put("installPath", slotStatus.getInstallPath())
  362. .put("resources", ImmutableMap.<String, Integer>of("memory", 512))
  363. .build();
  364. assertEquals(response, expected);
  365. }
  366. @Test
  367. public void testRestart()
  368. throws Exception
  369. {
  370. SlotStatus slotStatus = agent.install(appleInstallation);
  371. Request request = RequestBuilder.preparePut()
  372. .setUri(urlFor(slotStatus, "lifecycle"))
  373. .setBodyGenerator(createStaticBodyGenerator("restarting", UTF_8))
  374. .build();
  375. Map<String, Object> response = client.execute(request, createJsonResponseHandler(mapCodec, Status.OK.getStatusCode()));
  376. Map<String, Object> expected = ImmutableMap.<String, Object>builder()
  377. .put("id", slotStatus.getId().toString())
  378. .put("shortId", slotStatus.getId().toString())
  379. .put("binary", appleInstallation.getAssignment().getBinary())
  380. .put("shortBinary", appleInstallation.getAssignment().getBinary())
  381. .put("config", appleInstallation.getAssignment().getConfig())
  382. .put("shortConfig", appleInstallation.getAssignment().getConfig())
  383. .put("self", urlFor(slotStatus).toASCIIString())
  384. .put("externalUri", urlFor(slotStatus).toASCIIString())
  385. .put("location", slotStatus.getLocation())
  386. .put("shortLocation", slotStatus.getLocation())
  387. .put("status", RUNNING.toString())
  388. .put("version", VersionsUtil.createSlotVersion(slotStatus.getId(), RUNNING, appleInstallation.getAssignment()))
  389. .put("installPath", slotStatus.getInstallPath())
  390. .put("resources", ImmutableMap.<String, Integer>of("memory", 512))
  391. .build();
  392. assertEquals(response, expected);
  393. }
  394. @Test
  395. public void testLifecycleUnknown()
  396. throws Exception
  397. {
  398. SlotStatus slotStatus = agent.install(appleInstallation);
  399. Request request = RequestBuilder.preparePut()
  400. .setUri(urlFor(slotStatus, "lifecycle"))
  401. .setBodyGenerator(createStaticBodyGenerator("unknown", UTF_8))
  402. .build();
  403. StatusResponse response = client.execute(request, createStatusResponseHandler());
  404. assertEquals(response.getStatusCode(), Status.BAD_REQUEST.getStatusCode());
  405. }
  406. private URI urlFor(String... pathParts)
  407. {
  408. HttpUriBuilder builder = uriBuilderFrom(server.getBaseUrl());
  409. for (String pathPart : pathParts) {
  410. builder.appendPath(pathPart);
  411. }
  412. return builder.build();
  413. }
  414. private URI urlFor(Slot slot, String... pathParts)
  415. {
  416. return urlFor(slot.status(), pathParts);
  417. }
  418. private URI urlFor(SlotStatus slotStatus, String... pathParts)
  419. {
  420. HttpUriBuilder builder = uriBuilderFrom(server.getBaseUrl())
  421. .appendPath("/v1/agent/slot/")
  422. .appendPath(slotStatus.getId().toString());
  423. for (String pathPart : pathParts) {
  424. builder.appendPath(pathPart);
  425. }
  426. return builder.build();
  427. }
  428. }