|
| 1 | +/* |
| 2 | + Copyright The containerd 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 containerutil |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +func TestParseExtraHosts(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + extraHosts []string |
| 28 | + hostGateway string |
| 29 | + separator string |
| 30 | + expected []string |
| 31 | + expectedErrStr string |
| 32 | + }{ |
| 33 | + { |
| 34 | + name: "NoExtraHosts", |
| 35 | + expected: []string{}, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "ExtraHosts", |
| 39 | + extraHosts: []string{"localhost:127.0.0.1", "localhost:[::1]"}, |
| 40 | + separator: ":", |
| 41 | + expected: []string{"localhost:127.0.0.1", "localhost:[::1]"}, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "EqualsSeperator", |
| 45 | + extraHosts: []string{"localhost:127.0.0.1", "localhost:[::1]"}, |
| 46 | + separator: "=", |
| 47 | + expected: []string{"localhost=127.0.0.1", "localhost=[::1]"}, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "InvalidExtraHostFormat", |
| 51 | + extraHosts: []string{"localhost"}, |
| 52 | + expectedErrStr: "bad format for add-host: \"localhost\"", |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "ErrorOnHostGatewayExtraHostWithNoHostGatewayIPSet", |
| 56 | + extraHosts: []string{"localhost:host-gateway"}, |
| 57 | + separator: ":", |
| 58 | + expectedErrStr: "unable to derive the IP value for host-gateway", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "HostGatewayIP", |
| 62 | + extraHosts: []string{"localhost:host-gateway"}, |
| 63 | + hostGateway: "10.10.0.1", |
| 64 | + separator: ":", |
| 65 | + expected: []string{"localhost:10.10.0.1"}, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + for _, test := range tests { |
| 70 | + t.Run(test.name, func(t *testing.T) { |
| 71 | + extraHosts, err := ParseExtraHosts(test.extraHosts, test.hostGateway, test.separator) |
| 72 | + if err != nil && err.Error() != test.expectedErrStr { |
| 73 | + t.Fatalf("expected '%s', actual '%v'", test.expectedErrStr, err) |
| 74 | + } else if err == nil && test.expectedErrStr != "" { |
| 75 | + t.Fatalf("expected error '%s' but got none", test.expectedErrStr) |
| 76 | + } |
| 77 | + |
| 78 | + if !reflect.DeepEqual(test.expected, extraHosts) { |
| 79 | + t.Fatalf("expected %v, actual %v", test.expected, extraHosts) |
| 80 | + } |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
0 commit comments