最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to run Karma tests from docker container? - Stack Overflow

programmeradmin5浏览0评论

I've recently moved my node.js app into a docker image and I'd like to run my tests inside the image. My mocha/node tests work fine but the Karma tests involve starting Chrome to run the tests and Chrome isn't installed in the container.

How do I go about addressing this?

  • Install Chrome in the Container? Seems less than ideal as I don't want to ship Chrome to my production servers inside the container.
  • Somehow allow it to connect to Chrome on the host?
  • Create a new image that inherits from my app image and adds Chrome and other things?

Googling 'docker & karma' reveals docker images out there but I can't find instructions on how to think about the problem and the best approach.

I've recently moved my node.js app into a docker image and I'd like to run my tests inside the image. My mocha/node tests work fine but the Karma tests involve starting Chrome to run the tests and Chrome isn't installed in the container.

How do I go about addressing this?

  • Install Chrome in the Container? Seems less than ideal as I don't want to ship Chrome to my production servers inside the container.
  • Somehow allow it to connect to Chrome on the host?
  • Create a new image that inherits from my app image and adds Chrome and other things?

Googling 'docker & karma' reveals docker images out there but I can't find instructions on how to think about the problem and the best approach.

Share Improve this question asked Dec 15, 2015 at 10:05 MichaelJonesMichaelJones 1,4062 gold badges15 silver badges24 bronze badges 3
  • have a look at github./jfrazelle/dockerfiles/blob/master/chrome/stable/… about Chrome – user2915097 Commented Dec 15, 2015 at 10:09
  • Thanks I've read the blog post behind that before but it doesn't clarify for me how to proceed in this case. – MichaelJones Commented Dec 15, 2015 at 10:43
  • if you go with "Create a new image that inherits from my app image and adds Chrome and other things?", you will need supervisor docs.docker./engine/articles/using_supervisord or such (s6, runit, daemon tools...) to manager your processes – user2915097 Commented Dec 15, 2015 at 10:45
Add a ment  | 

4 Answers 4

Reset to default 3

I've found this docker image to be an excellent starting point for running karma tests quickly inside a docker container on Concourse CI:

https://hub.docker./r/markadams/chromium-xvfb-js/

It contains node 6.x (latest) + npm and a headless chromium instance using X virtual framebuffer. Working great for me!

You can try this (tested in Alpine Linux):

Option 1

  1. Ensure that the 9876 port is accessible from the host.

  2. Install chromium:

     apk add chromium # NOT google-chrome-stable
    
  3. Create a wrapper script that starts chromium-browser in headless mode:

     vi /usr/bin/google-chrome-stable
    

    Add the following lines:

     #!/bin/sh
    
     chromium \
     --no-sandbox \
     --headless \
     --disable-gpu \
     --remote-debugging-port=9222 \
     "$@"
    
  4. Make it executable:

     chmod +x /usr/bin/google-chrome-stable
    
  5. Run the tests in headless mode:

     ng test --browsers ChromeHeadless
    

Option 2

You can also run the browser in a virtual X Server called Xvfb:

  1. Remove the --headless option in the /usr/bin/google-chrome-stable script.

  2. Start the server and export the DISPLAY vairable:

     Xvfb :1 -ac -screen 0 1024x768x24 &
     export DISPLAY=:1
    
  3. Run the tests:

     ng test
    

There is also protractor-runner project, and Karma is similar to running Protractor, and so you might be able to learn how to do it from that example. But, I prefer the suggestion to use chromium-xvfb-js image.

Use PhantomJS instead of Chrome. Karma has PhantomJS launcher.

PhantomJS based on Blink.

Install PhantomJS:

npm karma-phantomjs-launcher --save-dev

Add to Karma config:

module.exports = function(config) {
  config.set({
    browsers : ['PhantomJS']
  });
};

EDIT

Here is part od Dockerfile

ENV NODE_VERSION 0.12.7
ENV NPM_VERSION 3.5.1
ENV PHANTOM_JS phantomjs-1.9.8-linux-x86_64

# App and test
RUN set -x \
    && buildDeps='curl git bzip2 file libfreetype6 libfontconfig1 python-pip python-dev libpq-dev libmemcached-dev libzmq-dev libjpeg62-turbo-dev zlib1g-dev libtiff5-dev make g++ psmisc' \
    && apt-get update \
    && apt-get install -y $buildDeps --no-install-remends \
    && rm -rf /var/lib/apt/lists/* \
    && curl -SLO "https://bitbucket/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2" \
    && tar xvjf $PHANTOM_JS.tar.bz2 \
    && mv $PHANTOM_JS /usr/local/share \
    && ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin \
    && curl -SLO "https://nodejs/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz" \
    && curl -SLO "https://nodejs/dist/v$NODE_VERSION/SHASUMS256.txt.asc" \
    && gpg --verify SHASUMS256.txt.asc \
    && grep " node-v$NODE_VERSION-linux-x64.tar.gz\$" SHASUMS256.txt.asc | sha256sum -c - \
    && tar -xzf "node-v$NODE_VERSION-linux-x64.tar.gz" -C /usr/local --strip-ponents=1 \
    && rm "node-v$NODE_VERSION-linux-x64.tar.gz" SHASUMS256.txt.asc \
    && npm install -g npm@"$NPM_VERSION" grunt-cli \
    && npm install \
    && npm cache clear \
    && ./node_modules/.bin/bower install --allow-root \
    && npm test \
    && apt-get purge -y --auto-remove $buildDeps \
    && rm -rf /usr/local/bin/node /usr/local/bin/npm /usr/local/include/node /usr/local/lib/node_modules \
    && rm -rf /usr/local/share/$PHANTOM_JS /usr/local/bin/phantomjs

In this case Docker made one layer. In this layer

  1. install libs and tools (debian jessie)
  2. install PhantomJS
  3. install NodeJS
  4. run tests
  5. remove libs and tools
  6. remove NodeJS
  7. remove PhantomJS

Tests are running and You don't have PhantomJS in production container.

发布评论

评论列表(0)

  1. 暂无评论