2020-06-18 16:13:24 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-06-18 16:13:24 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-06-15 23:21:52 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/metadata"
|
2020-08-03 09:54:18 +02:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/assert/cmp"
|
2020-06-15 23:21:52 +02:00
|
|
|
|
2020-08-21 17:24:53 +02:00
|
|
|
"github.com/docker/compose-cli/config"
|
|
|
|
apicontext "github.com/docker/compose-cli/context"
|
2020-06-15 23:21:52 +02:00
|
|
|
)
|
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
func testContext(t *testing.T) context.Context {
|
2020-06-15 23:21:52 +02:00
|
|
|
dir, err := ioutil.TempDir("", "example")
|
2020-08-03 09:54:18 +02:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
_ = os.RemoveAll(dir)
|
|
|
|
})
|
2020-06-15 23:21:52 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = config.WithDir(ctx, dir)
|
|
|
|
err = ioutil.WriteFile(path.Join(dir, "config.json"), []byte(`{"currentContext": "default"}`), 0644)
|
2020-08-03 09:54:18 +02:00
|
|
|
assert.NilError(t, err)
|
2020-06-15 23:21:52 +02:00
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
return ctx
|
2020-06-15 23:21:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
func TestUnaryGetCurrentContext(t *testing.T) {
|
|
|
|
ctx := testContext(t)
|
|
|
|
interceptor := unaryServerInterceptor(ctx)
|
2020-06-15 23:21:52 +02:00
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
currentContext := callUnary(context.Background(), t, interceptor)
|
|
|
|
assert.Equal(t, currentContext, "default")
|
2020-06-15 23:21:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
func TestUnaryContextFromMetadata(t *testing.T) {
|
|
|
|
ctx := testContext(t)
|
2020-06-15 23:21:52 +02:00
|
|
|
contextName := "test"
|
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
interceptor := unaryServerInterceptor(ctx)
|
2020-06-15 23:21:52 +02:00
|
|
|
reqCtx := context.Background()
|
|
|
|
reqCtx = metadata.NewIncomingContext(reqCtx, metadata.MD{
|
|
|
|
(key): []string{contextName},
|
|
|
|
})
|
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
currentContext := callUnary(reqCtx, t, interceptor)
|
|
|
|
assert.Equal(t, contextName, currentContext)
|
2020-06-15 23:21:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
func TestStreamGetCurrentContext(t *testing.T) {
|
|
|
|
ctx := testContext(t)
|
|
|
|
interceptor := streamServerInterceptor(ctx)
|
2020-06-15 23:21:52 +02:00
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
currentContext := callStream(context.Background(), t, interceptor)
|
2020-06-15 23:21:52 +02:00
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
assert.Equal(t, currentContext, "default")
|
2020-06-15 23:21:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
func TestStreamContextFromMetadata(t *testing.T) {
|
|
|
|
ctx := testContext(t)
|
2020-06-15 23:21:52 +02:00
|
|
|
contextName := "test"
|
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
interceptor := streamServerInterceptor(ctx)
|
2020-06-15 23:21:52 +02:00
|
|
|
reqCtx := context.Background()
|
|
|
|
reqCtx = metadata.NewIncomingContext(reqCtx, metadata.MD{
|
|
|
|
(key): []string{contextName},
|
|
|
|
})
|
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
currentContext := callStream(reqCtx, t, interceptor)
|
|
|
|
assert.Equal(t, currentContext, contextName)
|
2020-06-15 23:21:52 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
func callStream(ctx context.Context, t *testing.T, interceptor grpc.StreamServerInterceptor) string {
|
2020-06-15 23:21:52 +02:00
|
|
|
currentContext := ""
|
|
|
|
err := interceptor(nil, &contextServerStream{
|
|
|
|
ctx: ctx,
|
|
|
|
}, &grpc.StreamServerInfo{
|
|
|
|
FullMethod: "/com.docker.api.protos.context.v1.Contexts/test",
|
|
|
|
}, func(srv interface{}, stream grpc.ServerStream) error {
|
|
|
|
currentContext = apicontext.CurrentContext(stream.Context())
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
assert.NilError(t, err)
|
2020-06-15 23:21:52 +02:00
|
|
|
|
|
|
|
return currentContext
|
|
|
|
}
|
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
func callUnary(ctx context.Context, t *testing.T, interceptor grpc.UnaryServerInterceptor) string {
|
2020-06-15 23:21:52 +02:00
|
|
|
currentContext := ""
|
|
|
|
resp, err := interceptor(ctx, nil, &grpc.UnaryServerInfo{
|
|
|
|
FullMethod: "/com.docker.api.protos.context.v1.Contexts/test",
|
|
|
|
}, func(ctx context.Context, req interface{}) (interface{}, error) {
|
|
|
|
currentContext = apicontext.CurrentContext(ctx)
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
|
2020-08-03 09:54:18 +02:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Assert(t, cmp.Nil(resp))
|
2020-06-15 23:21:52 +02:00
|
|
|
|
|
|
|
return currentContext
|
|
|
|
}
|