dependencies_test.go 10.0 KB

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