viz_test.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. "strconv"
  17. "testing"
  18. "github.com/compose-spec/compose-go/types"
  19. "github.com/golang/mock/gomock"
  20. "github.com/stretchr/testify/assert"
  21. compose "github.com/docker/compose/v2/pkg/api"
  22. "github.com/docker/compose/v2/pkg/mocks"
  23. )
  24. func TestViz(t *testing.T) {
  25. project := types.Project{
  26. Name: "viz-test",
  27. WorkingDir: "/home",
  28. Services: types.Services{
  29. {
  30. Name: "service1",
  31. Image: "image-for-service1",
  32. Ports: []types.ServicePortConfig{
  33. {
  34. Published: "80",
  35. Target: 80,
  36. Protocol: "tcp",
  37. },
  38. {
  39. Published: "53",
  40. Target: 533,
  41. Protocol: "udp",
  42. },
  43. },
  44. Networks: map[string]*types.ServiceNetworkConfig{
  45. "internal": nil,
  46. },
  47. },
  48. {
  49. Name: "service2",
  50. Image: "image-for-service2",
  51. Ports: []types.ServicePortConfig{},
  52. },
  53. {
  54. Name: "service3",
  55. Image: "some-image",
  56. DependsOn: map[string]types.ServiceDependency{
  57. "service2": {},
  58. "service1": {},
  59. },
  60. },
  61. {
  62. Name: "service4",
  63. Image: "another-image",
  64. DependsOn: map[string]types.ServiceDependency{
  65. "service3": {},
  66. },
  67. Ports: []types.ServicePortConfig{
  68. {
  69. Published: "8080",
  70. Target: 80,
  71. },
  72. },
  73. Networks: map[string]*types.ServiceNetworkConfig{
  74. "external": nil,
  75. },
  76. },
  77. {
  78. Name: "With host IP",
  79. Image: "user/image-name",
  80. DependsOn: map[string]types.ServiceDependency{
  81. "service1": {},
  82. },
  83. Ports: []types.ServicePortConfig{
  84. {
  85. Published: "8888",
  86. Target: 8080,
  87. HostIP: "127.0.0.1",
  88. },
  89. },
  90. },
  91. },
  92. Networks: types.Networks{
  93. "internal": types.NetworkConfig{},
  94. "external": types.NetworkConfig{},
  95. "not-used": types.NetworkConfig{},
  96. },
  97. Volumes: nil,
  98. Secrets: nil,
  99. Configs: nil,
  100. Extensions: nil,
  101. ComposeFiles: nil,
  102. Environment: nil,
  103. DisabledServices: nil,
  104. Profiles: nil,
  105. }
  106. mockCtrl := gomock.NewController(t)
  107. defer mockCtrl.Finish()
  108. cli := mocks.NewMockCli(mockCtrl)
  109. tested := composeService{
  110. dockerCli: cli,
  111. }
  112. ctx := context.Background()
  113. t.Run("viz (no ports, networks or image)", func(t *testing.T) {
  114. graphStr, err := tested.Viz(ctx, &project, compose.VizOptions{
  115. Indentation: " ",
  116. IncludePorts: false,
  117. IncludeImageName: false,
  118. IncludeNetworks: false,
  119. })
  120. assert.NoError(t, err, "viz command failed")
  121. // check indentation
  122. assert.Contains(t, graphStr, "\n ", graphStr)
  123. assert.NotContains(t, graphStr, "\n ", graphStr)
  124. // check digraph name
  125. assert.Contains(t, graphStr, "digraph \""+project.Name+"\"", graphStr)
  126. // check nodes
  127. for _, service := range project.Services {
  128. assert.Contains(t, graphStr, "\""+service.Name+"\" [style=\"filled\"", graphStr)
  129. }
  130. // check node attributes
  131. assert.NotContains(t, graphStr, "Networks", graphStr)
  132. assert.NotContains(t, graphStr, "Image", graphStr)
  133. assert.NotContains(t, graphStr, "Ports", graphStr)
  134. // check edges that SHOULD exist in the generated graph
  135. allowedEdges := make(map[string][]string)
  136. for _, service := range project.Services {
  137. allowedEdges[service.Name] = make([]string, 0, len(service.DependsOn))
  138. for depName := range service.DependsOn {
  139. allowedEdges[service.Name] = append(allowedEdges[service.Name], depName)
  140. }
  141. }
  142. for serviceName, dependencies := range allowedEdges {
  143. for _, dependencyName := range dependencies {
  144. assert.Contains(t, graphStr, "\""+serviceName+"\" -> \""+dependencyName+"\"", graphStr)
  145. }
  146. }
  147. // check edges that SHOULD NOT exist in the generated graph
  148. forbiddenEdges := make(map[string][]string)
  149. for _, service := range project.Services {
  150. forbiddenEdges[service.Name] = make([]string, 0, len(project.ServiceNames())-len(service.DependsOn))
  151. for _, serviceName := range project.ServiceNames() {
  152. _, edgeExists := service.DependsOn[serviceName]
  153. if !edgeExists {
  154. forbiddenEdges[service.Name] = append(forbiddenEdges[service.Name], serviceName)
  155. }
  156. }
  157. }
  158. for serviceName, forbiddenDeps := range forbiddenEdges {
  159. for _, forbiddenDep := range forbiddenDeps {
  160. assert.NotContains(t, graphStr, "\""+serviceName+"\" -> \""+forbiddenDep+"\"")
  161. }
  162. }
  163. })
  164. t.Run("viz (with ports, networks and image)", func(t *testing.T) {
  165. graphStr, err := tested.Viz(ctx, &project, compose.VizOptions{
  166. Indentation: "\t",
  167. IncludePorts: true,
  168. IncludeImageName: true,
  169. IncludeNetworks: true,
  170. })
  171. assert.NoError(t, err, "viz command failed")
  172. // check indentation
  173. assert.Contains(t, graphStr, "\n\t", graphStr)
  174. assert.NotContains(t, graphStr, "\n\t\t", graphStr)
  175. // check digraph name
  176. assert.Contains(t, graphStr, "digraph \""+project.Name+"\"", graphStr)
  177. // check nodes
  178. for _, service := range project.Services {
  179. assert.Contains(t, graphStr, "\""+service.Name+"\" [style=\"filled\"", graphStr)
  180. }
  181. // check node attributes
  182. assert.Contains(t, graphStr, "Networks", graphStr)
  183. assert.Contains(t, graphStr, ">internal<", graphStr)
  184. assert.Contains(t, graphStr, ">external<", graphStr)
  185. assert.Contains(t, graphStr, "Image", graphStr)
  186. for _, service := range project.Services {
  187. assert.Contains(t, graphStr, ">"+service.Image+"<", graphStr)
  188. }
  189. assert.Contains(t, graphStr, "Ports", graphStr)
  190. for _, service := range project.Services {
  191. for _, portConfig := range service.Ports {
  192. assert.NotContains(t, graphStr, ">"+portConfig.Published+":"+strconv.Itoa(int(portConfig.Target))+"<", graphStr)
  193. }
  194. }
  195. })
  196. }