Skip to content

Commit 7d81bd2

Browse files
efiacorliamfallon
andauthored
Add mockery test case to token reconciler (#446)
Add mock nephio gitea client. Add example test case fro deketeToken function. Anti pattern arises in relation to the gitea client wrapper as it's also being used by the repo reconciler. --------- Signed-off-by: efiacor <[email protected]> Co-authored-by: Liam Fallon <[email protected]>
1 parent 3ddd4e2 commit 7d81bd2

File tree

7 files changed

+186
-2
lines changed

7 files changed

+186
-2
lines changed

controllers/pkg/.mockery.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packages:
2+
github.com/nephio-project/nephio/controllers/pkg/giteaclient:
3+
interfaces:
4+
GiteaClient:
5+
config:
6+
dir: "{{.InterfaceDir}}"

controllers/pkg/giteaclient/giteaclient.go

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type GiteaClient interface {
4040
GetRepo(userName string, repoCRName string) (*gitea.Repository, *gitea.Response, error)
4141
CreateRepo(createRepoOption gitea.CreateRepoOption) (*gitea.Repository, *gitea.Response, error)
4242
EditRepo(userName string, repoCRName string, editRepoOption gitea.EditRepoOption) (*gitea.Repository, *gitea.Response, error)
43+
DeleteAccessToken(value interface{}) (*gitea.Response, error)
4344
}
4445

4546
var lock = &sync.Mutex{}
@@ -166,3 +167,7 @@ func (r *gc) CreateRepo(createRepoOption gitea.CreateRepoOption) (*gitea.Reposit
166167
func (r *gc) EditRepo(userName string, repoCRName string, editRepoOption gitea.EditRepoOption) (*gitea.Repository, *gitea.Response, error) {
167168
return r.giteaClient.EditRepo(userName, repoCRName, editRepoOption)
168169
}
170+
171+
func (r *gc) DeleteAccessToken(value interface{}) (*gitea.Response, error) {
172+
return r.giteaClient.DeleteAccessToken(value)
173+
}

controllers/pkg/giteaclient/mock_GiteaClient.go

+55
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controllers/pkg/reconcilers/repository/reconciler_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type repoTest struct {
4949
wantErr bool
5050
}
5151

52+
5253
func TestUpsertRepo(t *testing.T) {
5354
dummyString := "Dummy String"
5455
dummyBool := true

controllers/pkg/reconcilers/token/reconciler.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
118118
// Delete the token from the git server
119119
// when successful remove the finalizer
120120
if cr.Spec.Lifecycle.DeletionPolicy == commonv1alpha1.DeletionDelete {
121-
if err := r.deleteToken(ctx, giteaClient, cr); err != nil {
121+
if err := r.deleteToken(ctx, r.giteaClient, cr); err != nil {
122122
return ctrl.Result{Requeue: true}, errors.Wrap(r.Status().Update(ctx, cr), errUpdateStatus)
123123
}
124124
}
@@ -219,7 +219,7 @@ func (r *reconciler) createToken(ctx context.Context, giteaClient *gitea.Client,
219219
return nil
220220
}
221221

222-
func (r *reconciler) deleteToken(ctx context.Context, giteaClient *gitea.Client, cr *infrav1alpha1.Token) error {
222+
func (r *reconciler) deleteToken(ctx context.Context, giteaClient giteaclient.GiteaClient, cr *infrav1alpha1.Token) error {
223223
_, err := giteaClient.DeleteAccessToken(cr.GetTokenName())
224224
if err != nil {
225225
log.FromContext(ctx).Error(err, "cannot delete token")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2023 The Nephio Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package token
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"github.com/go-logr/logr"
21+
"github.com/nephio-project/nephio/controllers/pkg/giteaclient"
22+
"github.com/nephio-project/nephio/controllers/pkg/resource"
23+
"github.com/stretchr/testify/mock"
24+
"sigs.k8s.io/controller-runtime/pkg/log"
25+
"testing"
26+
infrav1alpha1 "github.com/nephio-project/api/infra/v1alpha1"
27+
)
28+
29+
func TestDeleteToken(t *testing.T) {
30+
type mockHelper struct {
31+
methodName string
32+
argType []string
33+
retArgList []interface{}
34+
}
35+
type fields struct {
36+
APIPatchingApplicator resource.APIPatchingApplicator
37+
giteaClient giteaclient.GiteaClient
38+
finalizer *resource.APIFinalizer
39+
l logr.Logger
40+
}
41+
type args struct {
42+
ctx context.Context
43+
giteaClient giteaclient.GiteaClient
44+
cr *infrav1alpha1.Token
45+
}
46+
tests := []struct {
47+
name string
48+
fields fields
49+
args args
50+
mocks []mockHelper
51+
wantErr bool
52+
}{
53+
{
54+
name: "Delete Access token reports error",
55+
fields: fields{resource.NewAPIPatchingApplicator(nil), nil, nil, log.FromContext(nil)},
56+
args: args{nil, nil, &infrav1alpha1.Token{}},
57+
mocks: []mockHelper{
58+
{"DeleteAccessToken", []string{"string"}, []interface{}{nil, fmt.Errorf("\"username\" not set: only BasicAuth allowed")}},
59+
},
60+
wantErr: true,
61+
},
62+
{
63+
name: "Delete Access token success",
64+
fields: fields{resource.NewAPIPatchingApplicator(nil), nil, nil, log.FromContext(nil)},
65+
args: args{nil, nil, &infrav1alpha1.Token{}},
66+
mocks: []mockHelper{
67+
{"DeleteAccessToken", []string{"string"}, []interface{}{nil, nil}},
68+
},
69+
wantErr: false,
70+
},
71+
}
72+
for _, tt := range tests {
73+
t.Run(tt.name, func(t *testing.T) {
74+
r := &reconciler{
75+
APIPatchingApplicator: tt.fields.APIPatchingApplicator,
76+
giteaClient: tt.fields.giteaClient,
77+
finalizer: tt.fields.finalizer,
78+
}
79+
// The below block being setup and processing of mocks before invoking the function to be tested
80+
mockGClient := new(giteaclient.MockGiteaClient)
81+
tt.args.giteaClient = mockGClient
82+
tt.fields.giteaClient = mockGClient
83+
for counter := range tt.mocks {
84+
call := mockGClient.Mock.On(tt.mocks[counter].methodName)
85+
for _, arg := range tt.mocks[counter].argType {
86+
call.Arguments = append(call.Arguments, mock.AnythingOfType(arg))
87+
}
88+
for _, ret := range tt.mocks[counter].retArgList {
89+
call.ReturnArguments = append(call.ReturnArguments, ret)
90+
}
91+
}
92+
93+
if err := r.deleteToken(tt.args.ctx, tt.args.giteaClient, tt.args.cr); (err != nil) != tt.wantErr {
94+
t.Errorf("deleteToken() error = %v, wantErr %v", err, tt.wantErr)
95+
}
96+
})
97+
}
98+
}

default-go-test.mk

+19
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414

1515

1616
GO_VERSION ?= 1.20.2
17+
MOCKERY_VERSION=2.37.1
1718
TEST_COVERAGE_FILE=lcov.info
1819
TEST_COVERAGE_HTML_FILE=coverage_unit.html
1920
TEST_COVERAGE_FUNC_FILE=func_coverage.out
2021
GIT_ROOT_DIR ?= $(dir $(lastword $(MAKEFILE_LIST)))
22+
OS_ARCH ?= $(shell uname -m)
23+
OS ?= $(shell uname)
2124
include $(GIT_ROOT_DIR)/detect-container-runtime.mk
2225

2326
.PHONY: unit
@@ -36,6 +39,22 @@ else
3639
go tool cover -func=${TEST_COVERAGE_FILE} -o ${TEST_COVERAGE_FUNC_FILE}
3740
endif
3841

42+
.PHONY: install-mockery
43+
install-mockery: ## install mockery
44+
ifeq ($(CONTAINER_RUNNABLE), 0)
45+
$(CONTAINER_RUNTIME) pull docker.io/vektra/mockery:v${MOCKERY_VERSION}
46+
else
47+
wget -qO- https://github.com/vektra/mockery/releases/download/v${MOCKERY_VERSION}/mockery_${MOCKERY_VERSION}_${OS}_${OS_ARCH}.tar.gz | sudo tar -xvzf - -C /usr/local/bin
48+
endif
49+
50+
.PHONY: generate-mocks
51+
generate-mocks:
52+
ifeq ($(CONTAINER_RUNNABLE), 0)
53+
$(CONTAINER_RUNTIME) run --security-opt label=disable -v ${PWD}:/src -w /src docker.io/vektra/mockery:v${MOCKERY_VERSION}
54+
else
55+
mockery
56+
endif
57+
3958
.PHONY: unit-clean
4059
unit-clean: ## Clean up the artifacts created by the unit tests
4160
ifeq ($(CONTAINER_RUNNABLE), 0)

0 commit comments

Comments
 (0)