Skip to content

Commit

Permalink
feat(check): add check script forwarding to git-resource
Browse files Browse the repository at this point in the history
using test-infra from git-resource
  • Loading branch information
JohannesRudolph committed Oct 4, 2018
1 parent 7e42c5e commit b5bc1fb
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.swp
test/gpg/*
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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,
`[email protected]:concourse-git-tester/git-resource-integration-tests.git` or
`[email protected]:concourse-git-tester/git-resource-integration-tests.git#testing`
17 changes: 17 additions & 0 deletions assets/check
Original file line number Diff line number Diff line change
@@ -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 $@
8 changes: 8 additions & 0 deletions payload
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"source": {
"git": {
"uri": "/tmp/git-tests.okbgNh/git-tests.PKjJDj/repo.njodkK"
}
}
}

2 changes: 2 additions & 0 deletions scripts/check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /usr/bin/env node

10 changes: 10 additions & 0 deletions test/all.sh
Original file line number Diff line number Diff line change
@@ -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'
16 changes: 16 additions & 0 deletions test/check.sh
Original file line number Diff line number Diff line change
@@ -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
103 changes: 103 additions & 0 deletions test/helpers.sh
Original file line number Diff line number Diff line change
@@ -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='[email protected]' \
commit -q --allow-empty -m "init"

# create some bogus branch
git checkout -b bogus

git \
-c user.name='test' \
-c user.email='[email protected]' \
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='[email protected]' \
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:-}"
}

0 comments on commit b5bc1fb

Please sign in to comment.