Skip to content
Closed
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: 5 additions & 1 deletion .github/workflows/fmt-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:

jobs:
terraform:
name: Lint
name: Lint and Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -23,4 +23,8 @@ jobs:
terraform_version: "1.8.2"
- name: Terraform fmt
run: task fmt:check
- name: Terraform Init
run: terraform init
- name: Terraform Test
run: task test

30 changes: 30 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
name: Terraform Tests

on:
pull_request:
branches:
- main
push:
branches:
- main

jobs:
test:
name: Unit Tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Task
uses: arduino/setup-task@v2
with:
version: 3.x
repo-token: ${{ secrets.GITHUB_TOKEN }}
- name: Install Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: "1.8.2"
- name: Terraform Init
run: terraform init
- name: Terraform Test
run: task test:verbose
469 changes: 450 additions & 19 deletions README.md

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ tasks:
desc: Check if the input is formatted
cmds:
- terraform fmt -recursive -check -diff .

test:
desc: Run Terraform unit tests
cmds:
- terraform test

test:verbose:
desc: Run Terraform unit tests with verbose output
cmds:
- terraform test -verbose
7 changes: 7 additions & 0 deletions data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ data "cloudinit_config" "config" {
})
filename = "sensor-build.yaml"
}

lifecycle {
precondition {
condition = (var.fleet_token == "" && var.fleet_url == "" && var.fleet_server_sslname == "") || (var.fleet_token != "" && var.fleet_url != "" && var.fleet_server_sslname != "")
error_message = "Fleet Manager pairing requires all three variables to be set together: fleet_token, fleet_url, and fleet_server_sslname. Either set all three or leave all three empty."
}
}
}
28 changes: 0 additions & 28 deletions examples/deployment/main.tf

This file was deleted.

3 changes: 0 additions & 3 deletions examples/deployment/versions.tf

This file was deleted.

4 changes: 3 additions & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
output "cloudinit_config" {
value = data.cloudinit_config.config
value = data.cloudinit_config.config
sensitive = true
description = "The complete cloudinit_config data source object. Use .rendered for the final user data string."
}
91 changes: 91 additions & 0 deletions tests/basic_configuration.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Test basic sensor configuration without optional features
run "basic_sensor_config" {
command = plan

variables {
fleet_community_string = "test-community-string"
sensor_license = "test-license-key"
sensor_management_interface_name = "eth0"
sensor_monitoring_interface_name = "eth1"
sensor_health_check_probe_source_ranges_cidr = ["35.191.0.0/16", "130.211.0.0/22"]
subnetwork_monitoring_cidr = "10.0.1.0/24"
subnetwork_monitoring_gateway = "10.0.1.1"
}

assert {
condition = output.cloudinit_config.rendered != null
error_message = "cloudinit_config output should not be null"
}

assert {
condition = can(regex("password: test-community-string", output.cloudinit_config.rendered))
error_message = "Community string should be present in rendered config"
}

assert {
condition = can(regex("license_key: test-license-key", output.cloudinit_config.rendered))
error_message = "License should be present in rendered config"
}

assert {
condition = can(regex("name: eth0", output.cloudinit_config.rendered))
error_message = "Management interface name should be present in rendered config"
}

assert {
condition = can(regex("name: eth1", output.cloudinit_config.rendered))
error_message = "Monitoring interface name should be present in rendered config"
}
}

# Test minimal configuration without health checks
run "minimal_config_without_health_checks" {
command = plan

variables {
fleet_community_string = "test-community-string"
sensor_license = "test-license-key"
sensor_management_interface_name = "eth0"
sensor_monitoring_interface_name = "eth1"
}

assert {
condition = output.cloudinit_config.rendered != null
error_message = "cloudinit_config output should not be null"
}

assert {
condition = can(regex("password: test-community-string", output.cloudinit_config.rendered))
error_message = "Community string should be present in rendered config"
}
}

