Skip to content

Commit 79ccba4

Browse files
authored
Update tests (jasonheecs#2)
* Update testing library * Refactored tests to use new testing library * Add travis * Modify travis to use https for git submodules * Add shellcheck * Remove shellcheck * Add debian apt sources * Add shellcheck * Add travis status image
1 parent 65eb29e commit 79ccba4

File tree

6 files changed

+66
-20
lines changed

6 files changed

+66
-20
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
*.log
33
.DS_Store
44
!/tests/results/.gitkeep
5-
/tests/results/*
5+
/tests/results/*
6+
Vagrantfile

.travis.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
language: generic
2+
sudo: required
3+
matrix:
4+
include:
5+
- dist: trusty
6+
sudo: required
7+
# Use https (public access) instead of git for git-submodules. This modifies only Travis-CI behavior
8+
git:
9+
submodules: false
10+
# use sed to replace the SSH URL with the public URL, then init and update submodules
11+
before_install:
12+
- sed -i 's/[email protected]:/https:\/\/github.com\//' .gitmodules
13+
- git submodule update --init --recursive
14+
script:
15+
- bash -c 'shopt -s globstar; shellcheck ./*.sh; shellcheck ./tests/*.sh'
16+
- bash tests/unit-tests.sh -skip

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Bash setup script for Ubuntu servers
2+
[![Build Status](https://travis-ci.org/jasonheecs/ubuntu-server-setup.svg?branch=master)](https://travis-ci.org/jasonheecs/ubuntu-server-setup)
3+
24
This is a setup script to automate the setup and provisioning of Ubuntu servers. It does the following:
35
* Adds a new user account with sudo access
46
* Adds a public ssh key for the new user account
@@ -40,4 +42,8 @@ cat ~/.ssh/id_rsa.pub
4042
Finally, you will be prompted to specify a [timezone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) for the server. It will be set to 'Asia/Singapore' if you do not specify a value.
4143

4244
# Supported versions
43-
This setup script has been tested against Ubuntu 14.04 and Ubuntu 16.04.
45+
This setup script has been tested against Ubuntu 14.04 and Ubuntu 16.04.
46+
47+
# Running tests
48+
Tests are run against a set of Vagrant VMs. To run the tests, run the following in the project's directory:
49+
`./tests/tests.sh`

tests/tests.sh

100644100755
File mode changed.

tests/unit-tests.sh

+40-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
#!/bin/bash
22

3+
set -e
4+
5+
while getopts "skip" opt; do
6+
case $opt in
7+
s) SKIP_SETUP=true ;;
8+
esac
9+
done
10+
311
function getCurrentDir() {
412
local current_dir="${BASH_SOURCE%/*}"
513
if [[ ! -d "${current_dir}" ]]; then current_dir="$PWD"; fi
614
echo "${current_dir}"
715
}
816

917
current_dir=$(getCurrentDir)
10-
source "${current_dir}/lib/BashScriptTestingLibrary.shl"
18+
# shellcheck source=/dev/null
19+
source "${current_dir}/lib/bunit.shl"
20+
# shellcheck source=/dev/null
1121
source "${current_dir}/../setupLibrary.sh"
1222

1323
test_user_account=testuser
1424
test_account_password="123%pass_321"
1525

26+
# shellcheck disable=SC2034
27+
VERBOSE_MODE="true"
28+
1629
### Unit Tests ###
1730

1831
function testSetup () {
@@ -27,29 +40,35 @@ function testUserAccountCreated() {
2740
}
2841

2942
function testIfUserIsSudo() {
30-
local user_access
31-
user_access="$(sudo -l -U ${test_user_account})"
32-
assertContains "(ALL : ALL) ALL" "${user_access}"
43+
if [[ $SKIP_SETUP != true ]]; then
44+
local user_access
45+
user_access="$(sudo -l -U ${test_user_account})"
46+
assertContains "(ALL : ALL) ALL" "${user_access}"
47+
fi
3348
}
3449

3550
function testAddingOfSSHKey() {
36-
disableSudoPassword "${test_user_account}"
51+
if [[ $SKIP_SETUP != true ]]; then
52+
disableSudoPassword "${test_user_account}"
3753

38-
local dummy_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBGTO0tsVejssuaYR5R3Y/i73SppJAhme1dH7W2c47d4gOqB4izP0+fRLfvbz/tnXFz4iOP/H6eCV05hqUhF+KYRxt9Y8tVMrpDZR2l75o6+xSbUOMu6xN+uVF0T9XzKcxmzTmnV7Na5up3QM3DoSRYX/EP3utr2+zAqpJIfKPLdA74w7g56oYWI9blpnpzxkEd3edVJOivUkpZ4JoenWManvIaSdMTJXMy3MtlQhva+j9CgguyVbUkdzK9KKEuah+pFZvaugtebsU+bllPTB0nlXGIJk98Ie9ZtxuY3nCKneB+KjKiXrAvXUPCI9mWkYS/1rggpFmu3HbXBnWSUdf [email protected]"
39-
addSSHKey "${test_user_account}" "${dummy_key}"
54+
local dummy_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBGTO0tsVejssuaYR5R3Y/i73SppJAhme1dH7W2c47d4gOqB4izP0+fRLfvbz/tnXFz4iOP/H6eCV05hqUhF+KYRxt9Y8tVMrpDZR2l75o6+xSbUOMu6xN+uVF0T9XzKcxmzTmnV7Na5up3QM3DoSRYX/EP3utr2+zAqpJIfKPLdA74w7g56oYWI9blpnpzxkEd3edVJOivUkpZ4JoenWManvIaSdMTJXMy3MtlQhva+j9CgguyVbUkdzK9KKEuah+pFZvaugtebsU+bllPTB0nlXGIJk98Ie9ZtxuY3nCKneB+KjKiXrAvXUPCI9mWkYS/1rggpFmu3HbXBnWSUdf [email protected]"
55+
addSSHKey "${test_user_account}" "${dummy_key}"
4056

41-
local ssh_file
42-
ssh_file="$(sudo cat /home/${test_user_account}/.ssh/authorized_keys)"
43-
assertEquals "${ssh_file}" "${dummy_key}"
57+
local ssh_file
58+
ssh_file="$(sudo cat /home/${test_user_account}/.ssh/authorized_keys)"
59+
assertEquals "${ssh_file}" "${dummy_key}"
60+
fi
4461
}
4562

4663
function testChangeSSHConfig() {
47-
changeSSHConfig
64+
if [[ $SKIP_SETUP != true ]]; then
65+
changeSSHConfig
4866

49-
local ssh_config
50-
ssh_config="$(sudo cat /etc/ssh/sshd_config)"
51-
assertContains "PasswordAuthentication no" "${ssh_config}"
52-
assertContains "PermitRootLogin no" "${ssh_config}"
67+
local ssh_config
68+
ssh_config="$(sudo cat /etc/ssh/sshd_config)"
69+
assertContains "PasswordAuthentication no" "${ssh_config}"
70+
assertContains "PermitRootLogin no" "${ssh_config}"
71+
fi
5372
}
5473

5574
function testUfw() {
@@ -101,8 +120,12 @@ function testTeardown () {
101120
echo "Test Teardown"
102121

103122
deleteTestUser
104-
revertSudoers
105-
revertSSHConfig
123+
124+
if [[ $SKIP_SETUP != true ]]; then
125+
revertSudoers
126+
revertSSHConfig
127+
fi
128+
106129
revertUfw
107130
deleteSwap
108131

0 commit comments

Comments
 (0)