View Javadoc
1   /**
2    * This file Copyright (c) 2017 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.integrationtests.docker;
35  
36  import org.junit.runner.Description;
37  import org.testcontainers.containers.BindMode;
38  import org.testcontainers.containers.GenericContainer;
39  import org.testcontainers.containers.SeleniumUtils;
40  import org.testcontainers.containers.traits.LinkableContainer;
41  import org.testcontainers.containers.wait.LogMessageWaitStrategy;
42  
43  /**
44   * Forked from {@link org.testcontainers.containers.BrowserWebDriverContainer similar TestContainers' class}.
45   * Does not provide a web driver itself, but rather starts a Selenium node and attaches it to a configured hub.
46   */
47  public class SeleniumGridNodeContainer extends GenericContainer<SeleniumGridNodeContainer> implements LinkableContainer {
48  
49      private static final String CHROME_IMAGE = "selenium/node-chrome%s:%s";
50      private static final String FIREFOX_IMAGE = "selenium/node-firefox%s:%s";
51  
52      private static final int VNC_PORT = 5900;
53  
54      private boolean debug = false;
55  
56      private String targetBrowser;
57  
58      public SeleniumGridNodeContainer() {
59          this.waitStrategy = new LogMessageWaitStrategy().withRegEx(".*node is registered.*\n");
60      }
61  
62      @Override
63      protected Integer getLivenessCheckPort() {
64          return getMappedPort(VNC_PORT);
65      }
66  
67      public SeleniumGridNodeContainer withDebugMode(boolean isDebugModeOn) {
68          this.debug = isDebugModeOn;
69          return this;
70      }
71  
72      public SeleniumGridNodeContainer withTargetBrowser(String targetBrowser) {
73          this.targetBrowser = targetBrowser;
74          return this;
75      }
76  
77      public SeleniumGridNodeContainer withSeleniumHubHost(String seleniumHubHost) {
78          return withEnv("HUB_PORT_4444_TCP_ADDR", seleniumHubHost);
79      }
80  
81      public SeleniumGridNodeContainer withSeleniumHubPort(int seleniumHubPort) {
82          return withEnv("HUB_PORT_4444_TCP_PORT", String.valueOf(seleniumHubPort));
83      }
84  
85      public SeleniumGridNodeContainer withUploadDirectory(String uploadDirectory) {
86          return withFileSystemBind(uploadDirectory, "/var/upload", BindMode.READ_ONLY);
87      }
88  
89      public SeleniumGridNodeContainer withTestNetwork(String testNetwork) {
90          return withCreateContainerCmdModifier(cmd -> cmd.withNetworkMode(testNetwork));
91      }
92  
93      @Override
94      protected void configure() {
95  
96          String timeZone = System.getProperty("user.timezone");
97  
98          if (timeZone == null || timeZone.isEmpty()) {
99              timeZone = "Etc/UTC";
100         }
101 
102         addFileSystemBind("/dev/shm", "/dev/shm", BindMode.READ_WRITE);
103         setDockerImageName(getImageForSelectedBrowser());
104         
105         addExposedPorts(VNC_PORT);
106         addEnv("TZ", timeZone);
107         addEnv("no_proxy", "localhost");
108         setCommand("/opt/bin/entry_point.sh");
109 
110         addEnv("SCREEN_DEPTH", "24");
111         addEnv("SCREEN_WIDTH", "1280");
112         addEnv("SCREEN_HEIGHT", "1024");
113 
114         /*
115          * Some unreliability of the selenium browser containers has been observed, so allow multiple attempts to start.
116          */
117         setStartupAttempts(3);
118     }
119 
120     public String getImageForSelectedBrowser() {
121 
122         final String seleniumVersion = SeleniumUtils.determineClasspathSeleniumVersion();
123         final String debugSuffix = debug ? "-debug" : "";
124 
125         switch (targetBrowser) {
126         case "chrome":
127             return String.format(CHROME_IMAGE, debugSuffix, seleniumVersion);
128         case "firefox":
129             return String.format(FIREFOX_IMAGE, debugSuffix, seleniumVersion);
130         default:
131             throw new UnsupportedOperationException(String.format("Browser name must be 'chrome' or 'firefox'; provided '%s' is not supported", targetBrowser));
132         }
133     }
134 
135     @Override
136     protected void finished(Description description) {
137         this.stop();
138     }
139 }
140