add python compatibility module
[multicloud/k8s.git] / kud / tests / plugin.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2018
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 set -o errexit
12 set -o nounset
13 set -o pipefail
14 #set -o xtrace
15
16 source _common.sh
17 source _common_test.sh
18 source _functions.sh
19
20 base_url="http://localhost:8081"
21 #Will resolve to file $KUBE_CONFIG_DIR/kud
22 cloud_region_id="kud"
23 namespace="testns"
24 csar_id="94e414f6-9ca4-11e8-bb6a-52540067263b"
25 rb_name="test-rbdef"
26 rb_version="v1"
27 profile_name="profile1"
28 release_name="testrelease"
29 vnf_customization_uuid="ebe353d2-30b7-11e9-9515-525400277b3d"
30
31 # _build_generic_sim() - Creates a generic simulator image in case that doesn't exist
32 function _build_generic_sim {
33     if [[ -n $(docker images -q generic_sim) ]]; then
34         return
35     fi
36     BUILD_ARGS="--no-cache"
37     if [ $HTTP_PROXY ]; then
38         BUILD_ARGS+=" --build-arg HTTP_PROXY=${HTTP_PROXY}"
39     fi
40     if [ $HTTPS_PROXY ]; then
41         BUILD_ARGS+=" --build-arg HTTPS_PROXY=${HTTPS_PROXY}"
42     fi
43
44     pushd generic_simulator
45     echo "Building generic simulator image..."
46     docker build ${BUILD_ARGS} -t generic_sim:latest .
47     popd
48 }
49
50 # start_aai_service() - Starts a simulator for AAI service
51 function start_aai_service {
52     _build_generic_sim
53     if [[ $(docker ps -q --all --filter "name=aai") ]]; then
54         docker rm aai -f
55     fi
56     echo "Start AAI simulator.."
57     docker run --name aai -v $(mktemp):/tmp/generic_sim/ -v $(pwd)/generic_simulator/aai/:/etc/generic_sim/ -p 8443:8080 -d generic_sim
58 }
59
60 # Setup
61 install_deps
62 destroy_deployment $plugin_deployment_name
63
64 #start_aai_service
65 populate_CSAR_plugin $csar_id
66 populate_CSAR_rbdefinition $csar_id
67
68 # Test
69 print_msg "Create Resource Bundle Definition Metadata"
70 payload_raw="
71 {
72     \"rb-name\": \"${rb_name}\",
73     \"rb-version\": \"${rb_version}\",
74     \"chart-name\": \"vault-consul-dev\",
75     \"description\": \"testing resource bundle definition api\",
76     \"labels\": {
77         \"vnf_customization_uuid\": \"${vnf_customization_uuid}\"
78     }
79 }
80 "
81 payload=$(echo $payload_raw | tr '\n' ' ')
82 rb_ret_name=$(curl -s -d "$payload" -X POST "${base_url}/v1/rb/definition" | jq -r '."rb-name"')
83
84 print_msg "Upload Resource Bundle Definition Content"
85 curl -s --data-binary @${CSAR_DIR}/${csar_id}/${rbd_content_tarball}.gz -X POST "${base_url}/v1/rb/definition/$rb_name/$rb_version/content"
86
87 print_msg "Listing Resource Bundle Definitions"
88 rb_list=$(curl -s -X GET "${base_url}/v1/rb/definition/$rb_name")
89 if [[ "$rb_list" != *"${rb_name}"* ]]; then
90     echo $rb_list
91     echo "Resource Bundle Definition not stored"
92     exit 1
93 fi
94
95 print_msg "Create Resource Bundle Profile Metadata"
96 kubeversion=$(kubectl version | grep 'Server Version' | awk -F '"' '{print $6}')
97 payload_raw="
98 {
99     \"profile-name\": \"${profile_name}\",
100     \"rb-name\": \"${rb_name}\",
101     \"rb-version\": \"${rb_version}\",
102     \"release-name\": \"${release_name}\",
103     \"namespace\": \"$namespace\",
104     \"kubernetesversion\": \"$kubeversion\",
105     \"labels\": {
106         \"vnf_customization_uuid\": \"${vnf_customization_uuid}\"
107     }
108 }
109 "
110 payload=$(echo $payload_raw | tr '\n' ' ')
111 rbp_ret_name=$(curl -s -d "$payload" -X POST "${base_url}/v1/rb/definition/$rb_name/$rb_version/profile" | jq -r '."profile-name"')
112
113 print_msg "Upload Resource Bundle Profile Content"
114 curl -s --data-binary @${CSAR_DIR}/${csar_id}/${rbp_content_tarball}.gz -X POST "${base_url}/v1/rb/definition/$rb_name/$rb_version/profile/$profile_name/content"
115
116 print_msg "Getting Resource Bundle Profile"
117 rbp_ret=$(curl -s -X GET "${base_url}/v1/rb/definition/$rb_name/$rb_version/profile/$profile_name")
118 if [[ "$rbp_ret" != *"${profile_name}"* ]]; then
119     echo $rbp_ret
120     echo "Resource Bundle Profile not stored"
121     exit 1
122 fi
123
124 print_msg "Instantiate Profile"
125 payload_raw="
126 {
127     \"cloud-region\": \"$cloud_region_id\",
128     \"rb-name\":\"$rb_name\",
129     \"rb-version\":\"$rb_version\",
130     \"profile-name\":\"$profile_name\"
131 }
132 "
133 payload=$(echo $payload_raw | tr '\n' ' ')
134 inst_id=$(curl -s -d "$payload" "${base_url}/v1/instance" | jq -r '.id')
135
136 print_msg "Validating Kubernetes"
137 kubectl get --no-headers=true --namespace=${namespace} deployment ${release_name}-vault-consul-dev
138 kubectl get --no-headers=true --namespace=${namespace} service override-vault-consul
139 echo "VNF Instance created succesfully with id: $inst_id"
140
141 print_msg "Getting $inst_id VNF Instance information"
142 vnf_details=$(curl -s -X GET "${base_url}/v1/instance/${inst_id}")
143 if [[ -z "$vnf_details" ]]; then
144     echo "Cannot retrieved VNF Instance details"
145     exit 1
146 fi
147 echo "VNF details $vnf_details"
148
149 print_msg "Deleting $rb_name/$rb_version Resource Bundle Definition"
150 curl -X DELETE "${base_url}/v1/rb/definition/$rb_name/$rb_version"
151 if [[ 500 -ne $(curl -o /dev/null -w %{http_code} -s -X GET "${base_url}/v1/rb/definition/$rb_name/$rb_version") ]]; then
152     echo "Resource Bundle Definition not deleted"
153 # TODO: Change the HTTP code for 404 when the resource is not found in the API
154     exit 1
155 fi
156
157 print_msg "Deleting $profile_name Resource Bundle Profile"
158 curl -X DELETE "${base_url}/v1/rb/definition/$rb_name/$rb_version/profile/$profile_name"
159 if [[ 500 -ne $(curl -o /dev/null -w %{http_code} -s -X GET "${base_url}/v1/rb/definition/$rb_name/$rb_version/profile/$profile_name") ]]; then
160     echo "Resource Bundle Profile not deleted"
161 # TODO: Change the HTTP code for 404 when the resource is not found in the API
162     exit 1
163 fi
164
165 print_msg "Deleting $inst_id VNF Instance"
166 curl -X DELETE "${base_url}/v1/instance/${inst_id}"
167 if [[ 404 -ne $(curl -o /dev/null -w %{http_code} -s -X GET "${base_url}/${inst_id}") ]]; then
168     echo "VNF Instance not deleted"
169     exit 1
170 fi
171
172 # Teardown
173 teardown $plugin_deployment_name