container_routes_test.go 5.3 KB

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