Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
shijiesheng committed Sep 23, 2024
1 parent b5fe0a4 commit 89c3cae
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 74 deletions.
5 changes: 2 additions & 3 deletions tools/cli/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ import (
func newAdminWorkflowCommands() []*cli.Command {
return []*cli.Command{
{
Name: "show",
Aliases: []string{"show"},
Usage: "show workflow history from database",
Name: "show",
Usage: "show workflow history from database",
Flags: append(getDBFlags(),
// v2 history events
&cli.StringFlag{
Expand Down
3 changes: 1 addition & 2 deletions tools/cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,11 @@ func NewCliApp() *cli.App {
},
{
Name: "dlq",
Aliases: []string{"dlq"},
Usage: "Run admin operation on DLQ",
Subcommands: newAdminDLQCommands(),
},
{
Name: "db",
Name: "database",
Aliases: []string{"db"},
Usage: "Run admin operations on database",
Subcommands: newDBCommands(),
Expand Down
85 changes: 25 additions & 60 deletions tools/cli/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,6 @@ func (s *cliAppSuite) TearDownTest() {
s.mockCtrl.Finish() // assert mock’s expectations
}

func (s *cliAppSuite) RunErrorExitCode(arguments []string) int {
oldOsExit := osExit
defer func() { osExit = oldOsExit }()
var errorCode int
osExit = func(code int) {
errorCode = code
}
s.NoError(s.app.Run(arguments))
return errorCode
}

func (s *cliAppSuite) TestAppCommands() {
for _, test := range commands {
cmd := s.app.Command(test)
Expand All @@ -128,26 +117,22 @@ func (s *cliAppSuite) TestAppCommands() {

func (s *cliAppSuite) TestDomainRegister_LocalDomain() {
s.serverFrontendClient.EXPECT().RegisterDomain(gomock.Any(), gomock.Any()).Return(nil)
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "register", "--global_domain", "false"})
s.Equal(0, errorCode)
s.NoError(s.app.Run([]string{"", "--do", domainName, "domain", "register", "--global_domain", "false"}))
}

func (s *cliAppSuite) TestDomainRegister_GlobalDomain() {
s.serverFrontendClient.EXPECT().RegisterDomain(gomock.Any(), gomock.Any()).Return(nil)
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "register", "--global_domain", "true"})
s.Equal(0, errorCode)
s.NoError(s.app.Run([]string{"", "--do", domainName, "domain", "register", "--global_domain", "true"}))
}

func (s *cliAppSuite) TestDomainRegister_DomainExist() {
s.serverFrontendClient.EXPECT().RegisterDomain(gomock.Any(), gomock.Any()).Return(&types.DomainAlreadyExistsError{})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "register", "--global_domain", "true"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "domain", "register", "--global_domain", "true"}))
}

func (s *cliAppSuite) TestDomainRegister_Failed() {
s.serverFrontendClient.EXPECT().RegisterDomain(gomock.Any(), gomock.Any()).Return(&types.BadRequestError{"fake error"})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "register", "--global_domain", "true"})
s.Equal(1, errorCode)
s.Error(s.app.Run(([]string{"", "--do", domainName, "domain", "register", "--global_domain", "true"})))
}

var describeDomainResponseServer = &types.DescribeDomainResponse{
Expand Down Expand Up @@ -187,22 +172,19 @@ func (s *cliAppSuite) TestDomainUpdate_DomainNotExist() {
resp := describeDomainResponseServer
s.serverFrontendClient.EXPECT().DescribeDomain(gomock.Any(), gomock.Any()).Return(resp, nil)
s.serverFrontendClient.EXPECT().UpdateDomain(gomock.Any(), gomock.Any()).Return(nil, &types.EntityNotExistsError{})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "update"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "domain", "update"}))
}

func (s *cliAppSuite) TestDomainUpdate_ActiveClusterFlagNotSet_DomainNotExist() {
s.serverFrontendClient.EXPECT().DescribeDomain(gomock.Any(), gomock.Any()).Return(nil, &types.EntityNotExistsError{})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "update"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "domain", "update"}))
}

func (s *cliAppSuite) TestDomainUpdate_Failed() {
resp := describeDomainResponseServer
s.serverFrontendClient.EXPECT().DescribeDomain(gomock.Any(), gomock.Any()).Return(resp, nil)
s.serverFrontendClient.EXPECT().UpdateDomain(gomock.Any(), gomock.Any()).Return(nil, &types.BadRequestError{"faked error"})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "update"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "domain", "update"}))
}

