Skip to content

Commit

Permalink
add console client mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz committed Oct 9, 2023
1 parent 530d764 commit 2d44f2e
Show file tree
Hide file tree
Showing 6 changed files with 514 additions and 6 deletions.
12 changes: 9 additions & 3 deletions cmd/plural/cd.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ func (p *Plural) handleListCDRepositories(_ *cli.Context) error {
if err != nil {
return err
}

if repos == nil {
return fmt.Errorf("returned objects list [ListRepositories] is nil")
}
headers := []string{"ID", "URL", "Status", "Error"}
return utils.PrintTable(repos.GitRepositories.Edges, headers, func(r *gqlclient.GitRepositoryEdgeFragment) ([]string, error) {
return []string{r.Node.ID, r.Node.URL, string(*r.Node.Health), lo.FromPtr(r.Node.Error)}, nil
Expand All @@ -243,7 +245,9 @@ func (p *Plural) handleListClusterServices(c *cli.Context) error {
if err != nil {
return err
}

if sd == nil {
return fmt.Errorf("returned objects list [ListClusterServices] is nil")
}
headers := []string{"Id", "Name", "Namespace", "Git Ref", "Git Folder", "Repo"}
return utils.PrintTable(sd, headers, func(sd *gqlclient.ServiceDeploymentEdgeFragment) ([]string, error) {
return []string{sd.Node.ID, sd.Node.Name, sd.Node.Namespace, sd.Node.Git.Ref, sd.Node.Git.Folder, sd.Node.Repository.URL}, nil
Expand Down Expand Up @@ -449,7 +453,9 @@ func (p *Plural) handleListClusters(_ *cli.Context) error {
if err != nil {
return err
}

if clusters == nil {
return fmt.Errorf("returned objects list [ListClusters] is nil")
}
headers := []string{"Id", "Name", "Handle", "Version", "Provider"}
return utils.PrintTable(clusters.Clusters.Edges, headers, func(cl *gqlclient.ClusterEdgeFragment) ([]string, error) {
provider := ""
Expand Down
188 changes: 188 additions & 0 deletions cmd/plural/cd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package plural_test

import (
"os"
"testing"

consoleclient "github.com/pluralsh/console-client-go"
plural "github.com/pluralsh/plural/cmd/plural"
"github.com/pluralsh/plural/pkg/test/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestListCDClusters(t *testing.T) {
tests := []struct {
name string
args []string
expectedResponse string
result *consoleclient.ListClusters
}{
{
name: `test "deployments clusters list" when returns nil object`,
result: nil,
args: []string{plural.ApplicationName, "deployments", "clusters", "list"},
expectedResponse: `returned objects list [ListClusters] is nil`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
client := mocks.NewConsoleClient(t)
client.On("ListClusters").Return(test.result, nil)
app := plural.CreateNewApp(&plural.Plural{
Client: nil,
ConsoleClient: client,
Kube: nil,
HelmConfiguration: nil,
})
app.HelpName = plural.ApplicationName
os.Args = test.args
_, err := captureStdout(app, os.Args)

assert.Error(t, err)
assert.Equal(t, test.expectedResponse, err.Error())
})
}
}

func TestDescribeCDCluster(t *testing.T) {
tests := []struct {
name string
args []string
expectedResponse string
expectedError string
result *consoleclient.GetCluster
}{
{
name: `test "deployments clusters describe" when returns nil`,
result: nil,
args: []string{plural.ApplicationName, "deployments", "clusters", "describe", "abc"},
expectedError: `existing cluster is empty`,
},
{
name: `test "deployments clusters describe"`,
result: &consoleclient.GetCluster{
Cluster: &consoleclient.ClusterFragment{
ID: "abc",
Name: "test",
},
},
args: []string{plural.ApplicationName, "deployments", "clusters", "describe", "abc"},
expectedResponse: `cluster:
currentVersion: null
handle: null
id: abc
name: test
nodePools: null
pingedAt: null
provider: null
self: null
version: null
`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
client := mocks.NewConsoleClient(t)
client.On("GetCluster", mock.AnythingOfType("string")).Return(test.result, nil)
app := plural.CreateNewApp(&plural.Plural{
Client: nil,
ConsoleClient: client,
Kube: nil,
HelmConfiguration: nil,
})
app.HelpName = plural.ApplicationName
os.Args = test.args
out, err := captureStdout(app, os.Args)

if test.expectedError != "" {
assert.Error(t, err)
assert.Equal(t, test.expectedError, err.Error())
}
if test.expectedResponse != "" {
assert.Equal(t, test.expectedResponse, out)
}
})
}
}

func TestListCDRepositories(t *testing.T) {
tests := []struct {
name string
args []string
expectedResponse string
expectedError string
result *consoleclient.ListGitRepositories
}{
{
name: `test "deployments repositories list" when returns nil`,
result: nil,
args: []string{plural.ApplicationName, "deployments", "repositories", "list"},
expectedError: `returned objects list [ListRepositories] is nil`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
client := mocks.NewConsoleClient(t)
client.On("ListRepositories").Return(test.result, nil)
app := plural.CreateNewApp(&plural.Plural{
Client: nil,
ConsoleClient: client,
Kube: nil,
HelmConfiguration: nil,
})
app.HelpName = plural.ApplicationName
os.Args = test.args
out, err := captureStdout(app, os.Args)

if test.expectedError != "" {
assert.Error(t, err)
assert.Equal(t, test.expectedError, err.Error())
}
if test.expectedResponse != "" {
assert.Equal(t, test.expectedResponse, out)
}
})
}
}

func TestListCDServices(t *testing.T) {
tests := []struct {
name string
args []string
expectedResponse string
expectedError string
result []*consoleclient.ServiceDeploymentEdgeFragment
}{
{
name: `test "deployments services list" when returns nil`,
result: nil,
args: []string{plural.ApplicationName, "deployments", "services", "list", "clusterID"},
expectedError: `returned objects list [ListClusterServices] is nil`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
client := mocks.NewConsoleClient(t)
client.On("ListClusterServices", mock.AnythingOfType("*string"), mock.AnythingOfType("*string")).Return(test.result, nil)
app := plural.CreateNewApp(&plural.Plural{
Client: nil,
ConsoleClient: client,
Kube: nil,
HelmConfiguration: nil,
})
app.HelpName = plural.ApplicationName
os.Args = test.args
out, err := captureStdout(app, os.Args)

if test.expectedError != "" {
assert.Error(t, err)
assert.Equal(t, test.expectedError, err.Error())
}
if test.expectedResponse != "" {
assert.Equal(t, test.expectedResponse, out)
}
})
}
}
3 changes: 2 additions & 1 deletion hack/gen-client-mocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ cd $(dirname $0)/..

source hack/lib.sh

CONTAINERIZE_IMAGE=golang:1.18.4 containerize ./hack/gen-client-mocks.sh
CONTAINERIZE_IMAGE=golang:1.21.1 containerize ./hack/gen-client-mocks.sh

go run github.com/vektra/mockery/v2@latest --dir=pkg/api/ --name=Client --output=pkg/test/mocks
go run github.com/vektra/mockery/v2@latest --dir=pkg/kubernetes --name=Kube --output=pkg/test/mocks
go run github.com/vektra/mockery/v2@latest --dir=pkg/console --name=ConsoleClient --output=pkg/test/mocks
2 changes: 1 addition & 1 deletion pkg/test/mocks/Client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2d44f2e

Please sign in to comment.