Skip to content

Commit

Permalink
Add CI tests to this repo. (#218)
Browse files Browse the repository at this point in the history
* Initial travis file to set up openshift

* adding a project and template to test

* set oc binary location

* Test image build

* downgrade oc

* try xenial distro

* downgrade to 3.10

* fix command

* Try again

* Refactor and tests

* more updates

* Applier 2.0.10

* Ensure openshift login succeeds, add status checks

* Move applier to main folder so we can use it for more things; add testing doc

* Fix script

* Ensure script exits when oc fails

* boom
  • Loading branch information
etsauer authored and sabre1041 committed Jun 24, 2019
1 parent d13a5bb commit 95dfa4c
Show file tree
Hide file tree
Showing 21 changed files with 199 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
namespace: jenkins-slaves

repository_url: https://github.com/redhat-cop/containers-quickstarts.git
slave_repo_ref: v1.3

openshift_cluster_content:
Expand All @@ -21,6 +22,7 @@ openshift_cluster_content:
params: "{{ inventory_dir }}/../.openshift/params/ansible"
params_from_vars:
SOURCE_REPOSITORY_REF: "{{ slave_repo_ref }}"
SOURCE_REPOSITORY_URL: "{{ repository_url }}"
namespace: "{{ namespace }}"
tags:
- jenkins-slaves
Expand All @@ -30,6 +32,7 @@ openshift_cluster_content:
params: "{{ inventory_dir }}/../.openshift/params/arachni"
params_from_vars:
SOURCE_REPOSITORY_REF: "{{ slave_repo_ref }}"
SOURCE_REPOSITORY_URL: "{{ repository_url }}"
namespace: "{{ namespace }}"
tags:
- jenkins-slaves
Expand All @@ -39,6 +42,7 @@ openshift_cluster_content:
params: "{{ inventory_dir }}/../.openshift/params/golang"
params_from_vars:
SOURCE_REPOSITORY_REF: "{{ slave_repo_ref }}"
SOURCE_REPOSITORY_URL: "{{ repository_url }}"
namespace: "{{ namespace }}"
tags:
- jenkins-slaves
Expand All @@ -48,6 +52,7 @@ openshift_cluster_content:
params: "{{ inventory_dir }}/../.openshift/params/gradle"
params_from_vars:
SOURCE_REPOSITORY_REF: "{{ slave_repo_ref }}"
SOURCE_REPOSITORY_URL: "{{ repository_url }}"
namespace: "{{ namespace }}"
tags:
- jenkins-slaves
Expand All @@ -57,6 +62,7 @@ openshift_cluster_content:
params: "{{ inventory_dir }}/../.openshift/params/mongodb"
params_from_vars:
SOURCE_REPOSITORY_REF: "{{ slave_repo_ref }}"
SOURCE_REPOSITORY_URL: "{{ repository_url }}"
namespace: "{{ namespace }}"
tags:
- jenkins-slaves
Expand All @@ -66,6 +72,7 @@ openshift_cluster_content:
params: "{{ inventory_dir }}/../.openshift/params/mvn"
params_from_vars:
SOURCE_REPOSITORY_REF: "{{ slave_repo_ref }}"
SOURCE_REPOSITORY_URL: "{{ repository_url }}"
namespace: "{{ namespace }}"
tags:
- jenkins-slaves
Expand All @@ -75,6 +82,7 @@ openshift_cluster_content:
params: "{{ inventory_dir }}/../.openshift/params/npm"
params_from_vars:
SOURCE_REPOSITORY_REF: "{{ slave_repo_ref }}"
SOURCE_REPOSITORY_URL: "{{ repository_url }}"
namespace: "{{ namespace }}"
tags:
- jenkins-slaves
Expand All @@ -84,6 +92,7 @@ openshift_cluster_content:
params: "{{ inventory_dir }}/../.openshift/params/python"
params_from_vars:
SOURCE_REPOSITORY_REF: "{{ slave_repo_ref }}"
SOURCE_REPOSITORY_URL: "{{ repository_url }}"
namespace: "{{ namespace }}"
tags:
- jenkins-slaves
Expand All @@ -93,7 +102,20 @@ openshift_cluster_content:
params: "{{ inventory_dir }}/../.openshift/params/zap"
params_from_vars:
SOURCE_REPOSITORY_REF: "{{ slave_repo_ref }}"
SOURCE_REPOSITORY_URL: "{{ repository_url }}"
namespace: "{{ namespace }}"
tags:
- jenkins-slaves
- zap-slave
- object: test-pipelines
content:
- name: Deploy all test pipelines
template: "https://raw.githubusercontent.com/redhat-cop/openshift-templates/v1.4.5/jenkins-pipelines/jenkins-pipeline-template-no-ocp-triggers.yml"
params: "{{ inventory_dir }}/../.openshift/params/test_pipelines/"
params_from_vars:
PIPELINE_SOURCE_REPOSITORY_URL: "{{ repository_url }}"
PIPELINE_SOURCE_REPOSITORY_REF: "{{ slave_repo_ref }}"
PIPELINE_FILENAME: "Jenkinsfile.test"
namespace: "{{ namespace }}"
tags:
- test-pipelines
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions .openshift/params/test_pipelines/golang
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NAME=jenkins-slave-golang
PIPELINE_CONTEXT_DIR=jenkins-slaves/jenkins-slave-golang
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ objects:
name: jenkins-slave-base-centos7:v3.11
type: Docker
triggers:
- type: ConfigChange
- imageChange: {}
type: ImageChange
- apiVersion: v1
Expand Down
40 changes: 40 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
dist: xenial
language: python
python:
- "3.7"

cache:
directories:
- $HOME/.cache/pip

env:
global:
- ANSIBLE_HOST_KEY_CHECKING=False
- PIP_DOWNLOAD_CACHE=$HOME/.cache/pip
- OC_BINARY_URL=https://mirror.openshift.com/pub/openshift-v3/clients/3.10.45/linux/oc.tar.gz
- ANSIBLE_VERSION="2.7"

before_install:
- sudo apt-get update -qq

install:
- pip install -U pip
- if [ "$ANSIBLE_VERSION" = "latest" ]; then pip install ansible; else pip install ansible~=$ANSIBLE_VERSION; fi
#- pip install "ansible-lint<4.0" yamllint flake8 molecule docker "pytest<3.10" "testinfra==3.0.4"
# Configure OpenShift Binary
- sudo wget -qO- ${OC_BINARY_URL} | sudo tar -xvz -C /bin

before_script:
# Configure Docker
- sudo service docker stop
- sudo mkdir -p /etc/docker
- echo '{"insecure-registries":["172.30.0.0/16"]}' | sudo tee /etc/docker/daemon.json
- sudo service docker start
# Launch OpenShift Environment
- _test/setup.sh cluster_up
# Run Applier to provision all of the builds
- _test/setup.sh applier

script:
# Test to ensure that builds all succeed
- _test/setup.sh test
32 changes: 32 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Testing this Repository

Containers-quickstarts is outfitted with a set of automated tests. This document deals with writing and running tests for your contributions.

## Tesing Architecture

We use Travis-CI to test this repo. You can start to examine how we set up and invoke tests by examining the [.travis.yml](/.travis.yml) file.

We have split the architect of the tests up into 4 phases:

1. Environment setup and prereqs
2. Launching an openshift test cluster, using `oc cluster up`
3. Running [openshift-applier](https://github.com/redhat-cop/openshift-applier) to deploy a set of builds
4. Checking `.status.phase` in each build to validate all builds end with in a `Completed` state.

The 3 phases of testing are captured in a [script](/_test/setup.sh) that is executed by travis, but can also be run locally in order to validate your changes before committing code.

## Writing Tests

Adding a new test to our CI is as simple as adding one or more `BuildConfig`s to the [global Applier inventory](/.applier). Currently we deploy all assets into a single namespace to make all builds "discoverable" by our test scripts.

Every build that gets created will then be executed, and the test script will wait until all builds complete, and ensure that none of them fail.

## Running the tests locally.

There are a number of ways to run the tests, but the easiest is to run all phases against a local cluster with `oc cluster up`:

```
oc cluster up --base-dir=$HOME/ocp && \
./_test/setup.sh applier && \
./_test/setup.sh test
```
85 changes: 85 additions & 0 deletions _test/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/bin/bash
trap "exit 1" TERM
export TOP_PID=$$
NAMESPACE=containers-quickstarts-tests

cluster_up() {
set +e
built=false
while true; do
if [ -z $IP_ADDR ]; then
DEV=$(ip link | awk '/state UP/{ gsub(":", ""); print $2}')
IP_ADDR=$(ip addr show $DEV | awk '/inet /{ gsub("/.*", ""); print $2}')
fi
oc cluster up --public-hostname=${IP_ADDR} --routing-suffix=${IP_ADDR}.nip.io --base-dir=$HOME/ocp
if [ "$?" -eq 0 ]; then
built=true
break
fi
echo "Retrying oc cluster up after failure"
oc cluster down
sleep 5
done
echo "OpenShift Cluster Running"
}

applier() {
echo "${TRAVIS_BRANCH:=master}"
echo "${TRAVIS_REPO_SLUG:=redhat-cop/containers-quickstarts}"
ansible-galaxy install -r requirements.yml -p galaxy --force
ansible-playbook -i .applier/ galaxy/openshift-applier/playbooks/openshift-cluster-seed.yml -e namespace=containers-quickstarts-tests -e slave_repo_ref=${TRAVIS_BRANCH} -e repository_url=https://github.com/${TRAVIS_REPO_SLUG}.git
}

get_build_phases() {
phase=$1
result=$(oc get builds -o jsonpath="{.items[?(@.status.phase==\"${phase}\")].metadata.name}" -n $NAMESPACE) || kill -s TERM $TOP_PID
echo ${result} | wc -w
}

test() {
#oc status || exit 1

echo "Ensure all Builds are executed..."
for pipeline in $(oc get bc -n ${NAMESPACE} -o jsonpath='{.items[*].metadata.name}'); do
if [ "$(oc get build -n containers-quickstarts-tests -o jsonpath="{.items[?(@.metadata.annotations.openshift\.io/build-config\.name==\"${pipeline}\")].metadata.name}")" == "" ]; then
oc start-build ${pipeline} -n ${NAMESPACE}
fi
done

echo "Waiting for all builds to start..."
while [[ "$(get_build_phases "New")" -ne 0 || $(get_build_phases "Pending") -ne 0 ]]; do
echo -ne "New Builds: $(get_build_phases "New"), Pending Builds: $(get_build_phases "Pending")\r"
sleep 1
done

echo "Waiting for all builds to complete..."
while [ $(get_build_phases "Running") -ne 0 ]; do
echo -ne "Running Builds: $(get_build_phases "Running")\r"
sleep 1
done

echo "Check to see how many builds Failed"
if [ $(get_build_phases "Failed") -ne 0 ]; then
echo "Some builds failed. Printing Report"
oc get builds -n $NAMESPACE -o custom-columns=NAME:.metadata.name,TYPE:.spec.strategy.type,FROM:.spec.source.type,STATUS:.status.phase,REASON:.status.reason
exit 1
fi

echo "Tests Completed Successfully!"
}

# Process arguments
case $1 in
cluster_up)
cluster_up
;;
applier)
applier
;;
test)
test
;;
*)
echo "Not an option"
exit 1
esac
17 changes: 17 additions & 0 deletions jenkins-slaves/jenkins-slave-golang/Jenkinsfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pipeline {
agent {
label 'jenkins-slave-golang'
}

stages {
stage ('Run Test') {
steps {
sh """
go version
"""
}
}

}

}
2 changes: 1 addition & 1 deletion jenkins-slaves/requirements.yml → requirements.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
- name: openshift-applier
scm: git
src: https://github.com/redhat-cop/openshift-applier
version: v2.0.8
version: v2.0.10

0 comments on commit 95dfa4c

Please sign in to comment.