Răsfoiți Sursa

Merge pull request #246 from kalliope-project/docker_CI

Docker ci
Monf 8 ani în urmă
părinte
comite
d4f293fc37

+ 14 - 1
.travis.yml

@@ -1,8 +1,15 @@
 language: python
+
+services:
+  - docker
+
 python:
   - "2.7"
+
 # command to install dependencies
 before_install:
+- docker build --force-rm=true --build-arg TRAVIS_BRANCH=${TRAVIS_BRANCH} --build-arg TRAVIS_EVENT_TYPE=${TRAVIS_EVENT_TYPE} --build-arg TRAVIS_PULL_REQUEST_SLUG=${TRAVIS_PULL_REQUEST_SLUG} --build-arg TRAVIS_PULL_REQUEST_BRANCH=${TRAVIS_PULL_REQUEST_BRANCH} -t kalliope-ubuntu1604 -f docker/ubuntu_16_04.dockerfile .
+- docker build --force-rm=true --build-arg TRAVIS_BRANCH=${TRAVIS_BRANCH} --build-arg TRAVIS_EVENT_TYPE=${TRAVIS_EVENT_TYPE} --build-arg TRAVIS_PULL_REQUEST_SLUG=${TRAVIS_PULL_REQUEST_SLUG} --build-arg TRAVIS_PULL_REQUEST_BRANCH=${TRAVIS_PULL_REQUEST_BRANCH} -t kalliope-debian8 -f docker/debian8.dockerfile .
 - sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu trusty main restricted universe multiverse"
 - sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse"
 - sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse"
@@ -11,6 +18,12 @@ before_install:
 - sudo apt-get install libstdc++6
 - wget https://bootstrap.pypa.io/get-pip.py
 - sudo python get-pip.py
+
 install: "pip install -r install/files/python_requirements.txt"
+
 # command to run tests
-script: pytest
+script:
+  - pytest
+  - docker run -it --rm kalliope-ubuntu1604
+  - docker run -it --rm kalliope-debian8
+

+ 2 - 0
Tests/test_brain_loader.py

@@ -16,6 +16,8 @@ from kalliope.core.Models.Settings import Settings
 class TestBrainLoader(unittest.TestCase):
 
     def setUp(self):
+        # be sure the brain haven't been instantiated before
+        Singleton._instances = {}
         if "/Tests" in os.getcwd():
             self.brain_to_test = os.getcwd() + os.sep + "brains/brain_test.yml"
         else:

+ 3 - 0
Tests/test_rest_api.py

@@ -14,6 +14,9 @@ from kalliope.core.RestAPI.FlaskAPI import FlaskAPI
 
 class TestRestAPI(LiveServerTestCase):
 
