Merge branch 'master' of https://osm.etsi.org/gerrit/osm/RO
[osm/RO.git] / test / basictest.sh
1 #!/bin/bash
2
3 ##
4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
6 # All Rights Reserved.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License"); you may
9 # not use this file except in compliance with the License. You may obtain
10 # a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17 # License for the specific language governing permissions and limitations
18 # under the License.
19 #
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
22 ##
23
24 #This script can be used as a basic test of openmano.
25 #WARNING: It destroy the database content
26
27
28 function usage(){
29 echo -e "usage: ${BASH_SOURCE[0]} [OPTIONS] <action>\n test openmano using openvim as a VIM"
30 echo -e " the OPENVIM_HOST, OPENVIM_PORT shell variables indicate openvim location"
31 echo -e " <action> is a list of the following items (by default 'reset create delete')"
32 echo -e " reset reset the openmano database content"
33 echo -e " create creates items"
34 echo -e " delete delete created items"
35 echo -e " OPTIONS:"
36 echo -e " -f --force does not prompt for confirmation"
37 echo -e " -h --help shows this help"
38 echo -e " --insert-bashrc insert the created tenant,datacenter variables at"
39 echo -e " ~/.bashrc to be available by openmano CLI"
40 echo -e " --init-openvim if openvim runs locally, an init is called to clean openvim database"
41 echo -e " and add fake hosts"
42 }
43
44 function is_valid_uuid(){
45 echo "$1" | grep -q -E '^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$' && return 0
46 return 1
47 }
48
49 #detect if is called with a source to use the 'exit'/'return' command for exiting
50 [[ ${BASH_SOURCE[0]} != $0 ]] && _exit="return" || _exit="exit"\1a
51
52 #check openvim client variables are set
53 fail=""
54 [[ -z $OPENVIM_HOST ]] && echo "OPENVIM_HOST variable not defined" >&2 && fail=1
55 [[ -z $OPENVIM_PORT ]] && echo "OPENVIM_PORT variable not defined" >&2 && fail=1
56 [[ -n $fail ]] && $_exit 1
57
58 #check correct arguments
59 force=""
60 action_list=""
61 insert_bashrc=""
62 init_openvim=""
63 for param in $*
64 do
65 if [[ $param == reset ]] || [[ $param == create ]] || [[ $param == delete ]]
66 then
67 action_list="$action_list $param"
68 elif [[ $param == -h ]] || [[ $param == --help ]]
69 then
70 usage
71 $_exit 0
72 elif [[ $param == -f ]] || [[ $param == --force ]]
73 then
74 force="-f"
75 elif [[ $param == --insert-bashrc ]]
76 then
77 insert_bashrc=y
78 elif [[ $param == --init-openvim ]]
79 then
80 init_openvim=y
81 else
82 echo "invalid argument '$param'? Type -h for help" >&2 && $_exit 1
83 fi
84 done
85
86 DIRNAME=$(dirname $(readlink -f ${BASH_SOURCE[0]}))
87 DIRmano=$(dirname $DIRNAME)
88 DIRscript=${DIRmano}/scripts
89 #by default action should be reset and create
90 [[ -z $action_list ]] && action_list="reset create delete"
91 [[ -z $init_openvim ]] || initopenvim $force || echo "WARNING openvim cannot be initialized. The rest of test can fail!"
92
93 for action in $action_list
94 do
95 #if [[ $action == "install-openvim" ]]
96 #echo "Installing and starting openvim"
97 #mkdir -p temp
98 #pushd temp
99 #wget https://github.com/nfvlabs/openvim/raw/v0.4/scripts/install-openvim.sh
100 #chmod -x install-openvim.sh
101 #fi
102
103 if [[ $action == "reset" ]]
104 then
105
106 #ask for confirmation if argument is not -f --force
107 [[ -z $force ]] && read -e -p "WARNING: reset openmano database, content will be lost!!! Continue(y/N) " force_
108 [[ $force_ != y ]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
109
110 echo "Stopping openmano"
111 $DIRscript/service-openmano.sh mano stop
112 echo "Initializing openmano database"
113 $DIRmano/database_utils/init_mano_db.sh -u mano -p manopw
114 echo "Starting openmano"
115 $DIRscript/service-openmano.sh mano start
116 echo
117
118 elif [[ $action == "delete" ]]
119 then
120 result=`openmano tenant-list TEST-tenant`
121 nfvotenant=`echo $result |gawk '{print $1}'`
122 #check a valid uuid is obtained
123 is_valid_uuid $nfvotenant || ! echo "Tenant TEST-tenant not found. Already delete?" >&2 || $_exit 1
124 export OPENMANO_TENANT=$nfvotenant
125 ${DIRmano}/openmano instance-scenario-delete -f simple-instance || echo "fail"
126 ${DIRmano}/openmano instance-scenario-delete -f complex-instance || echo "fail"
127 ${DIRmano}/openmano instance-scenario-delete -f complex2-instance || echo "fail"
128 ${DIRmano}/openmano scenario-delete -f simple || echo "fail"
129 ${DIRmano}/openmano scenario-delete -f complex || echo "fail"
130 ${DIRmano}/openmano scenario-delete -f complex2 || echo "fail"
131 ${DIRmano}/openmano vnf-delete -f linux || echo "fail"
132 ${DIRmano}/openmano vnf-delete -f dataplaneVNF_2VMs || echo "fail"
133 ${DIRmano}/openmano vnf-delete -f dataplaneVNF2 || echo "fail"
134 ${DIRmano}/openmano vnf-delete -f dataplaneVNF3 || echo "fail"
135 ${DIRmano}/openmano datacenter-detach TEST-dc || echo "fail"
136 ${DIRmano}/openmano datacenter-delete -f TEST-dc || echo "fail"
137 ${DIRmano}/openmano tenant-delete -f TEST-tenant || echo "fail"
138 echo
139
140 elif [[ $action == "create" ]]
141 then
142 printf "%-50s" "Creating openmano tenant 'TEST-tenant': "
143 result=`${DIRmano}/openmano tenant-create TEST-tenant --description="created by basictest.sh"`
144 nfvotenant=`echo $result |gawk '{print $1}'`
145 #check a valid uuid is obtained
146 ! is_valid_uuid $nfvotenant && echo "FAIL" && echo " $result" && $_exit 1
147 export OPENMANO_TENANT=$nfvotenant
148 [[ $insert_bashrc == y ]] && echo -e "\nexport OPENMANO_TENANT=$nfvotenant" >> ~/.bashrc
149 echo $nfvotenant
150
151 printf "%-50s" "Creating datacenter 'TEST-dc' in openmano:"
152 [[ -z $OPENVIM_HOST ]] && OPENVIM_HOST=localhost
153 [[ -z $OPENVIM_PORT ]] && OPENVIM_PORT=9080
154 URL_ADMIN_PARAM=""
155 [[ -n $OPENVIM_ADMIN_PORT ]] && URL_ADMIN_PARAM="--url_admin=http://${$OPENVIM_HOST}:${OPENVIM_ADMIN_PORT}/openvim"
156 result=`${DIRmano}/openmano datacenter-create TEST-dc "http://${OPENVIM_HOST}:${OPENVIM_PORT}/openvim" --type=openvim $URL_ADMIN_PARAM`
157 datacenter=`echo $result |gawk '{print $1}'`
158 #check a valid uuid is obtained
159 ! is_valid_uuid $datacenter && echo "FAIL" && echo " $result" && $_exit 1
160 echo $datacenter
161 export OPENMANO_DATACENTER=$datacenter
162 [[ $insert_bashrc == y ]] && echo -e "\nexport OPENMANO_DATACENTER=$datacenter" >> ~/.bashrc
163
164 printf "%-50s" "Attaching openmano tenant to the datacenter:"
165 result=`${DIRmano}/openmano datacenter-attach TEST-dc`
166 [[ $? != 0 ]] && echo "FAIL" && echo " $result" && $_exit 1
167 echo OK
168
169 printf "%-50s" "Updating external nets in openmano: "
170 result=`${DIRmano}/openmano datacenter-netmap-delete -f --all`
171 [[ $? != 0 ]] && echo "FAIL" && echo " $result" && $_exit 1
172 result=`${DIRmano}/openmano datacenter-netmap-upload -f`
173 [[ $? != 0 ]] && echo "FAIL" && echo " $result" && $_exit 1
174 echo OK
175
176 for VNF in linux dataplaneVNF1 dataplaneVNF2 dataplaneVNF_2VMs dataplaneVNF3
177 do
178 printf "%-50s" "Creating VNF '${VNF}': "
179 result=`$DIRmano/openmano vnf-create $DIRmano/vnfs/examples/${VNF}.yaml`
180 vnf=`echo $result |gawk '{print $1}'`
181 #check a valid uuid is obtained
182 ! is_valid_uuid $vnf && echo FAIL && echo " $result" && $_exit 1
183 echo $vnf
184 done
185 for NS in simple complex complex2
186 do
187 printf "%-50s" "Creating scenario '${NS}':"
188 result=`$DIRmano/openmano scenario-create $DIRmano/scenarios/examples/${NS}.yaml`
189 scenario=`echo $result |gawk '{print $1}'`
190 ! is_valid_uuid $scenario && echo FAIL && echo " $result" && $_exit 1
191 echo $scenario
192 done
193
194 for IS in simple complex complex2
195 do
196 printf "%-50s" "Creating instance-scenario '${IS}':"
197 result=`$DIRmano/openmano instance-scenario-create --scenario ${IS} --name ${IS}-instance`
198 instance=`echo $result |gawk '{print $1}'`
199 ! is_valid_uuid $instance && echo FAIL && echo " $result" && $_exit 1
200 echo $instance
201 done
202
203 echo
204 #echo "Check virtual machines are deployed"
205 #vms_error=`openvim vm-list | grep ERROR | wc -l`
206 #vms=`openvim vm-list | wc -l`
207 #[[ $vms -ne 8 ]] && echo "WARNING: $vms VMs created, must be 8 VMs" >&2 && $_exit 1
208 #[[ $vms_error -gt 0 ]] && echo "WARNING: $vms_error VMs with ERROR" >&2 && $_exit 1
209 fi
210 done
211
212 echo
213 echo DONE
214
215