Attempt to fix the verify job problem
[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.annotation.DirtiesContext;
49 import org.springframework.test.context.TestPropertySource;
50 import org.springframework.test.context.junit4.SpringRunner;
51
52 @RunWith(SpringRunner.class)
53 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
54 @TestPropertySource(locations = "classpath:robotframework/robotframework-test.properties")
55 @DirtiesContext
56 public class RobotItCase {
57
58     @Value("${server.port}")
59     private String httpPort;
60     private static final int TIMEOUT_S = 150;
61     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(RobotItCase.class);
62
63     @Test
64     public void robotTests() throws Exception {
65         File robotFolder = new File(getClass().getClassLoader().getResource("robotframework").getFile());
66         Volume testsVolume = new Volume("/opt/robotframework/tests");
67         DockerClient client = DockerClientBuilder
68                 .getInstance()
69                 .withDockerCmdExecFactory(new NettyDockerCmdExecFactory())
70                 .build();
71
72
73         BuildImageResultCallback callback = new BuildImageResultCallback() {
74             @Override
75             public void onNext(BuildResponseItem item) {
76                 System.out.println("XXX ITEM " + item);
77                 super.onNext(item);
78             }
79         };
80
81         String imageId = client.buildImageCmd(robotFolder).exec(callback).awaitImageId();
82         CreateContainerResponse createContainerResponse = client.createContainerCmd(imageId)
83                 .withVolumes(testsVolume)
84                 .withBinds(
85                         new Bind(robotFolder.getAbsolutePath() + "/tests/", testsVolume, AccessMode.rw))
86                 .withEnv("CLAMP_PORT=" + httpPort)
87                 .withStopTimeout(TIMEOUT_S)
88                 .withNetworkMode("host")
89                 .exec();
90         String id = createContainerResponse.getId();
91         client.startContainerCmd(id).exec();
92         InspectContainerResponse exec;
93
94         int tries = 0;
95         do {
96             Thread.sleep(1000);
97             exec = client.inspectContainerCmd(id).exec();
98             tries++;
99         } while (exec.getState().getRunning() && tries < TIMEOUT_S);
100         Assert.assertEquals(exec.getState().getError(), 0L,
101                 Objects.requireNonNull(exec.getState().getExitCodeLong()).longValue());
102         LogContainerCmd logContainerCmd = client.logContainerCmd(id);
103         logContainerCmd.withStdOut(true).withStdErr(true);
104         try {
105             logContainerCmd.exec(new LogContainerResultCallback() {
106                 @Override
107                 public void onNext(Frame item) {
108                     logger.info(item.toString());
109                 }
110             }).awaitCompletion();
111         } catch (InterruptedException e) {
112             throw new Exception("Failed to retrieve logs of container " + id, e);
113         }
114         client.stopContainerCmd(id);
115     }
116 }