1#!/usr/local/bin/bash -eu
2
3set -x
4
5NAMESPACE=${NAMESPACE:-default}
6
7# IMAGE_FORMAT is in the form $registry/$org/$image:$$component, ie
8# quay.io/openshift/release:$component
9# To test with your own image, build and push the test image
10# (using the Dockerfile in ci/Dockerfile)
11# and set the IMAGE_FORMAT environment variable so that it properly
12# resolves to your image. For example, quay.io/mynamespace/$component
13# would resolve to quay.io/mynamespace/molecule-test-runner
14# shellcheck disable=SC2034
15component='molecule-test-runner'
16eval IMAGE="$IMAGE_FORMAT"
17
18PULL_POLICY=${PULL_POLICY:-IfNotPresent}
19
20if ! oc get namespace "$NAMESPACE"
21then
22  oc create namespace "$NAMESPACE"
23fi
24
25oc project "$NAMESPACE"
26oc adm policy add-cluster-role-to-user cluster-admin -z default
27oc adm policy who-can create projectrequests
28
29echo "Deleting test job if it exists"
30oc delete job molecule-integration-test --wait --ignore-not-found
31
32echo "Creating molecule test job"
33cat << EOF | oc create -f -
34---
35apiVersion: batch/v1
36kind: Job
37metadata:
38  name: molecule-integration-test
39spec:
40  template:
41    spec:
42      containers:
43        - name: test-runner
44          image: ${IMAGE}
45          imagePullPolicy: ${PULL_POLICY}
46          command:
47            - make
48            - test-integration
49      restartPolicy: Never
50  backoffLimit: 2
51  completions: 1
52  parallelism: 1
53EOF
54
55function check_success {
56  oc wait --for=condition=complete job/molecule-integration-test --timeout 5s -n "$NAMESPACE" \
57   && oc logs job/molecule-integration-test \
58   && echo "Molecule integration tests ran successfully" \
59   && return 0
60  return 1
61}
62
63function check_failure {
64  oc wait --for=condition=failed job/molecule-integration-test --timeout 5s -n "$NAMESPACE" \
65   && oc logs job/molecule-integration-test \
66   && echo "Molecule integration tests failed, see logs for more information..." \
67   && return 0
68  return 1
69}
70
71runtime="15 minute"
72endtime=$(date -ud "$runtime" +%s)
73
74echo "Waiting for test job to complete"
75while [[ $(date -u +%s) -le $endtime ]]
76do
77  if check_success
78  then
79    exit 0
80  elif check_failure
81  then
82    exit 1
83  fi
84  sleep 10
85done
86
87exit 1
88