metrics_test.go 3.2 KB

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