From 0d7c5825e34ea233714fac7efc0163807edbccf4 Mon Sep 17 00:00:00 2001
From: Federico Ressi <fressi@redhat.com>
Date: Tue, 5 Jan 2021 14:36:01 +0100
Subject: [PATCH] Refactor container files

Change-Id: I42eeadb2183d517d6f72f578cb5dfd6e03daced4
---
 .dockerignore      |  18 ++++--
 Dockerfile         | 144 +++++++++++++++++++--------------------------
 docker-compose.yml |  76 +++++++++++-------------
 3 files changed, 108 insertions(+), 130 deletions(-)

diff --git a/.dockerignore b/.dockerignore
index 00c6e4dd7..6cc71ac72 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -7,12 +7,14 @@
 # Hidden files
 .coverage
 .coverage.*
+.dockerignore
 .eggs
 .mypy_cache
 .stestr
 .test
 .testrepository
 .tobiko-env
+.tobiko
 .tox
 .vagrant
 .venv
@@ -21,21 +23,29 @@ __pycache__
 AUTHORS
 build/*
 ChangeLog
-doc/build/*
+doc/
 cover/
 dist/
 etc/*.sample
+playbooks/
+releasenotes/
 report/
+roles/
 test_results*
 zuul/versioninfo
 
 # Files created by releasenotes build
 releasenotes/build
 
-# Docs related files
-doc/source/_static/config-samples/*.sample
-
 Pipfile.lock
 
 # Infrared things
 .infrared
+
+# Docker files
+Dockerfile
+docker-compose.yaml
+docker-compose.yml
+
+# Tobiko configuration files
+tobiko.conf
diff --git a/Dockerfile b/Dockerfile
index 55b5101a7..23293db6e 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,107 +1,81 @@
-ARG base_image="docker.io/library/centos:8"
+FROM python:3.9 as base
 
-FROM "${base_image}" as base
+ENV TOBIKO_DIR=/tobiko
+ENV WHEEL_DIR=/wheel
 
-# Make sure Git and Python 3 are installed on your system.
-RUN yum install -y git python3 rsync which
+RUN apt update
 
-# Check your Python 3 version is greater than 3.6
-RUN python3 -c 'import sys; sys.version_info >= (3, 6)'
-
-# Ensure Pip is installed and up-to date
-RUN curl https://bootstrap.pypa.io/get-pip.py | python3
-
-# Check installed Pip version
-RUN python3 -m pip --version
-
-# Ensure basic Python packages are installed and up-to-date
-RUN python3 -m pip install --upgrade setuptools wheel virtualenv tox six
-
-# Check installed Tox version
-RUN tox --version
+# Set the locale
+RUN apt install -y locales
+ENV LANG en_US.UTF-8
+ENV LANGUAGE en_US:en
+ENV LC_ALL en_US.UTF-8
+RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && \
+    locale-gen
 
 
-# -----------------------------------------------------------------------------
+FROM base as source
 
-FROM base as sources
+# Populate tobiko source dir
+# RUN mkdir -p ${TOBIKO_DIR}
 
-# Get Tobiko source code using Git
-RUN mkdir -p /src
-ADD . /src/tobiko
-WORKDIR /src/tobiko
+ADD .gitignore \
+    extra-requirements.txt \
+    requirements.txt \
+    README.rst \
+    setup.cfg \
+    setup.py \
+    test-requirements.txt \
+    upper-constraints.txt \
+    ${TOBIKO_DIR}/
+
+ADD .git ${TOBIKO_DIR}/.git/
+ADD tobiko/ ${TOBIKO_DIR}/tobiko/
 
 
-# -----------------------------------------------------------------------------
+FROM source as build
 
-FROM sources as bindeps
+# Install binary dependencies
+RUN apt install -y git
 
-# Ensure required binary packages are installed
-RUN ./tools/install-bindeps.sh
-
-# Check bindeps are installed
-CMD tox -e bindeps
+# Build wheel files
+RUN python3 -m pip wheel -w ${WHEEL_DIR} \
+    -c ${TOBIKO_DIR}/upper-constraints.txt \
+    -r ${TOBIKO_DIR}/requirements.txt \
+    -r ${TOBIKO_DIR}/test-requirements.txt \
+    -r ${TOBIKO_DIR}/extra-requirements.txt \
+    --src ${TOBIKO_DIR}/
 
 
-# -----------------------------------------------------------------------------
+FROM base as install
 
-FROM bindeps as py3
-
-# Prepare py3 Tox virtualenv
-RUN tox -e py3 --notest
-
-# Run unit yest cases
-CMD tox -e py3
+# Install wheels
+RUN mkdir -p ${WHEEL_DIR}
+COPY --from=build ${WHEEL_DIR} ${WHEEL_DIR}
+RUN pip install ${WHEEL_DIR}/*.whl
 
 
-# -----------------------------------------------------------------------------
+FROM source as tobiko
 
-FROM py3 as venv
+# Install packages
+RUN apt install -y iperf3 iputils-ping ncat
 
-# Run bash inside py3 Tox environment
-CMD tox -e venv
+# Run tests variables
+ENV PYTHONWARNINGS=ignore::Warning
+ENV OS_TEST_PATH=${TOBIKO_DIR}/tobiko/tests/unit
+ENV TOX_REPORT_DIR=/report
+ENV TOX_REPORT_NAME=tobiko_results
+ENV TOBIKO_PREVENT_CREATE=false
 
+# Write log files to report directory
+RUN mkdir -p /etc/tobiko
+RUN printf "[DEFAULT]\nlog_dir=${TOBIKO_REPORT_DIR}" > /etc/tobiko/tobiko.conf
 
-# -----------------------------------------------------------------------------
+# Copy python pacakges
+COPY --from=install /usr/local /usr/local/
 
-FROM py3 as functional
+# Copy tobiko tools
+ADD tools/ ${TOBIKO_DIR}/tools/
 
-# Run functional test cases
-CMD tox -e functional
-
-
-# -----------------------------------------------------------------------------
-
-FROM py3 as scenario
-
-# Run scenario test cases
-CMD tox -e scenario
-
-
-# -----------------------------------------------------------------------------
-
-FROM py3 as neutron
-
-# Run scenario test cases
-CMD tox -e neutron
-
-
-# -----------------------------------------------------------------------------
-
-FROM py3 as faults
-
-# Run faults test cases
-CMD tox -e faults
-
-
-# -----------------------------------------------------------------------------
-
-from bindeps as infrared
-
-# Set Python 3 as default alternative for python command
-RUN alternatives --set python /usr/bin/python3
-
-# Prepare infrared Tox virtualenv
-RUN tox -e infrared --notest
-
-# Run Tobiko InfraRed plugin
-CMD tox -e infrared
+WORKDIR ${TOBIKO_DIR}
+ENTRYPOINT tools/run_tests.py ${OS_TEST_PATH}
diff --git a/docker-compose.yml b/docker-compose.yml
index 6e2ae59e1..b58854b29 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -2,52 +2,46 @@
 
 version: '3.4'
 services:
-  base:
+  unit:
     build:
       context: .
-      target: base
+      target: tobiko
+    hostname: tobiko
+    environment:
+      OS_TEST_PATH: tobiko/tests/unit
+      TOX_REPORT_NAME: tobiko_results_unit
+    volumes:
+      - ./report:/report
 
-  sources:
-    build:
-      context: .
-      target: sources
-
-  bindeps:
-    build:
-      context: .
-      target: bindeps
-
-  py3:
-    build:
-      context: .
-      target: py3
-
-  venv:
-    build:
-      context: .
-      target: venv
+  shell:
+    extends:
+      service: unit
+    entrypoint: /bin/bash
 
   functional:
-    build:
-      context: .
-      target: functional
+    extends:
+      service: unit
+    environment:
+      OS_TEST_PATH: tobiko/tests/functional
+      TOX_REPORT_NAME: tobiko_results_functional
 
-  scenario:
-    build:
-      context: .
-      target: scenario
+  create:
+    extends:
+      service: unit
+    environment:
+      OS_TEST_PATH: tobiko/tests/scenario
+      TOX_REPORT_NAME: tobiko_results_create
 
-  neutron:
-    build:
-      context: .
-      target: neutron
+  disrupt:
+    extends:
+      service: unit
+    environment:
+      OS_TEST_PATH: tobiko/tests/faults
+      TOX_REPORT_NAME: tobiko_results_disrupt
 
-  faults:
-    build:
-      context: .
-      target: faults
-
-  infrared:
-    build:
-      context: .
-      target: infrared
+  verify:
+    extends:
+      service: create
+    environment:
+      PREVENT_CREATE: true
+      TOX_REPORT_NAME: tobiko_results_verify