knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/injection/sharedmain/main_test.go (about) 1 /* 2 Copyright 2021 The Knative Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package sharedmain 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 "go.uber.org/zap/zapcore" 25 26 "knative.dev/pkg/injection" 27 "knative.dev/pkg/leaderelection" 28 "knative.dev/pkg/logging" 29 "knative.dev/pkg/observability" 30 ) 31 32 func TestEnabledControllers(t *testing.T) { 33 tests := []struct { 34 name string 35 disabledControllers []string 36 ctors []injection.NamedControllerConstructor 37 wantNames []string 38 }{{ 39 name: "zero", 40 disabledControllers: []string{"foo"}, 41 ctors: []injection.NamedControllerConstructor{{Name: "bar"}}, 42 wantNames: []string{"bar"}, 43 }, { 44 name: "one", 45 disabledControllers: []string{"foo"}, 46 ctors: []injection.NamedControllerConstructor{{Name: "foo"}}, 47 wantNames: []string{}, 48 }, { 49 name: "two", 50 disabledControllers: []string{"foo"}, 51 ctors: []injection.NamedControllerConstructor{ 52 {Name: "foo"}, 53 {Name: "bar"}, 54 }, 55 wantNames: []string{"bar"}, 56 }} 57 58 for _, tt := range tests { 59 t.Run(tt.name, func(t *testing.T) { 60 got := enabledControllers(tt.disabledControllers, tt.ctors) 61 if diff := cmp.Diff(tt.wantNames, namesOf(got)); diff != "" { 62 t.Error("(-want, +got)", diff) 63 } 64 }) 65 } 66 } 67 68 func namesOf(ctors []injection.NamedControllerConstructor) []string { 69 names := make([]string, 0, len(ctors)) 70 for _, x := range ctors { 71 names = append(names, x.Name) 72 } 73 return names 74 } 75 76 func TestWithLoggingConfig(t *testing.T) { 77 want := &logging.Config{ 78 LoggingLevel: map[string]zapcore.Level{ 79 "foo": zapcore.DebugLevel, 80 }, 81 } 82 ctx := logging.WithConfig(context.Background(), want) 83 84 got, err := GetLoggingConfig(ctx) 85 if err != nil { 86 t.Fatalf("GetLoggingConfig() = %v", err) 87 } 88 if diff := cmp.Diff(got, want); diff != "" { 89 t.Errorf("(-got, +want) = %s", diff) 90 } 91 } 92 93 func TestWithLeaderElectionConfig(t *testing.T) { 94 want := &leaderelection.Config{ 95 Buckets: 12, 96 } 97 ctx := leaderelection.WithConfig(context.Background(), want) 98 99 got, err := GetLeaderElectionConfig(ctx) 100 if err != nil { 101 t.Fatalf("GetLeaderElectionConfig() = %v", err) 102 } 103 if diff := cmp.Diff(got, want); diff != "" { 104 t.Errorf("(-got, +want) = %s", diff) 105 } 106 } 107 108 func TestWithObservabilityConfig(t *testing.T) { 109 want := &observability.Config{ 110 Tracing: observability.TracingConfig{ 111 Protocol: "some-protocol", 112 }, 113 } 114 ctx := observability.WithConfig(context.Background(), want) 115 116 got, err := GetObservabilityConfig(ctx) 117 if err != nil { 118 t.Fatalf("GetObservabilityConfig() = %v", err) 119 } 120 if diff := cmp.Diff(got, want); diff != "" { 121 t.Errorf("(-got, +want) = %s", diff) 122 } 123 }