Skip to content

Commit 24e58a4

Browse files
authored
Fix broken aws_lambda_function_deprecated_runtime test (#175)
1 parent feed9d6 commit 24e58a4

File tree

2 files changed

+41
-52
lines changed

2 files changed

+41
-52
lines changed

rules/aws_lambda_function_deprecated_runtime.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package rules
22

33
import (
44
"fmt"
5+
"time"
6+
57
hcl "github.com/hashicorp/hcl/v2"
68
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
79
"github.com/terraform-linters/tflint-ruleset-aws/project"
8-
"time"
910
)
1011

1112
// AwsLambdaFunctionDeprecatedRuntimeRule checks to see if the lambda runtime has reached End Of Support
@@ -14,6 +15,8 @@ type AwsLambdaFunctionDeprecatedRuntimeRule struct {
1415
attributeName string
1516
eosRuntimes map[string]time.Time
1617
eolRuntimes map[string]time.Time
18+
19+
Now time.Time
1720
}
1821

1922
// NewAwsLambdaFunctionDeprecatedRuntimeRule returns new rule with default attributes
@@ -40,6 +43,7 @@ func NewAwsLambdaFunctionDeprecatedRuntimeRule() *AwsLambdaFunctionDeprecatedRun
4043
"python2.7": time.Date(2021, time.September, 30, 0, 0, 0, 0, time.UTC),
4144
"dotnetcore2.1": time.Date(2021, time.October, 30, 0, 0, 0, 0, time.UTC),
4245
},
46+
Now: time.Now().UTC(),
4347
}
4448
}
4549

