Skip to content

Commit

Permalink
cert-manager test in bats
Browse files Browse the repository at this point in the history
  • Loading branch information
HumbertoPeluso committed Sep 11, 2021
2 parents 72a5840 + 65e3585 commit 0020c41
Show file tree
Hide file tree
Showing 118 changed files with 958 additions and 601 deletions.
60 changes: 60 additions & 0 deletions vkpr-test/keycloak-test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

VKPR_HOME=~/.vkpr

setup_file() {
load 'common-setup'
_common_setup
if [ "$VKPR_TEST_SKIP_SETUP" == "true" ]; then
echo "setup: skipping setup due to VKPR_TEST_SKIP_SETUP=true" >&3
else
echo "setup: installing ingress...." >&3
rit vkpr ingress install
kubectl wait --for=condition=ready --timeout=1m pod --all
echo "setup: installing keycloak...." >&3
rit vkpr keycloak install --default
kubectl wait --for=condition=ready --timeout=1m pod --all
sleep 2
fi
}

setup() {
load $VKPR_HOME/bats/bats-support/load.bash
load $VKPR_HOME/bats/bats-assert/load.bash
}

@test "Check if keycloak is up" {
run curlKeycloak
actual="${lines[3]}"
trim "$actual"
actual="$TRIMMED"
expected='"realm":"master"'
assert_equal "$actual" "$expected"
}

teardown_file() {
if [ "$VKPR_TEST_SKIP_TEARDOWN" == "true" ]; then
echo "teardown: skipping teardown due to VKPR_TEST_SKIP_TEARDOWN=true" >&3
else
echo "teardown: uninstalling ingress...." >&3
rit vkpr keycloak remove
sleep 5
rit vkpr ingress remove
sleep 5
rit vkpr infra down
sleep 5
fi
}

