-
Notifications
You must be signed in to change notification settings - Fork 895
/
handshake_test.go
198 lines (178 loc) · 5.54 KB
/
handshake_test.go
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
// 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/driverutil"
"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"
)
func TestHandshakeProse(t *testing.T) {
mt := mtest.New(t)
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(driverutil.EnvVarAWSExecutionEnv, "")
t.Setenv(driverutil.EnvVarFunctionsWorkerRuntime, "")
t.Setenv(driverutil.EnvVarKService, "")
t.Setenv(driverutil.EnvVarVercel, "")
t.Setenv(driverutil.EnvVarAWSRegion, "")
t.Setenv(driverutil.EnvVarAWSLambdaFunctionMemorySize, "")
t.Setenv(driverutil.EnvVarFunctionMemoryMB, "")
t.Setenv(driverutil.EnvVarFunctionTimeoutSec, "")
t.Setenv(driverutil.EnvVarFunctionRegion, "")
t.Setenv(driverutil.EnvVarVercelRegion, "")
for _, test := range []struct {
name string
env map[string]string
want bson.D
}{
{
name: "1. valid AWS",
env: map[string]string{
driverutil.EnvVarAWSExecutionEnv: "AWS_Lambda_java8",
driverutil.EnvVarAWSRegion: "us-east-2",
driverutil.EnvVarAWSLambdaFunctionMemorySize: "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{
driverutil.EnvVarFunctionsWorkerRuntime: "node",
},
want: clientMetadata(bson.D{
{Key: "name", Value: "azure.func"},
}),
},
{
name: "3. valid GCP",
env: map[string]string{
driverutil.EnvVarKService: "servicename",
driverutil.EnvVarFunctionMemoryMB: "1024",
driverutil.EnvVarFunctionTimeoutSec: "60",
driverutil.EnvVarFunctionRegion: "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{
driverutil.EnvVarVercel: "1",
driverutil.EnvVarVercelRegion: "cdg1",
},
want: clientMetadata(bson.D{
{Key: "name", Value: "vercel"},
{Key: "region", Value: "cdg1"},
}),
},
{
name: "5. invalid multiple providers",
env: map[string]string{
driverutil.EnvVarAWSExecutionEnv: "AWS_Lambda_java8",
driverutil.EnvVarFunctionsWorkerRuntime: "node",
},
want: clientMetadata(nil),
},
{
name: "6. invalid long string",
env: map[string]string{
driverutil.EnvVarAWSExecutionEnv: "AWS_Lambda_java8",
driverutil.EnvVarAWSRegion: 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{
driverutil.EnvVarAWSExecutionEnv: "AWS_Lambda_java8",
driverutil.EnvVarAWSLambdaFunctionMemorySize: "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{
driverutil.EnvVarAWSExecutionEnv: "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)
})
}
}