viz_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.ServiceConfig{
  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. Networks: types.Networks{
  79. "internal": types.NetworkConfig{},
  80. "external": types.NetworkConfig{},
  81. "not-used": types.NetworkConfig{},
  82. },
  83. Volumes: nil,
  84. Secrets: nil,
  85. Configs: nil,
  86. Extensions: nil,
  87. ComposeFiles: nil,
  88. Environment: nil,
  89. DisabledServices: nil,
  90. Profiles: nil,
  91. }
  92. mockCtrl := gomock.NewController(t)
  93. defer mockCtrl.Finish()
  94. cli := mocks.NewMockCli(mockCtrl)
  95. tested := composeService{
  96. dockerCli: cli,
  97. }
  98. ctx := context.Background()
  99. t.Run("viz (no ports, networks or image)", func(t *testing.T) {
  100. graphStr, err := tested.Viz(ctx, &project, compose.VizOptions{
  101. Indentation: " ",
  102. IncludePorts: false,
  103. IncludeImageName: false,
  104. IncludeNetworks: false,
  105. })
  106. assert.NoError(t, err, "viz command failed")
  107. // check indentation
  108. assert.Contains(t, graphStr, "\n ", graphStr)
  109. assert.NotContains(t, graphStr, "\n ", graphStr)
  110. // check digraph name
  111. assert.Contains(t, graphStr, "digraph "+project.Name, graphStr)
  112. // check nodes
  113. for _, service := range project.Services {
  114. assert.Contains(t, graphStr, "\""+service.Name+"\" [style=\"filled\"", graphStr)
  115. }
  116. // check node attributes
  117. assert.NotContains(t, graphStr, "Networks", graphStr)
  118. assert.NotContains(t, graphStr, "Image", graphStr)
  119. assert.NotContains(t, graphStr, "Ports", graphStr)
  120. // check edges that SHOULD exist in the generated graph
  121. allowedEdges := make(map[string][]string)
  122. for _, service := range project.Services {
  123. allowedEdges[service.Name] = make([]string, 0, len(service.DependsOn))
  124. for depName := range service.DependsOn {
  125. allowedEdges[service.Name] = append(allowedEdges[service.Name], depName)
  126. }
  127. }
  128. for serviceName, dependencies := range allowedEdges {
  129. for _, dependencyName := range dependencies {
  130. assert.Contains(t, graphStr, "\""+serviceName+"\" -> \""+dependencyName+"\"", graphStr)
  131. }
  132. }
  133. // check edges that SHOULD NOT exist in the generated graph
  134. forbiddenEdges := make(map[string][]string)
  135. for _, service := range project.Services {
  136. forbiddenEdges[service.Name] = make([]string, 0, len(project.ServiceNames())-len(service.DependsOn))
  137. for _, serviceName := range project.ServiceNames() {
  138. _, edgeExists := service.DependsOn[serviceName]
  139. if !edgeExists {
  140. forbiddenEdges[service.Name] = append(forbiddenEdges[service.Name], serviceName)
  141. }
  142. }
  143. }
  144. for serviceName, forbiddenDeps := range forbiddenEdges {
  145. for _, forbiddenDep := range forbiddenDeps {
  146. assert.NotContains(t, graphStr, "\""+serviceName+"\" -> \""+forbiddenDep+"\"")
  147. }
  148. }
  149. })
  150. t.Run("viz (with ports, networks and image)", func(t *testing.T) {
  151. graphStr, err := tested.Viz(ctx, &project, compose.VizOptions{
  152. Indentation: "\t",
  153. IncludePorts: true,
  154. IncludeImageName: true,
  155. IncludeNetworks: true,
  156. })
  157. assert.NoError(t, err, "viz command failed")
  158. // check indentation
  159. assert.Contains(t, graphStr, "\n\t", graphStr)
  160. assert.NotContains(t, graphStr, "\n\t\t", graphStr)
  161. // check digraph name
  162. assert.Contains(t, graphStr, "digraph "+project.Name, graphStr)
  163. // check nodes
  164. for _, service := range project.Services {
  165. assert.Contains(t, graphStr, "\""+service.Name+"\" [style=\"filled\"", graphStr)
  166. }
  167. // check node attributes
  168. assert.Contains(t, graphStr, "Networks", graphStr)
  169. assert.Contains(t, graphStr, ">internal<", graphStr)
  170. assert.Contains(t, graphStr, ">external<", graphStr)
  171. assert.Contains(t, graphStr, "Image", graphStr)
  172. for _, service := range project.Services {
  173. assert.Contains(t, graphStr, ">"+service.Image+"<", graphStr)
  174. }
  175. assert.Contains(t, graphStr, "Ports", graphStr)
  176. for _, service := range project.Services {
  177. for _, portConfig := range service.Ports {
  178. assert.NotContains(t, graphStr, ">"+portConfig.Published+":"+strconv.Itoa(int(portConfig.Target))+"<", graphStr)
  179. }
  180. }
  181. })
  182. }