interceptor_test.go 3.1 KB

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