func (s *cliAppSuite) TestDomainDeprecate() {
Expand All @@ -217,29 +199,25 @@ func (s *cliAppSuite) TestDomainDeprecate_DomainNotExist() {
s.serverFrontendClient.EXPECT().ListClosedWorkflowExecutions(gomock.Any(), gomock.Any()).Return(&types.ListClosedWorkflowExecutionsResponse{}, nil)
s.serverFrontendClient.EXPECT().ListOpenWorkflowExecutions(gomock.Any(), gomock.Any()).Return(&types.ListOpenWorkflowExecutionsResponse{}, nil)
s.serverFrontendClient.EXPECT().DeprecateDomain(gomock.Any(), gomock.Any()).Return(&types.EntityNotExistsError{})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "deprecate"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "domain", "deprecate"}))
}

func (s *cliAppSuite) TestDomainDeprecate_Failed() {
s.serverFrontendClient.EXPECT().ListClosedWorkflowExecutions(gomock.Any(), gomock.Any()).Return(&types.ListClosedWorkflowExecutionsResponse{}, nil)
s.serverFrontendClient.EXPECT().ListOpenWorkflowExecutions(gomock.Any(), gomock.Any()).Return(&types.ListOpenWorkflowExecutionsResponse{}, nil)
s.serverFrontendClient.EXPECT().DeprecateDomain(gomock.Any(), gomock.Any()).Return(&types.BadRequestError{"faked error"})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "deprecate"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "domain", "deprecate"}))
}

func (s *cliAppSuite) TestDomainDeprecate_ClosedWorkflowsExist() {
s.serverFrontendClient.EXPECT().ListClosedWorkflowExecutions(gomock.Any(), gomock.Any()).Return(listClosedWorkflowExecutionsResponse, nil)
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "deprecate"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "domain", "deprecate"}))
}

func (s *cliAppSuite) TestDomainDeprecate_OpenWorkflowsExist() {
s.serverFrontendClient.EXPECT().ListClosedWorkflowExecutions(gomock.Any(), gomock.Any()).Return(&types.ListClosedWorkflowExecutionsResponse{}, nil)
s.serverFrontendClient.EXPECT().ListOpenWorkflowExecutions(gomock.Any(), gomock.Any()).Return(listOpenWorkflowExecutionsResponse, nil)
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "deprecate"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "domain", "deprecate"}))
}

func (s *cliAppSuite) TestDomainDeprecate_Force() {
Expand All @@ -250,14 +228,12 @@ func (s *cliAppSuite) TestDomainDeprecate_Force() {

func (s *cliAppSuite) TestDomainDeprecate_DomainNotExist_Force() {
s.serverFrontendClient.EXPECT().DeprecateDomain(gomock.Any(), gomock.Any()).Return(&types.EntityNotExistsError{})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "deprecate", "--force"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "domain", "deprecate", "--force"}))
}

func (s *cliAppSuite) TestDomainDeprecate_Failed_Force() {
s.serverFrontendClient.EXPECT().DeprecateDomain(gomock.Any(), gomock.Any()).Return(&types.BadRequestError{"faked error"})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "deprecate", "--force"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "domain", "deprecate", "--force"}))
}

func (s *cliAppSuite) TestDomainDescribe() {
Expand All @@ -270,15 +246,13 @@ func (s *cliAppSuite) TestDomainDescribe() {
func (s *cliAppSuite) TestDomainDescribe_DomainNotExist() {
resp := describeDomainResponseServer
s.serverFrontendClient.EXPECT().DescribeDomain(gomock.Any(), gomock.Any()).Return(resp, &types.EntityNotExistsError{})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "describe"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "domain", "describe"}))
}

func (s *cliAppSuite) TestDomainDescribe_Failed() {
resp := describeDomainResponseServer
s.serverFrontendClient.EXPECT().DescribeDomain(gomock.Any(), gomock.Any()).Return(resp, &types.BadRequestError{"faked error"})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "domain", "describe"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "domain", "describe"}))
}

var (
Expand Down Expand Up @@ -357,8 +331,7 @@ func (s *cliAppSuite) TestRestartWorkflow() {
func (s *cliAppSuite) TestRestartWorkflow_Failed() {
resp := &types.RestartWorkflowExecutionResponse{RunID: uuid.New()}
s.serverFrontendClient.EXPECT().RestartWorkflowExecution(gomock.Any(), gomock.Any()).Return(resp, &types.BadRequestError{"faked error"})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "workflow", "restart", "-w", "wid"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "workflow", "restart", "-w", "wid"}))
}

