Skip to content

Commit e9eb457

Browse files
janikmuenkjanikmuenk
janikmuenk
authored and
janikmuenk
committed
feat(tests): adding tests for examples
1 parent 2aa6061 commit e9eb457

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
resource "aws_dynamodb_table" "this" {
2+
// using for_each would be better practice, but would also break compatibility to upstream. Migration move block could be possible
23
count = var.create_table && !var.autoscaling_enabled ? 1 : 0
34

45
name = var.name

tests/autoscaling.tftest.hcl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
provider "aws" {
2+
profile = "developer-sandbox-administratoraccess"
3+
}
4+
5+
variables {
6+
tags = {
7+
//Should be $pwd showing name of the testfile
8+
"Created_by": "automated Test autoscaling_apply",
9+
"Created_at": timestamp()
10+
"IaC_Module": "https://github.com/andsafe-AG/terraform-aws-dynamodb-table"
11+
// local exec - git branch
12+
"IaC_Branch": "main"
13+
// local exec - git log -1 --pretty=format:%h
14+
"IaC_Commit": "hash"
15+
}
16+
}
17+
18+
run "autoscaling_plan" {
19+
command = plan
20+
module {
21+
source = "./examples/autoscaling"
22+
}
23+
}
24+
25+
run "autoscaling_apply" {
26+
command = apply
27+
28+
module {
29+
source = "./examples/autoscaling"
30+
}
31+
assert {
32+
condition = module.dynamodb_table.dynamodb_table_id == format("my-table-%s", random_pet.this.id)
33+
error_message = "Name output produced unexpected result"
34+
}
35+
assert {
36+
condition = can(regex("^arn:aws:dynamodb:[^:]+:[0-9]{12}:table/[a-zA-Z0-9-]+$", module.dynamodb_table.dynamodb_table_arn))
37+
error_message = format("ARN %s - output produced unexpected result", module.dynamodb_table.dynamodb_table_arn)
38+
}
39+
}

tests/basic.tftest.hcl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
provider "aws" {
2+
profile = "developer-sandbox-administratoraccess"
3+
}
4+
5+
variables {
6+
tags = {
7+
//Should be $pwd showing name of the testfile
8+
"Created_by": "automated Test autoscaling_apply",
9+
"Created_at": timestamp()
10+
// local exec - git config --get remote.origin.url
11+
"IaC_Module": "https://github.com/andsafe-AG/terraform-aws-dynamodb-table"
12+
// local exec - git branch
13+
"IaC_Branch": "main"
14+
// local exec - git log -1 --pretty=format:%h
15+
"IaC_Commit": "hash"
16+
}
17+
}
18+
19+
20+
run "basic_plan" {
21+
command = plan
22+
module {
23+
source = "./examples/basic"
24+
}
25+
}
26+
27+
run "basic_apply" {
28+
command = apply
29+
30+
module {
31+
source = "./examples/basic"
32+
}
33+
assert {
34+
condition = module.dynamodb_table.dynamodb_table_id == format("my-table-%s", random_pet.this.id)
35+
error_message = "Name output produced unexpected result"
36+
}
37+
assert {
38+
condition = can(regex("^arn:aws:dynamodb:[^:]+:[0-9]{12}:table/[a-zA-Z0-9-]+$", module.dynamodb_table.dynamodb_table_arn))
39+
error_message = format("ARN %s - output produced unexpected result", module.dynamodb_table.dynamodb_table_arn)
40+
}
41+
}

tests/global-tables.tftest.hcl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
provider "aws" {
2+
profile = "developer-sandbox-administratoraccess"
3+
}
4+
5+
variables {
6+
tags = {
7+
//Should be $pwd showing name of the testfile
8+
"Created_by": "automated Test autoscaling_apply",
9+
"Created_at": timestamp()
10+
"IaC_Module": "https://github.com/andsafe-AG/terraform-aws-dynamodb-table"
11+
// local exec - git branch
12+
"IaC_Branch": "main"
13+
// local exec - git log -1 --pretty=format:%h
14+
"IaC_Commit": "hash"
15+
}
16+
}
17+
18+
run "global_tables_plan" {
19+
command = plan
20+
module {
21+
source = "./examples/global-tables"
22+
}
23+
}
24+
25+
run "global_tables_apply" {
26+
command = apply
27+
28+
module {
29+
source = "./examples/global-tables"
30+
}
31+
assert {
32+
condition = module.dynamodb_table.dynamodb_table_id == format("my-table-%s", random_pet.this.id)
33+
error_message = "Name output produced unexpected result"
34+
}
35+
assert {
36+
condition = can(regex("^arn:aws:dynamodb:[^:]+:[0-9]{12}:table/[a-zA-Z0-9-]+$", module.dynamodb_table.dynamodb_table_arn))
37+
error_message = format("ARN %s - output produced unexpected result", module.dynamodb_table.dynamodb_table_arn)
38+
}
39+
assert {
40+
condition = can(regex("^arn:aws:dynamodb:[^:]+:[0-9]{12}:table/[a-zA-Z0-9-]+/stream/.+$", module.dynamodb_table.dynamodb_table_stream_arn))
41+
error_message = format("ARN %s - output produced unexpected result", module.dynamodb_table.dynamodb_table_stream_arn)
42+
}
43+
}

0 commit comments

Comments
 (0)