Merge "Before deleting the instance, check if the instance exists"
[doctor.git] / tests / run.sh
index bbe093a..56bacca 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash -ex
+#!/bin/bash -e
 ##############################################################################
 # Copyright (c) 2016 NEC Corporation and others.
 #
@@ -8,6 +8,8 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
 
+[[ "${CI_DEBUG:-true}" == "true" ]] && set -x
+
 IMAGE_URL=https://launchpad.net/cirros/trunk/0.3.0/+download/cirros-0.3.0-x86_64-disk.img
 IMAGE_NAME=cirros
 IMAGE_FILE="${IMAGE_NAME}.img"
@@ -17,6 +19,10 @@ VM_FLAVOR=m1.tiny
 ALARM_NAME=doctor_alarm1
 INSPECTOR_PORT=12345
 CONSUMER_PORT=12346
+TEST_USER=demo
+TEST_PW=demo
+TEST_PROJECT=demo
+TEST_ROLE=_member_
 
 SUPPORTED_INSTALLER_TYPES="apex local"
 INSTALLER_TYPE=${INSTALLER_TYPE:-apex}
@@ -82,12 +88,33 @@ register_image() {
                         --file "$IMAGE_FILE"
 }
 
+create_test_user() {
+    openstack user list | grep -q "$TEST_USER" || {
+        openstack user create "$TEST_USER" --password "$TEST_PW"
+    }
+    openstack project list | grep -q "$TEST_PROJECT" || {
+        openstack project create "$TEST_PROJECT"
+    }
+    openstack user role list "$TEST_USER" --project "$TEST_PROJECT" \
+    | grep -q "$TEST_ROLE" || {
+        openstack role add "$TEST_ROLE" --user "$TEST_USER" \
+                           --project "$TEST_PROJECT"
+    }
+}
+
 boot_vm() {
     nova list | grep -q " $VM_NAME " && return 0
-    nova boot --flavor "$VM_FLAVOR" \
-              --image "$IMAGE_NAME" \
-              "$VM_NAME"
-    sleep 1
+    (
+        # test VM done with test user, so can test non-admin
+        export OS_USERNAME="$TEST_USER"
+        export OS_PASSWORD="$TEST_PW"
+        export OS_TENANT_NAME="$TEST_PROJECT"
+        nova boot --flavor "$VM_FLAVOR" \
+                  --image "$IMAGE_NAME" \
+                  "$VM_NAME"
+        sleep 1
+    )
+
 }
 
 create_alarm() {
@@ -170,6 +197,26 @@ calculate_notification_time() {
         awk '{d = $1 - $2; if (d < 1 && d > 0) print d " OK"; else print d " NG"}'
 }
 
+check_host_status_down() {
+    (
+        # Switching to test user
+        export OS_USERNAME="$TEST_USER"
+        export OS_PASSWORD="$TEST_PW"
+        export OS_TENANT_NAME="$TEST_PROJECT"
+
+        host_status_line=$(nova show $VM_NAME | grep "host_status")
+        [[ $? -ne 0 ]] && {
+            echo "ERROR: host_status not configured for owner in Nova policy.json"
+        }
+
+        host_status=$(echo $host_status_line | awk '{print $4}')
+        [[ "$host_status" == "DOWN" ]] && {
+            echo "$VM_NAME showing host_status: $host_status"
+        }
+        echo "ERROR: host_status not reported by: nova show $VM_NAME"
+    )
+}
+
 cleanup() {
     set +e
     echo "cleanup..."
@@ -179,7 +226,7 @@ cleanup() {
 
     python ./nova_force_down.py "$COMPUTE_HOST" --unset
     sleep 1
-    nova delete "$VM_NAME"
+    nova list | grep -q " $VM_NAME " && nova delete "$VM_NAME"
     sleep 1
     alarm_id=$(ceilometer alarm-list | grep " $ALARM_NAME " | awk '{print $2}')
     sleep 1
@@ -188,6 +235,11 @@ cleanup() {
     image_id=$(glance image-list | grep " $IMAGE_NAME " | awk '{print $2}')
     sleep 1
     [ -n "$image_id" ] && glance image-delete "$image_id"
+    openstack role remove "$TEST_ROLE" --user "$TEST_USER" \
+                              --project "$TEST_PROJECT"
+    openstack project delete "$TEST_PROJECT"
+    openstack user delete "$TEST_USER"
+
     #TODO: add host status check via nova admin api
     echo "waiting disabled compute host back to be enabled..."
     sleep 180
@@ -211,6 +263,9 @@ start_monitor
 start_inspector
 start_consumer
 
+echo "creating test user..."
+create_test_user
+
 echo "creating VM and alarm..."
 boot_vm
 create_alarm
@@ -221,6 +276,7 @@ echo "injecting host failure..."
 inject_failure
 sleep 10
 
+check_host_status_down
 calculate_notification_time
 
 echo "done"