From f519793273ad7f172d527cd1967ab050fe5db82f Mon Sep 17 00:00:00 2001 From: Sung Won Cho Date: Tue, 12 Nov 2019 18:22:20 +0800 Subject: [PATCH] Write a smoke test for self-hosting (#310) * Fix release script * Write a smoke testing for self-hosting * Allow to supply tarball path * Clear db --- .gitignore | 2 +- Makefile | 6 +++++ host/smoketest/.gitignore | 1 + host/smoketest/README.md | 1 + host/smoketest/Vagrantfile | 9 ++++++++ host/smoketest/run_test.sh | 42 +++++++++++++++++++++++++++++++++++ host/smoketest/setup.sh | 17 ++++++++++++++ host/smoketest/testsuite.sh | 44 +++++++++++++++++++++++++++++++++++++ scripts/release.sh | 7 +++++- 9 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 host/smoketest/.gitignore create mode 100644 host/smoketest/README.md create mode 100644 host/smoketest/Vagrantfile create mode 100755 host/smoketest/run_test.sh create mode 100755 host/smoketest/setup.sh create mode 100755 host/smoketest/testsuite.sh diff --git a/.gitignore b/.gitignore index cf5cac32..c68ac173 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ /vendor /build -/.vagrant +.vagrant *.log diff --git a/Makefile b/Makefile index 6efc8a98..ac48393b 100644 --- a/Makefile +++ b/Makefile @@ -73,6 +73,12 @@ else endif .PHONY: test-jslib +test-selfhost: + @echo "==> running a smoke test for self-hosting" + + @${currentDir}/host/smoketest/run_test.sh ${tarballPath} +.PHONY: test-jslib + # development dev-server: @echo "==> running dev environment" diff --git a/host/smoketest/.gitignore b/host/smoketest/.gitignore new file mode 100644 index 00000000..463ebfd4 --- /dev/null +++ b/host/smoketest/.gitignore @@ -0,0 +1 @@ +/volume diff --git a/host/smoketest/README.md b/host/smoketest/README.md new file mode 100644 index 00000000..9025f533 --- /dev/null +++ b/host/smoketest/README.md @@ -0,0 +1 @@ +This directory contains a smoke test for running a self-hosted instance using a virtual machine. diff --git a/host/smoketest/Vagrantfile b/host/smoketest/Vagrantfile new file mode 100644 index 00000000..54b28fcf --- /dev/null +++ b/host/smoketest/Vagrantfile @@ -0,0 +1,9 @@ +# -*- mode: ruby -*- + +Vagrant.configure("2") do |config| + config.vm.box = "ubuntu/bionic64" + config.vm.synced_folder './volume', '/vagrant' + config.vm.network "forwarded_port", guest: 2300, host: 2300 + + config.vm.provision 'shell', path: './setup.sh', privileged: false +end diff --git a/host/smoketest/run_test.sh b/host/smoketest/run_test.sh new file mode 100755 index 00000000..8137dfcd --- /dev/null +++ b/host/smoketest/run_test.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +# run_test.sh builds a fresh server image, and mounts it on a fresh +# virtual machine and runs a smoke test. If a tarball path is not provided, +# this script builds a new version and uses it. +set -ex + +# tarballPath is an absolute path to a release tarball containing the dnote server. +tarballPath=$1 + +dir=$(dirname "${BASH_SOURCE[0]}") +projectDir="$dir/../.." + +# build +if [ -z "$tarballPath" ]; then + pushd "$projectDir" + make version=integration_test build-server + popd + tarballPath="$projectDir/build/server/dnote_server_integration_test_linux_amd64.tar.gz" +fi + +pushd "$dir" + +# start a virtual machine +volume="$dir/volume" +rm -rf "$volume" +mkdir -p "$volume" +cp "$tarballPath" "$volume" +cp "$dir/testsuite.sh" "$volume" + +vagrant up + +# run tests +set +e +if ! vagrant ssh -c "/vagrant/testsuite.sh"; then + echo "Test failed. Please see the output." + vagrant halt + exit 1 +fi +set -e + +vagrant halt +popd diff --git a/host/smoketest/setup.sh b/host/smoketest/setup.sh new file mode 100755 index 00000000..3fb44f95 --- /dev/null +++ b/host/smoketest/setup.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +set -ex + +sudo apt-get install wget ca-certificates +wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - +sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list' + +sudo apt-get update +sudo apt-get install -y postgresql-11 + +# set up database +sudo -u postgres createdb dnote +# allow connection from host and allow to connect without password +sudo sed -i "/port*/a listen_addresses = '*'" /etc/postgresql/11/main/postgresql.conf +sudo sed -i 's/host.*all.*.all.*md5/# &/' /etc/postgresql/11/main/pg_hba.conf +sudo sed -i "$ a host all all all trust" /etc/postgresql/11/main/pg_hba.conf +sudo service postgresql restart diff --git a/host/smoketest/testsuite.sh b/host/smoketest/testsuite.sh new file mode 100755 index 00000000..3e8eab4d --- /dev/null +++ b/host/smoketest/testsuite.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash +# testsuite.sh runs the smoke tests for a self-hosted instance. +# It is meant to be run inside a virtual machine which has been +# set up by an entry script. +set -eu + +echo 'Running a smoke test' + +sudo -u postgres dropdb dnote +sudo -u postgres createdb dnote + +cd /vagrant + +tar -xvf dnote_server_integration_test_linux_amd64.tar.gz + +GO_ENV=PRODUCTION \ + DBHost=localhost \ + DBPort=5432 \ + DBName=dnote \ + DBUser=postgres \ + DBPassword="" \ + WebURL=localhost:3000 \ + ./dnote-server -port 2300 start & sleep 3 + +assert_http_status() { + url=$1 + expected=$2 + + echo "======== [TEST CASE] asserting response status code for $url ========" + + got=$(curl --write-out %"{http_code}" --silent --output /dev/null "$url") + + if [ "$got" != "$expected" ]; then + echo "======== ASSERTION FAILED ========" + echo "status code for $url: expected: $expected got: $got" + echo "==================================" + exit 1 + fi +} + +assert_http_status http://localhost:2300 "200" +assert_http_status http://localhost:2300/api/health "200" + +echo "======== [SUCCESS] TEST PASSED! ========" diff --git a/scripts/release.sh b/scripts/release.sh index 5d72455d..06369ee2 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -46,8 +46,13 @@ fi echo "* creating release" set -x + +# first line is the title and the rest are body in markdown +message="$version_tag +Please see the [CHANGELOG](https://github.com/dnote/dnote/blob/master/CHANGELOG.md)" + hub release create \ "${file_flags[@]}" \ "${flags[@]}" \ - --message="$version_tag\nPlease see the [CHANGELOG](https://github.com/dnote/dnote/blob/master/CHANGELOG.md)"\ + --message="$message"\ "$version_tag"