Skip to content

test: Add terraform test files #17

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

Merged
merged 1 commit into from
Mar 9, 2025
Merged
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
21 changes: 21 additions & 0 deletions tests/final/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
terraform {
required_providers {
tailscale = {
source = "tailscale/tailscale"
version = "~> 0.0"
}
}
}

variable "tailscale_hostname" {
type = string
}

variable "tailscale_tag" {
type = string
}

data "tailscale_device" "test_instance" {
hostname = var.tailscale_hostname
wait_for = "60s"
}
73 changes: 73 additions & 0 deletions tests/main.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
mock_provider "aws" {
}

mock_provider "tailscale" {
mock_data "tailscale_device" {
defaults = {
tags = [
"tag:exit"
]
}
}
}

variables {
lightsail_region = "eu-central-1"
lightsail_region_friendly_name = "frankfurt"
tailscale_oauth_client_id = "fakeoauthid"
tailscale_oauth_client_secret = "fakeoauthsecret"
}

# Setup instance_prefix and timestamp
run "setup" {
module {
source = "./tests/setup"
}
}

# Run terraform-aws-lightsail-tailscale-exit-node module
run "create" {
variables {
lightsail_instance_name = "${run.setup.instance_prefix}-vpn-${var.lightsail_region}"
lightsail_tags = {
"created" = "${run.setup.timestamp}"
}
tailscale_hostname = var.lightsail_region_friendly_name
}

# Check the instance name
assert {
condition = aws_lightsail_instance.this.name == var.lightsail_instance_name
error_message = "Invalid instance name."
}

# Check the instance tag
assert {
condition = aws_lightsail_instance.this.tags["created"] == var.lightsail_tags["created"]
error_message = "Invalid tag."
}
}

# Validate Tailscale status
run "validate" {
module {
source = "./tests/final"
}

variables {
tailscale_hostname = var.lightsail_region_friendly_name
tailscale_tag = "tag:exit"
}

# Check hostname in Tailscale dashboard
assert {
condition = data.tailscale_device.test_instance.hostname == var.tailscale_hostname
error_message = "Invalid Tailscale hostname."
}

# Check tag in Tailscale dashboard
assert {
condition = sort(data.tailscale_device.test_instance.tags)[0] == var.tailscale_tag
error_message = "Invalid Tailscale tag."
}
}
26 changes: 26 additions & 0 deletions tests/setup/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
terraform {
required_providers {
random = {
source = "hashicorp/random"
version = "~> 3.0"
}
time = {
source = "hashicorp/time"
version = "~> 0.0"
}
}
}

resource "random_pet" "instance_prefix" {
length = 1
}

resource "time_static" "timestamp" {}

output "timestamp" {
value = formatdate("YYYYMMDD-hhmmss", "${time_static.timestamp.rfc3339}")
}

output "instance_prefix" {
value = random_pet.instance_prefix.id
}
Loading