rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # ============LICENSE_START==================================================== |
rameshiyer27 | 93fd998 | 2023-02-23 10:42:55 +0000 | [diff] [blame] | 4 | # Copyright (C) 2022-2023 Nordix Foundation. |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 5 | # ============================================================================= |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | # SPDX-License-Identifier: Apache-2.0 |
| 19 | # ============LICENSE_END====================================================== |
| 20 | |
| 21 | # This script spins up kubernetes cluster in Microk8s for deploying policy helm charts. |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 22 | # Runs CSITs in kubernetes. |
| 23 | |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 24 | if [ -z "${WORKSPACE}" ]; then |
| 25 | WORKSPACE=$(git rev-parse --show-toplevel) |
| 26 | export WORKSPACE |
| 27 | fi |
| 28 | |
| 29 | CSIT_SCRIPT="scripts/run-test.sh" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 30 | ROBOT_DOCKER_IMAGE="policy-csit-robot" |
| 31 | POLICY_CLAMP_ROBOT="policy-clamp-test.robot" |
| 32 | POLICY_API_ROBOT="api-test.robot" |
| 33 | POLICY_PAP_ROBOT="pap-test.robot" |
| 34 | POLICY_APEX_PDP_ROBOT="apex-pdp-test.robot" |
| 35 | POLICY_XACML_PDP_ROBOT="xacml-pdp-test.robot" |
| 36 | POLICY_DROOLS_PDP_ROBOT="drools-pdp-test.robot" |
| 37 | POLICY_API_CONTAINER="policy-api" |
| 38 | POLICY_PAP_CONTAINER="policy-pap" |
| 39 | POLICY_CLAMP_CONTAINER="policy-clamp-runtime-acm" |
| 40 | POLICY_APEX_CONTAINER="policy-apex-pdp" |
rameshiyer27 | 93fd998 | 2023-02-23 10:42:55 +0000 | [diff] [blame] | 41 | POLICY_DROOLS_CONTAINER="policy-drools-pdp" |
| 42 | POLICY_XACML_CONTAINER="policy-xacml-pdp" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 43 | |
| 44 | export PROJECT="" |
| 45 | export ROBOT_FILE="" |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 46 | export ROBOT_LOG_DIR=${WORKSPACE}/csit/archives |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 47 | export READINESS_CONTAINERS=() |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 48 | |
| 49 | function spin_microk8s_cluster () { |
| 50 | echo "Verify if Microk8s cluster is running.." |
| 51 | microk8s version |
| 52 | exitcode="${?}" |
| 53 | |
| 54 | if [ "$exitcode" -ne 0 ]; then |
| 55 | echo "Microk8s cluster not available, Spinning up the cluster.." |
| 56 | sudo snap install microk8s --classic --channel=1.25/stable |
| 57 | |
| 58 | if [ "${?}" -ne 0 ]; then |
| 59 | echo "Failed to install kubernetes cluster. Aborting.." |
| 60 | return 1 |
| 61 | fi |
| 62 | echo "Microk8s cluster installed successfully" |
| 63 | sudo usermod -a -G microk8s $USER |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 64 | echo "Enabling DNS and helm3 plugins" |
rameshiyer27 | 6ebe5b6 | 2023-02-14 17:52:49 +0000 | [diff] [blame] | 65 | microk8s.enable dns helm3 hostpath-storage |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 66 | echo "Creating configuration file for Microk8s" |
| 67 | microk8s kubectl config view --raw > $HOME/.kube/config |
| 68 | chmod 600 $HOME/.kube/config |
| 69 | echo "K8s installation completed" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 70 | echo "----------------------------------------" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 71 | else |
| 72 | echo "K8s cluster is already running" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 73 | echo "----------------------------------------" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 74 | return 0 |
| 75 | fi |
| 76 | |
| 77 | } |
| 78 | |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 79 | |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 80 | function teardown_cluster () { |
| 81 | echo "Removing k8s cluster and k8s configuration file" |
| 82 | sudo snap remove microk8s;rm -rf $HOME/.kube/config |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 83 | sudo rm -rf /dockerdata-nfs/mariadb-galera/ |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 84 | echo "K8s Cluster removed" |
| 85 | } |
| 86 | |
| 87 | |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 88 | function build_robot_image () { |
| 89 | echo "Build docker image for robot framework" |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 90 | cd ${WORKSPACE}/csit/resources || exit; |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 91 | clone_models |
| 92 | echo "Build robot framework docker image" |
| 93 | docker login -u docker -p docker nexus3.onap.org:10001 |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 94 | docker build . --file Dockerfile \ |
| 95 | --build-arg CSIT_SCRIPT="$CSIT_SCRIPT" \ |
| 96 | --build-arg ROBOT_FILE="$ROBOT_FILE" \ |
| 97 | --tag "${ROBOT_DOCKER_IMAGE}" --no-cache |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 98 | echo "---------------------------------------------" |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 99 | echo "Importing robot image into microk8s registry" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 100 | docker save -o policy-csit-robot.tar ${ROBOT_DOCKER_IMAGE}:latest |
| 101 | microk8s ctr image import policy-csit-robot.tar |
| 102 | if [ "${?}" -eq 0 ]; then |
| 103 | rm -rf policy-csit-robot.tar |
| 104 | rm -rf tests/models/ |
| 105 | echo "---------------------------------------------" |
| 106 | echo "Installing Robot framework pod for running CSIT" |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 107 | cd ${WORKSPACE}/helm |
| 108 | mkdir -p ${ROBOT_LOG_DIR} |
| 109 | microk8s helm install csit-robot robot --set robot="$ROBOT_FILE" --set "readiness={${READINESS_CONTAINERS[*]}}" --set robotLogDir=$ROBOT_LOG_DIR; |
rameshiyer27 | be22625 | 2023-02-09 14:46:18 +0000 | [diff] [blame] | 110 | print_robot_log |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 111 | fi |
| 112 | } |
| 113 | |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 114 | |
rameshiyer27 | be22625 | 2023-02-09 14:46:18 +0000 | [diff] [blame] | 115 | function print_robot_log () { |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 116 | count_pods=0 |
| 117 | while [[ ${count_pods} -eq 0 ]]; do |
| 118 | echo "Waiting for pods to come up..." |
| 119 | sleep 5 |
| 120 | count_pods=$(microk8s kubectl get pods --output name | wc -l) |
| 121 | done |
rameshiyer27 | be22625 | 2023-02-09 14:46:18 +0000 | [diff] [blame] | 122 | robotpod=$(microk8s kubectl get po | grep policy-csit) |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 123 | podName=$(echo "$robotpod" | awk '{print $1}') |
rameshiyer27 | be22625 | 2023-02-09 14:46:18 +0000 | [diff] [blame] | 124 | echo "The robot tests will begin once the policy components {${READINESS_CONTAINERS[*]}} are up and running..." |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 125 | microk8s kubectl wait --for=jsonpath='{.status.phase}'=Running --timeout=700s pod/"$podName" |
| 126 | microk8s kubectl logs -f "$podName" |
rameshiyer27 | be22625 | 2023-02-09 14:46:18 +0000 | [diff] [blame] | 127 | echo "Please check the logs of policy-csit-robot pod for the test execution results" |
| 128 | } |
| 129 | |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 130 | |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 131 | function clone_models () { |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 132 | GERRIT_BRANCH=$(awk -F= '$1 == "defaultbranch" { print $2 }' "${WORKSPACE}"/.gitreview) |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 133 | echo GERRIT_BRANCH="${GERRIT_BRANCH}" |
| 134 | # download models examples |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 135 | git clone -b "${GERRIT_BRANCH}" --single-branch https://github.com/onap/policy-models.git "${WORKSPACE}"/csit/resources/tests/models |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 136 | |
| 137 | # create a couple of variations of the policy definitions |
| 138 | sed -e 's!Measurement_vGMUX!ADifferentValue!' \ |
| 139 | tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.json \ |
| 140 | >tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.v1_2.json |
| 141 | |
| 142 | sed -e 's!"version": "1.0.0"!"version": "2.0.0"!' \ |
| 143 | -e 's!"policy-version": 1!"policy-version": 2!' \ |
| 144 | tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.json \ |
| 145 | >tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.v2.json |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
| 149 | function get_robot_file () { |
| 150 | case $PROJECT in |
| 151 | |
| 152 | clamp | policy-clamp) |
| 153 | export ROBOT_FILE=$POLICY_CLAMP_ROBOT |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 154 | export READINESS_CONTAINERS=($POLICY_CLAMP_CONTAINER) |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 155 | ;; |
| 156 | |
| 157 | api | policy-api) |
| 158 | export ROBOT_FILE=$POLICY_API_ROBOT |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 159 | export READINESS_CONTAINERS=($POLICY_API_CONTAINER) |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 160 | ;; |
| 161 | |
| 162 | pap | policy-pap) |
| 163 | export ROBOT_FILE=$POLICY_PAP_ROBOT |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 164 | export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_PAP_CONTAINER,$POLICY_API_CONTAINER) |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 165 | ;; |
| 166 | |
| 167 | apex-pdp | policy-apex-pdp) |
| 168 | export ROBOT_FILE=$POLICY_APEX_PDP_ROBOT |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 169 | export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER) |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 170 | ;; |
| 171 | |
| 172 | xacml-pdp | policy-xacml-pdp) |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 173 | export ROBOT_FILE=($POLICY_XACML_PDP_ROBOT) |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 174 | export READINESS_CONTAINERS=($POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER,$POLICY_XACML_CONTAINER) |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 175 | ;; |
| 176 | |
| 177 | drools-pdp | policy-drools-pdp) |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 178 | export ROBOT_FILE=($POLICY_DROOLS_PDP_ROBOT) |
rameshiyer27 | 93fd998 | 2023-02-23 10:42:55 +0000 | [diff] [blame] | 179 | export READINESS_CONTAINERS=($POLICY_DROOLS_CONTAINER) |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 180 | ;; |
| 181 | |
| 182 | *) |
| 183 | echo "unknown project supplied" |
| 184 | ;; |
| 185 | esac |
| 186 | |
| 187 | } |
| 188 | |
| 189 | |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 190 | if [ $1 == "install" ]; then |
| 191 | spin_microk8s_cluster |
| 192 | if [ "${?}" -eq 0 ]; then |
| 193 | echo "Installing policy helm charts in the default namespace" |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 194 | cd ${WORKSPACE}/helm || exit; |
| 195 | microk8s helm dependency build policy |
| 196 | microk8s helm install csit-policy policy |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 197 | echo "Policy chart installation completed" |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 198 | echo "-------------------------------------------" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 199 | fi |
| 200 | |
| 201 | if [ "$2" ]; then |
| 202 | export PROJECT=$2 |
adheli.tavares | 1153b6d | 2023-02-28 12:59:37 +0000 | [diff] [blame^] | 203 | export ROBOT_LOG_DIR=${WORKSPACE}/csit/archives/${PROJECT} |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 204 | get_robot_file |
| 205 | echo "CSIT will be invoked from $ROBOT_FILE" |
rameshiyer27 | 62ccbbb | 2023-02-01 15:06:45 +0000 | [diff] [blame] | 206 | echo "Readiness containers: ${READINESS_CONTAINERS[*]}" |
| 207 | echo "-------------------------------------------" |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 208 | build_robot_image |
| 209 | else |
| 210 | echo "No project supplied for running CSIT" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 211 | fi |
| 212 | |
| 213 | elif [ $1 == "uninstall" ]; then |
| 214 | teardown_cluster |
| 215 | else |
rameshiyer27 | 0700486 | 2023-01-23 11:50:01 +0000 | [diff] [blame] | 216 | echo "Invalid arguments provided. Usage: $0 [option..] {install {project} | uninstall}" |
rameshiyer27 | a1954d4 | 2022-11-14 06:00:12 +0000 | [diff] [blame] | 217 | fi |
| 218 | |