Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make our own low-level driver for testing #4193

Merged
merged 49 commits into from Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
e469531
wip
noisersup Mar 14, 2024
1fb8e35
wip
noisersup Mar 14, 2024
e922efa
wip
noisersup Mar 18, 2024
392a1c7
wip
noisersup Mar 18, 2024
dfbf30e
wip
noisersup Mar 18, 2024
73280d8
wip
noisersup Mar 18, 2024
4763c05
wip
noisersup Mar 19, 2024
038ffd0
fix bug
noisersup Mar 20, 2024
63812b6
wip
noisersup Mar 20, 2024
0cd7038
wip
noisersup Mar 20, 2024
970d801
wip
noisersup Mar 20, 2024
023c31b
dbname
noisersup Mar 20, 2024
18aef6c
dbname
noisersup Mar 20, 2024
cff1a98
parsing
noisersup Mar 20, 2024
377c4ec
wip
noisersup Mar 20, 2024
556c25a
wip
noisersup Mar 20, 2024
27d7dad
wip
noisersup Mar 20, 2024
d15e69f
wip
noisersup Mar 21, 2024
65c23a9
wip
noisersup Mar 21, 2024
b5e2c86
validate request response_id
noisersup Mar 21, 2024
48bccc2
wip
noisersup Mar 21, 2024
a68f867
wip
noisersup Mar 21, 2024
9034641
wip
noisersup Mar 21, 2024
8e50292
fully functional test, but ugly
noisersup Mar 21, 2024
00ba4e2
cmnt
noisersup Mar 21, 2024
0715c9e
default msg len
noisersup Mar 21, 2024
627a607
sort
noisersup Mar 21, 2024
f59fce7
default opcode
noisersup Mar 21, 2024
35929ba
default id
noisersup Mar 21, 2024
638c32e
remove helper
noisersup Mar 21, 2024
744b0cc
wip
noisersup Mar 22, 2024
04c9ed7
wip
noisersup Mar 22, 2024
eb33ee1
wip
noisersup Mar 22, 2024
508fedc
wip
noisersup Mar 22, 2024
2fbeb6e
wip
noisersup Mar 22, 2024
bcac9dd
Merge branch 'main' into driver-4146
noisersup Mar 22, 2024
de165ce
wip
noisersup Mar 22, 2024
5e384fd
Apply suggestions from code review
noisersup Mar 22, 2024
2799f4a
wip
noisersup Mar 22, 2024
bcd93f1
wip
noisersup Mar 22, 2024
9055859
wip
noisersup Mar 22, 2024
926b579
wip
noisersup Mar 22, 2024
4d854e6
wip
noisersup Mar 22, 2024
5d46ce4
wip
noisersup Mar 22, 2024
e1abbb4
wip
noisersup Mar 22, 2024
fe5c0c5
wip
noisersup Mar 22, 2024
464d821
wip
noisersup Mar 22, 2024
591cd77
wip
noisersup Mar 22, 2024
79d4319
Merge branch 'main' into driver-4146
AlekSi Mar 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Expand Up @@ -36,6 +36,7 @@ linters-settings:
files:
- $all
- "!**/internal/bson/*_test.go"
- "!**/internal/driver/*.go"
- "!**/internal/util/testutil/*.go"
- "!**/internal/wire/*.go"
deny:
Expand All @@ -44,6 +45,7 @@ linters-settings:
files:
- $all
- "!**/internal/bson/*.go"
- "!**/internal/driver/*.go"
deny:
- pkg: github.com/cristalhq/bson
- pkg: github.com/cristalhq/bson/bsonproto
Expand Down
74 changes: 70 additions & 4 deletions internal/driver/driver.go
Expand Up @@ -37,6 +37,8 @@
r *bufio.Reader
w *bufio.Writer
l *slog.Logger

lastRequestID int32
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
}

// Connect creates a new connection for the given MongoDB URI and logger.
Expand Down Expand Up @@ -120,7 +122,7 @@
c.l.Debug(
fmt.Sprintf("<<<\n%s", body.String()),
slog.Int("length", int(header.MessageLength)),
slog.Int("id", int(header.ResponseTo)),
slog.Int("id", int(header.RequestID)),
slog.Int("response_to", int(header.ResponseTo)),
slog.String("opcode", header.OpCode.String()),
)
Expand All @@ -133,7 +135,7 @@
c.l.Debug(
fmt.Sprintf(">>>\n%s", body.String()),
slog.Int("length", int(header.MessageLength)),
slog.Int("id", int(header.ResponseTo)),
slog.Int("id", int(header.RequestID)),
slog.Int("response_to", int(header.ResponseTo)),
slog.String("opcode", header.OpCode.String()),
)
Expand Down Expand Up @@ -165,7 +167,71 @@
}

