Skip to content

Commit b350f7a

Browse files
authored
Add mockery test for approval reconciler (#482)
1 parent 211bc1b commit b350f7a

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed

controllers/pkg/.mockery.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ packages:
1010
Client:
1111
config:
1212
dir: "mocks/external/{{ .InterfaceName | lower }}"
13-
outpkg: "mocks"
13+
outpkg: "mocks"

controllers/pkg/reconcilers/approval/reconciler.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (r *reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, c i
6767
return nil, fmt.Errorf("cannot initialize, expecting controllerConfig, got: %s", reflect.TypeOf(c).Name())
6868
}
6969

70-
r.Client = mgr.GetClient()
70+
r.baseClient = mgr.GetClient()
7171
r.porchClient = cfg.PorchClient
7272
r.porchRESTClient = cfg.PorchRESTClient
7373
r.recorder = mgr.GetEventRecorderFor("approval-controller")
@@ -80,7 +80,7 @@ func (r *reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, c i
8080

8181
// reconciler reconciles a NetworkInstance object
8282
type reconciler struct {
83-
client.Client
83+
baseClient client.Client
8484
porchClient client.Client
8585
porchRESTClient rest.Interface
8686
recorder record.EventRecorder
@@ -91,7 +91,7 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
9191
log.Info("reconcile approval")
9292

9393
pr := &porchv1alpha1.PackageRevision{}
94-
if err := r.Get(ctx, req.NamespacedName, pr); err != nil {
94+
if err := r.baseClient.Get(ctx, req.NamespacedName, pr); err != nil {
9595
// There's no need to requeue if we no longer exist. Otherwise we'll be
9696
// requeued implicitly because we return an error.
9797
if resource.IgnoreNotFound(err) != nil {
@@ -194,7 +194,7 @@ func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
194194
action = "proposing"
195195
reason = "Proposed"
196196
pr.Spec.Lifecycle = porchv1alpha1.PackageRevisionLifecycleProposed
197-
err = r.Update(ctx, pr)
197+
err = r.baseClient.Update(ctx, pr)
198198
} else {
199199
err = porchclient.UpdatePackageRevisionApproval(ctx, r.porchRESTClient, client.ObjectKey{
200200
Namespace: pr.Namespace,
@@ -251,7 +251,7 @@ func manageDelay(pr *porchv1alpha1.PackageRevision) (time.Duration, error) {
251251

252252
func (r *reconciler) policyInitial(ctx context.Context, pr *porchv1alpha1.PackageRevision) (bool, error) {
253253
var prList porchv1alpha1.PackageRevisionList
254-
if err := r.Client.List(ctx, &prList); err != nil {
254+
if err := r.baseClient.List(ctx, &prList); err != nil {
255255
return false, err
256256
}
257257

controllers/pkg/reconcilers/approval/reconciler_test.go

+90-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@
1515
package approval
1616

1717
import (
18+
context "context"
19+
"fmt"
1820
"testing"
1921
"time"
2022

23+
"github.com/stretchr/testify/mock"
24+
2125
porchapi "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
2226
"github.com/stretchr/testify/require"
2327
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"github.com/nephio-project/nephio/controllers/pkg/mocks/external/client"
2429
)
2530

2631
func TestShouldProcess(t *testing.T) {
@@ -117,7 +122,7 @@ func TestManageDelay(t *testing.T) {
117122
"not old enough": {
118123
pr: porchapi.PackageRevision{
119124
ObjectMeta: metav1.ObjectMeta{
120-
CreationTimestamp: metav1.Time{now},
125+
CreationTimestamp: metav1.Time{Time: now},
121126
Annotations: map[string]string{
122127
"approval.nephio.org/delay": "1h",
123128
},
@@ -129,7 +134,7 @@ func TestManageDelay(t *testing.T) {
129134
"old enough": {
130135
pr: porchapi.PackageRevision{
131136
ObjectMeta: metav1.ObjectMeta{
132-
CreationTimestamp: metav1.Time{now.AddDate(-1, 0, 0)},
137+
CreationTimestamp: metav1.Time{Time: now.AddDate(-1, 0, 0)},
133138
Annotations: map[string]string{
134139
"approval.nephio.org/delay": "1h",
135140
},
@@ -147,3 +152,86 @@ func TestManageDelay(t *testing.T) {
147152
})
148153
}
149154
}
155+
156+
157+
158+
func TestPolicyInitial(t *testing.T) {
159+
160+
testCases := map[string]struct {
161+
pr porchapi.PackageRevision
162+
prl *porchapi.PackageRevisionList
163+
expectedApprove bool
164+
expectedError error
165+
mockReturnErr error
166+
}{
167+
"Draft with proposed lifecycle": {
168+
pr: porchapi.PackageRevision{
169+
ObjectMeta: metav1.ObjectMeta{
170+
Annotations: map[string]string{
171+
"approval.nephio.org/policy": "initial",
172+
},
173+
},
174+
},
175+
prl: &porchapi.PackageRevisionList{
176+
TypeMeta: metav1.TypeMeta{
177+
APIVersion: "Blah",
178+
Kind: "Blah",
179+
},
180+
Items: []porchapi.PackageRevision{
181+
{
182+
Spec: porchapi.PackageRevisionSpec{
183+
Lifecycle: porchapi.PackageRevisionLifecycleProposed,
184+
},
185+
},
186+
},
187+
},
188+
expectedApprove: true,
189+
expectedError: nil,
190+
mockReturnErr: nil,
191+
},
192+
"Draft with existing version": {
193+
pr: porchapi.PackageRevision{
194+
Spec: porchapi.PackageRevisionSpec{
195+
RepositoryName: "MyRepo",
196+
PackageName: "MyPackage",
197+
},
198+
},
199+
prl: &porchapi.PackageRevisionList{
200+
Items: []porchapi.PackageRevision{
201+
{
202+
Spec: porchapi.PackageRevisionSpec{
203+
Lifecycle: porchapi.PackageRevisionLifecyclePublished,
204+
RepositoryName: "MyRepo",
205+
PackageName: "MyPackage",
206+
},
207+
},
208+
},
209+
},
210+
expectedApprove: false,
211+
expectedError: nil,
212+
mockReturnErr: nil,
213+
},
214+
"runtime client list failure": {
215+
pr: porchapi.PackageRevision{},
216+
prl: &porchapi.PackageRevisionList{},
217+
expectedApprove: false,
218+
expectedError: fmt.Errorf("Failed to list items"),
219+
mockReturnErr: fmt.Errorf("Failed to list items"),
220+
},
221+
}
222+
for tn, tc := range testCases {
223+
// Create a new instance of the mock object
224+
clientMock := new(mocks.MockClient)
225+
clientMock.On("List", context.TODO(), mock.AnythingOfType("*v1alpha1.PackageRevisionList")).Return(tc.mockReturnErr).Run(func(args mock.Arguments) {
226+
packRevList := args.Get(1).(*porchapi.PackageRevisionList)
227+
*packRevList = *tc.prl // tc.prl is what r.Get will store in 2nd Argument
228+
})
229+
// Create an instance of the component under test
230+
r := reconciler{baseClient: clientMock}
231+
t.Run(tn, func(t *testing.T) {
232+
actualApproval, actualError := r.policyInitial(context.TODO(), &tc.pr)
233+
require.Equal(t, tc.expectedApprove, actualApproval)
234+
require.Equal(t, tc.expectedError, actualError)
235+
})
236+
}
237+
}

0 commit comments

Comments
 (0)