Skip to content

Commit

Permalink
Add integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall committed May 21, 2024
1 parent 9f50612 commit c24615e
Show file tree
Hide file tree
Showing 10 changed files with 322 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/plan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
terraform_version: 1.5.7

- name: lint
run: terraform fmt *.tf modules/*/*.tf
run: find . -type f -name "*.tf" -exec terraform fmt {} \;
shell: bash

- name: Set up Cloud SDK
Expand Down
74 changes: 74 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Integration Test

on:
push:
branches-ignore:
- main
env:
TF_VAR_project: ${{ secrets.GCLOUD_PROJECT }}
jobs:
terraform:
name: Create test CR
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
pull-requests: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.5.7

- name: lint
run: terraform fmt *.tf modules/*/*.tf
shell: bash

- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v1
with:
version: 'latest'

- name: Authenticate to Google Cloud
id: 'auth'
uses: google-github-actions/auth@v1
with:
workload_identity_provider: ${{ secrets.GCLOUD_OIDC_POOL }}
service_account: ${{ secrets.GSA }}
token_format: 'access_token'

- name: Configure gcloud
run: |
gcloud config set project ${{ secrets.GCLOUD_PROJECT }}
gcloud config set disable_prompts true
- uses: 'docker/login-action@v3'
name: 'Docker login'
with:
registry: 'us-docker.pkg.dev'
username: 'oauth2accesstoken'
password: '${{ steps.auth.outputs.access_token }}'

- name: terraform init
run: terraform init -upgrade
working-directory: ./test

- name : pick a region, any region
run: terraform apply -target random_shuffle.region -auto-approve
working-directory: ./test

- name: terraform apply
run: terraform apply -auto-approve
working-directory: ./test

- name: run tests
run: ./test.sh
working-directory: ./test