// Request sends the given request to the connection and returns the response.
// If header MessageLength or RequestID is not specified, it assings the proper values.
// For header.OpCode the wire.OpCodeMsg is used as default.
func (c *Conn) Request(ctx context.Context, header *wire.MsgHeader, body wire.MsgBody) (*wire.MsgHeader, wire.MsgBody, error) {
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
// TODO https://github.com/FerretDB/FerretDB/issues/4146
panic("not implemented")
if header.MessageLength == 0 {
msgBin, err := body.MarshalBinary()
if err != nil {
return nil, nil, lazyerrors.Error(err)

Check warning on line 176 in internal/driver/driver.go

View check run for this annotation

Codecov / codecov/patch

internal/driver/driver.go#L176

Added line #L176 was not covered by tests
}

header.MessageLength = int32(len(msgBin) + wire.MsgHeaderLen)
}

if header.OpCode == 0 {
header.OpCode = wire.OpCodeMsg
}

if header.RequestID == 0 {
header.RequestID = c.nextRequestID()
}
c.lastRequestID = header.RequestID
AlekSi marked this conversation as resolved.
Show resolved Hide resolved

if header.ResponseTo != 0 {
return nil, nil, lazyerrors.Errorf("response_to is not allowed")

Check warning on line 192 in internal/driver/driver.go

View check run for this annotation

Codecov / codecov/patch

internal/driver/driver.go#L192

Added line #L192 was not covered by tests
noisersup marked this conversation as resolved.
Show resolved Hide resolved
}

if m, ok := body.(*wire.OpMsg); ok {
if m.Flags != 0 {
return nil, nil, lazyerrors.Errorf("unsupported request flags %s", m.Flags)

Check warning on line 197 in internal/driver/driver.go

View check run for this annotation

Codecov / codecov/patch

internal/driver/driver.go#L197

Added line #L197 was not covered by tests
}
}

if err := c.Write(header, body); err != nil {
return nil, nil, lazyerrors.Error(err)

Check warning on line 202 in internal/driver/driver.go

View check run for this annotation

Codecov / codecov/patch

internal/driver/driver.go#L202

Added line #L202 was not covered by tests
}

resHeader, resBody, err := c.Read()
if err != nil {
return nil, nil, lazyerrors.Error(err)

Check warning on line 207 in internal/driver/driver.go

View check run for this annotation

Codecov / codecov/patch

internal/driver/driver.go#L207

Added line #L207 was not covered by tests
}

AlekSi marked this conversation as resolved.
Show resolved Hide resolved
if resHeader.ResponseTo != header.RequestID {
c.l.Error(
fmt.Sprintf("response_to not equal to request_id"),

Check failure on line 212 in internal/driver/driver.go

View workflow job for this annotation

GitHub Actions / golangci-lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
noisersup marked this conversation as resolved.
Show resolved Hide resolved
slog.Int("request_id", int(header.RequestID)),
slog.Int("response_id", int(resHeader.RequestID)),
slog.Int("response_to", int(resHeader.ResponseTo)),
)
return nil, nil, lazyerrors.Errorf(
"response_to not equal to request_id (response_to=%d; expected=%d)",
noisersup marked this conversation as resolved.
Show resolved Hide resolved
resHeader.ResponseTo,
header.RequestID,
)

Check warning on line 221 in internal/driver/driver.go

View check run for this annotation

Codecov / codecov/patch

internal/driver/driver.go#L211-L221

Added lines #L211 - L221 were not covered by tests
}

if m, ok := resBody.(*wire.OpMsg); ok {
if m.Flags != 0 {
return nil, nil, lazyerrors.Errorf("unsupported response flags %s", m.Flags)

Check warning on line 226 in internal/driver/driver.go

View check run for this annotation

Codecov / codecov/patch

internal/driver/driver.go#L226

Added line #L226 was not covered by tests
}
}

return resHeader, resBody, nil
}

// nextRequestID returns the incremented value of last recorded request header ID from `Request` function.
func (c *Conn) nextRequestID() int32 {
c.lastRequestID += 1
return c.lastRequestID
}
150 changes: 148 additions & 2 deletions internal/driver/driver_test.go
Expand Up @@ -17,9 +17,14 @@
import (
"testing"

"github.com/cristalhq/bson/bsonproto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/FerretDB/FerretDB/internal/bson"
"github.com/FerretDB/FerretDB/internal/util/must"
"github.com/FerretDB/FerretDB/internal/util/testutil"
"github.com/FerretDB/FerretDB/internal/wire"
)

func TestDriver(t *testing.T) {
Expand All @@ -33,6 +38,147 @@
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, c.Close()) })

// TODO https://github.com/FerretDB/FerretDB/issues/4146
_ = c
dbName := "TestDriver"
AlekSi marked this conversation as resolved.
Show resolved Hide resolved

var lsid bson.Binary

Check failure on line 43 in internal/driver/driver_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

S1021: should merge variable declaration with assignment on next line (gosimple)

lsid = bson.Binary{
B: []byte{
0xa3, 0x19, 0xf2, 0xb4, 0xa1, 0x75, 0x40, 0xc7,
0xb8, 0xe7, 0xa3, 0xa3, 0x2e, 0xc2, 0x56, 0xbe,
},
Subtype: bsonproto.BinaryUUID,
}

