client_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package stack
  2. import (
  3. "strings"
  4. "github.com/docker/docker/api/types"
  5. "github.com/docker/docker/api/types/filters"
  6. "github.com/docker/docker/api/types/swarm"
  7. "github.com/docker/docker/cli/compose/convert"
  8. "github.com/docker/docker/client"
  9. "golang.org/x/net/context"
  10. )
  11. type fakeClient struct {
  12. client.Client
  13. services []string
  14. networks []string
  15. secrets []string
  16. removedServices []string
  17. removedNetworks []string
  18. removedSecrets []string
  19. serviceListFunc func(options types.ServiceListOptions) ([]swarm.Service, error)
  20. networkListFunc func(options types.NetworkListOptions) ([]types.NetworkResource, error)
  21. secretListFunc func(options types.SecretListOptions) ([]swarm.Secret, error)
  22. serviceRemoveFunc func(serviceID string) error
  23. networkRemoveFunc func(networkID string) error
  24. secretRemoveFunc func(secretID string) error
  25. }
  26. func (cli *fakeClient) ServiceList(ctx context.Context, options types.ServiceListOptions) ([]swarm.Service, error) {
  27. if cli.serviceListFunc != nil {
  28. return cli.serviceListFunc(options)
  29. }
  30. namespace := namespaceFromFilters(options.Filters)
  31. servicesList := []swarm.Service{}
  32. for _, name := range cli.services {
  33. if belongToNamespace(name, namespace) {
  34. servicesList = append(servicesList, serviceFromName(name))
  35. }
  36. }
  37. return servicesList, nil
  38. }
  39. func (cli *fakeClient) NetworkList(ctx context.Context, options types.NetworkListOptions) ([]types.NetworkResource, error) {
  40. if cli.networkListFunc != nil {
  41. return cli.networkListFunc(options)
  42. }
  43. namespace := namespaceFromFilters(options.Filters)
  44. networksList := []types.NetworkResource{}
  45. for _, name := range cli.networks {
  46. if belongToNamespace(name, namespace) {
  47. networksList = append(networksList, networkFromName(name))
  48. }
  49. }
  50. return networksList, nil
  51. }
  52. func (cli *fakeClient) SecretList(ctx context.Context, options types.SecretListOptions) ([]swarm.Secret, error) {
  53. if cli.secretListFunc != nil {
  54. return cli.secretListFunc(options)
  55. }
  56. namespace := namespaceFromFilters(options.Filters)
  57. secretsList := []swarm.Secret{}
  58. for _, name := range cli.secrets {
  59. if belongToNamespace(name, namespace) {
  60. secretsList = append(secretsList, secretFromName(name))
  61. }
  62. }
  63. return secretsList, nil
  64. }
  65. func (cli *fakeClient) ServiceRemove(ctx context.Context, serviceID string) error {
  66. if cli.serviceRemoveFunc != nil {
  67. return cli.serviceRemoveFunc(serviceID)
  68. }
  69. cli.removedServices = append(cli.removedServices, serviceID)
  70. return nil
  71. }
  72. func (cli *fakeClient) NetworkRemove(ctx context.Context, networkID string) error {
  73. if cli.networkRemoveFunc != nil {
  74. return cli.networkRemoveFunc(networkID)
  75. }
  76. cli.removedNetworks = append(cli.removedNetworks, networkID)
  77. return nil
  78. }
  79. func (cli *fakeClient) SecretRemove(ctx context.Context, secretID string) error {
  80. if cli.secretRemoveFunc != nil {
  81. return cli.secretRemoveFunc(secretID)
  82. }
  83. cli.removedSecrets = append(cli.removedSecrets, secretID)
  84. return nil
  85. }
  86. func serviceFromName(name string) swarm.Service {
  87. return swarm.Service{
  88. ID: "ID-" + name,
  89. Spec: swarm.ServiceSpec{
  90. Annotations: swarm.Annotations{Name: name},
  91. },
  92. }
  93. }
  94. func networkFromName(name string) types.NetworkResource {
  95. return types.NetworkResource{
  96. ID: "ID-" + name,
  97. Name: name,
  98. }
  99. }
  100. func secretFromName(name string) swarm.Secret {
  101. return swarm.Secret{
  102. ID: "ID-" + name,
  103. Spec: swarm.SecretSpec{
  104. Annotations: swarm.Annotations{Name: name},
  105. },
  106. }
  107. }
  108. func namespaceFromFilters(filters filters.Args) string {
  109. label := filters.Get("label")[0]
  110. return strings.TrimPrefix(label, convert.LabelNamespace+"=")
  111. }
  112. func belongToNamespace(id, namespace string) bool {
  113. return strings.HasPrefix(id, namespace+"_")
  114. }
  115. func objectName(namespace, name string) string {
  116. return namespace + "_" + name
  117. }
  118. func objectID(name string) string {
  119. return "ID-" + name
  120. }
  121. func buildObjectIDs(objectNames []string) []string {
  122. IDs := make([]string, len(objectNames))
  123. for i, name := range objectNames {
  124. IDs[i] = objectID(name)
  125. }
  126. return IDs
  127. }