From 8d2422274ae948f7412b6960597f5de91f3d8830 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 31 Jul 2021 17:39:46 +0300 Subject: move all tests to test/ --- test/integration/docker/.dockerignore | 5 + test/integration/docker/client/etc/cgitize.toml | 4 + test/integration/docker/client/output/.gitignore | 1 + test/integration/docker/docker-compose.yml | 16 ++ test/integration/docker/server/Dockerfile | 22 +++ test/integration/docker/server/setup_repo.sh | 29 +++ test/integration/docker/ssh/client_key | 8 + test/integration/docker/ssh/client_key.pub | 1 + test/integration/docker/ssh/server_key | 7 + test/integration/docker/ssh/server_key.pub | 1 + test/integration/docker/test.sh | 132 +++++++++++++ test/integration/local/test.sh | 229 +++++++++++++++++++++++ test/test_bitbucket.py | 36 ---- test/test_github.py | 27 --- test/unit/__init__.py | 0 test/unit/test_bitbucket.py | 36 ++++ test/unit/test_github.py | 27 +++ 17 files changed, 518 insertions(+), 63 deletions(-) create mode 100644 test/integration/docker/.dockerignore create mode 100644 test/integration/docker/client/etc/cgitize.toml create mode 100644 test/integration/docker/client/output/.gitignore create mode 100644 test/integration/docker/docker-compose.yml create mode 100644 test/integration/docker/server/Dockerfile create mode 100755 test/integration/docker/server/setup_repo.sh create mode 100644 test/integration/docker/ssh/client_key create mode 100644 test/integration/docker/ssh/client_key.pub create mode 100644 test/integration/docker/ssh/server_key create mode 100644 test/integration/docker/ssh/server_key.pub create mode 100755 test/integration/docker/test.sh create mode 100755 test/integration/local/test.sh delete mode 100644 test/test_bitbucket.py delete mode 100644 test/test_github.py create mode 100644 test/unit/__init__.py create mode 100644 test/unit/test_bitbucket.py create mode 100644 test/unit/test_github.py (limited to 'test') diff --git a/test/integration/docker/.dockerignore b/test/integration/docker/.dockerignore new file mode 100644 index 0000000..c0cb0ce --- /dev/null +++ b/test/integration/docker/.dockerignore @@ -0,0 +1,5 @@ +* + +!/server/** +/server/Dockerfile +!/ssh/** diff --git a/test/integration/docker/client/etc/cgitize.toml b/test/integration/docker/client/etc/cgitize.toml new file mode 100644 index 0000000..4e8fda7 --- /dev/null +++ b/test/integration/docker/client/etc/cgitize.toml @@ -0,0 +1,4 @@ +[repositories.test_repo] + +name = "test_repo" +clone_url = "root@server:~/test_repo" diff --git a/test/integration/docker/client/output/.gitignore b/test/integration/docker/client/output/.gitignore new file mode 100644 index 0000000..355164c --- /dev/null +++ b/test/integration/docker/client/output/.gitignore @@ -0,0 +1 @@ +*/ diff --git a/test/integration/docker/docker-compose.yml b/test/integration/docker/docker-compose.yml new file mode 100644 index 0000000..c04bfb3 --- /dev/null +++ b/test/integration/docker/docker-compose.yml @@ -0,0 +1,16 @@ +version: '3' +services: + server: + build: + context: . + dockerfile: server/Dockerfile + image: server + client: + build: + context: ../../.. + dockerfile: Dockerfile + image: client + volumes: + - ./client/etc/:/etc/cgitize:ro + - "$SSH_AUTH_SOCK:/var/run/cgitize/ssh-agent.sock" + - ./client/output:/var/tmp/cgitize/output diff --git a/test/integration/docker/server/Dockerfile b/test/integration/docker/server/Dockerfile new file mode 100644 index 0000000..a6283d0 --- /dev/null +++ b/test/integration/docker/server/Dockerfile @@ -0,0 +1,22 @@ +FROM alpine:3.11 + +RUN apk --no-cache add bash git openssh-server && \ + echo 'root:root' | chpasswd && \ + git config --global user.name 'John Doe' && \ + git config --global user.email 'John.Doe@example.com' && \ + sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config + +WORKDIR /root + +COPY ["ssh/server_key", "/etc/ssh/ssh_host_ed25519_key"] +COPY ["ssh/server_key.pub", "/etc/ssh/ssh_host_ed25519_key.pub"] +COPY ["ssh/client_key.pub", "./.ssh/authorized_keys"] + +RUN chmod 0600 -- /etc/ssh/ssh_host_ed25519_key && \ + chmod 0700 -- ./.ssh/ && \ + chmod 0600 -- ./.ssh/authorized_keys + +COPY ["server/setup_repo.sh", "./"] +RUN ./setup_repo.sh + +CMD ["/usr/sbin/sshd", "-D"] diff --git a/test/integration/docker/server/setup_repo.sh b/test/integration/docker/server/setup_repo.sh new file mode 100755 index 0000000..82ae711 --- /dev/null +++ b/test/integration/docker/server/setup_repo.sh @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +set -o errexit -o nounset -o pipefail + +readonly local_repo_path="$HOME/test_repo" + +setup_local_repo() { + echo + echo ---------------------------------------------------------------------- + echo Setting up upstream repository + echo ---------------------------------------------------------------------- + + mkdir -p -- "$local_repo_path" + pushd -- "$local_repo_path" > /dev/null + git init + echo '1' > 1.txt + git add . + git commit -m 'first commit' + echo '2' > 2.txt + git add . + git commit -m 'second commit' + popd > /dev/null +} + +main() { + setup_local_repo +} + +main diff --git a/test/integration/docker/ssh/client_key b/test/integration/docker/ssh/client_key new file mode 100644 index 0000000..7502c82 --- /dev/null +++ b/test/integration/docker/ssh/client_key @@ -0,0 +1,8 @@ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAACmFlczI1Ni1jdHIAAAAGYmNyeXB0AAAAGAAAABC0VWjnPC +6+rVnkkogRW2onAAAAZAAAAAEAAAAzAAAAC3NzaC1lZDI1NTE5AAAAIDkpIMideN05ni89 +cmRjdbff6fj4qyukThD2X/gINlVMAAAAkOVOKvBjvIbIoc9JA2q8ui62PqP6dQwwsR94Vr +/czMBQah5jFKdH9vDiI2JLaGjoH4VvT9mzuPBoE0Vchk6MEpud2dNXWMdb2ubG1d9yTWfV +XhMXMeNd6UEjY7ypsldhR2X55dvIFeNr9B6A8dMy1Qpeyd62maBOwNdumbsG6MalXOFuv7 +srvnjlwUdHKZV9wg== +-----END OPENSSH PRIVATE KEY----- diff --git a/test/integration/docker/ssh/client_key.pub b/test/integration/docker/ssh/client_key.pub new file mode 100644 index 0000000..6b0e3e9 --- /dev/null +++ b/test/integration/docker/ssh/client_key.pub @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDkpIMideN05ni89cmRjdbff6fj4qyukThD2X/gINlVM root@client diff --git a/test/integration/docker/ssh/server_key b/test/integration/docker/ssh/server_key new file mode 100644 index 0000000..07774eb --- /dev/null +++ b/test/integration/docker/ssh/server_key @@ -0,0 +1,7 @@ +-----BEGIN OPENSSH PRIVATE KEY----- +b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW +QyNTUxOQAAACCXhJs2H7dvamDdK51mhWEAGuXnBsCVEKdWxyPjWYNgigAAAJDx95CI8feQ +iAAAAAtzc2gtZWQyNTUxOQAAACCXhJs2H7dvamDdK51mhWEAGuXnBsCVEKdWxyPjWYNgig +AAAEBkubMLhAF/0hCKnq2l3BuexD4UIQ+qcXW5ke+DWhQ8DJeEmzYft29qYN0rnWaFYQAa +5ecGwJUQp1bHI+NZg2CKAAAAC3Jvb3RAc2VydmVyAQI= +-----END OPENSSH PRIVATE KEY----- diff --git a/test/integration/docker/ssh/server_key.pub b/test/integration/docker/ssh/server_key.pub new file mode 100644 index 0000000..601277f --- /dev/null +++ b/test/integration/docker/ssh/server_key.pub @@ -0,0 +1 @@ +ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJeEmzYft29qYN0rnWaFYQAa5ecGwJUQp1bHI+NZg2CK root@server diff --git a/test/integration/docker/test.sh b/test/integration/docker/test.sh new file mode 100755 index 0000000..7c76298 --- /dev/null +++ b/test/integration/docker/test.sh @@ -0,0 +1,132 @@ +#!/usr/bin/env bash + +set -o errexit -o nounset -o pipefail + +script_dir="$( dirname -- "${BASH_SOURCE[0]}" )" +script_dir="$( cd -- "$script_dir" && pwd )" +readonly script_dir +script_name="$( basename -- "${BASH_SOURCE[0]}" )" +readonly script_name + +dump() { + local prefix="${FUNCNAME[0]}" + [ "${#FUNCNAME[@]}" -gt 1 ] && prefix="${FUNCNAME[1]}" + + local msg + for msg; do + echo "$script_name: $prefix: $msg" + done +} + +kill_ssh_agent() { + [ -n "${SSH_AGENT_PID:+x}" ] || return 0 + dump "killing ssh-agent with PID $SSH_AGENT_PID" + kill "$SSH_AGENT_PID" +} + +spawn_ssh_agent() { + [ -n "${SSH_AGENT_PID:+x}" ] && return 0 + if ! command -v ssh-agent > /dev/null 2>&1; then + dump "could not find ssh-agent" >&2 + return 1 + fi + local output + output="$( ssh-agent -s )" + eval "$output" + if [ -z "${SSH_AGENT_PID:+x}" ]; then + dump "could not start ssh-agent" >&2 + return 1 + fi + trap kill_ssh_agent EXIT +} + +setup_ssh_agent() { + echo + echo ---------------------------------------------------------------------- + echo Setting up ssh-agent + echo ---------------------------------------------------------------------- + + spawn_ssh_agent + + local key='ssh/client_key' + chmod 0600 -- "$key" + local password='password' + + local askpass_path + askpass_path="$( mktemp --tmpdir="$script_dir" )" + + local askpass_rm + askpass_rm="$( printf -- 'rm -- %q; trap - RETURN' "$askpass_path" )" + trap "$askpass_rm" RETURN + + chmod 0700 -- "$askpass_path" + + local echo_password + echo_password="$( printf -- 'echo %q' "$password" )" + echo "$echo_password" > "$askpass_path" + + SSH_ASKPASS="$askpass_path" SSH_ASKPASS_REQUIRE=force DISPLAY= ssh-add "$key" > /dev/null 2>&1 < /dev/null +} + +docker_build() { + echo + echo ---------------------------------------------------------------------- + echo Building Docker images + echo ---------------------------------------------------------------------- + + docker-compose build +} + +setup() { + setup_ssh_agent + docker_build +} + +run_server() { + echo + echo ---------------------------------------------------------------------- + echo Running the server + echo ---------------------------------------------------------------------- + + docker-compose up -d server +} + +run_client() { + echo + echo ---------------------------------------------------------------------- + echo Running the client + echo ---------------------------------------------------------------------- + + if [ -z "${SSH_AUTH_SOCK:+x}" ]; then + dump 'SSH_AUTH_SOCK is not defined' >&2 + return 1 + fi + dump "SSH_AUTH_SOCK: $SSH_AUTH_SOCK" + docker-compose run --rm client +} + +run() { + run_server + run_client +} + +verify() { + echo + echo ---------------------------------------------------------------------- + echo Checking the pulled repository + echo ---------------------------------------------------------------------- + + pushd -- "$script_dir/client/output/test_repo" > /dev/null + git log --oneline + popd > /dev/null +} + +main() { + pushd -- "$script_dir" > /dev/null + setup + run + verify + popd > /dev/null +} + +main diff --git a/test/integration/local/test.sh b/test/integration/local/test.sh new file mode 100755 index 0000000..088539b --- /dev/null +++ b/test/integration/local/test.sh @@ -0,0 +1,229 @@ +#!/usr/bin/env bash + +set -o errexit -o nounset -o pipefail + +script_dir="$( dirname -- "${BASH_SOURCE[0]}" )" +script_dir="$( cd -- "$script_dir" && pwd )" +readonly script_dir + +upstream_repo_dir= +readonly etc_dir="$script_dir/etc" +readonly cgitize_toml_path="$etc_dir/cgitize.toml" +readonly output_dir="$script_dir/output" + +cleanup() { + echo + echo ---------------------------------------------------------------------- + echo Cleaning up + echo ---------------------------------------------------------------------- + + echo "Removing upstream repository directory: $upstream_repo_dir" + rm -rf -- "$upstream_repo_dir" + echo "Removing etc directory: $etc_dir" + rm -rf -- "$etc_dir" + echo "Removing output directory: $output_dir" + rm -rf -- "$output_dir" +} + +setup_upstream_repo() { + echo + echo ---------------------------------------------------------------------- + echo Setting up upstream repository + echo ---------------------------------------------------------------------- + + upstream_repo_dir="$( mktemp -d )" + pushd -- "$upstream_repo_dir" > /dev/null + + git init + echo '1' > 1.txt + git add . + git commit -m 'first commit' + echo '2' > 2.txt + git add . + git commit -m 'second commit' + + popd > /dev/null +} + +add_commits() { + echo + echo ---------------------------------------------------------------------- + echo Adding new commits + echo ---------------------------------------------------------------------- + + pushd -- "$upstream_repo_dir" > /dev/null + + echo '3' > 3.txt + git add . + git commit -m 'third commit' + + popd > /dev/null +} + +setup_cgitize_toml() { + echo + echo ---------------------------------------------------------------------- + echo cgitize.toml + echo ---------------------------------------------------------------------- + + local conf_dir + conf_dir="$( dirname -- "$cgitize_toml_path" )" + mkdir -p -- "$conf_dir" + + cat <&2 + return 1 + fi + + local test_string="$1" + shift + + local pattern + for pattern; do + # Be careful to _not_ use grep -q, since this fucks stuff up: + # https://mywiki.wooledge.org/BashPitfalls#pipefail. + if ! echo "$test_string" | grep --fixed-strings -- "$pattern" > /dev/null; then + echo "${FUNCNAME[0]}: couldn't find the following pattern: $pattern" >&2 + return 1 + fi + done +} + +verify_commits() { + # This is fucking stupid, but otherwise stuff like `if verify_commits;` + # doesn't work: https://stackoverflow.com/q/4072984/514684 + # TODO: figure this out? + pushd -- "$output_dir" > /dev/null && + cd -- test_repo && + local output && + output="$( git log --oneline )" && + echo "$output" && + check_contains "$output" "$@" && + popd > /dev/null +} + +verify_initial_commits() { + echo + echo ---------------------------------------------------------------------- + echo Checking the initial commits + echo ---------------------------------------------------------------------- + + verify_commits 'first commit' 'second commit' +} + +verify_added_commits() { + echo + echo ---------------------------------------------------------------------- + echo Checking the added commits + echo ---------------------------------------------------------------------- + + verify_commits 'first commit' 'second commit' 'third commit' +} + +test_bare() { + echo + echo ====================================================================== + echo "${FUNCNAME[0]}" + echo ====================================================================== + + setup_bare + cgitize + verify_initial_commits + add_commits + cgitize + verify_added_commits + cleanup +} + +test_workdir() { + echo + echo ====================================================================== + echo "${FUNCNAME[0]}" + echo ====================================================================== + + setup_workdir + cgitize + verify_initial_commits + add_commits + cgitize + verify_added_commits + cleanup +} + +test_failure() { + echo + echo ====================================================================== + echo "${FUNCNAME[0]}" + echo ====================================================================== + + setup_bare + cgitize + verify_initial_commits + add_commits + + echo + echo ---------------------------------------------------------------------- + echo Removing upstream repository + echo ---------------------------------------------------------------------- + rm -rf -- "$upstream_repo_dir" + + if cgitize; then + echo "cgitize should have failed to pull the upstream repository." >&2 + return 1 + fi + verify_initial_commits + if verify_added_commits; then + echo "The added commits should not have been pulled." >&2 + return 1 + fi + cleanup +} + +main() { + trap cleanup EXIT + test_bare + test_workdir + test_failure +} + +main diff --git a/test/test_bitbucket.py b/test/test_bitbucket.py deleted file mode 100644 index 7a070ce..0000000 --- a/test/test_bitbucket.py +++ /dev/null @@ -1,36 +0,0 @@ -# Copyright (c) 2021 Egor Tensin -# This file is part of the "cgitize" project. -# For details, see https://github.com/egor-tensin/cgitize. -# Distributed under the MIT License. - -import unittest - -from atlassian.bitbucket.cloud import Cloud -from requests.exceptions import HTTPError - - -class BitbucketTests(unittest.TestCase): - def setUp(self): - self.bitbucket = Cloud(cloud=True) - - def test_nonexistent_repo(self): - with self.assertRaises(HTTPError): - self.bitbucket.repositories.get('doesnot/exist') - - def test_existing_repo(self): - r = self.bitbucket.repositories.get('egor-tensin/cgitize-test-repository') - self.assertEqual(r['name'], 'cgitize-test-repository') - self.assertEqual(r['description'], 'Test cgitize repository') - - self.assertEqual(r['owner']['display_name'], 'Egor Tensin') - self.assertEqual(r['owner']['nickname'], 'egor-tensin') - - self.assertEqual(r['links']['html']['href'], 'https://bitbucket.org/egor-tensin/cgitize-test-repository') - - clone_urls = [link for link in r['links']['clone'] if link['name'] == 'https'] - self.assertEqual(len(clone_urls), 1) - self.assertEqual(clone_urls[0]['href'], 'https://bitbucket.org/egor-tensin/cgitize-test-repository.git') - - ssh_urls = [link for link in r['links']['clone'] if link['name'] == 'ssh'] - self.assertEqual(len(ssh_urls), 1) - self.assertEqual(ssh_urls[0]['href'], 'git@bitbucket.org:egor-tensin/cgitize-test-repository.git') diff --git a/test/test_github.py b/test/test_github.py deleted file mode 100644 index be1daf6..0000000 --- a/test/test_github.py +++ /dev/null @@ -1,27 +0,0 @@ -# Copyright (c) 2021 Egor Tensin -# This file is part of the "cgitize" project. -# For details, see https://github.com/egor-tensin/cgitize. -# Distributed under the MIT License. - -import unittest - -from github import Github, GithubException - - -class GitHubTests(unittest.TestCase): - def setUp(self): - self.github = Github() - - def test_nonexistent_repo(self): - with self.assertRaises(GithubException): - self.github.get_repo('doesnot/exist') - - def test_existing_repo(self): - r = self.github.get_repo('egor-tensin/cgitize-test-repository') - self.assertEqual(r.name, 'cgitize-test-repository') - self.assertEqual(r.description, 'Test cgitize repository') - self.assertEqual(r.owner.name, 'Egor Tensin') - self.assertEqual(r.owner.login, 'egor-tensin') - self.assertEqual(r.html_url, 'https://github.com/egor-tensin/cgitize-test-repository') - self.assertEqual(r.clone_url, 'https://github.com/egor-tensin/cgitize-test-repository.git') - self.assertEqual(r.ssh_url, 'git@github.com:egor-tensin/cgitize-test-repository.git') diff --git a/test/unit/__init__.py b/test/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/unit/test_bitbucket.py b/test/unit/test_bitbucket.py new file mode 100644 index 0000000..7a070ce --- /dev/null +++ b/test/unit/test_bitbucket.py @@ -0,0 +1,36 @@ +# Copyright (c) 2021 Egor Tensin +# This file is part of the "cgitize" project. +# For details, see https://github.com/egor-tensin/cgitize. +# Distributed under the MIT License. + +import unittest + +from atlassian.bitbucket.cloud import Cloud +from requests.exceptions import HTTPError + + +class BitbucketTests(unittest.TestCase): + def setUp(self): + self.bitbucket = Cloud(cloud=True) + + def test_nonexistent_repo(self): + with self.assertRaises(HTTPError): + self.bitbucket.repositories.get('doesnot/exist') + + def test_existing_repo(self): + r = self.bitbucket.repositories.get('egor-tensin/cgitize-test-repository') + self.assertEqual(r['name'], 'cgitize-test-repository') + self.assertEqual(r['description'], 'Test cgitize repository') + + self.assertEqual(r['owner']['display_name'], 'Egor Tensin') + self.assertEqual(r['owner']['nickname'], 'egor-tensin') + + self.assertEqual(r['links']['html']['href'], 'https://bitbucket.org/egor-tensin/cgitize-test-repository') + + clone_urls = [link for link in r['links']['clone'] if link['name'] == 'https'] + self.assertEqual(len(clone_urls), 1) + self.assertEqual(clone_urls[0]['href'], 'https://bitbucket.org/egor-tensin/cgitize-test-repository.git') + + ssh_urls = [link for link in r['links']['clone'] if link['name'] == 'ssh'] + self.assertEqual(len(ssh_urls), 1) + self.assertEqual(ssh_urls[0]['href'], 'git@bitbucket.org:egor-tensin/cgitize-test-repository.git') diff --git a/test/unit/test_github.py b/test/unit/test_github.py new file mode 100644 index 0000000..be1daf6 --- /dev/null +++ b/test/unit/test_github.py @@ -0,0 +1,27 @@ +# Copyright (c) 2021 Egor Tensin +# This file is part of the "cgitize" project. +# For details, see https://github.com/egor-tensin/cgitize. +# Distributed under the MIT License. + +import unittest + +from github import Github, GithubException + + +class GitHubTests(unittest.TestCase): + def setUp(self): + self.github = Github() + + def test_nonexistent_repo(self): + with self.assertRaises(GithubException): + self.github.get_repo('doesnot/exist') + + def test_existing_repo(self): + r = self.github.get_repo('egor-tensin/cgitize-test-repository') + self.assertEqual(r.name, 'cgitize-test-repository') + self.assertEqual(r.description, 'Test cgitize repository') + self.assertEqual(r.owner.name, 'Egor Tensin') + self.assertEqual(r.owner.login, 'egor-tensin') + self.assertEqual(r.html_url, 'https://github.com/egor-tensin/cgitize-test-repository') + self.assertEqual(r.clone_url, 'https://github.com/egor-tensin/cgitize-test-repository.git') + self.assertEqual(r.ssh_url, 'git@github.com:egor-tensin/cgitize-test-repository.git') -- cgit v1.2.3