Skip to content

Commit fe68735

Browse files
committed
Ensure write concern not sent when not supported
GODRIVER-668 Change-Id: Iaa4e686d86f2a35f043969f1db1889839f958e4f
1 parent 5514df9 commit fe68735

14 files changed

+343
-49
lines changed

x/network/command/create_indexes.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ func (ci *CreateIndexes) encode(desc description.SelectedServer) (*Write, error)
5050
}
5151
cmd = append(cmd, ci.Opts...)
5252

53-
return &Write{
54-
Clock: ci.Clock,
55-
DB: ci.NS.DB,
56-
Command: cmd,
57-
WriteConcern: ci.WriteConcern,
58-
Session: ci.Session,
59-
}, nil
53+
write := &Write{
54+
Clock: ci.Clock,
55+
DB: ci.NS.DB,
56+
Command: cmd,
57+
Session: ci.Session,
58+
}
59+
if desc.WireVersion != nil && desc.WireVersion.Max >= 5 {
60+
write.WriteConcern = ci.WriteConcern
61+
}
62+
return write, nil
6063
}
6164

6265
// Decode will decode the wire message using the provided server description. Errors during decoding
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package command
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
7+
"github.com/mongodb/mongo-go-driver/x/network/description"
8+
)
9+
10+
func TestCreateIndexes(t *testing.T) {
11+
t.Run("Encode Write Concern for MaxWireVersion >= 5", func(t *testing.T) {
12+
desc := description.SelectedServer{
13+
Server: description.Server{
14+
WireVersion: &description.VersionRange{Min: 0, Max: 5},
15+
},
16+
}
17+
wc := writeconcern.New(writeconcern.WMajority())
18+
cmd := CreateIndexes{WriteConcern: wc}
19+
write, err := cmd.encode(desc)
20+
noerr(t, err)
21+
if write.WriteConcern != wc {
22+
t.Error("write concern should be added to write command, but is missing")
23+
}
24+
})
25+
t.Run("Omit Write Concern for MaxWireVersion < 5", func(t *testing.T) {
26+
desc := description.SelectedServer{
27+
Server: description.Server{
28+
WireVersion: &description.VersionRange{Min: 0, Max: 4},
29+
},
30+
}
31+
wc := writeconcern.New(writeconcern.WMajority())
32+
cmd := CreateIndexes{WriteConcern: wc}
33+
write, err := cmd.encode(desc)
34+
noerr(t, err)
35+
if write.WriteConcern != nil {
36+
t.Error("write concern should be omitted from write command, but is present")
37+
}
38+
})
39+
}

x/network/command/drop_collection.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ func (dc *DropCollection) Encode(desc description.SelectedServer) (wiremessage.W
4444
func (dc *DropCollection) encode(desc description.SelectedServer) (*Write, error) {
4545
cmd := bsonx.Doc{{"drop", bsonx.String(dc.Collection)}}
4646

47-
return &Write{
48-
Clock: dc.Clock,
49-
WriteConcern: dc.WriteConcern,
50-
DB: dc.DB,
51-
Command: cmd,
52-
Session: dc.Session,
53-
}, nil
47+
write := &Write{
48+
Clock: dc.Clock,
49+
DB: dc.DB,
50+
Command: cmd,
51+
Session: dc.Session,
52+
}
53+
if desc.WireVersion != nil && desc.WireVersion.Max >= 5 {
54+
write.WriteConcern = dc.WriteConcern
55+
}
56+
return write, nil
5457
}
5558

5659
// Decode will decode the wire message using the provided server description. Errors during decoding
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package command
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
7+
"github.com/mongodb/mongo-go-driver/x/network/description"
8+
)
9+
10+
func TestDropCollection(t *testing.T) {
11+
t.Run("Encode Write Concern for MaxWireVersion >= 5", func(t *testing.T) {
12+
desc := description.SelectedServer{
13+
Server: description.Server{
14+
WireVersion: &description.VersionRange{Min: 0, Max: 5},
15+
},
16+
}
17+
wc := writeconcern.New(writeconcern.WMajority())
18+
cmd := DropCollection{WriteConcern: wc}
19+
write, err := cmd.encode(desc)
20+
noerr(t, err)
21+
if write.WriteConcern != wc {
22+
t.Error("write concern should be added to write command, but is missing")
23+
}
24+
})
25+
t.Run("Omit Write Concern for MaxWireVersion < 5", func(t *testing.T) {
26+
desc := description.SelectedServer{
27+
Server: description.Server{
28+
WireVersion: &description.VersionRange{Min: 0, Max: 4},
29+
},
30+
}
31+
wc := writeconcern.New(writeconcern.WMajority())
32+
cmd := DropCollection{WriteConcern: wc}
33+
write, err := cmd.encode(desc)
34+
noerr(t, err)
35+
if write.WriteConcern != nil {
36+
t.Error("write concern should be omitted from write command, but is present")
37+
}
38+
})
39+
}

