From 95dfa4c525c744ae14fe167a68aa2b7a939cff01 Mon Sep 17 00:00:00 2001 From: Eric Sauer Date: Sun, 23 Jun 2019 20:33:59 -0400 Subject: [PATCH] Add CI tests to this repo. (#218) * 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 --- .../group_vars/seed-hosts.yml | 22 +++++ {jenkins-slaves/.applier => .applier}/hosts | 0 .../.openshift => .openshift}/params/ansible | 0 .../.openshift => .openshift}/params/arachni | 0 .../.openshift => .openshift}/params/golang | 0 .../.openshift => .openshift}/params/gradle | 0 .../.openshift => .openshift}/params/mongodb | 0 .../.openshift => .openshift}/params/mvn | 0 .../.openshift => .openshift}/params/npm | 0 .../.openshift => .openshift}/params/python | 0 .openshift/params/test_pipelines/golang | 2 + .../.openshift => .openshift}/params/zap | 0 .../jenkins-slave-ansible-stacks-template.yml | 0 .../jenkins-slave-generic-template.yml | 0 .../jenkins-slave-image-mgmt-template.yml | 0 .../templates/jenkins-slave-ruby-template.yml | 1 - .travis.yml | 40 +++++++++ TESTING.md | 32 +++++++ _test/setup.sh | 85 +++++++++++++++++++ .../jenkins-slave-golang/Jenkinsfile.test | 17 ++++ .../requirements.yml => requirements.yml | 2 +- 21 files changed, 199 insertions(+), 2 deletions(-) rename {jenkins-slaves/.applier => .applier}/group_vars/seed-hosts.yml (76%) rename {jenkins-slaves/.applier => .applier}/hosts (100%) rename {jenkins-slaves/.openshift => .openshift}/params/ansible (100%) rename {jenkins-slaves/.openshift => .openshift}/params/arachni (100%) rename {jenkins-slaves/.openshift => .openshift}/params/golang (100%) rename {jenkins-slaves/.openshift => .openshift}/params/gradle (100%) rename {jenkins-slaves/.openshift => .openshift}/params/mongodb (100%) rename {jenkins-slaves/.openshift => .openshift}/params/mvn (100%) rename {jenkins-slaves/.openshift => .openshift}/params/npm (100%) rename {jenkins-slaves/.openshift => .openshift}/params/python (100%) create mode 100644 .openshift/params/test_pipelines/golang rename {jenkins-slaves/.openshift => .openshift}/params/zap (100%) rename {jenkins-slaves/.openshift => .openshift}/templates/jenkins-slave-ansible-stacks-template.yml (100%) rename {jenkins-slaves/.openshift => .openshift}/templates/jenkins-slave-generic-template.yml (100%) rename {jenkins-slaves/.openshift => .openshift}/templates/jenkins-slave-image-mgmt-template.yml (100%) rename {jenkins-slaves/.openshift => .openshift}/templates/jenkins-slave-ruby-template.yml (98%) create mode 100644 .travis.yml create mode 100644 TESTING.md create mode 100755 _test/setup.sh create mode 100644 jenkins-slaves/jenkins-slave-golang/Jenkinsfile.test rename jenkins-slaves/requirements.yml => requirements.yml (92%) diff --git a/jenkins-slaves/.applier/group_vars/seed-hosts.yml b/.applier/group_vars/seed-hosts.yml similarity index 76% rename from jenkins-slaves/.applier/group_vars/seed-hosts.yml rename to .applier/group_vars/seed-hosts.yml index 0c855360f..171d621b4 100644 --- a/jenkins-slaves/.applier/group_vars/seed-hosts.yml +++ b/.applier/group_vars/seed-hosts.yml @@ -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: @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/jenkins-slaves/.applier/hosts b/.applier/hosts similarity index 100% rename from jenkins-slaves/.applier/hosts rename to .applier/hosts diff --git a/jenkins-slaves/.openshift/params/ansible b/.openshift/params/ansible similarity index 100% rename from jenkins-slaves/.openshift/params/ansible rename to .openshift/params/ansible diff --git a/jenkins-slaves/.openshift/params/arachni b/.openshift/params/arachni similarity index 100% rename from jenkins-slaves/.openshift/params/arachni rename to .openshift/params/arachni diff --git a/jenkins-slaves/.openshift/params/golang b/.openshift/params/golang similarity index 100% rename from jenkins-slaves/.openshift/params/golang rename to .openshift/params/golang diff --git a/jenkins-slaves/.openshift/params/gradle b/.openshift/params/gradle similarity index 100% rename from jenkins-slaves/.openshift/params/gradle rename to .openshift/params/gradle diff --git a/jenkins-slaves/.openshift/params/mongodb b/.openshift/params/mongodb similarity index 100% rename from jenkins-slaves/.openshift/params/mongodb rename to .openshift/params/mongodb diff --git a/jenkins-slaves/.openshift/params/mvn b/.openshift/params/mvn similarity index 100% rename from jenkins-slaves/.openshift/params/mvn rename to .openshift/params/mvn diff --git a/jenkins-slaves/.openshift/params/npm b/.openshift/params/npm similarity index 100% rename from jenkins-slaves/.openshift/params/npm rename to .openshift/params/npm diff --git a/jenkins-slaves/.openshift/params/python b/.openshift/params/python similarity index 100% rename from jenkins-slaves/.openshift/params/python rename to .openshift/params/python diff --git a/.openshift/params/test_pipelines/golang b/.openshift/params/test_pipelines/golang new file mode 100644 index 000000000..b4f2a08de --- /dev/null +++ b/.openshift/params/test_pipelines/golang @@ -0,0 +1,2 @@ +NAME=jenkins-slave-golang +PIPELINE_CONTEXT_DIR=jenkins-slaves/jenkins-slave-golang diff --git a/jenkins-slaves/.openshift/params/zap b/.openshift/params/zap similarity index 100% rename from jenkins-slaves/.openshift/params/zap rename to .openshift/params/zap diff --git a/jenkins-slaves/.openshift/templates/jenkins-slave-ansible-stacks-template.yml b/.openshift/templates/jenkins-slave-ansible-stacks-template.yml similarity index 100% rename from jenkins-slaves/.openshift/templates/jenkins-slave-ansible-stacks-template.yml rename to .openshift/templates/jenkins-slave-ansible-stacks-template.yml diff --git a/jenkins-slaves/.openshift/templates/jenkins-slave-generic-template.yml b/.openshift/templates/jenkins-slave-generic-template.yml similarity index 100% rename from jenkins-slaves/.openshift/templates/jenkins-slave-generic-template.yml rename to .openshift/templates/jenkins-slave-generic-template.yml diff --git a/jenkins-slaves/.openshift/templates/jenkins-slave-image-mgmt-template.yml b/.openshift/templates/jenkins-slave-image-mgmt-template.yml similarity index 100% rename from jenkins-slaves/.openshift/templates/jenkins-slave-image-mgmt-template.yml rename to .openshift/templates/jenkins-slave-image-mgmt-template.yml diff --git a/jenkins-slaves/.openshift/templates/jenkins-slave-ruby-template.yml b/.openshift/templates/jenkins-slave-ruby-template.yml similarity index 98% rename from jenkins-slaves/.openshift/templates/jenkins-slave-ruby-template.yml rename to .openshift/templates/jenkins-slave-ruby-template.yml index 04ebffb43..41d0d459d 100644 --- a/jenkins-slaves/.openshift/templates/jenkins-slave-ruby-template.yml +++ b/.openshift/templates/jenkins-slave-ruby-template.yml @@ -39,7 +39,6 @@ objects: name: jenkins-slave-base-centos7:v3.11 type: Docker triggers: - - type: ConfigChange - imageChange: {} type: ImageChange - apiVersion: v1 diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..c78eee8c4 --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/TESTING.md b/TESTING.md new file mode 100644 index 000000000..84e119fa4 --- /dev/null +++ b/TESTING.md @@ -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 +``` diff --git a/_test/setup.sh b/_test/setup.sh new file mode 100755 index 000000000..1acef190e --- /dev/null +++ b/_test/setup.sh @@ -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 diff --git a/jenkins-slaves/jenkins-slave-golang/Jenkinsfile.test b/jenkins-slaves/jenkins-slave-golang/Jenkinsfile.test new file mode 100644 index 000000000..2a8263e59 --- /dev/null +++ b/jenkins-slaves/jenkins-slave-golang/Jenkinsfile.test @@ -0,0 +1,17 @@ +pipeline { + agent { + label 'jenkins-slave-golang' + } + + stages { + stage ('Run Test') { + steps { + sh """ + go version + """ + } + } + + } + +} diff --git a/jenkins-slaves/requirements.yml b/requirements.yml similarity index 92% rename from jenkins-slaves/requirements.yml rename to requirements.yml index 4e66d4ccf..b6037070e 100644 --- a/jenkins-slaves/requirements.yml +++ b/requirements.yml @@ -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