Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #141 Replace avocado based tests with shell scripts based test #152

Merged
merged 1 commit into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ $(BIN_DIR)/minishift:

.PHONY: test
test: $(BIN_DIR)/minishift
avocado run $(SHOW_LOG) tests/test.py
sh tests/test.sh
18 changes: 10 additions & 8 deletions centos_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,37 @@ if [ -e "jenkins-env" ]; then
source ~/.jenkins-env
fi

# Enable extra packages
yum --enablerepo=extras install -y epel-release

# Get all the deps in
yum -y install \
make \
git \
epel-release \
livecd-tools \
curl \
docker \
parted \
kvm \
qemu-kvm \
libvirt
libvirt \
python2-pip

# Install 'requests' module used by tests/utils/minishift_latest_version.py
pip install requests

# Start Libvirt
sudo systemctl start libvirtd

# Install Avocado
sudo curl https://repos-avocadoproject.rhcloud.com/static/avocado-el.repo -o /etc/yum.repos.d/avocado.repo
sudo yum install -y avocado

# Setup test drivers
curl -L https://github.com/dhiltgen/docker-machine-kvm/releases/download/v0.7.0/docker-machine-driver-kvm > /usr/local/bin/docker-machine-driver-kvm && \
chmod +x /usr/local/bin/docker-machine-driver-kvm

# Prepare ISO for testing
make centos_iso

# Let's test with showing log enabled
SHOW_LOG=--show-job-log make test
# Run tests
make test

