Skip to content
Permalink
Browse files

Add SSH Plugin Smoke Tests (#125)

* add ssh plugin tests
* fix until loops
* Add DOCKER_BATS env var check
  • Loading branch information...
patthiel committed May 13, 2019
1 parent 2e47375 commit b32f4584895eaa95ed6f5b26750d78781f1fde6b
@@ -3,22 +3,19 @@ FROM python:2
ENV TOKEN_1 ""
ENV TOKEN_2 ""
ENV TEST_ENVIRONMENT "prod"
ENV PYTHONPATH=.
ENV PATH="/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.local/bin"

RUN apt-get update && apt-get install -y python3 python3-pip bats parallel \
RUN apt-get update && apt-get install -y python3 python3-pip parallel \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /src

# Install BATS testing framework
RUN mkdir bats-core \
RUN git clone --branch v1.1.0 https://github.com/bats-core/bats-core.git \
&& cd bats-core \
&& git clone https://github.com/bats-core/bats-core.git --branch master v1.1.0 --single-branch \
&& cd v1.1.0 \
&& ./install.sh /usr/local

ENV PYTHONPATH=.
ENV PATH="/usr/local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.local/bin"

WORKDIR /src/linode-cli

COPY requirements.txt .
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

# Get an available image and set it as an env variable
if [ -z "$test_image" ]; then
@@ -87,16 +87,24 @@ removeTag() {
}

createLinodeAndWait() {
local test_image=$(linode-cli images list --format id --text --no-header | egrep "linode\/.*" | head -n 1)
local default_plan=$(linode-cli linodes types --text --no-headers --format="id" | xargs | awk '{ print $1 }')
local linode_type=${1:-$default_plan}
local linode_image=${1:-$test_image}
local linode_type=${2:-$default_plan}

run bash -c "LINODE_CLI_TOKEN=$LINODE_CLI_TOKEN linode-cli linodes create --type=$linode_type --region us-east --image=$test_image --root_pass=$random_pass"
assert_success
# $3 is ssh-keys
if [ -n "$3" ]; then
run bash -c "LINODE_CLI_TOKEN=$LINODE_CLI_TOKEN linode-cli linodes create --type=$linode_type --region us-east --image=$linode_image --root_pass=$random_pass --authorized_keys=\"$3\""
assert_success
else
run bash -c "LINODE_CLI_TOKEN=$LINODE_CLI_TOKEN linode-cli linodes create --type=$linode_type --region us-east --image=$linode_image --root_pass=$random_pass"
assert_success
fi

local linode_id=$(LINODE_CLI_TOKEN=$LINODE_CLI_TOKEN linode-cli linodes list --format id --text --no-header | head -n 1)

SECONDS=0
until [ $(LINODE_CLI_TOKEN=$LINODE_CLI_TOKEN linode-cli linodes view $linode_id --format="status" --text --no-headers) = "running" ]; do
until [[ $(LINODE_CLI_TOKEN=$LINODE_CLI_TOKEN linode-cli linodes view $linode_id --format="status" --text --no-headers) = "running" ]]; do
echo 'still provisioning'
sleep 5 # Wait 5 seconds before checking status again, to rate-limit ourselves
if (( $SECONDS > 180 )); then
@@ -107,6 +115,22 @@ createLinodeAndWait() {
done
}

waitForSsh() {
local linode_label=$(linode-cli linodes list --format "label" --text --no-headers)
local linode_ip=$(linode-cli linodes list --format "ipv4" --text --no-headers)

# Wait until SSH is available
SECONDS=0
sshUp=$(nc -z -G 2 "$linode_ip" 22 || echo "port closed")

while [ "$sshUp" == "port closed" ]; do
# Timeout if it takes more than 15 seconds for ssh availability
if (( "$SECONDS" > 15)); then
break
fi
sshUp=$(nc -z -G 2 "$linode_ip" 22 || echo "port closed")
done
}

setToken() {
source $PWD/.env
@@ -95,7 +95,7 @@ teardown() {

if [ "$RUN_LONG_TESTS" = "TRUE" ]; then
SECONDS=0
until [ $(linode-cli linodes view "$linode_id" --format="status" --text --no-headers) = "running" ]; do
until [[ $(linode-cli linodes view "$linode_id" --format="status" --text --no-headers) = "running" ]]; do
echo 'still provisioning'
# Wait 5 seconds before checking status again, to rate-limit ourselves
sleep 5
@@ -90,7 +90,7 @@ teardown() {
local linode_id=$(linode-cli linodes list --format="id" --text --no-headers)

SECONDS=0
until [ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "offline" ]; do
until [[ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "offline" ]]; do
if (( $SECONDS > 180 )); then
echo "Timeout elapsed! Linode did not initialize in time"
assert_failure # This will fail the test
@@ -39,7 +39,7 @@ teardown() {

@test "it should create a linode and boot" {
# Wait For Linode to be Running
until [ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "running" ]; do
until [[ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "running" ]]; do
echo 'still provisioning'
sleep 5 # Rate limit ourselves
if (( $SECONDS > 180 )); then
@@ -52,7 +52,7 @@ teardown() {

@test "it should reboot the linode" {
# Wait For Linode to be Running
until [ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "running" ]; do
until [[ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "running" ]]; do
echo "still provisioning"
sleep 5 # Rate limit ourselves
if (( $SECONDS > 180 )); then
@@ -69,7 +69,7 @@ teardown() {

# Wait For Linode to NOT be running
SECONDS=0
until [ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) != "running" ]; do
until [[ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) != "running" ]]; do
sleep 5 # Rate limit ourselves
if (( $SECONDS > 180 )); then
echo "Timeout elapsed! Linode did not reboot in time"
@@ -82,7 +82,7 @@ teardown() {
@test "it should shutdown the linode" {
LAST_TEST="TRUE"
# Wait For Linode to be Running
until [ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "running" ]; do
until [[ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "running" ]]; do
echo 'still provisioning'
sleep 5 # Rate limit ourselves
if (( $SECONDS > 180 )); then
@@ -97,7 +97,7 @@ teardown() {

# Wait For Linode to be offline
SECONDS=0
until [ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "offline" ]; do
until [[ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "offline" ]]; do
echo 'still shutting down'
sleep 5 # Rate limit ourselves
if (( $SECONDS > 180 )); then
@@ -17,39 +17,38 @@ setup() {

teardown() {
unset timestamp
run removeLinodes

if [ "$LAST_TEST" = "TRUE" ]; then
run removeLinodes
clearToken "$suiteName"
fi
}

@test "it should fail to rebuild without providing the image" {
run createLinodeAndWait
linode_id=$(linode-cli linodes list --format id --text --no-header | head -n 1)
rebuild_image=$(linode-cli images list --text --no-headers --format id | sed -n 3p)

run linode-cli linodes rebuild \
--image=$rebuild_image \
--root_pass=$random_pass \
$linode_id \
--text \
--no-headers

assert_failure
assert_output --partial "linode-cli: error: too few arguments"
assert_output --partial "Request failed: 400"
assert_output --partial "You must specify an image"
}

@test "it should fail to rebuild with an invalid image" {
run createLinodeAndWait

linode_id=$(linode-cli linodes list --format id --text --no-header | head -n 1)
rebuild_image="bad/image"

run linode-cli linodes rebuild \
--image=$rebuild_image \
--root_pass=$random_pass \
$linode_id \
--text \
--no-headers \
$linode_id
--no-headers

assert_failure
assert_output --partial "Request failed: 400"
@@ -60,8 +59,6 @@ teardown() {
LAST_TEST="TRUE"

if [ $RUN_LONG_TESTS = "TRUE" ]; then
run createLinodeAndWait

linode_id=$(linode-cli linodes list --format id --text --no-header | head -n 1)
rebuild_image=$(linode-cli images list --text --no-headers --format id | sed -n 3p)

@@ -26,7 +26,7 @@ teardown() {

@test "it should fail to resize to the same plan" {
local plan=$(linode-cli linodes types --format="id" --text --no-headers | sed -n 2p)
run createLinodeAndWait $plan
run createLinodeAndWait $test_image $plan
linode_id=$(linode-cli linodes list --format id --text --no-header | head -n 1)
linode_plan=$(linode-cli linodes view $linode_id --format="type" --text --no-headers)

@@ -46,7 +46,7 @@ teardown() {
smaller_plan=$(linode-cli linodes types --format="id" --text --no-headers | sed -n 1p)
local plan=$(linode-cli linodes types --format="id" --text --no-headers | sed -n 2p)

run createLinodeAndWait $plan
run createLinodeAndWait $test_image $plan
linode_id=$(linode-cli linodes list --format id --text --no-header | head -n 1)

run linode-cli linodes resize \
@@ -62,7 +62,7 @@ teardown() {

@test "it should fail to resize to an invalid plan" {
local plan=$(linode-cli linodes types --format="id" --text --no-headers | sed -n 2p)
run createLinodeAndWait $plan
run createLinodeAndWait $test_image $plan
linode_id=$(linode-cli linodes list --format id --text --no-header | head -n 1)
invalid_plan="g15-bad-plan"

@@ -91,7 +91,7 @@ teardown() {

# Wait for status = "Resizing"
SECONDS=0
until [ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "resizing" ]; do
until [[ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "resizing" ]]; do
echo 'waiting for resize to start'
sleep 5
if (( $SECONDS > 180 )); then
@@ -103,7 +103,7 @@ teardown() {
# Wait for offline status.
# Linodes that are resized do not boot automatically
SECONDS=0
until [ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "offline" ]; do
until [[ $(linode-cli linodes view $linode_id --format="status" --text --no-headers) = "offline" ]]; do
echo 'still resizing'

# Check for resizing completion every 15 seconds
@@ -0,0 +1,53 @@
#!/usr/bin/env bats

load '../test_helper/bats-support/load'
load '../test_helper/bats-assert/load'
load '../common'

# ##################################################################
# # WARNING: THIS TEST WILL DELETE ALL OF YOUR LINODES #
# # WARNING: USE A SEPARATE TEST ACCOUNT WHEN RUNNING THESE TESTS #
# ##################################################################

setup() {
suiteName="distro-and-connection-check"
setToken "$suiteName"
# linode_label="sshTestLinode"
}

teardown() {
if [[ "$LAST_TEST" = "TRUE" ]]; then
run removeLinodes
fi

if [ "$LAST_TEST" = "TRUE" ]; then
clearToken "$suiteName"
fi
}

@test "it should create a linode and wait for it to be running" {
alpine_image=$(linode-cli images list --format "id" --text --no-headers | grep 'alpine')
plan=$(linode-cli linodes types --text --no-headers --format="id" | xargs | awk '{ print $1 }')
ssh_key="$(cat ~/.ssh/id_rsa.pub)"
createLinodeAndWait "$alpine_image" "$plan" "$ssh_key"
assert_success
}

@test "it should confirm the distro image is the distro selected in the CLI" {
# Replace this with polling for ssh port open
sleep 25

linode_label=$(linode-cli linodes list --format "label" --text --no-headers)
run linode-cli ssh "root@$linode_label" -oStrictHostKeyChecking=no "cat /etc/os-release"
assert_success
assert_output --partial "Alpine Linux"
}


@test "it should check the vm for ipv4 connectivity" {
LAST_TEST="TRUE"
linode_label=$(linode-cli linodes list --format "label" --text --no-headers)
run linode-cli ssh "root@$linode_label" -oStrictHostKeyChecking=no "ping -4 -W60 -c3 google.com"
assert_success
assert_output --partial "0% packet loss"
}
@@ -0,0 +1,68 @@
#!/usr/bin/env bats

load '../test_helper/bats-support/load'
load '../test_helper/bats-assert/load'
load '../common'

# ##################################################################
# # WARNING: THIS TEST WILL DELETE ALL OF YOUR LINODES #
# # WARNING: USE A SEPARATE TEST ACCOUNT WHEN RUNNING THESE TESTS #
# ##################################################################

setup() {
suiteName="ssh"
setToken "$suiteName"
# linode_label="sshTestLinode"
}

teardown() {
if [[ "$LAST_TEST" = "TRUE" ]]; then
run removeLinodes
fi

if [ "$LAST_TEST" = "TRUE" ]; then
clearToken "$suiteName"
fi
}

@test "it should display the ssh plugin usage information" {
run linode-cli ssh -h

assert_success
assert_output --partial "usage: linode-cli ssh [-h] [-6] [[USERNAME@]LABEL]"
assert_output --partial "positional arguments:"
assert_output --partial "[USERNAME@]LABEL The label of the Linode to SSH into, optionally with a"
assert_output --partial "username before it in USERNAME@LABEL format. If no"
assert_output --partial "username is given, defaults to the current user."
assert_output --partial "optional arguments:"
assert_output --partial "-h, --help show this help message and exit"
assert_output --partial "-6 If given, uses the Linode's SLAAC address for SSH."
}

@test "it should create a linode and wait for it to be running" {
alpine_image=$(linode-cli images list --format "id" --text --no-headers | grep 'alpine')
plan=$(linode-cli linodes types --text --no-headers --format="id" | xargs | awk '{ print $1 }')
ssh_key="$(cat ~/.ssh/id_rsa.pub)"
createLinodeAndWait "$alpine_image" "$plan" "$ssh_key"
assert_success
}

@test "it should fail to ssh into a linode that does not match any existing linodes" {
run linode-cli ssh root@aasdkjlf
assert_failure
assert_output "No Linode found for label aasdkjlf"
}

@test "it should successfully ssh into a linode and get the kernel version" {
LAST_TEST="TRUE"
## Figure out a better way to get IP of a linode label
linode_label=$(linode-cli linodes list --format "label" --text --no-headers)

# Replace this with polling for ssh port open
sleep 25

run linode-cli ssh "root@$linode_label" -oStrictHostKeyChecking=no uname -r
assert_success
# Assert the kernel version matching this regex:
assert_output --regexp "[0-9]\.[0-9]*\.[0-9]*-.*-virt"
}
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

# Trap ctrl-c and reset .env file
trap ctrl_c INT
@@ -45,7 +45,7 @@ fi

# If .env file does not exist, attempt to generate one from global env vars
# Only relevant when running via docker
if [ -z "$DOCKER_HOST" ]; then
if [[ $DOCKER_BATS = "TRUE" ]]; then
echo -e "[DEFAULT]\ntoken = ${TOKEN_1}\ndefault-user = test-user\n\n[test-user]" > /root/.linode-cli \
&& echo -e "export TOKEN_1=$TOKEN_1\nexport TOKEN_2=$TOKEN_2\nexport TOKEN_1_IN_USE_BY=NONE\nexport TOKEN_2_IN_USE_BY=NONE\nexport TEST_ENVIRONMENT=$TEST_ENVIRONMENT" > /src/linode-cli/test/.env
fi

0 comments on commit b32f458

Please sign in to comment.
You can’t perform that action at this time.