Skip to content

Commit

Permalink
add backend test
Browse files Browse the repository at this point in the history
  • Loading branch information
javdet committed Mar 17, 2023
1 parent bda2b97 commit f42b000
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 265 deletions.
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@ module github.com/dodopizza/tflint-ruleset-dodo

go 1.20

require github.com/terraform-linters/tflint-plugin-sdk v0.15.0
require (
github.com/hashicorp/hcl/v2 v2.15.0
github.com/stretchr/testify v1.8.2
github.com/terraform-linters/tflint-plugin-sdk v0.15.0
)

require (
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/go-hclog v1.4.0 // indirect
github.com/hashicorp/go-plugin v1.4.8 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hcl/v2 v2.15.0 // indirect
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 // indirect
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/zclconf/go-cty v1.12.1 // indirect
Expand All @@ -30,4 +35,5 @@ require (
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 7 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/terraform-linters/tflint-plugin-sdk v0.15.0 h1:bUJ9OskzT/I98XaJ5+rs7ymVPHiGT8oI4bG86LkopVY=
github.com/terraform-linters/tflint-plugin-sdk v0.15.0/go.mod h1:enH5i7SHelcvC2AGZavEJzcrRF7nhAaOwTdaBjr/Zjo=
github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvCazn8G65U=
Expand Down Expand Up @@ -143,6 +148,7 @@ google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
51 changes: 51 additions & 0 deletions rules/backend_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package rules

import (
"fmt"

"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/tflint"
)

const (
requiredBackendType = "azurerm"
requiredYCBackendType = "s3"
backendTypeMessageTemplate = "backend type should be \"%s\" but defined: \"%s\""
)

func NewBackendTypeRule() *Rule {
return NewRule(
"backend_type",
func(runner tflint.Runner, rule tflint.Rule) error {
body, err := runner.GetModuleContent(
&hclext.BodySchema{
Blocks: []hclext.BlockSchema{
{Type: "backend", LabelNames: []string{"type"}, Body: &hclext.BodySchema{
Attributes: []hclext.AttributeSchema{{Name: "key"}},
}},
},
},
&tflint.GetModuleContentOption{},
)
if err != nil {
return err
}

for _, backend := range body.Blocks {
if backend.Type != requiredBackendType || backend.Labels[0] != requiredYCBackendType {
return runner.EmitIssue(
rule,
fmt.Sprintf(
backendTypeMessageTemplate,
requiredBackendType,
backend.Type,
),
backend.DefRange,
)
}
}

return nil
},
)
}
70 changes: 70 additions & 0 deletions rules/backend_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package rules

import (
"fmt"
"testing"

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

func Test_BackendType(t *testing.T) {
cases := []struct {
Name string
Content string
Expected helper.Issues
}{
{
Name: "no issues",
Content: `
terraform {
backend "azurerm" {
resource_group_name = "rg"
storage_account_name = "sa"
container_name = "tfstate"
key = "path/to/my/key"
}
}`,
Expected: helper.Issues{},
},
{
Name: "issue found",
Content: `
terraform {
backend "s3" {
bucket = "mybucket"
key = "path/to/my/key"
region = "us-east-1"
}
}`,
Expected: helper.Issues{
{
Rule: NewBackendTypeRule(),
Message: fmt.Sprintf(
backendTypeMessageTemplate,
requiredBackendType,
"s3",
),
Range: hcl.Range{
Filename: filename,
Start: hcl.Pos{Line: 3, Column: 3},
End: hcl.Pos{Line: 3, Column: 15},
},
},
},
},
}

rule := NewBackendTypeRule()

for _, tc := range cases {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
runner := helper.TestRunner(t, map[string]string{filename: tc.Content})

require.NoError(t, rule.Check(runner))
helper.AssertIssues(t, tc.Expected, runner.Issues)
})
}
}
83 changes: 0 additions & 83 deletions rules/foreach_count.go

This file was deleted.

Loading

0 comments on commit f42b000

Please sign in to comment.