Skip to content

Commit

Permalink
Unit test coverage for DeleteRepo and upsertRepo functions in reposit…
Browse files Browse the repository at this point in the history
…ory reconciler (#426)
  • Loading branch information
liamfallon authored Nov 9, 2023
1 parent 7ae021d commit c1d7bd4
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 5 deletions.
26 changes: 25 additions & 1 deletion controllers/pkg/giteaclient/giteaclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ import (

type GiteaClient interface {
Start(ctx context.Context)

Get() *gitea.Client
GetMyUserInfo() (*gitea.User, *gitea.Response, error)
DeleteRepo(owner string, repo string) (*gitea.Response, error)
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)
}

var lock = &sync.Mutex{}
Expand Down Expand Up @@ -137,3 +141,23 @@ func getClientAuth(secret *corev1.Secret) gitea.ClientOption {
func (r *gc) Get() *gitea.Client {
return r.giteaClient
}

func (r *gc) GetMyUserInfo() (*gitea.User, *gitea.Response, error) {
return r.giteaClient.GetMyUserInfo()
}

func (r *gc) DeleteRepo(owner string, repo string) (*gitea.Response, error) {
return r.giteaClient.DeleteRepo(owner, repo)
}

func (r *gc) GetRepo(userName string, repoCRName string) (*gitea.Repository, *gitea.Response, error) {
return r.giteaClient.GetRepo(userName, repoCRName)
}

func (r *gc) CreateRepo(createRepoOption gitea.CreateRepoOption) (*gitea.Repository, *gitea.Response, error) {
return r.giteaClient.CreateRepo(createRepoOption)
}

func (r *gc) EditRepo(userName string, repoCRName string, editRepoOption gitea.EditRepoOption) (*gitea.Repository, *gitea.Response, error) {
return r.giteaClient.EditRepo(userName, repoCRName, editRepoOption)
}
8 changes: 4 additions & 4 deletions controllers/pkg/reconcilers/repository/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
// Delete the repo from the git server
// when successful remove the finalizer
if cr.Spec.Lifecycle.DeletionPolicy == commonv1alpha1.DeletionDelete {
if err := r.deleteRepo(ctx, giteaClient, cr); err != nil {
if err := r.deleteRepo(ctx, r.giteaClient, cr); err != nil {
r.l.Error(err, "cannot delete repo in git server")
return ctrl.Result{Requeue: true}, errors.Wrap(r.Status().Update(ctx, cr), errUpdateStatus)
}
Expand All @@ -143,14 +143,14 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
}

// upsert repo in git server
if err := r.upsertRepo(ctx, giteaClient, cr); err != nil {
if err := r.upsertRepo(ctx, r.giteaClient, cr); err != nil {
return ctrl.Result{Requeue: true}, errors.Wrap(r.Status().Update(ctx, cr), errUpdateStatus)
}
cr.SetConditions(infrav1alpha1.Ready())
return ctrl.Result{}, errors.Wrap(r.Status().Update(ctx, cr), errUpdateStatus)
}

func (r *reconciler) upsertRepo(ctx context.Context, giteaClient *gitea.Client, cr *infrav1alpha1.Repository) error {
func (r *reconciler) upsertRepo(ctx context.Context, giteaClient giteaclient.GiteaClient, cr *infrav1alpha1.Repository) error {
u, _, err := giteaClient.GetMyUserInfo()
if err != nil {
r.l.Error(err, "cannot get user info")
Expand Down Expand Up @@ -226,7 +226,7 @@ func (r *reconciler) upsertRepo(ctx context.Context, giteaClient *gitea.Client,
return nil
}

func (r *reconciler) deleteRepo(ctx context.Context, giteaClient *gitea.Client, cr *infrav1alpha1.Repository) error {
func (r *reconciler) deleteRepo(ctx context.Context, giteaClient giteaclient.GiteaClient, cr *infrav1alpha1.Repository) error {
u, _, err := giteaClient.GetMyUserInfo()
if err != nil {
r.l.Error(err, "cannot get user info")
Expand Down
198 changes: 198 additions & 0 deletions controllers/pkg/reconcilers/repository/reconciler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// 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 repository

import (
"context"
"errors"
"testing"

"code.gitea.io/sdk/gitea"
infrav1alpha1 "github.com/nephio-project/api/infra/v1alpha1"
"github.com/stretchr/testify/require"
)

type GiteaClientMock struct {
}

type NephioGiteaClientMock struct {
myUserInfoError error
deleteRepoError error
getRepoError error
createRepoError error
editRepoError error
}

func (gc NephioGiteaClientMock) Get() *gitea.Client {
return nil
}

func (gc NephioGiteaClientMock) Start(ctx context.Context) {
}

func (gc NephioGiteaClientMock) GetMyUserInfo() (*gitea.User, *gitea.Response, error) {
return &gitea.User{}, &gitea.Response{}, gc.myUserInfoError
}

func (gc NephioGiteaClientMock) DeleteRepo(owner string, repo string) (*gitea.Response, error) {
return nil, gc.deleteRepoError
}

func (gc NephioGiteaClientMock) GetRepo(userName string, repoCRName string) (*gitea.Repository, *gitea.Response, error) {
return nil, nil, gc.getRepoError
}

func (gc NephioGiteaClientMock) CreateRepo(createRepoOption gitea.CreateRepoOption) (*gitea.Repository, *gitea.Response, error) {
return &gitea.Repository{}, nil, gc.createRepoError
}

func (gc NephioGiteaClientMock) EditRepo(userName string, repoCRName string, editRepoOption gitea.EditRepoOption) (*gitea.Repository, *gitea.Response, error) {
return &gitea.Repository{}, nil, gc.editRepoError
}

func TestUpsertRepo(t *testing.T) {
dummyString := "Dummy String"
dummyBool := true
dummyTrustModel := infrav1alpha1.TrustModel("Trust Model")

testCases := map[string]struct {
ctx context.Context
giteaClient NephioGiteaClientMock
cr infrav1alpha1.Repository
expectedErr error
}{
"User Info reports error": {
ctx: nil,
giteaClient: NephioGiteaClientMock{
myUserInfoError: errors.New("Error getting User Information"),
},
cr: infrav1alpha1.Repository{},
expectedErr: errors.New("Error getting User Information"),
},
"Repo exists, cr fields blank": {
ctx: nil,
giteaClient: NephioGiteaClientMock{},
cr: infrav1alpha1.Repository{
Status: infrav1alpha1.RepositoryStatus{},
},
expectedErr: nil,
},
"Repo exists, cr fields not blank": {
ctx: nil,
giteaClient: NephioGiteaClientMock{
editRepoError: errors.New("Repo update failed"),
},
cr: infrav1alpha1.Repository{
Spec: infrav1alpha1.RepositorySpec{
Description: &dummyString,
Private: &dummyBool,
},
Status: infrav1alpha1.RepositoryStatus{},
},
expectedErr: errors.New("Repo update failed"),
},
"Repo exists, update fails": {
ctx: nil,
giteaClient: NephioGiteaClientMock{},
cr: infrav1alpha1.Repository{
Status: infrav1alpha1.RepositoryStatus{},
},
expectedErr: nil,
},
"Create repo: cr fields not blank": {
ctx: nil,
giteaClient: NephioGiteaClientMock{
getRepoError: errors.New("Repo does not exist"),
},
cr: infrav1alpha1.Repository{
Spec: infrav1alpha1.RepositorySpec{
Description: &dummyString,
Private: &dummyBool,
IssueLabels: &dummyString,
Gitignores: &dummyString,
License: &dummyString,
Readme: &dummyString,
DefaultBranch: &dummyString,
TrustModel: &dummyTrustModel,
},
},
expectedErr: nil,
},
"Create repo: cr fields blank": {
ctx: nil,
giteaClient: NephioGiteaClientMock{
getRepoError: errors.New("Repo does not exist"),
},
cr: infrav1alpha1.Repository{},
expectedErr: nil,
},
"Create repo: fails": {
ctx: nil,
giteaClient: NephioGiteaClientMock{
getRepoError: errors.New("Repo does not exist"),
createRepoError: errors.New("Error creating repo"),
},
cr: infrav1alpha1.Repository{},
expectedErr: errors.New("Error creating repo"),
},
}
for tn, tc := range testCases {
t.Run(tn, func(t *testing.T) {
r := reconciler{}
err := r.upsertRepo(tc.ctx, tc.giteaClient, &tc.cr)
require.Equal(t, tc.expectedErr, err)
})
}
}

func TestDeleteRepo(t *testing.T) {

testCases := map[string]struct {
ctx context.Context
giteaClient NephioGiteaClientMock
cr infrav1alpha1.Repository
expectedErr error
}{
"User Info and Delete repo both work": {
ctx: nil,
giteaClient: NephioGiteaClientMock{},
cr: infrav1alpha1.Repository{},
expectedErr: nil,
},
"User Info reports error": {
ctx: nil,
giteaClient: NephioGiteaClientMock{
myUserInfoError: errors.New("Error getting User Information"),
},
cr: infrav1alpha1.Repository{},
expectedErr: errors.New("Error getting User Information"),
},
"Delete repo reports error": {
ctx: nil,
giteaClient: NephioGiteaClientMock{
deleteRepoError: errors.New("Error deleting repo"),
},
cr: infrav1alpha1.Repository{},
expectedErr: errors.New("Error deleting repo"),
},
}
for tn, tc := range testCases {
t.Run(tn, func(t *testing.T) {
r := reconciler{}
err := r.deleteRepo(tc.ctx, tc.giteaClient, &tc.cr)
require.Equal(t, tc.expectedErr, err)
})
}
}

0 comments on commit c1d7bd4

Please sign in to comment.