mirror of
https://github.com/hanzoai/docdb.git
synced 2026-10-30 18:39:39 +00:00
The rebrand commit (deb28452) renamed github.com/FerretDB/wire to
github.com/hanzoai/docdb-wire, but the hanzoai/docdb-wire repo does
not exist. Reverting that rename so the build works at HEAD and so
future merges with FerretDB upstream stay clean.
Per docdb LLM convention, the wire dep stays as FerretDB/wire
upstream. Only the FerretDB project itself is rebranded to docdb.
301 lines
7.6 KiB
Go
301 lines
7.6 KiB
Go
// Copyright 2021 DocDB Inc.
|
|
//
|
|
// 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.
|
|
|
|
package integration
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/FerretDB/wire"
|
|
"github.com/FerretDB/wire/wirebson"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/hanzoai/docdb/internal/util/testutil"
|
|
|
|
"github.com/hanzoai/docdb/integration/setup"
|
|
)
|
|
|
|
func TestOpQuery(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := setup.SetupWithOpts(t, &setup.SetupOpts{
|
|
WireConn: setup.WireConnAuth,
|
|
})
|
|
|
|
ctx, conn := s.Ctx, s.WireConn
|
|
|
|
t.Run("CollectionNameWithout.$cmd", func(t *testing.T) {
|
|
q := wire.MustOpQuery("unknown", int32(1))
|
|
q.FullCollectionName = "invalid"
|
|
q.NumberToReturn = -1
|
|
|
|
_, resBody, err := conn.Request(ctx, q)
|
|
require.NoError(t, err)
|
|
|
|
resMsg, err := resBody.(*wire.OpReply).DocumentRaw().Decode()
|
|
require.NoError(t, err)
|
|
|
|
ok := resMsg.Get("ok")
|
|
assert.Equal(t, float64(0), ok)
|
|
|
|
code := resMsg.Get("code")
|
|
assert.Equal(t, int32(5739101), code)
|
|
|
|
expectedMsg := "OP_QUERY is no longer supported."
|
|
resErr := resMsg.Get("$err")
|
|
assert.Contains(t, resErr, expectedMsg)
|
|
})
|
|
|
|
t.Run("UnknownOpQuery", func(t *testing.T) {
|
|
q := wire.MustOpQuery("unknown", int32(1))
|
|
q.FullCollectionName = "admin.$cmd"
|
|
q.NumberToReturn = -1
|
|
|
|
_, resBody, err := conn.Request(ctx, q)
|
|
require.NoError(t, err)
|
|
|
|
resMsg, err := resBody.(*wire.OpReply).DocumentRaw().Decode()
|
|
require.NoError(t, err)
|
|
|
|
ok := resMsg.Get("ok")
|
|
assert.Equal(t, float64(0), ok)
|
|
|
|
code := resMsg.Get("code")
|
|
assert.Equal(t, int32(352), code)
|
|
|
|
expectedMsg := "Unsupported OP_QUERY command: unknown."
|
|
errMsg := resMsg.Get("errmsg")
|
|
assert.Contains(t, errMsg, expectedMsg)
|
|
})
|
|
|
|
t.Run("BadNumberToReturn", func(t *testing.T) {
|
|
q := wire.MustOpQuery("ismaster", int32(1))
|
|
q.FullCollectionName = "admin.$cmd"
|
|
q.NumberToReturn = 0
|
|
|
|
_, resBody, err := conn.Request(ctx, q)
|
|
require.NoError(t, err)
|
|
|
|
res, err := resBody.(*wire.OpReply).DocumentRaw().Decode()
|
|
require.NoError(t, err)
|
|
|
|
FixCluster(t, res)
|
|
|
|
expected := wirebson.MustDocument(
|
|
"ok", float64(0),
|
|
"errmsg", "Bad numberToReturn (0) for $cmd type ns - can only be 1 or -1",
|
|
"code", int32(16979),
|
|
"codeName", "Location16979",
|
|
)
|
|
|
|
testutil.AssertEqual(t, expected, res)
|
|
})
|
|
}
|
|
|
|
func TestOpQueryIsMaster(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := setup.SetupWithOpts(t, &setup.SetupOpts{
|
|
WireConn: setup.WireConnAuth,
|
|
})
|
|
|
|
ctx, conn := s.Ctx, s.WireConn
|
|
|
|
for name, tc := range map[string]struct {
|
|
command string
|
|
}{
|
|
"IsMaster": {command: "isMaster"},
|
|
"Ismaster": {command: "ismaster"},
|
|
} {
|
|
t.Run(name, func(tt *testing.T) {
|
|
t := setup.FailsForDocDB(tt, "https://github.com/hanzoai/docdb-DocumentDB/issues/955")
|
|
|
|
q := wire.MustOpQuery(tc.command, int32(1))
|
|
q.FullCollectionName = "admin.$cmd"
|
|
q.NumberToReturn = -1
|
|
|
|
resHeader, resBody, err := conn.Request(ctx, q)
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, resHeader.RequestID)
|
|
|
|
res, err := resBody.(*wire.OpReply).DocumentRaw().Decode()
|
|
require.NoError(t, err)
|
|
|
|
connectionID := res.Get("connectionId")
|
|
assert.IsType(t, int32(0), connectionID)
|
|
res.Remove("connectionId")
|
|
|
|
localTime := res.Get("localTime")
|
|
assert.IsType(t, time.Time{}, localTime)
|
|
res.Remove("localTime")
|
|
|
|
// TODO https://github.com/hanzoai/docdb-DocumentDB/issues/566
|
|
res.Remove("hosts")
|
|
res.Remove("setName")
|
|
res.Remove("topologyVersion")
|
|
res.Remove("setVersion")
|
|
res.Remove("secondary")
|
|
res.Remove("primary")
|
|
res.Remove("me")
|
|
res.Remove("electionId")
|
|
res.Remove("lastWrite")
|
|
|
|
FixCluster(t, res)
|
|
|
|
expectedComparable := wirebson.MustDocument(
|
|
"ismaster", true,
|
|
"maxBsonObjectSize", int32(16777216),
|
|
"maxMessageSizeBytes", int32(48000000),
|
|
"maxWriteBatchSize", int32(100000),
|
|
"logicalSessionTimeoutMinutes", int32(30),
|
|
"minWireVersion", int32(0),
|
|
"maxWireVersion", int32(21),
|
|
"readOnly", false,
|
|
"ok", float64(1),
|
|
)
|
|
testutil.AssertEqual(t, expectedComparable, res)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOpQueryIsMasterHelloOk(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s := setup.SetupWithOpts(t, &setup.SetupOpts{
|
|
WireConn: setup.WireConnAuth,
|
|
})
|
|
|
|
ctx, conn := s.Ctx, s.WireConn
|
|
|
|
for name, tc := range map[string]struct {
|
|
command string
|
|
}{
|
|
"IsMaster": {command: "isMaster"},
|
|
"Ismaster": {command: "ismaster"},
|
|
} {
|
|
t.Run(name, func(tt *testing.T) {
|
|
t := setup.FailsForDocDB(tt, "https://github.com/hanzoai/docdb-DocumentDB/issues/955")
|
|
|
|
q := wire.MustOpQuery(
|
|
tc.command, int32(1),
|
|
"helloOk", true,
|
|
)
|
|
q.FullCollectionName = "admin.$cmd"
|
|
q.NumberToReturn = -1
|
|
|
|
resHeader, resBody, err := conn.Request(ctx, q)
|
|
require.NoError(t, err)
|
|
assert.NotZero(t, resHeader.RequestID)
|
|
|
|
res, err := resBody.(*wire.OpReply).DocumentRaw().Decode()
|
|
require.NoError(t, err)
|
|
|
|
connectionID := res.Get("connectionId")
|
|
assert.IsType(t, int32(0), connectionID)
|
|
res.Remove("connectionId")
|
|
|
|
localTime := res.Get("localTime")
|
|
assert.IsType(t, time.Time{}, localTime)
|
|
res.Remove("localTime")
|
|
|
|
// TODO https://github.com/hanzoai/docdb-DocumentDB/issues/566
|
|
res.Remove("hosts")
|
|
res.Remove("setName")
|
|
res.Remove("topologyVersion")
|
|
res.Remove("setVersion")
|
|
res.Remove("secondary")
|
|
res.Remove("primary")
|
|
res.Remove("me")
|
|
res.Remove("electionId")
|
|
res.Remove("lastWrite")
|
|
|
|
FixCluster(t, res)
|
|
|
|
expectedComparable := wirebson.MustDocument(
|
|
"helloOk", true,
|
|
"ismaster", true,
|
|
"maxBsonObjectSize", int32(16777216),
|
|
"maxMessageSizeBytes", int32(48000000),
|
|
"maxWriteBatchSize", int32(100000),
|
|
"logicalSessionTimeoutMinutes", int32(30),
|
|
"minWireVersion", int32(0),
|
|
"maxWireVersion", int32(21),
|
|
"readOnly", false,
|
|
"ok", float64(1),
|
|
)
|
|
testutil.AssertEqual(t, expectedComparable, res)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOpQueryHello(tt *testing.T) {
|
|
t := setup.FailsForDocDB(tt, "https://github.com/hanzoai/docdb-DocumentDB/issues/955")
|
|
|
|
tt.Parallel()
|
|
|
|
s := setup.SetupWithOpts(tt, &setup.SetupOpts{
|
|
WireConn: setup.WireConnAuth,
|
|
})
|
|
|
|
ctx, conn := s.Ctx, s.WireConn
|
|
|
|
q := wire.MustOpQuery(
|
|
"hello", int32(1),
|
|
)
|
|
q.FullCollectionName = "admin.$cmd"
|
|
q.NumberToReturn = -1
|
|
|
|
_, resBody, err := conn.Request(ctx, q)
|
|
require.NoError(t, err)
|
|
|
|
res, err := resBody.(*wire.OpReply).DocumentRaw().Decode()
|
|
require.NoError(t, err)
|
|
|
|
connectionID := res.Get("connectionId")
|
|
assert.IsType(t, int32(0), connectionID)
|
|
res.Remove("connectionId")
|
|
|
|
localTime := res.Get("localTime")
|
|
assert.IsType(t, time.Time{}, localTime)
|
|
res.Remove("localTime")
|
|
|
|
// TODO https://github.com/hanzoai/docdb-DocumentDB/issues/566
|
|
res.Remove("topologyVersion")
|
|
res.Remove("hosts")
|
|
res.Remove("setName")
|
|
res.Remove("setVersion")
|
|
res.Remove("secondary")
|
|
res.Remove("primary")
|
|
res.Remove("me")
|
|
res.Remove("electionId")
|
|
res.Remove("lastWrite")
|
|
|
|
FixCluster(t, res)
|
|
|
|
expectedComparable := wirebson.MustDocument(
|
|
"isWritablePrimary", true,
|
|
"maxBsonObjectSize", int32(16777216),
|
|
"maxMessageSizeBytes", int32(48000000),
|
|
"maxWriteBatchSize", int32(100000),
|
|
"logicalSessionTimeoutMinutes", int32(30),
|
|
"minWireVersion", int32(0),
|
|
"maxWireVersion", int32(21),
|
|
"readOnly", false,
|
|
"ok", float64(1),
|
|
)
|
|
testutil.AssertEqual(t, expectedComparable, res)
|
|
}
|