Wait for addon ready before testing
[icn.git] / deploy / addons / addons.sh
1 #!/usr/bin/env bash
2 set -eux -o pipefail
3
4 SCRIPTDIR="$(readlink -f $(dirname ${BASH_SOURCE[0]}))"
5 LIBDIR="$(dirname $(dirname ${SCRIPTDIR}))/env/lib"
6
7 source $LIBDIR/logging.sh
8 source $LIBDIR/common.sh
9
10 BUILDDIR=${SCRIPTDIR/deploy/build}
11 mkdir -p ${BUILDDIR}
12
13 function is_emco_ready {
14     local -r cluster_name=${CLUSTER_NAME:-icn}
15     local -r cluster_kubeconfig="${BUILDDIR}/${cluster_name}.conf"
16     kubectl --kubeconfig=${cluster_kubeconfig} -n emco wait pod --all --for=condition=Ready --timeout=0s >/dev/null 2>&1
17 }
18
19 function register_emco_controllers {
20     wait_for is_emco_ready
21     local -r cluster_name=${CLUSTER_NAME:-icn}
22     local -r host=$(kubectl -n metal3 get cluster/${cluster_name} -o jsonpath='{.spec.controlPlaneEndpoint.host}')
23     cat <<EOF >${BUILDDIR}/${cluster_name}-config.yaml
24 orchestrator:
25   host: ${host}
26   port: 30415
27 EOF
28     cat <<EOF >${BUILDDIR}/${cluster_name}-controllers.yaml
29 ---
30 version: emco/v2
31 resourceContext:
32   anchor: controllers
33 metadata:
34   name: rsync
35 spec:
36   host: ${host}
37   port: 30441
38 ---
39 version: emco/v2
40 resourceContext:
41   anchor: controllers
42 metadata:
43   name: gac
44 spec:
45   host: ${host}
46   port: 30493
47   type: "action"
48   priority: 1
49 ---
50 version: emco/v2
51 resourceContext:
52   anchor: controllers
53 metadata:
54   name: ovnaction
55 spec:
56   host: ${host}
57   port: 30473
58   type: "action"
59   priority: 1
60 ---
61 version: emco/v2
62 resourceContext:
63   anchor: controllers
64 metadata:
65   name: dtc
66 spec:
67   host: ${host}
68   port: 30483
69   type: "action"
70   priority: 1
71 EOF
72     emcoctl --config ${BUILDDIR}/${cluster_name}-config.yaml apply -f ${BUILDDIR}/${cluster_name}-controllers.yaml
73 }
74
75 function unregister_emco_controllers {
76     local -r cluster_name=${CLUSTER_NAME:-icn}
77     emcoctl --config ${BUILDDIR}/${cluster_name}-config.yaml delete -f ${BUILDDIR}/${cluster_name}-controllers.yaml
78 }
79
80 function is_addon_ready {
81     local -r addon=$1
82     local -r cluster_name=${CLUSTER_NAME:-icn}
83     local -r cluster_kubeconfig="${BUILDDIR}/${cluster_name}.conf"
84     [[ $(kubectl --kubeconfig=${cluster_kubeconfig} -n kud get HelmRelease/${addon} -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}') == "True" ]]
85 }
86
87 function test_addons {
88     # Create a temporary kubeconfig file for the tests
89     local -r cluster_name=${CLUSTER_NAME:-icn}
90     local -r cluster_kubeconfig="${BUILDDIR}/${cluster_name}.conf"
91     clusterctl -n metal3 get kubeconfig ${cluster_name} >${cluster_kubeconfig}
92
93     clone_kud_repository
94     pushd ${KUDPATH}/kud/tests
95     failed_kud_tests=""
96     container_runtime=$(KUBECONFIG=${cluster_kubeconfig} kubectl get nodes -o jsonpath='{.items[].status.nodeInfo.containerRuntimeVersion}')
97     # TODO Temporarily remove kubevirt from kud_tests below.  The
98     # kubevirt self-test needs AllowTcpForwarding yes in
99     # /etc/ssh/sshd_config which is currently disabled by the OS
100     # security hardening.
101     if [[ "${container_runtime}" == "containerd://1.2.13" ]]; then
102         # With containerd 1.2.13, the qat test container image fails to unpack.
103         kud_tests="topology-manager-sriov:sriov-network multus:multus-cni ovn4nfv:ovn4nfv-network nfd:node-feature-discovery sriov-network:sriov-network cmk:cpu-manager"
104     else
105         kud_tests="topology-manager-sriov:sriov-network multus:multus-cni ovn4nfv:ovn4nfv-network nfd:node-feature-discovery sriov-network:sriov-network qat:qat-device-plugin cmk:cpu-manager"
106     fi
107     for kud_test in ${kud_tests}; do
108         addon="${kud_test#*:}"
109         test="${kud_test%:*}"
110         if [[ ! -z ${addon} ]]; then
111             wait_for is_addon_ready ${addon}
112         fi
113         KUBECONFIG=${cluster_kubeconfig} bash ${test}.sh || failed_kud_tests="${failed_kud_tests} ${test}"
114     done
115     # The plugin_fw_v2 test needs the EMCO controllers in place
116     register_emco_controllers
117     DEMO_FOLDER=${KUDPATH}/kud/demo KUBECONFIG=${cluster_kubeconfig} bash plugin_fw_v2.sh --external || failed_kud_tests="${failed_kud_tests} plugin_fw_v2"
118     unregister_emco_controllers
119     popd
120     if [[ ! -z "$failed_kud_tests" ]]; then
121         echo "Test cases failed:${failed_kud_tests}"
122         exit 1
123     fi
124     echo "All test cases passed"
125
126     rm ${cluster_kubeconfig}
127 }
128
129 case $1 in
130     "test") test_addons ;;
131     *) cat <<EOF
132 Usage: $(basename $0) COMMAND
133
134 The "test" command looks for the CLUSTER_NAME variable in the
135 environment (default: "icn").  This should be the name of the
136 Cluster resource to execute the tests in.
137
138 Commands:
139   test  - Test the addons
140 EOF
141        ;;
142 esac