metrics_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 server
  14. import (
  15. "context"
  16. "strings"
  17. "testing"
  18. "github.com/docker/compose-cli/api/resources"
  19. "github.com/stretchr/testify/mock"
  20. "google.golang.org/grpc"
  21. "google.golang.org/grpc/metadata"
  22. "gotest.tools/v3/assert"
  23. "github.com/docker/compose-cli/api/client"
  24. "github.com/docker/compose-cli/api/compose"
  25. "github.com/docker/compose-cli/api/containers"
  26. "github.com/docker/compose-cli/api/secrets"
  27. "github.com/docker/compose-cli/api/volumes"
  28. "github.com/docker/compose-cli/errdefs"
  29. "github.com/docker/compose-cli/metrics"
  30. containersv1 "github.com/docker/compose-cli/protos/containers/v1"
  31. contextsv1 "github.com/docker/compose-cli/protos/contexts/v1"
  32. streamsv1 "github.com/docker/compose-cli/protos/streams/v1"
  33. volumesv1 "github.com/docker/compose-cli/protos/volumes/v1"
  34. "github.com/docker/compose-cli/server/proxy"
  35. )
  36. func TestAllMethodsHaveCorrespondingCliCommand(t *testing.T) {
  37. s := setupServer()
  38. i := s.GetServiceInfo()
  39. for k, v := range i {
  40. if k == "grpc.health.v1.Health" {
  41. continue
  42. }
  43. var errs []string
  44. for _, m := range v.Methods {
  45. name := "/" + k + "/" + m.Name
  46. if _, keyExists := methodMapping[name]; !keyExists {
  47. errs = append(errs, name+" not mapped to a corresponding cli command")
  48. }
  49. }
  50. assert.Equal(t, "", strings.Join(errs, "\n"))
  51. }
  52. }
  53. func TestTrackSuccess(t *testing.T) {
  54. var mockMetrics = &mockMetricsClient{}
  55. mockMetrics.On("Send", metrics.Command{Command: "ps", Context: "aci", Status: "success", Source: "api"}).Return()
  56. newClient := client.NewClient("aci", noopService{})
  57. interceptor := metricsServerInterceptor(mockMetrics)
  58. ctx := proxy.WithClient(incomingContext("acicontext"), &newClient)
  59. _, err := interceptor(ctx, nil, containerMethodRoute("List"), mockHandler(nil))
  60. assert.NilError(t, err)
  61. }
  62. func TestTrackSFailures(t *testing.T) {
  63. var mockMetrics = &mockMetricsClient{}
  64. newClient := client.NewClient("moby", noopService{})
  65. interceptor := metricsServerInterceptor(mockMetrics)
  66. ctx := proxy.WithClient(incomingContext("default"), &newClient)
  67. _, err := interceptor(ctx, nil, containerMethodRoute("Create"), mockHandler(errdefs.ErrLoginRequired))
  68. assert.Assert(t, err == errdefs.ErrLoginRequired)
  69. }
  70. func containerMethodRoute(action string) *grpc.UnaryServerInfo {
  71. var info = &grpc.UnaryServerInfo{
  72. FullMethod: "/com.docker.api.protos.containers.v1.Containers/" + action,
  73. }
  74. return info
  75. }
  76. func mockHandler(err error) func(ctx context.Context, req interface{}) (interface{}, error) {
  77. return func(ctx context.Context, req interface{}) (interface{}, error) {
  78. return nil, err
  79. }
  80. }
  81. func incomingContext(status string) context.Context {
  82. ctx := metadata.NewIncomingContext(context.TODO(), metadata.MD{
  83. (key): []string{status},
  84. })
  85. return ctx
  86. }
  87. func setupServer() *grpc.Server {
  88. ctx := context.TODO()
  89. s := New(ctx)
  90. p := proxy.New(ctx)
  91. containersv1.RegisterContainersServer(s, p)
  92. streamsv1.RegisterStreamingServer(s, p)
  93. volumesv1.RegisterVolumesServer(s, p)
  94. contextsv1.RegisterContextsServer(s, p.ContextsProxy())
  95. return s
  96. }
  97. type noopService struct{}
  98. func (noopService) ContainerService() containers.Service { return nil }
  99. func (noopService) ComposeService() compose.Service { return nil }
  100. func (noopService) SecretsService() secrets.Service { return nil }
  101. func (noopService) VolumeService() volumes.Service { return nil }
  102. func (noopService) ResourceService() resources.Service { return nil }
  103. type mockMetricsClient struct {
  104. mock.Mock
  105. }
  106. func (s *mockMetricsClient) Send(command metrics.Command) {
  107. s.Called(command)
  108. }