View Javadoc
1   package org.kuali.common.devops.docker;
2   
3   import static com.google.common.base.Preconditions.checkState;
4   import static java.lang.String.format;
5   import static org.apache.commons.io.IOUtils.closeQuietly;
6   import static org.kuali.common.core.io.Files.getCanonicalFile;
7   import static org.kuali.common.util.FormatUtils.getTime;
8   import static org.kuali.common.util.log.Loggers.newLogger;
9   
10  import java.io.File;
11  import java.io.InputStream;
12  
13  import org.junit.Test;
14  import org.kuali.common.core.json.api.JsonService;
15  import org.kuali.common.core.json.jackson.JacksonJsonService;
16  import org.slf4j.Logger;
17  
18  import com.amazonaws.util.IOUtils;
19  import com.fasterxml.jackson.databind.JsonNode;
20  import com.github.dockerjava.api.DockerClient;
21  import com.github.dockerjava.core.DockerClientBuilder;
22  import com.google.common.base.Splitter;
23  import com.google.common.base.Stopwatch;
24  
25  public class DockerBuildImageTest {
26  
27      private static final Logger logger = newLogger();
28  
29      @Test
30      public void test() {
31          try {
32              String url = "http://localhost:2375";
33              DockerClient client = DockerClientBuilder.getInstance(url).build();
34              File folder = getCanonicalFile("./src/main/resources/org/kuali/common/kuali-devops/docker/jcaddel/ubuntu");
35              String tag = "jcaddel/ubuntu:trusty";
36              InputStream response = client.buildImageCmd(folder).withTag(tag).exec();
37              String content = IOUtils.toString(response);
38              closeQuietly(response);
39              JsonService json = new JacksonJsonService();
40              JsonNode node = json.readString(content, JsonNode.class);
41              info(json, node);
42          } catch (Throwable e) {
43              e.printStackTrace();
44          }
45      }
46  
47      /**
48       * Convert the object reference to json.
49       *
50       * Create a new object instance from the json and then create a new json string from the new object instance.
51       *
52       * Verify that the original json string and the new json string are exactly the same.
53       */
54      protected static <T> T checkPerfectReadWrite(JsonService json, T reference, Class<T> type) {
55          String expected = json.writeString(reference);
56          String actual = json.writeString(json.readString(expected, type));
57          checkState(expected.equals(actual));
58          return reference;
59      }
60  
61      protected static void elapsed(Stopwatch sw) {
62          info("elapsed -> %s", getTime(sw));
63      }
64  
65      protected static <T> void info(JsonService json, T reference) {
66          String text = json.writeString(reference);
67          Iterable<String> lines = Splitter.on('\n').split(text);
68          for (String line : lines) {
69              info("%s", line);
70          }
71      }
72  
73      protected static void debug(String msg, Object... args) {
74          logger.debug((args == null || args.length == 0) ? msg : format(msg, args));
75      }
76  
77      protected static void info(String msg, Object... args) {
78          logger.info((args == null || args.length == 0) ? msg : format(msg, args));
79      }
80  
81  }