Make exercise.sh with quantum work

- added support for quantum-debug command
- added ping and ssh method for quantum

Change-Id: Iebf8a0e9e2ed2bb56bee6533e69827e6caa2bc82
diff --git a/exercises/boot_from_volume.sh b/exercises/boot_from_volume.sh
index 460b50c..4c7890b 100755
--- a/exercises/boot_from_volume.sh
+++ b/exercises/boot_from_volume.sh
@@ -32,6 +32,12 @@
 # Import configuration
 source $TOP_DIR/openrc
 
+# Import quantum functions if needed
+if is_service_enabled quantum; then
+    source $TOP_DIR/lib/quantum
+    setup_quantum
+fi
+
 # Import exercise configuration
 source $TOP_DIR/exerciserc
 
@@ -168,6 +174,10 @@
 # Delete a secgroup
 nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
 
+if is_service_enabled quantum; then
+    teardown_quantum
+fi
+
 set +o xtrace
 echo "*********************************************************************"
 echo "SUCCESS: End DevStack Exercise: $0"
diff --git a/exercises/euca.sh b/exercises/euca.sh
index b121493..29141ec 100755
--- a/exercises/euca.sh
+++ b/exercises/euca.sh
@@ -33,6 +33,12 @@
 # Import EC2 configuration
 source $TOP_DIR/eucarc
 
+# Import quantum functions if needed
+if is_service_enabled quantum; then
+    source $TOP_DIR/lib/quantum
+    setup_quantum
+fi
+
 # Import exercise configuration
 source $TOP_DIR/exerciserc
 
@@ -169,6 +175,10 @@
 # Delete group
 euca-delete-group $SECGROUP || die "Failure deleting security group $SECGROUP"
 
+if is_service_enabled quantum; then
+    teardown_quantum
+fi
+
 set +o xtrace
 echo "*********************************************************************"
 echo "SUCCESS: End DevStack Exercise: $0"
diff --git a/exercises/floating_ips.sh b/exercises/floating_ips.sh
index 6787878..ae5691f 100755
--- a/exercises/floating_ips.sh
+++ b/exercises/floating_ips.sh
@@ -31,6 +31,12 @@
 # Import configuration
 source $TOP_DIR/openrc
 
+# Import quantum functions if needed
+if is_service_enabled quantum; then
+    source $TOP_DIR/lib/quantum
+    setup_quantum
+fi
+
 # Import exercise configuration
 source $TOP_DIR/exerciserc
 
@@ -155,14 +161,16 @@
 # test we can ping our floating ip within ASSOCIATE_TIMEOUT seconds
 ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT
 
