Skip to content

add sample terraform test based e2e tests #305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}"
}
}
Loading