Exemple #1
0
func simpleMongoDumpInstance() *MongoDump {
	ssl := testutil.GetSSLOptions()
	auth := testutil.GetAuthOptions()
	namespace := &options.Namespace{
		DB: testDB,
	}
	connection := &options.Connection{
		Host: testServer,
		Port: testPort,
	}
	toolOptions := &options.ToolOptions{
		SSL:           &ssl,
		Namespace:     namespace,
		Connection:    connection,
		Auth:          &auth,
		HiddenOptions: &options.HiddenOptions{},
		Verbosity:     &options.Verbosity{},
	}
	outputOptions := &OutputOptions{}
	inputOptions := &InputOptions{}

	log.SetVerbosity(toolOptions.Verbosity)

	return &MongoDump{
		ToolOptions:     toolOptions,
		InputOptions:    inputOptions,
		OutputOptions:   outputOptions,
		ProgressManager: progress.NewProgressBarManager(log.Writer(0), progressBarWaitTime),
		HandleSignals:   HandleSignals,
	}
}
Exemple #2
0
func simpleMongoDumpInstance() *MongoDump {
	ssl := testutil.GetSSLOptions()
	auth := testutil.GetAuthOptions()
	namespace := &options.Namespace{
		DB: testDB,
	}
	connection := &options.Connection{
		Host: testServer,
		Port: testPort,
	}
	toolOptions := &options.ToolOptions{
		SSL:        &ssl,
		Namespace:  namespace,
		Connection: connection,
		Auth:       &auth,
		Verbosity:  &options.Verbosity{},
	}
	outputOptions := &OutputOptions{
		NumParallelCollections: 1,
	}
	inputOptions := &InputOptions{}

	log.SetVerbosity(toolOptions.Verbosity)

	return &MongoDump{
		ToolOptions:   toolOptions,
		InputOptions:  inputOptions,
		OutputOptions: outputOptions,
	}
}
// getBasicToolOptions returns a test helper to instantiate the session provider
// for calls to StreamDocument
func getBasicToolOptions() *options.ToolOptions {
	ssl := testutil.GetSSLOptions()
	auth := testutil.GetAuthOptions()
	namespace := &options.Namespace{
		DB:         testDb,
		Collection: testCollection,
	}
	connection := &options.Connection{
		Host: "localhost",
		Port: db.DefaultTestPort,
	}
	return &options.ToolOptions{
		SSL:           &ssl,
		Namespace:     namespace,
		Connection:    connection,
		HiddenOptions: &options.HiddenOptions{},
		Auth:          &auth,
	}
}
Exemple #4
0
func getBareSession() (*mgo.Session, error) {
	ssl := testutil.GetSSLOptions()
	auth := testutil.GetAuthOptions()
	sessionProvider, err := db.NewSessionProvider(options.ToolOptions{
		Connection: &options.Connection{
			Host: testServer,
			Port: testPort,
		},
		Auth: &auth,
		SSL:  &ssl,
	})
	if err != nil {
		return nil, err
	}
	session, err := sessionProvider.GetSession()
	if err != nil {
		return nil, err
	}
	return session, nil
}
Exemple #5
0
func TestCollectionExists(t *testing.T) {

	testutil.VerifyTestType(t, testutil.IntegrationTestType)

	Convey("With a test mongorestore", t, func() {
		ssl := testutil.GetSSLOptions()
		auth := testutil.GetAuthOptions()
		sessionProvider, err := db.NewSessionProvider(commonOpts.ToolOptions{
			Connection: &commonOpts.Connection{
				Host: "localhost",
				Port: db.DefaultTestPort,
			},
			Auth: &auth,
			SSL:  &ssl,
		})
		So(err, ShouldBeNil)

		restore := &MongoRestore{
			SessionProvider: sessionProvider,
		}

		Convey("and some test data in a server", func() {
			session, err := restore.SessionProvider.GetSession()
			So(err, ShouldBeNil)
			So(session.DB(ExistsDB).C("one").Insert(bson.M{}), ShouldBeNil)
			So(session.DB(ExistsDB).C("two").Insert(bson.M{}), ShouldBeNil)
			So(session.DB(ExistsDB).C("three").Insert(bson.M{}), ShouldBeNil)

			Convey("collections that exist should return true", func() {
				exists, err := restore.CollectionExists(&intents.Intent{DB: ExistsDB, C: "one"})
				So(err, ShouldBeNil)
				So(exists, ShouldBeTrue)
				exists, err = restore.CollectionExists(&intents.Intent{DB: ExistsDB, C: "two"})
				So(err, ShouldBeNil)
				So(exists, ShouldBeTrue)
				exists, err = restore.CollectionExists(&intents.Intent{DB: ExistsDB, C: "three"})
				So(err, ShouldBeNil)
				So(exists, ShouldBeTrue)

				Convey("and those that do not exist should return false", func() {
					exists, err = restore.CollectionExists(&intents.Intent{DB: ExistsDB, C: "four"})
					So(err, ShouldBeNil)
					So(exists, ShouldBeFalse)
				})
			})

			Reset(func() {
				session.DB(ExistsDB).DropDatabase()
				session.Close()
			})
		})

		Convey("and a fake cache should be used instead of the server when it exists", func() {
			restore.knownCollections = map[string][]string{
				ExistsDB: []string{"cats", "dogs", "snakes"},
			}
			exists, err := restore.CollectionExists(&intents.Intent{DB: ExistsDB, C: "dogs"})
			So(err, ShouldBeNil)
			So(exists, ShouldBeTrue)
			exists, err = restore.CollectionExists(&intents.Intent{DB: ExistsDB, C: "two"})
			So(err, ShouldBeNil)
			So(exists, ShouldBeFalse)
		})
	})
}
Exemple #6
0
	. "github.com/smartystreets/goconvey/convey"
	"gopkg.in/mgo.v2"
	"io"
	"io/ioutil"
	"os"
	"strings"
	"testing"
)

