dependencies_test.go 6.7 KB

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