Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NewRunner hook not injecting a custom runner #252

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions plugin/host2plugin/host2plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type mockRuleSetImpl struct {
configSchema func() *hclext.BodySchema
applyGlobalConfig func(*tflint.Config) error
applyConfig func(*hclext.BodyContent) error
newRunner func(tflint.Runner) (tflint.Runner, error)
check func(tflint.Runner) error
}

Expand Down Expand Up @@ -79,6 +80,13 @@ func (r *mockRuleSet) ApplyConfig(content *hclext.BodyContent) error {
return nil
}

func (r *mockRuleSet) NewRunner(runner tflint.Runner) (tflint.Runner, error) {
if r.impl.newRunner != nil {
return r.impl.newRunner(runner)
}
return runner, nil
}

func (r *mockRuleSet) Check(runner tflint.Runner) error {
if r.impl.check != nil {
return r.impl.check(runner)
Expand Down Expand Up @@ -575,6 +583,14 @@ func (s *mockServer) GetFiles(tflint.ModuleCtxType) map[string][]byte {
return map[string][]byte{}
}

type mockCustomRunner struct {
tflint.Runner
}

func (s *mockCustomRunner) Hello() string {
return "Hello from custom runner!"
}

func TestCheck(t *testing.T) {
// default error check helper
neverHappend := func(err error) bool { return err != nil }
Expand All @@ -589,10 +605,11 @@ func TestCheck(t *testing.T) {
}

tests := []struct {
Name string
Arg func() plugin2host.Server
ServerImpl func(tflint.Runner) error
ErrCheck func(error) bool
Name string
Arg func() plugin2host.Server
ServerImpl func(tflint.Runner) error
NewRunnerImpl func(tflint.Runner) (tflint.Runner, error)
ErrCheck func(error) bool
}{
{
Name: "bidirectional",
Expand Down Expand Up @@ -667,11 +684,26 @@ resource "aws_instance" "foo" {
return err == nil || err.Error() != "unexpected error"
},
},
{
Name: "inject new runner",
Arg: func() plugin2host.Server {
return &mockServer{}
},
NewRunnerImpl: func(runner tflint.Runner) (tflint.Runner, error) {
return &mockCustomRunner{runner}, nil
},
ServerImpl: func(runner tflint.Runner) error {
return errors.New(runner.(*mockCustomRunner).Hello())
},
ErrCheck: func(err error) bool {
return err == nil || err.Error() != "Hello from custom runner!"
},
},
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
client := startTestGRPCPluginServer(t, newMockRuleSet("test_ruleset", "0.1.0", mockRuleSetImpl{check: test.ServerImpl}))
client := startTestGRPCPluginServer(t, newMockRuleSet("test_ruleset", "0.1.0", mockRuleSetImpl{check: test.ServerImpl, newRunner: test.NewRunnerImpl}))

err := client.Check(test.Arg())
if test.ErrCheck(err) {
Expand Down
7 changes: 5 additions & 2 deletions plugin/host2plugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,11 @@ func (s *GRPCServer) Check(ctx context.Context, req *proto.Check_Request) (*prot
}
defer conn.Close()

err = s.impl.Check(&plugin2host.GRPCClient{Client: proto.NewRunnerClient(conn)})

runner, err := s.impl.NewRunner(&plugin2host.GRPCClient{Client: proto.NewRunnerClient(conn)})
if err != nil {
return nil, toproto.Error(codes.FailedPrecondition, err)
}
err = s.impl.Check(runner)
if err != nil {
return nil, toproto.Error(codes.Aborted, err)
}
Expand Down
5 changes: 0 additions & 5 deletions tflint/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@ func (r *BuiltinRuleSet) NewRunner(runner Runner) (Runner, error) {

// Check runs inspection for each rule by applying Runner.
func (r *BuiltinRuleSet) Check(runner Runner) error {
runner, err := r.NewRunner(runner)
if err != nil {
return err
}

for _, rule := range r.EnabledRules {
if err := rule.Check(runner); err != nil {
return fmt.Errorf("Failed to check `%s` rule: %s", rule.Name(), err)
Expand Down