Exemple #1
0
func TestIgnoreMismatchingReplicaSets(t *testing.T) {
	t.Parallel()
	creator := ReplicaSetStateCreator{
		Log: nopLogger{},
	}
	replicaSet := mgotest.NewReplicaSet(2, t)
	singleMongo := mgotest.NewStartedServer(t)
	defer func() {
		replicaSet.Stop()
		singleMongo.Stop()
	}()

	urls := replicaSet.Addrs()
	urls = append(urls, singleMongo.URL())

	state, err := creator.FromAddrs("", "", urls, "rs")
	if err != nil {
		t.Fatalf("unexpected error: %s", err)
	}
	if state.lastRS.Name != "rs" {
		t.Fatalf("unexpected replicaset: %s", state.lastRS.Name)
	}

	_, err = creator.FromAddrs("", "", urls, "")
	if err == nil {
		t.Fatalf("missing expected error: %s", err)
	}
}
Exemple #2
0
func NewReplicaSetHarness(n uint, t testing.TB) *ReplicaSetHarness {
	mgoRS := mgotest.NewReplicaSet(n, t)
	return &ReplicaSetHarness{
		Harness:       newHarnessInternal(mgoRS.Addrs()[n-1], mgoRS, t),
		MgoReplicaSet: mgoRS,
	}
}
Exemple #3
0
func NewReplicaSetHarness(n uint, t testing.TB) *ReplicaSetHarness {
	if disableSlowTests {
		t.Skip("disabled because it's slow")
	}
	mgoRS := mgotest.NewReplicaSet(n, t)
	return &ReplicaSetHarness{
		Harness:       newHarnessInternal(mgoRS.Addrs()[n-1], mgoRS, t),
		MgoReplicaSet: mgoRS,
	}
}
Exemple #4
0
func main() {
	l := log.New(os.Stdout, "", log.LstdFlags)
	n := flag.Uint("n", 3, "num nodes")
	flag.Parse()
	rs := mgotest.NewReplicaSet(*n, l)
	fmt.Println(rs.Addrs())
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	<-c
	rs.Stop()
}
Exemple #5
0
func Test_Tail(t *testing.T) {
	replset := mgotest.NewReplicaSet(2, t)
	defer replset.Stop()
	session := replset.Session()
	defer session.Close()
	session.EnsureSafe(&mgo.Safe{WMode: "majority"})

	var results bytes.Buffer
	var buffer bytes.Buffer

	logs := make(chan Oplog)
	done := make(chan bool)
	last := LastTime(session)

	q := OplogQuery{session, bson.M{"ts": bson.M{"$gt": last}, "ns": "TailTest.test"}, time.Second * 10}
	go q.Tail(logs, done)
	var printwg sync.WaitGroup
	go func() {
		printwg.Add(1)
		defer printwg.Done()
		printlog(&results, logs)
	}()

	db := session.DB("TailTest")
	defer db.DropDatabase()
	coll := db.C("test")
	for i := 0; i < 5; i++ {
		id := bson.NewObjectId()
		if err := coll.Insert(bson.M{"name": "test_0", "_id": id}); err != nil {
			t.Fatal(err)
		}
		fmt.Fprintf(&buffer, "TailTest.test|i|%s\n", id.Hex())
	}

	<-done
	close(logs)
	printwg.Wait()

	resultsString := results.String()
	bufferString := buffer.String()
	if resultsString != bufferString {
		fmt.Printf("Got:\n %s\n\n Should have gotten: \n%s", resultsString, bufferString)
		t.Fail()
	}
}
Exemple #6
0
func TestChecksWithReplicaSets(t *testing.T) {
	t.Parallel()
	standalone := mgotest.NewStartedServer(t)
	defer standalone.Stop()
	rs := mgotest.NewReplicaSet(3, t)
	defer rs.Stop()

	if err := checkReplSetStatus(rs.Addrs(), "rs"); err != nil {
		t.Error("check should pass if all members are in the replica set:", err)
	}
	if err := checkReplSetStatus([]string{standalone.URL()}, "rs"); err == nil {
		t.Error("expected failure if single server running in standalone")
	}
	if err := checkReplSetStatus(append(rs.Addrs(), standalone.URL()), "rs"); err != nil {
		t.Error("check should ignore standalone if there are other healthy members:", err)
	}
	if err := checkReplSetStatus(rs.Addrs(), "rs-alt"); err == nil {
		t.Error("check should fail if members are in a different replica set")
	}
}