|
| 1 | +/* |
| 2 | + Copyright 2020 The Compose Specification 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 types |
| 18 | + |
| 19 | +import ( |
| 20 | + "sort" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "gotest.tools/v3/assert" |
| 25 | + is "gotest.tools/v3/assert/cmp" |
| 26 | +) |
| 27 | + |
| 28 | +func TestHostsList(t *testing.T) { |
| 29 | + testCases := []struct { |
| 30 | + doc string |
| 31 | + input map[string]any |
| 32 | + expectedError string |
| 33 | + expectedOut string |
| 34 | + }{ |
| 35 | + { |
| 36 | + doc: "IPv4", |
| 37 | + input: map[string]any{"myhost": "192.168.0.1"}, |
| 38 | + expectedOut: "myhost:192.168.0.1", |
| 39 | + }, |
| 40 | + { |
| 41 | + doc: "Weird but permitted, IPv4 with brackets", |
| 42 | + input: map[string]any{"myhost": "[192.168.0.1]"}, |
| 43 | + expectedOut: "myhost:192.168.0.1", |
| 44 | + }, |
| 45 | + { |
| 46 | + doc: "Host and domain", |
| 47 | + input: map[string]any{"host.invalid": "10.0.2.1"}, |
| 48 | + expectedOut: "host.invalid:10.0.2.1", |
| 49 | + }, |
| 50 | + { |
| 51 | + doc: "IPv6", |
| 52 | + input: map[string]any{"anipv6host": "2003:ab34:e::1"}, |
| 53 | + expectedOut: "anipv6host:2003:ab34:e::1", |
| 54 | + }, |
| 55 | + { |
| 56 | + doc: "IPv6, brackets", |
| 57 | + input: map[string]any{"anipv6host": "[2003:ab34:e::1]"}, |
| 58 | + expectedOut: "anipv6host:2003:ab34:e::1", |
| 59 | + }, |
| 60 | + { |
| 61 | + doc: "IPv6 localhost", |
| 62 | + input: map[string]any{"ipv6local": "::1"}, |
| 63 | + expectedOut: "ipv6local:::1", |
| 64 | + }, |
| 65 | + { |
| 66 | + doc: "IPv6 localhost, brackets", |
| 67 | + input: map[string]any{"ipv6local": "[::1]"}, |
| 68 | + expectedOut: "ipv6local:::1", |
| 69 | + }, |
| 70 | + { |
| 71 | + doc: "host-gateway special case", |
| 72 | + input: map[string]any{"host.docker.internal": "host-gateway"}, |
| 73 | + expectedOut: "host.docker.internal:host-gateway", |
| 74 | + }, |
| 75 | + { |
| 76 | + doc: "multiple inputs", |
| 77 | + input: map[string]any{ |
| 78 | + "myhost": "192.168.0.1", |
| 79 | + "anipv6host": "[2003:ab34:e::1]", |
| 80 | + "host.docker.internal": "host-gateway", |
| 81 | + }, |
| 82 | + expectedOut: "anipv6host:2003:ab34:e::1 host.docker.internal:host-gateway myhost:192.168.0.1", |
| 83 | + }, |
| 84 | + { |
| 85 | + // This won't work, but address validation is left to the engine. |
| 86 | + doc: "no ip", |
| 87 | + input: map[string]any{"myhost": nil}, |
| 88 | + expectedOut: "myhost:", |
| 89 | + }, |
| 90 | + { |
| 91 | + doc: "bad host, colon", |
| 92 | + input: map[string]any{":": "::1"}, |
| 93 | + expectedError: "bad host name", |
| 94 | + }, |
| 95 | + { |
| 96 | + doc: "bad host, eq", |
| 97 | + input: map[string]any{"=": "::1"}, |
| 98 | + expectedError: "bad host name", |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + inputAsList := func(input map[string]any, sep string) []any { |
| 103 | + result := make([]any, 0, len(input)) |
| 104 | + for host, ip := range input { |
| 105 | + if ip == nil { |
| 106 | + result = append(result, host+sep) |
| 107 | + } else { |
| 108 | + result = append(result, host+sep+ip.(string)) |
| 109 | + } |
| 110 | + } |
| 111 | + return result |
| 112 | + } |
| 113 | + |
| 114 | + for _, tc := range testCases { |
| 115 | + // Decode the input map, check the output is as-expected. |
| 116 | + var hlFromMap HostsList |
| 117 | + t.Run(tc.doc+"_map", func(t *testing.T) { |
| 118 | + err := hlFromMap.DecodeMapstructure(tc.input) |
| 119 | + if tc.expectedError == "" { |
| 120 | + assert.NilError(t, err) |
| 121 | + actualOut := hlFromMap.AsList(":") |
| 122 | + sort.Strings(actualOut) |
| 123 | + sortedActualStr := strings.Join(actualOut, " ") |
| 124 | + assert.Check(t, is.Equal(sortedActualStr, tc.expectedOut)) |
| 125 | + |
| 126 | + // The YAML rendering of HostsList should be the same as the AsList() output, but |
| 127 | + // with '=' separators. |
| 128 | + yamlOut, err := hlFromMap.MarshalYAML() |
| 129 | + assert.NilError(t, err) |
| 130 | + expYAMLOut := make([]string, len(actualOut)) |
| 131 | + for i, s := range actualOut { |
| 132 | + expYAMLOut[i] = strings.Replace(s, ":", "=", 1) |
| 133 | + } |
| 134 | + assert.DeepEqual(t, yamlOut.([]string), expYAMLOut) |
| 135 | + |
| 136 | + // The JSON rendering of HostsList should also have '=' separators. Same as the |
| 137 | + // YAML output, but as a JSON list of strings. |
| 138 | + jsonOut, err := hlFromMap.MarshalJSON() |
| 139 | + assert.NilError(t, err) |
| 140 | + expJSONStrings := make([]string, len(expYAMLOut)) |
| 141 | + for i, s := range expYAMLOut { |
| 142 | + expJSONStrings[i] = `"` + s + `"` |
| 143 | + } |
| 144 | + expJSONString := "[" + strings.Join(expJSONStrings, ",") + "]" |
| 145 | + assert.Check(t, is.Equal(string(jsonOut), expJSONString)) |
| 146 | + } else { |
| 147 | + assert.ErrorContains(t, err, tc.expectedError) |
| 148 | + } |
| 149 | + }) |
| 150 | + |
| 151 | + // Convert the input into a ':' separated list, check that the result is the same |
| 152 | + // as for the map-input. |
| 153 | + t.Run(tc.doc+"_colon_sep", func(t *testing.T) { |
| 154 | + var hl HostsList |
| 155 | + err := hl.DecodeMapstructure(inputAsList(tc.input, ":")) |
| 156 | + if tc.expectedError == "" { |
| 157 | + assert.NilError(t, err) |
| 158 | + assert.DeepEqual(t, hl, hlFromMap) |
| 159 | + } else { |
| 160 | + assert.ErrorContains(t, err, tc.expectedError) |
| 161 | + } |
| 162 | + }) |
| 163 | + |
| 164 | + // Convert the input into a ':' separated list, check that the result is the same |
| 165 | + // as for the map-input. |
| 166 | + t.Run(tc.doc+"_eq_sep", func(t *testing.T) { |
| 167 | + var hl HostsList |
| 168 | + err := hl.DecodeMapstructure(inputAsList(tc.input, "=")) |
| 169 | + if tc.expectedError == "" { |
| 170 | + assert.NilError(t, err) |
| 171 | + assert.DeepEqual(t, hl, hlFromMap) |
| 172 | + } else { |
| 173 | + assert.ErrorContains(t, err, tc.expectedError) |
| 174 | + } |
| 175 | + }) |
| 176 | + } |
| 177 | +} |
0 commit comments