Skip to content

Commit

Permalink
Add BATS tests
Browse files Browse the repository at this point in the history
  • Loading branch information
burakince committed Dec 1, 2024
1 parent f5dbc66 commit 7503c4c
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 26 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Bats and bats libs
id: setup-bats
uses: bats-core/bats-action@3.0.0

- name: Run Tests
shell: bash
env:
BATS_LIB_PATH: ${{ steps.setup-bats.outputs.lib-path }}
TERM: xterm
run: bats test

- name: Install Cosign
id: install_cosign
uses: sigstore/cosign-installer@v3.7.0
Expand Down
38 changes: 29 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ Helm Chart AppVersion and Chart version updater DroneCI Plugin

## Environment Variables

| Env Variable | Description | Required | Default |
| ----------------- | ------------------ | -------- | ------- |
| PLUGIN_SSH_URL | Repository SSH URL | Yes | |
| PLUGIN_SSH_KEY | SSH Private Key | Yes | |
| PLUGIN_NAME | Git user name | Yes | |
| PLUGIN_EMAIL | Git user email | Yes | |
| PLUGIN_IMAGE_TAG | New image tag | Yes | |
| PLUGIN_CHART_PATH | Helm Chart path | Yes | |
| PLUGIN_DEBUG | Enable debug mode | No | false |
| Env Variable | Description | Required | Default |
| --------------------------- | ------------------------- | -------- | ----------------- |
| PLUGIN_SSH_URL | Repository SSH URL | Yes | |
| PLUGIN_SSH_KEY | SSH Private Key | Yes | |
| PLUGIN_NAME | Git user name | Yes | |
| PLUGIN_EMAIL | Git user email | Yes | |
| PLUGIN_IMAGE_TAG | New image tag | Yes | |
| PLUGIN_CHART_PATH | Helm Chart path | Yes | |
| PLUGIN_DEBUG | Enable debug mode | No | false |
| PLUGIN_TEST | Enable test mode | No | false |
| PLUGIN_SSH_FOLDER | Git SSH Folder | No | ~/.ssh |
| PLUGIN_SSH_PRIVATE_KEY_FILE | SSH PK file name | No | ~/ssh/id_rsa |
| PLUGIN_KNOWN_HOSTS_FILE | SSH Known Hosts file name | No | ~/ssh/known_hosts |

## Image Usage

