1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
// Copyright (C) MongoDB, Inc. 2023-present.
//
// 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
package integration
import (
"context"
"os"
"reflect"
"runtime"
"testing"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/internal/assert"
"go.mongodb.org/mongo-driver/internal/handshake"
"go.mongodb.org/mongo-driver/internal/require"
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
"go.mongodb.org/mongo-driver/version"
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
"go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage"
)
func TestHandshakeProse(t *testing.T) {
mt := mtest.New(t)
if len(os.Getenv("DOCKER_RUNNING")) > 0 {
t.Skip("These tests gives different results when run in Docker due to extra environment data.")
}
opts := mtest.NewOptions().
CreateCollection(false).
ClientType(mtest.Proxy)
clientMetadata := func(env bson.D) bson.D {
elems := bson.D{
{Key: "driver", Value: bson.D{
{Key: "name", Value: "mongo-go-driver"},
{Key: "version", Value: version.Driver},
}},
{Key: "os", Value: bson.D{
{Key: "type", Value: runtime.GOOS},
{Key: "architecture", Value: runtime.GOARCH},
}},
}
elems = append(elems, bson.E{Key: "platform", Value: runtime.Version()})
// If env is empty, don't include it in the metadata.
if env != nil && !reflect.DeepEqual(env, bson.D{}) {
elems = append(elems, bson.E{Key: "env", Value: env})
}
return elems
}
// Reset the environment variables to avoid environment namespace
// collision.
t.Setenv("AWS_EXECUTION_ENV", "")
t.Setenv("FUNCTIONS_WORKER_RUNTIME", "")
t.Setenv("K_SERVICE", "")
t.Setenv("VERCEL", "")
t.Setenv("AWS_REGION", "")
t.Setenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "")
t.Setenv("FUNCTION_MEMORY_MB", "")
t.Setenv("FUNCTION_TIMEOUT_SEC", "")
t.Setenv("FUNCTION_REGION", "")
t.Setenv("VERCEL_REGION", "")
for _, test := range []struct {
name string
env map[string]string
want bson.D
}{
{
name: "1. valid AWS",
env: map[string]string{
"AWS_EXECUTION_ENV": "AWS_Lambda_java8",
"AWS_REGION": "us-east-2",
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "1024",
},
want: clientMetadata(bson.D{
{Key: "name", Value: "aws.lambda"},
{Key: "memory_mb", Value: 1024},
{Key: "region", Value: "us-east-2"},
}),
},
{
name: "2. valid Azure",
env: map[string]string{
"FUNCTIONS_WORKER_RUNTIME": "node",
},
want: clientMetadata(bson.D{
{Key: "name", Value: "azure.func"},
}),
},
{
name: "3. valid GCP",
env: map[string]string{
"K_SERVICE": "servicename",
"FUNCTION_MEMORY_MB": "1024",
"FUNCTION_TIMEOUT_SEC": "60",
"FUNCTION_REGION": "us-central1",
},
want: clientMetadata(bson.D{
{Key: "name", Value: "gcp.func"},
{Key: "memory_mb", Value: 1024},
{Key: "region", Value: "us-central1"},
{Key: "timeout_sec", Value: 60},
}),
},
{
name: "4. valid Vercel",
env: map[string]string{
"VERCEL": "1",
"VERCEL_REGION": "cdg1",
},
want: clientMetadata(bson.D{
{Key: "name", Value: "vercel"},
{Key: "region", Value: "cdg1"},
}),
},
{
name: "5. invalid multiple providers",
env: map[string]string{
"AWS_EXECUTION_ENV": "AWS_Lambda_java8",
"FUNCTIONS_WORKER_RUNTIME": "node",
},
want: clientMetadata(nil),
},
{
name: "6. invalid long string",
env: map[string]string{
"AWS_EXECUTION_ENV": "AWS_Lambda_java8",
"AWS_REGION": func() string {
var s string
for i := 0; i < 512; i++ {
s += "a"
}
return s
}(),
},
want: clientMetadata(bson.D{
{Key: "name", Value: "aws.lambda"},
}),
},
{
name: "7. invalid wrong types",
env: map[string]string{
"AWS_EXECUTION_ENV": "AWS_Lambda_java8",
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE": "big",
},
want: clientMetadata(bson.D{
{Key: "name", Value: "aws.lambda"},
}),
},
{
name: "8. Invalid - AWS_EXECUTION_ENV does not start with \"AWS_Lambda_\"",
env: map[string]string{
"AWS_EXECUTION_ENV": "EC2",
},
want: clientMetadata(nil),
},
} {
test := test
mt.RunOpts(test.name, opts, func(mt *mtest.T) {
for k, v := range test.env {
mt.Setenv(k, v)
}
// Ping the server to ensure the handshake has completed.
err := mt.Client.Ping(context.Background(), nil)
require.NoError(mt, err, "Ping error: %v", err)
messages := mt.GetProxiedMessages()
handshakeMessage := messages[:1][0]
hello := handshake.LegacyHello
if os.Getenv("REQUIRE_API_VERSION") == "true" {
hello = "hello"
}
assert.Equal(mt, hello, handshakeMessage.CommandName)
// Lookup the "client" field in the command document.
clientVal, err := handshakeMessage.Sent.Command.LookupErr("client")
require.NoError(mt, err, "expected command %s to contain client field", handshakeMessage.Sent.Command)
got, ok := clientVal.DocumentOK()
require.True(mt, ok, "expected client field to be a document, got %s", clientVal.Type)
wantBytes, err := bson.Marshal(test.want)
require.NoError(mt, err, "error marshaling want document: %v", err)
want := bsoncore.Document(wantBytes)
assert.Equal(mt, want, got, "want: %v, got: %v", want, got)
})
}
}
func TestLoadBalancedConnectionHandshake(t *testing.T) {
mt := mtest.New(t)
lbopts := mtest.NewOptions().ClientType(mtest.Proxy).Topologies(
mtest.LoadBalanced)
mt.RunOpts("LB connection handshake uses OP_MSG", lbopts, func(mt *mtest.T) {
// Ping the server to ensure the handshake has completed.
err := mt.Client.Ping(context.Background(), nil)
require.NoError(mt, err, "Ping error: %v", err)
messages := mt.GetProxiedMessages()
handshakeMessage := messages[:1][0]
// Per the specifications, if loadBalanced=true, drivers MUST use the hello
// command for the initial handshake and use the OP_MSG protocol.
assert.Equal(mt, "hello", handshakeMessage.CommandName)
assert.Equal(mt, wiremessage.OpMsg, handshakeMessage.Sent.OpCode)
})
opts := mtest.NewOptions().ClientType(mtest.Proxy).Topologies(
mtest.ReplicaSet,
mtest.Sharded,
mtest.Single,
mtest.ShardedReplicaSet)
mt.RunOpts("non-LB connection handshake uses OP_QUERY", opts, func(mt *mtest.T) {
// Ping the server to ensure the handshake has completed.
err := mt.Client.Ping(context.Background(), nil)
require.NoError(mt, err, "Ping error: %v", err)
messages := mt.GetProxiedMessages()
handshakeMessage := messages[:1][0]
want := wiremessage.OpQuery
hello := handshake.LegacyHello
if os.Getenv("REQUIRE_API_VERSION") == "true" {
hello = "hello"
// If the server API version is requested, then we should use OP_MSG
// regardless of the topology
want = wiremessage.OpMsg
}
assert.Equal(mt, hello, handshakeMessage.CommandName)
assert.Equal(mt, want, handshakeMessage.Sent.OpCode)
})
}
|