Skip to content

Commit

Permalink
feat: add test for EnsureAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalgalaw committed Jan 8, 2024
1 parent faecbb4 commit 104c752
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 0 deletions.
69 changes: 69 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// SPDX-License-Identifier: MIT

package client

import (
"testing"

"github.com/cloudbase/garm/client/enterprises"
"github.com/cloudbase/garm/client/instances"
"github.com/cloudbase/garm/params"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"

"github.com/mercedes-benz/garm-operator/pkg/client/mock"
"github.com/mercedes-benz/garm-operator/pkg/metrics"
)

func TestBaseClient_Reauthenticate(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

expectGarmRequest := func(m *mock.MockEnterpriseClientMockRecorder, m1 *mock.MockGarmClientMockRecorder) {
m.GetEnterprise(enterprises.NewGetEnterpriseParams().
WithEnterpriseID("e1dbf9a6-a9f6-4594-a5ac-ae78a8f27a3e"),
).Return(nil, instances.NewGetInstanceDefault(401))

m1.Init().Return(nil)
m1.Login().Return(nil)

m.GetEnterprise(
enterprises.NewGetEnterpriseParams().
WithEnterpriseID("e1dbf9a6-a9f6-4594-a5ac-ae78a8f27a3e"),
).Return(&enterprises.GetEnterpriseOK{
Payload: params.Enterprise{
ID: "e1dbf9a6-a9f6-4594-a5ac-ae78a8f27a3e",
Name: "existing-enterprise",
CredentialsName: "foobar",
},
}, nil)
}

expectedResult := &enterprises.GetEnterpriseOK{
Payload: params.Enterprise{
ID: "e1dbf9a6-a9f6-4594-a5ac-ae78a8f27a3e",
Name: "existing-enterprise",
CredentialsName: "foobar",
},
}

mockEnterpriseClient := mock.NewMockEnterpriseClient(mockCtrl)
mockBaseClient := mock.NewMockGarmClient(mockCtrl)
Client = mockBaseClient

expectGarmRequest(mockEnterpriseClient.EXPECT(), mockBaseClient.EXPECT())

result, err := EnsureAuth[*enterprises.GetEnterpriseOK](func() (*enterprises.GetEnterpriseOK, error) {
metrics.TotalGarmCalls.WithLabelValues("enterprises.Get").Inc()

enterprise, err := mockEnterpriseClient.GetEnterprise(enterprises.NewGetEnterpriseParams().WithEnterpriseID("e1dbf9a6-a9f6-4594-a5ac-ae78a8f27a3e"))
if err != nil {
metrics.GarmCallErrors.WithLabelValues("enterprises.Get").Inc()
return nil, err
}
return enterprise, nil
})

assert.Equal(t, expectedResult, result)
assert.Equal(t, nil, err)
}
93 changes: 93 additions & 0 deletions pkg/client/mock/client.go

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

2 changes: 2 additions & 0 deletions pkg/client/mock/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ package mock
//go:generate /usr/bin/env bash -c "cat ../../../hack/boilerplate.go.txt instance.go > _instance.go && mv _instance.go instance.go"
//go:generate ../../../bin/mockgen -package mock -destination=repository.go -source=../repository.go Repository
//go:generate /usr/bin/env bash -c "cat ../../../hack/boilerplate.go.txt repository.go > _repository.go && mv _repository.go repository.go"
//go:generate ../../../bin/mockgen -package mock -destination=client.go -source=../client.go GarmClient
//go:generate /usr/bin/env bash -c "cat ../../../hack/boilerplate.go.txt client.go > _client.go && mv _client.go client.go"

0 comments on commit 104c752

Please sign in to comment.