#!/bin/bash
set -ex

_dir="$( cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd)"
_release=$(lsb_release -cs)

sudo apt-get update
sudo apt-get install -y tox python3-dev libssl-dev libffi-dev build-essential

lxc config set core.trust_password password
lxc config set core.https_address [::]

if [[ "${_release}" == "bionic" ]]; then
	# force generation of client certificate to an address that doesn't work (http)
	# generate an openssl certificate and key for the remote not verified test
	mkdir -p $HOME/.config/lxc
	openssl genrsa 1024 > $HOME/.config/lxc/client.key
	chmod 400 $HOME/.config/lxc/client.key

	openssl req -new -x509 -nodes -sha1 -days 365 \
		-key $HOME/.config/lxc/client.key -out $HOME/.config/lxc/client.crt \
		-subj="/C=UK/ST=London/L=London/O=OrgName/OU=Test/CN=example.com"

	# create a default dir storage pool for bionic
	lxc storage create default dir
	lxc profile device add default root disk path=/ pool=default
	echo "Run 18.04 (bionic) integration tests"
else
	echo "Run 16.04 (xenial) integration tests"
fi

# finally run the integration tests
tox -e integration