- name: terraform destroy
run: terraform destroy -auto-approve
working-directory: ./test
4 changes: 3 additions & 1 deletion modules/cloudrun/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ resource "google_cloud_run_service_iam_member" "invoker" {

# create a serverless NEG for this set of regional services
resource "google_compute_region_network_endpoint_group" "neg" {
for_each = toset(var.regions)
for_each = var.skipNeg ? toset([]) : toset(var.regions)
name = "libops-neg-${var.name}-${each.value}"
network_endpoint_type = "SERVERLESS"
region = each.value
Expand All @@ -136,6 +136,8 @@ resource "google_compute_region_network_endpoint_group" "neg" {
}

resource "google_compute_backend_service" "backend" {
count = var.skipNeg ? 0 : 1

project = var.project
name = "libops-backend-${var.name}"

Expand Down
9 changes: 8 additions & 1 deletion modules/cloudrun/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
output "backend" {
value = google_compute_backend_service.backend.id
value = var.skipNeg ? "" : google_compute_backend_service.backend[0].id
}

output "urls" {
value = {
for region, service in google_cloud_run_service.cloudrun :
region => service.status[0].url
}
}
6 changes: 6 additions & 0 deletions modules/cloudrun/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ variable "project" {
description = "The GCP project to use"
}

variable "skipNeg" {
type = bool
default = false
}

variable "secrets" {
type = list(object({
name = string
Expand All @@ -42,6 +47,7 @@ variable "secrets" {
}))
default = []
}

variable "containers" {
type = list(object({
image = string
Expand Down
172 changes: 172 additions & 0 deletions test/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
terraform {
required_version = "= 1.5.7"
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "3.0.1"
}
github = {
source = "integrations/github"
version = "6.2.1"
}
google = {
source = "hashicorp/google"
version = "5.29.1"
}
}

backend "gcs" {
bucket = "libops-public-microservices-terraform"
prefix = "/github-test"
}
}

provider "google" {
alias = "default"
project = var.project
}

provider "docker" {
alias = "local"
registry_auth {
address = "us-docker.pkg.dev"
config_file = pathexpand("~/.docker/config.json")
}
}

resource "random_shuffle" "region" {
input = [
"us-east4",
"us-east5",
"us-central1",
"us-west3",
"us-west1",
"us-west4",
"us-south1"
]
result_count = 1
}


module "houdini" {
source = "../modules/cloudrun"

name = "houdini-test"
project = var.project
regions = random_shuffle.region.result
skipNeg = true
containers = tolist([
{
name = "houdini",
image = "us-docker.pkg.dev/${var.project}/shared/imagemagick:main",
port = 8080
liveness_probe = "/healthcheck"
}
])
providers = {
google = google.default
docker = docker.local
}
}

module "homarus" {
source = "../modules/cloudrun"

name = "homarus-test"
project = var.project
regions = random_shuffle.region.result
skipNeg = true
containers = tolist([
{
name = "homarus",
image = "us-docker.pkg.dev/${var.project}/shared/ffmpeg:main",
port = 8080
liveness_probe = "/healthcheck"
}
])
providers = {
google = google.default
docker = docker.local
}
}

module "hypercube" {
source = "../modules/cloudrun"

name = "hypercube-test"
project = var.project
regions = random_shuffle.region.result
skipNeg = true
containers = tolist([
{
name = "hypercube",
image = "us-docker.pkg.dev/${var.project}/shared/tesseract:main",
port = 8080
liveness_probe = "/healthcheck"
}
])
providers = {
google = google.default
docker = docker.local
}
}

module "fits" {
source = "../modules/cloudrun"

name = "fits-test"
project = var.project
regions = random_shuffle.region.result
skipNeg = true
containers = tolist([
{
name = "fits",
image = "us-docker.pkg.dev/${var.project}/shared/harvard-fits:main",
memory = "2Gi"
cpu = "2000m"
}
])

providers = {
google = google.default
docker = docker.local
}
}

module "crayfits" {
source = "../modules/cloudrun"

name = "crayfits-test"
project = var.project
regions = random_shuffle.region.result
skipNeg = true
containers = tolist([
{
name = "crayfits",
image = "us-docker.pkg.dev/${var.project}/shared/fits:main",
liveness_probe = "/healthcheck"
}
])
addl_env_vars = tolist([
{
name = "SCYLLARIDAE_YML"
value = <<EOT
allowedMimeTypes:
- "*"
cmdByMimeType:
default:
cmd: "curl"
args:
- "-X"
- "POST"
- "-F"
- "datafile=@-"
- "${module.fits.urls[random_shuffle.region.result[0]]}/fits/examine"
EOT
}
])
providers = {
google = google.default
docker = docker.local
}
}
8 changes: 8 additions & 0 deletions test/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
output "urls" {
value = {
crayfits = module.crayfits.urls[random_shuffle.region.result[0]],
homarus = module.homarus.urls[random_shuffle.region.result[0]],
houdini = module.houdini.urls[random_shuffle.region.result[0]],
hypercube = module.hypercube.urls[random_shuffle.region.result[0]]
}
}
47 changes: 47 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env bash

set -eou pipefail

JSON=$(terraform output -json)

KEYS=$(echo "$JSON" | jq -r '.urls.value | keys[]')

for KEY in $KEYS; do
URL=$(echo "$JSON" | jq -r ".urls.value[\"$KEY\"]")
echo "Testing $KEY at $URL"

if [ "$KEY" == "crayfits" ]; then
curl -s -o fits.xml \
--header "Accept: application/xml" \
--header "Apix-Ldp-Resource: https://www.libops.io/themes/custom/libops_www/assets/img/200x200/islandora.png" \
"$URL"
# check the md5 of that file exists in the FITS XML
grep d6c508e600dcd72d86b599b3afa06ec2 fits.xml | grep md5checksum
rm fits.xml
elif [ "$KEY" == "homarus" ]; then
curl -s -o image.jpg \
--header "X-Islandora-Args: -ss 00:00:45.000 -frames 1 -vf scale=720:-2" \
--header "Accept: image/jpeg" \
--header "Apix-Ldp-Resource: http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" \
"$URL"
md5 image.jpg | grep fe7dd57460dbaf50faa38affde54b694
rm image.jpg
elif [ "$KEY" == "houdini" ]; then
curl -s -o image.png \
--header "Accept: image/png" \
--header "Apix-Ldp-Resource: https://www.libops.io/themes/custom/libops_www/assets/img/200x200/islandora.png" \
"$URL"
file image.png | grep PNG
rm image.png
elif [ "$KEY" == "hypercube" ]; then
curl -s -o ocr.txt \
--header "Accept: text/plain" \
--header "Apix-Ldp-Resource: https://www.libops.io/sites/default/files/2024-05/Screen%20Shot%20on%202024-05-21%20at%2002-32-42.png" \
"$URL"
grep healthcheck ocr.txt
rm ocr.txt
else
echo "Unknown service"
exit 1
fi
done
3 changes: 3 additions & 0 deletions test/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
variable "project" {
type = string
}
1 change: 0 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
variable "project" {
type = string
}

0 comments on commit c24615e

Please sign in to comment.