dependencies_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. "sort"
  18. "sync"
  19. "testing"
  20. "github.com/compose-spec/compose-go/v2/types"
  21. "gotest.tools/v3/assert"
  22. is "gotest.tools/v3/assert/cmp"
  23. "github.com/docker/compose/v5/pkg/utils"
  24. )
  25. func createTestProject() *types.Project {
  26. return &types.Project{
  27. Services: types.Services{
  28. "test1": {
  29. Name: "test1",
  30. DependsOn: map[string]types.ServiceDependency{
  31. "test2": {},
  32. },
  33. },
  34. "test2": {
  35. Name: "test2",
  36. DependsOn: map[string]types.ServiceDependency{
  37. "test3": {},
  38. },
  39. },
  40. "test3": {
  41. Name: "test3",
  42. },
  43. },
  44. }
  45. }
  46. func TestTraversalWithMultipleParents(t *testing.T) {
  47. dependent := types.ServiceConfig{
  48. Name: "dependent",
  49. DependsOn: make(types.DependsOnConfig),
  50. }
  51. project := types.Project{
  52. Services: types.Services{"dependent": dependent},
  53. }
  54. for i := 1; i <= 100; i++ {
  55. name := fmt.Sprintf("svc_%d", i)
  56. dependent.DependsOn[name] = types.ServiceDependency{}
  57. svc := types.ServiceConfig{Name: name}
  58. project.Services[name] = svc
  59. }
  60. svc := make(chan string, 10)
  61. seen := make(map[string]int)
  62. done := make(chan struct{})
  63. go func() {
  64. for service := range svc {
  65. seen[service]++
  66. }
  67. done <- struct{}{}
  68. }()
  69. err := InDependencyOrder(t.Context(), &project, func(ctx context.Context, service string) error {
  70. svc <- service
  71. return nil
  72. })
  73. assert.NilError(t, err, "Error during iteration")
  74. close(svc)
  75. <-done
  76. assert.Check(t, is.Len(seen, 101))
  77. for svc, count := range seen {
  78. assert.Equal(t, 1, count, "Service: %s", svc)
  79. }
  80. }
  81. func TestInDependencyUpCommandOrder(t *testing.T) {
  82. var order []string
  83. err := InDependencyOrder(t.Context(), createTestProject(), func(ctx context.Context, service string) error {
  84. order = append(order, service)
  85. return nil
  86. })
  87. assert.NilError(t, err, "Error during iteration")
  88. assert.DeepEqual(t, []string{"test3", "test2", "test1"}, order)
  89. }
  90. func TestInDependencyReverseDownCommandOrder(t *testing.T) {
  91. var order []string
  92. err := InReverseDependencyOrder(t.Context(), createTestProject(), func(ctx context.Context, service string) error {
  93. order = append(order, service)
  94. return nil
  95. })
  96. assert.NilError(t, err, "Error during iteration")
  97. assert.DeepEqual(t, []string{"test1", "test2", "test3"}, order)
  98. }
  99. func TestBuildGraph(t *testing.T) {
  100. testCases := []struct {
  101. desc string
  102. services types.Services
  103. expectedVertices map[string]*Vertex
  104. }{
  105. {
  106. desc: "builds graph with single service",
  107. services: types.Services{
  108. "test": {
  109. Name: "test",
  110. DependsOn: types.DependsOnConfig{},
  111. },
  112. },
  113. expectedVertices: map[string]*Vertex{
  114. "test": {
  115. Key: "test",
  116. Service: "test",
  117. Status: ServiceStopped,
  118. Children: map[string]*Vertex{},
  119. Parents: map[string]*Vertex{},
  120. },
  121. },
  122. },
  123. {
  124. desc: "builds graph with two separate services",
  125. services: types.Services{
  126. "test": {
  127. Name: "test",
  128. DependsOn: types.DependsOnConfig{},
  129. },
  130. "another": {
  131. Name: "another",
  132. DependsOn: types.DependsOnConfig{},
  133. },
  134. },
  135. expectedVertices: map[string]*Vertex{
  136. "test": {
  137. Key: "test",
  138. Service: "test",
  139. Status: ServiceStopped,
  140. Children: map[string]*Vertex{},
  141. Parents: map[string]*Vertex{},
  142. },
  143. "another": {
  144. Key: "another",
  145. Service: "another",
  146. Status: ServiceStopped,
  147. Children: map[string]*Vertex{},
  148. Parents: map[string]*Vertex{},
  149. },
  150. },
  151. },
  152. {
  153. desc: "builds graph with a service and a dependency",
  154. services: types.Services{
  155. "test": {
  156. Name: "test",
  157. DependsOn: types.DependsOnConfig{
  158. "another": types.ServiceDependency{},
  159. },
  160. },
  161. "another": {
  162. Name: "another",
  163. DependsOn: types.DependsOnConfig{},
  164. },
  165. },
  166. expectedVertices: map[string]*Vertex{
  167. "test": {
  168. Key: "test",
  169. Service: "test",
  170. Status: ServiceStopped,
  171. Children: map[string]*Vertex{
  172. "another": {},
  173. },
  174. Parents: map[string]*Vertex{},
  175. },
  176. "another": {
  177. Key: "another",
  178. Service: "another",
  179. Status: ServiceStopped,
  180. Children: map[string]*Vertex{},
  181. Parents: map[string]*Vertex{
  182. "test": {},
  183. },
  184. },
  185. },
  186. },
  187. {
  188. desc: "builds graph with multiple dependency levels",
  189. services: types.Services{
  190. "test": {
  191. Name: "test",
  192. DependsOn: types.DependsOnConfig{
  193. "another": types.ServiceDependency{},
  194. },
  195. },
  196. "another": {
  197. Name: "another",
  198. DependsOn: types.DependsOnConfig{
  199. "another_dep": types.ServiceDependency{},
  200. },
  201. },
  202. "another_dep": {
  203. Name: "another_dep",
  204. DependsOn: types.DependsOnConfig{},
  205. },
  206. },
  207. expectedVertices: map[string]*Vertex{
  208. "test": {
  209. Key: "test",
  210. Service: "test",
  211. Status: ServiceStopped,
  212. Children: map[string]*Vertex{
  213. "another": {},
  214. },
  215. Parents: map[string]*Vertex{},
  216. },
  217. "another": {
  218. Key: "another",
  219. Service: "another",
  220. Status: ServiceStopped,
  221. Children: map[string]*Vertex{
  222. "another_dep": {},
  223. },
  224. Parents: map[string]*Vertex{
  225. "test": {},
  226. },
  227. },
  228. "another_dep": {
  229. Key: "another_dep",
  230. Service: "another_dep",
  231. Status: ServiceStopped,
  232. Children: map[string]*Vertex{},
  233. Parents: map[string]*Vertex{
  234. "another": {},
  235. },
  236. },
  237. },
  238. },
  239. }
  240. for _, tC := range testCases {
  241. t.Run(tC.desc, func(t *testing.T) {
  242. project := types.Project{
  243. Services: tC.services,
  244. }
  245. graph, err := NewGraph(&project, ServiceStopped)
  246. assert.NilError(t, err, fmt.Sprintf("failed to build graph for: %s", tC.desc))
  247. for k, vertex := range graph.Vertices {
  248. expected, ok := tC.expectedVertices[k]
  249. assert.Equal(t, true, ok)
  250. assert.Equal(t, true, isVertexEqual(*expected, *vertex))
  251. }
  252. })
  253. }
  254. }
  255. func TestBuildGraphDependsOn(t *testing.T) {
  256. testCases := []struct {
  257. desc string
  258. services types.Services
  259. expectedVertices map[string]*Vertex
  260. }{
  261. {
  262. desc: "service depends on init container which is already removed",
  263. services: types.Services{
  264. "test": {
  265. Name: "test",
  266. DependsOn: types.DependsOnConfig{
  267. "test-removed-init-container": types.ServiceDependency{
  268. Condition: "service_completed_successfully",
  269. Restart: false,
  270. Extensions: types.Extensions(nil),
  271. Required: false,
  272. },
  273. },
  274. },
  275. },
  276. expectedVertices: map[string]*Vertex{
  277. "test": {
  278. Key: "test",
  279. Service: "test",
  280. Status: ServiceStopped,
  281. Children: map[string]*Vertex{},
  282. Parents: map[string]*Vertex{},
  283. },
  284. },
  285. },
  286. }
  287. for _, tC := range testCases {
  288. t.Run(tC.desc, func(t *testing.T) {
  289. project := types.Project{
  290. Services: tC.services,
  291. }
  292. graph, err := NewGraph(&project, ServiceStopped)
  293. assert.NilError(t, err, fmt.Sprintf("failed to build graph for: %s", tC.desc))
  294. for k, vertex := range graph.Vertices {
  295. expected, ok := tC.expectedVertices[k]
  296. assert.Equal(t, true, ok)
  297. assert.Equal(t, true, isVertexEqual(*expected, *vertex))
  298. }
  299. })
  300. }
  301. }
  302. func isVertexEqual(a, b Vertex) bool {
  303. childrenEquality := true
  304. for c := range a.Children {
  305. if _, ok := b.Children[c]; !ok {
  306. childrenEquality = false
  307. }
  308. }
  309. parentEquality := true
  310. for p := range a.Parents {
  311. if _, ok := b.Parents[p]; !ok {
  312. parentEquality = false
  313. }
  314. }
  315. return a.Key == b.Key &&
  316. a.Service == b.Service &&
  317. childrenEquality &&
  318. parentEquality
  319. }
  320. func TestWith_RootNodesAndUp(t *testing.T) {
  321. graph := &Graph{
  322. lock: sync.RWMutex{},
  323. Vertices: map[string]*Vertex{},
  324. }
  325. /** graph topology:
  326. A B
  327. / \ / \
  328. G C E
  329. \ /
  330. D
  331. |
  332. F
  333. */
  334. graph.AddVertex("A", "A", 0)
  335. graph.AddVertex("B", "B", 0)
  336. graph.AddVertex("C", "C", 0)
  337. graph.AddVertex("D", "D", 0)
  338. graph.AddVertex("E", "E", 0)
  339. graph.AddVertex("F", "F", 0)
  340. graph.AddVertex("G", "G", 0)
  341. _ = graph.AddEdge("C", "A")
  342. _ = graph.AddEdge("C", "B")
  343. _ = graph.AddEdge("E", "B")
  344. _ = graph.AddEdge("D", "C")
  345. _ = graph.AddEdge("D", "E")
  346. _ = graph.AddEdge("F", "D")
  347. _ = graph.AddEdge("G", "A")
  348. tests := []struct {
  349. name string
  350. nodes []string
  351. want []string
  352. }{
  353. {
  354. name: "whole graph",
  355. nodes: []string{"A", "B"},
  356. want: []string{"A", "B", "C", "D", "E", "F", "G"},
  357. },
  358. {
  359. name: "only leaves",
  360. nodes: []string{"F", "G"},
  361. want: []string{"F", "G"},
  362. },
  363. {
  364. name: "simple dependent",
  365. nodes: []string{"D"},
  366. want: []string{"D", "F"},
  367. },
  368. {
  369. name: "diamond dependents",
  370. nodes: []string{"B"},
  371. want: []string{"B", "C", "D", "E", "F"},
  372. },
  373. {
  374. name: "partial graph",
  375. nodes: []string{"A"},
  376. want: []string{"A", "C", "D", "F", "G"},
  377. },
  378. }
  379. for _, tt := range tests {
  380. t.Run(tt.name, func(t *testing.T) {
  381. mx := sync.Mutex{}
  382. expected := utils.Set[string]{}
  383. expected.AddAll("C", "G", "D", "F")
  384. var visited []string
  385. gt := downDirectionTraversal(func(ctx context.Context, s string) error {
  386. mx.Lock()
  387. defer mx.Unlock()
  388. visited = append(visited, s)
  389. return nil
  390. })
  391. WithRootNodesAndDown(tt.nodes)(gt)
  392. err := gt.visit(t.Context(), graph)
  393. assert.NilError(t, err)
  394. sort.Strings(visited)
  395. assert.DeepEqual(t, tt.want, visited)
  396. })
  397. }
  398. }