metrics_test.go 4.2 KB

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