x/network/command/drop_database.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,16 @@ func (dd *DropDatabase) Encode(desc description.SelectedServer) (wiremessage.Wir
4343
func (dd *DropDatabase) encode(desc description.SelectedServer) (*Write, error) {
4444
cmd := bsonx.Doc{{"dropDatabase", bsonx.Int32(1)}}
4545

46-
return &Write{
47-
Clock: dd.Clock,
48-
DB: dd.DB,
49-
Command: cmd,
50-
WriteConcern: dd.WriteConcern,
51-
Session: dd.Session,
52-
}, nil
46+
write := &Write{
47+
Clock: dd.Clock,
48+
DB: dd.DB,
49+
Command: cmd,
50+
Session: dd.Session,
51+
}
52+
if desc.WireVersion != nil && desc.WireVersion.Max >= 5 {
53+
write.WriteConcern = dd.WriteConcern
54+
}
55+
return write, nil
5356
}
5457

5558
// Decode will decode the wire message using the provided server description. Errors during decoding
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package command
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
7+
"github.com/mongodb/mongo-go-driver/x/network/description"
8+
)
9+
10+
func TestDropDatabase(t *testing.T) {
11+
t.Run("Encode Write Concern for MaxWireVersion >= 5", func(t *testing.T) {
12+
desc := description.SelectedServer{
13+
Server: description.Server{
14+
WireVersion: &description.VersionRange{Min: 0, Max: 5},
15+
},
16+
}
17+
wc := writeconcern.New(writeconcern.WMajority())
18+
cmd := DropDatabase{WriteConcern: wc}
19+
write, err := cmd.encode(desc)
20+
noerr(t, err)
21+
if write.WriteConcern != wc {
22+
t.Error("write concern should be added to write command, but is missing")
23+
}
24+
})
25+
t.Run("Omit Write Concern for MaxWireVersion < 5", func(t *testing.T) {
26+
desc := description.SelectedServer{
27+
Server: description.Server{
28+
WireVersion: &description.VersionRange{Min: 0, Max: 4},
29+
},
30+
}
31+
wc := writeconcern.New(writeconcern.WMajority())
32+
cmd := DropDatabase{WriteConcern: wc}
33+
write, err := cmd.encode(desc)
34+
noerr(t, err)
35+
if write.WriteConcern != nil {
36+
t.Error("write concern should be omitted from write command, but is present")
37+
}
38+
})
39+
}

x/network/command/drop_indexes.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@ func (di *DropIndexes) encode(desc description.SelectedServer) (*Write, error) {
4949
}
5050
cmd = append(cmd, di.Opts...)
5151

52-
return &Write{
53-
Clock: di.Clock,
54-
DB: di.NS.DB,
55-
Command: cmd,
56-
WriteConcern: di.WriteConcern,
57-
Session: di.Session,
58-
}, nil
52+
write := &Write{
53+
Clock: di.Clock,
54+
DB: di.NS.DB,
55+
Command: cmd,
56+
Session: di.Session,
57+
}
58+
if desc.WireVersion != nil && desc.WireVersion.Max >= 5 {
59+
write.WriteConcern = di.WriteConcern
60+
}
61+
return write, nil
5962
}
6063