-# Allocate an IP from second floating pool
-TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | get_field 1`
-die_if_not_set TEST_FLOATING_IP "Failure creating floating IP in $TEST_FLOATING_POOL"
+if ! is_service_enabled quantum; then
+    # Allocate an IP from second floating pool
+    TEST_FLOATING_IP=`nova floating-ip-create $TEST_FLOATING_POOL | grep $TEST_FLOATING_POOL | get_field 1`
+    die_if_not_set TEST_FLOATING_IP "Failure creating floating IP in $TEST_FLOATING_POOL"
 
-# list floating addresses
-if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep $TEST_FLOATING_POOL | grep -q $TEST_FLOATING_IP; do sleep 1; done"; then
-    echo "Floating IP not allocated"
-    exit 1
+    # list floating addresses
+    if ! timeout $ASSOCIATE_TIMEOUT sh -c "while ! nova floating-ip-list | grep $TEST_FLOATING_POOL | grep -q $TEST_FLOATING_IP; do sleep 1; done"; then
+        echo "Floating IP not allocated"
+        exit 1
+     fi
 fi
 
 # dis-allow icmp traffic (ping)
@@ -171,12 +179,13 @@
 # FIXME (anthony): make xs support security groups
 if [ "$VIRT_DRIVER" != "xenserver" -a "$VIRT_DRIVER" != "openvz" ]; then
     # test we can aren't able to ping our floating ip within ASSOCIATE_TIMEOUT seconds
-    ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT
+    ping_check "$PUBLIC_NETWORK_NAME" $FLOATING_IP $ASSOCIATE_TIMEOUT Fail
 fi
 
-# Delete second floating IP
-nova floating-ip-delete $TEST_FLOATING_IP || die "Failure deleting floating IP $TEST_FLOATING_IP"
-
+if ! is_service_enabled quantum; then
+    # Delete second floating IP
+    nova floating-ip-delete $TEST_FLOATING_IP || die "Failure deleting floating IP $TEST_FLOATING_IP"
+fi
 
 # de-allocate the floating ip
 nova floating-ip-delete $FLOATING_IP || die "Failure deleting floating IP $FLOATING_IP"
@@ -193,6 +202,10 @@
 # Delete a secgroup
 nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
 
+if is_service_enabled quantum; then
+    teardown_quantum
+fi
+
 set +o xtrace
 echo "*********************************************************************"
 echo "SUCCESS: End DevStack Exercise: $0"
diff --git a/exercises/quantum-adv-test.sh b/exercises/quantum-adv-test.sh
index 8f15b63..2ee82ff 100755
--- a/exercises/quantum-adv-test.sh
+++ b/exercises/quantum-adv-test.sh
@@ -52,13 +52,17 @@
 # Import configuration
 source $TOP_DIR/openrc
 
-# Import exercise configuration
-source $TOP_DIR/exerciserc
-
 # If quantum is not enabled we exit with exitcode 55 which mean
 # exercise is skipped.
 is_service_enabled quantum && is_service_enabled q-agt && is_service_enabled q-dhcp || exit 55
 
+# Import quantum fucntions
+source $TOP_DIR/lib/quantum
+setup_quantum
+
+# Import exercise configuration
+source $TOP_DIR/exerciserc
+
 #------------------------------------------------------------------------------
 # Test settings for quantum
 #------------------------------------------------------------------------------
@@ -76,14 +80,14 @@
 DEMO2_NUM_NET=2
 
 PUBLIC_NET1_CIDR="200.0.0.0/24"
-DEMO1_NET1_CIDR="10.1.0.0/24"
-DEMO2_NET1_CIDR="10.2.0.0/24"
-DEMO2_NET2_CIDR="10.2.1.0/24"
+DEMO1_NET1_CIDR="10.10.0.0/24"
+DEMO2_NET1_CIDR="10.20.0.0/24"
+DEMO2_NET2_CIDR="10.20.1.0/24"
 
 PUBLIC_NET1_GATEWAY="200.0.0.1"
-DEMO1_NET1_GATEWAY="10.1.0.1"
-DEMO2_NET1_GATEWAY="10.2.0.1"
-DEMO2_NET2_GATEWAY="10.2.1.1"
+DEMO1_NET1_GATEWAY="10.10.0.1"
+DEMO2_NET1_GATEWAY="10.20.0.1"
+DEMO2_NET2_GATEWAY="10.20.1.1"
 
 PUBLIC_NUM_VM=1
 DEMO1_NUM_VM=1
@@ -188,7 +192,7 @@
 
 function confirm_server_active {
     local VM_UUID=$1
-    if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova  --no_cache show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
+    if ! timeout $ACTIVE_TIMEOUT sh -c "while ! nova show $VM_UUID | grep status | grep -q ACTIVE; do sleep 1; done"; then
     echo "server '$VM_UUID' did not become active!"
     false
 fi
@@ -232,6 +236,7 @@
     source $TOP_DIR/openrc admin admin
     add_tenant demo1 demo1 demo1
     add_tenant demo2 demo2 demo2
+    source $TOP_DIR/openrc demo demo
 }
 
 function delete_tenants_and_users {
@@ -241,6 +246,7 @@
     remove_user demo2
     remove_tenant demo2
     echo "removed all tenants"
+    source $TOP_DIR/openrc demo demo
 }
 
 function create_network {
@@ -256,12 +262,8 @@
     source $TOP_DIR/openrc $TENANT $TENANT
     local NET_ID=$(quantum net-create --tenant_id $TENANT_ID $NET_NAME $EXTRA| grep ' id ' | awk '{print $4}' )
     quantum subnet-create --ip_version 4 --tenant_id $TENANT_ID --gateway $GATEWAY $NET_ID $CIDR
-    #T0DO(nati) comment out until l3-agent is merged
-    #local ROUTER_ID=$($QUANTUM router-create --tenant_id $TENANT_ID $ROUTER_NAME| grep ' id ' | awk '{print $4}' )
-    #for NET_NAME in ${NET_NAMES//,/ };do
-    #    SUBNET_ID=`get_subnet_id $NET_NAME`
-    #    $QUANTUM router-interface-create $NAME --subnet_id $SUBNET_ID
-    #done
+    quantum-debug probe-create $NET_ID
+    source $TOP_DIR/openrc demo demo
 }
 
 function create_networks {
@@ -285,7 +287,7 @@
     done
     #TODO (nati) Add multi-nic test
     #TODO (nati) Add public-net test
-    local VM_UUID=`nova --no_cache boot --flavor $(get_flavor_id m1.tiny) \
+    local VM_UUID=`nova boot --flavor $(get_flavor_id m1.tiny) \
         --image $(get_image_id) \
         $NIC \
         $TENANT-server$NUM | grep ' id ' | cut -d"|" -f3 | sed 's/ //g'`
