knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/convert_test.go (about) 1 /* 2 Copyright 2020 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 apis 18 19 import ( 20 "context" 21 "errors" 22 "testing" 23 ) 24 25 func TestConvertToViaProxy(t *testing.T) { 26 sink := &testResource{} 27 proxy := &testResource{} 28 source := &testResource{proxy: proxy} 29 30 err := ConvertToViaProxy(context.Background(), source, proxy, sink) 31 if err != nil { 32 t.Error("ConvertToViaProxy returned unexpected err:", err) 33 } 34 35 if source.to != proxy { 36 t.Errorf("expected source to be converted to the proxy") 37 } 38 39 if proxy.to != sink { 40 t.Errorf("expected proxy to be converted to the sink") 41 } 42 } 43 44 func TestConvertToViaProxyError(t *testing.T) { 45 tests := []struct { 46 name string 47 source, proxy testResource 48 }{{ 49 name: "converting source to proxy fails", 50 source: testResource{ 51 err: errors.New("converting up failed"), 52 }, 53 proxy: testResource{}, 54 }, { 55 name: "converting proxy to sink fails", 56 source: testResource{}, 57 proxy: testResource{ 58 err: errors.New("converting up failed"), 59 }, 60 }} 61 62 for _, test := range tests { 63 t.Run(test.name, func(t *testing.T) { 64 err := ConvertToViaProxy(context.Background(), 65 &test.source, 66 &test.proxy, 67 nil, /* sink */ 68 ) 69 70 if err == nil { 71 t.Errorf("expected error to have occurred") 72 } 73 }) 74 } 75 } 76 77 func TestConvertFromViaProxy(t *testing.T) { 78 proxy := &testResource{} 79 sink := &testResource{} 80 source := &testResource{} 81 82 err := ConvertFromViaProxy(context.Background(), source, proxy, sink) 83 if err != nil { 84 t.Error("ConvertFromViaProxy returned unexpected err:", err) 85 } 86 87 if proxy.from != source { 88 t.Errorf("expected proxy to be converted from the source") 89 } 90 91 if sink.from != proxy { 92 t.Errorf("expected sink to be converted from the proxy") 93 } 94 } 95 96 func TestConvertFromViaProxyError(t *testing.T) { 97 tests := []struct { 98 name string 99 sink, proxy testResource 100 }{{ 101 name: "converting proxy from source fails", 102 sink: testResource{}, 103 proxy: testResource{ 104 err: errors.New("converting down failed"), 105 }, 106 }, { 107 name: "converting sink from proxy fails", 108 sink: testResource{ 109 err: errors.New("converting down failed"), 110 }, 111 proxy: testResource{}, 112 }} 113 114 for _, test := range tests { 115 t.Run(test.name, func(t *testing.T) { 116 err := ConvertFromViaProxy(context.Background(), 117 nil, /* source */ 118 &test.proxy, 119 &test.sink, 120 ) 121 if err == nil { 122 t.Errorf("expected error to have occurred") 123 } 124 }) 125 } 126 } 127 128 type testResource struct { 129 proxy, to, from Convertible 130 err error 131 } 132 133 var _ Convertible = (*testResource)(nil) 134 135 func (r *testResource) ConvertTo(ctx context.Context, to Convertible) error { 136 r.to = to 137 return r.err 138 } 139 140 func (r *testResource) ConvertFrom(ctx context.Context, from Convertible) error { 141 r.from = from 142 return r.err 143 }