Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions testing/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ type ReconcilerTestCase struct {
// execution, or to make assertions for mocks.
CleanUp func(t *testing.T, ctx context.Context, tc *ReconcilerTestCase) error
// Now is the time the test should run as, defaults to the current time. This value can be used
// by reconcilers via the reconcilers.RetireveNow(ctx) method.
// by reconcilers via the reconcilers.RetrieveNow(ctx) method.
Now time.Time
// Differ methods to use to compare expected and actual values. An empty string is returned for equivalent items.
Differ Differ
Expand Down Expand Up @@ -159,7 +159,7 @@ func (tc *ReconcilerTestCase) Run(t *testing.T, scheme *runtime.Scheme, factory
}

ctx := context.Background()
if tc.Now == (time.Time{}) {
if tc.Now.IsZero() {
tc.Now = time.Now()
}
ctx = rtime.StashNow(ctx, tc.Now)
Expand Down
75 changes: 75 additions & 0 deletions testing/reconciler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2025 the original author or 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 testing

import (
"context"
"testing"
"time"

"k8s.io/apimachinery/pkg/runtime"
"reconciler.io/runtime/reconcilers"
rtime "reconciler.io/runtime/time"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

func TestReconcilerTestCase_Now(t *testing.T) {
start := time.Now()

type testCase struct {
name string
rtc *ReconcilerTestCase
verify func(*testing.T, context.Context)
}
tests := []*testCase{
{
name: "Now defaults to time.Now()",
rtc: &ReconcilerTestCase{},
verify: func(t *testing.T, ctx context.Context) {
now := rtime.RetrieveNow(ctx)
// compare with a range to allow for time skew
// test must complete within 1 hour of wall clock time
if now.Before(start.Add(-1*time.Second)) || now.After(start.Add(time.Hour)) {
t.Error("expected test case to be initialized with time.Now()")
}
},
},
{
name: "Now is set explicitly",
rtc: &ReconcilerTestCase{
Now: time.UnixMilli(1000),
},
verify: func(t *testing.T, ctx context.Context) {
now := rtime.RetrieveNow(ctx)
if !now.Equal(time.UnixMilli(1000)) {
t.Error("expected time to be initialized from the test case")
}
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tc.rtc.ExpectedResult = reconcile.Result{RequeueAfter: time.Second}
tc.rtc.Run(t, runtime.NewScheme(), func(t *testing.T, rtc *ReconcilerTestCase, c reconcilers.Config) reconcile.Reconciler {
return reconcile.Func(func(ctx context.Context, o reconcile.Request) (reconcile.Result, error) {
tc.verify(t, ctx)
return reconcile.Result{RequeueAfter: time.Second}, nil
})
})
})
}
}
4 changes: 2 additions & 2 deletions testing/subreconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ type SubReconcilerTestCase[Type client.Object] struct {
// execution, or to make assertions for mocks.
CleanUp func(t *testing.T, ctx context.Context, tc *SubReconcilerTestCase[Type]) error
// Now is the time the test should run as, defaults to the current time. This value can be used
// by reconcilers via the reconcilers.RetireveNow(ctx) method.
// by reconcilers via the reconcilers.RetrieveNow(ctx) method.
Now time.Time
// Differ methods to use to compare expected and actual values. An empty string is returned for equivalent items.
Differ Differ
Expand Down Expand Up @@ -168,7 +168,7 @@ func (tc *SubReconcilerTestCase[T]) Run(t *testing.T, scheme *runtime.Scheme, fa
}

ctx := stash.WithContext(context.Background())
if tc.Now == (time.Time{}) {
if tc.Now.IsZero() {
tc.Now = time.Now()
}
ctx = rtime.StashNow(ctx, tc.Now)
Expand Down
2 changes: 1 addition & 1 deletion testing/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type AdmissionWebhookTestCase struct {
// execution, or to make assertions for mocks.
CleanUp func(t *testing.T, ctx context.Context, tc *AdmissionWebhookTestCase) error
// Now is the time the test should run as, defaults to the current time. This value can be used
// by reconcilers via the reconcilers.RetireveNow(ctx) method.
// by reconcilers via the reconcilers.RetrieveNow(ctx) method.
Now time.Time
// Differ methods to use to compare expected and actual values. An empty string is returned for equivalent items.
Differ Differ
Expand Down
Loading