var (
	testDB     = "mongofiles_test_db"
	testServer = "localhost"
	testPort   = db.DefaultTestPort

	ssl        = testutil.GetSSLOptions()
	auth       = testutil.GetAuthOptions()
	connection = &options.Connection{
		Host: testServer,
		Port: testPort,
	}
	toolOptions = &options.ToolOptions{
		SSL:        &ssl,
		Connection: connection,
		Auth:       &auth,
		Verbosity:  &options.Verbosity{},
	}
)

// put in some test data into GridFS
func setUpGridFSTestData() ([]interface{}, error) {
	sessionProvider, err := db.NewSessionProvider(*toolOptions)
func TestBasicOps(t *testing.T) {
	testutil.VerifyTestType(t, testutil.IntegrationTestType)

	var opts *options.ToolOptions
	var sourceOpts *SourceOptions

	Convey("When replicating operations", t, func() {
		ssl := testutil.GetSSLOptions()
		auth := testutil.GetAuthOptions()

		// specify localhost:33333 as the destination host
		opts = &options.ToolOptions{
			Namespace: &options.Namespace{},
			SSL:       &ssl,
			Auth:      &auth,
			Kerberos:  &options.Kerberos{},
			Connection: &options.Connection{
				Host: "localhost",
				Port: db.DefaultTestPort,
			},
		}

		// specify localhost:33333 as the source host
		sourceOpts = &SourceOptions{
			Seconds: 84600,            // the default
			OplogNS: "local.oplog.rs", // the default
		}

		Convey("all operations should be applied correctly, without"+
			" error", func() {

			// set the "oplog" we will use
			sourceOpts.OplogNS = "mongooplog_test.oplog"

			// initialize a session provider for the source
			sourceSP, err := db.NewSessionProvider(*opts)
			So(err, ShouldBeNil)

			// initialize a session provider for the destination
			destSP, err := db.NewSessionProvider(*opts)
			So(err, ShouldBeNil)

			// clear out the oplog
			sess, err := sourceSP.GetSession()
			So(err, ShouldBeNil)
			defer sess.Close()
			oplogColl := sess.DB("mongooplog_test").C("oplog")
			oplogColl.DropCollection()

			// create the oplog as a capped collection, so it can be tailed
			So(sess.DB("mongooplog_test").Run(
				bson.D{{"create", "oplog"}, {"capped", true},
					{"size", 10000}},
				bson.M{}),
				ShouldBeNil)

			// create the collection we are testing against (ignore errors)
			sess.DB("mongooplog_test").C("data").Create(&mgo.CollectionInfo{})

			// clear out the collection we'll use for testing
			testColl := sess.DB("mongooplog_test").C("data")
			_, err = testColl.RemoveAll(bson.M{})
			So(err, ShouldBeNil)

			// insert some "ops" into the oplog to be found and applied
			op1 := &db.Oplog{
				Timestamp: bson.MongoTimestamp(1<<63 - 1), // years in the future
				HistoryID: 100,
				Version:   2,
				Operation: "i",
				Namespace: "mongooplog_test.data",
				Object: bson.D{
					bson.DocElem{
						"_id", 3,
					},
				},
			}
			So(oplogColl.Insert(op1), ShouldBeNil)
			op2 := &db.Oplog{
				Timestamp: bson.MongoTimestamp(1<<63 - 1), // years in the future
				HistoryID: 200,
				Version:   2,
				Operation: "i",
				Namespace: "mongooplog_test.data",
				Object: bson.D{
					bson.DocElem{
						"_id", 4,
					},
				},
			}
			So(oplogColl.Insert(op2), ShouldBeNil)

			// this one should be filtered out, since it occurred before the
			// threshold
			op3 := &db.Oplog{
				Timestamp: bson.MongoTimestamp(1<<62 - 1), // more than 1 day in the past
				HistoryID: 300,
				Version:   2,
				Operation: "i",
				Namespace: "mongooplog_test.data",
				Object: bson.D{
					bson.DocElem{
						"_id", 3,
					},
				},
			}
			So(oplogColl.Insert(op3), ShouldBeNil)

			// initialize the mongooplog
			oplog := MongoOplog{
				ToolOptions:         opts,
				SourceOptions:       sourceOpts,
				SessionProviderFrom: sourceSP,
				SessionProviderTo:   destSP,
			}

			// run it
			So(oplog.Run(), ShouldBeNil)

			// the operations should have been applied
			var inserted []bson.M
			So(testColl.Find(bson.M{}).Sort("_id").All(&inserted),
				ShouldBeNil)
			So(len(inserted), ShouldEqual, 2)
			So(inserted[0]["_id"], ShouldEqual, 3)
			So(inserted[1]["_id"], ShouldEqual, 4)

		})

	})

}
func TestMongorestore(t *testing.T) {
	ssl := testutil.GetSSLOptions()
	auth := testutil.GetAuthOptions()

	testutil.VerifyTestType(t, testutil.IntegrationTestType)
	toolOptions := &options.ToolOptions{
		Connection: &options.Connection{
			Host: testServer,
			Port: testPort,
		},
		Auth: &auth,
		SSL:  &ssl,
	}
	inputOptions := &InputOptions{}
	outputOptions := &OutputOptions{
		NumParallelCollections: 1,
		NumInsertionWorkers:    1,
		WriteConcern:           "majority",
	}
	nsOptions := &NSOptions{}
	Convey("With a test MongoRestore", t, func() {
		provider, err := db.NewSessionProvider(*toolOptions)
		if err != nil {
			log.Logvf(log.Always, "error connecting to host: %v", err)
			os.Exit(util.ExitError)
		}
		restore := MongoRestore{
			ToolOptions:     toolOptions,
			OutputOptions:   outputOptions,
			InputOptions:    inputOptions,
			NSOptions:       nsOptions,
			SessionProvider: provider,
		}
		session, _ := provider.GetSession()
		c1 := session.DB("db1").C("c1")
		c1.DropCollection()
		Convey("and an explicit target restores from that dump directory", func() {
			restore.TargetDirectory = "testdata/testdirs"
			err = restore.Restore()
			So(err, ShouldBeNil)
			count, err := c1.Count()
			So(err, ShouldBeNil)
			So(count, ShouldEqual, 100)
		})

		Convey("and an target of '-' restores from standard input", func() {
			bsonFile, err := os.Open("testdata/testdirs/db1/c1.bson")
			restore.NSOptions.Collection = "c1"
			restore.NSOptions.DB = "db1"
			So(err, ShouldBeNil)
			restore.stdin = bsonFile
			restore.TargetDirectory = "-"
			err = restore.Restore()
			So(err, ShouldBeNil)
			count, err := c1.Count()
			So(err, ShouldBeNil)
			So(count, ShouldEqual, 100)
		})

	})
}