diff --git a/README.md b/README.md index 0fba7df64b563baa34c38e8db3b3365caafd0274..e8112ad511bfd8fa65a709f4b12eb0cf5405f710 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,15 +101,15 @@ 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-attach -n test-runner +sudo lxc exec test-runner -- bash ``` Build and install the CLMC test Python module: ```shell -cd /vagrant/src/test -python setup.py sdist --dist-dir=../../build -pip3 install /vagrant/build/clmctest-<version>.tar.gz +cd /opt/clmc/src/test +python3 setup.py sdist --dist-dir=../../build +pip3 install /opt/clmc/build/clmctest-<version>.tar.gz ``` The following module is unit tests: @@ -122,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 02fc7bb504c593f0da8c3dc4e28c27f4da86525d..529e1dcd3959ce65f9b2967833111cd1cb3139d8 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -21,6 +21,8 @@ $lxc_script = <<-SCRIPT +export DEBIAN_FRONTEND=noninteractive + apt-get update apt-get install python3 python3-setuptools jq -y @@ -29,7 +31,8 @@ echo iptables-persistent iptables-persistent/autosave_v4 boolean true | sudo deb echo iptables-persistent iptables-persistent/autosave_v6 boolean true | sudo debconf-set-selections apt-get -y install iptables-persistent -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 # enable NTP @@ -45,6 +48,7 @@ timedatectl set-ntp no #done apt-get install ntp -y + echo "timesync set to ntpd" # set timezone to London @@ -54,14 +58,29 @@ SCRIPT Vagrant.configure("2") do |config| config.vm.box = "ubuntu/bionic64" - config.disksize.size = '50GB' + config.vm.box_version = '20190627.1.0' + + # 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 = 4 - vb.memory = "8192" + vb.cpus = 2 + #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 dc20be92e0b945cde9fed1c3cec247ebc2645979..821cb71971cdaea64f0d4522160af20e87068552 100755 --- a/scripts/test/fixture.sh +++ b/scripts/test/fixture.sh @@ -28,6 +28,12 @@ repo_root="/vagrant" target_root="/opt/clmc" config_file="rspec.json" +# make sure UID is root +if [ "$EUID" -ne 0 ]; then + echo "please run as root (requires to change iptables)" + exit +fi + usage() { echo "Usage: $0 create|start|stop|destroy [-f config_file] [-r repo_root] [-c container_name|all]" 1>&2 echo " -f defaults to '${config_file}'" 1>&2 @@ -36,6 +42,14 @@ usage() { exit 1 } +failed() { + msg=$1 + sn=$2 + echo "failed to: $msg for service $service_name" + delete $sn + exit 1 +} + create() { service_name=$1 config_file=$2 @@ -47,25 +61,28 @@ create() { echo $SERVICE ip=$(echo $SERVICE | jq -r '.ip_address') - lxc init ubuntu:16.04 ${service_name} + lxc init ubuntu:16.04 ${service_name} || failed "crate lxc image" ${service_name} echo "Creating network: ${service_name}" - lxc network attach lxcbr0 ${service_name} eth0 - lxc config device set ${service_name} eth0 ipv4.address ${ip} - lxc config set ${service_name} security.privileged true + lxc network attach lxcbr0 ${service_name} eth0 || failed "attach lxcbr0" ${service_name} + lxc config device set ${service_name} eth0 ipv4.address ${ip} || failed "config eth0" ${service_name} + lxc config set ${service_name} security.privileged true || failed "config security" ${service_name} # copy flame clmc files into the root container 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 "create target directory" ${service_name} + cp -f ${repo_root}/reporc ${container_repo_root} || failed "copy reporc" ${service_name} + cp -rf ${repo_root}/scripts ${container_repo_root} || failed "copy scripts" ${service_name} + cp -rf ${repo_root}/src ${container_repo_root} || failed "copy src" ${service_name} + chown -R 100000:100000 ${container_repo_root} || failed "change permissions" ${service_name} + # start the container echo "Starting: ${service_name}" lxc start ${service_name} + # add delay to avoid while-loop lxc command failing initially + sleep 1 while :; do echo "Waiting for container to start: ${service_name}" lxc file pull ${service_name}/etc/resolv.conf - | grep -q nameserver && break @@ -82,16 +99,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 +131,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 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 @@ -124,13 +141,14 @@ create() { lxc exec ${service_name} -- service telegraf stop # copy telegraf configuration templates - cp -f ${repo_root}/scripts/clmc-agent/telegraf.conf ${container_dir}/etc/telegraf/ - cp -f ${repo_root}/scripts/clmc-agent/telegraf_output.conf ${container_dir}/etc/telegraf/telegraf.d/ + echo "copy telegraf configuration templates" + lxc file push -r scripts/clmc-agent/telegraf.conf ${service_name}/etc/telegraf/ + lxc file push -r scripts/clmc-agent/telegraf_output.conf ${service_name}/etc/telegraf/telegraf.d/ # copy the 'host' config into all service containers - cp -f ${repo_root}/src/test/clmctest/services/host/telegraf*.conf ${container_dir}/etc/telegraf/telegraf.d/ + lxc file push -r src/test/clmctest/services/host/telegraf*.conf ${service_name}/etc/telegraf/telegraf.d/ # copy the service-specific config - cp -f ${repo_root}/src/test/clmctest/services/${sf_package_id}/telegraf*.conf ${container_dir}/etc/telegraf/telegraf.d/ - chown -R 100000:100000 ${container_dir}/etc/telegraf/ + lxc file push -r src/test/clmctest/services/${sf_package_id}/telegraf*.conf ${service_name}/etc/telegraf/telegraf.d/ + lxc exec ${service_name} -- chown -R 100000:100000 /etc/telegraf/ # replace telegraf template with container parameters cmd="${target_root}/scripts/clmc-agent/configure.sh ${location} ${sfc_id} ${sfc_instance_id} ${sf_package_id} ${sf_id} ${sf_endpoint_id} ${influxdb_url}" @@ -167,10 +185,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() { + 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