knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/injection/clients_test.go (about) 1 /* 2 Copyright 2019 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 injection 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 "k8s.io/client-go/rest" 25 ) 26 27 func injectFoo(ctx context.Context, cfg *rest.Config) context.Context { 28 return ctx 29 } 30 31 func injectBar(ctx context.Context, cfg *rest.Config) context.Context { 32 return ctx 33 } 34 35 func TestRegisterClient(t *testing.T) { 36 i := &impl{} 37 38 if want, got := 0, len(i.GetClients()); got != want { 39 t.Errorf("GetClients() = %d, wanted %d", want, got) 40 } 41 42 i.RegisterClient(injectFoo) 43 44 if want, got := 1, len(i.GetClients()); got != want { 45 t.Errorf("GetClients() = %d, wanted %d", want, got) 46 } 47 48 i.RegisterClient(injectBar) 49 50 if want, got := 2, len(i.GetClients()); got != want { 51 t.Errorf("GetClients() = %d, wanted %d", want, got) 52 } 53 } 54 55 type fakeClient struct { 56 Name string 57 } 58 59 func TestRegisterClientFetcher(t *testing.T) { 60 i := &impl{} 61 62 fakeA := fakeClient{Name: "a"} 63 fetchA := func(ctx context.Context) interface{} { 64 return fakeA 65 } 66 67 fakeB := fakeClient{Name: "b"} 68 fetchB := func(ctx context.Context) interface{} { 69 return fakeB 70 } 71 72 ctx := context.Background() 73 if want, got := 0, len(i.FetchAllClients(ctx)); got != want { 74 t.Errorf("FetchAllClients() = %d, wanted %d", got, want) 75 } 76 77 i.RegisterClientFetcher(fetchA) 78 i.RegisterClientFetcher(fetchB) 79 80 got := i.FetchAllClients(ctx) 81 want := []interface{}{fakeA, fakeB} 82 if !cmp.Equal(got, want) { 83 t.Errorf("FetchAllClients() = %v, wanted %v", got, want) 84 } 85 }