dependencies_test.go 9.8 KB

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