blob: d84f7318e570624b7a0b041844531482eb9a683e [file] [log] [blame]
rameshiyer27a1954d42022-11-14 06:00:12 +00001#!/bin/bash
2#
3# ============LICENSE_START====================================================
rameshiyer2793fd9982023-02-23 10:42:55 +00004# Copyright (C) 2022-2023 Nordix Foundation.
rameshiyer27a1954d42022-11-14 06:00:12 +00005# =============================================================================
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.
rameshiyer2707004862023-01-23 11:50:01 +000022# Runs CSITs in kubernetes.
23
adheli.tavares1153b6d2023-02-28 12:59:37 +000024if [ -z "${WORKSPACE}" ]; then
25 WORKSPACE=$(git rev-parse --show-toplevel)
26 export WORKSPACE
27fi
28
29CSIT_SCRIPT="scripts/run-test.sh"
rameshiyer2707004862023-01-23 11:50:01 +000030ROBOT_DOCKER_IMAGE="policy-csit-robot"
31POLICY_CLAMP_ROBOT="policy-clamp-test.robot"
32POLICY_API_ROBOT="api-test.robot"
33POLICY_PAP_ROBOT="pap-test.robot"
34POLICY_APEX_PDP_ROBOT="apex-pdp-test.robot"
35POLICY_XACML_PDP_ROBOT="xacml-pdp-test.robot"
36POLICY_DROOLS_PDP_ROBOT="drools-pdp-test.robot"
37POLICY_API_CONTAINER="policy-api"
38POLICY_PAP_CONTAINER="policy-pap"
39POLICY_CLAMP_CONTAINER="policy-clamp-runtime-acm"
40POLICY_APEX_CONTAINER="policy-apex-pdp"
rameshiyer2793fd9982023-02-23 10:42:55 +000041POLICY_DROOLS_CONTAINER="policy-drools-pdp"
42POLICY_XACML_CONTAINER="policy-xacml-pdp"
rameshiyer2707004862023-01-23 11:50:01 +000043
44export PROJECT=""
45export ROBOT_FILE=""
adheli.tavares1153b6d2023-02-28 12:59:37 +000046export ROBOT_LOG_DIR=${WORKSPACE}/csit/archives
rameshiyer2762ccbbb2023-02-01 15:06:45 +000047export READINESS_CONTAINERS=()
rameshiyer27a1954d42022-11-14 06:00:12 +000048
49function 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
rameshiyer2707004862023-01-23 11:50:01 +000064 echo "Enabling DNS and helm3 plugins"
rameshiyer276ebe5b62023-02-14 17:52:49 +000065 microk8s.enable dns helm3 hostpath-storage
rameshiyer27a1954d42022-11-14 06:00:12 +000066 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"
rameshiyer2707004862023-01-23 11:50:01 +000070 echo "----------------------------------------"
rameshiyer27a1954d42022-11-14 06:00:12 +000071 else
72 echo "K8s cluster is already running"
rameshiyer2707004862023-01-23 11:50:01 +000073 echo "----------------------------------------"
rameshiyer27a1954d42022-11-14 06:00:12 +000074 return 0
75 fi
76
77}
78
rameshiyer2707004862023-01-23 11:50:01 +000079
rameshiyer27a1954d42022-11-14 06:00:12 +000080function teardown_cluster () {
81 echo "Removing k8s cluster and k8s configuration file"
82 sudo snap remove microk8s;rm -rf $HOME/.kube/config
rameshiyer2707004862023-01-23 11:50:01 +000083 sudo rm -rf /dockerdata-nfs/mariadb-galera/
rameshiyer27a1954d42022-11-14 06:00:12 +000084 echo "K8s Cluster removed"
85}
86
87
rameshiyer2707004862023-01-23 11:50:01 +000088function build_robot_image () {
89 echo "Build docker image for robot framework"
adheli.tavares1153b6d2023-02-28 12:59:37 +000090 cd ${WORKSPACE}/csit/resources || exit;
rameshiyer2707004862023-01-23 11:50:01 +000091 clone_models
92 echo "Build robot framework docker image"
93 docker login -u docker -p docker nexus3.onap.org:10001
adheli.tavares1153b6d2023-02-28 12:59:37 +000094 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
rameshiyer2707004862023-01-23 11:50:01 +000098 echo "---------------------------------------------"
adheli.tavares1153b6d2023-02-28 12:59:37 +000099 echo "Importing robot image into microk8s registry"
rameshiyer2707004862023-01-23 11:50:01 +0000100 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.tavares1153b6d2023-02-28 12:59:37 +0000107 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;
rameshiyer27be226252023-02-09 14:46:18 +0000110 print_robot_log
rameshiyer2707004862023-01-23 11:50:01 +0000111 fi
112}
113
adheli.tavares1153b6d2023-02-28 12:59:37 +0000114
rameshiyer27be226252023-02-09 14:46:18 +0000115function print_robot_log () {
adheli.tavares1153b6d2023-02-28 12:59:37 +0000116 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
rameshiyer27be226252023-02-09 14:46:18 +0000122 robotpod=$(microk8s kubectl get po | grep policy-csit)
adheli.tavares1153b6d2023-02-28 12:59:37 +0000123 podName=$(echo "$robotpod" | awk '{print $1}')
rameshiyer27be226252023-02-09 14:46:18 +0000124 echo "The robot tests will begin once the policy components {${READINESS_CONTAINERS[*]}} are up and running..."
adheli.tavares1153b6d2023-02-28 12:59:37 +0000125 microk8s kubectl wait --for=jsonpath='{.status.phase}'=Running --timeout=700s pod/"$podName"
126 microk8s kubectl logs -f "$podName"
rameshiyer27be226252023-02-09 14:46:18 +0000127 echo "Please check the logs of policy-csit-robot pod for the test execution results"
128}
129
adheli.tavares1153b6d2023-02-28 12:59:37 +0000130
rameshiyer2707004862023-01-23 11:50:01 +0000131function clone_models () {
adheli.tavares1153b6d2023-02-28 12:59:37 +0000132 GERRIT_BRANCH=$(awk -F= '$1 == "defaultbranch" { print $2 }' "${WORKSPACE}"/.gitreview)
rameshiyer2707004862023-01-23 11:50:01 +0000133 echo GERRIT_BRANCH="${GERRIT_BRANCH}"
134 # download models examples
adheli.tavares1153b6d2023-02-28 12:59:37 +0000135 git clone -b "${GERRIT_BRANCH}" --single-branch https://github.com/onap/policy-models.git "${WORKSPACE}"/csit/resources/tests/models
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000136
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
rameshiyer2707004862023-01-23 11:50:01 +0000146}
147
148
149function get_robot_file () {
150 case $PROJECT in
151
152 clamp | policy-clamp)
153 export ROBOT_FILE=$POLICY_CLAMP_ROBOT
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000154 export READINESS_CONTAINERS=($POLICY_CLAMP_CONTAINER)
rameshiyer2707004862023-01-23 11:50:01 +0000155 ;;
156
157 api | policy-api)
158 export ROBOT_FILE=$POLICY_API_ROBOT
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000159 export READINESS_CONTAINERS=($POLICY_API_CONTAINER)
rameshiyer2707004862023-01-23 11:50:01 +0000160 ;;
161
162 pap | policy-pap)
163 export ROBOT_FILE=$POLICY_PAP_ROBOT
adheli.tavares1153b6d2023-02-28 12:59:37 +0000164 export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_PAP_CONTAINER,$POLICY_API_CONTAINER)
rameshiyer2707004862023-01-23 11:50:01 +0000165 ;;
166
167 apex-pdp | policy-apex-pdp)
168 export ROBOT_FILE=$POLICY_APEX_PDP_ROBOT
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000169 export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER)
rameshiyer2707004862023-01-23 11:50:01 +0000170 ;;
171
172 xacml-pdp | policy-xacml-pdp)
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000173 export ROBOT_FILE=($POLICY_XACML_PDP_ROBOT)
adheli.tavares1153b6d2023-02-28 12:59:37 +0000174 export READINESS_CONTAINERS=($POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER,$POLICY_XACML_CONTAINER)
rameshiyer2707004862023-01-23 11:50:01 +0000175 ;;
176
177 drools-pdp | policy-drools-pdp)
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000178 export ROBOT_FILE=($POLICY_DROOLS_PDP_ROBOT)
rameshiyer2793fd9982023-02-23 10:42:55 +0000179 export READINESS_CONTAINERS=($POLICY_DROOLS_CONTAINER)
rameshiyer2707004862023-01-23 11:50:01 +0000180 ;;
181
182 *)
183 echo "unknown project supplied"
184 ;;
185esac
186
187}
188
189
rameshiyer27a1954d42022-11-14 06:00:12 +0000190if [ $1 == "install" ]; then
191 spin_microk8s_cluster
192 if [ "${?}" -eq 0 ]; then
193 echo "Installing policy helm charts in the default namespace"
adheli.tavares1153b6d2023-02-28 12:59:37 +0000194 cd ${WORKSPACE}/helm || exit;
195 microk8s helm dependency build policy
196 microk8s helm install csit-policy policy
rameshiyer27a1954d42022-11-14 06:00:12 +0000197 echo "Policy chart installation completed"
adheli.tavares1153b6d2023-02-28 12:59:37 +0000198 echo "-------------------------------------------"
rameshiyer2707004862023-01-23 11:50:01 +0000199 fi
200
201 if [ "$2" ]; then
202 export PROJECT=$2
adheli.tavares1153b6d2023-02-28 12:59:37 +0000203 export ROBOT_LOG_DIR=${WORKSPACE}/csit/archives/${PROJECT}
rameshiyer2707004862023-01-23 11:50:01 +0000204 get_robot_file
205 echo "CSIT will be invoked from $ROBOT_FILE"
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000206 echo "Readiness containers: ${READINESS_CONTAINERS[*]}"
207 echo "-------------------------------------------"
rameshiyer2707004862023-01-23 11:50:01 +0000208 build_robot_image
209 else
210 echo "No project supplied for running CSIT"
rameshiyer27a1954d42022-11-14 06:00:12 +0000211 fi
212
213elif [ $1 == "uninstall" ]; then
214 teardown_cluster
215else
rameshiyer2707004862023-01-23 11:50:01 +0000216 echo "Invalid arguments provided. Usage: $0 [option..] {install {project} | uninstall}"
rameshiyer27a1954d42022-11-14 06:00:12 +0000217fi
218