Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mockery test case to token reconciler #446

Merged
merged 27 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
15edf23
Optimize logging for repo controller
efiacor Nov 13, 2023
b816d10
Revert removal of finalizer const
efiacor Nov 14, 2023
349f652
Fix formatting
efiacor Nov 14, 2023
9b3a5ae
Add tests using mockery to token reconciler
efiacor Nov 27, 2023
ddd53f4
Merge branch 'nephio-project:main' into token_rec_mockery
efiacor Nov 27, 2023
c1c9fc8
Merge branch 'nephio-project:main' into token_rec_mockery
efiacor Nov 28, 2023
e855c21
Rebase on main
efiacor Nov 28, 2023
ac45014
Merge branch 'main' into token_rec_mockery
liamfallon Dec 4, 2023
ad40b41
Fix go.mod
efiacor Dec 5, 2023
5a4fe15
Merge branch 'main' into token_rec_mockery
efiacor Dec 5, 2023
6b29826
Add testify lib to operators dir mod
efiacor Dec 5, 2023
0748619
Add missing method from interface
efiacor Dec 5, 2023
a6cb358
Merge branch 'nephio-project:main' into token_rec_mockery
efiacor Jan 12, 2024
0181f6f
Optimize logging for repo controller
efiacor Nov 13, 2023
9e9e0c3
Revert removal of finalizer const
efiacor Nov 14, 2023
70dcd07
Add tests using mockery to token reconciler
efiacor Nov 27, 2023
86ea699
Fix go.mod
efiacor Dec 5, 2023
da3c2e0
Fix rebase errors
efiacor Jan 12, 2024
24e8bd8
Update go.mod
efiacor Jan 12, 2024
5e8317d
Revert removal of finalizer const
efiacor Nov 14, 2023
5fcd6f1
Add tests using mockery to token reconciler
efiacor Nov 27, 2023
3593345
Fix go.mod
efiacor Dec 5, 2023
82bb12d
Add missing interface method mock
efiacor Jan 12, 2024
66534d7
Fix mod.go again..
efiacor Jan 12, 2024
ba6cd12
Fix gofmt
efiacor Jan 12, 2024
96a491d
Merge branch 'main' into token_rec_mockery
efiacor Feb 12, 2024
6572b57
Fix rebase conflicts
efiacor Feb 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions controllers/pkg/.mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
packages:
github.com/nephio-project/nephio/controllers/pkg/giteaclient:
interfaces:
GiteaClient:
config:
dir: "{{.InterfaceDir}}"
5 changes: 5 additions & 0 deletions controllers/pkg/giteaclient/giteaclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type GiteaClient interface {
GetRepo(userName string, repoCRName string) (*gitea.Repository, *gitea.Response, error)
CreateRepo(createRepoOption gitea.CreateRepoOption) (*gitea.Repository, *gitea.Response, error)
EditRepo(userName string, repoCRName string, editRepoOption gitea.EditRepoOption) (*gitea.Repository, *gitea.Response, error)
DeleteAccessToken(value interface{}) (*gitea.Response, error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious as to why is this an interface{}?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

var lock = &sync.Mutex{}
Expand Down Expand Up @@ -166,3 +167,7 @@ func (r *gc) CreateRepo(createRepoOption gitea.CreateRepoOption) (*gitea.Reposit
func (r *gc) EditRepo(userName string, repoCRName string, editRepoOption gitea.EditRepoOption) (*gitea.Repository, *gitea.Response, error) {
return r.giteaClient.EditRepo(userName, repoCRName, editRepoOption)
}

func (r *gc) DeleteAccessToken(value interface{}) (*gitea.Response, error) {
return r.giteaClient.DeleteAccessToken(value)
}
55 changes: 55 additions & 0 deletions controllers/pkg/giteaclient/mock_GiteaClient.go

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

1 change: 1 addition & 0 deletions controllers/pkg/reconcilers/repository/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type repoTest struct {
wantErr bool
}


func TestUpsertRepo(t *testing.T) {
dummyString := "Dummy String"
dummyBool := true
Expand Down
4 changes: 2 additions & 2 deletions controllers/pkg/reconcilers/token/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
// Delete the token from the git server
// when successful remove the finalizer
if cr.Spec.Lifecycle.DeletionPolicy == commonv1alpha1.DeletionDelete {
if err := r.deleteToken(ctx, giteaClient, cr); err != nil {
if err := r.deleteToken(ctx, r.giteaClient, cr); err != nil {
return ctrl.Result{Requeue: true}, errors.Wrap(r.Status().Update(ctx, cr), errUpdateStatus)
}
}
Expand Down Expand Up @@ -219,7 +219,7 @@ func (r *reconciler) createToken(ctx context.Context, giteaClient *gitea.Client,
return nil
}

func (r *reconciler) deleteToken(ctx context.Context, giteaClient *gitea.Client, cr *infrav1alpha1.Token) error {
func (r *reconciler) deleteToken(ctx context.Context, giteaClient giteaclient.GiteaClient, cr *infrav1alpha1.Token) error {
_, err := giteaClient.DeleteAccessToken(cr.GetTokenName())
if err != nil {
log.FromContext(ctx).Error(err, "cannot delete token")
Expand Down
98 changes: 98 additions & 0 deletions controllers/pkg/reconcilers/token/reconciler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2023 The Nephio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package token

import (
"context"
"fmt"
"github.com/go-logr/logr"
"github.com/nephio-project/nephio/controllers/pkg/giteaclient"
"github.com/nephio-project/nephio/controllers/pkg/resource"
"github.com/stretchr/testify/mock"
"sigs.k8s.io/controller-runtime/pkg/log"
"testing"
infrav1alpha1 "github.com/nephio-project/api/infra/v1alpha1"
)

func TestDeleteToken(t *testing.T) {
type mockHelper struct {
methodName string
argType []string
retArgList []interface{}
}
type fields struct {
APIPatchingApplicator resource.APIPatchingApplicator
giteaClient giteaclient.GiteaClient
finalizer *resource.APIFinalizer
l logr.Logger
}
type args struct {
ctx context.Context
giteaClient giteaclient.GiteaClient
cr *infrav1alpha1.Token
}
tests := []struct {
name string
fields fields
args args
mocks []mockHelper
wantErr bool
}{
{
name: "Delete Access token reports error",
fields: fields{resource.NewAPIPatchingApplicator(nil), nil, nil, log.FromContext(nil)},
args: args{nil, nil, &infrav1alpha1.Token{}},
mocks: []mockHelper{
{"DeleteAccessToken", []string{"string"}, []interface{}{nil, fmt.Errorf("\"username\" not set: only BasicAuth allowed")}},
},
wantErr: true,
},
{
name: "Delete Access token success",
fields: fields{resource.NewAPIPatchingApplicator(nil), nil, nil, log.FromContext(nil)},
args: args{nil, nil, &infrav1alpha1.Token{}},
mocks: []mockHelper{
{"DeleteAccessToken", []string{"string"}, []interface{}{nil, nil}},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &reconciler{
APIPatchingApplicator: tt.fields.APIPatchingApplicator,
giteaClient: tt.fields.giteaClient,
finalizer: tt.fields.finalizer,
}
// The below block being setup and processing of mocks before invoking the function to be tested
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the block of code from L85 to L91 can be captured in a function to be reused by every test case that uses mockery, right? What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like its generic/common to all yes. I pulled it directly from your other example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can leave it here for now and maybe refactor later. I'd like to get this PR merged so that I can refactor the Repository unit tests to use Mockery.

mockGClient := new(giteaclient.MockGiteaClient)
tt.args.giteaClient = mockGClient
tt.fields.giteaClient = mockGClient
for counter := range tt.mocks {
call := mockGClient.Mock.On(tt.mocks[counter].methodName)
for _, arg := range tt.mocks[counter].argType {
call.Arguments = append(call.Arguments, mock.AnythingOfType(arg))
}
for _, ret := range tt.mocks[counter].retArgList {
call.ReturnArguments = append(call.ReturnArguments, ret)
}
}

if err := r.deleteToken(tt.args.ctx, tt.args.giteaClient, tt.args.cr); (err != nil) != tt.wantErr {
t.Errorf("deleteToken() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
19 changes: 19 additions & 0 deletions default-go-test.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@


GO_VERSION ?= 1.20.2
MOCKERY_VERSION=2.37.1
TEST_COVERAGE_FILE=lcov.info
TEST_COVERAGE_HTML_FILE=coverage_unit.html
TEST_COVERAGE_FUNC_FILE=func_coverage.out
GIT_ROOT_DIR ?= $(dir $(lastword $(MAKEFILE_LIST)))
OS_ARCH ?= $(shell uname -m)
OS ?= $(shell uname)
include $(GIT_ROOT_DIR)/detect-container-runtime.mk

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

.PHONY: install-mockery
install-mockery: ## install mockery
ifeq ($(CONTAINER_RUNNABLE), 0)
$(CONTAINER_RUNTIME) pull docker.io/vektra/mockery:v${MOCKERY_VERSION}
else
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
endif

.PHONY: generate-mocks
generate-mocks:
ifeq ($(CONTAINER_RUNNABLE), 0)
$(CONTAINER_RUNTIME) run --security-opt label=disable -v ${PWD}:/src -w /src docker.io/vektra/mockery:v${MOCKERY_VERSION}
else
mockery
endif

.PHONY: unit-clean
unit-clean: ## Clean up the artifacts created by the unit tests
ifeq ($(CONTAINER_RUNNABLE), 0)
Expand Down