summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/src/github.com/mongodb/mongo-tools/common/db/connector_test.go
blob: 48ef3611587840177fab47bbd505e6c4a8c0c281 (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
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
// Copyright (C) MongoDB, Inc. 2014-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 db_test

import (
	"testing"
	"time"

	"github.com/mongodb/mongo-tools/common/db"
	"github.com/mongodb/mongo-tools/common/options"
	"github.com/mongodb/mongo-tools/common/testtype"
	"github.com/mongodb/mongo-tools/common/testutil"
	. "github.com/smartystreets/goconvey/convey"
	"gopkg.in/mgo.v2"
)

func TestVanillaDBConnector(t *testing.T) {

	testtype.VerifyTestType(t, "db")

	Convey("With a vanilla db connector", t, func() {

		var connector *db.VanillaDBConnector

		Convey("calling Configure should populate the addrs and dial timeout"+
			" appropriately with no error", func() {

			connector = &db.VanillaDBConnector{}

			opts := options.ToolOptions{
				Connection: &options.Connection{
					Host: "host1,host2",
					Port: "20000",
				},
				Auth: &options.Auth{},
			}
			So(connector.Configure(opts), ShouldBeNil)
			info := db.VanillaTestWrapper(*connector).DialInfo()
			So(info.Addrs, ShouldResemble,
				[]string{"host1:20000", "host2:20000"})
			So(info.Timeout, ShouldResemble, time.Duration(opts.Timeout)*time.Second)

		})

		Convey("calling GetNewSession with a running mongod should connect"+
			" successfully", func() {

			connector = &db.VanillaDBConnector{}

			opts := options.ToolOptions{
				Connection: &options.Connection{
					Host: "localhost",
					Port: db.DefaultTestPort,
				},
				Auth: &options.Auth{},
			}
			So(connector.Configure(opts), ShouldBeNil)

			session, err := connector.GetNewSession()
			So(err, ShouldBeNil)
			So(session, ShouldNotBeNil)
			session.Close()

		})

	})

}

func TestVanillaDBConnectorWithAuth(t *testing.T) {
	testtype.VerifyTestType(t, "auth")
	session, err := mgo.Dial("localhost:33333")
	if err != nil {
		t.Fatalf("error dialing server: %v", err)
	}

	err = testutil.CreateUserAdmin(session)
	So(err, ShouldBeNil)
	err = testutil.CreateUserWithRole(session, "cAdmin", "password",
		mgo.RoleClusterAdmin, true)
	So(err, ShouldBeNil)
	session.Close()

	Convey("With a vanilla db connector and a mongod running with"+
		" auth", t, func() {

		var connector *db.VanillaDBConnector

		Convey("connecting without authentication should not be able"+
			" to run commands", func() {

			connector = &db.VanillaDBConnector{}

			opts := options.ToolOptions{
				Connection: &options.Connection{
					Host: "localhost",
					Port: db.DefaultTestPort,
				},
				Auth: &options.Auth{},
			}
			So(connector.Configure(opts), ShouldBeNil)

			session, err := connector.GetNewSession()
			So(err, ShouldBeNil)
			So(session, ShouldNotBeNil)

			So(session.DB("admin").Run("top", &struct{}{}), ShouldNotBeNil)
			session.Close()

		})

		Convey("connecting with authentication should succeed and"+
			" authenticate properly", func() {

			connector = &db.VanillaDBConnector{}

			opts := options.ToolOptions{
				Connection: &options.Connection{
					Host: "localhost",
					Port: db.DefaultTestPort,
				},
				Auth: &options.Auth{
					Username: "cAdmin",
					Password: "password",
				},
			}
			So(connector.Configure(opts), ShouldBeNil)

			session, err := connector.GetNewSession()
			So(err, ShouldBeNil)
			So(session, ShouldNotBeNil)

			So(session.DB("admin").Run("top", &struct{}{}), ShouldBeNil)
			session.Close()

		})

	})

}