예제 #1
0
func setConnectionURL() error {
	testPort := os.Getenv("DB_PORT")
	if testPort == "" {
		testPort = defaultTestPort
	}
	var url string
	if os.Getenv("AUTH") == "1" {
		url = authTestServerURL
	} else {
		url = nonAuthTestServerURL
	}
	dialURL := fmt.Sprintf("%s:%v", url, testPort)
	if os.Getenv("AUTH") == "1" {
		dialURL += "/admin"
	}
	session, err := mgo.DialWithTimeout(dialURL, 30*time.Second)
	if err != nil {
		return fmt.Errorf("%v:%v", dialURL, err)
	}

	port, err := getPrimaryPort(session)
	if err != nil {
		return fmt.Errorf("%v:%v", dialURL, err)
	}
	if port == "" {
		port = testPort
	}
	urlNonAuth = fmt.Sprintf("%s:%s", nonAuthTestServerURL, port)
	urlAuth = fmt.Sprintf("%s:%v/admin", authTestServerURL, port)
	currentTestURL = urlNonAuth
	if os.Getenv("AUTH") == "1" {
		currentTestURL = urlAuth
	}
	return nil
}
예제 #2
0
func teardownDB() error {
	session, err := mgo.DialWithTimeout(currentTestURL, 30*time.Second)
	if err != nil {
		return err
	}

	session.DB(testDB).C(testCollection).DropCollection()
	session.Close()
	return nil
}
예제 #3
0
func TestCommandOpInsertLiveDB(t *testing.T) {
	if err := teardownDB(); err != nil {
		t.Error(err)
	}
	if isMongosTestServer {
		t.Skipf("Skipping OpCommand test against mongos")
	}

	numInserts := 20
	insertName := "LiveDB CommandOp insert test"
	generator := newRecordedOpGenerator()
	go func() {
		defer close(generator.opChan)
		// generate numInserts RecordedOps
		t.Logf("Generating %d commandOp inserts\n", numInserts)
		err := generator.generateCommandOpInsertHelper(insertName, 0, numInserts)
		if err != nil {
			t.Error(err)
		}
	}()

	statCollector, _ := newStatCollector(testCollectorOpts, true, true)
	statRec := statCollector.StatRecorder.(*BufferedStatRecorder)
	context := NewExecutionContext(statCollector)

	// run mongoreplay's Play loop with the stubbed objects
	t.Logf("Beginning mongoreplay playback of generated traffic against host: %v\n", currentTestURL)
	err := Play(context, generator.opChan, testSpeed, currentTestURL, 1, 10)
	if err != nil {
		t.Errorf("Error Playing traffic: %v\n", err)
	}
	t.Log("Completed mongoreplay playback of generated traffic")

	// prepare a query for the database
	session, err := mgo.DialWithTimeout(currentTestURL, 30*time.Second)
	if err != nil {
		t.Errorf("Error connecting to test server: %v", err)
	}

	coll := session.DB(testDB).C(testCollection)

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

	// iterate over the results of the query and ensure they match expected documents
	t.Log("Querying database to ensure insert occured successfully")
	for iter.Next(&result) {
		t.Logf("Query result: %#v\n", result)
		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 != insertName {
			t.Errorf("Inserted document name did not match expected name. Found %v -- Expected: %v", result.Name, "LiveDB Insert Test")
		}
		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)
	}

	// iterate over the operations found by the BufferedStatCollector
	t.Log("Examining collected stats to ensure they match expected")
	for i := 0; i < numInserts; i++ {
		stat := statRec.Buffer[i]
		t.Logf("Stat result: %#v\n", stat)
		// all commands should be inserts into mongoreplay.test
		if stat.OpType != "op_command" ||
			stat.Command != "insert" ||
			stat.Ns != "mongoreplay" {
			t.Errorf("Expected to see an insert into mongoreplay, but instead saw %v, %v into %v\n", stat.OpType, stat.Command, stat.Ns)
		}
	}

	if err := teardownDB(); err != nil {
		t.Error(err)
	}
}
예제 #4
0
// TestUpdateOpLiveDB tests the functionality of mongoreplay replaying an update
// against a live database Generates 20 recorded inserts and an update and
// passes them to the main execution of mongoreplay and queries the database to
// verify they were completed. It then checks its BufferedStatCollector to
// ensure the update matches what we expected.
func TestUpdateOpLiveDB(t *testing.T) {
	if err := teardownDB(); err != nil {
		t.Error(err)
	}

	numInserts := 20
	insertName := "LiveDB update test"
	generator := newRecordedOpGenerator()
	nameSpace := fmt.Sprintf("%s.%s", testDB, testCollection)
	flags := uint32(1<<1 | 1)

	docNum := bson.D{{"$lte", 9.0}}
	selector := bson.D{{"docNum", docNum}}
	change := bson.D{{"updated", true}}
	update := bson.D{{"$set", change}}
	updateOp := mgo.UpdateOp{
		Collection: nameSpace,
		Selector:   selector,
		Update:     update,
		Flags:      flags,
		Multi:      true,
		Upsert:     true,
	}

	go func() {
		defer close(generator.opChan)
		// generate numInserts RecordedOps
		t.Logf("Generating %d inserts\n", numInserts)
		err := generator.generateInsertHelper(insertName, 0, numInserts)
		if err != nil {
			t.Error(err)
		}
		recordedUpdate, err := generator.fetchRecordedOpsFromConn(&updateOp)
		if err != nil {
			t.Error(err)
		}
		generator.pushDriverRequestOps(recordedUpdate)

		t.Log("Generating getLastError")
		err = generator.generateGetLastError()
		if err != nil {
			t.Error(err)
		}
	}()

	statCollector, _ := newStatCollector(testCollectorOpts, true, true)
	statRec := statCollector.StatRecorder.(*BufferedStatRecorder)
	context := NewExecutionContext(statCollector)

	// run mongoreplay's Play loop with the stubbed objects
	t.Logf("Beginning mongoreplay playback of generated traffic against host: %v\n", currentTestURL)
	err := Play(context, generator.opChan, testSpeed, currentTestURL, 1, 10)
	if err != nil {
		t.Errorf("Error Playing traffic: %v\n", err)
	}
	t.Log("Completed mongoreplay playback of generated traffic")

	// prepare a query for the database
	session, err := mgo.DialWithTimeout(currentTestURL, 30*time.Second)
	if err != nil {
		t.Errorf("Error connecting to test server: %v", err)
	}

	coll := session.DB(testDB).C(testCollection)

	iter := coll.Find(bson.D{}).Sort("docNum").Iter()
	ind := 0
	result := struct {
		DocumentNumber int    `bson:"docNum"`
		Name           string `bson:"name"`
		Updated        bool   `bson:"updated"`
	}{}

	// iterate over the results of the query and ensure they match expected documents
	t.Log("Querying database to ensure insert occured successfully")
	for iter.Next(&result) {
		t.Logf("Query result: %#v\n", result)
		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 != insertName {
			t.Errorf("Inserted document name did not match expected name. Found %v -- Expected: %v", result.Name, "LiveDB update test")
		}
		if result.DocumentNumber <= 9 {
			if result.Updated != true {
				t.Errorf("Document with number %v was supposed to be updated but wasn't", result.DocumentNumber)
			}
		}
		ind++
	}
	if err := iter.Close(); err != nil {
		t.Error(err)
	}

	// iterate over the operations found by the BufferedStatCollector
	t.Log("Examining collected stats to ensure they match expected")

	stat := statRec.Buffer[numInserts]
	t.Logf("Stat result: %#v\n", stat)
	// All commands should be inserts into mongoreplay.test
	if stat.OpType != "update" ||
		stat.Ns != "mongoreplay.test" {
		t.Errorf("Expected to see an update to mongoreplay.test, but instead saw %v, %v\n", stat.OpType, stat.Ns)
	}
	if err := teardownDB(); err != nil {
		t.Error(err)
	}
}