|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package shim |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "path/filepath" |
| 22 | + "reflect" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/containerd/ttrpc" |
| 26 | +) |
| 27 | + |
| 28 | +func TestChainUnaryServerInterceptors(t *testing.T) { |
| 29 | + methodInfo := &ttrpc.UnaryServerInfo{ |
| 30 | + FullMethod: filepath.Join("/", t.Name(), "foo"), |
| 31 | + } |
| 32 | + |
| 33 | + type callKey struct{} |
| 34 | + callValue := "init" |
| 35 | + callCtx := context.WithValue(context.Background(), callKey{}, callValue) |
| 36 | + |
| 37 | + verifyCallCtxFn := func(ctx context.Context, key interface{}, expected interface{}) { |
| 38 | + got := ctx.Value(key) |
| 39 | + if !reflect.DeepEqual(expected, got) { |
| 40 | + t.Fatalf("[context(key:%s) expected %v, but got %v", key, expected, got) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + verifyInfoFn := func(info *ttrpc.UnaryServerInfo) { |
| 45 | + if !reflect.DeepEqual(methodInfo, info) { |
| 46 | + t.Fatalf("[info] expected %+v, but got %+v", methodInfo, info) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + origUnmarshaler := func(obj interface{}) error { |
| 51 | + v := obj.(*int64) |
| 52 | + *v *= 2 |
| 53 | + return nil |
| 54 | + } |
| 55 | + |
| 56 | + type firstKey struct{} |
| 57 | + firstValue := "from first" |
| 58 | + var firstUnmarshaler ttrpc.Unmarshaler |
| 59 | + first := func(ctx context.Context, unmarshal ttrpc.Unmarshaler, info *ttrpc.UnaryServerInfo, method ttrpc.Method) (interface{}, error) { |
| 60 | + verifyCallCtxFn(ctx, callKey{}, callValue) |
| 61 | + verifyInfoFn(info) |
| 62 | + |
| 63 | + ctx = context.WithValue(ctx, firstKey{}, firstValue) |
| 64 | + |
| 65 | + firstUnmarshaler = func(obj interface{}) error { |
| 66 | + if err := unmarshal(obj); err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + v := obj.(*int64) |
| 71 | + *v *= 2 |
| 72 | + return nil |
| 73 | + } |
| 74 | + |
| 75 | + return method(ctx, firstUnmarshaler) |
| 76 | + } |
| 77 | + |
| 78 | + type secondKey struct{} |
| 79 | + secondValue := "from second" |
| 80 | + second := func(ctx context.Context, unmarshal ttrpc.Unmarshaler, info *ttrpc.UnaryServerInfo, method ttrpc.Method) (interface{}, error) { |
| 81 | + verifyCallCtxFn(ctx, callKey{}, callValue) |
| 82 | + verifyCallCtxFn(ctx, firstKey{}, firstValue) |
| 83 | + verifyInfoFn(info) |
| 84 | + |
| 85 | + v := int64(3) // should return 12 |
| 86 | + if err := unmarshal(&v); err != nil { |
| 87 | + t.Fatalf("unexpected error %v", err) |
| 88 | + } |
| 89 | + if expected := int64(12); v != expected { |
| 90 | + t.Fatalf("expected int64(%v), but got %v", expected, v) |
| 91 | + } |
| 92 | + |
| 93 | + ctx = context.WithValue(ctx, secondKey{}, secondValue) |
| 94 | + return method(ctx, unmarshal) |
| 95 | + } |
| 96 | + |
| 97 | + methodFn := func(ctx context.Context, unmarshal func(interface{}) error) (interface{}, error) { |
| 98 | + verifyCallCtxFn(ctx, callKey{}, callValue) |
| 99 | + verifyCallCtxFn(ctx, firstKey{}, firstValue) |
| 100 | + verifyCallCtxFn(ctx, secondKey{}, secondValue) |
| 101 | + |
| 102 | + v := int64(2) |
| 103 | + if err := unmarshal(&v); err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + return v, nil |
| 107 | + } |
| 108 | + |
| 109 | + interceptor := chainUnaryServerInterceptors(first, second) |
| 110 | + v, err := interceptor(callCtx, origUnmarshaler, methodInfo, methodFn) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("expected nil, but got %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + if expected := int64(8); v != expected { |
| 116 | + t.Fatalf("expected result is int64(%v), but got %v", expected, v) |
| 117 | + } |
| 118 | +} |
0 commit comments