viz_test.go 6.1 KB

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