refactor some commmon function 29/18929/4
authordongwenjuan <dong.wenjuan@zte.com.cn>
Thu, 18 Aug 2016 08:20:44 +0000 (16:20 +0800)
committerRyota Mibu <r-mibu@cq.jp.nec.com>
Sat, 20 Aug 2016 13:41:15 +0000 (13:41 +0000)
get installer info and get key to access cloud can be picked
up as common function for prepare env before test

Change-Id: I1a96c4ad12f3f36dec0291c2f458e97e85f3ca27
Signed-off-by: dongwenjuan <dong.wenjuan@zte.com.cn>
tests/run.sh

index 965f978..0852a67 100755 (executable)
@@ -46,6 +46,46 @@ if [[ ! "$SUPPORTED_INSPECTOR_TYPES" =~ "$INSPECTOR_TYPE" ]] ; then
     exit 1
 fi
 
+get_installer_ip() {
+    if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
+        if [[ "$INSTALLER_IP" == "none" ]] ; then
+            instack_mac=$(sudo virsh domiflist instack | awk '/default/{print $5}')
+            INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
+        fi
+    elif [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
+        if [[ "$INSTALLER_IP" == "none" ]] ; then
+            instack_mac=$(sudo virsh domiflist fuel-opnfv | awk '/pxebr/{print $5}')
+            INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
+        fi
+    fi
+
+    if [[ "$INSTALLER_TYPE" -ne "local" ]] ; then
+        if [[ -z "$INSTALLER_IP" ]] ; then
+            echo "ERROR: no installer ip"
+            exit 1
+        fi
+    fi
+}
+
+prepare_ssh_to_cloud() {
+    ssh_opts_cpu="$ssh_opts"
+
+    # get ssh key from installer node
+    if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
+        sudo scp $ssh_opts root@"$INSTALLER_IP":/home/stack/.ssh/id_rsa instack_key
+        sudo chown $(whoami):$(whoami) instack_key
+        chmod 400 instack_key
+        ssh_opts_cpu+=" -i instack_key"
+    elif [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
+        sshpass -p r00tme scp $ssh_opts root@${INSTALLER_IP}:.ssh/id_rsa instack_key
+        sudo chown $(whoami):$(whoami) instack_key
+        chmod 400 instack_key
+        ssh_opts_cpu+=" -i instack_key"
+    elif [[ "$INSTALLER_TYPE" == "local" ]] ; then
+        echo "INSTALLER_TYPE set to 'local'. Assuming SSH keys already exchanged with $COMPUTE_HOST"
+    fi
+}
+
 get_compute_host_info() {
     # get computer host info which VM boot in
     COMPUTE_HOST=$(openstack $as_doctor_user server show $VM_NAME |
@@ -58,20 +98,12 @@ get_compute_host_info() {
 
     if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
         COMPUTE_USER=${COMPUTE_USER:-heat-admin}
-        if [[ "$INSTALLER_IP" == "none" ]] ; then
-            instack_mac=$(sudo virsh domiflist instack | awk '/default/{print $5}')
-            INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
-        fi
         COMPUTE_IP=$(sudo ssh $ssh_opts $INSTALLER_IP \
              "source stackrc; \
              nova show $compute_host_in_undercloud \
              | awk '/ ctlplane network /{print \$5}'")
     elif [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
         COMPUTE_USER=${COMPUTE_USER:-root}
-        if [[ "$INSTALLER_IP" == "none" ]] ; then
-            instack_mac=$(sudo virsh domiflist fuel-opnfv | awk '/pxebr/{print $5}')
-            INSTALLER_IP=$(/usr/sbin/arp -e | grep ${instack_mac} | awk '{print $1}')
-        fi
         node_id=$(echo $compute_host_in_undercloud | cut -d "-" -f 2)
         COMPUTE_IP=$(sshpass -p r00tme ssh 2>/dev/null $ssh_opts root@${INSTALLER_IP} \
              "fuel node|awk -F '|' -v id=$node_id '{if (\$1 == id) print \$5}' |xargs")
@@ -93,25 +125,6 @@ get_compute_host_info() {
         echo "ERROR: can not ping to computer host"
         exit 1
     fi
-}
-
-prepare_compute_ssh() {
-    ssh_opts_cpu="$ssh_opts"
-
-    # get ssh key from installer node
-    if [[ "$INSTALLER_TYPE" == "apex" ]] ; then
-        sudo scp $ssh_opts root@"$INSTALLER_IP":/home/stack/.ssh/id_rsa instack_key
-        sudo chown $(whoami):$(whoami) instack_key
-        chmod 400 instack_key
-        ssh_opts_cpu+=" -i instack_key"
-    elif [[ "$INSTALLER_TYPE" == "fuel" ]] ; then
-        sshpass -p r00tme scp $ssh_opts root@${INSTALLER_IP}:.ssh/id_rsa instack_key
-        sudo chown $(whoami):$(whoami) instack_key
-        chmod 400 instack_key
-        ssh_opts_cpu+=" -i instack_key"
-    elif [[ "$INSTALLER_TYPE" == "local" ]] ; then
-        echo "INSTALLER_TYPE set to 'local'. Assuming SSH keys already exchanged with $COMPUTE_HOST"
-    fi
 
     # verify ssh to target compute host
     ssh $ssh_opts_cpu "$COMPUTE_USER@$COMPUTE_IP" 'exit'
@@ -412,6 +425,10 @@ echo "Note: doctor/tests/run.sh has been executed."
 
 trap cleanup EXIT
 
+echo "preparing test env..."
+get_installer_ip
+prepare_ssh_to_cloud
+
 echo "preparing VM image..."
 download_image
 register_image
@@ -424,9 +441,8 @@ boot_vm
 wait_for_vm_launch
 openstack $as_doctor_user server show $VM_NAME
 
-echo "get computer host info and prepare to ssh..."
+echo "get computer host info..."
 get_compute_host_info
-prepare_compute_ssh
 
 echo "creating alarm..."
 get_consumer_ip