blob: 22a67f9c48fc3619a8dac7fff96cfff3cde7bd75 [file] [log] [blame]
rameshiyer27a1954d42022-11-14 06:00:12 +00001#!/bin/bash
2#
3# ============LICENSE_START====================================================
4# Copyright (C) 2022 Nordix Foundation.
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.
rameshiyer2707004862023-01-23 11:50:01 +000022# Runs CSITs in kubernetes.
23
24CSIT_SCRIPT="run-test.sh"
25ROBOT_DOCKER_IMAGE="policy-csit-robot"
26POLICY_CLAMP_ROBOT="policy-clamp-test.robot"
27POLICY_API_ROBOT="api-test.robot"
28POLICY_PAP_ROBOT="pap-test.robot"
29POLICY_APEX_PDP_ROBOT="apex-pdp-test.robot"
30POLICY_XACML_PDP_ROBOT="xacml-pdp-test.robot"
31POLICY_DROOLS_PDP_ROBOT="drools-pdp-test.robot"
32POLICY_API_CONTAINER="policy-api"
33POLICY_PAP_CONTAINER="policy-pap"
34POLICY_CLAMP_CONTAINER="policy-clamp-runtime-acm"
35POLICY_APEX_CONTAINER="policy-apex-pdp"
36
37export PROJECT=""
38export ROBOT_FILE=""
rameshiyer276ebe5b62023-02-14 17:52:49 +000039export ROBOT_LOG_DIR=${PWD}/archives
rameshiyer2762ccbbb2023-02-01 15:06:45 +000040export READINESS_CONTAINERS=()
rameshiyer27a1954d42022-11-14 06:00:12 +000041
42function spin_microk8s_cluster () {
43 echo "Verify if Microk8s cluster is running.."
44 microk8s version
45 exitcode="${?}"
46
47 if [ "$exitcode" -ne 0 ]; then
48 echo "Microk8s cluster not available, Spinning up the cluster.."
49 sudo snap install microk8s --classic --channel=1.25/stable
50
51 if [ "${?}" -ne 0 ]; then
52 echo "Failed to install kubernetes cluster. Aborting.."
53 return 1
54 fi
55 echo "Microk8s cluster installed successfully"
56 sudo usermod -a -G microk8s $USER
rameshiyer2707004862023-01-23 11:50:01 +000057 echo "Enabling DNS and helm3 plugins"
rameshiyer276ebe5b62023-02-14 17:52:49 +000058 microk8s.enable dns helm3 hostpath-storage
rameshiyer27a1954d42022-11-14 06:00:12 +000059 echo "Creating configuration file for Microk8s"
60 microk8s kubectl config view --raw > $HOME/.kube/config
61 chmod 600 $HOME/.kube/config
62 echo "K8s installation completed"
rameshiyer2707004862023-01-23 11:50:01 +000063 echo "----------------------------------------"
rameshiyer27a1954d42022-11-14 06:00:12 +000064 else
65 echo "K8s cluster is already running"
rameshiyer2707004862023-01-23 11:50:01 +000066 echo "----------------------------------------"
rameshiyer27a1954d42022-11-14 06:00:12 +000067 return 0
68 fi
69
70}
71
rameshiyer2707004862023-01-23 11:50:01 +000072
rameshiyer27a1954d42022-11-14 06:00:12 +000073function teardown_cluster () {
74 echo "Removing k8s cluster and k8s configuration file"
75 sudo snap remove microk8s;rm -rf $HOME/.kube/config
rameshiyer2707004862023-01-23 11:50:01 +000076 sudo rm -rf /dockerdata-nfs/mariadb-galera/
rameshiyer27a1954d42022-11-14 06:00:12 +000077 echo "K8s Cluster removed"
78}
79
80
rameshiyer2707004862023-01-23 11:50:01 +000081function build_robot_image () {
82 echo "Build docker image for robot framework"
83 cd ../helm;
84 clone_models
85 echo "Build robot framework docker image"
86 docker login -u docker -p docker nexus3.onap.org:10001
87 docker build . --file Dockerfile --build-arg CSIT_SCRIPT="$CSIT_SCRIPT" --build-arg ROBOT_FILE="$ROBOT_FILE" --tag "${ROBOT_DOCKER_IMAGE}" --no-cache
88 echo "---------------------------------------------"
89 echo "Importing robot image in to microk8s registry"
90 docker save -o policy-csit-robot.tar ${ROBOT_DOCKER_IMAGE}:latest
91 microk8s ctr image import policy-csit-robot.tar
92 if [ "${?}" -eq 0 ]; then
93 rm -rf policy-csit-robot.tar
94 rm -rf tests/models/
95 echo "---------------------------------------------"
96 echo "Installing Robot framework pod for running CSIT"
rameshiyer276ebe5b62023-02-14 17:52:49 +000097 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 +000098 print_robot_log
rameshiyer2707004862023-01-23 11:50:01 +000099 fi
100}
101
rameshiyer27be226252023-02-09 14:46:18 +0000102function print_robot_log () {
103 robotpod=$(microk8s kubectl get po | grep policy-csit)
104 podName=$(echo $robotpod | awk '{print $1}')
105 echo "The robot tests will begin once the policy components {${READINESS_CONTAINERS[*]}} are up and running..."
106 microk8s kubectl wait --for=condition=ready --timeout=180s pod/$podName
107 microk8s kubectl logs -f $podName
108 echo "Please check the logs of policy-csit-robot pod for the test execution results"
109}
110
rameshiyer2707004862023-01-23 11:50:01 +0000111function clone_models () {
112 GIT_TOP=$(git rev-parse --show-toplevel)
113 GERRIT_BRANCH=$(awk -F= '$1 == "defaultbranch" { print $2 }' \
114 "${GIT_TOP}"/.gitreview)
115 echo GERRIT_BRANCH="${GERRIT_BRANCH}"
116 # download models examples
117 git clone -b "${GERRIT_BRANCH}" --single-branch https://github.com/onap/policy-models.git tests/models
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000118
119 # create a couple of variations of the policy definitions
120 sed -e 's!Measurement_vGMUX!ADifferentValue!' \
121 tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.json \
122 >tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.v1_2.json
123
124 sed -e 's!"version": "1.0.0"!"version": "2.0.0"!' \
125 -e 's!"policy-version": 1!"policy-version": 2!' \
126 tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.json \
127 >tests/models/models-examples/src/main/resources/policies/vCPE.policy.monitoring.input.tosca.v2.json
rameshiyer2707004862023-01-23 11:50:01 +0000128}
129
130
131function get_robot_file () {
132 case $PROJECT in
133
134 clamp | policy-clamp)
135 export ROBOT_FILE=$POLICY_CLAMP_ROBOT
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000136 export READINESS_CONTAINERS=($POLICY_CLAMP_CONTAINER)
rameshiyer2707004862023-01-23 11:50:01 +0000137 ;;
138
139 api | policy-api)
140 export ROBOT_FILE=$POLICY_API_ROBOT
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000141 export READINESS_CONTAINERS=($POLICY_API_CONTAINER)
rameshiyer2707004862023-01-23 11:50:01 +0000142 ;;
143
144 pap | policy-pap)
145 export ROBOT_FILE=$POLICY_PAP_ROBOT
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000146 export READINESS_CONTAINERS=($POLICY_PAP_CONTAINER,$POLICY_API_CONTAINER)
rameshiyer2707004862023-01-23 11:50:01 +0000147 ;;
148
149 apex-pdp | policy-apex-pdp)
150 export ROBOT_FILE=$POLICY_APEX_PDP_ROBOT
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000151 export READINESS_CONTAINERS=($POLICY_APEX_CONTAINER,$POLICY_API_CONTAINER,$POLICY_PAP_CONTAINER)
rameshiyer2707004862023-01-23 11:50:01 +0000152 ;;
153
154 xacml-pdp | policy-xacml-pdp)
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000155 export ROBOT_FILE=($POLICY_XACML_PDP_ROBOT)
rameshiyer2707004862023-01-23 11:50:01 +0000156 ;;
157
158 drools-pdp | policy-drools-pdp)
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000159 export ROBOT_FILE=($POLICY_DROOLS_PDP_ROBOT)
rameshiyer2707004862023-01-23 11:50:01 +0000160 ;;
161
162 *)
163 echo "unknown project supplied"
164 ;;
165esac
166
167}
168
169
rameshiyer27a1954d42022-11-14 06:00:12 +0000170if [ $1 == "install" ]; then
171 spin_microk8s_cluster
172 if [ "${?}" -eq 0 ]; then
173 echo "Installing policy helm charts in the default namespace"
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000174 cd ../helm/;microk8s helm dependency build policy;microk8s helm install csit-policy policy;
rameshiyer27a1954d42022-11-14 06:00:12 +0000175 echo "Policy chart installation completed"
rameshiyer2707004862023-01-23 11:50:01 +0000176 echo "-------------------------------------------"
177 fi
178
179 if [ "$2" ]; then
180 export PROJECT=$2
181 get_robot_file
182 echo "CSIT will be invoked from $ROBOT_FILE"
rameshiyer2762ccbbb2023-02-01 15:06:45 +0000183 echo "Readiness containers: ${READINESS_CONTAINERS[*]}"
184 echo "-------------------------------------------"
rameshiyer2707004862023-01-23 11:50:01 +0000185 build_robot_image
186 else
187 echo "No project supplied for running CSIT"
rameshiyer27a1954d42022-11-14 06:00:12 +0000188 fi
189
190elif [ $1 == "uninstall" ]; then
191 teardown_cluster
192else
rameshiyer2707004862023-01-23 11:50:01 +0000193 echo "Invalid arguments provided. Usage: $0 [option..] {install {project} | uninstall}"
rameshiyer27a1954d42022-11-14 06:00:12 +0000194fi
195