dependencies_test.go 5.7 KB

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