# Test that configuration includes health check when both CIDR and gateway are provided
run "health_check_present_when_configured" {
command = plan

variables {
fleet_community_string = "test-community-string"
sensor_license = "test-license-key"
sensor_management_interface_name = "eth0"
sensor_monitoring_interface_name = "eth1"
subnetwork_monitoring_cidr = "10.0.1.0/24"
subnetwork_monitoring_gateway = "10.0.1.1"
sensor_health_check_probe_source_ranges_cidr = ["35.191.0.0/16"]
}

assert {
condition = can(regex("health_check:", output.cloudinit_config.rendered))
error_message = "Health check section should be present when CIDR and gateway are configured"
}

assert {
condition = can(regex("10.0.1.0/24", output.cloudinit_config.rendered))
error_message = "Monitoring CIDR should be present in health check config"
}

assert {
condition = can(regex("10.0.1.1", output.cloudinit_config.rendered))
error_message = "Monitoring gateway should be present in health check config"
}
}
115 changes: 115 additions & 0 deletions tests/feature_flags.tftest.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Test Prometheus feature flag
run "prometheus_enabled" {
command = plan

variables {
fleet_community_string = "test-community-string"
sensor_license = "test-license-key"
sensor_management_interface_name = "eth0"
sensor_monitoring_interface_name = "eth1"
prometheus_enabled = true
}

assert {
condition = output.cloudinit_config.rendered != null
error_message = "cloudinit_config output should not be null"
}

assert {
condition = can(regex("prometheus:", output.cloudinit_config.rendered))
error_message = "Prometheus section should be present when enabled"
}
}

# Test Prometheus disabled (default)
run "prometheus_disabled" {
command = plan

variables {
fleet_community_string = "test-community-string"
sensor_license = "test-license-key"
sensor_management_interface_name = "eth0"
sensor_monitoring_interface_name = "eth1"
prometheus_enabled = false
}

assert {
condition = output.cloudinit_config.rendered != null
error_message = "cloudinit_config output should not be null"
}

assert {
condition = !can(regex("prometheus:", output.cloudinit_config.rendered))
error_message = "Prometheus section should not be present when disabled"
}
}

# Test FedRAMP mode enabled
run "fedramp_enabled" {
command = plan

variables {
fleet_community_string = "test-community-string"
sensor_license = "test-license-key"
sensor_management_interface_name = "eth0"
sensor_monitoring_interface_name = "eth1"
fedramp_mode_enabled = true
}

assert {
condition = output.cloudinit_config.rendered != null
error_message = "cloudinit_config output should not be null"
}

assert {
condition = can(regex("fedramp_mode:", output.cloudinit_config.rendered))
error_message = "FedRAMP mode section should be present when enabled"
}
}

# Test FedRAMP mode disabled (default)
run "fedramp_disabled" {
command = plan

variables {
fleet_community_string = "test-community-string"
sensor_license = "test-license-key"
sensor_management_interface_name = "eth0"
sensor_monitoring_interface_name = "eth1"
fedramp_mode_enabled = false
}

assert {
condition = output.cloudinit_config.rendered != null
error_message = "cloudinit_config output should not be null"
}

assert {
condition = !can(regex("fedramp_mode:", output.cloudinit_config.rendered))
error_message = "FedRAMP mode section should not be present when disabled"
}
}

# Test both features enabled together
run "multiple_features_enabled" {
command = plan

variables {
fleet_community_string = "test-community-string"
sensor_license = "test-license-key"
sensor_management_interface_name = "eth0"
sensor_monitoring_interface_name = "eth1"
prometheus_enabled = true
fedramp_mode_enabled = true
}

assert {
condition = can(regex("prometheus:", output.cloudinit_config.rendered))
error_message = "Prometheus section should be present when enabled"
}

assert {
condition = can(regex("fedramp_mode:", output.cloudinit_config.rendered))
error_message = "FedRAMP mode section should be present when enabled"
}
}
Loading