summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/src/github.com/mongodb/mongo-tools/common/db/db_test.go
blob: a922bbdff44b588802b1c1c6b9ac3fe7bd6ceb21 (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
145
146
147
148
149
150
151
152
153
154
// 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

import (
	"reflect"
	"testing"

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

func TestNewSessionProvider(t *testing.T) {

	testtype.SkipUnlessTestType(t, testtype.IntegrationTestType)
	auth := DBGetAuthOptions()
	ssl := DBGetSSLOptions()

	Convey("When initializing a session provider", t, func() {

		Convey("with the standard options, a provider with a standard"+
			" connector should be returned", func() {
			opts := options.ToolOptions{
				Connection: &options.Connection{
					Port: DefaultTestPort,
				},
				SSL:  &ssl,
				Auth: &auth,
			}
			provider, err := NewSessionProvider(opts)
			So(err, ShouldBeNil)
			if !testtype.HasTestType(testtype.SSLTestType) {
				So(reflect.TypeOf(provider.connector), ShouldEqual,
					reflect.TypeOf(&VanillaDBConnector{}))
			}

			Convey("and should be closeable", func() {
				provider.Close()
			})

		})

		Convey("the master session should be successfully "+
			" initialized", func() {
			opts := options.ToolOptions{
				Connection: &options.Connection{
					Port: DefaultTestPort,
				},
				SSL:  &ssl,
				Auth: &auth,
			}
			provider, err := NewSessionProvider(opts)
			So(err, ShouldBeNil)
			So(provider.masterSession, ShouldBeNil)
			session, err := provider.GetSession()
			So(err, ShouldBeNil)
			So(session, ShouldNotBeNil)
			session.Close()
			So(provider.masterSession, ShouldNotBeNil)
			err = provider.masterSession.Ping()
			So(err, ShouldBeNil)
			provider.Close()
			So(func() {
				provider.masterSession.Ping()
			}, ShouldPanic)

		})

	})

}

func TestGetIndexes(t *testing.T) {

	testtype.SkipUnlessTestType(t, testtype.IntegrationTestType)

	Convey("With a valid session", t, func() {
		auth := DBGetAuthOptions()
		ssl := DBGetSSLOptions()
		opts := options.ToolOptions{
			Connection: &options.Connection{
				Port: DefaultTestPort,
			},
			SSL:  &ssl,
			Auth: &auth,
		}
		provider, err := NewSessionProvider(opts)
		So(err, ShouldBeNil)
		session, err := provider.GetSession()
		So(err, ShouldBeNil)

		existing := session.DB("exists").C("collection")
		missing := session.DB("exists").C("missing")
		missingDB := session.DB("missingDB").C("missingCollection")

		err = existing.Database.DropDatabase()
		So(err, ShouldBeNil)
		err = existing.Create(&mgo.CollectionInfo{})
		So(err, ShouldBeNil)
		err = missingDB.Database.DropDatabase()
		So(err, ShouldBeNil)

		Convey("When GetIndexes is called on", func() {
			Convey("an existing collection there should be no error", func() {
				indexesIter, err := GetIndexes(existing)
				So(err, ShouldBeNil)
				Convey("and indexes should be returned", func() {
					So(indexesIter, ShouldNotBeNil)
					var indexes []mgo.Index
					err := indexesIter.All(&indexes)
					So(err, ShouldBeNil)
					So(len(indexes), ShouldBeGreaterThan, 0)
				})
			})

			Convey("a missing collection there should be no error", func() {
				indexesIter, err := GetIndexes(missing)
				So(err, ShouldBeNil)
				Convey("and there should be no indexes", func() {
					So(indexesIter, ShouldBeNil)
				})
			})

			Convey("a missing database there should be no error", func() {
				indexesIter, err := GetIndexes(missingDB)
				So(err, ShouldBeNil)
				Convey("and there should be no indexes", func() {
					So(indexesIter, ShouldBeNil)
				})
			})
		})

		Reset(func() {
			existing.Database.DropDatabase()
			session.Close()
			provider.Close()
		})
	})
}

type listDatabasesCommand struct {
	Databases []map[string]interface{} `json:"databases"`
	Ok        bool                     `json:"ok"`
}

func (self *listDatabasesCommand) AsRunnable() interface{} {
	return "listDatabases"
}