@@ -301,32 +303,26 @@
    # Test agent connection.  Assumes namespaces are disabled, and
    # that DHCP is in use, but not L3
    local VM_NAME=$1
-   IP=`nova  --no_cache show $VM_NAME | grep 'network' | awk '{print $5}'`
-   if ! timeout $BOOT_TIMEOUT sh -c "while ! ping -c1 -w1 $IP; do sleep 1; done"; then
-        echo "Could not ping $VM_NAME"
-        false
-   fi
+   local NET_NAME=$2
+   IP=`nova show $VM_NAME | grep 'network' | awk '{print $5}'`
+   ping_check $NET_NAME $IP $BOOT_TIMEOUT
 }
 
 function check_vm {
     local TENANT=$1
     local NUM=$2
     local VM_NAME="$TENANT-server$NUM"
+    local NET_NAME=$3
     source $TOP_DIR/openrc $TENANT $TENANT
-    ping_ip $VM_NAME
+    ping_ip $VM_NAME $NET_NAME
     # TODO (nati) test ssh connection
     # TODO (nati) test inter connection between vm
-    # TODO (nati) test namespace dhcp
     # TODO (nati) test dhcp host routes
     # TODO (nati) test multi-nic
-    # TODO (nati) use test-agent
-    # TODO (nati) test L3 forwarding
-    # TODO (nati) test floating ip
-    # TODO (nati) test security group
 }
 
 function check_vms {
-    foreach_tenant_vm 'check_vm ${%TENANT%_NAME} %NUM%'
+    foreach_tenant_vm 'check_vm ${%TENANT%_NAME} %NUM% ${%TENANT%_VM%NUM%_NET}'
 }
 
 function shutdown_vm {
@@ -334,12 +330,12 @@
     local NUM=$2
     source $TOP_DIR/openrc $TENANT $TENANT
     VM_NAME=${TENANT}-server$NUM
-    nova --no_cache delete $VM_NAME
+    nova delete $VM_NAME
 }
 
 function shutdown_vms {
     foreach_tenant_vm 'shutdown_vm ${%TENANT%_NAME} %NUM%'
-    if ! timeout $TERMINATE_TIMEOUT sh -c "while nova --no_cache list | grep -q ACTIVE; do sleep 1; done"; then
+    if ! timeout $TERMINATE_TIMEOUT sh -c "while nova list | grep -q ACTIVE; do sleep 1; done"; then
         echo "Some VMs failed to shutdown"
         false
     fi
@@ -347,17 +343,22 @@
 
 function delete_network {
     local TENANT=$1
+    local NUM=$2
+    local NET_NAME="${TENANT}-net$NUM"
     source $TOP_DIR/openrc admin admin
     local TENANT_ID=$(get_tenant_id $TENANT)
     #TODO(nati) comment out until l3-agent merged
     #for res in port subnet net router;do
-    for res in port subnet net;do
-        quantum ${res}-list -F id -F tenant_id | grep $TENANT_ID | awk '{print $2}' | xargs -I % quantum ${res}-delete %
+    for net_id in `quantum net-list -c id -c name | grep $NET_NAME | awk '{print $2}'`;do
+        delete_probe $net_id
+        quantum subnet-list | grep $net_id | awk '{print $2}' | xargs -I% quantum subnet-delete %
+        quantum net-delete $net_id
     done
+    source $TOP_DIR/openrc demo demo
 }
 
 function delete_networks {
-   foreach_tenant 'delete_network ${%TENANT%_NAME}'
+   foreach_tenant_net 'delete_network ${%TENANT%_NAME} ${%NUM%}'
    #TODO(nati) add secuirty group check after it is implemented
    # source $TOP_DIR/openrc demo1 demo1
    # nova secgroup-delete-rule default icmp -1 -1 0.0.0.0/0
@@ -474,6 +475,7 @@
 }
 
 
+teardown_quantum
 #-------------------------------------------------------------------------------
 # Kick off script.
 #-------------------------------------------------------------------------------
diff --git a/exercises/volumes.sh b/exercises/volumes.sh
index 1c73786..8533993 100755
--- a/exercises/volumes.sh
+++ b/exercises/volumes.sh
@@ -30,6 +30,12 @@
 # Import configuration
 source $TOP_DIR/openrc
 
+# Import quantum functions if needed
+if is_service_enabled quantum; then
+    source $TOP_DIR/lib/quantum
+    setup_quantum
+fi
+
 # Import exercise configuration
 source $TOP_DIR/exerciserc
 
@@ -206,6 +212,10 @@
 # Delete a secgroup
 nova secgroup-delete $SECGROUP || die "Failure deleting security group $SECGROUP"
 
+if is_service_enabled quantum; then
+    teardown_quantum
+fi
+
 set +o xtrace
 echo "*********************************************************************"
 echo "SUCCESS: End DevStack Exercise: $0"
diff --git a/functions b/functions
index dbe9d30..f806b5a 100644
--- a/functions
+++ b/functions
@@ -852,7 +852,11 @@
 # ping check
 # Uses globals ``ENABLED_SERVICES``
 function ping_check() {
-    _ping_check_novanet "$1" $2 $3
+    if is_service_enabled quantum; then
+        _ping_check_quantum  "$1" $2 $3 $4
+        return
+    fi
+    _ping_check_novanet "$1" $2 $3 $4
 }
 
 # ping check for nova
@@ -861,19 +865,39 @@
     local from_net=$1
     local ip=$2
     local boot_timeout=$3
+    local expected=${4:-"True"}
+    local check_command=""
     MULTI_HOST=`trueorfalse False $MULTI_HOST`
     if [[ "$MULTI_HOST" = "True" && "$from_net" = "$PRIVATE_NETWORK_NAME" ]]; then
         sleep $boot_timeout
         return
     fi
-    if ! timeout $boot_timeout sh -c "while ! ping -c1 -w1 $ip; do sleep 1; done"; then
-        echo "Couldn't ping server"
+    if [[ "$expected" = "True" ]]; then
+        check_command="while ! ping -c1 -w1 $ip; do sleep 1; done"
+    else
+        check_command="while ping -c1 -w1 $ip; do sleep 1; done"
+    fi
+    if ! timeout $boot_timeout sh -c "$check_command"; then
+        if [[ "$expected" = "True" ]]; then
+            echo "[Fail] Couldn't ping server"
+        else
+            echo "[Fail] Could ping server"
+        fi
         exit 1
     fi
 }
 
 # ssh check
