diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6515370 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.swp +test/gpg/* diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..707f261 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM concourse/git-resource as resource + +RUN mv /opt/resource /opt/git-resource + +ADD assets/ /opt/resource/ +RUN chmod +x /opt/resource/* + + +FROM resource AS tests +ADD test/ /tests +RUN /tests/all.sh \ No newline at end of file diff --git a/README.md b/README.md index d4b1f1d..d7442a1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,35 @@ # gate-resource + A generic quality-gate resource for Concourse CI + +## Development + +### Prerequisites + +* docker is *required* - version 17.06.x is tested; earlier versions may also + work. + +### Running the tests + +The tests have been embedded with the `Dockerfile`; ensuring that the testing +environment is consistent across any `docker` enabled platform. When the docker +image builds, the test are run inside the docker container, on failure they +will stop the build. + +Run the tests with the following command: + +```sh +docker build -t gate-resource . +``` + +#### Note about the integration tests + +If you want to run the integration tests, a bit more work is required. You will require +an actual git repo to which you can push and pull, configured for SSH access. To do this, +add two files to `integration-tests/ssh` (note that names **are** important): + +* `test_key`: This is the private key used to authenticate against your repo. +* `test_repo`: This file contains one line of the form `test_repo_url[#test_branch]`. + If the branch is not specified, it defaults to `master`. For example, + `git@github.com:concourse-git-tester/git-resource-integration-tests.git` or + `git@github.com:concourse-git-tester/git-resource-integration-tests.git#testing` \ No newline at end of file diff --git a/assets/check b/assets/check new file mode 100755 index 0000000..598745c --- /dev/null +++ b/assets/check @@ -0,0 +1,17 @@ +#!/bin/bash +# vim: set ft=sh + +set -e + +exec 3>&1 # make stdout available as fd 3 for the result +exec 1>&2 # redirect all output to stderr for logging + +payload=$TMPDIR/gate-resource-request +cat > $payload <&0 + +# extract git configuration and pass it to git resource +git_source=$(cat "$payload" | jq -r .source.git) +git_payload=$(echo '{ "source": '$git_source' }' | jq -r) + +# forward to git-resource +echo "$git_payload" | /opt/git-resource/check $@ \ No newline at end of file diff --git a/payload b/payload new file mode 100644 index 0000000..06ae971 --- /dev/null +++ b/payload @@ -0,0 +1,8 @@ +{ + "source": { + "git": { + "uri": "/tmp/git-tests.okbgNh/git-tests.PKjJDj/repo.njodkK" + } + } + } + diff --git a/scripts/check b/scripts/check new file mode 100644 index 0000000..60eec7a --- /dev/null +++ b/scripts/check @@ -0,0 +1,2 @@ +#! /usr/bin/env node + \ No newline at end of file diff --git a/test/all.sh b/test/all.sh new file mode 100755 index 0000000..35f4656 --- /dev/null +++ b/test/all.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +set -e + +# $(dirname $0)/image.sh +$(dirname $0)/check.sh +# $(dirname $0)/get.sh +# $(dirname $0)/put.sh + +echo -e '\e[32mall tests passed!\e[0m' diff --git a/test/check.sh b/test/check.sh new file mode 100755 index 0000000..4c5831b --- /dev/null +++ b/test/check.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +set -e + +source $(dirname $0)/helpers.sh + +it_can_check_from_head() { + local repo=$(init_repo) + local ref=$(make_commit $repo) + + check_uri $repo | jq -e " + . == [{ref: $(echo $ref | jq -R .)}] + " +} + +run it_can_check_from_head \ No newline at end of file diff --git a/test/helpers.sh b/test/helpers.sh new file mode 100644 index 0000000..ed397e9 --- /dev/null +++ b/test/helpers.sh @@ -0,0 +1,103 @@ +#!/bin/bash + +set -e -u + +set -o pipefail + +export TMPDIR_ROOT=$(mktemp -d /tmp/git-tests.XXXXXX) +trap "rm -rf $TMPDIR_ROOT" EXIT + +if [ -d /opt/resource ]; then + resource_dir=/opt/resource +else + resource_dir=$(cd $(dirname $0)/../assets && pwd) +fi +test_dir=$(cd $(dirname $0) && pwd) +keygrip=276D99F5B65388AF85DF54B16B08EF0A44C617AC +fingerprint=A3E20CD6371D49E244B0730D1CDD25AEB0F5F8EF + +run() { + export TMPDIR=$(mktemp -d ${TMPDIR_ROOT}/git-tests.XXXXXX) + + echo -e 'running \e[33m'"$@"$'\e[0m...' + eval "$@" 2>&1 | sed -e 's/^/ /g' + echo "" +} + +init_repo() { + ( + set -e + + cd $(mktemp -d $TMPDIR/repo.XXXXXX) + + git init -q + + # start with an initial commit + git \ + -c user.name='test' \ + -c user.email='test@example.com' \ + commit -q --allow-empty -m "init" + + # create some bogus branch + git checkout -b bogus + + git \ + -c user.name='test' \ + -c user.email='test@example.com' \ + commit -q --allow-empty -m "commit on other branch" + + # back to master + git checkout master + + # print resulting repo + pwd + ) +} + +check_uri() { + jq -n "{ + source: { + git: { + uri: $(echo $1 | jq -R .) + } + } + }" | ${resource_dir}/check | tee /dev/stderr +} + +make_commit_to_file_on_branch() { + local repo=$1 + local file=$2 + local branch=$3 + local msg=${4-} + + # ensure branch exists + if ! git -C $repo rev-parse --verify $branch >/dev/null; then + git -C $repo branch $branch master + fi + + # switch to branch + git -C $repo checkout -q $branch + + # modify file and commit + echo x >> $repo/$file + git -C $repo add $file + git -C $repo \ + -c user.name='test' \ + -c user.email='test@example.com' \ + commit -q -m "commit $(wc -l $repo/$file) $msg" + + # output resulting sha + git -C $repo rev-parse HEAD +} + +make_commit_to_file() { + make_commit_to_file_on_branch $1 $2 master "${3-}" +} + +make_commit_to_branch() { + make_commit_to_file_on_branch $1 some-file $2 +} + +make_commit() { + make_commit_to_file $1 some-file "${2:-}" +} \ No newline at end of file