Exemplo n.º 1
0
func TestCBClustering(t *testing.T) {
	if !couchbase_running(couchbase_location) {
		t.Skip("Couchbase not running - skipping test")
	}
	ds, err := mock.NewDatastore("mock:")
	as, err := accounting_stub.NewAccountingStore("stub:")
	cs, err := NewConfigstore("http://" + couchbase_location + ":8091")
	if err != nil {
		t.Fatalf("Error creating configstore: ", err)
	}
	version := clustering.NewVersion("0.7.0")

	fmt.Printf("Created config store %v\n\n", cs)

	cfm := cs.ConfigurationManager()

	cluster1, _ := NewCluster("cluster1", version, cs, ds, as)

	fmt.Printf("Creating cluster %v\n\n", cluster1)

	cluster1, err = cfm.AddCluster(cluster1)

	if err != nil {
		t.Fatalf("Error adding cluster: ", err)
	}

	_, no_such_cluster := cs.ClusterByName("no_such_cluster")
	if no_such_cluster == nil {
		t.Fatalf("Expected error retrieving configuration of non-existent cluster")
	}
	if no_such_cluster.Code() != 2040 && no_such_cluster.TranslationKey() != "admin.clustering.get_cluster_error" {
		t.Fatalf("Expected error code %d", 2010)
	}
	// There should be a cluster called "default" in the Couchbase installation:
	cluster1check, errCheck := cs.ClusterByName("default")
	if errCheck != nil {
		t.Fatalf("Unexpected Error retrieving cluster by name: ", errCheck)
	}

	fmt.Printf("Retrieved cluster: %v\n\n", cluster1check)

	cm := cs.ConfigurationManager()

	// Get all clusters. There should be at least one ("default")
	clusters, errCheck := cm.GetClusters()
	clusters_json, json_err := json.Marshal(clusters)
	if err != nil {
		t.Fatalf("Unexpected Error marshalling GetClusters: ", json_err)
	}

	fmt.Printf("Retrieved clusters: %s\n", string(clusters_json))
	if errCheck != nil {
		t.Fatalf("Unexpected Error retrieving all cluster configs: ", errCheck)
	}
	iterateClusters(clusters, t)
}
Exemplo n.º 2
0
func NewDatastore(uri string) (datastore.Datastore, errors.Error) {
	if strings.HasPrefix(uri, ".") || strings.HasPrefix(uri, "/") {
		return file.NewDatastore(uri)
	}

	if strings.HasPrefix(uri, "http:") {
		return couchbase.NewDatastore(uri)
	}

	if strings.HasPrefix(uri, "dir:") {
		return file.NewDatastore(uri[4:])
	}

	if strings.HasPrefix(uri, "file:") {
		return file.NewDatastore(uri[5:])
	}

	if strings.HasPrefix(uri, "mock:") {
		return mock.NewDatastore(uri)
	}

	return nil, errors.NewError(nil, fmt.Sprintf("Invalid datastore uri: %s", uri))
}
Exemplo n.º 3
0
func TestSystem(t *testing.T) {
	// Use mock to test system; 2 namespaces with 5 keyspaces per namespace
	m, err := mock.NewDatastore("mock:namespaces=2,keyspaces=5,items=5000")
	if err != nil {
		t.Fatalf("failed to create mock store: %v", err)
	}

	// Create systems store with mock m as the ActualStore
	s, err := NewDatastore(m)
	if err != nil {
		t.Fatalf("failed to create system store: %v", err)
	}

	// The systems store should have keyspaces "system", "namespaces", "keyspaces", "indexes"
	p, err := s.NamespaceByName("#system")
	if err != nil {
		t.Fatalf("failed to get system namespace: %v", err)
	}

	pb, err := p.KeyspaceByName("namespaces")
	if err != nil {
		t.Fatalf("failed to get keyspace by name %v", err)
	}

	bb, err := p.KeyspaceByName("keyspaces")
	if err != nil {
		t.Fatalf("failed to get keyspace by name %v", err)
	}

	ib, err := p.KeyspaceByName("indexes")
	if err != nil {
		t.Fatalf("failed to get keyspace by name %v", err)
	}

	// Expect count of 2 namespaces for the namespaces keyspace
	pb_c, err := pb.Count()
	if err != nil || pb_c != 2 {
		t.Fatalf("failed to get expected namespaces keyspace count %v", err)
	}

	// Expect count of 10 for the keyspaces keyspace
	bb_c, err := bb.Count()
	if err != nil || bb_c != 10 {
		t.Fatalf("failed to get expected keyspaces keyspace count %v", err)
	}

	// Expect count of 10 for the indexes keyspace (all the primary indexes)
	ib_c, err := ib.Count()
	if err != nil || ib_c != 10 {
		t.Fatalf("failed to get expected indexes keyspace count %v", err)
	}

	// Scan all Primary Index entries of the keyspaces keyspace
	bb_e, err := doPrimaryIndexScan(t, bb)

	// Check for expected and unexpected names:
	if !bb_e["p0/b1"] {
		t.Fatalf("failed to get expected keyspace name from index scan: p0/b1")
	}

	if bb_e["not a name"] {
		t.Fatalf("found unexpected name in index scan")
	}

	// Scan all Primary Index entries of the indexes keyspace
	ib_e, err := doPrimaryIndexScan(t, ib)

	// Check for expected and unexpected names:
	if !ib_e["p1/b4/#primary"] {
		t.Fatalf("failed to get expected keyspace name from index scan: p1/b4/#primary")
	}

	if ib_e["p0/b4"] {
		t.Fatalf("found unexpected name in index scan")
	}

	// Fetch on the keyspaces keyspace - expect to find a value for this key:
	vals, errs := bb.Fetch([]string{"p0/b1"})
	if errs != nil {
		t.Fatalf("errors in key fetch %v", errs)
	}

	if vals == nil || (len(vals) == 1 && vals[0].Value == nil) {
		t.Fatalf("failed to fetch expected key from keyspaces keyspace")
	}

	// Fetch on the indexes keyspace - expect to find a value for this key:
	vals, errs = ib.Fetch([]string{"p0/b1/#primary"})
	if errs != nil {
		t.Fatalf("errors in key fetch %v", errs)
	}

	if vals == nil || (len(vals) == 1 && vals[0].Value == nil) {
		t.Fatalf("failed to fetch expected key from indexes keyspace")
	}

	// Fetch on the keyspaces keyspace - expect to not find a value for this key:
	vals, errs = bb.Fetch([]string{"p0/b5"})
	if errs == nil {
		t.Fatalf("Expected not found error for key fetch on %s", "p0/b5")
	}

	if vals == nil || (len(vals) == 1 && vals[0].Value != nil) {
		t.Fatalf("Found unexpected key in keyspaces keyspace")
	}

}
Exemplo n.º 4
0
func TestZKClustering(t *testing.T) {
	cs, err := NewConfigstore("localhost:2181")
	ds, err := mock.NewDatastore("mock:")
	as, err := accounting_stub.NewAccountingStore("stub:")
	version := clustering.NewVersion("0.7.0")
	version2 := clustering.NewVersion("0.7.9")
	stdCfg := clustering.NewStandalone(version, cs, ds, as)
	stdCfg2 := clustering.NewStandalone(version2, cs, ds, as)
	stdOpts := clustering.NewOptions(ds.URL(), cs.URL(), as.URL(), "default", false, false, true,
		runtime.NumCPU()<<16, runtime.NumCPU()<<6, 0, 0, ":8093", ":8094", "", false, "cluster1", "", "")

	if !zookeeper_running() {
		t.Skip("Zookeeper not running - skipping test")
	}

	if err != nil {
		t.Fatalf("Error creating configstore: ", err)
	}
	fmt.Printf("Created config store %v\n\n", cs)

	cfm := cs.ConfigurationManager()

	cluster1, _ := NewCluster("cluster1", version, cs, ds, as)

	fmt.Printf("Creating cluster %v\n\n", cluster1)

	cluster1, err = cfm.AddCluster(cluster1)

	if err != nil {
		t.Fatalf("Error adding cluster: ", err)
	}

	cluster1check, errCheck := cs.ClusterByName("cluster1")
	if errCheck != nil {
		t.Fatalf("Unexpected Error retrieving cluster by name: ", errCheck)
	}

	fmt.Printf("Retrieved cluster: %v\n\n", cluster1check)

	qn, err_qn := cluster1check.QueryNodeByName("qryNode")
	if err_qn == nil {
		t.Fatalf("Expected error getting query node: ", qn)
	}
	if qn != nil {
		t.Fatalf("Unexpected query node! ", qn)
	}

	clusterMgr := cluster1check.ClusterManager()

	queryNode, _ := NewQueryNode(":8093", stdCfg, stdOpts)
	queryNode, err_qn = clusterMgr.AddQueryNode(queryNode)
	if err_qn != nil {
		t.Fatalf("Unexpected error adding query node: ", err_qn)
	}
	fmt.Printf("Added query node %v to cluster %v\n\n", queryNode, cluster1)

	queryNode, _ = NewQueryNode(":8094", stdCfg, stdOpts)
	queryNode, err_qn = clusterMgr.AddQueryNode(queryNode)
	if err_qn != nil {
		t.Fatalf("Unexpected error adding query node: ", err_qn)
	}
	fmt.Printf("Added query node %v to cluster %v\n\n", queryNode, cluster1)

	queryNode, _ = NewQueryNode(":8095", stdCfg, stdOpts)
	queryNode, err_qn = clusterMgr.AddQueryNode(queryNode)
	if err_qn != nil {
		t.Fatalf("Unexpected error adding query node: ", err_qn)
	}
	fmt.Printf("Added query node %v to cluster %v\n\n", queryNode, cluster1)

	queryNode, _ = NewQueryNode(":8095", stdCfg2, stdOpts)
	queryNode, err_qn = clusterMgr.AddQueryNode(queryNode)
	if err_qn == nil {
		t.Fatalf("Expected error adding query node: version incompatibility")
	}
	fmt.Printf("Version incompatibility adding query node 4: %v\n\n", err_qn)

	qryNodes, errT := clusterMgr.GetQueryNodes()
	if errT != nil {
		t.Fatalf("Unexpected error getting query nodes: ", errT)
	}
	for i, qNode := range qryNodes {
		fmt.Printf("Query Node %d: configuration = %v\n\n", i, qNode)
	}

	clusters, errC := cfm.GetClusters()
	if errC != nil {
		t.Fatalf("Unexpected error getting clusters: ", errC)
	}
	for c, cluster := range clusters {
		fmt.Printf("Cluster %d: configuration = %v\n\n", c, cluster)
	}

	for _, qNode := range qryNodes {
		qNode, errT = clusterMgr.RemoveQueryNodeByName(qNode.Name())
		if errT != nil {
			t.Fatalf("Unexpected error removing query node: ", errT)
		}
	}
	r, err := cfm.RemoveClusterByName(cluster1check.Name())
	if err != nil {
		t.Fatalf("Unexpected error removing cluster: ", err)
	}
	if r {
		fmt.Printf("Successfully removed cluster \n\n", cluster1)
	}

}