+
 function ssh_check() {
+    if is_service_enabled quantum; then
+        _ssh_check_quantum  "$1" $2 $3 $4 $5
+        return
+    fi
+    _ssh_check_novanet "$1" $2 $3 $4 $5
+}
+
+function _ssh_check_novanet() {
     local NET_NAME=$1
     local KEY_FILE=$2
     local FLOATING_IP=$3
diff --git a/lib/quantum b/lib/quantum
index f9e1782..ba98b64 100644
--- a/lib/quantum
+++ b/lib/quantum
@@ -5,6 +5,8 @@
 XTRACE=$(set +o | grep xtrace)
 set +o xtrace
 
+export QUANTUM_TEST_CONFIG_FILE=${QUANTUM_TEST_CONFIG_FILE:-"/etc/quantum/debug.ini"}
+
 # Configures keystone integration for quantum service and agents
 function quantum_setup_keystone() {
     local conf_file=$1
@@ -57,5 +59,72 @@
     return 1
 }
 
+function _get_net_id() {
+    quantum --os-tenant-name admin --os-username admin --os-password $ADMIN_PASSWORD net-list | grep $1 | awk '{print $2}'
+}
+
+function _get_probe_cmd_prefix() {
+    local from_net="$1"
+    net_id=`_get_net_id $from_net`
+    probe_id=`quantum-debug --os-tenant-name admin --os-username admin --os-password $ADMIN_PASSWORD probe-list -c id -c network_id | grep $net_id | awk '{print $2}' | head -n 1`
+    echo "sudo ip netns exec qprobe-$probe_id"
+}
+
+function delete_probe() {
+    local from_net="$1"
+    net_id=`_get_net_id $from_net`
+    probe_id=`quantum-debug --os-tenant-name admin --os-username admin --os-password $ADMIN_PASSWORD probe-list -c id -c network_id | grep $net_id | awk '{print $2}'`
+    quantum-debug --os-tenant-name admin --os-username admin probe-delete $probe_id
+}
+
+function _ping_check_quantum() {
+    local from_net=$1
+    local ip=$2
+    local timeout_sec=$3
+    local expected=${4:-"True"}
+    local check_command=""
+    probe_cmd=`_get_probe_cmd_prefix $from_net`
+    if [[ "$expected" = "True" ]]; then
+        check_command="while ! $probe_cmd ping -c1 -w1 $ip; do sleep 1; done"
+    else
+        check_command="while $probe_cmd ping -c1 -w1 $ip; do sleep 1; done"
+    fi
+    if ! timeout $timeout_sec sh -c "$check_command"; then
+        if [[ "$expected" = "True" ]]; then
+            echo "[Fail] Couldn't ping server"
+        else
+            echo "[Fail] Could ping server"
+        fi
+        exit 1
+    fi
+}
+
+# ssh check
+function _ssh_check_quantum() {
+    local from_net=$1
+    local key_file=$2
+    local ip=$3
+    local user=$4
+    local timeout_sec=$5
+    local probe_cmd = ""
+    probe_cmd=`_get_probe_cmd_prefix $from_net`
+    if ! timeout $timeout_sec sh -c "while ! $probe_cmd ssh -o StrictHostKeyChecking=no -i $key_file ${user}@$ip echo success ; do sleep 1; done"; then
+        echo "server didn't become ssh-able!"
+        exit 1
+    fi
+}
+
+function setup_quantum() {
+    public_net_id=`_get_net_id $PUBLIC_NETWORK_NAME`
+    quantum-debug --os-tenant-name admin --os-username admin --os-password $ADMIN_PASSWORD probe-create $public_net_id
+    private_net_id=`_get_net_id $PRIVATE_NETWORK_NAME`
+    quantum-debug --os-tenant-name admin --os-username admin --os-password $ADMIN_PASSWORD probe-create $private_net_id
+}
+
+function teardown_quantum() {
+    delete_probe $PUBLIC_NETWORK_NAME
+    delete_probe $PRIVATE_NETWORK_NAME
+}
+
 # Restore xtrace
 $XTRACE
diff --git a/openrc b/openrc
index 0a6a215..4b6b9b2 100644
--- a/openrc
+++ b/openrc
@@ -73,5 +73,5 @@
 # export KEYSTONECLIENT_DEBUG=1
 # export NOVACLIENT_DEBUG=1
 
-# set qunatum debug command
-export TEST_CONFIG_FILE=/etc/quantum/debug.ini
+# set quantum debug command
+export QUANTUM_TEST_CONFIG_FILE=${QUANTUM_TEST_CONFIG_FILE:-"/etc/quantum/debug.ini"}
diff --git a/stack.sh b/stack.sh
index 59b2167..d15d7e7 100755
--- a/stack.sh
+++ b/stack.sh
@@ -341,6 +341,8 @@
 Q_USE_ROOTWRAP=${Q_USE_ROOTWRAP=:-True}
 # Meta data IP
 Q_META_DATA_IP=${Q_META_DATA_IP:-$HOST_IP}
+# Use quantum-debug command
+Q_USE_DEBUG_COMMAND=${Q_USE_DEBUG_COMMAND:-False}
 
 RYU_DIR=$DEST/ryu
 # Ryu API Host
@@ -1503,6 +1505,15 @@
         iniset $Q_CONF_FILE DEFAULT rabbit_host $RABBIT_HOST
         iniset $Q_CONF_FILE DEFAULT rabbit_password $RABBIT_PASSWORD
     fi
+    if [[ "$Q_USE_DEBUG_COMMAND" == "True" ]]; then
+        Q_DEBUG_CONF_FILE=/etc/quantum/debug.ini
+        cp $QUANTUM_DIR/etc/l3_agent.ini $Q_DEBUG_CONF_FILE
+        iniset $Q_L3_CONF_FILE DEFAULT verbose False
+        iniset $Q_L3_CONF_FILE DEFAULT debug False
+        iniset $Q_L3_CONF_FILE DEFAULT metadata_ip $Q_META_DATA_IP
+        iniset $Q_L3_CONF_FILE DEFAULT use_namespaces $Q_USE_NAMESPACE
+        iniset $Q_L3_CONF_FILE DEFAULT root_helper "sudo"
+    fi
 fi
 
 # Nova