-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat): Add test coverage for Action.DefaultExecutor()
- Loading branch information
Showing
3 changed files
with
132 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |