Add post-deploy tests as an Ansible role

Change-Id: Id033dbf8ef697091459c5ee424d765239e907c01
(cherry picked from commit a27effe0c79ac28da7750b375279557270297076)
diff --git a/build.gradle b/build.gradle
index 8bde489..ba94d23 100644
--- a/build.gradle
+++ b/build.gradle
@@ -250,3 +250,56 @@
     args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "cord-single-playbook.yml"
 }
 
+task postDeployTests (type: Exec) {
+    dependsOn copyAnsibleInventory
+
+    println "Using deployment config: $deployConfig"
+    File configFile = new File(deployConfig)
+    def yaml = new Yaml()
+    def config = yaml.load(configFile.newReader())
+
+    executable = "ansible-playbook"
+    args = ["-i", "inventory/single-prod"]
+
+    if ( config.seedServer.user != null && config.seedServer.user != "" ) {
+        args = args << "--user=$config.seedServer.user"
+    }
+
+    def extraVars = []
+    if (config.seedServer) {
+        extraVars = extraVars.p(config.seedServer.extraVars)
+            .p(config.seedServer.password, "ansible_ssh_pass")
+            .p(config.seedServer.sudoPassword, "ansible_sudo_pass")
+            .p(config.seedServer.fabric_ip, "fabric_ip")
+            .p(config.seedServer.management_ip, "management_ip")
+            .p(config.seedServer.management_network, "management_network")
+            .p(config.seedServer.management_iface, "management_iface")
+            .p(config.seedServer.external_ip, "external_ip")
+            .p(config.seedServer.external_network, "external_network")
+            .p(config.seedServer.external_iface, "external_iface")
+            .p(config.seedServer.fabric_ip, "fabric_ip")
+            .p(config.seedServer.fabric_network, "fabric_network")
+            .p(config.seedServer.fabric_iface, "fabric_iface")
+            .p(config.seedServer.domain, "domain")
+            .p(config.seedServer.virtualbox_support, "virtualbox_support")
+            .p(config.seedServer.power_helper_user, "power_helper_user")
+            .p(config.seedServer.power_helper_host, "power_helper_host")
+            .p(config.seedServer.port, "ansible_ssh_port")
+    }
+
+    if (config.otherServers) {
+        extraVars = extraVars.p(config.otherServers.location, "prov_location")
+            .p(config.otherServers.rolesPath, "prov_role_path")
+            .p(config.otherServers.role, "prov_role")
+    }
+
+    if (config.docker) {
+        extraVars = extraVars.p(config.docker.registry, "docker_registry")
+            .p(config.docker.imageVersion, "docker_image_version")
+    }
+
+    def skipTags = [].p(config.seedServer.skipTags)
+
+    args = args.p(skipTags.asParam("skip-tags", ",")).p(extraVars.asParam("extra-vars", " ")) << "cord-post-deploy-playbook.yml"
+}
+
diff --git a/cord-post-deploy-playbook.yml b/cord-post-deploy-playbook.yml
new file mode 100644
index 0000000..c1c2395
--- /dev/null
+++ b/cord-post-deploy-playbook.yml
@@ -0,0 +1,16 @@
+---
+# Tests single node cord-pod XOS configuration
+
+- name: Include vars
+  hosts: head
+  tasks:
+  - include_vars: vars/cord_single_defaults.yml
+  - include_vars: vars/cord.yml
+  - include_vars: vars/example_keystone.yml
+
+- name: Run post-deploy test
+  hosts: head
+  become: no
+  roles:
+    - post-deploy-tests
+
diff --git a/cord-single-playbook.yml b/cord-single-playbook.yml
index ed1f648..48867be 100644
--- a/cord-single-playbook.yml
+++ b/cord-single-playbook.yml
@@ -44,9 +44,9 @@
   roles:
     - xos-vm-install
     - onos-vm-install
-    - { role: test-client-install, when: test_client_install }
     - juju-setup
-    - docker-compose
     - simulate-fabric
+    - { role: test-client-install, when: test_client_install }
+    - docker-compose
     - onos-load-apps
     - xos-start
