dependencies_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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/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. {
  30. Name: "test1",
  31. DependsOn: map[string]types.ServiceDependency{
  32. "test2": {},
  33. },
  34. },
  35. {
  36. Name: "test2",
  37. DependsOn: map[string]types.ServiceDependency{
  38. "test3": {},
  39. },
  40. },
  41. {
  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},
  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 = append(project.Services, 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. {
  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. {
  134. Name: "test",
  135. DependsOn: types.DependsOnConfig{},
  136. },
  137. {
  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. {
  163. Name: "test",
  164. DependsOn: types.DependsOnConfig{
  165. "another": types.ServiceDependency{},
  166. },
  167. },
  168. {
  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. {
  198. Name: "test",
  199. DependsOn: types.DependsOnConfig{
  200. "another": types.ServiceDependency{},
  201. },
  202. },
  203. {
  204. Name: "another",
  205. DependsOn: types.DependsOnConfig{
  206. "another_dep": types.ServiceDependency{},
  207. },
  208. },
  209. {
  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 isVertexEqual(a, b Vertex) bool {
  263. childrenEquality := true
  264. for c := range a.Children {
  265. if _, ok := b.Children[c]; !ok {
  266. childrenEquality = false
  267. }
  268. }
  269. parentEquality := true
  270. for p := range a.Parents {
  271. if _, ok := b.Parents[p]; !ok {
  272. parentEquality = false
  273. }
  274. }
  275. return a.Key == b.Key &&
  276. a.Service == b.Service &&
  277. childrenEquality &&
  278. parentEquality
  279. }
  280. func TestWith_RootNodesAndUp(t *testing.T) {
  281. graph := &Graph{
  282. lock: sync.RWMutex{},
  283. Vertices: map[string]*Vertex{},
  284. }
  285. /** graph topology:
  286. A B
  287. / \ / \
  288. G C E
  289. \ /
  290. D
  291. |
  292. F
  293. */
  294. graph.AddVertex("A", "A", 0)
  295. graph.AddVertex("B", "B", 0)
  296. graph.AddVertex("C", "C", 0)
  297. graph.AddVertex("D", "D", 0)
  298. graph.AddVertex("E", "E", 0)
  299. graph.AddVertex("F", "F", 0)
  300. graph.AddVertex("G", "G", 0)
  301. _ = graph.AddEdge("C", "A")
  302. _ = graph.AddEdge("C", "B")
  303. _ = graph.AddEdge("E", "B")
  304. _ = graph.AddEdge("D", "C")
  305. _ = graph.AddEdge("D", "E")
  306. _ = graph.AddEdge("F", "D")
  307. _ = graph.AddEdge("G", "A")
  308. tests := []struct {
  309. name string
  310. nodes []string
  311. want []string
  312. }{
  313. {
  314. name: "whole graph",
  315. nodes: []string{"A", "B"},
  316. want: []string{"A", "B", "C", "D", "E", "F", "G"},
  317. },
  318. {
  319. name: "only leaves",
  320. nodes: []string{"F", "G"},
  321. want: []string{"F", "G"},
  322. },
  323. {
  324. name: "simple dependent",
  325. nodes: []string{"D"},
  326. want: []string{"D", "F"},
  327. },
  328. {
  329. name: "diamond dependents",
  330. nodes: []string{"B"},
  331. want: []string{"B", "C", "D", "E", "F"},
  332. },
  333. {
  334. name: "partial graph",
  335. nodes: []string{"A"},
  336. want: []string{"A", "C", "D", "F", "G"},
  337. },
  338. }
  339. for _, tt := range tests {
  340. t.Run(tt.name, func(t *testing.T) {
  341. mx := sync.Mutex{}
  342. expected := utils.Set[string]{}
  343. expected.AddAll("C", "G", "D", "F")
  344. var visited []string
  345. gt := downDirectionTraversal(func(ctx context.Context, s string) error {
  346. mx.Lock()
  347. defer mx.Unlock()
  348. visited = append(visited, s)
  349. return nil
  350. })
  351. WithRootNodesAndDown(tt.nodes)(gt)
  352. err := gt.visit(context.TODO(), graph)
  353. assert.NilError(t, err)
  354. sort.Strings(visited)
  355. assert.DeepEqual(t, tt.want, visited)
  356. })
  357. }
  358. }