Skip to content

Commit

Permalink
feat: add mock client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kian99 committed Aug 6, 2024
1 parent 403b6eb commit 7c97c25
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 37 deletions.
8 changes: 4 additions & 4 deletions internal/juju/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
type JujuSuite struct {
suite.Suite

testModelName string
testModelName *string

mockConnection *MockConnection
mockSharedClient *MockSharedClient
}

func (s *JujuSuite) setupMocks(t *testing.T) *gomock.Controller {
s.testModelName = "test-secret-model"
func (s *JujuSuite) setupMocks(t *testing.T, modelName *string) *gomock.Controller {
s.testModelName = modelName

ctlr := gomock.NewController(t)

Expand All @@ -35,7 +35,7 @@ func (s *JujuSuite) setupMocks(t *testing.T) *gomock.Controller {
s.mockSharedClient.EXPECT().Errorf(gomock.Any(), gomock.Any()).Do(log).AnyTimes()
s.mockSharedClient.EXPECT().Tracef(gomock.Any(), gomock.Any()).Do(log).AnyTimes()
s.mockSharedClient.EXPECT().JujuLogger().Return(&jujuLoggerShim{}).AnyTimes()
s.mockSharedClient.EXPECT().GetConnection(&s.testModelName).Return(s.mockConnection, nil).AnyTimes()
s.mockSharedClient.EXPECT().GetConnection(s.testModelName).Return(s.mockConnection, nil).AnyTimes()

return ctlr
}
2 changes: 1 addition & 1 deletion internal/juju/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ type SecretAPIClient interface {
RevokeSecret(uri *secrets.URI, name string, apps []string) ([]error, error)
}

type JaasApiClient interface {
type JaasAPIClient interface {
ListRelationshipTuples(req *jaasparams.ListRelationshipTuplesRequest) (*jaasparams.ListRelationshipTuplesResponse, error)
AddRelation(req *jaasparams.AddRelationRequest) error
RemoveRelation(req *jaasparams.RemoveRelationRequest) error
Expand Down
10 changes: 5 additions & 5 deletions internal/juju/jaas.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import (

type jaasClient struct {
SharedClient
getJaasApiClient func(jujuapi.Connection) JaasApiClient
getJaasApiClient func(jujuapi.Connection) JaasAPIClient
}

func newJaasClient(sc SharedClient) *jaasClient {
return &jaasClient{
SharedClient: sc,
getJaasApiClient: func(conn jujuapi.Connection) JaasApiClient {
getJaasApiClient: func(conn jujuapi.Connection) JaasAPIClient {
return api.NewClient(conn)
},
}
}

func (jc *jaasClient) AddTuples(tuples []params.RelationshipTuple) error {
func (jc *jaasClient) AddRelations(tuples []params.RelationshipTuple) error {
conn, err := jc.GetConnection(nil)
if err != nil {
return err
Expand All @@ -35,7 +35,7 @@ func (jc *jaasClient) AddTuples(tuples []params.RelationshipTuple) error {
return cl.AddRelation(&req)
}

func (jc *jaasClient) DeleteTuples(tuples []params.RelationshipTuple) error {
func (jc *jaasClient) DeleteRelations(tuples []params.RelationshipTuple) error {
conn, err := jc.GetConnection(nil)
if err != nil {
return err
Expand All @@ -48,7 +48,7 @@ func (jc *jaasClient) DeleteTuples(tuples []params.RelationshipTuple) error {
return cl.RemoveRelation(&req)
}

func (jc *jaasClient) ReadRelation(tuple *params.RelationshipTuple) ([]params.RelationshipTuple, error) {
func (jc *jaasClient) ReadRelations(tuple *params.RelationshipTuple) ([]params.RelationshipTuple, error) {
if tuple == nil {
return nil, errors.New("add relation request nil")
}
Expand Down
99 changes: 92 additions & 7 deletions internal/juju/jaas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package juju
import (
"testing"

"github.com/canonical/jimm-go-sdk/v3/api/params"
"github.com/juju/juju/api"
"github.com/stretchr/testify/suite"
"go.uber.org/mock/gomock"
)
Expand All @@ -11,18 +13,101 @@ type JaasSuite struct {
suite.Suite
JujuSuite

testModelName string

mockJaasClient *MockSecretAPIClient
mockJaasClient *MockJaasAPIClient
}

func (s *JaasSuite) SetupTest() {}

func (s *JaasSuite) setupMocks(t *testing.T) *gomock.Controller {
s.testModelName = "test-secret-model"

ctlr := s.JujuSuite.setupMocks(t)
s.mockJaasClient = NewMockSecretAPIClient(ctlr)
ctlr := s.JujuSuite.setupMocks(t, nil)
s.mockJaasClient = NewMockJaasAPIClient(ctlr)

return ctlr
}

func (s *JaasSuite) getJaasClient() jaasClient {
return jaasClient{
SharedClient: s.JujuSuite.mockSharedClient,
getJaasApiClient: func(connection api.Connection) JaasAPIClient {
return s.mockJaasClient
},
}
}

func (s *JaasSuite) TestAddRelations() {
ctlr := s.setupMocks(s.T())
defer ctlr.Finish()

tuples := []params.RelationshipTuple{
{Object: "object-1", Relation: "relation", TargetObject: "target-1"},
{Object: "object-2", Relation: "relation", TargetObject: "target-2"},
}
req := params.AddRelationRequest{
Tuples: tuples,
}

s.mockJaasClient.EXPECT().AddRelation(
&req,
).Return(nil).Times(1)

client := s.getJaasClient()
err := client.AddRelations(tuples)
s.Require().NoError(err)
}

func (s *JaasSuite) TestDeleteRelations() {
ctlr := s.setupMocks(s.T())
defer ctlr.Finish()

tuples := []params.RelationshipTuple{
{Object: "object-1", Relation: "relation", TargetObject: "target-1"},
{Object: "object-2", Relation: "relation", TargetObject: "target-2"},
}
req := params.RemoveRelationRequest{
Tuples: tuples,
}

s.mockJaasClient.EXPECT().RemoveRelation(
&req,
).Return(nil).Times(1)

client := s.getJaasClient()
err := client.DeleteRelations(tuples)
s.Require().NoError(err)
}

func (s *JaasSuite) TestReadRelations() {
ctlr := s.setupMocks(s.T())
defer ctlr.Finish()

tuple := params.RelationshipTuple{Object: "object-1", Relation: "relation", TargetObject: "target-1"}
// 1st request/response has no token in the request and a token in the response indicating another page is available.
req := &params.ListRelationshipTuplesRequest{Tuple: tuple}
respWithToken := &params.ListRelationshipTuplesResponse{
Tuples: []params.RelationshipTuple{tuple},
ContinuationToken: "token",
}
s.mockJaasClient.EXPECT().ListRelationshipTuples(
req,
).Return(respWithToken, nil).Times(1)
// 2nd request/response has the previous token in the request and no token in the response, indicating all pages have been consumed.
reqWithToken := &params.ListRelationshipTuplesRequest{Tuple: tuple, ContinuationToken: "token"}
respWithoutToken := &params.ListRelationshipTuplesResponse{
Tuples: []params.RelationshipTuple{tuple},
ContinuationToken: "",
}
s.mockJaasClient.EXPECT().ListRelationshipTuples(
reqWithToken,
).Return(respWithoutToken, nil).Times(1)

client := s.getJaasClient()
relations, err := client.ReadRelations(&tuple)
s.Require().NoError(err)
s.Require().Len(relations, 2)
}

// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestJaasSuite(t *testing.T) {
suite.Run(t, new(JaasSuite))
}
101 changes: 84 additions & 17 deletions internal/juju/mock_test.go

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

Loading

0 comments on commit 7c97c25

Please sign in to comment.