-
Notifications
You must be signed in to change notification settings - Fork 53
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
Changes from all commits
15edf23
b816d10
349f652
9b3a5ae
ddd53f4
c1c9fc8
e855c21
ac45014
ad40b41
5a4fe15
6b29826
0748619
a6cb358
0181f6f
9e9e0c3
70dcd07
86ea699
da3c2e0
24e8bd8
5e8317d
5fcd6f1
3593345
82bb12d
66534d7
ba6cd12
96a491d
6572b57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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}}" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
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{}
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess to allow for a broader identifier.
https://gitea.com/gitea/go-sdk/src/branch/main/gitea/user_app.go#L118