add robotframework to integration tests
[clamp.git] / src / test / java / org / onap / clamp / clds / it / RobotItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.it;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.github.dockerjava.api.DockerClient;
29 import com.github.dockerjava.api.command.BuildImageResultCallback;
30 import com.github.dockerjava.api.command.CreateContainerResponse;
31 import com.github.dockerjava.api.command.InspectContainerResponse;
32 import com.github.dockerjava.api.command.LogContainerCmd;
33 import com.github.dockerjava.api.model.AccessMode;
34 import com.github.dockerjava.api.model.Bind;
35 import com.github.dockerjava.api.model.BuildResponseItem;
36 import com.github.dockerjava.api.model.Frame;
37 import com.github.dockerjava.api.model.Volume;
38 import com.github.dockerjava.core.DockerClientBuilder;
39 import com.github.dockerjava.core.command.LogContainerResultCallback;
40 import com.github.dockerjava.netty.NettyDockerCmdExecFactory;
41 import java.io.File;
42 import java.util.Objects;
43 import org.junit.Assert;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.springframework.beans.factory.annotation.Value;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.test.context.TestPropertySource;
49 import org.springframework.test.context.junit4.SpringRunner;
50
51 @RunWith(SpringRunner.class)
52 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
53 @TestPropertySource(locations = "classpath:robotframework/robotframework-test.properties")
54 public class RobotItCase {
55
56     @Value("${server.port}")
57     private String httpPort;
58     private static final int TIMEOUT_S = 150;
59     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(RobotItCase.class);
60
61     @Test
62     public void robotTests() throws Exception {
63         File robotFolder = new File(getClass().getClassLoader().getResource("robotframework").getFile());
64         Volume testsVolume = new Volume("/opt/robotframework/tests");
65         DockerClient client = DockerClientBuilder
66                 .getInstance()
67                 .withDockerCmdExecFactory(new NettyDockerCmdExecFactory())
68                 .build();
69
70
71         BuildImageResultCallback callback = new BuildImageResultCallback() {
72             @Override
73             public void onNext(BuildResponseItem item) {
74                 System.out.println("XXX ITEM " + item);
75                 super.onNext(item);
76             }
77         };
78
79         String imageId = client.buildImageCmd(robotFolder).exec(callback).awaitImageId();
80         CreateContainerResponse createContainerResponse = client.createContainerCmd(imageId)
81                 .withVolumes(testsVolume)
82                 .withBinds(
83                         new Bind(robotFolder.getAbsolutePath() + "/tests/", testsVolume, AccessMode.rw))
84                 .withEnv("CLAMP_PORT=" + httpPort)
85                 .withStopTimeout(TIMEOUT_S)
86                 .withNetworkMode("host")
87                 .exec();
88         String id = createContainerResponse.getId();
89         client.startContainerCmd(id).exec();
90         InspectContainerResponse exec;
91
92         int tries = 0;
93         do {
94             Thread.sleep(1000);
95             exec = client.inspectContainerCmd(id).exec();
96             tries++;
97         } while (exec.getState().getRunning() && tries < TIMEOUT_S);
98         Assert.assertEquals(exec.getState().getError(), 0L,
99                 Objects.requireNonNull(exec.getState().getExitCodeLong()).longValue());
100         LogContainerCmd logContainerCmd = client.logContainerCmd(id);
101         logContainerCmd.withStdOut(true).withStdErr(true);
102         try {
103             logContainerCmd.exec(new LogContainerResultCallback() {
104                 @Override
105                 public void onNext(Frame item) {
106                     logger.info(item.toString());
107                 }
108             }).awaitCompletion();
109         } catch (InterruptedException e) {
110             throw new Exception("Failed to retrieve logs of container " + id, e);
111         }
112         client.stopContainerCmd(id);
113     }
114 }