# On reaching successfully at this point, upload artifacts
PASS=$(echo $CICO_API_KEY | cut -d'-' -f1-2)
Expand Down
34 changes: 17 additions & 17 deletions scripts/handle-user-data
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mount_partitions() {
mkdir -p /mnt/$PARTNAME/var/lib/minishift
mkdir -p /var/lib/minishift
mount --bind /mnt/$PARTNAME/var/lib/minishift /var/lib/minishift

# Make sure /tmp is on the disk too
rm -rf /mnt/$PARTNAME/tmp || true
mv /tmp /mnt/$PARTNAME/tmp
Expand Down Expand Up @@ -86,7 +86,7 @@ prepare_partions() {
# wait for the partition to actually exist, timeout after about 5 seconds
local timer=0
while [ "$timer" -lt 10 -a ! -b "${UNPARTITIONED_HD}2" ]; do
timer=$((timer + 1))
timer=$((timer + 1))
sleep 0.5
done

Expand All @@ -100,7 +100,7 @@ prepare_partions() {
# wait for the partition to actually exist, timeout after about 5 seconds
timer=0
while [ "$timer" -lt 10 -a ! -b "${UNPARTITIONED_HD}1" ]; do
timer=$((timer + 1))
timer=$((timer + 1))
sleep 0.5
done

Expand All @@ -110,15 +110,15 @@ prepare_partions() {

# Function to extract userdata.tar containing the ssh keys into the home directory /home/docker
handle_user_data() {
# Extract the userdata into docker user home directory
if [ -e "/var/lib/boot2docker/userdata.tar" ]; then
tar xf /var/lib/boot2docker/userdata.tar -C /home/docker/ > /var/log/userdata.log 2>&1
rm -f '/home/docker/boot2docker, please format-me'
chown -R docker:docker /home/docker/.ssh
else
echo "Unable to find userdata.tar"
exit 1
fi
# Extract the userdata into docker user home directory
if [ -e "/var/lib/boot2docker/userdata.tar" ]; then
tar xf /var/lib/boot2docker/userdata.tar -C /home/docker/ > /var/log/userdata.log 2>&1
rm -f '/home/docker/boot2docker, please format-me'
chown -R docker:docker /home/docker/.ssh
else
echo "Unable to find userdata.tar"
exit 1
fi
}

# TODO Need to make sure to have /sbin on the PATH. Is there a better way?
Expand All @@ -131,16 +131,16 @@ echo 'PATH=$PATH:/sbin' >> /home/docker/.bashrc
# userdata.tar. Remember, /home/docker is not persistent
BOOT2DOCKER_DATA=`blkid -o device -l -t LABEL=$LABEL`
if [ -n "$BOOT2DOCKER_DATA" ]; then
mount_partitions
handle_user_data
exit 0
mount_partitions
handle_user_data
exit 0
fi

# Test for our magic string (it means that the disk was made by ./boot2docker init)
HEADER=`dd if=$UNPARTITIONED_HD bs=1 count=${#MAGIC} 2>/dev/null`
if [ "$HEADER" = "$MAGIC" ]; then
# Read /userdata.tar with ssh keys and place it temporarily under /
dd if=/dev/sda of=/userdata.tar bs=1 count=4096 2>/dev/null
# Read /userdata.tar with ssh keys and place it temporarily under /
dd if=/dev/sda of=/userdata.tar bs=1 count=4096 2>/dev/null

prepare_partions
mount_partitions
Expand Down
138 changes: 138 additions & 0 deletions tests/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!/bin/sh

BINARY=build/bin/minishift
ISO=file://$(pwd)/build/minishift-centos7.iso
EXTRA_FLAGS="--show-libmachine-logs"

function print_success_message() {
echo ""
echo " ------------ [ $1 - Passed ]"
echo ""
}

function exit_with_message() {
if [[ "$1" != 0 ]]; then
echo "$2"
exit 1
fi
}

function assert_equal() {
if [ "$1" != "$2" ]; then
echo "Expected '$1' equal to '$2'"
exit 1
fi
}

# http://www.linuxjournal.com/content/validating-ip-address-bash-script
function assert_valid_ip() {
local ip=$1
local valid=1

if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
valid=$?
fi

if [[ "$valid" != 0 ]]; then
echo "IP '$1' is invalid"
exit 1
fi
}

function verify_start_instance() {
$BINARY start --iso-url $ISO $EXTRA_FLAGS
exit_with_message "$?" "Error starting Minishift VM"
output=`$BINARY status`
assert_equal $output "Running"
print_success_message "Starting VM"
}

function verify_stop_instance() {
$BINARY stop
exit_with_message "$?" "Error starting Minishift VM"
output=`$BINARY status`
assert_equal $output "Stopped"
print_success_message "Stopping VM"
}

function verify_swap_space() {
output=`$BINARY ssh -- echo $(free | tail -n 1 | awk '{print $2}')`
if [ "$output" == "0" ]; then
echo "Expected '$output' not equal to 0"
exit 1
fi
print_success_message "Swap space check"
}

function verify_ssh_connection() {
output=`$BINARY ssh -- echo hello`
assert_equal $output "hello"
print_success_message "SSH Connection"
}

function verify_vm_ip() {
output=`$BINARY ip`
assert_valid_ip $output
print_success_message "Getting VM IP"
}

function verify_cifs_installation() {
expected="mount.cifs version: 6.2"
output=`$BINARY ssh -- sudo /sbin/mount.cifs -V`
assert_equal "$output" "$expected"
print_success_message "CIFS installation"
}

function verify_sshfs_installation() {
expected="SSHFS version 2.5"
output=`$BINARY ssh -- sudo sshfs -V`
echo $output | grep "$expected" > /dev/null
if [ "$?" != "0" ]; then
echo "Expected exit status of command '$BINARY ssh -- sudo sshfs -V' to be 0."
exit 1
fi
print_success_message "SSHFS installation"
}

function verify_nfs_installation() {
expected="mount.nfs: (linux nfs-utils 1.3.0)"
output=`$BINARY ssh -- sudo /sbin/mount.nfs -V /need/for/version`
assert_equal "$output" "$expected"
print_success_message "NFS installation"
}

function verify_bind_mount() {
output=`$BINARY ssh 'findmnt | grep "\[/var/lib/" | wc -l'`
assert_equal $output "4"
print_success_message "Bind mount check"
}

function verify_delete() {
$BINARY delete --force
exit_with_message "$?" "Error deleting Minishift VM"
output=`$BINARY status`
if [ "$1" != "$2" ]; then
echo "Expected '$1' equal to '$2'"
exit 1
fi
print_success_message "Deleting VM"
}

# Tests
verify_start_instance
sleep 90
verify_stop_instance
verify_start_instance
verify_swap_space
verify_vm_ip
verify_cifs_installation
verify_sshfs_installation
verify_nfs_installation
verify_bind_mount
verify_delete