summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/src/github.com/mongodb/mongo-tools/mongoreplay/auth_test.go
blob: 93e73308211696f34c7ee223666a623938bbf1f6 (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
// 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 mongoreplay

import (
	"testing"

	mgo "github.com/10gen/llmgo"
	"github.com/10gen/llmgo/bson"
)

// TestCommandsAgainstAuthedDBWhenAuthed tests some basic commands against a
// database that requires authentication when the driver has proper
// authentication credentials
func TestCommandsAgainstAuthedDBWhenAuthed(t *testing.T) {
	if !authTestServerMode {
		t.Skipf("Skipping auth test with non-auth DB")
	}
	if err := teardownDB(); err != nil {
		t.Error(err)
	}
	numInserts := 20
	generator := newRecordedOpGenerator()
	docName := "Authed Insert Test"

	go func() {
		defer close(generator.opChan)
		t.Logf("Generating %d inserts\n", numInserts)
		err := generator.generateInsertHelper(docName, 0, numInserts)
		if err != nil {
			t.Error(err)
		}
	}()
	statCollector, _ := newStatCollector(testCollectorOpts, "format", true, true)
	replaySession, err := mgo.Dial(urlAuth)
	if err != nil {
		t.Error(err)
	}

	context := NewExecutionContext(statCollector, replaySession, &ExecutionOptions{})
	t.Logf("Beginning mongoreplay playback of generated traffic against host: %v\n", urlAuth)
	err = Play(context, generator.opChan, testSpeed, 1, 10)
	if err != nil {
		t.Error(err)
	}
	t.Log("Completed mongoreplay playback of generated traffic")

	session, err := mgo.Dial(urlAuth)
	if err != nil {
		t.Error(err)
	}
	coll := session.DB(testDB).C(testCollection)

	iter := coll.Find(bson.D{}).Sort("docNum").Iter()
	ind := 0
	result := testDoc{}

	t.Log("Querying database to ensure insert occurred successfully")
	for iter.Next(&result) {
		t.Logf("Query result: %#v\n", result)
		if err := iter.Err(); err != nil {
			t.Errorf("Iterator returned an error: %v\n", err)
		}
		if result.DocumentNumber != ind {
			t.Errorf("Inserted document number did not match expected document number. Found: %v -- Expected: %v", result.DocumentNumber, ind)
		}
		if result.Name != "Authed Insert Test" {
			t.Errorf("Inserted document name did not match expected name. Found %v -- Expected: %v", result.Name, docName)
		}
		if !result.Success {
			t.Errorf("Inserted document field 'Success' was expected to be true, but was false")
		}
		ind++
	}
	if err := iter.Close(); err != nil {
		t.Error(err)
	}
	if err := teardownDB(); err != nil {
		t.Error(err)
	}

}

// TestCommandsAgainstAuthedDBWhenNotAuthed tests some basic commands against a
// database that requires authentication when the driver does not have proper
// authentication. It generates a series of inserts and ensures that the docs
// they are attempting to insert are not later found in the database
func TestCommandsAgainstAuthedDBWhenNotAuthed(t *testing.T) {
	if !authTestServerMode {
		t.Skipf("Skipping auth test with non-auth DB")
	}
	if err := teardownDB(); err != nil {
		t.Error(err)
	}
	numInserts := 3
	generator := newRecordedOpGenerator()

	go func() {
		defer close(generator.opChan)

		t.Logf("Generating %d inserts\n", numInserts)
		err := generator.generateInsertHelper("Non-Authed Insert Test", 0, numInserts)
		if err != nil {
			t.Error(err)
		}
	}()
	statCollector, _ := newStatCollector(testCollectorOpts, "format", true, true)
	replaySession, err := mgo.Dial(urlNonAuth)
	if err != nil {
		t.Error(err)
	}
	context := NewExecutionContext(statCollector, replaySession, &ExecutionOptions{})
	err = Play(context, generator.opChan, testSpeed, 1, 10)
	if err != nil {
		t.Error(err)
	}
	t.Log("Completed mongoreplay playback of generated traffic")

	session, err := mgo.Dial(urlAuth)
	if err != nil {
		t.Errorf("Error connecting to test server: %v", err)
	}
	coll := session.DB(testDB).C(testCollection)

	t.Log("Performing query to ensure collection received no documents")

	num, err := coll.Find(bson.D{}).Count()
	if err != nil {
		t.Error(err)
	}
	if num != 0 {
		t.Errorf("Collection contained documents, expected it to be empty. Num: %d\n", num)
	}
	if err := teardownDB(); err != nil {
		t.Error(err)
	}

}