func (s *cliAppSuite) TestDiagnoseWorkflow() {
Expand All @@ -370,8 +343,7 @@ func (s *cliAppSuite) TestDiagnoseWorkflow() {

func (s *cliAppSuite) TestDiagnoseWorkflow_Failed() {
s.serverFrontendClient.EXPECT().DiagnoseWorkflowExecution(gomock.Any(), gomock.Any()).Return(nil, &types.BadRequestError{"faked error"})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "workflow", "diagnose", "-w", "wid", "-r", "rid"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "workflow", "diagnose", "-w", "wid", "-r", "rid"}))
}

func (s *cliAppSuite) TestStartWorkflow() {
Expand All @@ -389,8 +361,7 @@ func (s *cliAppSuite) TestStartWorkflow_Failed() {
resp := &types.StartWorkflowExecutionResponse{RunID: uuid.New()}
s.serverFrontendClient.EXPECT().StartWorkflowExecution(gomock.Any(), gomock.Any()).Return(resp, &types.BadRequestError{"faked error"})
// start with wid
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "workflow", "start", "-tl", "testTaskList", "-wt", "testWorkflowType", "-et", "60", "-w", "wid"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "workflow", "start", "-tl", "testTaskList", "-wt", "testWorkflowType", "-et", "60", "-w", "wid"}))
}

func (s *cliAppSuite) TestRunWorkflow() {
Expand All @@ -410,10 +381,9 @@ func (s *cliAppSuite) TestRunWorkflow_Failed() {
resp := &types.StartWorkflowExecutionResponse{RunID: uuid.New()}
history := getWorkflowExecutionHistoryResponse
s.serverFrontendClient.EXPECT().StartWorkflowExecution(gomock.Any(), gomock.Any()).Return(resp, &types.BadRequestError{"faked error"})
s.serverFrontendClient.EXPECT().GetWorkflowExecutionHistory(gomock.Any(), gomock.Any()).Return(history, nil)
s.serverFrontendClient.EXPECT().GetWorkflowExecutionHistory(gomock.Any(), gomock.Any()).Return(history, nil).Times(0)
// start with wid
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "workflow", "run", "-tl", "testTaskList", "-wt", "testWorkflowType", "-et", "60", "-w", "wid"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "workflow", "run", "-tl", "testTaskList", "-wt", "testWorkflowType", "-et", "60", "-w", "wid"}))
}

func (s *cliAppSuite) TestTerminateWorkflow() {
Expand All @@ -424,8 +394,7 @@ func (s *cliAppSuite) TestTerminateWorkflow() {

func (s *cliAppSuite) TestTerminateWorkflow_Failed() {
s.serverFrontendClient.EXPECT().TerminateWorkflowExecution(gomock.Any(), gomock.Any()).Return(&types.BadRequestError{"faked error"})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "workflow", "terminate", "-w", "wid"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "workflow", "terminate", "-w", "wid"}))
}

func (s *cliAppSuite) TestCancelWorkflow() {
Expand All @@ -436,8 +405,7 @@ func (s *cliAppSuite) TestCancelWorkflow() {

func (s *cliAppSuite) TestCancelWorkflow_Failed() {
s.serverFrontendClient.EXPECT().RequestCancelWorkflowExecution(gomock.Any(), gomock.Any()).Return(&types.BadRequestError{"faked error"})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "workflow", "cancel", "-w", "wid"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "workflow", "cancel", "-w", "wid"}))
}

func (s *cliAppSuite) TestSignalWorkflow() {
Expand All @@ -448,8 +416,7 @@ func (s *cliAppSuite) TestSignalWorkflow() {

func (s *cliAppSuite) TestSignalWorkflow_Failed() {
s.serverFrontendClient.EXPECT().SignalWorkflowExecution(gomock.Any(), gomock.Any()).Return(&types.BadRequestError{"faked error"})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "workflow", "signal", "-w", "wid", "-n", "signal-name"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "workflow", "signal", "-w", "wid", "-n", "signal-name"}))
}

func (s *cliAppSuite) TestQueryWorkflow() {
Expand All @@ -475,8 +442,7 @@ func (s *cliAppSuite) TestQueryWorkflow_Failed() {
QueryResult: []byte("query-result"),
}
s.serverFrontendClient.EXPECT().QueryWorkflow(gomock.Any(), gomock.Any()).Return(resp, &types.BadRequestError{"faked error"})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "workflow", "query", "-w", "wid", "-qt", "query-type-test"})
s.Equal(1, errorCode)
s.Error(s.app.Run([]string{"", "--do", domainName, "workflow", "query", "-w", "wid", "-qt", "query-type-test"}))
}

