summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/common/util/mongo_test.go
blob: 58001698524a1daa0021182dd63319013451995a (plain)
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
package util

import (
	"github.com/mongodb/mongo-tools/common/testutil"
	. "github.com/smartystreets/goconvey/convey"
	"testing"
)

func TestParseConnectionString(t *testing.T) {

	testutil.VerifyTestType(t, "unit")

	Convey("When extracting the replica set and hosts from a connection"+
		" url", t, func() {

		Convey("an empty url should lead to an empty replica set name"+
			" and hosts slice", func() {
			hosts, setName := ParseConnectionString("")
			So(hosts, ShouldResemble, []string{""})
			So(setName, ShouldEqual, "")
		})

		Convey("a url not specifying a replica set name should lead to"+
			" an empty replica set name", func() {
			hosts, setName := ParseConnectionString("host1,host2")
			So(hosts, ShouldResemble, []string{"host1", "host2"})
			So(setName, ShouldEqual, "")
		})

		Convey("a url specifying a replica set name should lead to that name"+
			" being returned", func() {
			hosts, setName := ParseConnectionString("foo/host1,host2")
			So(hosts, ShouldResemble, []string{"host1", "host2"})
			So(setName, ShouldEqual, "foo")
		})

	})

}

func TestCreateConnectionAddrs(t *testing.T) {

	testutil.VerifyTestType(t, "unit")

	Convey("When creating the slice of connection addresses", t, func() {

		Convey("if no port is specified, the addresses should all appear"+
			" unmodified in the result", func() {

			addrs := CreateConnectionAddrs("host1,host2", "")
			So(addrs, ShouldResemble, []string{"host1", "host2"})

		})

		Convey("if a port is specified, it should be appended to each host"+
			" from the host connection string", func() {

			addrs := CreateConnectionAddrs("host1,host2", "20000")
			So(addrs, ShouldResemble, []string{"host1:20000", "host2:20000"})

		})

	})

}

func TestInvalidNames(t *testing.T) {

	Convey("Checking some invalid collection names, ", t, func() {
		Convey("test.col$ is invalid", func() {
			So(ValidateDBName("test"), ShouldBeNil)
			So(ValidateCollectionName("col$"), ShouldNotBeNil)
			So(ValidateFullNamespace("test.col$"), ShouldNotBeNil)
		})
		Convey("db/aaa.col is invalid", func() {
			So(ValidateDBName("db/aaa"), ShouldNotBeNil)
			So(ValidateCollectionName("col"), ShouldBeNil)
			So(ValidateFullNamespace("db/aaa.col"), ShouldNotBeNil)
		})
		Convey("db. is invalid", func() {
			So(ValidateDBName("db"), ShouldBeNil)
			So(ValidateCollectionName(""), ShouldNotBeNil)
			So(ValidateFullNamespace("db."), ShouldNotBeNil)
		})
		Convey("db space.col is invalid", func() {
			So(ValidateDBName("db space"), ShouldNotBeNil)
			So(ValidateCollectionName("col"), ShouldBeNil)
			So(ValidateFullNamespace("db space.col"), ShouldNotBeNil)
		})
		Convey("db x$x is invalid", func() {
			So(ValidateDBName("x$x"), ShouldNotBeNil)
			So(ValidateFullNamespace("x$x.y"), ShouldNotBeNil)
		})
		Convey("[null].[null] is invalid", func() {
			So(ValidateDBName("\x00"), ShouldNotBeNil)
			So(ValidateCollectionName("\x00"), ShouldNotBeNil)
			So(ValidateFullNamespace("\x00.\x00"), ShouldNotBeNil)
		})
		Convey("[empty] is invalid", func() {
			So(ValidateFullNamespace(""), ShouldNotBeNil)
		})
		Convey("db.col is valid", func() {
			So(ValidateDBName("db"), ShouldBeNil)
			So(ValidateCollectionName("col"), ShouldBeNil)
			So(ValidateFullNamespace("db.col"), ShouldBeNil)
		})

	})

}