dependencies_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. "testing"
  18. "github.com/compose-spec/compose-go/types"
  19. testify "github.com/stretchr/testify/assert"
  20. "github.com/stretchr/testify/require"
  21. "gotest.tools/v3/assert"
  22. )
  23. func createTestProject() *types.Project {
  24. return &types.Project{
  25. Services: []types.ServiceConfig{
  26. {
  27. Name: "test1",
  28. DependsOn: map[string]types.ServiceDependency{
  29. "test2": {},
  30. },
  31. },
  32. {
  33. Name: "test2",
  34. DependsOn: map[string]types.ServiceDependency{
  35. "test3": {},
  36. },
  37. },
  38. {
  39. Name: "test3",
  40. },
  41. },
  42. }
  43. }
  44. func TestTraversalWithMultipleParents(t *testing.T) {
  45. dependent := types.ServiceConfig{
  46. Name: "dependent",
  47. DependsOn: make(types.DependsOnConfig),
  48. }
  49. project := types.Project{
  50. Services: []types.ServiceConfig{dependent},
  51. }
  52. for i := 1; i <= 100; i++ {
  53. name := fmt.Sprintf("svc_%d", i)
  54. dependent.DependsOn[name] = types.ServiceDependency{}
  55. svc := types.ServiceConfig{Name: name}
  56. project.Services = append(project.Services, svc)
  57. }
  58. ctx, cancel := context.WithCancel(context.Background())
  59. t.Cleanup(cancel)
  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(ctx, &project, func(ctx context.Context, service string) error {
  70. svc <- service
  71. return nil
  72. })
  73. require.NoError(t, err, "Error during iteration")
  74. close(svc)
  75. <-done
  76. testify.Len(t, 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. ctx, cancel := context.WithCancel(context.Background())
  83. t.Cleanup(cancel)
  84. var order []string
  85. err := InDependencyOrder(ctx, createTestProject(), func(ctx context.Context, service string) error {
  86. order = append(order, service)
  87. return nil
  88. })
  89. require.NoError(t, err, "Error during iteration")
  90. require.Equal(t, []string{"test3", "test2", "test1"}, order)
  91. }
  92. func TestInDependencyReverseDownCommandOrder(t *testing.T) {
  93. ctx, cancel := context.WithCancel(context.Background())
  94. t.Cleanup(cancel)
  95. var order []string
  96. err := InReverseDependencyOrder(ctx, createTestProject(), func(ctx context.Context, service string) error {
  97. order = append(order, service)
  98. return nil
  99. })
  100. require.NoError(t, err, "Error during iteration")
  101. require.Equal(t, []string{"test1", "test2", "test3"}, order)
  102. }
  103. func TestBuildGraph(t *testing.T) {
  104. testCases := []struct {
  105. desc string
  106. services types.Services
  107. expectedVertices map[string]*Vertex
  108. }{
  109. {
  110. desc: "builds graph with single service",
  111. services: types.Services{
  112. {
  113. Name: "test",
  114. DependsOn: types.DependsOnConfig{},
  115. },
  116. },
  117. expectedVertices: map[string]*Vertex{
  118. "test": {
  119. Key: "test",
  120. Service: "test",
  121. Status: ServiceStopped,
  122. Children: map[string]*Vertex{},
  123. Parents: map[string]*Vertex{},
  124. },
  125. },
  126. },
  127. {
  128. desc: "builds graph with two separate services",
  129. services: types.Services{
  130. {
  131. Name: "test",
  132. DependsOn: types.DependsOnConfig{},
  133. },
  134. {
  135. Name: "another",
  136. DependsOn: types.DependsOnConfig{},
  137. },
  138. },
  139. expectedVertices: map[string]*Vertex{
  140. "test": {
  141. Key: "test",
  142. Service: "test",
  143. Status: ServiceStopped,
  144. Children: map[string]*Vertex{},
  145. Parents: map[string]*Vertex{},
  146. },
  147. "another": {
  148. Key: "another",
  149. Service: "another",
  150. Status: ServiceStopped,
  151. Children: map[string]*Vertex{},
  152. Parents: map[string]*Vertex{},
  153. },
  154. },
  155. },
  156. {
  157. desc: "builds graph with a service and a dependency",
  158. services: types.Services{
  159. {
  160. Name: "test",
  161. DependsOn: types.DependsOnConfig{
  162. "another": types.ServiceDependency{},
  163. },
  164. },
  165. {
  166. Name: "another",
  167. DependsOn: types.DependsOnConfig{},
  168. },
  169. },
  170. expectedVertices: map[string]*Vertex{
  171. "test": {
  172. Key: "test",
  173. Service: "test",
  174. Status: ServiceStopped,
  175. Children: map[string]*Vertex{
  176. "another": {},
  177. },
  178. Parents: map[string]*Vertex{},
  179. },
  180. "another": {
  181. Key: "another",
  182. Service: "another",
  183. Status: ServiceStopped,
  184. Children: map[string]*Vertex{},
  185. Parents: map[string]*Vertex{
  186. "test": {},
  187. },
  188. },
  189. },
  190. },
  191. {
  192. desc: "builds graph with multiple dependency levels",
  193. services: types.Services{
  194. {
  195. Name: "test",
  196. DependsOn: types.DependsOnConfig{
  197. "another": types.ServiceDependency{},
  198. },
  199. },
  200. {
  201. Name: "another",
  202. DependsOn: types.DependsOnConfig{
  203. "another_dep": types.ServiceDependency{},
  204. },
  205. },
  206. {
  207. Name: "another_dep",
  208. DependsOn: types.DependsOnConfig{},
  209. },
  210. },
  211. expectedVertices: map[string]*Vertex{
  212. "test": {
  213. Key: "test",
  214. Service: "test",
  215. Status: ServiceStopped,
  216. Children: map[string]*Vertex{
  217. "another": {},
  218. },
  219. Parents: map[string]*Vertex{},
  220. },
  221. "another": {
  222. Key: "another",
  223. Service: "another",
  224. Status: ServiceStopped,
  225. Children: map[string]*Vertex{
  226. "another_dep": {},
  227. },
  228. Parents: map[string]*Vertex{
  229. "test": {},
  230. },
  231. },
  232. "another_dep": {
  233. Key: "another_dep",
  234. Service: "another_dep",
  235. Status: ServiceStopped,
  236. Children: map[string]*Vertex{},
  237. Parents: map[string]*Vertex{
  238. "another": {},
  239. },
  240. },
  241. },
  242. },
  243. }
  244. for _, tC := range testCases {
  245. t.Run(tC.desc, func(t *testing.T) {
  246. project := types.Project{
  247. Services: tC.services,
  248. }
  249. graph, err := NewGraph(project.Services, ServiceStopped)
  250. assert.NilError(t, err, fmt.Sprintf("failed to build graph for: %s", tC.desc))
  251. for k, vertex := range graph.Vertices {
  252. expected, ok := tC.expectedVertices[k]
  253. assert.Equal(t, true, ok)
  254. assert.Equal(t, true, isVertexEqual(*expected, *vertex))
  255. }
  256. })
  257. }
  258. }
  259. func isVertexEqual(a, b Vertex) bool {
  260. childrenEquality := true
  261. for c := range a.Children {
  262. if _, ok := b.Children[c]; !ok {
  263. childrenEquality = false
  264. }
  265. }
  266. parentEquality := true
  267. for p := range a.Parents {
  268. if _, ok := b.Parents[p]; !ok {
  269. parentEquality = false
  270. }
  271. }
  272. return a.Key == b.Key &&
  273. a.Service == b.Service &&
  274. childrenEquality &&
  275. parentEquality
  276. }