Convert test client to use LXD (LXC and LXD conflict)

Change-Id: I891915ed742f7787ecef500607e18d2087dad915
diff --git a/cord-post-deploy-playbook.yml b/cord-post-deploy-playbook.yml
index 4341ae3..a562c5b 100644
--- a/cord-post-deploy-playbook.yml
+++ b/cord-post-deploy-playbook.yml
@@ -11,10 +11,15 @@
         - vars/cord.yml
         - vars/example_keystone.yml
 
+- name: Create test client
+  hosts: head
+  become: yes
+  roles:
+    - maas-test-client-install
+
 - name: Run post-deploy tests
   hosts: head
   become: no
   roles:
-    - maas-test-client-install
     - test-vsg
     - test-exampleservice
diff --git a/roles/maas-test-client-install/tasks/main.yml b/roles/maas-test-client-install/tasks/main.yml
index e7fbc6a..f1ce154 100644
--- a/roles/maas-test-client-install/tasks/main.yml
+++ b/roles/maas-test-client-install/tasks/main.yml
@@ -1,26 +1,72 @@
 ---
-- name: Install software
-  apt:
-    name={{ item }}
-    update_cache=yes
-    cache_valid_time=3600
-  become: yes
-  register: result
-  until: result | success
-  retries: 15
-  delay: 60
+- name: Enable trusty-backports
+  apt_repository:
+    repo: "{{ item }}"
+    state: present
   with_items:
-   - lxc
-   - lxc-dev
-   - python-dev
+    - "deb http://us.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe"
+    - "deb-src http://us.archive.ubuntu.com/ubuntu/ trusty-backports main restricted universe"
 
-- name: Install PIP packages
-  pip:
-    name={{ item }}
-  become: yes
+- name: Ensure LXD
+  apt:
+    name: lxd
+    state: present
+    update_cache: yes
+    default_release: trusty-backports
+
+- name: get "fabric" bridge interface
+  shell: route -n | grep 10.6.1.0 | awk '{print $8}'
+  register: bridge
+  tags:
+     - skip_ansible_lint # running a sub job
+
+- name: Create testclient LXD profile
+  lxd_profile:
+    name: testclient
+    state: present
+    config:
+      user.user-data: |
+        #cloud-config
+        write_files:
+          - path: /etc/rc.local
+            permissions: "0755"
+            content: |
+              #!/bin/sh -e
+              # Set up VLAN tagging interface ("simulated OLT")
+              ip link add link eth0 name eth0.222 type vlan id 222
+              ip link add link eth0.222 name eth0.222.111 type vlan id 111
+              ifconfig eth0.222 up
+              ifconfig eth0.222.111 up
+              dhclient eth0.222.111
+              exit 0
+        bootcmd:
+          - sed -i 's/eth0 inet dhcp/eth0 inet manual/g' /etc/network/interfaces.d/eth0.cfg
+    description: 'Subscriber test client'
+    devices:
+      eth0:
+        nictype: bridged
+        parent: "{{ bridge.stdout }}"
+        type: nic
+
+- name: Create testclient container
+  lxd_container:
+    name: testclient
+    architecture: x86_64
+    state: started
+    source:
+      type: image
+      mode: pull
+      server: https://cloud-images.ubuntu.com/releases
+      protocol: simplestreams
+      alias: "{{ ansible_distribution_release }}"
+    profiles: ["testclient"]
+    timeout: 600
+
+- name: Stop container
+  lxd_container:
+    name: testclient
+    state: stopped
   register: result
   until: result | success
-  retries: 15
-  delay: 60
-  with_items:
-   - lxc-python2
+  retries: 3
+  delay: 10
diff --git a/roles/test-exampleservice/tasks/main.yml b/roles/test-exampleservice/tasks/main.yml
index 4cca9f5..5d6a6bc 100644
--- a/roles/test-exampleservice/tasks/main.yml
+++ b/roles/test-exampleservice/tasks/main.yml
@@ -60,18 +60,32 @@
   tags:
     - skip_ansible_lint # running a sub job
 
-- name: Install curl in testclient
+- name: start container
   become: yes
-  lxc_container:
+  lxd_container:
     name: testclient
-    container_command: apt-get -y install curl
+    state: started
+
+- name: Make sure testclient has default route to vSG
+  become: yes
+  shell: lxc exec testclient -- route | grep default | grep "eth0.222.111"
+  register: result
+  until: result | success
+  retries: 3
+  delay: 10
+  tags:
+    - skip_ansible_lint # running a sub job
+
+- name: Download the curl package through the vSG
+  become: yes
+  command: lxc exec testclient -- apt-get install -y curl
   tags:
     - skip_ansible_lint # running a sub job
 
 # Don't use lxc_container in order to get output
 - name: Test connectivity to ExampleService from test client
   become: yes
-  command: lxc-attach -n testclient -- curl -s http://{{ public_ip.stdout }}
+  command: lxc exec testclient -- curl -s http://{{ public_ip.stdout }}
   register: curltest
   tags:
     - skip_ansible_lint # running a sub job
diff --git a/roles/test-vsg/tasks/main.yml b/roles/test-vsg/tasks/main.yml
index 4685010..7e02e15 100644
--- a/roles/test-vsg/tasks/main.yml
+++ b/roles/test-vsg/tasks/main.yml
@@ -54,42 +54,25 @@
   tags:
     - skip_ansible_lint # running a sub job
 
-- name: get "fabric" bridge interface
-  shell: route -n | grep 10.6.1.0 | awk '{print $8}'
-  register: bridge
-  tags:
-     - skip_ansible_lint # running a sub job
-
-# Specifying this through container_config below was not working...
-- name: set lxc bridge interface
+- name: start container
   become: yes
-  lineinfile:
-    dest: /etc/lxc/default.conf
-    regexp: "^lxc.network.link ="
-    line: "lxc.network.link = {{ bridge.stdout }}"
-
-- name: Create testclient
-  become: yes
-  lxc_container:
+  lxd_container:
     name: testclient
-    container_log: true
-    template: ubuntu
     state: started
-    template_options: --release trusty
-    container_command: |
-      ifdown eth0
-      sed -i 's/eth0 inet dhcp/eth0 inet manual/g' /etc/network/interfaces
-      ifup eth0
-      ip link show eth0.222 || ip link add link eth0 name eth0.222 type vlan id 222
-      ip link show eth0.222.111 || ip link add link eth0.222 name eth0.222.111 type vlan id 111
-      ifconfig eth0.222 up
-      ifconfig eth0.222.111 up
-      dhclient eth0.222.111
 
-# Don't use lxc_container so that we can get output
+- name: Make sure testclient has default route to vSG
+  become: yes
+  shell: lxc exec testclient -- route | grep default | grep "eth0.222.111"
+  register: result
+  until: result | success
+  retries: 3
+  delay: 10
+  tags:
+    - skip_ansible_lint # running a sub job
+
 - name: Test external connectivity in test client
   become: yes
-  command: lxc-attach -n testclient -- ping -c 3 8.8.8.8
+  command: lxc exec testclient -- ping -c 3 8.8.8.8
   register: pingtest
   tags:
     - skip_ansible_lint # running a sub job