From 3ea1df43e537db3c2c59d34757a5f9e3f5fa40b2 Mon Sep 17 00:00:00 2001
From: panos <pm@it-innovation.soton.ac.uk>
Date: Mon, 12 Aug 2019 14:36:49 +0100
Subject: [PATCH] update README, VBox, installation (fixture)

---
 README.md               | 24 ++++++++++++----------
 Vagrantfile             | 22 +++++++++++++++++----
 scripts/test/fixture.sh | 44 +++++++++++++++++++++++++++++------------
 3 files changed, 63 insertions(+), 27 deletions(-)

diff --git a/README.md b/README.md
index d30c806..e8112ad 100644
--- a/README.md
+++ b/README.md
@@ -50,6 +50,8 @@ Testing is implemented using pytest using the following convention:
 * Tests are executed from the test-runner container on the VM using install python modules
 * CI testing is on givry using nested LXD containers
 
+##### Create VM
+
 Create a single VM with LXC installed and configured with lxcbr0 configured for the network 172.40.231.0/24
 
 ```shell
@@ -76,18 +78,22 @@ To create|start|stop|destroy specific services use the `-c` option e.g.
 /vagrant/scripts/test/fixture.sh create -f /vagrant/src/test/clmctest/rspec.json -c clmc-service
 ```
 
+##### Create `reporc` file
+
 The installation of several of the services depend on accessing the Nexus binary repository (for the custom Telegraf agent). To do this, a username and password for the repository must be specified in a `reporc` file in the user's home directory, e.g.
 
+
 ```shell
 REPO_USER=itinnov.flame.integration
 REPO_PASS=xxxxxxx
 ```
 
+##### Build all services
+
 Create all the services needed for integration tests:
 
 ```shell
-sudo su
-/vagrant/scripts/test/fixture.sh create -f /vagrant/src/test/clmctest/rspec.json -c all
+sudo /vagrant/scripts/test/fixture.sh create -f /vagrant/src/test/clmctest/rspec.json -c all
 ```
 
 As part of the clmc-service installation, the service's unit tests have been run. The fixture script will fail if any individual service installation fails to install (or fails its tests).
@@ -95,22 +101,20 @@ As part of the clmc-service installation, the service's unit tests have been run
 Attach to the test-runner to run the tests
 
 ```shell
-lxc exec test-runner /bin/bash
+sudo lxc exec test-runner -- bash
 ```
 
 Build and install the CLMC test Python module:
 
 ```shell
 cd /opt/clmc/src/test
-python setup.py sdist --dist-dir=../../build
-apt install python-pip
-pip install /opt/clmc/build/clmctest-<version>.tar.gz
+python3 setup.py sdist --dist-dir=../../build
+pip3 install /opt/clmc/build/clmctest-<version>.tar.gz
 ```
 
 The following module is unit tests:
 
 ```shell
-apt install python3-pytest
 pytest -s --pyargs clmctest.scripts
 ```
 
@@ -124,19 +128,19 @@ pytest -s --pyargs clmctest.monitoring
 #### CI Testing
 
 
-A lxd container is setup on givry called `flame-clmc-ci`. The container is priviledged and allows for nested containers. The container was created using the following commands. 
+A lxd container is setup on givry called `flame-clmc-ci`. The container is priviledged and allows for nested containers. The container was created using the following commands.
 
 Note that the container only works on the `default` storage pool and not the large storage device `pool2`
 
 ```
-lxc launch ubuntu:18.04 flame-clmc-ci -c security.privileged=true -c security.nesting=true 
+lxc launch ubuntu:18.04 flame-clmc-ci -c security.privileged=true -c security.nesting=true
 ```
 
 the container is then started and LXD initialised
 
 ```
 lxc exec flame-clmc-ci -- bash
-lxd init --auto --storage-backend dir 
+lxd init --auto --storage-backend dir
 lxc network create lxcbr0 ipv6.address=none ipv4.address=172.40.231.1/24 ipv4.nat=true
 
 ```
diff --git a/Vagrantfile b/Vagrantfile
index c0eba41..8915c7b 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -59,14 +59,28 @@ SCRIPT
 Vagrant.configure("2") do |config|
   config.vm.box = "ubuntu/bionic64"
   config.vm.box_version = '20190627.1.0'
-  config.disksize.size = '50GB'
+
+  # use public network by allowing the IP to be assigned via DHCP
+  config.vm.network "public_network", use_dhcp_assigned_default_route: true
+
+  # forward ports to host
+  #config.vm.network "forwarded_port", guest: 80, host: 80
+
+  # increase default disk size, it requires  to install the plugin first
+  # e.g. vagrant plugin install vagrant-disksize
+  config.disksize.size = '15GB'
+
+  # forward X11
+  config.ssh.forward_agent = true
+  config.ssh.forward_x11 = true
+
+  # provision the VM 
   config.vm.provider "virtualbox" do |vb|
     vb.cpus = 2
