|
| 1 | +package rules |
| 2 | + |
| 3 | +import ( |
| 4 | + hcl "github.com/hashicorp/hcl/v2" |
| 5 | + "github.com/terraform-linters/tflint-plugin-sdk/helper" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func Test_AwsLambdaFunctionEndOfSupport(t *testing.T) { |
| 11 | + type caseStudy struct { |
| 12 | + Name string |
| 13 | + Content string |
| 14 | + 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", |
| 30 | + Content: ` |
| 31 | +resource "aws_lambda_function" "function" { |
| 32 | + function_name = "test_function" |
| 33 | + role = "test_role" |
| 34 | + runtime = "` + runtime + `" |
| 35 | +} |
| 36 | +`, |
| 37 | + Expected: helper.Issues{ |
| 38 | + { |
| 39 | + Rule: NewAwsLambdaFunctionDeprecatedRuntimeRule(), |
| 40 | + Message: "The \"" + runtime + "\" runtime has reached the end of support", |
| 41 | + Range: hcl.Range{ |
| 42 | + Filename: "resource.tf", |
| 43 | + Start: hcl.Pos{Line: 5, Column: 12}, |
| 44 | + End: hcl.Pos{Line: 5, Column: len(runtime) + 14}, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 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", |
| 70 | + Content: ` |
| 71 | +resource "aws_lambda_function" "function" { |
| 72 | + function_name = "test_function" |
| 73 | + role = "test_role" |
| 74 | + runtime = "` + runtime + `" |
| 75 | +} |
| 76 | +`, |
| 77 | + Expected: helper.Issues{ |
| 78 | + { |
| 79 | + Rule: NewAwsLambdaFunctionDeprecatedRuntimeRule(), |
| 80 | + Message: "The \"" + runtime + "\" runtime has reached the end of life", |
| 81 | + Range: hcl.Range{ |
| 82 | + Filename: "resource.tf", |
| 83 | + Start: hcl.Pos{Line: 5, Column: 12}, |
| 84 | + End: hcl.Pos{Line: 5, Column: len(runtime) + 14}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | + cases = append(cases, study) |
| 90 | + } |
| 91 | + |
| 92 | + rule := NewAwsLambdaFunctionDeprecatedRuntimeRule() |
| 93 | + |
| 94 | + for _, tc := range cases { |
| 95 | + runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content}) |
| 96 | + |
| 97 | + if err := rule.Check(runner); err != nil { |
| 98 | + t.Fatalf("Unexpected error occurred: %s", err) |
| 99 | + } |
| 100 | + |
| 101 | + helper.AssertIssues(t, tc.Expected, runner.Issues) |
| 102 | + } |
| 103 | +} |
0 commit comments