Skip to content

Commit

Permalink
add sample terraform test based e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshrwolf committed Apr 25, 2024
1 parent eb660c6 commit 7845aae
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/regional-go-service/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ output "names" {
for k, v in google_cloud_run_v2_service.this : k => v.name
}
}

output "uris" {
value = {
for k, v in google_cloud_run_v2_service.this : k => v.uri
}
}
6 changes: 6 additions & 0 deletions modules/regional-go-service/tests/asserts/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
variable "endpoint" {}

data "http" "endpoint" {
url = var.endpoint
method = "GET"
}
16 changes: 16 additions & 0 deletions modules/regional-go-service/tests/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"
"html"
"log"
"net/http"
)

func main() {
http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
})

log.Fatal(http.ListenAndServe(":8080", nil))

Check failure on line 15 in modules/regional-go-service/tests/cmd/main.go

View workflow job for this annotation

GitHub Actions / golangci-lint

G114: Use of net/http serve function that has no support for setting timeouts (gosec)
}
24 changes: 24 additions & 0 deletions modules/regional-go-service/tests/setup/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
variable "project_id" {}

resource "random_string" "name" {
numeric = false
upper = false
special = false
length = 6
}

resource "google_service_account" "iam" {
project = var.project_id

account_id = random_string.name.id
display_name = "test-${random_string.name.id}"
description = "Dedicated service account for ${random_string.name.id}"
}

output "name" {
value = random_string.name.id
}

output "email" {
value = google_service_account.iam.email
}
60 changes: 60 additions & 0 deletions modules/regional-go-service/tests/valid.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
variables {
# set with -var 'project=your-project-id'
# project_id = ""
}

run "setup" {
module {
source = "./tests/setup/"
}
}

run "create" {
variables {
name = run.setup.name
project_id = var.project_id
regions = {
"us-central1" = {
network = "default"
subnet = "default"
}
}

service_account = run.setup.email

ingress = "INGRESS_TRAFFIC_ALL"
egress = "ALL_TRAFFIC"

containers = {
"hello" = {
source = {
working_dir = "./tests"
importpath = "./cmd/"
}
ports = [{ container_port = 8080 }]
}
}

notification_channels = []
}

assert {
condition = google_cloud_run_v2_service.this["us-central1"].terminal_condition[0].type == "Ready"
error_message = "Service not ready"
}
}

run "validate" {
module {
source = "./tests/asserts/"
}

variables {
endpoint = "${run.create.uris["us-central1"]}/bar"
}

assert {
condition = data.http.endpoint.status_code == 200
error_message = "Website responded with HTTP status ${data.http.endpoint.status_code}"
}
}

0 comments on commit 7845aae

Please sign in to comment.