Skip to content

Commit

Permalink
(feat): Add test coverage for Action.DefaultExecutor()
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed Dec 27, 2023
1 parent 133354e commit 83de845
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/ebs-bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
fb := backend.NewLinuxFileBackend(ufs)
ub := backend.NewLinuxOwnerBackend(uos)
dmb := backend.NewLinuxDeviceMetricsBackend(lds, fssf)
dae := action.NewDefaultActionExecutor(c)
dae := action.NewDefaultActionExecutor()

// Modify Config
modifiers := []config.Modifier{
Expand Down
7 changes: 2 additions & 5 deletions internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"strings"

"github.com/reecetech/ebs-bootstrap/internal/config"
"github.com/reecetech/ebs-bootstrap/internal/model"
)

Expand All @@ -23,13 +22,11 @@ type ActionExecutor interface {
}

type DefaultActionExecutor struct {
config *config.Config
read func(buffer *string) error
read func(buffer *string) error
}

func NewDefaultActionExecutor(c *config.Config) *DefaultActionExecutor {
func NewDefaultActionExecutor() *DefaultActionExecutor {
return &DefaultActionExecutor{
config: c,
read: func(buffer *string) error {
_, err := fmt.Scanln(buffer)
return err
Expand Down
129 changes: 129 additions & 0 deletions internal/action/action_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package action

import (
"fmt"
"testing"

"github.com/reecetech/ebs-bootstrap/internal/model"
"github.com/reecetech/ebs-bootstrap/internal/utils"
)

type MockAction struct {
execute func() error
mode model.Mode
}

func (ma *MockAction) Execute() error {
return ma.execute()
}

func (ma *MockAction) Success() string {
return "Successfully executed action"
}

func (ma *MockAction) Prompt() string {
return "Would you like to execute action"
}

func (ma *MockAction) Refuse() string {
return "Refused to execute action"
}

func (ma *MockAction) GetMode() model.Mode {
return ma.mode
}

func (ma *MockAction) SetMode(mode model.Mode) Action {
ma.mode = mode
return ma
}

func TestDefaultActionExecutor(t *testing.T) {
var input = func(i string) func(buffer *string) error {
return func(buffer *string) error {
*buffer = i
return nil
}
}
var fail = func(err string) func(buffer *string) error {
return func(buffer *string) error {
return fmt.Errorf(err)
}
}

subtests := []struct {
Name string
Read func(buffer *string) error
Error error
Mode model.Mode
ExpectedErr error
}{
{
Name: "Mode=Empty + Read=Disabled + Action=Success",
Read: fail("πŸ”΄ Standard Input Disabled"),
Error: nil,
Mode: model.Empty,
ExpectedErr: fmt.Errorf("πŸ”΄ Unsupported mode was encountered. Refused to execute action"),
},
{
Name: "Mode=Healthcheck + Read=Avoid + Action=Success",
Read: fail("πŸ”΄ Standard Input Disabled"),
Error: nil,
Mode: model.Healthcheck,
ExpectedErr: fmt.Errorf("πŸ”΄ Healthcheck mode enabled. Refused to execute action"),
},
{
Name: "Mode=Prompt + Read=Input<y> + Action=Success",
Read: input("y"),
Error: nil,
Mode: model.Prompt,
ExpectedErr: nil,
},
{
Name: "Mode=Prompt + Read=Input<yes> + Action=Success",
Read: input("yes"),
Error: nil,
Mode: model.Prompt,
ExpectedErr: nil,
},
{
Name: "Mode=Prompt + Read=Failure + Action=Success",
Read: fail("πŸ”΄ Failed to Read From Standard Input"),
Error: nil,
Mode: model.Prompt,
ExpectedErr: fmt.Errorf("πŸ”΄ Action rejected. Refused to execute action"),
},
{
Name: "Mode=Prompt + Read=Input<n> + Action=Success",
Read: input("n"),
Error: nil,
Mode: model.Prompt,
ExpectedErr: fmt.Errorf("πŸ”΄ Action rejected. Refused to execute action"),
},
{
Name: "Mode=Force + Read=Disabled + Action=Success",
Read: fail("πŸ”΄ Standard Input Disabled"),
Error: nil,
Mode: model.Force,
ExpectedErr: nil,
},
{
Name: "Mode=Force + Read=Disabled + Action=Failure",
Read: fail("πŸ”΄ Standard Input Disabled"),
Error: fmt.Errorf("πŸ”΄ Error encountered while executing action"),
Mode: model.Force,
ExpectedErr: fmt.Errorf("πŸ”΄ Error encountered while executing action"),
},
}
for _, subtest := range subtests {
dae := &DefaultActionExecutor{
read: subtest.Read,
}
a := &MockAction{
execute: func() error { return subtest.Error },
mode: subtest.Mode,
}
err := dae.Execute([]Action{a})
utils.CheckError("dae.Execute()", t, subtest.ExpectedErr, err)
}
}

0 comments on commit 83de845

Please sign in to comment.