knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/v1alpha1/addressable_types_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 v1alpha1 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 "knative.dev/pkg/apis" 25 v1 "knative.dev/pkg/apis/duck/v1" 26 "knative.dev/pkg/apis/duck/v1beta1" 27 ) 28 29 func TestGetURL(t *testing.T) { 30 tests := []struct { 31 name string 32 addr Addressable 33 want *apis.URL 34 }{{ 35 name: "just hostname", 36 addr: Addressable{ 37 Hostname: "foo.com", 38 }, 39 want: apis.HTTP("foo.com"), 40 }, { 41 name: "just url", 42 addr: Addressable{ 43 Addressable: v1beta1.Addressable{ 44 URL: apis.HTTP("bar.com"), 45 }, 46 }, 47 want: apis.HTTP("bar.com"), 48 }, { 49 name: "both fields", 50 addr: Addressable{ 51 Hostname: "foo.bar.svc.cluster.local", 52 Addressable: v1beta1.Addressable{ 53 URL: apis.HTTPS("baz.com"), 54 }, 55 }, 56 want: apis.HTTPS("baz.com"), 57 }} 58 59 for _, test := range tests { 60 t.Run(test.name, func(t *testing.T) { 61 got := test.addr.GetURL() 62 if got.String() != test.want.String() { 63 t.Errorf("GetURL() = %v, wanted %v", got, test.want) 64 } 65 }) 66 } 67 } 68 69 func TestConversion(t *testing.T) { 70 tests := []struct { 71 name string 72 addr *Addressable 73 conv apis.Convertible 74 want string 75 wantErrUp bool 76 wantErrDown bool 77 }{{ 78 name: "v1", 79 addr: &Addressable{ 80 Addressable: v1beta1.Addressable{ 81 URL: apis.HTTP("bar.com"), 82 }, 83 Hostname: "bar.com", 84 }, 85 conv: &v1.Addressable{}, 86 wantErrUp: false, 87 wantErrDown: false, 88 }, { 89 name: "v1beta1", 90 addr: &Addressable{ 91 Addressable: v1beta1.Addressable{ 92 URL: apis.HTTP("bar.com"), 93 }, 94 Hostname: "bar.com", 95 }, 96 conv: &v1beta1.Addressable{}, 97 wantErrUp: false, 98 wantErrDown: false, 99 }, { 100 name: "v1alpha1", 101 addr: &Addressable{ 102 Addressable: v1beta1.Addressable{ 103 URL: apis.HTTPS("bar.com"), 104 }, 105 }, 106 conv: &Addressable{}, 107 wantErrUp: true, 108 wantErrDown: true, 109 }, { 110 name: "v1alpha1 - empty", 111 addr: &Addressable{}, 112 conv: &Addressable{}, 113 wantErrUp: true, 114 wantErrDown: true, 115 }} 116 117 for _, test := range tests { 118 t.Run(test.name, func(t *testing.T) { 119 conv := test.conv 120 if err := test.addr.ConvertTo(context.Background(), conv); err != nil { 121 if !test.wantErrUp { 122 t.Error("ConvertTo() =", err) 123 } 124 } else if test.wantErrUp { 125 t.Errorf("ConvertTo() = %#v, wanted error", conv) 126 } 127 got := &Addressable{} 128 if err := got.ConvertFrom(context.Background(), conv); err != nil { 129 if !test.wantErrDown { 130 t.Error("ConvertFrom() =", err) 131 } 132 return 133 } else if test.wantErrDown { 134 t.Errorf("ConvertFrom() = %#v, wanted error", conv) 135 return 136 } 137 138 if diff := cmp.Diff(test.addr, got); diff != "" { 139 t.Error("roundtrip (-want, +got) =", diff) 140 } 141 }) 142 } 143 } 144 145 func TestConvertTo(t *testing.T) { 146 tests := []struct { 147 name string 148 addr *Addressable 149 conv apis.Convertible 150 want apis.Convertible 151 wantErrUp bool 152 wantErrDown bool 153 }{{ 154 name: "empty to v1beta1", 155 addr: &Addressable{}, 156 conv: &v1beta1.Addressable{}, 157 want: &v1beta1.Addressable{}, 158 wantErrUp: false, 159 wantErrDown: false, 160 }, { 161 name: "to v1beta1", 162 addr: &Addressable{ 163 Hostname: "bar.com", 164 }, 165 conv: &v1beta1.Addressable{}, 166 want: &v1beta1.Addressable{ 167 URL: apis.HTTP("bar.com"), 168 }, 169 wantErrUp: false, 170 wantErrDown: false, 171 }, { 172 name: "empty to v1", 173 addr: &Addressable{}, 174 conv: &v1.Addressable{}, 175 want: &v1.Addressable{}, 176 wantErrUp: false, 177 wantErrDown: false, 178 }, { 179 name: "to v1", 180 addr: &Addressable{ 181 Hostname: "bar.com", 182 }, 183 conv: &v1.Addressable{}, 184 want: &v1.Addressable{ 185 URL: apis.HTTP("bar.com"), 186 }, 187 wantErrUp: false, 188 wantErrDown: false, 189 }} 190 191 for _, test := range tests { 192 t.Run(test.name, func(t *testing.T) { 193 got := test.conv 194 if err := test.addr.ConvertTo(context.Background(), got); err != nil { 195 if !test.wantErrUp { 196 t.Error("ConvertTo() =", err) 197 } 198 } else if test.wantErrUp { 199 t.Errorf("ConvertTo() = %#v, wanted error", got) 200 } 201 202 if diff := cmp.Diff(test.want, got); diff != "" { 203 t.Error("roundtrip (-want, +got) =", diff) 204 } 205 }) 206 } 207 } 208 209 func TestConvertFrom(t *testing.T) { 210 tests := []struct { 211 name string 212 in apis.Convertible 213 want *Addressable 214 }{{ 215 name: "v1beta1", 216 in: &v1beta1.Addressable{URL: apis.HTTP("foo.example.com")}, 217 want: &Addressable{Addressable: v1beta1.Addressable{URL: apis.HTTP("foo.example.com")}, Hostname: "foo.example.com"}, 218 }, { 219 name: "v1", 220 in: &v1.Addressable{URL: apis.HTTP("bar.example.com")}, 221 want: &Addressable{Addressable: v1beta1.Addressable{URL: apis.HTTP("bar.example.com")}, Hostname: "bar.example.com"}, 222 }} 223 224 for _, test := range tests { 225 t.Run(test.name, func(t *testing.T) { 226 got := &Addressable{} 227 if err := got.ConvertFrom(context.Background(), test.in); err != nil { 228 t.Error("ConvertFrom() =", err) 229 } 230 231 if diff := cmp.Diff(test.want, got); diff != "" { 232 t.Error("roundtrip (-want, +got) =", diff) 233 } 234 }) 235 } 236 }