Merge "Add X710 to iavf driver NICs"
[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:9015/v1"
21 kubeconfig_path="$HOME/.kube/config"
22 #Will resolve to file $KUBE_CONFIG_DIR/kud
23 cloud_region_id="kud"
24 cloud_region_owner="test_owner"
25 namespace="testns"
26 csar_id="94e414f6-9ca4-11e8-bb6a-52540067263b"
27 rb_name="test-rbdef"
28 rb_version="v1"
29 profile_name="profile1"
30 release_name="testrelease"
31 vnf_customization_uuid="ebe353d2-30b7-11e9-9515-525400277b3d"
32
33 # _build_generic_sim() - Creates a generic simulator image in case that doesn't exist
34 function _build_generic_sim {
35     if [[ -n $(docker images -q generic_sim) ]]; then
36         return
37     fi
38     BUILD_ARGS="--no-cache"
39     if [ ${HTTP_PROXY:-} ]; then
40         BUILD_ARGS+=" --build-arg HTTP_PROXY=${HTTP_PROXY}"
41     fi
42     if [ ${HTTPS_PROXY:-} ]; then
43         BUILD_ARGS+=" --build-arg HTTPS_PROXY=${HTTPS_PROXY}"
44     fi
45
46     pushd generic_simulator
47     echo "Building generic simulator image..."
48     docker build ${BUILD_ARGS} -t generic_sim:latest .
49     popd
50 }
51
52 # start_aai_service() - Starts a simulator for AAI service
53 function start_aai_service {
54     _build_generic_sim
55     if [[ $(docker ps -q --all --filter "name=aai") ]]; then
56         docker rm aai -f
57     fi
58     echo "Start AAI simulator.."
59     docker run --name aai -v $(mktemp):/tmp/generic_sim/ -v $(pwd)/generic_simulator/aai/:/etc/generic_sim/ -p 8443:8080 -d generic_sim
60 }
61
62 # Setup
63 install_deps
64 destroy_deployment $plugin_deployment_name
65
66 #start_aai_service
67 populate_CSAR_plugin $csar_id
68 populate_CSAR_rbdefinition $csar_id
69
70 # Test
71 print_msg "Create Resource Bundle Definition Metadata"
72 payload="
73 {
74     \"rb-name\": \"${rb_name}\",
75     \"rb-version\": \"${rb_version}\",
76     \"chart-name\": \"vault-consul-dev\",
77     \"description\": \"testing resource bundle definition api\",
78     \"labels\": {
79         \"vnf_customization_uuid\": \"${vnf_customization_uuid}\"
80     }
81 }
82 "
83 call_api -d "$payload" "${base_url}/rb/definition"
84
85 print_msg "Upload Resource Bundle Definition Content"
86 call_api --data-binary "@${CSAR_DIR}/${csar_id}/vault-consul-dev-0.0.0.tgz" "${base_url}/rb/definition/$rb_name/$rb_version/content"
87
88 print_msg "Listing Resource Bundle Definitions"
89 rb_list=$(call_api "${base_url}/rb/definition/$rb_name")
90 if [[ "$rb_list" != *"${rb_name}"* ]]; then
91     echo $rb_list
92     echo "Resource Bundle Definition not stored"
93     exit 1
94 fi
95
96 print_msg "Create Resource Bundle Profile Metadata"
97 kubeversion=$(kubectl version | grep 'Server Version' | awk -F '"' '{print $6}')
98 payload="
99 {
100     \"profile-name\": \"${profile_name}\",
101     \"rb-name\": \"${rb_name}\",
102     \"rb-version\": \"${rb_version}\",
103     \"release-name\": \"${release_name}\",
104     \"namespace\": \"$namespace\",
105     \"kubernetesversion\": \"$kubeversion\",
106     \"labels\": {
107         \"vnf_customization_uuid\": \"${vnf_customization_uuid}\"
108     }
109 }
110 "
111 call_api -d "$payload" "${base_url}/rb/definition/$rb_name/$rb_version/profile"
112
113 print_msg "Upload Resource Bundle Profile Content"
114 call_api --data-binary "@${CSAR_DIR}/${csar_id}/rb_profile.tar.gz" "${base_url}/rb/definition/$rb_name/$rb_version/profile/$profile_name/content"
115
116 print_msg "Getting Resource Bundle Profile"
117 rbp_ret=$(call_api "${base_url}/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 "Setup cloud data"
125 payload="$(cat <<EOF
126 {
127     "cloud-region": "$cloud_region_id",
128     "cloud-owner": "$cloud_region_owner"
129 }
130 EOF
131 )"
132 call_api -F "metadata=$payload" \
133     -F "file=@$kubeconfig_path" \
134     "${base_url}/connectivity-info" >/dev/null
135
136 print_msg "Instantiate Profile"
137 payload="
138 {
139     \"cloud-region\": \"$cloud_region_id\",
140     \"rb-name\":\"$rb_name\",
141     \"rb-version\":\"$rb_version\",
142     \"profile-name\":\"$profile_name\"
143 }
144 "
145 inst_id=$(call_api -d "$payload" "${base_url}/instance")
146 echo "$inst_id"
147 inst_id=$(jq -r '.id' <<< "$inst_id")
148
149 print_msg "Validating Kubernetes"
150 kubectl get --no-headers=true --namespace=${namespace} deployment ${release_name}-vault-consul-dev
151 kubectl get --no-headers=true --namespace=${namespace} service override-vault-consul
152 echo "VNF Instance created succesfully with id: $inst_id"
153
154 print_msg "Getting $inst_id VNF Instance information"
155 call_api "${base_url}/instance/${inst_id}"
156
157 # Teardown
158 print_msg "Deleting $rb_name/$rb_version Resource Bundle Definition"
159 # TODO: Change the HTTP code for 404 when the resource is not found in the API
160 delete_resource "${base_url}/rb/definition/$rb_name/$rb_version"
161
162 print_msg "Deleting $profile_name Resource Bundle Profile"
163 # TODO: Change the HTTP code for 404 when the resource is not found in the API
164 delete_resource "${base_url}/rb/definition/$rb_name/$rb_version/profile/$profile_name"
165
166 print_msg "Deleting $inst_id VNF Instance"
167 delete_resource "${base_url}/instance/${inst_id}"
168
169 print_msg "Deleting ${cloud_region_id} cloud region connection"
170 delete_resource "${base_url}/connectivity-info/${cloud_region_id}"
171
172 teardown $plugin_deployment_name