interceptor_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "io/ioutil"
  17. "os"
  18. "path"
  19. "testing"
  20. "github.com/stretchr/testify/assert"
  21. "github.com/stretchr/testify/require"
  22. "github.com/stretchr/testify/suite"
  23. "google.golang.org/grpc"
  24. "google.golang.org/grpc/metadata"
  25. "github.com/docker/api/config"
  26. apicontext "github.com/docker/api/context"
  27. )
  28. type interceptorSuite struct {
  29. suite.Suite
  30. dir string
  31. ctx context.Context
  32. }
  33. func (is *interceptorSuite) BeforeTest(suiteName, testName string) {
  34. dir, err := ioutil.TempDir("", "example")
  35. require.Nil(is.T(), err)
  36. ctx := context.Background()
  37. ctx = config.WithDir(ctx, dir)
  38. err = ioutil.WriteFile(path.Join(dir, "config.json"), []byte(`{"currentContext": "default"}`), 0644)
  39. require.Nil(is.T(), err)
  40. is.dir = dir
  41. is.ctx = ctx
  42. }
  43. func (is *interceptorSuite) AfterTest(suiteName, tesName string) {
  44. err := os.RemoveAll(is.dir)
  45. require.Nil(is.T(), err)
  46. }
  47. func (is *interceptorSuite) TestUnaryGetCurrentContext() {
  48. interceptor := unaryServerInterceptor(is.ctx)
  49. currentContext := is.callUnary(context.Background(), interceptor)
  50. assert.Equal(is.T(), "default", currentContext)
  51. }
  52. func (is *interceptorSuite) TestUnaryContextFromMetadata() {
  53. contextName := "test"
  54. interceptor := unaryServerInterceptor(is.ctx)
  55. reqCtx := context.Background()
  56. reqCtx = metadata.NewIncomingContext(reqCtx, metadata.MD{
  57. (key): []string{contextName},
  58. })
  59. currentContext := is.callUnary(reqCtx, interceptor)
  60. assert.Equal(is.T(), contextName, currentContext)
  61. }
  62. func (is *interceptorSuite) TestStreamGetCurrentContext() {
  63. interceptor := streamServerInterceptor(is.ctx)
  64. currentContext := is.callStream(context.Background(), interceptor)
  65. assert.Equal(is.T(), "default", currentContext)
  66. }
  67. func (is *interceptorSuite) TestStreamContextFromMetadata() {
  68. contextName := "test"
  69. interceptor := streamServerInterceptor(is.ctx)
  70. reqCtx := context.Background()
  71. reqCtx = metadata.NewIncomingContext(reqCtx, metadata.MD{
  72. (key): []string{contextName},
  73. })
  74. currentContext := is.callStream(reqCtx, interceptor)
  75. assert.Equal(is.T(), contextName, currentContext)
  76. }
  77. func (is *interceptorSuite) callStream(ctx context.Context, interceptor grpc.StreamServerInterceptor) string {
  78. currentContext := ""
  79. err := interceptor(nil, &contextServerStream{
  80. ctx: ctx,
  81. }, &grpc.StreamServerInfo{
  82. FullMethod: "/com.docker.api.protos.context.v1.Contexts/test",
  83. }, func(srv interface{}, stream grpc.ServerStream) error {
  84. currentContext = apicontext.CurrentContext(stream.Context())
  85. return nil
  86. })
  87. require.Nil(is.T(), err)
  88. return currentContext
  89. }
  90. func (is *interceptorSuite) callUnary(ctx context.Context, interceptor grpc.UnaryServerInterceptor) string {
  91. currentContext := ""
  92. resp, err := interceptor(ctx, nil, &grpc.UnaryServerInfo{
  93. FullMethod: "/com.docker.api.protos.context.v1.Contexts/test",
  94. }, func(ctx context.Context, req interface{}) (interface{}, error) {
  95. currentContext = apicontext.CurrentContext(ctx)
  96. return nil, nil
  97. })
  98. require.Nil(is.T(), err)
  99. require.Nil(is.T(), resp)
  100. return currentContext
  101. }
  102. func TestInterceptor(t *testing.T) {
  103. suite.Run(t, new(interceptorSuite))
  104. }