Skip to content

Commit

Permalink
use table-driven tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpivkin committed Feb 26, 2024
1 parent 56e86aa commit 6c11ee0
Showing 1 changed file with 49 additions and 72 deletions.
121 changes: 49 additions & 72 deletions pkg/iac/scanners/terraform/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,59 +905,41 @@ data "http" "example" {
}

func TestForEach(t *testing.T) {

t.Run("arg is set and ref to each.key", func(t *testing.T) {
src := `locals {
tests := []struct {
name string
src string
expectedBucketName string
expectedNameLabel string
}{
{
name: "arg is set and ref to each.key",
src: `locals {
buckets = ["bucket1"]
}
resource "aws_s3_bucket" "this" {
for_each = toset(local.buckets)
bucket = each.key
}`

modules := parse(t, map[string]string{
"main.tf": src,
})
require.Len(t, modules, 1)

buckets := modules.GetResourcesByType("aws_s3_bucket")
assert.Len(t, buckets, 1)

bucket := buckets[0]
bucketName := bucket.GetAttribute("bucket").Value().AsString()
assert.Equal(t, "bucket1", bucketName)

assert.Equal(t, `this["bucket1"]`, bucket.NameLabel())
})

t.Run("arg is set and ref to each.value", func(t *testing.T) {
src := `locals {
}`,
expectedBucketName: "bucket1",
expectedNameLabel: `this["bucket1"]`,
},
{
name: "arg is set and ref to each.value",
src: `locals {
buckets = ["bucket1"]
}
resource "aws_s3_bucket" "this" {
for_each = toset(local.buckets)
bucket = each.value
}`

modules := parse(t, map[string]string{
"main.tf": src,
})
require.Len(t, modules, 1)

buckets := modules.GetResourcesByType("aws_s3_bucket")
assert.Len(t, buckets, 1)

bucket := buckets[0]
bucketName := bucket.GetAttribute("bucket").Value().AsString()
assert.Equal(t, "bucket1", bucketName)

assert.Equal(t, `this["bucket1"]`, bucket.NameLabel())
})

t.Run("arg is map and ref to each.key", func(t *testing.T) {
src := `locals {
}`,
expectedBucketName: "bucket1",
expectedNameLabel: `this["bucket1"]`,
},
{
name: "arg is map and ref to each.key",
src: `locals {
buckets = {
bucket1key = "bucket1value"
}
Expand All @@ -966,25 +948,13 @@ resource "aws_s3_bucket" "this" {
resource "aws_s3_bucket" "this" {
for_each = local.buckets
bucket = each.key
}`

modules := parse(t, map[string]string{
"main.tf": src,
})
require.Len(t, modules, 1)

buckets := modules.GetResourcesByType("aws_s3_bucket")
assert.Len(t, buckets, 1)

bucket := buckets[0]
bucketName := bucket.GetAttribute("bucket").Value().AsString()
assert.Equal(t, "bucket1key", bucketName)

assert.Equal(t, `this["bucket1key"]`, bucket.NameLabel())
})

t.Run("arg is map and ref to each.value", func(t *testing.T) {
src := `locals {
}`,
expectedBucketName: "bucket1key",
expectedNameLabel: `this["bucket1key"]`,
},
{
name: "arg is map and ref to each.value",
src: `locals {
buckets = {
bucket1key = "bucket1value"
}
Expand All @@ -993,22 +963,29 @@ resource "aws_s3_bucket" "this" {
resource "aws_s3_bucket" "this" {
for_each = local.buckets
bucket = each.value
}`
}`,
expectedBucketName: "bucket1value",
expectedNameLabel: `this["bucket1key"]`,
},
}

modules := parse(t, map[string]string{
"main.tf": src,
})
require.Len(t, modules, 1)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
modules := parse(t, map[string]string{
"main.tf": tt.src,
})
require.Len(t, modules, 1)

buckets := modules.GetResourcesByType("aws_s3_bucket")
assert.Len(t, buckets, 1)
buckets := modules.GetResourcesByType("aws_s3_bucket")
assert.Len(t, buckets, 1)

bucket := buckets[0]
bucketName := bucket.GetAttribute("bucket").Value().AsString()
assert.Equal(t, "bucket1value", bucketName)
bucket := buckets[0]
bucketName := bucket.GetAttribute("bucket").Value().AsString()
assert.Equal(t, tt.expectedBucketName, bucketName)

assert.Equal(t, `this["bucket1key"]`, bucket.NameLabel())
})
assert.Equal(t, tt.expectedNameLabel, bucket.NameLabel())
})
}

}

Expand Down

0 comments on commit 6c11ee0

Please sign in to comment.