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.annotation.DirtiesContext;
49 import org.springframework.test.context.TestPropertySource;
50 import org.springframework.test.context.junit4.SpringRunner;
52 @RunWith(SpringRunner.class)
53 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
54 @TestPropertySource(locations = "classpath:robotframework/robotframework-test.properties")
56 public class RobotItCase {
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);
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
69 .withDockerCmdExecFactory(new NettyDockerCmdExecFactory())
73 BuildImageResultCallback callback = new BuildImageResultCallback() {
75 public void onNext(BuildResponseItem item) {
76 System.out.println("XXX ITEM " + item);
81 String imageId = client.buildImageCmd(robotFolder).exec(callback).awaitImageId();
82 CreateContainerResponse createContainerResponse = client.createContainerCmd(imageId)
83 .withVolumes(testsVolume)
85 new Bind(robotFolder.getAbsolutePath() + "/tests/", testsVolume, AccessMode.rw))
86 .withEnv("CLAMP_PORT=" + httpPort)
87 .withStopTimeout(TIMEOUT_S)
88 .withNetworkMode("host")
90 String id = createContainerResponse.getId();
91 client.startContainerCmd(id).exec();
92 InspectContainerResponse exec;
97 exec = client.inspectContainerCmd(id).exec();
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);
105 logContainerCmd.exec(new LogContainerResultCallback() {
107 public void onNext(Frame item) {
108 logger.info(item.toString());
110 }).awaitCompletion();
111 } catch (InterruptedException e) {
112 throw new Exception("Failed to retrieve logs of container " + id, e);
114 client.stopContainerCmd(id);