Git Product home page Git Product logo

sokoban's Introduction

Sokoban

Build Status

npm version Dependency Status devDependency Status

Sokoban moves Docker containers around. Meant for usage in an E2E testing environment. Note: Sokoban was written to aid in testing Docker-based systems and was not tested in production.

Installation

npm install sokoban

Prerequisites

Docker needs to be installed and working on your local machine. Plesae follow the instructions on the Docker website, then make sure the installation was successful by running docker run hello-world from your terminal.

Usage

Instantiate in your test code and run containers once in a before hook, then call killAll in an after hook.

Getting Started

const Sokoban = require('sokoban');
const sokoban = new Sokoban();

sokoban.run({imageName: "hello-world", containerName: "hello"});

Dude, where's my Docker?

Being a TDD-supporting project, Sokoban is meant to run on multiple types of environments: Linux, OS X, Windows and from inside other Docker containers (for instance, in CI environment). Since the major part of containers running on Sokoban are servers, we need Sokoban to be able to tell us the host and port where we can access these servers. Unfortunately, finding out the host to which the containers are bound is trickier than you might initially think, and it depends on when Sokoban (or the test running Sokoban) is executed. This could be:

  • localhost - if the tests are running on the same machine where Docker is running; this could be either on a Linux dev machine, or in CI, if the same machine happens to be running both Docker and the CI agent iteslf.
  • process.env.DOCKER_HOST - on an OS X or Windows dev machine, this environment variable will point to the Docker Machine IP address.
  • Queryable by calling ip route and looking for the default gateway - but only if we're running inside a Docker container
  • completely unknown

Sokoban will attempt to resolve using the DOCKER_HOST environment variable, then by calling ip route, but if you know the IP of your Docker host it's best to pass it when instantiating Sokoban:

const sokoban = new Sokoban({hostname: "my.docker.host"});

Randomizing container names

By default, if you do not specify container names, Docker will choose them for you. Sokoban mandates that you specify a name - to make linking containers less painful, and to aid you in reading the app logs. However, when running multiple tests on the same Docker host, you could end up with a name collision - for instance, if two tests attempt to create a container named redis. Sokoban solves this by adding a random suffix to the container name, if the randomizeNames option is turned on:

const sokoban = new Sokoban({randomizeNames: true});

Running a container

Sokoban.run() takes an options object with the following properties, and returns a promise, to be resolved when the container has started.

  • imageName - mandatory, the image to run.
  • containerName - mandatory, the container to run.
  • ports - an object describing a single TCP port binding with from and to properties
  • publishAllPorts - instead of using ports, you can specify this property, which instructs Docker to bind all exposed ports to randomly-allocated ports
  • volumes - an mapping of volumes to bind
  • links - an object describing links to other containers
  • env - an object where keys represent environment variable names and values represent their respective values, to be passed to the container upon startup
  • barrier - a function that checks that the container has finished starting up. The run function will not resolve the promise until this barrier function returned successfully (i.e. did not throw an exception)
  • maxRetries - how many times to retry on the barrier before giving up

Examples

Running GitLab with Redis and Postgres:

const db = "postgresql-gitlab";
const cache = "redis-gitlab";
const app = "gitlab";
    
return Promise.join(
    sokoban.run({imageName: "quay.io/sameersbn/postgresql:latest",
                 containerName: db,
                 volumes: {"/srv/docker/gitlab/postgresql": "/var/lib/postgresql"},
                 env: {DB_NAME: "gitlabhq_production", DB_USER: "gitlab", DB_PASS: "password"}}),

    sokoban.run({imageName: "quay.io/sameersbn/redis:latest", 
                 containerName: cache, 
                 volumes: {"/srv/docker/gitlab/redis": "/var/lib/redis"}}),

  (dbInfo, cacheInfo) => sokoban.run({
      imageName: "quay.io/sameersbn/gitlab:latest",
      containerName: app,
      publishAllPorts: true,
      links: linksFor(dbInfo, cacheInfo),
      env: {GITLAB_SECRETS_DB_KEY_BASE: "foobar"}
  })
);

function linksFor(postgresInfo, redisInfo) {
    const links = {};
    // the values are the link names as expected by the Gitlab image
    links[postgresInfo.containerName] = "postgresql"; 
    links[redisInfo.containerName] = "redisio";
    return links;
}

(Setup taken from https://github.com/sameersbn/docker-gitlab)

Contribution

This project is very early stage and is under heavy development. Please feel free to open issues for any missing functionality, etc.

sokoban's People

Contributors

electricmonk avatar

Stargazers

Rafa Mel avatar Max P. avatar

Watchers

Imran Nazar avatar Shmulik Flint avatar Evgeny Boxer avatar yaron nachshon avatar Slava Pozdniakov avatar Shani Koren avatar Luis Godinez avatar Valentin Lavrinenko avatar Ivan Kamenev avatar Hed avatar James Cloos avatar Jonathan Bensaid avatar  avatar yaniv even haim avatar  avatar Eyal Yaniv avatar Adam Matan avatar Tanya avatar Yoav Abrahami avatar Boris Litvinsky avatar Dor Raba avatar Ofir Attal avatar Danny Steinhoff avatar Constantin Farber avatar Yoav Amit avatar Avi Haimov avatar Mykyta Shyrin avatar Shai Ben-Yehuda avatar Taras Danylov avatar  avatar Omar Shibli avatar Algimantas Krasauskas avatar  avatar Mariya Feldman avatar Andrey Moiseev avatar Denys avatar Tom Peres avatar Gilad Artzi avatar Mantas Indrašius avatar Guy Sopher avatar Ofer LaOr avatar  avatar Jonathan Adler avatar Shai Nagar avatar Kristina avatar avgar marciano avatar Ariel avatar Noam Katzir avatar Rob Rush avatar Ziv Levy avatar Yaniv Cohen avatar  avatar Ihor Krysenko avatar JJ Major avatar  avatar Roman Shevchuk avatar Guy Ben Moshe avatar Elia Grady avatar Sagy Rozman avatar itay shmool avatar  avatar  gal sharir avatar Dovile avatar Nikita Bunchuk avatar Valera avatar Jony Vesterman Cohen avatar Andrey Makarov avatar Dan Bar-Shalom avatar Vytenis Butkevicius avatar Matan Lieberman avatar David Zuckerman avatar Uri Tagger avatar Oleksii Makodzeba avatar Lev Vidrak avatar Yuval Aviyam avatar Daniel Bond avatar Ohad Raz avatar Olesia Zaichenko avatar Andrii Ziabriev avatar Albert Elrom avatar  avatar Maor Hamami avatar Noam Moravtchik avatar Gregory Bondar avatar Gil Meir avatar Dmitriy Kovalenko avatar Alan Brian Dardic avatar Yossi Ankri avatar nadav abrahami avatar Dror avatar Evaldas avatar Ward Abbass avatar Vadym Mateushev avatar IngaS avatar Jurij Mikelionis avatar Sasha Kurapin avatar  avatar uri bechar avatar Vladimir Sorkin avatar Alexander Jdanov avatar

sokoban's Issues

enable to tell sokoban to pull force image

I have a use case, where I want to tell sokoban to pull image even if it's already present on docker machine, because now it checks if image is present and if so, it does not pull.

And in some cases I have new version of image, but it's not on docker machine where containers are running. Do you agree if I will add this capability?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.