curlKeycloak(){
content=$(curl -H "Host: keycloak.localhost" http://127.0.0.1:8000/auth/realms/master)
echo ${content:1:16}
}

trim() {
local var="$*"
# remove leading whitespace characters
var="${var#"${var%%[![:space:]]*}"}"
# remove trailing whitespace characters
var="${var%"${var##*[![:space:]]}"}"
TRIMMED="$var"
}
41 changes: 41 additions & 0 deletions vkpr-test/postgres.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

VKPR_HOME=~/.vkpr

setup_file() {
load 'common-setup'
_common_setup
if [ "$VKPR_TEST_SKIP_SETUP" == "true" ]; then
echo "setup: skipping setup due to VKPR_TEST_SKIP_SETUP=true" >&3
else
echo "setup: installing postgres...." >&3
rit vkpr postgres install
kubectl wait --for=condition=ready --timeout=1m pod --all
sleep 2
fi
}

setup() {
load $VKPR_HOME/bats/bats-support/load.bash
load $VKPR_HOME/bats/bats-assert/load.bash
}

@test "Ping in DB and must show if is accepting connections" {
run ping_db
actual="${lines[0]}"
expected="postgres-postgresql:5432 - accepting connections"
assert_equal "$actual" "$expected"
}

teardown_file() {
if [ "$VKPR_TEST_SKIP_TEARDOWN" == "true" ]; then
echo "teardown: skipping teardown due to VKPR_TEST_SKIP_TEARDOWN=true" >&3
else
echo "teardown: uninstalling postgres...." >&3
rit vkpr postgres remove
rit vkpr infra down
fi
}

ping_db(){
$VKPR_HOME/bin/kubectl run test-db --rm -it --restart='Never' --image docker.io/bitnami/postgresql:11.13.0-debian-10-r12 --env="PGUSER=postgres" --env="PGPASSWORD=123" --env="PGHOST=postgres-postgresql" --env="PGPORT=5432" --command -- pg_isready
}
50 changes: 50 additions & 0 deletions vkpr-test/whoami.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

VKPR_HOME=~/.vkpr

setup_file() {
load 'common-setup'
_common_setup
if [ "$VKPR_TEST_SKIP_SETUP" == "true" ]; then
echo "setup: skipping setup due to VKPR_TEST_SKIP_SETUP=true" >&3
else
echo "setup: installing ingress...." >&3
rit vkpr ingress install
kubectl wait --for=condition=ready --timeout=1m pod --all
echo "setup: installing whoami...." >&3
rit vkpr whoami install --default
kubectl wait --for=condition=ready --timeout=1m pod --all
sleep 20
fi
}

setup() {
load $VKPR_HOME/bats/bats-support/load.bash
load $VKPR_HOME/bats/bats-assert/load.bash
}

@test "Curl to Whoami and must return Hostname" {
run curlWhoami
actual="${lines[3]}"
expected=$(podName)
assert_equal "$actual" "$expected"
}

teardown_file() {
if [ "$VKPR_TEST_SKIP_TEARDOWN" == "true" ]; then
echo "teardown: skipping teardown due to VKPR_TEST_SKIP_TEARDOWN=true" >&3
else
echo "teardown: uninstalling ingress...." >&3
rit vkpr ingress remove
rit vkpr infra down
fi
}

podName(){
local pod=$($VKPR_HOME/bin/kubectl get po -o name | grep whoami | cut -d "/" -f 2)
echo "Hostname: ${pod}"
}

curlWhoami(){
curl -k -H "Host: whoami.localhost" https://127.0.0.1:8001 | sed 's/<\/*[^>]*>//g'
sleep 5
}
20 changes: 20 additions & 0 deletions vkpr/aws/eks/deploy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Description

Create a Cluster EKS in AWS with [Gitops Terraform Module](https://gitlab.com/vkpr/terraform-aws-eks).

## Commands

Interactive inputs:

```bash
vkpr aws eks deploy
```

Non-interactive:

```bash
rit set credential --fields="token,username" --provider="gitlab" --values="<your-gitlab-token>,<your-gitlab-username>"
vkpr aws eks deploy
```

**Note**: If you set the credentials of gitlab in formula `vkpr aws eks up`, you dont need to set again.
3 changes: 1 addition & 2 deletions vkpr/globals/set/build.sh → vkpr/aws/eks/deploy/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh

BIN_FOLDER=bin
BINARY_NAME_UNIX=run.sh
Expand All @@ -9,4 +9,3 @@ ENTRY_POINT_UNIX=main.sh
cp -r src/* $BIN_FOLDER
mv $BIN_FOLDER/$ENTRY_POINT_UNIX $BIN_FOLDER/$BINARY_NAME_UNIX
chmod +x $BIN_FOLDER/$BINARY_NAME_UNIX

14 changes: 14 additions & 0 deletions vkpr/aws/eks/deploy/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"inputs": [
{
"name": "gitlab_token",
"type": "CREDENTIAL_GITLAB_TOKEN"
},
{
"name": "gitlab_username",
"type": "CREDENTIAL_GITLAB_USERNAME"
}
],
"template": "shell-bat",
"templateRelease:": "2.16.2"
}
4 changes: 4 additions & 0 deletions vkpr/aws/eks/deploy/help.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"short": "Deploy AWS EKS with Gitops",
"long": "Deploy the AWS EKS by your Gitlab CI Pipeline. The KUBECONFIG will be in the Artifacts on the pipeline."
}
18 changes: 18 additions & 0 deletions vkpr/aws/eks/deploy/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"execution": [
"local"
],
"os": {
"deps": [],
"support": [
"mac",
"linux"
]
},
"tags": [
"vkpr",
"aws",
"eks",
"deploy"
]
}
12 changes: 12 additions & 0 deletions vkpr/aws/eks/deploy/src/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

VKPR_SCRIPTS=~/.vkpr/src

source $VKPR_SCRIPTS/log.sh
source $VKPR_SCRIPTS/var.sh
source $VKPR_SCRIPTS/helper.sh

# shellcheck source=/dev/null
. "$(dirname "$0")"/unix/formula/formula.sh --source-only

runFormula
15 changes: 15 additions & 0 deletions vkpr/aws/eks/deploy/src/unix/formula/formula.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

runFormula() {
PROJECT_ID=$(curl https://gitlab.com/api/v4/users/$GITLAB_USERNAME/projects | jq '.[0] | .id')
BUILD_COMPLETE=$(curl -H "PRIVATE-TOKEN: $GITLAB_TOKEN" https://gitlab.com/api/v4/projects/$PROJECT_ID/jobs | jq '.[2] | .status')
SECONDS=0
while [[ $BUILD_COMPLETE != '"success"' ]]; do
echoColor "yellow" "Pipeline still executing, await more... ${SECONDS}s passed"
sleep 30
let "SECONDS+30"
BUILD_COMPLETE=$(curl -H "PRIVATE-TOKEN: $GITLAB_TOKEN" https://gitlab.com/api/v4/projects/$PROJECT_ID/jobs | jq '.[2] | .status')
done
DEPLOY_ID=$(curl -H "PRIVATE-TOKEN: $GITLAB_TOKEN" https://gitlab.com/api/v4/projects/$PROJECT_ID/jobs | jq '.[1] | .id')
curl -H "PRIVATE-TOKEN: $GITLAB_TOKEN" -X POST -s https://gitlab.com/api/v4/projects/$PROJECT_ID/jobs/$DEPLOY_ID/play > /dev/null
}
11 changes: 11 additions & 0 deletions vkpr/aws/eks/destroy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Description

Destroy the EKS in AWS.

## Commands

Non-interactive:

```bash
vkpr aws eks destroy
```
11 changes: 11 additions & 0 deletions vkpr/aws/eks/destroy/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh

BIN_FOLDER=bin
BINARY_NAME_UNIX=run.sh
ENTRY_POINT_UNIX=main.sh

#bash-build:
mkdir -p $BIN_FOLDER
cp -r src/* $BIN_FOLDER
mv $BIN_FOLDER/$ENTRY_POINT_UNIX $BIN_FOLDER/$BINARY_NAME_UNIX
chmod +x $BIN_FOLDER/$BINARY_NAME_UNIX
14 changes: 14 additions & 0 deletions vkpr/aws/eks/destroy/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"inputs": [
{
"name": "gitlab_token",
"type": "CREDENTIAL_GITLAB_TOKEN"
},
{
"name": "gitlab_username",
"type": "CREDENTIAL_GITLAB_USERNAME"
}
],
"template": "shell-bat",
"templateRelease:": "2.16.2"
}
4 changes: 4 additions & 0 deletions vkpr/aws/eks/destroy/help.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"short": "Destroy the AWS EKS",
"long": "Destroy the AWS EKS by your Gitlab CI Pipeline."
}
18 changes: 18 additions & 0 deletions vkpr/aws/eks/destroy/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"execution": [
"local"
],
"os": {
"deps": [],
"support": [
"mac",
"linux"
]
},
"tags": [
"vkpr",
"aws",
"eks",
"destroy"
]
}
12 changes: 12 additions & 0 deletions vkpr/aws/eks/destroy/src/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

VKPR_SCRIPTS=~/.vkpr/src

source $VKPR_SCRIPTS/log.sh
source $VKPR_SCRIPTS/var.sh
source $VKPR_SCRIPTS/helper.sh

# shellcheck source=/dev/null
. "$(dirname "$0")"/unix/formula/formula.sh --source-only

runFormula
7 changes: 7 additions & 0 deletions vkpr/aws/eks/destroy/src/unix/formula/formula.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

runFormula() {
PROJECT_ID=$(curl https://gitlab.com/api/v4/users/$GITLAB_USERNAME/projects | jq '.[0] | .id')
DESTROY_ID=$(curl -H "PRIVATE-TOKEN: $GITLAB_TOKEN" https://gitlab.com/api/v4/projects/$PROJECT_ID/jobs | jq '.[0] | .id')
curl -H "PRIVATE-TOKEN: $GITLAB_TOKEN" -X POST -s https://gitlab.com/api/v4/projects/$PROJECT_ID/jobs/$DESTROY_ID/play > /dev/null
}
4 changes: 4 additions & 0 deletions vkpr/aws/eks/help.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"short": "Manage AWS EKS",
"long": "Amazon Elastic Kubernetes Service (Amazon EKS) gives you the flexibility to start, run, and scale Kubernetes applications in the AWS Cloud or on-premises."
}
39 changes: 39 additions & 0 deletions vkpr/aws/eks/up/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Description

Fork the project from [Gitops EKS](https://gitlab.com/vkpr/aws-eks) and run the pipeline to build the terraform config.

## Commands

Interactive inputs:

```bash
vkpr aws eks up
```

Non-interactive:

```bash
rit set credential --fields="accesskeyid,secretaccesskey" --provider="aws" --values="<your-access-key-id>,<your-secret-access-key>"
rit set credential --fields="token,username" --provider="gitlab" --values="<your-gitlab-token>,<your-gitlab-username>"
vkpr aws eks up --aws_region="us-east-1"
```

```bash
vkpr aws eks up --default
```

## Parameters

```bash
--aws_region= Define the region to create the EKS. Default: us-east-1
--default= Set all values with default.
```

## Globals File Parameters

```yaml
global:
aws:
eks:
aws_region: <String>
```
Loading

0 comments on commit 0020c41

Please sign in to comment.