Skip to content

Commit

Permalink
use ctx setup by the test which has a test logger (#12784)
Browse files Browse the repository at this point in the history
Then the log output is associated with the test rather than being printed out to the terminal
  • Loading branch information
dprotaso authored Mar 26, 2022
1 parent 0bc4171 commit ad64980
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pkg/reconciler/autoscaling/hpa/hpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func TestControllerCanReconcile(t *testing.T) {
la.Promote(reconciler.UniversalBucket(), func(reconciler.Bucket, types.NamespacedName) {})
}

err = ctl.Reconciler.Reconcile(context.Background(), testNamespace+"/"+testRevision)
err = ctl.Reconciler.Reconcile(ctx, testNamespace+"/"+testRevision)
if err != nil {
t.Error("Reconcile() =", err)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/reconciler/autoscaling/kpa/kpa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@ func TestUpdate(t *testing.T) {
}

// Wait for the Reconcile to complete.
if err := ctl.Reconciler.Reconcile(context.Background(), testNamespace+"/"+testRevision); err != nil {
if err := ctl.Reconciler.Reconcile(ctx, testNamespace+"/"+testRevision); err != nil {
t.Error("Reconcile() =", err)
}

Expand All @@ -1429,7 +1429,7 @@ func TestUpdate(t *testing.T) {
fakeservingclient.Get(ctx).AutoscalingV1alpha1().PodAutoscalers(testNamespace).Update(ctx, kpa, metav1.UpdateOptions{})
fakepainformer.Get(ctx).Informer().GetIndexer().Update(kpa)

if err := ctl.Reconciler.Reconcile(context.Background(), testNamespace+"/"+testRevision); err != nil {
if err := ctl.Reconciler.Reconcile(ctx, testNamespace+"/"+testRevision); err != nil {
t.Error("Reconcile() =", err)
}

Expand Down Expand Up @@ -1475,7 +1475,7 @@ func TestControllerCreateError(t *testing.T) {
la.Promote(reconciler.UniversalBucket(), func(reconciler.Bucket, types.NamespacedName) {})
}

got := ctl.Reconciler.Reconcile(context.Background(), key)
got := ctl.Reconciler.Reconcile(ctx, key)
if !errors.Is(got, want) {
t.Errorf("Reconcile() = %v, wanted %v wrapped", got, want)
}
Expand Down Expand Up @@ -1518,7 +1518,7 @@ func TestControllerUpdateError(t *testing.T) {
la.Promote(reconciler.UniversalBucket(), func(reconciler.Bucket, types.NamespacedName) {})
}

got := ctl.Reconciler.Reconcile(context.Background(), key)
got := ctl.Reconciler.Reconcile(ctx, key)
if !errors.Is(got, want) {
t.Errorf("Reconcile() = %v, wanted %v wrapped", got, want)
}
Expand Down Expand Up @@ -1560,7 +1560,7 @@ func TestControllerGetError(t *testing.T) {
la.Promote(reconciler.UniversalBucket(), func(reconciler.Bucket, types.NamespacedName) {})
}

got := ctl.Reconciler.Reconcile(context.Background(), key)
got := ctl.Reconciler.Reconcile(ctx, key)
if !errors.Is(got, want) {
t.Errorf("Reconcile() = %v, wanted %v wrapped", got, want)
}
Expand Down Expand Up @@ -1593,7 +1593,7 @@ func TestScaleFailure(t *testing.T) {
la.Promote(reconciler.UniversalBucket(), func(reconciler.Bucket, types.NamespacedName) {})
}

if err := ctl.Reconciler.Reconcile(context.Background(), testNamespace+"/"+testRevision); err == nil {
if err := ctl.Reconciler.Reconcile(ctx, testNamespace+"/"+testRevision); err == nil {
t.Error("Reconcile() = nil, wanted error")
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/reconciler/domainmapping/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ limitations under the License.
package domainmapping

import (
"context"
"testing"

network "knative.dev/networking/pkg"
"knative.dev/networking/pkg/apis/networking"
logtesting "knative.dev/pkg/logging/testing"
"knative.dev/serving/pkg/reconciler/domainmapping/config"
)

Expand Down Expand Up @@ -71,7 +71,8 @@ func TestAutoTLSEnabled(t *testing.T) {
wantAutoTLSEnabled: true,
}} {
t.Run(tc.name, func(t *testing.T) {
ctx := config.ToContext(context.Background(), &config.Config{
ctx := logtesting.TestContextWithLogger(t)
ctx = config.ToContext(ctx, &config.Config{
Network: &network.Config{
AutoTLS: tc.configAutoTLSEnabled,
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/reconciler/revision/revision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func createRevision(
// Since Reconcile looks in the lister, we need to add it to the informer
fakerevisioninformer.Get(ctx).Informer().GetIndexer().Add(rev)

if err := controller.Reconciler.Reconcile(context.Background(), KeyOrDie(rev)); err == nil {
if err := controller.Reconciler.Reconcile(ctx, KeyOrDie(rev)); err == nil {
rev, _, _ = addResourcesToInformers(t, ctx, rev)
}
return rev
Expand All @@ -186,7 +186,7 @@ func updateRevision(
fakeservingclient.Get(ctx).ServingV1().Revisions(rev.Namespace).Update(ctx, rev, metav1.UpdateOptions{})
fakerevisioninformer.Get(ctx).Informer().GetIndexer().Update(rev)

if err := controller.Reconciler.Reconcile(context.Background(), KeyOrDie(rev)); err == nil {
if err := controller.Reconciler.Reconcile(ctx, KeyOrDie(rev)); err == nil {
addResourcesToInformers(t, ctx, rev)
}
}
Expand Down Expand Up @@ -449,7 +449,7 @@ func TestStatusUnknownWhenDigestsNotResolvedYet(t *testing.T) {

fakeservingclient.Get(ctx).ServingV1().Revisions(rev.Namespace).Create(ctx, rev, metav1.CreateOptions{})
fakerevisioninformer.Get(ctx).Informer().GetIndexer().Add(rev)
if err := controller.Reconciler.Reconcile(context.Background(), KeyOrDie(rev)); err != nil {
if err := controller.Reconciler.Reconcile(ctx, KeyOrDie(rev)); err != nil {
t.Fatal("Reconcile failed:", err)
}

Expand Down
22 changes: 12 additions & 10 deletions pkg/reconciler/route/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
"knative.dev/networking/pkg/apis/networking/v1alpha1"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
logtesting "knative.dev/pkg/logging/testing"
pkgnet "knative.dev/pkg/network"
"knative.dev/pkg/ptr"
"knative.dev/pkg/reconciler"
Expand Down Expand Up @@ -199,7 +200,7 @@ func TestCreateRouteForOneReserveRevision(t *testing.T) {
// Since Reconcile looks in the lister, we need to add it to the informer
fakerouteinformer.Get(ctx).Informer().GetIndexer().Add(route)

ctl.Reconciler.Reconcile(context.Background(), KeyOrDie(route))
ctl.Reconciler.Reconcile(ctx, KeyOrDie(route))

ci := getRouteIngressFromClient(ctx, t, route)

Expand Down Expand Up @@ -268,7 +269,7 @@ func TestCreateRouteForOneReserveRevision(t *testing.T) {
}

fakeingressinformer.Get(ctx).Informer().GetIndexer().Update(ci)
ctl.Reconciler.Reconcile(context.Background(), KeyOrDie(route))
ctl.Reconciler.Reconcile(ctx, KeyOrDie(route))

// Look for the events. Events are delivered asynchronously so we need to use
// hooks here. Each hook tests for a specific event.
Expand Down Expand Up @@ -332,7 +333,7 @@ func TestCreateRouteWithMultipleTargets(t *testing.T) {
// Since Reconcile looks in the lister, we need to add it to the informer.
fakerouteinformer.Get(ctx).Informer().GetIndexer().Add(route)

ctl.Reconciler.Reconcile(context.Background(), KeyOrDie(route))
ctl.Reconciler.Reconcile(ctx, KeyOrDie(route))

ci := getRouteIngressFromClient(ctx, t, route)
domain := strings.Join([]string{route.Name, route.Namespace, defaultDomainSuffix}, ".")
Expand Down Expand Up @@ -450,7 +451,7 @@ func TestCreateRouteWithOneTargetReserve(t *testing.T) {
// Since Reconcile looks in the lister, we need to add it to the informer
fakerouteinformer.Get(ctx).Informer().GetIndexer().Add(route)

ctl.Reconciler.Reconcile(context.Background(), KeyOrDie(route))
ctl.Reconciler.Reconcile(ctx, KeyOrDie(route))

ci := getRouteIngressFromClient(ctx, t, route)
domain := strings.Join([]string{route.Name, route.Namespace, defaultDomainSuffix}, ".")
Expand Down Expand Up @@ -582,7 +583,7 @@ func TestCreateRouteWithDuplicateTargets(t *testing.T) {
// Since Reconcile looks in the lister, we need to add it to the informer
fakerouteinformer.Get(ctx).Informer().GetIndexer().Add(route)

ctl.Reconciler.Reconcile(context.Background(), KeyOrDie(route))
ctl.Reconciler.Reconcile(ctx, KeyOrDie(route))

ci := getRouteIngressFromClient(ctx, t, route)
domain := strings.Join([]string{route.Name, route.Namespace, defaultDomainSuffix}, ".")
Expand Down Expand Up @@ -788,7 +789,7 @@ func TestCreateRouteWithNamedTargets(t *testing.T) {
// Since Reconcile looks in the lister, we need to add it to the informer
fakerouteinformer.Get(ctx).Informer().GetIndexer().Add(route)

ctl.Reconciler.Reconcile(context.Background(), KeyOrDie(route))
ctl.Reconciler.Reconcile(ctx, KeyOrDie(route))

ci := getRouteIngressFromClient(ctx, t, route)
domain := strings.Join([]string{route.Name, route.Namespace, defaultDomainSuffix}, ".")
Expand Down Expand Up @@ -997,7 +998,7 @@ func TestCreateRouteWithNamedTargetsAndTagBasedRouting(t *testing.T) {
fakeservingclient.Get(ctx).ServingV1().Routes(testNamespace).Create(ctx, route, metav1.CreateOptions{})
// Since Reconcile looks in the lister, we need to add it to the informer
fakerouteinformer.Get(ctx).Informer().GetIndexer().Add(route)
ctl.Reconciler.Reconcile(context.Background(), KeyOrDie(route))
ctl.Reconciler.Reconcile(ctx, KeyOrDie(route))

ci := getRouteIngressFromClient(ctx, t, route)
domain := strings.Join([]string{route.Name, route.Namespace, defaultDomainSuffix}, ".")
Expand Down Expand Up @@ -1325,7 +1326,7 @@ func TestUpdateDomainConfigMap(t *testing.T) {
// Create a route.
fakerouteinformer.Get(ctx).Informer().GetIndexer().Add(route)
routeClient.Create(ctx, route, metav1.CreateOptions{})
if err := ctl.Reconciler.Reconcile(context.Background(), KeyOrDie(route)); err != nil {
if err := ctl.Reconciler.Reconcile(ctx, KeyOrDie(route)); err != nil {
t.Fatal("Reconcile() =", err)
}
addRouteToInformers(ctx, t, route)
Expand Down Expand Up @@ -1375,7 +1376,7 @@ func TestUpdateDomainConfigMap(t *testing.T) {

// Now that we know the exact version the reconciler is going to see in the
// informers, let's reconcile.
if err := ctl.Reconciler.Reconcile(context.Background(), KeyOrDie(route)); err != nil {
if err := ctl.Reconciler.Reconcile(ctx, KeyOrDie(route)); err != nil {
t.Fatal("Reconcile() =", err)
}

Expand Down Expand Up @@ -1625,7 +1626,8 @@ func TestAutoTLSEnabled(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := config.ToContext(context.Background(), &config.Config{
ctx := logtesting.TestContextWithLogger(t)
ctx = config.ToContext(ctx, &config.Config{
Network: &network.Config{
AutoTLS: test.configAutoTLSEnabled,
},
Expand Down

0 comments on commit ad64980

Please sign in to comment.