2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.it;
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;
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;
51 @RunWith(SpringRunner.class)
52 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
53 @TestPropertySource(locations = "classpath:robotframework/robotframework-test.properties")
54 public class RobotItCase {
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);
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
67 .withDockerCmdExecFactory(new NettyDockerCmdExecFactory())
71 BuildImageResultCallback callback = new BuildImageResultCallback() {
73 public void onNext(BuildResponseItem item) {
74 System.out.println("XXX ITEM " + item);
79 String imageId = client.buildImageCmd(robotFolder).exec(callback).awaitImageId();
80 CreateContainerResponse createContainerResponse = client.createContainerCmd(imageId)
81 .withVolumes(testsVolume)
83 new Bind(robotFolder.getAbsolutePath() + "/tests/", testsVolume, AccessMode.rw))
84 .withEnv("CLAMP_PORT=" + httpPort)
85 .withStopTimeout(TIMEOUT_S)
86 .withNetworkMode("host")
88 String id = createContainerResponse.getId();
89 client.startContainerCmd(id).exec();
90 InspectContainerResponse exec;
95 exec = client.inspectContainerCmd(id).exec();
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);
103 logContainerCmd.exec(new LogContainerResultCallback() {
105 public void onNext(Frame item) {
106 logger.info(item.toString());
108 }).awaitCompletion();
109 } catch (InterruptedException e) {
110 throw new Exception("Failed to retrieve logs of container " + id, e);
112 client.stopContainerCmd(id);