-    vb.memory = "8192"
+    #vb.memory = "8192"
+    vb.memory = "12288"
   end
 
-  #config.vm.network "forwarded_port", guest: 8888, host: 8888
-
   # Install lxc
   config.vm.provision :shell, inline: $lxc_script
 end
diff --git a/scripts/test/fixture.sh b/scripts/test/fixture.sh
index dc20be9..c5e6070 100755
--- a/scripts/test/fixture.sh
+++ b/scripts/test/fixture.sh
@@ -36,6 +36,14 @@ usage() {
     exit 1
 }
 
+failed() {
+    msg=$1
+    sn=$2
+    echo "failed on: $msg"
+    delete_c $sn
+    exit 1
+}
+
 create() {
     service_name=$1
     config_file=$2
@@ -47,7 +55,7 @@ create() {
         echo $SERVICE
         ip=$(echo $SERVICE | jq -r '.ip_address')
 
-        lxc init ubuntu:16.04 ${service_name}
+        lxc init ubuntu:16.04 ${service_name} || return
         echo "Creating network: ${service_name}"
         lxc network attach lxcbr0 ${service_name} eth0
         lxc config device set ${service_name} eth0 ipv4.address ${ip}
@@ -57,11 +65,12 @@ create() {
         echo "Copying files to rootfs"
         container_dir="/var/lib/lxd/containers/"${service_name}"/rootfs"
         container_repo_root=${container_dir}${target_root}
-        mkdir -p ${container_repo_root}       
-        cp -f ${repo_root}/reporc ${container_repo_root}   
-        cp -rf ${repo_root}/scripts ${container_repo_root}     
-        cp -rf ${repo_root}/src ${container_repo_root}
-        chown -R 100000:100000 ${container_repo_root} 
+        mkdir -p ${container_repo_root} || failed "to create target directory" ${service_name}
+        cp -f ${repo_root}/reporc ${container_repo_root} || failed "to copy reporc" ${service_name}
+        cp -rf ${repo_root}/scripts ${container_repo_root} || failed "to copy scripts" ${service_name}
+        cp -rf ${repo_root}/src ${container_repo_root} || failed "to copy src" ${service_name}
+        chown -R 100000:100000 ${container_repo_root} || failed "to change permissions" ${service_name}
+
 
         # start the container
         echo "Starting: ${service_name}"
@@ -82,16 +91,15 @@ create() {
             if [ $exit_code != 0 ]; then
                 echo "clmc-service installation failed with exit code ${exit_code}"
                 exit 1
-            fi            
-        elif [ ${service_name} == "test-runner" ]
-        then
+            fi
+        elif [ ${service_name} == "test-runner" ]; then
             cmd="${target_root}/src/test/clmctest/services/pytest/install.sh"
             lxc exec ${service_name} -- ${cmd}
             exit_code=$?
             if [ $exit_code != 0 ]; then
                 echo "test-runner installation failed with exit code ${exit_code}"
                 exit 1
-            fi            
+            fi
         else
             # get container parameters
             location=$(echo $SERVICE | jq -r '.location')
@@ -115,8 +123,9 @@ create() {
             lxc exec ${service_name} --env REPO_ROOT="${target_root}" -- ${cmd}
 
             # check that telegraf installed (it may not have if the reporc file was not present or Nexus server was down)
-            if lxc-attach -n ${service_name} -- ls /etc/telegraf |& grep 'ls: cannot access'; then
-                echo "Telegraf agent failed to install (check reporc?)"
+            #if lxc-attach -n ${service_name} -- ls /etc/telegraf |& grep 'ls: cannot access'; then
+            if lxc exec ${service_name} -- ls /etc/telegraf |& grep 'ls: cannot access'; then
+                echo "Telegraf agent failed to install for ${service_name} (check reporc?)"
                 exit 1
             fi
 
@@ -167,10 +176,19 @@ stop() {
     service_name=$1
     if lxc info ${service_name}; then
         echo "Stopping container: ${service_name}"
-        lxc stop -n ${service_name}
+        lxc stop ${service_name}
     fi
 }
 
+delete_c() {
+    service_name=$1
+    if lxc list | grep -q ${service_name}; then
+        echo "Deleting container: ${service_name}"
+        lxc delete ${service_name}
+    fi
+}
+
+
 destroy() {
     service_name=$1
     config_file=$2
-- 
GitLab