var (
Expand Down Expand Up @@ -631,8 +597,7 @@ func (s *cliAppSuite) TestAdminDescribeWorkflow() {

func (s *cliAppSuite) TestAdminDescribeWorkflow_Failed() {
s.serverAdminClient.EXPECT().DescribeWorkflowExecution(gomock.Any(), gomock.Any()).Return(nil, &types.BadRequestError{"faked error"})
errorCode := s.RunErrorExitCode([]string{"", "--do", domainName, "admin", "wf", "describe", "-w", "test-wf-id"})
s.Equal(1, errorCode)
s.Error(s.app.Run(([]string{"", "--do", domainName, "admin", "wf", "describe", "-w", "test-wf-id"})))
}

func (s *cliAppSuite) TestAdminAddSearchAttribute() {
Expand Down
6 changes: 2 additions & 4 deletions tools/cli/domain_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,7 @@ func (d *domainCLIImpl) UpdateDomain(c *cli.Context) error {
updateRequest.SecurityToken = securityToken
_, err := d.updateDomain(ctx, updateRequest)
if err != nil {
if _, ok := err.(*types.EntityNotExistsError); !ok {
return ErrorAndPrint("Operation UpdateDomain failed.", err)
}
return ErrorAndPrint("Operation UpdateDomain failed.", err)
}
fmt.Printf("Domain %s successfully updated.\n", domainName)
return nil
Expand Down Expand Up @@ -425,7 +423,7 @@ func (d *domainCLIImpl) DescribeDomain(c *cli.Context) error {
if _, ok := err.(*types.EntityNotExistsError); !ok {
return ErrorAndPrint("Operation DescribeDomain failed.", err)
}
ErrorAndPrint(fmt.Sprintf("Domain %s does not exist.", domainName), err)
return ErrorAndPrint(fmt.Sprintf("Domain %s does not exist.", domainName), err)
}

if printJSON {
Expand Down
5 changes: 3 additions & 2 deletions tools/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,9 @@ func getFlagsForShowID() []cli.Flag {
func getFlagsForStart() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: FlagTaskList,
Usage: "TaskList",
Name: FlagTaskList,
Aliases: []string{"tl"},
Usage: "TaskList",
},
&cli.StringFlag{
Name: FlagWorkflowID,
Expand Down
2 changes: 1 addition & 1 deletion tools/cli/workflow_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ func getWorkflowIDReusePolicy(value int) *types.WorkflowIDReusePolicy {
return types.WorkflowIDReusePolicy(value).Ptr()
}
// At this point, the policy should return if the value is valid
ErrorAndPrint(fmt.Sprintf("Option %v value is not in supported range.", FlagWorkflowIDReusePolicy), nil)
ErrorAndExit(fmt.Sprintf("Option %v value is not in supported range.", FlagWorkflowIDReusePolicy), nil)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions tools/common/flag/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestParseMultiStringMapFromEnv(t *testing.T) {
_ = os.Setenv("APP_SERVE", "x=y,w=v")
_ = (&cli.App{
Flags: []cli.Flag{
&cli.GenericFlag{Name: "serve, s", Value: &StringMap{}, EnvVars: []string{"APP_SERVE"}},
&cli.GenericFlag{Name: "serve", Aliases: []string{"s"}, Value: &StringMap{}, EnvVars: []string{"APP_SERVE"}},
},
Action: func(ctx *cli.Context) error {
if !reflect.DeepEqual(ctx.Generic("serve"), &StringMap{"x": "y", "w": "v"}) {
Expand All @@ -69,7 +69,7 @@ func TestParseMultiStringMapFromEnvCascade(t *testing.T) {
_ = os.Setenv("APP_FOO", "u=t,r=s")
_ = (&cli.App{
Flags: []cli.Flag{
&cli.GenericFlag{Name: "foos", Value: &StringMap{}, EnvVars: []string{"COMPAT_FOO,APP_FOO"}},
&cli.GenericFlag{Name: "foos", Value: &StringMap{}, EnvVars: []string{"COMPAT_FOO", "APP_FOO"}},
},
Action: func(ctx *cli.Context) error {
if !reflect.DeepEqual(ctx.Generic("foos"), &StringMap{"u": "t", "r": "s"}) {
Expand Down

0 comments on commit 89c3cae

Please sign in to comment.