t.Run("Drop", func(t *testing.T) {
doc := must.NotFail(bson.NewDocument(
"dropDatabase", int32(1),
"lsid", must.NotFail(bson.NewDocument("id", lsid)),
"$db", dbName,
))

body, err := wire.NewOpMsg(must.NotFail(doc.Encode()))
require.NoError(t, err)

_, _, err = c.Request(ctx, new(wire.MsgHeader), body)
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)
})

insertDocs := must.NotFail(bson.NewArray(
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
must.NotFail(bson.NewDocument("w", int32(2), "v", int32(1), "_id", int32(0))),
must.NotFail(bson.NewDocument("v", int32(2), "_id", int32(1))),
must.NotFail(bson.NewDocument("v", int32(3), "_id", int32(2))),
))

t.Run("Insert", func(t *testing.T) {
insertCmd := must.NotFail(bson.NewDocument(
"insert", "values",
"documents", insertDocs,
"ordered", true,
"lsid", must.NotFail(bson.NewDocument("id", lsid)),
"$db", dbName,
))

body, err := wire.NewOpMsg(must.NotFail(insertCmd.Encode()))
require.NoError(t, err)

resHeader, _, err := c.Request(ctx, new(wire.MsgHeader), body)
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)

assert.NotZero(t, resHeader.RequestID)
})

var cursorID int64

t.Run("Find", func(t *testing.T) {
findCmd := must.NotFail(bson.NewDocument(
"find", "values",
"filter", must.NotFail(bson.NewDocument()),
"sort", must.NotFail(bson.NewDocument("_id", int32(1))),
"lsid", must.NotFail(bson.NewDocument("id", lsid)),
"batchSize", int32(1),
"$db", dbName,
))

body, err := wire.NewOpMsg(must.NotFail(findCmd.Encode()))
require.NoError(t, err)

resHeader, resBody, err := c.Request(ctx, new(wire.MsgHeader), body)
require.NoError(t, err)
assert.NotZero(t, resHeader.RequestID)

resMsg, err := must.NotFail(resBody.(*wire.OpMsg).RawDocument()).Decode()
require.NoError(t, err)

cursor, err := resMsg.Get("cursor").(bson.RawDocument).Decode()
require.NoError(t, err)

firstBatch := cursor.Get("firstBatch").(bson.RawArray)
cursorID = cursor.Get("id").(int64)

expectedDocs := must.NotFail(bson.NewArray(
must.NotFail(bson.NewDocument("_id", int32(0), "w", int32(2), "v", int32(1))),
))

testutil.AssertEqual(t, must.NotFail(expectedDocs.Convert()), must.NotFail(firstBatch.Convert()))
require.NotZero(t, cursorID)
})

getMoreCmd := must.NotFail(bson.NewDocument(
"getMore", cursorID,
"collection", "values",
"lsid", must.NotFail(bson.NewDocument("id", lsid)),
"batchSize", int32(1),
"$db", dbName,
))

t.Run("GetMore", func(t *testing.T) {
for i := 0; i < 2; i++ {
body, err := wire.NewOpMsg(must.NotFail(getMoreCmd.Encode()))
require.NoError(t, err)

resHeader, resBody, err := c.Request(ctx, new(wire.MsgHeader), body)
require.NoError(t, err)
assert.NotZero(t, resHeader.RequestID)

resMsg, err := must.NotFail(resBody.(*wire.OpMsg).RawDocument()).Decode()
require.NoError(t, err)

cursor, err := resMsg.Get("cursor").(bson.RawDocument).Decode()
require.NoError(t, err)

nextBatch := cursor.Get("nextBatch").(bson.RawArray)
newCursorID := cursor.Get("id").(int64)

expectedDocs := must.NotFail(bson.NewArray(
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
must.NotFail(bson.NewDocument("_id", int32(i+1), "v", int32(i+2))),
))

testutil.AssertEqual(t, must.NotFail(expectedDocs.Convert()), must.NotFail(nextBatch.Convert()))
assert.Equal(t, cursorID, newCursorID)
}
})

t.Run("GetMoreEmpty", func(t *testing.T) {
body, err := wire.NewOpMsg(must.NotFail(getMoreCmd.Encode()))
require.NoError(t, err)

resHeader, resBody, err := c.Request(ctx, new(wire.MsgHeader), body)
require.NoError(t, err)
assert.NotZero(t, resHeader.RequestID)

resMsg, err := must.NotFail(resBody.(*wire.OpMsg).RawDocument()).Decode()
require.NoError(t, err)

cursor, err := resMsg.Get("cursor").(bson.RawDocument).Decode()
require.NoError(t, err)

nextBatch := cursor.Get("nextBatch").(bson.RawArray)
newCursorID := cursor.Get("id").(int64)

expectedDocs := must.NotFail(bson.NewArray())

testutil.AssertEqual(t, must.NotFail(expectedDocs.Convert()), must.NotFail(nextBatch.Convert()))
assert.Zero(t, newCursorID)
})
}