Expand Down Expand Up @@ -46,3 +50,19 @@ steps:
image_tag: "${DRONE_COMMIT:0:7}"
chart_path: "charts/mychart"
```

# Development

## Running Tests

Pleae install BATS package to your system with the following command.

```bash
brew install bats-core
```

And you can use the following command to run all tests under test folder.

```bash
bats test
```
48 changes: 31 additions & 17 deletions src/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ check_command curl
check_command git

DEBUG_MODE="${PLUGIN_DEBUG:-false}"
TEST_MODE="${PLUGIN_TEST:-false}"

URL="${PLUGIN_SSH_URL:?SSH URL empty or unset}"

SSH_KEY="${PLUGIN_SSH_KEY:?SSH Key empty or unset}"
echo -n "$SSH_KEY" > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
SSH_FOLDER="${PLUGIN_SSH_FOLDER:-~/.ssh}"
SSH_PRIVATE_KEY_FILE="${PLUGIN_SSH_PRIVATE_KEY_FILE:-$SSH_FOLDER/id_rsa}"
SSH_KNOWN_HOSTS_FILE="${PLUGIN_KNOWN_HOSTS_FILE:-$SSH_FOLDER/known_hosts}"

echo -n "$SSH_KEY" > $SSH_PRIVATE_KEY_FILE
chmod 600 $SSH_PRIVATE_KEY_FILE

if [ "$DEBUG_MODE" = true ] ; then
cat ~/.ssh/id_rsa
ls -al ~/.ssh/
cat $SSH_PRIVATE_KEY_FILE
ls -al $SSH_FOLDER
fi

HOSTNAME=""
Expand All @@ -36,12 +41,13 @@ REPOSITORY=""

REGEX="^(https|git)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+)(.git)*$"

if [[ $URL =~ $REGEX ]]; then
if [[ $URL =~ $REGEX ]]; then
protocol=${BASH_REMATCH[1]}
separator=${BASH_REMATCH[2]}
HOSTNAME=${BASH_REMATCH[3]}
USER=${BASH_REMATCH[4]}
REPOSITORY=${BASH_REMATCH[5]}
REPOSITORY="${REPOSITORY%.git}"

if [ "$DEBUG_MODE" = true ] ; then
echo $protocol
Expand All @@ -56,26 +62,30 @@ if [[ $URL =~ $REGEX ]]; then
exit 1
fi
else
echo "SSH URL is invalid!"
echo "SSH URL is invalid! URL is $URL"
exit 1
fi

ssh-keyscan -t rsa $HOSTNAME >> ~/.ssh/known_hosts
if [ "$TEST_MODE" = false ] ; then
ssh-keyscan -t rsa $HOSTNAME >> $SSH_KNOWN_HOSTS_FILE
fi

if [ "$DEBUG_MODE" = true ] ; then
cat ~/.ssh/known_hosts
cat $SSH_KNOWN_HOSTS_FILE
fi

EMAIL="${PLUGIN_EMAIL:?User email empty or unset}"
git config --global user.email "$EMAIL"
NAME="${PLUGIN_NAME:?User name empty or unset}"
git config --global user.name "$NAME"

IMAGE_TAG="${PLUGIN_IMAGE_TAG:?Image tag empty or unset}"
rm -rf $REPOSITORY
git clone git@$HOSTNAME:$USER/$REPOSITORY.git
if [ "$TEST_MODE" = false ] ; then
rm -rf $REPOSITORY
git clone git@$HOSTNAME:$USER/$REPOSITORY.git
fi
cd $REPOSITORY

EMAIL="${PLUGIN_EMAIL:?User email empty or unset}"
git config user.email "$EMAIL"
NAME="${PLUGIN_NAME:?User name empty or unset}"
git config user.name "$NAME"

if [ "$DEBUG_MODE" = true ] ; then
ls -al
fi
Expand All @@ -94,7 +104,7 @@ CHART_VERSION=$(grep '^version:' $CHART_PATH/Chart.yaml | awk '{print $2}')
CHART_VERSION_NEXT="${CHART_VERSION%.*}.$((${CHART_VERSION##*.}+1))"

sed -i'.bak' -e 's|^version:.*|version: '"$CHART_VERSION_NEXT"'|g' $CHART_PATH/Chart.yaml
sed -i'.bak' -e 's|^appVersion:.*|appVersion: '"$IMAGE_TAG"'|g' $CHART_PATH/Chart.yaml
sed -i'.bak' -e 's|^appVersion:.*|appVersion: '/""$IMAGE_TAG"/"'|g' $CHART_PATH/Chart.yaml

if [ "$DEBUG_MODE" = true ] ; then
cat $CHART_PATH/Chart.yaml
Expand All @@ -103,5 +113,9 @@ fi
echo "Commiting chart source changes to $CURRENT_BRANCH branch"
git add $CHART_PATH/Chart.yaml
git commit --message "Update image version to $IMAGE_TAG"
git push --set-upstream origin $CURRENT_BRANCH

if [ "$TEST_MODE" = false ] ; then
git push --set-upstream origin $CURRENT_BRANCH
fi

cd -
85 changes: 85 additions & 0 deletions test/test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env bats


# Define constants
CHART_FILE="Chart.yaml"
TEMP_CHART_FILE="Chart.temp.yaml"
CHART_PATH="example-chart"
REPOSITORY="example-chart-repo"
EXPECTED_VERSION="0.0.2"
EXPECTED_APP_VERSION="eb8b0d81"


# Setup a temporary directory for testing
setup() {
# Create a fake repository and Chart.yaml
TEST_REPO=$(mktemp -d)
cp src/run.sh $TEST_REPO

mkdir -p "$TEST_REPO/.ssh"

touch $TEST_REPO/.ssh/id_rsa $TEST_REPO/.ssh/known_hosts

mkdir -p "$TEST_REPO/$REPOSITORY"
cd "$TEST_REPO/$REPOSITORY"

git init
git config user.email "test@example.com"
git config user.name "Test User"
git config commit.gpgsign false

mkdir -p $CHART_PATH

cat <<EOF > $CHART_PATH/Chart.yaml
apiVersion: v2
name: example-chart
description: Helm Chart Description
type: application
version: 0.0.1
appVersion: "a1b2c3d4"
EOF

git add $CHART_PATH/Chart.yaml
git commit -m "Initial commit"
cd $TEST_REPO
}

# Teardown: Clean up the fake repository after testing
teardown() {
cd ..
rm -rf "$TEST_REPO"
}

@test "run.sh updates Chart.yaml with new appVersion and version" {

# Export necessary environment variables
export PLUGIN_TEST=true
export PLUGIN_SSH_FOLDER="$TEST_REPO/.ssh"
export PLUGIN_SSH_PRIVATE_KEY_FILE="$TEST_REPO/.ssh/id_rsa"
export PLUGIN_KNOWN_HOSTS_FILE="$TEST_REPO/.ssh/known_hosts"
export PLUGIN_SSH_URL="git@fakeaddress.com:user/$REPOSITORY"
export PLUGIN_SSH_KEY="fake-key"
export PLUGIN_EMAIL="test@example.com"
export PLUGIN_NAME="Test User"
export PLUGIN_IMAGE_TAG="$EXPECTED_APP_VERSION"
export PLUGIN_CHART_PATH="$CHART_PATH"

# Run the script
run $TEST_REPO/run.sh

# Check that the script ran successfully
[ "$status" -eq 0 ]

# Verify Chart.yaml was updated correctly
run grep "version: $EXPECTED_VERSION" $REPOSITORY/$CHART_PATH/Chart.yaml
[ "$status" -eq 0 ]

run grep "appVersion: /"$EXPECTED_APP_VERSION"/" $REPOSITORY/$CHART_PATH/Chart.yaml
[ "$status" -eq 0 ]

# Verify git commit was made
cd $TEST_REPO/$REPOSITORY
run git log --oneline
[ "$(grep -c "Update image version to $EXPECTED_APP_VERSION" <<<"$output")" -eq 1 ]
cd -
}

0 comments on commit 7503c4c

Please sign in to comment.