Skip to content

Commit

Permalink
More tests for admin_commands.go
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrotx committed Nov 5, 2024
1 parent 4c8b3f8 commit a7700c3
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 7 deletions.
17 changes: 10 additions & 7 deletions tools/cli/admin_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ func AdminDeleteWorkflow(c *cli.Context) error {
func AdminGetDomainIDOrName(c *cli.Context) error {
domainID := c.String(FlagDomainID)
domainName := c.String(FlagDomain)
if len(domainID) == 0 && len(domainName) == 0 {

if (len(domainID) == 0 && len(domainName) == 0) || (len(domainID) != 0 && len(domainName) != 0) {
return commoncli.Problem("Need either domainName or domainID", nil)
}

Expand All @@ -396,18 +397,20 @@ func AdminGetDomainIDOrName(c *cli.Context) error {
if err != nil {
return commoncli.Problem("Error in creating context: ", err)
}

output := getDeps(c).Output()
if len(domainID) > 0 {
domain, err := domainManager.GetDomain(ctx, &persistence.GetDomainRequest{ID: domainID})
res, err := domainManager.GetDomain(ctx, &persistence.GetDomainRequest{ID: domainID})
if err != nil {
return commoncli.Problem("SelectDomain error", err)
return commoncli.Problem("GetDomain error", err)
}
fmt.Printf("domainName for domainID %v is %v \n", domainID, domain.Info.Name)
fmt.Fprintf(output, "domainName for domainID %v is %v\n", domainID, res.Info.Name)
} else {
domain, err := domainManager.GetDomain(ctx, &persistence.GetDomainRequest{Name: domainName})
res, err := domainManager.GetDomain(ctx, &persistence.GetDomainRequest{Name: domainName})
if err != nil {
return commoncli.Problem("SelectDomain error", err)
return commoncli.Problem("GetDomain error", err)
}
fmt.Printf("domainID for domainName %v is %v \n", domain.Info.ID, domainID)
fmt.Fprintf(output, "domainID for domainName %v is %v\n", domainName, res.Info.ID)
}
return nil
}
Expand Down
160 changes: 160 additions & 0 deletions tools/cli/admin_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"time"

Expand All @@ -46,6 +47,7 @@ const (
testShardID = 1234
testCluster = "test-cluster"
testDomain = "test-domain"
testDomainID = "0000001-0000002-0000003"
testTaskList = "test-tasklist"
testTaskListType = "decision"
testQueueType = 2 // transfer queue
Expand Down Expand Up @@ -1225,3 +1227,161 @@ func TestAdminSetShardRangeID(t *testing.T) {
})
}
}

func TestAdminGetDomainIDOrName(t *testing.T) {
tests := []struct {
name string
testSetup func(td *cliTestData) *cli.Context
errContains string // empty if no error is expected
expectedOutput string
}{
{
name: "no DomainID or DomainName argument",
testSetup: func(td *cliTestData) *cli.Context {
return clitest.NewCLIContext(t, td.app /* arguments are missing */)
},
errContains: "Need either domainName or domainID",
},
{
name: "DomainID provided, valid response",
testSetup: func(td *cliTestData) *cli.Context {
cliCtx := clitest.NewCLIContext(
t,
td.app,
clitest.StringArgument(FlagDomainID, testDomainID),
)

mockDomainManager := persistence.NewMockDomainManager(td.ctrl)
mockDomainManager.EXPECT().GetDomain(
gomock.Any(),
&persistence.GetDomainRequest{ID: testDomainID},
).Return(&persistence.GetDomainResponse{
Info: &persistence.DomainInfo{
ID: testDomainID,
Name: testDomain,
},
}, nil)

td.mockManagerFactory.EXPECT().initializeDomainManager(gomock.Any()).
Return(mockDomainManager, nil)

return cliCtx
},
errContains: "",
expectedOutput: fmt.Sprintf("domainName for domainID %v is %v\n", testDomainID, testDomain),
},
{
name: "DomainName provided, valid response",
testSetup: func(td *cliTestData) *cli.Context {
cliCtx := clitest.NewCLIContext(
t,
td.app,
clitest.StringArgument(FlagDomain, testDomain),
)

mockDomainManager := persistence.NewMockDomainManager(td.ctrl)
mockDomainManager.EXPECT().GetDomain(
gomock.Any(),
&persistence.GetDomainRequest{Name: testDomain},
).Return(&persistence.GetDomainResponse{
Info: &persistence.DomainInfo{
ID: testDomainID,
Name: testDomain,
},
}, nil)

td.mockManagerFactory.EXPECT().initializeDomainManager(gomock.Any()).
Return(mockDomainManager, nil)

return cliCtx
},
errContains: "",
expectedOutput: fmt.Sprintf("domainID for domainName %v is %v\n", testDomain, testDomainID),
},
{
name: "DomainManager returns an error for domainID",
testSetup: func(td *cliTestData) *cli.Context {
cliCtx := clitest.NewCLIContext(
t,
td.app,
clitest.StringArgument(FlagDomainID, testDomainID),
)

mockDomainManager := persistence.NewMockDomainManager(td.ctrl)
mockDomainManager.EXPECT().GetDomain(gomock.Any(), gomock.Any()).
Return(nil, errors.New("critical error"))

td.mockManagerFactory.EXPECT().initializeDomainManager(gomock.Any()).
Return(mockDomainManager, nil)

return cliCtx
},
errContains: "GetDomain error",
},
{
name: "DomainManager returns an error for domainName",
testSetup: func(td *cliTestData) *cli.Context {
cliCtx := clitest.NewCLIContext(
t,
td.app,
clitest.StringArgument(FlagDomain, testDomain),
)

mockDomainManager := persistence.NewMockDomainManager(td.ctrl)
mockDomainManager.EXPECT().GetDomain(gomock.Any(), gomock.Any()).
Return(nil, errors.New("critical error"))

td.mockManagerFactory.EXPECT().initializeDomainManager(gomock.Any()).
Return(mockDomainManager, nil)

return cliCtx
},
errContains: "GetDomain error",
},
{
name: "failed to initializeDomainManager",
testSetup: func(td *cliTestData) *cli.Context {
cliCtx := clitest.NewCLIContext(
t,
td.app,
clitest.StringArgument(FlagDomainID, testDomainID),
)

td.mockManagerFactory.EXPECT().initializeDomainManager(gomock.Any()).
Return(nil, errors.New("failed to initializeDomainManager"))

return cliCtx
},
errContains: "failed to initializeDomainManager",
},
{
name: "both DomainID and DomainName provided",
testSetup: func(td *cliTestData) *cli.Context {
cliCtx := clitest.NewCLIContext(
t,
td.app,
clitest.StringArgument(FlagDomainID, testDomainID),
clitest.StringArgument(FlagDomain, testDomain),
)

return cliCtx
},
errContains: "Need either domainName or domainID", // Expecting the error when both are provided
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
td := newCLITestData(t)
cliCtx := tt.testSetup(td)

err := AdminGetDomainIDOrName(cliCtx)
if tt.errContains == "" {
assert.NoError(t, err)
} else {
assert.ErrorContains(t, err, tt.errContains)
}
assert.Equal(t, tt.expectedOutput, td.consoleOutput())
})
}
}

0 comments on commit a7700c3

Please sign in to comment.