6cf93cef6a69681fd9dc1b73840e7b6ecc00d2c1
[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 _functions.sh
18
19 base_url="http://localhost:8081"
20 cloud_region_id="kud"
21 namespace="default"
22 csar_id="94e414f6-9ca4-11e8-bb6a-52540067263b"
23 rbd_csar_id="7eb09e38-4363-9942-1234-3beb2e95fd85"
24 definition_id="9d117af8-30b8-11e9-af94-525400277b3d"
25 profile_id="ebe353d2-30b7-11e9-9515-525400277b3d"
26
27 # _build_generic_sim() - Creates a generic simulator image in case that doesn't exist
28 function _build_generic_sim {
29     if [[ -n $(docker images -q generic_sim) ]]; then
30         return
31     fi
32     BUILD_ARGS="--no-cache"
33     if [ $HTTP_PROXY ]; then
34         BUILD_ARGS+=" --build-arg HTTP_PROXY=${HTTP_PROXY}"
35     fi
36     if [ $HTTPS_PROXY ]; then
37         BUILD_ARGS+=" --build-arg HTTPS_PROXY=${HTTPS_PROXY}"
38     fi
39
40     pushd generic_simulator
41     echo "Building generic simulator image..."
42     docker build ${BUILD_ARGS} -t generic_sim:latest .
43     popd
44 }
45
46 # start_aai_service() - Starts a simulator for AAI service
47 function start_aai_service {
48     _build_generic_sim
49     if [[ $(docker ps -q --all --filter "name=aai") ]]; then
50         docker rm aai -f
51     fi
52     echo "Start AAI simulator.."
53     docker run --name aai -v $(mktemp):/tmp/generic_sim/ -v $(pwd)/generic_simulator/aai/:/etc/generic_sim/ -p 8443:8080 -d generic_sim
54 }
55
56 # Setup
57 destroy_deployment $plugin_deployment_name
58
59 #start_aai_service
60 populate_CSAR_plugin $csar_id
61 populate_CSAR_rbdefinition $rbd_csar_id
62
63 # Test
64 print_msg "Create Resource Bundle Definition Metadata"
65 payload_raw="
66 {
67     \"name\": \"test-rbdef\",
68     \"chart-name\": \"vault-consul-dev\",
69     \"description\": \"testing resource bundle definition api\",
70     \"uuid\": \"$definition_id\",
71     \"service-type\": \"firewall\"
72 }
73 "
74 payload=$(echo $payload_raw | tr '\n' ' ')
75 rbd_id=$(curl -s -d "$payload" -X POST "${base_url}/v1/rb/definition" | jq -r '.uuid')
76
77 print_msg "Upload Resource Bundle Definition Content"
78 curl -s --data-binary @${CSAR_DIR}/${rbd_csar_id}/${rbd_content_tarball}.gz -X POST "${base_url}/v1/rb/definition/$rbd_id/content"
79
80 print_msg "Listing Resource Bundle Definitions"
81 rbd_id_list=$(curl -s -X GET "${base_url}/v1/rb/definition")
82 if [[ "$rbd_id_list" != *"${rbd_id}"* ]]; then
83     echo $rbd_id_list
84     echo "Resource Bundle Definition not stored"
85     exit 1
86 fi
87
88 print_msg "Create Resource Bundle Profile Metadata"
89 kubeversion=$(kubectl version | grep 'Server Version' | awk -F '"' '{print $6}')
90 payload_raw="
91 {
92     \"name\": \"test-rbprofile\",
93     \"namespace\": \"$namespace\",
94     \"rbdid\": \"$definition_id\",
95     \"uuid\": \"$profile_id\",
96     \"kubernetesversion\": \"$kubeversion\"
97 }
98 "
99 payload=$(echo $payload_raw | tr '\n' ' ')
100 rbp_id=$(curl -s -d "$payload" -X POST "${base_url}/v1/rb/profile" | jq -r '.uuid')
101
102 print_msg "Upload Resource Bundle Profile Content"
103 curl -s --data-binary @${CSAR_DIR}/${rbd_csar_id}/${rbp_content_tarball}.gz -X POST "${base_url}/v1/rb/profile/$rbp_id/content"
104
105 print_msg "Listing Resource Bundle Profiles"
106 rbp_id_list=$(curl -s -X GET "${base_url}/v1/rb/profile")
107 if [[ "$rbp_id_list" != *"${rbp_id}"* ]]; then
108     echo $rbd_id_list
109     echo "Resource Bundle Profile not stored"
110     exit 1
111 fi
112
113 print_msg "Instantiate Profile"
114 payload_raw="
115 {
116     \"cloud_region_id\": \"$cloud_region_id\",
117     \"rb_profile_id\":\"$profile_id\",
118     \"csar_id\": \"$csar_id\"
119 }
120 "
121 payload=$(echo $payload_raw | tr '\n' ' ')
122 vnf_id=$(curl -s -d "$payload" "${base_url}/v1/vnf_instances/" | jq -r '.vnf_id')
123
124 print_msg "Validating Kubernetes"
125 kubectl get --no-headers=true --namespace=${namespace} deployment ${cloud_region_id}-${namespace}-${vnf_id}-test-rbprofile-vault-consul-dev
126 kubectl get --no-headers=true --namespace=${namespace} service ${cloud_region_id}-${namespace}-${vnf_id}-override-vault-consul
127 echo "VNF Instance created succesfully with id: $vnf_id"
128
129 print_msg "Listing VNF Instances"
130 vnf_id_list=$(curl -s -X GET "${base_url}/v1/vnf_instances/${cloud_region_id}/${namespace}" | jq -r '.vnf_id_list')
131 if [[ "$vnf_id_list" != *"${vnf_id}"* ]]; then
132     echo $vnf_id_list
133     echo "VNF Instance not stored"
134     exit 1
135 fi
136
137 print_msg "Getting $vnf_id VNF Instance information"
138 vnf_details=$(curl -s -X GET "${base_url}/v1/vnf_instances/${cloud_region_id}/${namespace}/${vnf_id}")
139 if [[ -z "$vnf_details" ]]; then
140     echo "Cannot retrieved VNF Instance details"
141     exit 1
142 fi
143 echo "VNF details $vnf_details"
144
145 print_msg "Deleting $rbd_id Resource Bundle Definition"
146 curl -X DELETE "${base_url}/v1/rb/definition/$rbd_id"
147 if [[ 500 -ne $(curl -o /dev/null -w %{http_code} -s -X GET "${base_url}/v1/rb/definition/$rbd_id") ]]; then
148     echo "Resource Bundle Definition not deleted"
149 # TODO: Change the HTTP code for 404 when the resource is not found in the API
150     exit 1
151 fi
152
153 print_msg "Deleting $vnf_id VNF Instance"
154 curl -X DELETE "${base_url}/v1/vnf_instances/${cloud_region_id}/${namespace}/${vnf_id}"
155 if [[ 404 -ne $(curl -o /dev/null -w %{http_code} -s -X GET "${base_url}${cloud_region_id}/${namespace}/${vnf_id}") ]]; then
156     echo "VNF Instance not deleted"
157     exit 1
158 fi
159
160 # Teardown
161 teardown $plugin_deployment_name