6164
// Decode will decode the wire message using the provided server description. Errors during decoding
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package command
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
7+
"github.com/mongodb/mongo-go-driver/x/network/description"
8+
)
9+
10+
func TestDropIndexes(t *testing.T) {
11+
t.Run("Encode Write Concern for MaxWireVersion >= 5", func(t *testing.T) {
12+
desc := description.SelectedServer{
13+
Server: description.Server{
14+
WireVersion: &description.VersionRange{Min: 0, Max: 5},
15+
},
16+
}
17+
wc := writeconcern.New(writeconcern.WMajority())
18+
cmd := DropIndexes{WriteConcern: wc}
19+
write, err := cmd.encode(desc)
20+
noerr(t, err)
21+
if write.WriteConcern != wc {
22+
t.Error("write concern should be added to write command, but is missing")
23+
}
24+
})
25+
t.Run("Omit Write Concern for MaxWireVersion < 5", func(t *testing.T) {
26+
desc := description.SelectedServer{
27+
Server: description.Server{
28+
WireVersion: &description.VersionRange{Min: 0, Max: 4},
29+
},
30+
}
31+
wc := writeconcern.New(writeconcern.WMajority())
32+
cmd := DropIndexes{WriteConcern: wc}
33+
write, err := cmd.encode(desc)
34+
noerr(t, err)
35+
if write.WriteConcern != nil {
36+
t.Error("write concern should be omitted from write command, but is present")
37+
}
38+
})
39+
}

x/network/command/find_one_delete.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,16 @@ func (f *FindOneAndDelete) encode(desc description.SelectedServer) (*Write, erro
5555
}
5656
command = append(command, f.Opts...)
5757

58-
return &Write{
59-
Clock: f.Clock,
60-
DB: f.NS.DB,
61-
Command: command,
62-
WriteConcern: f.WriteConcern,
63-
Session: f.Session,
64-
}, nil
58+
write := &Write{
59+
Clock: f.Clock,
60+
DB: f.NS.DB,
61+
Command: command,
62+
Session: f.Session,
63+
}
64+
if desc.WireVersion != nil && desc.WireVersion.Max >= 4 {
65+
write.WriteConcern = f.WriteConcern
66+
}
67+
return write, nil
6568
}
6669

6770
// Decode will decode the wire message using the provided server description. Errors during decoding
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package command
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
7+
"github.com/mongodb/mongo-go-driver/x/network/description"
8+
)
9+
10+
func TestFindOneAndDelete(t *testing.T) {
11+
t.Run("Encode Write Concern for MaxWireVersion >= 4", func(t *testing.T) {
12+
desc := description.SelectedServer{
13+
Server: description.Server{
14+
WireVersion: &description.VersionRange{Min: 0, Max: 4},
15+
},
16+
}
17+
wc := writeconcern.New(writeconcern.WMajority())
18+
cmd := FindOneAndDelete{NS: Namespace{DB: "foo", Collection: "bar"}, WriteConcern: wc}
19+
write, err := cmd.encode(desc)
20+
noerr(t, err)
21+
if write.WriteConcern != wc {
22+
t.Error("write concern should be added to write command, but is missing")
23+
}
24+
})
25+
t.Run("Omit Write Concern for MaxWireVersion < 4", func(t *testing.T) {
26+
desc := description.SelectedServer{
27+
Server: description.Server{
28+
WireVersion: &description.VersionRange{Min: 0, Max: 3},
29+
},
30+
}
31+
wc := writeconcern.New(writeconcern.WMajority())
32+
cmd := FindOneAndDelete{NS: Namespace{DB: "foo", Collection: "bar"}, WriteConcern: wc}
33+
write, err := cmd.encode(desc)
34+
noerr(t, err)
35+
if write.WriteConcern != nil {
36+
t.Error("write concern should be omitted from write command, but is present")
37+
}
38+
})
39+
}

0 commit comments

Comments
 (0)