Skip to content

Commit eb7c5fd

Browse files
committed
feat: add test_mode variable to skip AWS API calls during testing
1 parent c2a0c52 commit eb7c5fd

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed

registry/mavrickrishi/modules/aws-ami-snapshot/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ Users need the following IAM permissions for full functionality:
164164
| instance_id | The EC2 instance ID to create snapshots from | string | n/a | yes |
165165
| default_ami_id | The default AMI ID to use when not restoring from a snapshot | string | n/a | yes |
166166
| template_name | The name of the Coder template using this module | string | n/a | yes |
167+
| test_mode | Set to true when running tests to skip AWS API calls | bool | false | no |
167168
| tags | Additional tags to apply to snapshots | map(string) | {} | no |
168169
| enable_dlm_cleanup | Enable Data Lifecycle Manager for automated snapshot cleanup | bool | false | no |
169170
| dlm_role_arn | ARN of the IAM role for DLM | string | "" | no |

registry/mavrickrishi/modules/aws-ami-snapshot/main.test.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,45 @@ import {
88
describe("aws-ami-snapshot", async () => {
99
await runTerraformInit(import.meta.dir);
1010

11-
testRequiredVariables(import.meta.dir, {
12-
instance_id: "i-1234567890abcdef0",
13-
default_ami_id: "ami-12345678",
14-
template_name: "test-template",
11+
it("required variables with test mode", async () => {
12+
await runTerraformApply(import.meta.dir, {
13+
instance_id: "i-1234567890abcdef0",
14+
default_ami_id: "ami-12345678",
15+
template_name: "test-template",
16+
test_mode: true,
17+
});
18+
});
19+
20+
it("missing variable: instance_id", async () => {
21+
await expect(runTerraformApply(import.meta.dir, {
22+
default_ami_id: "ami-12345678",
23+
template_name: "test-template",
24+
test_mode: true,
25+
})).rejects.toThrow();
26+
});
27+
28+
it("missing variable: default_ami_id", async () => {
29+
await expect(runTerraformApply(import.meta.dir, {
30+
instance_id: "i-1234567890abcdef0",
31+
template_name: "test-template",
32+
test_mode: true,
33+
})).rejects.toThrow();
34+
});
35+
36+
it("missing variable: template_name", async () => {
37+
await expect(runTerraformApply(import.meta.dir, {
38+
instance_id: "i-1234567890abcdef0",
39+
default_ami_id: "ami-12345678",
40+
test_mode: true,
41+
})).rejects.toThrow();
1542
});
1643

1744
it("supports optional variables", async () => {
1845
await runTerraformApply(import.meta.dir, {
1946
instance_id: "i-1234567890abcdef0",
2047
default_ami_id: "ami-12345678",
2148
template_name: "test-template",
49+
test_mode: true,
2250
enable_dlm_cleanup: true,
2351
dlm_role_arn: "arn:aws:iam::123456789012:role/dlm-lifecycle-role",
2452
snapshot_retention_count: 5,

registry/mavrickrishi/modules/aws-ami-snapshot/main.tf

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ provider "aws" {
2525
}
2626

2727
# Variables
28+
variable "test_mode" {
29+
description = "Set to true when running tests to skip AWS API calls"
30+
type = bool
31+
default = false
32+
}
33+
2834
variable "instance_id" {
2935
description = "The EC2 instance ID to create snapshots from"
3036
type = string
@@ -96,11 +102,11 @@ data "coder_parameter" "use_previous_snapshot" {
96102
description = "Start with a fresh instance"
97103
}
98104
dynamic "option" {
99-
for_each = data.aws_ami_ids.workspace_snapshots.ids
105+
for_each = local.workspace_snapshot_ids
100106
content {
101-
name = "${data.aws_ami.snapshot_info[option.value].name} (${formatdate("YYYY-MM-DD hh:mm", data.aws_ami.snapshot_info[option.value].creation_date)})"
107+
name = var.test_mode ? "Test Snapshot" : "${local.snapshot_info[option.value].name} (${formatdate("YYYY-MM-DD hh:mm", local.snapshot_info[option.value].creation_date)})"
102108
value = option.value
103-
description = data.aws_ami.snapshot_info[option.value].description
109+
description = var.test_mode ? "Test Description" : local.snapshot_info[option.value].description
104110
}
105111
}
106112
}
@@ -109,8 +115,17 @@ data "coder_parameter" "use_previous_snapshot" {
109115
data "coder_workspace" "me" {}
110116
data "coder_workspace_owner" "me" {}
111117

118+
# Local values to handle test mode
119+
locals {
120+
workspace_snapshot_ids = var.test_mode ? [] : data.aws_ami_ids.workspace_snapshots[0].ids
121+
snapshot_info = var.test_mode ? {} : {
122+
for ami_id in local.workspace_snapshot_ids : ami_id => data.aws_ami.snapshot_info[ami_id]
123+
}
124+
}
125+
112126
# Retrieve existing snapshots for this workspace
113127
data "aws_ami_ids" "workspace_snapshots" {
128+
count = var.test_mode ? 0 : 1
114129
owners = ["self"]
115130

116131
filter {
@@ -136,7 +151,7 @@ data "aws_ami_ids" "workspace_snapshots" {
136151

137152
# Get detailed information about each snapshot
138153
data "aws_ami" "snapshot_info" {
139-
for_each = toset(data.aws_ami_ids.workspace_snapshots.ids)
154+
for_each = toset(local.workspace_snapshot_ids)
140155
owners = ["self"]
141156

142157
filter {
@@ -179,7 +194,7 @@ resource "aws_ami_from_instance" "workspace_snapshot" {
179194

180195
# Optional: Data Lifecycle Manager policy for automated cleanup
181196
resource "aws_dlm_lifecycle_policy" "workspace_snapshots" {
182-
count = var.enable_dlm_cleanup ? 1 : 0
197+
count = var.enable_dlm_cleanup && !var.test_mode ? 1 : 0
183198
description = "Lifecycle policy for Coder workspace AMI snapshots"
184199
execution_role_arn = var.dlm_role_arn
185200
state = "ENABLED"
@@ -227,17 +242,17 @@ output "snapshot_ami_id" {
227242

228243
output "available_snapshots" {
229244
description = "List of available snapshot AMI IDs for this workspace"
230-
value = data.aws_ami_ids.workspace_snapshots.ids
245+
value = local.workspace_snapshot_ids
231246
}
232247

233248
output "snapshot_info" {
234249
description = "Detailed information about available snapshots"
235-
value = {
236-
for ami_id in data.aws_ami_ids.workspace_snapshots.ids : ami_id => {
237-
name = data.aws_ami.snapshot_info[ami_id].name
238-
description = data.aws_ami.snapshot_info[ami_id].description
239-
created_date = data.aws_ami.snapshot_info[ami_id].creation_date
240-
tags = data.aws_ami.snapshot_info[ami_id].tags
250+
value = var.test_mode ? {} : {
251+
for ami_id in local.workspace_snapshot_ids : ami_id => {
252+
name = local.snapshot_info[ami_id].name
253+
description = local.snapshot_info[ami_id].description
254+
created_date = local.snapshot_info[ami_id].creation_date
255+
tags = local.snapshot_info[ami_id].tags
241256
}
242257
}
243258
}

0 commit comments

Comments
 (0)