diff --git a/.gitignore b/.gitignore index 1816b4c..622fb21 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.log .DS_Store !/tests/results/.gitkeep -/tests/results/* \ No newline at end of file +/tests/results/* +Vagrantfile \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a117c8f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,16 @@ +language: generic +sudo: required +matrix: + include: + - dist: trusty + sudo: required +# Use https (public access) instead of git for git-submodules. This modifies only Travis-CI behavior +git: + submodules: false +# use sed to replace the SSH URL with the public URL, then init and update submodules +before_install: + - sed -i 's/git@github.com:/https:\/\/github.com\//' .gitmodules + - git submodule update --init --recursive +script: + - bash -c 'shopt -s globstar; shellcheck ./*.sh; shellcheck ./tests/*.sh' + - bash tests/unit-tests.sh -skip \ No newline at end of file diff --git a/README.md b/README.md index 2f4724d..93a4cb1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ # Bash setup script for Ubuntu servers +[![Build Status](https://travis-ci.org/jasonheecs/ubuntu-server-setup.svg?branch=master)](https://travis-ci.org/jasonheecs/ubuntu-server-setup) + This is a setup script to automate the setup and provisioning of Ubuntu servers. It does the following: * Adds a new user account with sudo access * Adds a public ssh key for the new user account @@ -40,4 +42,8 @@ cat ~/.ssh/id_rsa.pub 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. # Supported versions -This setup script has been tested against Ubuntu 14.04 and Ubuntu 16.04. \ No newline at end of file +This setup script has been tested against Ubuntu 14.04 and Ubuntu 16.04. + +# Running tests +Tests are run against a set of Vagrant VMs. To run the tests, run the following in the project's directory: +`./tests/tests.sh` \ No newline at end of file diff --git a/tests/lib b/tests/lib index f4895ec..6ea340d 160000 --- a/tests/lib +++ b/tests/lib @@ -1 +1 @@ -Subproject commit f4895eca3bc72895944c53d45821f7631214629c +Subproject commit 6ea340d35f29ea2fa5a23ea6eca7073f3c6b27d0 diff --git a/tests/tests.sh b/tests/tests.sh old mode 100644 new mode 100755 diff --git a/tests/unit-tests.sh b/tests/unit-tests.sh index 672802b..b34d972 100644 --- a/tests/unit-tests.sh +++ b/tests/unit-tests.sh @@ -1,5 +1,13 @@ #!/bin/bash +set -e + +while getopts "skip" opt; do + case $opt in + s) SKIP_SETUP=true ;; + esac +done + function getCurrentDir() { local current_dir="${BASH_SOURCE%/*}" if [[ ! -d "${current_dir}" ]]; then current_dir="$PWD"; fi @@ -7,12 +15,17 @@ function getCurrentDir() { } current_dir=$(getCurrentDir) -source "${current_dir}/lib/BashScriptTestingLibrary.shl" +# shellcheck source=/dev/null +source "${current_dir}/lib/bunit.shl" +# shellcheck source=/dev/null source "${current_dir}/../setupLibrary.sh" test_user_account=testuser test_account_password="123%pass_321" +# shellcheck disable=SC2034 +VERBOSE_MODE="true" + ### Unit Tests ### function testSetup () { @@ -27,29 +40,35 @@ function testUserAccountCreated() { } function testIfUserIsSudo() { - local user_access - user_access="$(sudo -l -U ${test_user_account})" - assertContains "(ALL : ALL) ALL" "${user_access}" + if [[ $SKIP_SETUP != true ]]; then + local user_access + user_access="$(sudo -l -U ${test_user_account})" + assertContains "(ALL : ALL) ALL" "${user_access}" + fi } function testAddingOfSSHKey() { - disableSudoPassword "${test_user_account}" + if [[ $SKIP_SETUP != true ]]; then + disableSudoPassword "${test_user_account}" - local dummy_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBGTO0tsVejssuaYR5R3Y/i73SppJAhme1dH7W2c47d4gOqB4izP0+fRLfvbz/tnXFz4iOP/H6eCV05hqUhF+KYRxt9Y8tVMrpDZR2l75o6+xSbUOMu6xN+uVF0T9XzKcxmzTmnV7Na5up3QM3DoSRYX/EP3utr2+zAqpJIfKPLdA74w7g56oYWI9blpnpzxkEd3edVJOivUkpZ4JoenWManvIaSdMTJXMy3MtlQhva+j9CgguyVbUkdzK9KKEuah+pFZvaugtebsU+bllPTB0nlXGIJk98Ie9ZtxuY3nCKneB+KjKiXrAvXUPCI9mWkYS/1rggpFmu3HbXBnWSUdf localuser@machine.local" - addSSHKey "${test_user_account}" "${dummy_key}" + local dummy_key="ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBGTO0tsVejssuaYR5R3Y/i73SppJAhme1dH7W2c47d4gOqB4izP0+fRLfvbz/tnXFz4iOP/H6eCV05hqUhF+KYRxt9Y8tVMrpDZR2l75o6+xSbUOMu6xN+uVF0T9XzKcxmzTmnV7Na5up3QM3DoSRYX/EP3utr2+zAqpJIfKPLdA74w7g56oYWI9blpnpzxkEd3edVJOivUkpZ4JoenWManvIaSdMTJXMy3MtlQhva+j9CgguyVbUkdzK9KKEuah+pFZvaugtebsU+bllPTB0nlXGIJk98Ie9ZtxuY3nCKneB+KjKiXrAvXUPCI9mWkYS/1rggpFmu3HbXBnWSUdf localuser@machine.local" + addSSHKey "${test_user_account}" "${dummy_key}" - local ssh_file - ssh_file="$(sudo cat /home/${test_user_account}/.ssh/authorized_keys)" - assertEquals "${ssh_file}" "${dummy_key}" + local ssh_file + ssh_file="$(sudo cat /home/${test_user_account}/.ssh/authorized_keys)" + assertEquals "${ssh_file}" "${dummy_key}" + fi } function testChangeSSHConfig() { - changeSSHConfig + if [[ $SKIP_SETUP != true ]]; then + changeSSHConfig - local ssh_config - ssh_config="$(sudo cat /etc/ssh/sshd_config)" - assertContains "PasswordAuthentication no" "${ssh_config}" - assertContains "PermitRootLogin no" "${ssh_config}" + local ssh_config + ssh_config="$(sudo cat /etc/ssh/sshd_config)" + assertContains "PasswordAuthentication no" "${ssh_config}" + assertContains "PermitRootLogin no" "${ssh_config}" + fi } function testUfw() { @@ -101,8 +120,12 @@ function testTeardown () { echo "Test Teardown" deleteTestUser - revertSudoers - revertSSHConfig + + if [[ $SKIP_SETUP != true ]]; then + revertSudoers + revertSSHConfig + fi + revertUfw deleteSwap