Skip to content

Commit

Permalink
add a way to run integration tests inside openshift
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Tomecek <ttomecek@redhat.com>
  • Loading branch information
TomasTomecek committed Oct 15, 2019
1 parent 0aba881 commit 11ca8d5
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 40 deletions.
14 changes: 7 additions & 7 deletions Dockerfile.tests
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# For running tests locally, see check_in_container target in Makefile

FROM docker.io/usercont/packit
FROM docker.io/usercont/packit-service-worker

COPY files/install-deps.yaml ./
RUN ansible-playbook -vv -c local -i localhost, ./install-deps.yaml
RUN set -ex; mkdir -p /home/packit/.config \
&& ln -s $PS_PATH/files/packit-service.yaml /home/packit/.config/packit-service.yaml \
&& ansible-playbook -vv -c local -i localhost, ./files/recipe-tests.yaml

COPY files/recipe-tests.yaml ./
RUN ansible-playbook -vv -c local -i localhost, ./recipe-tests.yaml

WORKDIR /src
# we are doing the same here as in worker Df so that we don't need to rerun the
# playbook above for every code change: iterating fast <3
COPY . $PS_PATH
1 change: 1 addition & 0 deletions Dockerfile.worker
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ RUN cd $PS_PATH && \
ansible-playbook -vv -c local -i localhost, recipe-worker.yaml

COPY . $PS_PATH
WORKDIR $PS_PATH

CMD ["/usr/bin/run_worker.sh"]
34 changes: 27 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ SERVICE_IMAGE := docker.io/usercont/packit-service
WORKER_IMAGE := docker.io/usercont/packit-service-worker
WORKER_PROD_IMAGE := docker.io/usercont/packit-service-worker:prod
TEST_IMAGE := packit-service-tests
TEST_TARGET := ./tests/
TEST_TARGET := ./tests/unit ./tests/integration/
CONTAINER_ENGINE := podman

build: files/install-deps.yaml files/recipe.yaml
docker build --rm -t $(SERVICE_IMAGE) .

worker: CONTAINER_ENGINE ?= docker
worker: files/install-deps-worker.yaml files/recipe-worker.yaml
docker build --rm -t $(WORKER_IMAGE) -f Dockerfile.worker .
$(CONTAINER_ENGINE) build --rm -t $(WORKER_IMAGE) -f Dockerfile.worker .

# this is for cases when you want to deploy into production and don't want to wait for dockerhub
worker-prod: files/install-deps-worker.yaml files/recipe-worker.yaml
Expand Down Expand Up @@ -41,13 +43,31 @@ check:
find . -name "*.pyc" -exec rm {} \;
PYTHONPATH=$(CURDIR) PYTHONDONTWRITEBYTECODE=1 python3 -m pytest --color=yes --verbose --showlocals --cov=packit_service --cov-report=term-missing $(TEST_TARGET)

test_image: files/install-deps.yaml files/recipe-tests.yaml
podman build --rm -t $(TEST_IMAGE) -f Dockerfile.tests .
test_image: CONTAINER_ENGINE=podman
test_image: worker files/install-deps.yaml files/recipe-tests.yaml
$(CONTAINER_ENGINE) build --rm -t $(TEST_IMAGE) -f Dockerfile.tests .

check_in_container: test_image
podman run --rm -ti \
-v $(CURDIR):/src \
-w /src \
$(CONTAINER_ENGINE) run --rm -ti \
-v $(CURDIR):/src-packit-service \
-w /src-packit-service \
--security-opt label=disable \
-v $(CURDIR)/files/packit-service.yaml:/root/.config/packit-service.yaml \
$(TEST_IMAGE) make check

# deploy a pod with tests and run them
check-inside-openshift: CONTAINER_ENGINE=docker
check-inside-openshift: test_image
@# make sure we are running against a local cluster
oc whoami --show-server | grep localhost
oc delete job packit-tests || :
oc apply -f files/test-in-openshift.yaml
oc wait job/packit-tests --for condition=complete --timeout=60s
oc logs job/packit-tests
# this garbage tells us if the tests passed or not
oc get job packit-tests -o jsonpath='{.status.conditions[?(@.type=="Complete")].status}' | grep True