diff --git a/roles/post-deploy-tests/tasks/main.yml b/roles/post-deploy-tests/tasks/main.yml
new file mode 100644
index 0000000..32b8b63
--- /dev/null
+++ b/roles/post-deploy-tests/tasks/main.yml
@@ -0,0 +1,49 @@
+---
+# post-deploy-tasks/tasks/main.yml
+#
+# Run tests to check that the single-node deployment has worked
+
+- name: Create a sample CORD subscriber
+  command: ansible xos-1 -u ubuntu -m shell \
+    -a "cd ~/service-profile/cord-pod; make cord-subscriber"
+
+- name: Pause 60 seconds (work around bug in synchronizer)
+  pause: seconds=60
+
+- name: Re-run 'make vtn' (work around bug in synchronizer)
+  command: ansible xos-1 -u ubuntu -m shell \
+    -a "cd ~/service-profile/cord-pod; make vtn"
+
+- name: Wait for vSG VM to come up
+  shell: bash -c "source ~/admin-openrc.sh; nova list --all-tenants|grep 'vsg.*ACTIVE' > /dev/null"
+  register: result
+  until: result | success
+  retries: 10
+  delay: 60
+
+- name: Get ID of VM
+  shell: bash -c "source ~/admin-openrc.sh; nova list --all-tenants|grep mysite_vsg|cut -d '|' -f 2"
+  register: nova_id
+
+- name: Get mgmt IP of VM
+  shell: bash -c "source ~/admin-openrc.sh; nova interface-list {{ nova_id.stdout }}|grep -o -m 1 172.27.[[:digit:]]*.[[:digit:]]*"
+  register: mgmt_ip
+
+- name: Wait for Docker container inside VM to come up
+  shell: ssh -o ProxyCommand="ssh -W %h:%p ubuntu@nova-compute-1" ubuntu@{{ mgmt_ip.stdout }} "sudo docker ps|grep vcpe" > /dev/null
+  register: result
+  until: result | success
+  retries: 20
+  delay: 60
+
+- name: Run dhclient inside testclient to get IP address from vSG
+  command: ansible nova-compute-1 -u ubuntu -m shell \
+    -s -a "lxc-attach -n testclient -- dhclient eth0.222.111"
+
+- name: Test external connectivity in test client
+  command: ansible nova-compute-1 -u ubuntu -m shell \
+    -s -a "lxc-attach -n testclient -- ping -c 3 8.8.8.8"
+  register: pingtest
+
+- name: Output from ping test
+  debug: var=pingtest.stdout_lines
\ No newline at end of file
diff --git a/roles/test-client-install/files/test-client-playbook.yml b/roles/test-client-install/files/test-client-playbook.yml
index fa13250..263cbec 100644
--- a/roles/test-client-install/files/test-client-playbook.yml
+++ b/roles/test-client-install/files/test-client-playbook.yml
@@ -23,3 +23,16 @@
     - name: Create testclient
       become: yes
       shell: lxc-ls | grep testclient || lxc-create -t ubuntu -n testclient