@@ -69,16 +73,15 @@ func (r *AwsLambdaFunctionDeprecatedRuntimeRule) Check(runner tflint.Runner) err
6973
return runner.WalkResourceAttributes(r.resourceType, r.attributeName, func(attribute *hcl.Attribute) error {
7074
var val string
7175
err := runner.EvaluateExpr(attribute.Expr, &val, nil)
72-
now := time.Now().UTC()
7376

7477
return runner.EnsureNoError(err, func() error {
75-
if _, ok := r.eolRuntimes[val]; ok && now.After(r.eolRuntimes[val]) {
78+
if _, ok := r.eolRuntimes[val]; ok && r.Now.After(r.eolRuntimes[val]) {
7679
runner.EmitIssueOnExpr(
7780
r,
7881
fmt.Sprintf("The \"%s\" runtime has reached the end of life", val),
7982
attribute.Expr,
8083
)
81-
} else if _, ok := r.eosRuntimes[val]; ok && now.After(r.eosRuntimes[val]) {
84+
} else if _, ok := r.eosRuntimes[val]; ok && r.Now.After(r.eosRuntimes[val]) {
8285
runner.EmitIssueOnExpr(
8386
r,
8487
fmt.Sprintf("The \"%s\" runtime has reached the end of support", val),

rules/aws_lambda_function_deprecated_runtime_test.go

Lines changed: 34 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,83 @@
11
package rules
22

33
import (
4-
hcl "github.com/hashicorp/hcl/v2"
5-
"github.com/terraform-linters/tflint-plugin-sdk/helper"
64
"testing"
75
"time"
6+
7+
hcl "github.com/hashicorp/hcl/v2"
8+
"github.com/terraform-linters/tflint-plugin-sdk/helper"
89
)
910

1011
func Test_AwsLambdaFunctionEndOfSupport(t *testing.T) {
11-
type caseStudy struct {
12+
cases := []struct {
1213
Name string
1314
Content string
15+
Now time.Time
1416
Expected helper.Issues
15-
}
16-
eosRuntimes := map[string]time.Time{
17-
"nodejs10.x": time.Date(2021, time.July, 30, 0, 0, 0, 0, time.UTC),
18-
"ruby2.5": time.Date(2021, time.July, 30, 0, 0, 0, 0, time.UTC),
19-
"python2.7": time.Date(2021, time.July, 15, 0, 0, 0, 0, time.UTC),
20-
"dotnetcore2.1": time.Date(2021, time.September, 20, 0, 0, 0, 0, time.UTC),
21-
}
22-
var cases []caseStudy
23-
now := time.Now().UTC()
24-
for runtime, eosDate := range eosRuntimes {
25-
if now.Before(eosDate) {
26-
continue
27-
}
28-
study := caseStudy{
29-
Name: runtime + " end of support",
17+
}{
18+
{
19+
Name: "EOS",
3020
Content: `
3121
resource "aws_lambda_function" "function" {
3222
function_name = "test_function"
3323
role = "test_role"
34-
runtime = "` + runtime + `"
24+
runtime = "nodejs10.x"
3525
}
3626
`,
27+
Now: time.Date(2021, time.August, 10, 0, 0, 0, 0, time.UTC),
3728
Expected: helper.Issues{
3829
{
3930
Rule: NewAwsLambdaFunctionDeprecatedRuntimeRule(),
40-
Message: "The \"" + runtime + "\" runtime has reached the end of support",
31+
Message: "The \"nodejs10.x\" runtime has reached the end of support",
4132
Range: hcl.Range{
4233
Filename: "resource.tf",
4334
Start: hcl.Pos{Line: 5, Column: 12},
44-
End: hcl.Pos{Line: 5, Column: len(runtime) + 14},
35+
End: hcl.Pos{Line: 5, Column: 24},
4536
},
4637
},
4738
},
48-
}
49-
cases = append(cases, study)
50-
}
51-
eolRuntimes := map[string]time.Time{
52-
"dotnetcore1.0": time.Date(2019, time.July, 30, 0, 0, 0, 0, time.UTC),
53-
"dotnetcore2.0": time.Date(2019, time.May, 30, 0, 0, 0, 0, time.UTC),
54-
"nodejs": time.Date(2016, time.October, 31, 0, 0, 0, 0, time.UTC),
55-
"nodejs4.3": time.Date(2020, time.March, 06, 0, 0, 0, 0, time.UTC),
56-
"nodejs4.3-edge": time.Date(2019, time.April, 30, 0, 0, 0, 0, time.UTC),
57-
"nodejs6.10": time.Date(2019, time.August, 12, 0, 0, 0, 0, time.UTC),
58-
"nodejs8.10": time.Date(2020, time.March, 06, 0, 0, 0, 0, time.UTC),
59-
"nodejs10.x": time.Date(2021, time.August, 30, 0, 0, 0, 0, time.UTC),
60-
"ruby2.5": time.Date(2021, time.August, 30, 0, 0, 0, 0, time.UTC),
61-
"python2.7": time.Date(2021, time.September, 30, 0, 0, 0, 0, time.UTC),
62-
"dotnetcore2.1": time.Date(2021, time.October, 30, 0, 0, 0, 0, time.UTC),
63-
}
64-
for runtime, eolDate := range eolRuntimes {
65-
if now.Before(eolDate) {
66-
continue
67-
}
68-
study := caseStudy{
69-
Name: runtime + " end of life",
39+
},
40+
{
41+
Name: "EOF",
7042
Content: `
7143
resource "aws_lambda_function" "function" {
7244
function_name = "test_function"
7345
role = "test_role"
74-
runtime = "` + runtime + `"
46+
runtime = "nodejs10.x"
7547
}
7648
`,
49+
Now: time.Date(2021, time.September, 1, 0, 0, 0, 0, time.UTC),
7750
Expected: helper.Issues{
7851
{
7952
Rule: NewAwsLambdaFunctionDeprecatedRuntimeRule(),
80-
Message: "The \"" + runtime + "\" runtime has reached the end of life",
53+
Message: "The \"nodejs10.x\" runtime has reached the end of life",
8154
Range: hcl.Range{
8255
Filename: "resource.tf",
8356
Start: hcl.Pos{Line: 5, Column: 12},
84-
End: hcl.Pos{Line: 5, Column: len(runtime) + 14},
57+
End: hcl.Pos{Line: 5, Column: 24},
8558
},
8659
},
8760
},
88-
}
89-
cases = append(cases, study)
61+
},
62+
{
63+
Name: "Live",
64+
Content: `
65+
resource "aws_lambda_function" "function" {
66+
function_name = "test_function"
67+
role = "test_role"
68+
runtime = "nodejs10.x"
69+
}
70+
`,
71+
Now: time.Date(2021, time.June, 25, 0, 0, 0, 0, time.UTC),
72+
Expected: helper.Issues{},
73+
},
9074
}
9175

9276
rule := NewAwsLambdaFunctionDeprecatedRuntimeRule()
9377

9478
for _, tc := range cases {
79+
rule.Now = tc.Now
80+
9581
runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})
9682

9783
if err := rule.Check(runner); err != nil {

0 commit comments

Comments
 (0)