convergence_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "fmt"
  17. "strings"
  18. "testing"
  19. "github.com/compose-spec/compose-go/v2/types"
  20. "github.com/docker/cli/cli/config/configfile"
  21. moby "github.com/docker/docker/api/types"
  22. containerType "github.com/docker/docker/api/types/container"
  23. "github.com/docker/docker/api/types/filters"
  24. "github.com/docker/docker/api/types/network"
  25. "go.uber.org/mock/gomock"
  26. "gotest.tools/v3/assert"
  27. "github.com/docker/compose/v2/pkg/api"
  28. "github.com/docker/compose/v2/pkg/mocks"
  29. "github.com/docker/compose/v2/pkg/progress"
  30. )
  31. func TestContainerName(t *testing.T) {
  32. s := types.ServiceConfig{
  33. Name: "testservicename",
  34. ContainerName: "testcontainername",
  35. Scale: intPtr(1),
  36. Deploy: &types.DeployConfig{},
  37. }
  38. ret, err := getScale(s)
  39. assert.NilError(t, err)
  40. assert.Equal(t, ret, *s.Scale)
  41. s.Scale = intPtr(0)
  42. ret, err = getScale(s)
  43. assert.NilError(t, err)
  44. assert.Equal(t, ret, *s.Scale)
  45. s.Scale = intPtr(2)
  46. _, err = getScale(s)
  47. assert.Error(t, err, fmt.Sprintf(doubledContainerNameWarning, s.Name, s.ContainerName))
  48. }
  49. func intPtr(i int) *int {
  50. return &i
  51. }
  52. func TestServiceLinks(t *testing.T) {
  53. const dbContainerName = "/" + testProject + "-db-1"
  54. const webContainerName = "/" + testProject + "-web-1"
  55. s := types.ServiceConfig{
  56. Name: "web",
  57. Scale: intPtr(1),
  58. }
  59. containerListOptions := containerType.ListOptions{
  60. Filters: filters.NewArgs(
  61. projectFilter(testProject),
  62. serviceFilter("db"),
  63. oneOffFilter(false),
  64. hasConfigHashLabel(),
  65. ),
  66. All: true,
  67. }
  68. t.Run("service links default", func(t *testing.T) {
  69. mockCtrl := gomock.NewController(t)
  70. defer mockCtrl.Finish()
  71. apiClient := mocks.NewMockAPIClient(mockCtrl)
  72. cli := mocks.NewMockCli(mockCtrl)
  73. tested := composeService{
  74. dockerCli: cli,
  75. }
  76. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  77. s.Links = []string{"db"}
  78. c := testContainer("db", dbContainerName, false)
  79. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
  80. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  81. assert.NilError(t, err)
  82. assert.Equal(t, len(links), 3)
  83. assert.Equal(t, links[0], "testProject-db-1:db")
  84. assert.Equal(t, links[1], "testProject-db-1:db-1")
  85. assert.Equal(t, links[2], "testProject-db-1:testProject-db-1")
  86. })
  87. t.Run("service links", func(t *testing.T) {
  88. mockCtrl := gomock.NewController(t)
  89. defer mockCtrl.Finish()
  90. apiClient := mocks.NewMockAPIClient(mockCtrl)
  91. cli := mocks.NewMockCli(mockCtrl)
  92. tested := composeService{
  93. dockerCli: cli,
  94. }
  95. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  96. s.Links = []string{"db:db"}
  97. c := testContainer("db", dbContainerName, false)
  98. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
  99. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  100. assert.NilError(t, err)
  101. assert.Equal(t, len(links), 3)
  102. assert.Equal(t, links[0], "testProject-db-1:db")
  103. assert.Equal(t, links[1], "testProject-db-1:db-1")
  104. assert.Equal(t, links[2], "testProject-db-1:testProject-db-1")
  105. })
  106. t.Run("service links name", func(t *testing.T) {
  107. mockCtrl := gomock.NewController(t)
  108. defer mockCtrl.Finish()
  109. apiClient := mocks.NewMockAPIClient(mockCtrl)
  110. cli := mocks.NewMockCli(mockCtrl)
  111. tested := composeService{
  112. dockerCli: cli,
  113. }
  114. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  115. s.Links = []string{"db:dbname"}
  116. c := testContainer("db", dbContainerName, false)
  117. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
  118. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  119. assert.NilError(t, err)
  120. assert.Equal(t, len(links), 3)
  121. assert.Equal(t, links[0], "testProject-db-1:dbname")
  122. assert.Equal(t, links[1], "testProject-db-1:db-1")
  123. assert.Equal(t, links[2], "testProject-db-1:testProject-db-1")
  124. })
  125. t.Run("service links external links", func(t *testing.T) {
  126. mockCtrl := gomock.NewController(t)
  127. defer mockCtrl.Finish()
  128. apiClient := mocks.NewMockAPIClient(mockCtrl)
  129. cli := mocks.NewMockCli(mockCtrl)
  130. tested := composeService{
  131. dockerCli: cli,
  132. }
  133. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  134. s.Links = []string{"db:dbname"}
  135. s.ExternalLinks = []string{"db1:db2"}
  136. c := testContainer("db", dbContainerName, false)
  137. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptions).Return([]moby.Container{c}, nil)
  138. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  139. assert.NilError(t, err)
  140. assert.Equal(t, len(links), 4)
  141. assert.Equal(t, links[0], "testProject-db-1:dbname")
  142. assert.Equal(t, links[1], "testProject-db-1:db-1")
  143. assert.Equal(t, links[2], "testProject-db-1:testProject-db-1")
  144. // ExternalLink
  145. assert.Equal(t, links[3], "db1:db2")
  146. })
  147. t.Run("service links itself oneoff", func(t *testing.T) {
  148. mockCtrl := gomock.NewController(t)
  149. defer mockCtrl.Finish()
  150. apiClient := mocks.NewMockAPIClient(mockCtrl)
  151. cli := mocks.NewMockCli(mockCtrl)
  152. tested := composeService{
  153. dockerCli: cli,
  154. }
  155. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  156. s.Links = []string{}
  157. s.ExternalLinks = []string{}
  158. s.Labels = s.Labels.Add(api.OneoffLabel, "True")
  159. c := testContainer("web", webContainerName, true)
  160. containerListOptionsOneOff := containerType.ListOptions{
  161. Filters: filters.NewArgs(
  162. projectFilter(testProject),
  163. serviceFilter("web"),
  164. oneOffFilter(false),
  165. hasConfigHashLabel(),
  166. ),
  167. All: true,
  168. }
  169. apiClient.EXPECT().ContainerList(gomock.Any(), containerListOptionsOneOff).Return([]moby.Container{c}, nil)
  170. links, err := tested.getLinks(context.Background(), testProject, s, 1)
  171. assert.NilError(t, err)
  172. assert.Equal(t, len(links), 3)
  173. assert.Equal(t, links[0], "testProject-web-1:web")
  174. assert.Equal(t, links[1], "testProject-web-1:web-1")
  175. assert.Equal(t, links[2], "testProject-web-1:testProject-web-1")
  176. })
  177. }
  178. func TestWaitDependencies(t *testing.T) {
  179. mockCtrl := gomock.NewController(t)
  180. defer mockCtrl.Finish()
  181. apiClient := mocks.NewMockAPIClient(mockCtrl)
  182. cli := mocks.NewMockCli(mockCtrl)
  183. tested := composeService{
  184. dockerCli: cli,
  185. }
  186. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  187. t.Run("should skip dependencies with scale 0", func(t *testing.T) {
  188. dbService := types.ServiceConfig{Name: "db", Scale: intPtr(0)}
  189. redisService := types.ServiceConfig{Name: "redis", Scale: intPtr(0)}
  190. project := types.Project{Name: strings.ToLower(testProject), Services: types.Services{
  191. "db": dbService,
  192. "redis": redisService,
  193. }}
  194. dependencies := types.DependsOnConfig{
  195. "db": {Condition: ServiceConditionRunningOrHealthy},
  196. "redis": {Condition: ServiceConditionRunningOrHealthy},
  197. }
  198. assert.NilError(t, tested.waitDependencies(context.Background(), &project, "", dependencies, nil))
  199. })
  200. t.Run("should skip dependencies with condition service_started", func(t *testing.T) {
  201. dbService := types.ServiceConfig{Name: "db", Scale: intPtr(1)}
  202. redisService := types.ServiceConfig{Name: "redis", Scale: intPtr(1)}
  203. project := types.Project{Name: strings.ToLower(testProject), Services: types.Services{
  204. "db": dbService,
  205. "redis": redisService,
  206. }}
  207. dependencies := types.DependsOnConfig{
  208. "db": {Condition: types.ServiceConditionStarted, Required: true},
  209. "redis": {Condition: types.ServiceConditionStarted, Required: true},
  210. }
  211. assert.NilError(t, tested.waitDependencies(context.Background(), &project, "", dependencies, nil))
  212. })
  213. }
  214. func TestCreateMobyContainer(t *testing.T) {
  215. t.Run("connects container networks one by one if API <1.44", func(t *testing.T) {
  216. mockCtrl := gomock.NewController(t)
  217. defer mockCtrl.Finish()
  218. apiClient := mocks.NewMockAPIClient(mockCtrl)
  219. cli := mocks.NewMockCli(mockCtrl)
  220. tested := composeService{
  221. dockerCli: cli,
  222. }
  223. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  224. cli.EXPECT().ConfigFile().Return(&configfile.ConfigFile{}).AnyTimes()
  225. apiClient.EXPECT().DaemonHost().Return("").AnyTimes()
  226. apiClient.EXPECT().ImageInspectWithRaw(gomock.Any(), gomock.Any()).Return(moby.ImageInspect{}, nil, nil).AnyTimes()
  227. // force `RuntimeVersion` to fetch again
  228. runtimeVersion = runtimeVersionCache{}
  229. apiClient.EXPECT().ServerVersion(gomock.Any()).Return(moby.Version{
  230. APIVersion: "1.43",
  231. }, nil).AnyTimes()
  232. service := types.ServiceConfig{
  233. Name: "test",
  234. Networks: map[string]*types.ServiceNetworkConfig{
  235. "a": {
  236. Priority: 10,
  237. },
  238. "b": {
  239. Priority: 100,
  240. },
  241. },
  242. }
  243. project := types.Project{
  244. Name: "bork",
  245. Services: types.Services{
  246. "test": service,
  247. },
  248. Networks: types.Networks{
  249. "a": types.NetworkConfig{
  250. Name: "a-moby-name",
  251. },
  252. "b": types.NetworkConfig{
  253. Name: "b-moby-name",
  254. },
  255. },
  256. }
  257. apiClient.EXPECT().ContainerCreate(gomock.Any(), gomock.Any(), gomock.Cond(func(x any) bool {
  258. v := x.(*containerType.HostConfig)
  259. return v.NetworkMode == "b-moby-name"
  260. }), gomock.Eq(
  261. &network.NetworkingConfig{
  262. EndpointsConfig: map[string]*network.EndpointSettings{
  263. "b-moby-name": {
  264. IPAMConfig: &network.EndpointIPAMConfig{},
  265. Aliases: []string{"bork-test-0"},
  266. },
  267. },
  268. }), gomock.Any(), gomock.Any()).Times(1).Return(
  269. containerType.CreateResponse{
  270. ID: "an-id",
  271. }, nil)
  272. apiClient.EXPECT().ContainerInspect(gomock.Any(), gomock.Eq("an-id")).Times(1).Return(
  273. moby.ContainerJSON{
  274. ContainerJSONBase: &moby.ContainerJSONBase{
  275. ID: "an-id",
  276. Name: "a-name",
  277. },
  278. Config: &containerType.Config{},
  279. NetworkSettings: &moby.NetworkSettings{},
  280. }, nil)
  281. apiClient.EXPECT().NetworkConnect(gomock.Any(), "a-moby-name", "an-id", gomock.Eq(
  282. &network.EndpointSettings{
  283. IPAMConfig: &network.EndpointIPAMConfig{},
  284. Aliases: []string{"bork-test-0"},
  285. }))
  286. _, err := tested.createMobyContainer(context.Background(), &project, service, "test", 0, nil, createOptions{
  287. Labels: make(types.Labels),
  288. }, progress.ContextWriter(context.TODO()))
  289. assert.NilError(t, err)
  290. })
  291. t.Run("includes all container networks in ContainerCreate call if API >=1.44", func(t *testing.T) {
  292. mockCtrl := gomock.NewController(t)
  293. defer mockCtrl.Finish()
  294. apiClient := mocks.NewMockAPIClient(mockCtrl)
  295. cli := mocks.NewMockCli(mockCtrl)
  296. tested := composeService{
  297. dockerCli: cli,
  298. }
  299. cli.EXPECT().Client().Return(apiClient).AnyTimes()
  300. cli.EXPECT().ConfigFile().Return(&configfile.ConfigFile{}).AnyTimes()
  301. apiClient.EXPECT().DaemonHost().Return("").AnyTimes()
  302. apiClient.EXPECT().ImageInspectWithRaw(gomock.Any(), gomock.Any()).Return(moby.ImageInspect{}, nil, nil).AnyTimes()
  303. // force `RuntimeVersion` to fetch fresh version
  304. runtimeVersion = runtimeVersionCache{}
  305. apiClient.EXPECT().ServerVersion(gomock.Any()).Return(moby.Version{
  306. APIVersion: "1.44",
  307. }, nil).AnyTimes()
  308. service := types.ServiceConfig{
  309. Name: "test",
  310. Networks: map[string]*types.ServiceNetworkConfig{
  311. "a": {
  312. Priority: 10,
  313. },
  314. "b": {
  315. Priority: 100,
  316. },
  317. },
  318. }
  319. project := types.Project{
  320. Name: "bork",
  321. Services: types.Services{
  322. "test": service,
  323. },
  324. Networks: types.Networks{
  325. "a": types.NetworkConfig{
  326. Name: "a-moby-name",
  327. },
  328. "b": types.NetworkConfig{
  329. Name: "b-moby-name",
  330. },
  331. },
  332. }
  333. apiClient.EXPECT().ContainerCreate(gomock.Any(), gomock.Any(), gomock.Cond(func(x any) bool {
  334. v := x.(*containerType.HostConfig)
  335. return v.NetworkMode == "b-moby-name"
  336. }), gomock.Eq(
  337. &network.NetworkingConfig{
  338. EndpointsConfig: map[string]*network.EndpointSettings{
  339. "a-moby-name": {
  340. IPAMConfig: &network.EndpointIPAMConfig{},
  341. Aliases: []string{"bork-test-0"},
  342. },
  343. "b-moby-name": {
  344. IPAMConfig: &network.EndpointIPAMConfig{},
  345. Aliases: []string{"bork-test-0"},
  346. },
  347. },
  348. }), gomock.Any(), gomock.Any()).Times(1).Return(
  349. containerType.CreateResponse{
  350. ID: "an-id",
  351. }, nil)
  352. apiClient.EXPECT().ContainerInspect(gomock.Any(), gomock.Eq("an-id")).Times(1).Return(
  353. moby.ContainerJSON{
  354. ContainerJSONBase: &moby.ContainerJSONBase{
  355. ID: "an-id",
  356. Name: "a-name",
  357. },
  358. Config: &containerType.Config{},
  359. NetworkSettings: &moby.NetworkSettings{},
  360. }, nil)
  361. _, err := tested.createMobyContainer(context.Background(), &project, service, "test", 0, nil, createOptions{
  362. Labels: make(types.Labels),
  363. }, progress.ContextWriter(context.TODO()))
  364. assert.NilError(t, err)
  365. })
  366. }