PageRenderTime 1289ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/galaxy-integration-tests/src/test/java/com/proofpoint/galaxy/integration/MockLocalProvisioner.java

http://github.com/dain/galaxy-server
Java | 335 lines | 289 code | 44 blank | 2 comment | 16 complexity | c6c0880274f4f8aa9f0708ebcb93b046 MD5 | raw file
  1. package com.proofpoint.galaxy.integration;
  2. import com.google.common.base.Function;
  3. import com.google.common.base.Preconditions;
  4. import com.google.common.base.Throwables;
  5. import com.google.common.collect.ImmutableList;
  6. import com.google.common.collect.ImmutableMap;
  7. import com.google.common.collect.Iterables;
  8. import com.google.inject.Guice;
  9. import com.google.inject.Injector;
  10. import com.proofpoint.configuration.ConfigurationFactory;
  11. import com.proofpoint.configuration.ConfigurationModule;
  12. import com.proofpoint.discovery.client.testing.TestingDiscoveryModule;
  13. import com.proofpoint.event.client.NullEventModule;
  14. import com.proofpoint.galaxy.agent.Agent;
  15. import com.proofpoint.galaxy.agent.AgentMainModule;
  16. import com.proofpoint.galaxy.agent.Slot;
  17. import com.proofpoint.galaxy.coordinator.Instance;
  18. import com.proofpoint.galaxy.coordinator.Provisioner;
  19. import com.proofpoint.http.server.testing.TestingHttpServer;
  20. import com.proofpoint.http.server.testing.TestingHttpServerModule;
  21. import com.proofpoint.jaxrs.JaxrsModule;
  22. import com.proofpoint.json.JsonModule;
  23. import com.proofpoint.node.NodeModule;
  24. import java.io.File;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import java.net.URI;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Map.Entry;
  31. import java.util.Properties;
  32. import java.util.concurrent.ConcurrentHashMap;
  33. import java.util.concurrent.atomic.AtomicInteger;
  34. import static com.google.common.collect.Lists.newArrayList;
  35. import static com.proofpoint.galaxy.shared.FileUtils.createTempDir;
  36. import static com.proofpoint.galaxy.shared.FileUtils.deleteRecursively;
  37. public class MockLocalProvisioner implements Provisioner
  38. {
  39. private final Map<String, Instance> coordinators = new ConcurrentHashMap<String, Instance>();
  40. private final Map<String, AgentServer> agents = new ConcurrentHashMap<String, AgentServer>();
  41. private final AtomicInteger nextInstanceId = new AtomicInteger();
  42. public boolean autoStartInstances;
  43. public void addCoordinators(Instance... instances)
  44. {
  45. addCoordinators(ImmutableList.copyOf(instances));
  46. }
  47. public void addCoordinators(Iterable<Instance> instances)
  48. {
  49. for (Instance instance : instances) {
  50. coordinators.put(instance.getInstanceId(), instance);
  51. }
  52. }
  53. public void removeCoordinators(String... coordinatorIds)
  54. {
  55. removeCoordinators(ImmutableList.copyOf(coordinatorIds));
  56. }
  57. public void removeCoordinators(Iterable<String> coordinatorIds)
  58. {
  59. for (String coordinatorId : coordinatorIds) {
  60. coordinators.remove(coordinatorId);
  61. }
  62. }
  63. public void clearCoordinators()
  64. {
  65. coordinators.clear();
  66. }
  67. @Override
  68. public List<Instance> listCoordinators()
  69. {
  70. return ImmutableList.copyOf(coordinators.values());
  71. }
  72. @Override
  73. public List<Instance> provisionCoordinators(String coordinatorConfigSpec,
  74. int coordinatorCount,
  75. String instanceType,
  76. String availabilityZone,
  77. String ami,
  78. String keyPair,
  79. String securityGroup)
  80. {
  81. List<Instance> instances = newArrayList();
  82. for (int i = 0; i < coordinatorCount; i++) {
  83. String coordinatorInstanceId = String.format("i-%05d", nextInstanceId.incrementAndGet());
  84. String location = String.format("/mock/%s/coordinator", coordinatorInstanceId);
  85. Instance instance = new Instance(coordinatorInstanceId, instanceType, location, null, null);
  86. instances.add(instance);
  87. }
  88. addCoordinators(instances);
  89. if (autoStartInstances) {
  90. List<Instance> runningInstances = newArrayList();
  91. for (Instance instance : instances) {
  92. runningInstances.add(startCoordinator(instance.getInstanceId()));
  93. }
  94. instances = runningInstances;
  95. }
  96. return ImmutableList.copyOf(instances);
  97. }
  98. public Instance startCoordinator(String instanceId) {
  99. Instance instance = coordinators.get(instanceId);
  100. Preconditions.checkNotNull(instance, "instance is null");
  101. URI internalUri = URI.create("fake:/" + instanceId + "/internal");
  102. URI externalUri = URI.create("fake:/" + instanceId + "/external");
  103. Instance newCoordinatorInstance = new Instance(
  104. instanceId,
  105. instance.getInstanceType(),
  106. instance.getLocation(),
  107. internalUri,
  108. externalUri);
  109. coordinators.put(instanceId, newCoordinatorInstance);
  110. return newCoordinatorInstance;
  111. }
  112. @Override
  113. public List<Instance> listAgents()
  114. {
  115. return ImmutableList.copyOf(Iterables.transform(agents.values(), new GetInstanceFunction()));
  116. }
  117. @Override
  118. public List<Instance> provisionAgents(String agentConfig,
  119. int agentCount,
  120. String instanceType,
  121. String availabilityZone,
  122. String ami,
  123. String keyPair,
  124. String securityGroup)
  125. {
  126. List<Instance> instances = newArrayList();
  127. for (int i = 0; i < agentCount; i++) {
  128. String agentInstanceId = String.format("i-%05d", nextInstanceId.incrementAndGet());
  129. String location = String.format("/mock/%s/agent", agentInstanceId);
  130. Instance instance = new Instance(agentInstanceId,
  131. instanceType,
  132. location,
  133. null,
  134. null);
  135. AgentServer agentServer = new AgentServer(instance);
  136. instances.add(instance);
  137. if (autoStartInstances) {
  138. try {
  139. agentServer.start();
  140. }
  141. catch (Exception e) {
  142. throw Throwables.propagate(e);
  143. }
  144. }
  145. agents.put(agentInstanceId, agentServer);
  146. }
  147. return ImmutableList.copyOf(instances);
  148. }
  149. @Override
  150. public void terminateAgents(Iterable<String> instanceIds)
  151. {
  152. for (Entry<String, AgentServer> entry : agents.entrySet()) {
  153. entry.getValue().destroy();
  154. agents.remove(entry.getKey());
  155. }
  156. }
  157. public void clearAgents()
  158. throws Exception
  159. {
  160. terminateAgents(agents.keySet());
  161. }
  162. public ImmutableList<AgentServer> getAllAgents()
  163. {
  164. return ImmutableList.copyOf(agents.values());
  165. }
  166. public AgentServer getAgent(String instanceId)
  167. {
  168. return agents.get(instanceId);
  169. }
  170. public static class AgentServer
  171. {
  172. public static Map<String, Integer> AGENT_RESOURCES = ImmutableMap.<String, Integer>builder()
  173. .put("cpu", 16)
  174. .put("memory", 64000)
  175. .build();
  176. private Instance instance;
  177. private final File resourcesFile;
  178. private final File tempDir;
  179. private TestingHttpServer agentServer;
  180. private Agent agent;
  181. public AgentServer(Instance instance)
  182. {
  183. Preconditions.checkNotNull(instance, "instance is null");
  184. this.instance = instance;
  185. tempDir = createTempDir("agent");
  186. resourcesFile = new File(tempDir, "slots/galaxy-resources.properties");
  187. writeResources(AGENT_RESOURCES, resourcesFile);
  188. }
  189. public void start()
  190. throws Exception
  191. {
  192. Map<String, String> agentProperties = ImmutableMap.<String, String>builder()
  193. .put("node.environment", "test")
  194. .put("node.id", instance.getInstanceId())
  195. .put("node.location", instance.getLocation())
  196. .put("agent.slots-dir", new File(tempDir, "slots").getAbsolutePath())
  197. .put("agent.resources-file", resourcesFile.getAbsolutePath())
  198. .build();
  199. Injector agentInjector = Guice.createInjector(new NodeModule(),
  200. new TestingHttpServerModule(),
  201. new TestingDiscoveryModule(),
  202. new JsonModule(),
  203. new JaxrsModule(),
  204. new NullEventModule(),
  205. new AgentMainModule(),
  206. new ConfigurationModule(new ConfigurationFactory(agentProperties)));
  207. agentServer = agentInjector.getInstance(TestingHttpServer.class);
  208. agent = agentInjector.getInstance(Agent.class);
  209. agentServer.start();
  210. // update instance URIs to new http server
  211. instance = new Instance(instance.getInstanceId(),
  212. instance.getInstanceType(),
  213. instance.getLocation(),
  214. agentServer.getBaseUrl(),
  215. agentServer.getBaseUrl());
  216. }
  217. public void stop()
  218. {
  219. if (agentServer != null) {
  220. for (Slot slot : agent.getAllSlots()) {
  221. if (slot.status().getAssignment() != null) {
  222. slot.stop();
  223. }
  224. agent.terminateSlot(slot.getId());
  225. }
  226. try {
  227. agentServer.stop();
  228. }
  229. catch (Exception ignored) {
  230. }
  231. }
  232. // clear instance URIs
  233. instance = new Instance(instance.getInstanceId(),
  234. instance.getInstanceType(),
  235. instance.getLocation(),
  236. null,
  237. null);
  238. }
  239. public void destroy()
  240. {
  241. stop();
  242. if (tempDir != null) {
  243. deleteRecursively(tempDir);
  244. }
  245. }
  246. public String getInstanceId()
  247. {
  248. return instance.getInstanceId();
  249. }
  250. public Instance getInstance()
  251. {
  252. return instance;
  253. }
  254. public TestingHttpServer getAgentServer()
  255. {
  256. return agentServer;
  257. }
  258. public Agent getAgent()
  259. {
  260. return agent;
  261. }
  262. public static void writeResources(Map<String, Integer> resources, File resourcesFile)
  263. {
  264. Properties properties = new Properties();
  265. for (Entry<String, Integer> entry : resources.entrySet()) {
  266. properties.setProperty(entry.getKey(), String.valueOf(entry.getValue()));
  267. }
  268. resourcesFile.getParentFile().mkdirs();
  269. try {
  270. FileOutputStream out = new FileOutputStream(resourcesFile);
  271. try {
  272. properties.store(out, "");
  273. }
  274. finally {
  275. out.close();
  276. }
  277. }
  278. catch (IOException e) {
  279. throw Throwables.propagate(e);
  280. }
  281. }
  282. }
  283. private static class GetInstanceFunction implements Function<AgentServer, Instance>
  284. {
  285. @Override
  286. public Instance apply(AgentServer agentServer)
  287. {
  288. return agentServer.getInstance();
  289. }
  290. }
  291. }