]> git.somenet.org - pub/jan/dst18.git/blob - ass3-elastic/src/test/java/dst/ass3/elastic/ContainerServiceTest.java
Normalize versions across project to 2018.1
[pub/jan/dst18.git] / ass3-elastic / src / test / java / dst / ass3 / elastic / ContainerServiceTest.java
1 package dst.ass3.elastic;
2
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;
7
8 import java.util.List;
9 import java.util.concurrent.TimeUnit;
10
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;
21
22 import dst.ass3.elastic.impl.ElasticityFactory;
23 import org.springframework.amqp.core.Queue;
24
25 public class ContainerServiceTest {
26
27     private static final Logger LOG = LoggerFactory.getLogger(ContainerServiceTest.class);
28
29     @Rule
30     public RabbitResource rabbit = new RabbitResource();
31
32     @Rule
33     public Timeout timeout = new Timeout(30, TimeUnit.SECONDS);
34
35     IContainerService containerService;
36     IElasticityFactory factory;
37
38     @Before
39     public void setUp() throws Exception {
40         factory = new ElasticityFactory();
41
42         containerService = factory.createContainerService();
43
44         rabbit.getAdmin().declareQueue(new Queue("dst.document"));
45         rabbit.getAdmin().declareQueue(new Queue("dst.quiz"));
46         rabbit.getAdmin().declareQueue(new Queue("dst.video"));
47     }
48
49     @After
50     public void tearDown() throws Exception {
51         rabbit.getAdmin().deleteQueue("dst.document");
52         rabbit.getAdmin().deleteQueue("dst.quiz");
53         rabbit.getAdmin().deleteQueue("dst.video");
54     }
55
56     @Test
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));
60
61         ContainerInfo c1 = containerService.startWorker(RequestType.QUIZ);
62         LOG.info("Started container {}", c1);
63
64         ContainerInfo c2 = containerService.startWorker(RequestType.DOCUMENT);
65         LOG.info("Started container {}", c2);
66
67         ContainerInfo c3 = containerService.startWorker(RequestType.VIDEO);
68         LOG.info("Started container {}", c3);
69
70         LOG.info("Waiting for containers to boot...");
71         Thread.sleep(5000);
72
73         containers = containerService.listContainers();
74
75         assertThat(containers.size(), is(3));
76
77         LOG.info("Stopping containers...");
78         containerService.stopContainer(c1.getContainerId());
79         containerService.stopContainer(c2.getContainerId());
80         containerService.stopContainer(c3.getContainerId());
81
82         Thread.sleep(5000);
83
84         containers = containerService.listContainers();
85         assertThat(containers.size(), is(0));
86     }
87
88     @Test(expected = ContainerNotFoundException.class)
89     public void stopNonExistingContainer_throwsException() throws Exception {
90         containerService.stopContainer("Non-Existing-Id");
91     }
92
93     @Test
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...");
98         Thread.sleep(5000);
99         List<ContainerInfo> containers = containerService.listContainers();
100         ContainerInfo containerInfo = containers.stream()
101                 .filter(c -> c1.getContainerId().equals(c.getContainerId()))
102                 .findFirst().get();
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());
109     }
110
111 }