convergence_test.go 13 KB

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