Skip to content

Commit

Permalink
Fix broken aws_lambda_function_deprecated_runtime test (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 authored Aug 31, 2021
1 parent feed9d6 commit 24e58a4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 52 deletions.
11 changes: 7 additions & 4 deletions rules/aws_lambda_function_deprecated_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package rules

import (
"fmt"
"time"

hcl "github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/terraform-linters/tflint-ruleset-aws/project"
"time"
)

// AwsLambdaFunctionDeprecatedRuntimeRule checks to see if the lambda runtime has reached End Of Support
Expand All @@ -14,6 +15,8 @@ type AwsLambdaFunctionDeprecatedRuntimeRule struct {
attributeName string
eosRuntimes map[string]time.Time
eolRuntimes map[string]time.Time

Now time.Time
}

// NewAwsLambdaFunctionDeprecatedRuntimeRule returns new rule with default attributes
Expand All @@ -40,6 +43,7 @@ func NewAwsLambdaFunctionDeprecatedRuntimeRule() *AwsLambdaFunctionDeprecatedRun
"python2.7": time.Date(2021, time.September, 30, 0, 0, 0, 0, time.UTC),
"dotnetcore2.1": time.Date(2021, time.October, 30, 0, 0, 0, 0, time.UTC),
},
Now: time.Now().UTC(),
}
}

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

return runner.EnsureNoError(err, func() error {
if _, ok := r.eolRuntimes[val]; ok && now.After(r.eolRuntimes[val]) {
if _, ok := r.eolRuntimes[val]; ok && r.Now.After(r.eolRuntimes[val]) {
runner.EmitIssueOnExpr(
r,
fmt.Sprintf("The \"%s\" runtime has reached the end of life", val),
attribute.Expr,
)
} else if _, ok := r.eosRuntimes[val]; ok && now.After(r.eosRuntimes[val]) {
} else if _, ok := r.eosRuntimes[val]; ok && r.Now.After(r.eosRuntimes[val]) {
runner.EmitIssueOnExpr(
r,
fmt.Sprintf("The \"%s\" runtime has reached the end of support", val),
Expand Down
82 changes: 34 additions & 48 deletions rules/aws_lambda_function_deprecated_runtime_test.go
Original file line number Diff line number Diff line change
@@ -1,97 +1,83 @@
package rules

import (
hcl "github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/helper"
"testing"
"time"

hcl "github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/helper"
)

func Test_AwsLambdaFunctionEndOfSupport(t *testing.T) {
type caseStudy struct {
cases := []struct {
Name string
Content string
Now time.Time
Expected helper.Issues
}
eosRuntimes := map[string]time.Time{
"nodejs10.x": time.Date(2021, time.July, 30, 0, 0, 0, 0, time.UTC),
"ruby2.5": time.Date(2021, time.July, 30, 0, 0, 0, 0, time.UTC),
"python2.7": time.Date(2021, time.July, 15, 0, 0, 0, 0, time.UTC),
"dotnetcore2.1": time.Date(2021, time.September, 20, 0, 0, 0, 0, time.UTC),
}
var cases []caseStudy
now := time.Now().UTC()
for runtime, eosDate := range eosRuntimes {
if now.Before(eosDate) {
continue
}
study := caseStudy{
Name: runtime + " end of support",
}{
{
Name: "EOS",
Content: `
resource "aws_lambda_function" "function" {
function_name = "test_function"
role = "test_role"
runtime = "` + runtime + `"
runtime = "nodejs10.x"
}
`,
Now: time.Date(2021, time.August, 10, 0, 0, 0, 0, time.UTC),
Expected: helper.Issues{
{
Rule: NewAwsLambdaFunctionDeprecatedRuntimeRule(),
Message: "The \"" + runtime + "\" runtime has reached the end of support",
Message: "The \"nodejs10.x\" runtime has reached the end of support",
Range: hcl.Range{
Filename: "resource.tf",
Start: hcl.Pos{Line: 5, Column: 12},
End: hcl.Pos{Line: 5, Column: len(runtime) + 14},
End: hcl.Pos{Line: 5, Column: 24},
},
},
},
}
cases = append(cases, study)
}
eolRuntimes := map[string]time.Time{
"dotnetcore1.0": time.Date(2019, time.July, 30, 0, 0, 0, 0, time.UTC),
"dotnetcore2.0": time.Date(2019, time.May, 30, 0, 0, 0, 0, time.UTC),
"nodejs": time.Date(2016, time.October, 31, 0, 0, 0, 0, time.UTC),
"nodejs4.3": time.Date(2020, time.March, 06, 0, 0, 0, 0, time.UTC),
"nodejs4.3-edge": time.Date(2019, time.April, 30, 0, 0, 0, 0, time.UTC),
"nodejs6.10": time.Date(2019, time.August, 12, 0, 0, 0, 0, time.UTC),
"nodejs8.10": time.Date(2020, time.March, 06, 0, 0, 0, 0, time.UTC),
"nodejs10.x": time.Date(2021, time.August, 30, 0, 0, 0, 0, time.UTC),
"ruby2.5": time.Date(2021, time.August, 30, 0, 0, 0, 0, time.UTC),
"python2.7": time.Date(2021, time.September, 30, 0, 0, 0, 0, time.UTC),
"dotnetcore2.1": time.Date(2021, time.October, 30, 0, 0, 0, 0, time.UTC),
}
for runtime, eolDate := range eolRuntimes {
if now.Before(eolDate) {
continue
}
study := caseStudy{
Name: runtime + " end of life",
},
{
Name: "EOF",
Content: `
resource "aws_lambda_function" "function" {
function_name = "test_function"
role = "test_role"
runtime = "` + runtime + `"
runtime = "nodejs10.x"
}
`,
Now: time.Date(2021, time.September, 1, 0, 0, 0, 0, time.UTC),
Expected: helper.Issues{
{
Rule: NewAwsLambdaFunctionDeprecatedRuntimeRule(),
Message: "The \"" + runtime + "\" runtime has reached the end of life",
Message: "The \"nodejs10.x\" runtime has reached the end of life",
Range: hcl.Range{
Filename: "resource.tf",
Start: hcl.Pos{Line: 5, Column: 12},
End: hcl.Pos{Line: 5, Column: len(runtime) + 14},
End: hcl.Pos{Line: 5, Column: 24},
},
},
},
}
cases = append(cases, study)
},
{
Name: "Live",
Content: `
resource "aws_lambda_function" "function" {
function_name = "test_function"
role = "test_role"
runtime = "nodejs10.x"
}
`,
Now: time.Date(2021, time.June, 25, 0, 0, 0, 0, time.UTC),
Expected: helper.Issues{},
},
}

rule := NewAwsLambdaFunctionDeprecatedRuntimeRule()

for _, tc := range cases {
rule.Now = tc.Now

runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})

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

0 comments on commit 24e58a4

Please sign in to comment.