1
0

interceptor_test.go 3.3 KB

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