title: Creating a testing infrastructure with Pytest , Selenium Grid and Docker
body: <p>Based on this <a href="http://www.conductor.com/nightlight/running-selenium-grid-using-docker-compose/" rel="nofollow">article</a>, I successfully created the scalable selenium grid .
  Then I want to run my test suites [written in Python] using Pytest into that Grid.</p>
  
  <p>I am new to Docker and trying to find the best way to migrate my testing procedures into the microservices architecture. Essentially I want to give the ability to the devs to setup a full testing infrastructure locally on their PCs.</p>
  
  <p>So, I have 4 <em>Dockerfiles</em> and 1 <em>docker-compose.yml</em> .</p>
  
  <p>The BASE dockerfile:</p>
  
  <pre><code>FROM ubuntu
  
  ENV SEL_VERSION 2.44.0
  
  # Update repos for Java
  RUN apt-get update -qqy \
  &amp;&amp; apt-get -qqy --no-install-recommends install \
  software-properties-common \
  &amp;&amp; rm -rf /var/lib/apt/lists/*
  RUN add-apt-repository -y ppa:webupd8team/java
  
  RUN echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections
  RUN echo debconf shared/accepted-oracle-license-v1-1 seen true | debconf-set-selections
  
  # Install Java7
  RUN apt-get update -qqy \
  &amp;&amp; apt-get -qqy --no-install-recommends install \
  oracle-java7-installer \
  &amp;&amp; rm -rf /var/lib/apt/lists/*
  
  # Download Selenium Server
  RUN wget http://selenium-release.storage.googleapis.com/${SEL_VERSION%.*}/selenium-server-standalone-${SEL_VERSION}.jar
  </code></pre>
  
  <p>The HUB dockerfile :</p>
  
  <pre><code>FROM org/grid:base
  
  EXPOSE 4444
  
  # Add and set permissions to the script that will launch the hub
  ADD register-hub.sh /var/register-hub.sh
  RUN chmod 755 /var/register-hub.sh
  
  # start a shell and run the script
  #WORKDIR /
  CMD ["/bin/bash", "/var/register-hub.sh"]
  </code></pre>
  
  <p>The NODE dockerfile :</p>
  
  <pre><code>FROM org/grid:base
  
  # set the FF version to use
  ENV FIREFOX_MINOR 34.0.5
  
  # Update and install what's needed
  RUN apt-get update -qqy \
  &amp;&amp; apt-get -qqy --no-install-recommends install \
  firefox \
  xvfb \
  bzip2 \
  &amp;&amp; rm -rf /var/lib/apt/lists/*
  
  # setup FF
  RUN [ -e /usr/bin/firefox ] &amp;&amp; rm /usr/bin/firefox
  ADD https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/${FIREFOX_MINOR}/linux-x86_64/en-US/firefox-${FIREFOX_MINOR}.tar.bz2 /tmp/
  RUN apt-get install -q -y libdbus-glib-1-2
  RUN tar -xvjf /tmp/firefox-${FIREFOX_MINOR}.tar.bz2 -C /opt/
  RUN chmod -R +x /opt/firefox/
  RUN ln -s /opt/firefox/firefox /usr/bin/firefox
  
  # add and set permissions for the bash script to register the node
  ADD register-node.sh /var/register-node.sh
  RUN chmod 755 /var/register-node.sh
  
  # start a shell and run the script
  CMD ["/bin/bash", "/var/register-node.sh"]
  </code></pre>
  
  <p>And the PYTEST dockerfile :</p>
  
  <pre><code># Starting from base image
  FROM ubuntu
  
  # Set the Github personal token [to clone the QA code]
  #todo------secret------
  ENV GH_TOKEN some_token_some_token_some_token_
  
  # Install Python &amp; pip
  RUN apt-get update
  RUN apt-get upgrade -y
  RUN apt-get install -y python python-pip python-dev &amp;&amp; pip install --upgrade pip
  
  # Install GIT
  RUN apt-get update -y &amp;&amp; apt-get install git -y
  
  # [in the / folder] Create the folder and add the whole project from the repo to the container
  RUN git clone https://$GH_TOKEN:x-oauth-basic@github.com/org/org_QA.git /org_QA_folder
  
  # Install dependencies via pip
  WORKDIR /org_QA_folder
  RUN pip install -r dependencies.txt
  
  #
  CMD /bin/bash
  </code></pre>
  
  <p>And the docker-compose file :</p>
  
  <pre><code>hub:
    image: org/grid:hub # image must already exist
    ports:
      - "4444:4444" # HOST:CONTAINER
    external_links: # link to a container created outside of this YAML file
      - pytest
    volumes_from:
      - pytest # must be created first
  
  firefox:
    image: org/grid:nodeff # image must already exist
    links:
      - hub
    expose:
      - "5555" # grid console open to public
  </code></pre>
  
  <p>So...I cannot understand how should I run something like "py.test /org_QA_folder/testcase.py" and run on the grid [essentially the node].</p>
  
  <p>I first run the pytest container  with 
  <code>docker run -dit -v /org_QA_folder --name pytest schoox/grid:py</code> and the other services with <code>docker-compose up -d</code> .</p>
  
  <p>I tried multiple approaches:</p>
  
  <ol>
  <li><p>Not use a Pytest container but clone the code in the hub. But in this case , HUB container runs the hub of the grid [one CMD per container directive] . <strong>[EDIT: I chose the microservices architecture as Docker suggests]</strong></p></li>
  <li><p>I leave the hub and node containers to run their specified services, and create a separate container [pytest] to send the tests from there. But the test wont run cause this container does not have xvfb.<strong>[EDIT: I just installed xvfb and worked]</strong> </p></li>
  <li><p>I tried to use the pytest container as volume for the hub but remains the problem of running the tests from the hub container that already has a service running.<strong>[EDIT: For dev purposes I mount the code from the pytest container and use its console to test stuff on the other containers. After that I will use the pytest container to have the code separate]</strong></p></li>
  </ol>
  
  <p>Should I install all the packages ,that node container has, in the other containers? Ideally I want to use the pytest container console [to develop the infrastructure at least] to run the tests which will be sent to hub and run to many nodes.<strong>[EDIT: I didn't mingled with the hub and node, I just installed xvfb on pytest container]</strong></p>
  
  <p>How would you suggest to do that? Am I missing something ? </p>
  
  <p><strong>EDIT [steps towards solution]:</strong></p>
  
  <p>I first start the pytest container [<code>docker run -dit -v /org/org_QA_folder --name pytest org/grid:py</code>] and then I run <code>docker-compose up -d</code> to launch the grid</p>
  
  <p>In the compose file I use <code>external_links</code> and <code>volumes_from</code> for dev purposes. I wont work from inside any container when the setup is finished.</p>
  
  <p>I changed the Pytest dockerfile and added this:</p>
  
  <pre><code># Install xvfb [THAT WAS NEEDED!!!!!]
  RUN apt-get install -y xvfb
  </code></pre>
  
  <p>I run one of my tests ,from inside the pytest container, to my already running Grid[on an EC2 instance] as a proof of concept and worked fine. It remains to send it to the dockerized grid. </p>