|
| 1 | +package container |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/docker/docker/api/types/container" |
| 7 | + "github.com/docker/docker/api/types/network" |
| 8 | + "gotest.tools/v3/assert" |
| 9 | + is "gotest.tools/v3/assert/cmp" |
| 10 | +) |
| 11 | + |
| 12 | +func TestHandleMACAddressBC(t *testing.T) { |
| 13 | + testcases := []struct { |
| 14 | + name string |
| 15 | + apiVersion string |
| 16 | + ctrWideMAC string |
| 17 | + networkMode container.NetworkMode |
| 18 | + epConfig map[string]*network.EndpointSettings |
| 19 | + expEpWithCtrWideMAC string |
| 20 | + expEpWithNoMAC string |
| 21 | + expCtrWideMAC string |
| 22 | + expWarning string |
| 23 | + expError string |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "old api ctr-wide mac mix id and name", |
| 27 | + apiVersion: "1.43", |
| 28 | + ctrWideMAC: "11:22:33:44:55:66", |
| 29 | + networkMode: "aNetId", |
| 30 | + epConfig: map[string]*network.EndpointSettings{"aNetName": {}}, |
| 31 | + expEpWithCtrWideMAC: "aNetName", |
| 32 | + expCtrWideMAC: "11:22:33:44:55:66", |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "old api clear ep mac", |
| 36 | + apiVersion: "1.43", |
| 37 | + networkMode: "aNetId", |
| 38 | + epConfig: map[string]*network.EndpointSettings{"aNetName": {MacAddress: "11:22:33:44:55:66"}}, |
| 39 | + expEpWithNoMAC: "aNetName", |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "old api no-network ctr-wide mac", |
| 43 | + apiVersion: "1.43", |
| 44 | + networkMode: "none", |
| 45 | + ctrWideMAC: "11:22:33:44:55:66", |
| 46 | + expError: "conflicting options: mac-address and the network mode", |
| 47 | + expCtrWideMAC: "11:22:33:44:55:66", |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "old api create ep", |
| 51 | + apiVersion: "1.43", |
| 52 | + networkMode: "aNetId", |
| 53 | + ctrWideMAC: "11:22:33:44:55:66", |
| 54 | + epConfig: map[string]*network.EndpointSettings{}, |
| 55 | + expEpWithCtrWideMAC: "aNetId", |
| 56 | + expCtrWideMAC: "11:22:33:44:55:66", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "old api migrate ctr-wide mac", |
| 60 | + apiVersion: "1.43", |
| 61 | + ctrWideMAC: "11:22:33:44:55:66", |
| 62 | + networkMode: "aNetName", |
| 63 | + epConfig: map[string]*network.EndpointSettings{"aNetName": {}}, |
| 64 | + expEpWithCtrWideMAC: "aNetName", |
| 65 | + expCtrWideMAC: "11:22:33:44:55:66", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "new api no macs", |
| 69 | + apiVersion: "1.44", |
| 70 | + networkMode: "aNetId", |
| 71 | + epConfig: map[string]*network.EndpointSettings{"aNetName": {}}, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "new api ep specific mac", |
| 75 | + apiVersion: "1.44", |
| 76 | + networkMode: "aNetName", |
| 77 | + epConfig: map[string]*network.EndpointSettings{"aNetName": {MacAddress: "11:22:33:44:55:66"}}, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "new api migrate ctr-wide mac to new ep", |
| 81 | + apiVersion: "1.44", |
| 82 | + ctrWideMAC: "11:22:33:44:55:66", |
| 83 | + networkMode: "aNetName", |
| 84 | + epConfig: map[string]*network.EndpointSettings{}, |
| 85 | + expEpWithCtrWideMAC: "aNetName", |
| 86 | + expWarning: "The container-wide MacAddress field is now deprecated", |
| 87 | + expCtrWideMAC: "", |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "new api migrate ctr-wide mac to existing ep", |
| 91 | + apiVersion: "1.44", |
| 92 | + ctrWideMAC: "11:22:33:44:55:66", |
| 93 | + networkMode: "aNetName", |
| 94 | + epConfig: map[string]*network.EndpointSettings{"aNetName": {}}, |
| 95 | + expEpWithCtrWideMAC: "aNetName", |
| 96 | + expWarning: "The container-wide MacAddress field is now deprecated", |
| 97 | + expCtrWideMAC: "", |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "new api mode vs name mismatch", |
| 101 | + apiVersion: "1.44", |
| 102 | + ctrWideMAC: "11:22:33:44:55:66", |
| 103 | + networkMode: "aNetId", |
| 104 | + epConfig: map[string]*network.EndpointSettings{"aNetName": {}}, |
| 105 | + expError: "if a container-wide MAC address is supplied, HostConfig.NetworkMode must match the identity of a network in NetworkSettings.Networks", |
| 106 | + expCtrWideMAC: "11:22:33:44:55:66", |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "new api mac mismatch", |
| 110 | + apiVersion: "1.44", |
| 111 | + ctrWideMAC: "11:22:33:44:55:66", |
| 112 | + networkMode: "aNetName", |
| 113 | + epConfig: map[string]*network.EndpointSettings{"aNetName": {MacAddress: "00:11:22:33:44:55"}}, |
| 114 | + expError: "the container-wide MAC address must match the endpoint-specific MAC address", |
| 115 | + expCtrWideMAC: "11:22:33:44:55:66", |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + for _, tc := range testcases { |
| 120 | + t.Run(tc.name, func(t *testing.T) { |
| 121 | + cfg := &container.Config{ |
| 122 | + MacAddress: tc.ctrWideMAC, //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44. |
| 123 | + } |
| 124 | + hostCfg := &container.HostConfig{ |
| 125 | + NetworkMode: tc.networkMode, |
| 126 | + } |
| 127 | + epConfig := make(map[string]*network.EndpointSettings, len(tc.epConfig)) |
| 128 | + for k, v := range tc.epConfig { |
| 129 | + v := v |
| 130 | + epConfig[k] = v |
| 131 | + } |
| 132 | + netCfg := &network.NetworkingConfig{ |
| 133 | + EndpointsConfig: epConfig, |
| 134 | + } |
| 135 | + |
| 136 | + warning, err := handleMACAddressBC(cfg, hostCfg, netCfg, tc.apiVersion) |
| 137 | + |
| 138 | + if tc.expError == "" { |
| 139 | + assert.Check(t, err) |
| 140 | + } else { |
| 141 | + assert.Check(t, is.ErrorContains(err, tc.expError)) |
| 142 | + } |
| 143 | + if tc.expWarning == "" { |
| 144 | + assert.Check(t, is.Equal(warning, "")) |
| 145 | + } else { |
| 146 | + assert.Check(t, is.Contains(warning, tc.expWarning)) |
| 147 | + } |
| 148 | + if tc.expEpWithCtrWideMAC != "" { |
| 149 | + got := netCfg.EndpointsConfig[tc.expEpWithCtrWideMAC].MacAddress |
| 150 | + assert.Check(t, is.Equal(got, tc.ctrWideMAC)) |
| 151 | + } |
| 152 | + if tc.expEpWithNoMAC != "" { |
| 153 | + got := netCfg.EndpointsConfig[tc.expEpWithNoMAC].MacAddress |
| 154 | + assert.Check(t, is.Equal(got, "")) |
| 155 | + } |
| 156 | + gotCtrWideMAC := cfg.MacAddress //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44. |
| 157 | + assert.Check(t, is.Equal(gotCtrWideMAC, tc.expCtrWideMAC)) |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
0 commit comments