# this target is expected to run within an openshift pod
check-within-openshift:
/src-packit-service/files/setup_env_in_openshift.sh
pytest-3 -k test_update
16 changes: 0 additions & 16 deletions files/recipe-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,3 @@
- python3-pytest
- python3-pytest-cov
state: present
- name: Install sandcastle
pip:
name: git+https://github.com/packit-service/sandcastle.git
- name: Install ogr from git master
pip:
name: git+https://github.com/packit-service/ogr.git
# we don't install packit-service, so install deps manually
- name: Pip install dependencies
pip:
name:
- persistentdict
- celery[redis]
- name: Clean all the cache files (especially pip)
file:
state: absent
path: ~/.cache/
11 changes: 11 additions & 0 deletions files/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/bash

set -x

source /src-packit-service/files/setup_env_in_openshift.sh

id

cat $HOME/.config/packit-service.yaml

pytest-3 -vv tests/openshift_integration/
9 changes: 1 addition & 8 deletions files/run_worker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ if [[ -z ${APP} ]]; then
exit 1
fi

export PACKIT_HOME=/home/packit

# Generate passwd file based on current uid, needed for fedpkg
grep -v ^packit /etc/passwd > ${PACKIT_HOME}/passwd
printf "packit:x:$(id -u):0:Packit Service:/home/packit:/bin/bash\n" >> ${PACKIT_HOME}/passwd
export LD_PRELOAD=libnss_wrapper.so
export NSS_WRAPPER_PASSWD=${HOME}/passwd
export NSS_WRAPPER_GROUP=/etc/group
source /src-packit-service/files/setup_env_in_openshift.sh

mkdir --mode=0700 -p ${PACKIT_HOME}/.ssh
install -m 0400 /packit-ssh/id_rsa ${PACKIT_HOME}/.ssh/
Expand Down
11 changes: 11 additions & 0 deletions files/setup_env_in_openshift.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/bash

set -x

export PACKIT_HOME=/home/packit
# Generate passwd file based on current uid, needed for fedpkg
grep -v ^packit /etc/passwd > ${PACKIT_HOME}/passwd
printf "packit:x:$(id -u):0:Packit Service:/home/packit:/bin/bash\n" >> ${PACKIT_HOME}/passwd
export LD_PRELOAD=libnss_wrapper.so
export NSS_WRAPPER_PASSWD=${HOME}/passwd
export NSS_WRAPPER_GROUP=/etc/group
25 changes: 25 additions & 0 deletions files/test-in-openshift.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
apiVersion: batch/v1
kind: Job
metadata:
name: packit-tests
spec:
template:
spec:
restartPolicy: Never
containers:
- name: packit-tests
image: packit-service-tests
imagePullPolicy: Never # IfNotPresent
workingDir: /src-packit-service
# volumeMounts:
# - name: packit-ssh
# mountPath: /packit-ssh
# - name: packit-secrets
# mountPath: /secrets
# - name: packit-config
# mountPath: /home/packit/.config
# - name: packit-worker-vol
# mountPath: /sandcastle
command: ["bash", "/src-packit-service/files/run_tests.sh"]
backoffLimit: 1
9 changes: 7 additions & 2 deletions files/zuul-tests.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
---
- name: This is a recipe for how to run packit tests
- name: Run packit-service tests
hosts: all
tasks:
- include_tasks: tasks/zuul-project-setup.yaml
- name: Build packit-service worker image from current src dir
- name: Run tests within a container
command: "make check_in_container"
args:
chdir: "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}"
become: true
- name: Run tests which are executed within openshift
command: make check-inside-openshift
args:
chdir: "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}"
become: true
21 changes: 21 additions & 0 deletions tests/openshift_integration/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License
#
# Copyright (c) 2018-2019 Red Hat, Inc.

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

0 comments on commit 11ca8d5

Please sign in to comment.