4 # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
5 # This file is part of openmano
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
12 # http://www.apache.org/licenses/LICENSE-2.0
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
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact with: nfvlabs@tid.es
24 #This script can be used as a basic test of openvim
26 #WARNING: It destroy the database content
30 echo -e "usage: ${BASH_SOURCE[0]} [OPTIONS] <action>\n Deletes openvim content and add fake hosts, networks"
31 echo -e " <action> is a list of the following items (by default 'reset create')"
32 echo -e " reset reset the openvim database content"
33 echo -e " create creates fake hosts and networks"
34 echo -e " delete delete created items"
35 echo -e " delete-all delete vms. flavors, images, ..."
37 echo -e " -f --force : does not prompt for confirmation"
38 echo -e " -d --delete : same to action delete-all"
39 echo -e " --insert-bashrc insert the created tenant variables at"
40 echo -e " ~/.bashrc to be available by openvim CLI"
41 echo -e " -h --help : shows this help"
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
50 #detect if is called with a source to use the 'exit'/'return' command for exiting
51 [[ ${BASH_SOURCE[0]} != $0 ]] && _exit
="return" || _exit
="exit"
53 #check correct arguments
62 if [[ $argument == reset ]] ||
[[ $argument == create
]] ||
[[ $argument == delete
]] ||
[[ $argument == delete-all
]]
64 action_list
="$action_list $argument"
67 elif [[ ${argument:0:1} == "-" ]] && [[ ${argument:1:1} != "-" ]] && [[ ${#argument} -ge 2 ]]
70 while index
=$
((index
+1)) && [[ $index -lt ${#argument} ]]
72 [[ ${argument:$index:1} == h
]] && usage
&& $_exit 0
73 [[ ${argument:$index:1} == f
]] && force
=y
&& continue
74 [[ ${argument:$index:1} == d
]] && action_list
="delete-all $action_list" && continue
75 echo "invalid option '${argument:$index:1}'? Type -h for help" >&2 && $_exit 1
80 [[ $argument == --help ]] && usage
&& $_exit 0
81 [[ $argument == --force ]] && force
=y
&& continue
82 [[ $argument == --delete ]] && action_list
="delete-all $action_list" && continue
83 [[ $argument == --insert-bashrc ]] && insert_bashrc
=y
&& continue
84 echo "invalid argument '$argument'? Type -h for help" >&2 && $_exit 1
87 DIRNAME
=$
(dirname $
(readlink
-f ${BASH_SOURCE[0]}))
88 DIRvim
=$
(dirname $DIRNAME)
89 export OPENVIM_HOST
=localhost
90 export OPENVIM_PORT
=9080
91 [[ $insert_bashrc == y
]] && echo -e "\nexport OPENVIM_HOST=localhost" >> ~
/.bashrc
92 [[ $insert_bashrc == y
]] && echo -e "\nexport OPENVIM_PORT=9080" >> ~
/.bashrc
93 #by default action should be reset and create
94 [[ -z $action_list ]] && action_list
="reset create"
97 for action
in $action_list
99 if [[ $action == "reset" ]]
101 #ask for confirmation if argument is not -f --force
103 [[ $force != y
]] && read -e -p "WARNING: openvim database content will be lost!!! Continue(y/N)" force_
104 [[ $force_ != y
]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
105 echo "deleting deployed vm"
106 ${DIRvim}/openvim vm-delete
-f |
grep -q deleted
&& sleep 10 #give some time to get virtual machines deleted
107 echo "Stopping openvim"
108 $DIRNAME/service-openvim.sh stop
109 echo "Initializing databases"
110 $DIRvim/database_utils
/init_vim_db.sh
-u vim
-p vimpw
111 echo "Starting openvim"
112 $DIRNAME/service-openvim.sh start
114 elif [[ $action == delete-all
]]
116 for t
in `${DIRvim}/openvim tenant-list | awk '/^ *[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} +/{printf("%s:%s",$1,$2)}'`
120 [[ -z $t_id ]] && continue
121 export OPENVIM_TENANT
=${t_id}
122 for what
in vm image flavor port net
124 items
=`${DIRvim}/openvim $what-list | awk '/^ *[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} +/{print $1}'`
127 [[ $force == y
]] && echo deleting openvim
${what}s from tenant
${t_name}
128 [[ $force != y
]] && read -e -p "Delete openvim ${what}s from tenant ${t_name}?(y/N) " force_
129 [[ $force_ != y
]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
133 ${DIRvim}/openvim
$what-delete -f $item ||
! echo "fail" >&2 ||
$_exit 1
137 ${DIRvim}/openvim tenant-delete
-f $t_id ||
! echo "fail" >&2 ||
$_exit 1
140 items
=`${DIRvim}/openvim $what-list | awk '/^ *[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12} +/{print $1}'`
143 [[ $force == y
]] && echo deleting openvim
${what}s
144 [[ $force != y
]] && read -e -p "Delete openvim ${what}s?(y/N) " force_
145 [[ $force_ != y
]] && [[ $force_ != yes ]] && echo "aborted!" && $_exit
149 ${DIRvim}/openvim
$what-delete -f $item ||
! echo "fail" >&2 ||
$_exit 1
155 elif [[ $action == "delete" ]]
157 ${DIRvim}/openvim net-delete
-f default ||
echo "fail"
158 ${DIRvim}/openvim net-delete
-f macvtap
:em1 ||
echo "fail"
159 ${DIRvim}/openvim net-delete
-f shared_bridge_net ||
echo "fail"
160 ${DIRvim}/openvim net-delete
-f data_net ||
echo "fail"
161 ${DIRvim}/openvim host-remove
-f fake-host-0 ||
echo "fail"
162 ${DIRvim}/openvim host-remove
-f fake-host-1 ||
echo "fail"
163 ${DIRvim}/openvim host-remove
-f fake-host-2 ||
echo "fail"
164 ${DIRvim}/openvim host-remove
-f fake-host-3 ||
echo "fail"
165 result
=`openvim tenant-list TEST-admin`
166 vimtenant
=`echo $result |gawk '{print $1}'`
167 #check a valid uuid is obtained
168 is_valid_uuid
$vimtenant ||
! echo "Tenant TEST-admin not found. Already delete?" >&2 ||
$_exit 1
169 export OPENVIM_TENANT
=$vimtenant
170 ${DIRvim}/openvim tenant-delete
-f TEST-admin ||
echo "fail"
173 elif [[ $action == "create" ]]
175 echo "Adding example hosts"
176 ${DIRvim}/openvim host-add
$DIRvim/test
/hosts
/host-example0.json ||
! echo "fail" >&2 ||
$_exit 1
177 ${DIRvim}/openvim host-add
$DIRvim/test
/hosts
/host-example1.json ||
! echo "fail" >&2 ||
$_exit 1
178 ${DIRvim}/openvim host-add
$DIRvim/test
/hosts
/host-example2.json ||
! echo "fail" >&2 ||
$_exit 1
179 ${DIRvim}/openvim host-add
$DIRvim/test
/hosts
/host-example3.json ||
! echo "fail" >&2 ||
$_exit 1
180 echo "Adding example nets"
181 ${DIRvim}/openvim net-create
$DIRvim/test
/networks
/net-example0.yaml ||
! echo "fail" >&2 ||
$_exit 1
182 ${DIRvim}/openvim net-create
$DIRvim/test
/networks
/net-example1.yaml ||
! echo "fail" >&2 ||
$_exit 1
183 ${DIRvim}/openvim net-create
$DIRvim/test
/networks
/net-example2.yaml ||
! echo "fail" >&2 ||
$_exit 1
184 ${DIRvim}/openvim net-create
$DIRvim/test
/networks
/net-example3.yaml ||
! echo "fail" >&2 ||
$_exit 1
186 printf "%-50s" "Creating openvim tenant 'TEST-admin': "
187 result
=`openvim tenant-create '{"tenant": {"name":"TEST-admin", "description":"admin"}}'`
188 vimtenant
=`echo $result |gawk '{print $1}'`
189 #check a valid uuid is obtained
190 ! is_valid_uuid
$vimtenant && echo "FAIL" && echo " $result" && $_exit 1
192 export OPENVIM_TENANT
=$vimtenant
193 [[ $insert_bashrc == y
]] && echo -e "\nexport OPENVIM_TENANT=$vimtenant" >> ~
/.bashrc
196 #echo "Check virtual machines are deployed"
197 #vms_error=`openvim vm-list | grep ERROR | wc -l`
198 #vms=`openvim vm-list | wc -l`
199 #[[ $vms -ne 8 ]] && echo "WARNING: $vms VMs created, must be 8 VMs" >&2 && $_exit 1
200 #[[ $vms_error -gt 0 ]] && echo "WARNING: $vms_error VMs with ERROR" >&2 && $_exit 1
208 #echo "Listing scenarios"
209 #openvim scenario-list
210 #echo "Listing scenario instances"
211 #openvim instance-scenario-list