|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2026 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package hostname_test |
| 20 | + |
| 21 | +import ( |
| 22 | + "testing" |
| 23 | + |
| 24 | + "google.golang.org/grpc/attributes" |
| 25 | + "google.golang.org/grpc/experimental/balancer/hostname" |
| 26 | + "google.golang.org/grpc/internal/grpctest" |
| 27 | + "google.golang.org/grpc/resolver" |
| 28 | +) |
| 29 | + |
| 30 | +type s struct { |
| 31 | + grpctest.Tester |
| 32 | +} |
| 33 | + |
| 34 | +func Test(t *testing.T) { |
| 35 | + grpctest.RunSubTests(t, s{}) |
| 36 | +} |
| 37 | + |
| 38 | +func (s) TestHostnameToAndFromEndpoint(t *testing.T) { |
| 39 | + tests := []struct { |
| 40 | + desc string |
| 41 | + inputHostname string |
| 42 | + inputAttributes *attributes.Attributes |
| 43 | + wantHostname string |
| 44 | + }{ |
| 45 | + { |
| 46 | + desc: "empty_attributes", |
| 47 | + inputHostname: "myservice.example.com", |
| 48 | + wantHostname: "myservice.example.com", |
| 49 | + }, |
| 50 | + { |
| 51 | + desc: "non-empty_attributes", |
| 52 | + inputHostname: "myservice.example.com", |
| 53 | + inputAttributes: attributes.New("foo", "bar"), |
| 54 | + wantHostname: "myservice.example.com", |
| 55 | + }, |
| 56 | + { |
| 57 | + desc: "hostname_not_present_in_empty_attributes", |
| 58 | + }, |
| 59 | + { |
| 60 | + desc: "hostname_not_present_in_non-empty_attributes", |
| 61 | + inputAttributes: attributes.New("foo", "bar"), |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, test := range tests { |
| 66 | + t.Run(test.desc, func(t *testing.T) { |
| 67 | + endpoint := resolver.Endpoint{Attributes: test.inputAttributes} |
| 68 | + endpoint = hostname.Set(endpoint, test.inputHostname) |
| 69 | + gotHostname := hostname.FromEndpoint(endpoint) |
| 70 | + if gotHostname != test.wantHostname { |
| 71 | + t.Errorf("hostname.FromEndpoint(%+v) = %q, want %q", endpoint, gotHostname, test.wantHostname) |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
0 commit comments