1 package dst.ass3.elastic;
3 import static org.hamcrest.CoreMatchers.equalTo;
4 import static org.hamcrest.CoreMatchers.is;
5 import static org.hamcrest.CoreMatchers.notNullValue;
6 import static org.junit.Assert.assertThat;
9 import java.util.concurrent.TimeUnit;
11 import com.github.dockerjava.api.exception.NotFoundException;
12 import dst.ass3.messaging.RabbitResource;
13 import dst.ass3.messaging.RequestType;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Rule;
17 import org.junit.Test;
18 import org.junit.rules.Timeout;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 import dst.ass3.elastic.impl.ElasticityFactory;
23 import org.springframework.amqp.core.Queue;
25 public class ContainerServiceTest {
27 private static final Logger LOG = LoggerFactory.getLogger(ContainerServiceTest.class);
30 public RabbitResource rabbit = new RabbitResource();
33 public Timeout timeout = new Timeout(30, TimeUnit.SECONDS);
35 IContainerService containerService;
36 IElasticityFactory factory;
39 public void setUp() throws Exception {
40 factory = new ElasticityFactory();
42 containerService = factory.createContainerService();
44 rabbit.getAdmin().declareQueue(new Queue("dst.document"));
45 rabbit.getAdmin().declareQueue(new Queue("dst.quiz"));
46 rabbit.getAdmin().declareQueue(new Queue("dst.video"));
50 public void tearDown() throws Exception {
51 rabbit.getAdmin().deleteQueue("dst.document");
52 rabbit.getAdmin().deleteQueue("dst.quiz");
53 rabbit.getAdmin().deleteQueue("dst.video");
57 public void spawnListStop_lifecycleWorks() throws Exception {
58 List<ContainerInfo> containers = containerService.listContainers();
59 assertThat("Please stop all containers before running the test", containers.size(), is(0));
61 ContainerInfo c1 = containerService.startWorker(RequestType.QUIZ);
62 LOG.info("Started container {}", c1);
64 ContainerInfo c2 = containerService.startWorker(RequestType.DOCUMENT);
65 LOG.info("Started container {}", c2);
67 ContainerInfo c3 = containerService.startWorker(RequestType.VIDEO);
68 LOG.info("Started container {}", c3);
70 LOG.info("Waiting for containers to boot...");
73 containers = containerService.listContainers();
75 assertThat(containers.size(), is(3));
77 LOG.info("Stopping containers...");
78 containerService.stopContainer(c1.getContainerId());
79 containerService.stopContainer(c2.getContainerId());
80 containerService.stopContainer(c3.getContainerId());
84 containers = containerService.listContainers();
85 assertThat(containers.size(), is(0));
88 @Test(expected = ContainerNotFoundException.class)
89 public void stopNonExistingContainer_throwsException() throws Exception {
90 containerService.stopContainer("Non-Existing-Id");
94 public void listContainers_containsCompleteInfo() throws Exception {
95 ContainerInfo c1 = containerService.startWorker(RequestType.QUIZ);
96 LOG.info("Started container {}", c1);
97 LOG.info("Waiting for container to boot...");
99 List<ContainerInfo> containers = containerService.listContainers();
100 ContainerInfo containerInfo = containers.stream()
101 .filter(c -> c1.getContainerId().equals(c.getContainerId()))
103 assertThat(containerInfo, notNullValue());
104 assertThat(containerInfo.getImage(), equalTo("dst/ass3-worker"));
105 assertThat(containerInfo.getWorkerType(), equalTo(RequestType.QUIZ));
106 assertThat(containerInfo.isRunning(), is(true));
107 LOG.info("Stopping container...");
108 containerService.stopContainer(containerInfo.getContainerId());