+
+    - name: Start testclient
+      become: yes
+      shell: lxc-info -n testclient -s | grep RUNNING || lxc-start -n testclient
+
+    - name: Set up networking inside the testclient for testing sample CORD subscriber
+      become: yes
+      shell: "{{ item }}"
+      with_items:
+      - "lxc-attach -n testclient -- bash -c 'ip link show eth0.222 || ip link add link eth0 name eth0.222 type vlan id 222'"
+      - "lxc-attach -n testclient -- bash -c 'ip link show eth0.222.111 || ip link add link eth0.222 name eth0.222.111 type vlan id 111'"
+      - "lxc-attach -n testclient -- ifconfig eth0.222 up"
+      - "lxc-attach -n testclient -- ifconfig eth0.222.111 up"
diff --git a/scripts/single-node-pod.sh b/scripts/single-node-pod.sh
index 201f768..789a879 100755
--- a/scripts/single-node-pod.sh
+++ b/scripts/single-node-pod.sh
@@ -1,4 +1,4 @@
-#!/usr/bin/env bash
+!/usr/bin/env bash
 
 function cleanup_from_previous_test() {
     echo "## Cleanup ##"
@@ -40,7 +40,6 @@
     git checkout $SETUP_BRANCH
 
     sed -i "s/replaceme/`whoami`/" $INVENTORY
-    cp vars/example_keystone.yml vars/cord_keystone.yml
 
     # Log into the local node once to get host key
     ssh -o StrictHostKeyChecking=no localhost "ls > /dev/null"
@@ -62,90 +61,19 @@
 
 function setup_xos() {
 
-    ssh ubuntu@xos "cd service-profile/cord-pod; make cord-subscriber"
-
     if [[ $EXAMPLESERVICE -eq 1 ]]
     then
       ssh ubuntu@xos "cd service-profile/cord-pod; make exampleservice"
+
+      echo "(Temp workaround for bug in Synchronizer) Pause 60 seconds"
+      sleep 60
+      ssh ubuntu@xos "cd service-profile/cord-pod; make vtn"
     fi
 
-    echo ""
-    echo "(Temp workaround for bug in Synchronizer) Pause 60 seconds"
-    sleep 60
-    ssh ubuntu@xos "cd service-profile/cord-pod; make vtn"
 }
 
-function setup_test_client() {
-
-    # prep moved to roles/test-client-install
-
-    # start the test client
-    echo "starting testclient"
-    ssh ubuntu@nova-compute "sudo lxc-start -n testclient -d"
-
-    i=0
-    until ssh ubuntu@nova-compute "sudo lxc-wait -n testclient -s RUNNING -t 60"
-    do
-      echo "Waited $i minutes for testclient to start"
-    done
-
-    echo "test client started, configuring testclient network"
-
-    # Configure network interface inside of test client with s-tag and c-tag
-    ssh ubuntu@nova-compute "sudo lxc-attach -n testclient -- ip link add link eth0 name eth0.222 type vlan id 222"
-    ssh ubuntu@nova-compute "sudo lxc-attach -n testclient -- ip link add link eth0.222 name eth0.222.111 type vlan id 111"
-    ssh ubuntu@nova-compute "sudo lxc-attach -n testclient -- ifconfig eth0.222 up"
-    ssh ubuntu@nova-compute "sudo lxc-attach -n testclient -- ifconfig eth0.222.111 up"
-}
-
-function run_e2e_test() {
-    source ~/admin-openrc.sh
-
-    echo "*** Wait for vSG VM to come up"
-    i=0
-
-    until nova list --all-tenants|grep 'vsg.*ACTIVE' > /dev/null
-    do
-      sleep 60
-      (( i += 1 ))
-      echo "Waited $i minutes"
-    done
-
-    # get mgmt IP address
-    ID=$( nova list --all-tenants|grep mysite_vsg|awk '{print $2}' )
-    MGMTIP=$( nova interface-list $ID|grep 172.27|awk '{print $8}' )
-
-    echo ""
-    echo "*** ssh into vsg VM, wait for Docker container to come up"
-    i=0
-    until ssh -o ProxyCommand="ssh -W %h:%p ubuntu@nova-compute" ubuntu@$MGMTIP "sudo docker ps|grep vcpe" > /dev/null
-    do
-      sleep 60
-      (( i += 1 ))
-      echo "Waited $i minutes"
-    done
-
-    echo ""
-    echo "*** Run dhclient in test client"
-
-    ssh ubuntu@nova-compute "sudo lxc-attach -n testclient -- dhclient eth0.222.111" > /dev/null
-
-    echo ""
-    echo "*** Routes in test client"
-    ssh ubuntu@nova-compute "sudo lxc-attach -n testclient -- route -n"
-
-    echo ""
-    echo "*** Test external connectivity in test client"
-    ssh ubuntu@nova-compute "sudo lxc-attach -n testclient -- ping -c 3 8.8.8.8"
-
-    echo ""
-    if [ $? -eq 0 ]
-    then
-      echo "*** [PASSED] End-to-end connectivity test"
-    else
-      echo "*** [FAILED] End-to-end connectivity test"
-      exit 1
-    fi
+function run_e2e_test () {
+    ansible-playbook -i $INVENTORY cord-post-deploy-playbook.yml
 }
 
 function run_exampleservice_test () {
@@ -252,11 +180,11 @@
 
 if [[ $RUN_TEST -eq 1 ]]
 then
-  setup_xos
-  setup_test_client
   run_e2e_test
+
   if [[ $EXAMPLESERVICE -eq 1 ]]
   then
+    setup_xos
     run_exampleservice_test
   fi
 fi