+    def tearDown(self):
+        Singleton._instances = {}
+
     def create_app(self):
         """
         executed once at the beginning of the test

+ 36 - 0
docker/clone_and_test.sh

@@ -0,0 +1,36 @@
+#!/usr/bin/env bash
+
+# get travis env
+echo "TRAVIS_EVENT_TYPE: ${TRAVIS_EVENT_TYPE}"
+echo "TRAVIS_BRANCH: ${TRAVIS_BRANCH}"
+echo "TRAVIS_PULL_REQUEST_SLUG: ${TRAVIS_PULL_REQUEST_SLUG}"
+echo "TRAVIS_PULL_REQUEST_BRANCH: ${TRAVIS_PULL_REQUEST_BRANCH}"
+
+# set default variable if not in env
+if [ -z ${TRAVIS_EVENT_TYPE} ]; then
+TRAVIS_EVENT_TYPE="push";
+echo "TRAVIS_EVENT_TYPE set to push";
+fi
+
+if [ -z ${TRAVIS_BRANCH} ]; then
+TRAVIS_BRANCH="master";
+echo "TRAVIS_BRANCH set to master";
+fi
+
+# check if this is a pull request or a push
+if [ ${TRAVIS_EVENT_TYPE} == "pull_request" ]; then
+    git clone https://github.com/${TRAVIS_PULL_REQUEST_SLUG} kalliope;
+    cd kalliope;
+    git checkout ${TRAVIS_PULL_REQUEST_BRANCH};
+else
+    # it's a push
+    git clone https://github.com/kalliope-project/kalliope.git kalliope;
+    cd kalliope;
+    git checkout ${TRAVIS_BRANCH};
+fi
+
+# install
+sudo python setup.py install
+
+# tests
+python -m unittest discover

+ 41 - 0
docker/debian8.dockerfile

@@ -0,0 +1,41 @@
+FROM debian:8
+
+ENV no_proxy="127.0.0.1,localhost,kalliope.fr"
+
+# Need some packages in non-free repo
+RUN sed -i -- 's/jessie main/jessie main contrib non-free/g' /etc/apt/sources.list
+
+# install packages
+RUN apt-get update && apt-get install -y \
+    git python-dev libsmpeg0 libttspico-utils libsmpeg0 \
+    flac dialog portaudio19-dev build-essential libssl-dev \
+    libffi-dev sox libatlas3-base mplayer wget vim sudo\
+    && rm -rf /var/lib/apt/lists/*
+
+# Install the last PIP
+RUN wget https://bootstrap.pypa.io/get-pip.py
+RUN python get-pip.py
+
+# add a standart user. tests must not be ran as root
+RUN useradd -m -u 1000 tester
+RUN usermod -aG sudo tester
+RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
+
+# get TRAVIS environment variables
+ARG TRAVIS_BRANCH
+ARG TRAVIS_EVENT_TYPE
+ARG TRAVIS_PULL_REQUEST_SLUG
+ARG TRAVIS_PULL_REQUEST_BRANCH
+ENV TRAVIS_BRANCH=${TRAVIS_BRANCH}
+ENV TRAVIS_EVENT_TYPE=${TRAVIS_EVENT_TYPE}
+ENV TRAVIS_PULL_REQUEST_SLUG=${TRAVIS_PULL_REQUEST_SLUG}
+ENV TRAVIS_PULL_REQUEST_BRANCH=${TRAVIS_PULL_REQUEST_BRANCH}
+
+ADD docker/clone_and_test.sh /home/tester/clone_and_test.sh
+RUN chown tester /home/tester/clone_and_test.sh
+
+USER tester
+WORKDIR /home/tester
+
+# run tests
+CMD ./clone_and_test.sh

+ 51 - 0
docker/docker.md

@@ -0,0 +1,51 @@
+# Docker deployment and tests
+
+## Create images
+
+### Ubuntu 16.04
+Build the image for Ubuntu 16.04. By default, the master branch of the Kalliope project will be cloned.
+```
+docker build --force-rm=true -t kalliope-ubuntu1604 -f docker/ubuntu_16_04.dockerfile .
+```
+
+To build with TRAVIS env we need to send global variables
+```
+docker build \
+--force-rm=true \
+--build-arg TRAVIS_BRANCH=${TRAVIS_BRANCH} \
+--build-arg TRAVIS_EVENT_TYPE=${TRAVIS_EVENT_TYPE} \
+--build-arg TRAVIS_PULL_REQUEST_SLUG=${TRAVIS_PULL_REQUEST_SLUG} \
+--build-arg TRAVIS_PULL_REQUEST_BRANCH=${TRAVIS_PULL_REQUEST_BRANCH} \
+-t kalliope-ubuntu1604 \
+-f docker/ubuntu_16_04.dockerfile .
+```
+
+### Debian Jessie
+Build the image for Debian Jessie. By default, the master branch of the Kalliope project will be cloned.
+```
+docker build --force-rm=true -t kalliope-debian8 -f docker/debian8.dockerfile .
+```
+
+To build with TRAVIS env we need to send global variables
+```
+docker build \
+--force-rm=true \
+--build-arg TRAVIS_BRANCH=${TRAVIS_BRANCH} \
+--build-arg TRAVIS_EVENT_TYPE=${TRAVIS_EVENT_TYPE} \
+--build-arg TRAVIS_PULL_REQUEST_SLUG=${TRAVIS_PULL_REQUEST_SLUG} \
+--build-arg TRAVIS_PULL_REQUEST_BRANCH=${TRAVIS_PULL_REQUEST_BRANCH} \
+-t kalliope-debian8 \
+-f docker/debian8.dockerfile .
+```
+
+## Run the test
+
+Ubuntu image
+```
+docker run -it --rm kalliope-ubuntu1604
+```
+
+Debian image
+```
+docker run -it --rm kalliope-debian8
+```

+ 41 - 0
docker/ubuntu_16_04.dockerfile

@@ -0,0 +1,41 @@
+FROM ubuntu:16.04
+
+ENV no_proxy="127.0.0.1,localhost,kalliope.fr"
+
+# pico2wav is a multiverse package
+RUN echo "deb http://us.archive.ubuntu.com/ubuntu/ xenial multiverse" >> /etc/apt/sources.list
+
+# install packages
+RUN apt-get update && apt-get install -y \
+    git python-dev libsmpeg0 libttspico-utils libsmpeg0 flac dialog \
+    libffi-dev libffi-dev libssl-dev portaudio19-dev build-essential \
+    sox libatlas3-base mplayer wget vim sudo\
+    && rm -rf /var/lib/apt/lists/*
+
+# Install the last PIP
+RUN wget https://bootstrap.pypa.io/get-pip.py
+RUN python get-pip.py
+
+# add a standart user. tests must not be ran as root
+RUN useradd -m -u 1000 tester
+RUN usermod -aG sudo tester
+RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
+
+# get TRAVIS environment variables
+ARG TRAVIS_BRANCH
+ARG TRAVIS_EVENT_TYPE
+ARG TRAVIS_PULL_REQUEST_SLUG
+ARG TRAVIS_PULL_REQUEST_BRANCH
+ENV TRAVIS_BRANCH=${TRAVIS_BRANCH}
+ENV TRAVIS_EVENT_TYPE=${TRAVIS_EVENT_TYPE}
+ENV TRAVIS_PULL_REQUEST_SLUG=${TRAVIS_PULL_REQUEST_SLUG}
+ENV TRAVIS_PULL_REQUEST_BRANCH=${TRAVIS_PULL_REQUEST_BRANCH}
+
+ADD docker/clone_and_test.sh /home/tester/clone_and_test.sh
+RUN chown tester /home/tester/clone_and_test.sh
+
+USER tester
+WORKDIR /home/tester
+
+# run tests
+CMD ./clone_and_test.sh