Exemplo n.º 1
0
func TestClient(t *testing.T) {
	t.Skipf("Test cluster is not set up properly")
	zookeeper.EnsureZkFatjar()
	basePath := ""
	tc, err := zklib.StartTestCluster(1, nil, nil)
	if err != nil {
		t.Fatalf("could not start test zk cluster: %s", err)
	}
	defer os.RemoveAll(tc.Path)
	defer tc.Stop()
	time.Sleep(time.Second)

	servers := []string{fmt.Sprintf("127.0.0.1:%d", tc.Servers[0].Port)}

	dsnBytes, err := json.Marshal(zookeeper.DSN{Servers: servers, Timeout: time.Second * 15})
	if err != nil {
		t.Fatal("unexpected error creating zk DSN: %s", err)
	}
	dsn := string(dsnBytes)
	zClient, err := client.New("zookeeper", dsn, basePath, nil)

	zzk.InitializeLocalClient(zClient)

	conn, err := zzk.GetLocalConnection("/")
	if err != nil {
		t.Fatal("unexpected error getting connection")
	}

	h := host.New()
	h.ID = "nodeID"
	h.IPAddr = "192.168.1.5"
	h.PoolID = "default1"
	defer func(old func(string, os.FileMode) error) {
		mkdirAll = old
	}(mkdirAll)
	dir, err := ioutil.TempDir("", "serviced_var_")
	if err != nil {
		t.Fatalf("could not create tempdir: %s", err)
	}
	defer os.RemoveAll(dir)
	c, err := NewClient(h, dir)
	if err != nil {
		t.Fatalf("unexpected error creating client: %s", err)
	}
	defer c.Close()
	time.Sleep(time.Second * 5)

	// therefore, we need to check that the client was added under the pool from root
	nodePath := fmt.Sprintf("/storage/clients/%s", h.IPAddr)
	glog.Infof("about to check for %s", nodePath)
	if exists, err := conn.Exists(nodePath); err != nil {
		t.Fatalf("did not expect error checking for existence of %s: %s", nodePath, err)
	} else {
		if !exists {
			t.Fatalf("could not find %s", nodePath)
		}
	}
}
Exemplo n.º 2
0
func TestLock(t *testing.T) {

	/* start the cluster */
	tc, err := zklib.StartTestCluster(1)
	if err != nil {
		t.Fatalf("could not start test zk cluster: %s", err)
	}
	defer os.RemoveAll(tc.Path)
	defer tc.Stop()
	time.Sleep(time.Second)

	servers := []string{fmt.Sprintf("127.0.0.1:%d", tc.Servers[0].Port)}

	// setup the driver
	drv := Driver{}
	dsnBytes, err := json.Marshal(DSN{Servers: servers, Timeout: time.Second * 15})
	if err != nil {
		t.Fatalf("unexpected error creating zk DSN: %s", err)
	}
	dsn := string(dsnBytes)

	// create a connection
	conn, err := drv.GetConnection(dsn, "/test/basePath")
	if err != nil {
		t.Fatal("unexpected error getting connection")
	}

	// create  a lock & lock it
	lock := conn.NewLock("/foo/bar")
	if err = lock.Lock(); err != nil {
		t.Fatalf("unexpected error aquiring lock: %s", err)
	}

	// create a second lock and test that a locking attempt blocks
	lock2 := conn.NewLock("/foo/bar")
	lock2Response := make(chan error)
	go func() {
		lock2Response <- lock2.Lock()
	}()
	select {
	case response := <-lock2Response:
		t.Fatalf("Expected second lock to block, got %s", response)
	case <-time.After(time.Second):
		t.Log("good, lock2 failed to lock.")
	}

	// free the first lock, and test if the second lock unblocks
	if err = lock.Unlock(); err != nil {
		t.Fatalf("unexpected error releasing lock: %s", err)
	}
	select {
	case response := <-lock2Response:
		if response != nil {
			t.Fatalf("Did not expect error when attempting second lock!")
		}
	case <-time.After(time.Second * 3):
		t.Fatal("timeout on second lock")
	}

	// check if the second lock cleans up
	if err = lock2.Unlock(); err != nil {
		t.Fatalf("unexpected error releasing lock: %s", err)
	}

}
Exemplo n.º 3
0
func TestLeader(t *testing.T) {

	/* start the cluster */
	tc, err := zklib.StartTestCluster(1)
	if err != nil {
		t.Fatalf("could not start test zk cluster: %s", err)
	}
	defer os.RemoveAll(tc.Path)
	defer tc.Stop()
	time.Sleep(time.Second)

	servers := []string{fmt.Sprintf("127.0.0.1:%d", tc.Servers[0].Port)}

	// setup the driver
	drv := Driver{}
	dsnBytes, err := json.Marshal(DSN{Servers: servers, Timeout: time.Second * 15})
	if err != nil {
		t.Fatalf("unexpected error creating zk DSN: %s", err)
	}
	dsn := string(dsnBytes)

	// create a connection
	conn, err := drv.GetConnection(dsn, "/bossPath")
	if err != nil {
		t.Fatal("unexpected error getting connection")
	}

	// create  a leader and TakeLead
	leader1Node := &testNodeT{
		Name: "leader1",
	}
	leader1 := conn.NewLeader("/like/a/boss", leader1Node)
	_, err = leader1.TakeLead()
	if err != nil {
		t.Fatalf("could not take lead! %s", err)
	}

	leader2Node := &testNodeT{
		Name: "leader2",
	}
	leader2 := conn.NewLeader("/like/a/boss", leader2Node)
	leader2Response := make(chan error)
	go func() {
		_, err := leader2.TakeLead()
		leader2Response <- err
	}()

	select {
	case err = <-leader2Response:
		t.Fatalf("expected leader2 to block!: %s", err)
	case <-time.After(time.Second):
	}

	currentLeaderNode := &testNodeT{
		Name: "",
	}
	// get current Leader
	currentLeader := conn.NewLeader("/like/a/boss", nil)
	err = currentLeader.Current(currentLeaderNode)
	if err != nil {
		t.Fatalf("unexpected error getting current leader:%s", err)
	}

	if currentLeaderNode.Name != leader1Node.Name {
		t.Fatalf("expected leader %s , got %s", currentLeaderNode.Name, leader1Node.Name)
	}

	// let the first leader go
	err = leader1.ReleaseLead()
	if err != nil {
		t.Fatal("unexpected error releasing leader1 ")
	}

	select {
	case err = <-leader2Response:
		if err != nil {
			t.Fatalf("unexpected error when leader 1 was release and waiting on leader2: %s", err)

		}
	case <-time.After(time.Second * 3):
		t.Fatalf("expected leader2 to take over but we blocked")
	}

}
Exemplo n.º 4
0
func TestServer(t *testing.T) {
	t.Skip() // the zookeeper part doesnt work in this test, but does work in real life
	zookeeper.EnsureZkFatjar()
	basePath := ""
	tc, err := zklib.StartTestCluster(1, nil, nil)
	if err != nil {
		t.Fatalf("could not start test zk cluster: %s", err)
	}
	defer os.RemoveAll(tc.Path)
	defer tc.Stop()
	time.Sleep(time.Second)

	servers := []string{fmt.Sprintf("127.0.0.1:%d", tc.Servers[0].Port)}

	dsnBytes, err := json.Marshal(zookeeper.DSN{Servers: servers, Timeout: time.Second * 15})
	if err != nil {
		t.Fatal("unexpected error creating zk DSN: %s", err)
	}
	dsn := string(dsnBytes)

	zClient, err := client.New("zookeeper", dsn, basePath, nil)
	if err != nil {
		t.Fatal("unexpected error getting zk client")
	}
	zzk.InitializeLocalClient(zClient)

	defer func(orig func(nfs.Driver, string, string) error) {
		nfsMount = orig
	}(nfsMount)

	var local, remote string
	nfsMount = func(driver nfs.Driver, a, b string) error {
		glog.Infof("client is mounting %s to %s", a, b)
		remote = a
		local = b
		return nil
	}

	// creating a UUID in order to make a unique poolID
	// the poolID is somehow being saved on the filesystem (zookeeper config somewhere?)
	// making the poolID unique on every run will ensure it is stateless
	uuid, err := utils.NewUUID()
	if err != nil {
		t.Fatal("New UUID could not be created")
	}

	hostServer := host.New()
	hostServer.ID = "nodeID"
	hostServer.IPAddr = "192.168.1.50"
	hostServer.PoolID = uuid

	hostClient1 := host.New()
	hostClient1.ID = "nodeID_client1"
	hostClient1.IPAddr = "192.168.1.100"
	hostClient1.PoolID = uuid

	mockNfsDriver := &mockNfsDriverT{
		exportPath: "/exports",
		exportName: "serviced_var",
	}

	// TODO: this gets stuck at server.go:90 call to conn.CreateDir hangs
	s, err := NewServer(mockNfsDriver, hostServer, path.Join(mockNfsDriver.exportPath, mockNfsDriver.exportName))
	if err != nil {
		t.Fatalf("unexpected error creating Server: %s", err)
	}

	conn, err := zzk.GetLocalConnection("/")
	if err != nil {
		t.Fatalf("unexpected error getting connection: %s", err)
	}
	shutdown := make(chan interface{})
	defer close(shutdown)
	go s.Run(shutdown, conn)

	// give it some time
	time.Sleep(time.Second * 5)

	if !mockNfsDriver.syncCalled {
		t.Fatalf("sync() should have been called by now")
	}
	if len(mockNfsDriver.clients) != 0 {
		t.Fatalf("Expected number of clients: 0 --- Found: %v (%v)", len(mockNfsDriver.clients), mockNfsDriver.clients)
	}
	mockNfsDriver.syncCalled = false
	tmpVar, err := ioutil.TempDir("", "serviced_var")
	if err != nil {
		t.Fatalf("could not create tempdir: %s", err)
	}
	defer os.RemoveAll(tmpVar)
	c1, err := NewClient(hostClient1, tmpVar)
	if err != nil {
		t.Fatalf("could not create client: %s", err)
	}
	// give it some time
	time.Sleep(time.Second * 2)
	if !mockNfsDriver.syncCalled {
		t.Fatalf("sync() should have been called by now")
	}

	if len(mockNfsDriver.clients) != 1 {
		t.Fatalf("expecting 1 client, got %d", len(mockNfsDriver.clients))
	}
	if mockNfsDriver.clients[0] != hostClient1.IPAddr {
		t.Fatalf("expecting '%s', got '%s'", hostServer.IPAddr, mockNfsDriver.clients[0])
	}

	shareName := fmt.Sprintf("%s:%s", hostServer.IPAddr, mockNfsDriver.ExportPath())
	if remote != shareName {
		t.Fatalf("remote should be %s, not %s", remote, shareName)
	}

	glog.Info("about to call c1.Close()")
	c1.Close()
}
Exemplo n.º 5
0
func TestZkDriver(t *testing.T) {
	basePath := "/basePath"
	tc, err := zklib.StartTestCluster(1)
	if err != nil {
		t.Fatalf("could not start test zk cluster: %s", err)
	}
	defer os.RemoveAll(tc.Path)
	defer tc.Stop()
	time.Sleep(time.Second)

	servers := []string{fmt.Sprintf("127.0.0.1:%d", tc.Servers[0].Port)}

	drv := Driver{}
	dsnBytes, err := json.Marshal(DSN{Servers: servers, Timeout: time.Second * 15})
	if err != nil {
		t.Fatalf("unexpected error creating zk DSN: %s", err)
	}
	dsn := string(dsnBytes)

	conn, err := drv.GetConnection(dsn, basePath)
	if err != nil {
		t.Fatal("unexpected error getting connection")
	}
	exists, err := conn.Exists("/foo")
	if err != nil {
		t.Fatalf("err calling exists: %s", err)
	}
	if exists {
		t.Fatal("foo should not exist")
	}

	err = conn.Delete("/foo")
	if err == nil {
		t.Fatalf("delete on non-existent object should fail")
	}

	err = conn.CreateDir("/foo")
	if err != nil {
		t.Fatalf("creating /foo should work: %s", err)
	}

	testNode := &testNodeT{
		Name: "test",
	}
	err = conn.Create("/foo/bar", testNode)
	if err != nil {
		t.Fatalf("creating /foo/bar should work: %s", err)
	}
	t.Logf("testNode version: %v", testNode.Version())

	exists, err = conn.Exists("/foo/bar")
	if err != nil {
		t.Fatalf("could not call exists: %s", err)
	}

	if !exists {
		t.Fatal("/foo/bar should  exist")
	}

	testNode2 := &testNodeT{
		Name: "baz",
	}
	err = conn.Get("/foo/bar", testNode2)
	if err != nil {
		t.Fatalf("could not get /foo/bar node: %s", err)
	}

	if testNode.Name != testNode2.Name {
		t.Fatalf("expected testNodes to match %s  --- %s", testNode.Name, testNode2.Name)
	}

	err = conn.Get("/foo/bar", testNode2)
	t.Logf("testNode version: %v", testNode2.Version().(*zklib.Stat).Version)
	testNode2.Name = "abc"
	if err := conn.Set("/foo/bar", testNode2); err != nil {
		t.Fatalf("Could not update testNode: %s", err)
	}

	err = conn.Delete("/foo")
	if err != nil {
		t.Fatalf("delete of /foo should work: %s", err)
	}

	conn.Close()
}
Exemplo n.º 6
0
func TestZkDriver_Watch(t *testing.T) {
	basePath := "/basePath"
	tc, err := zklib.StartTestCluster(1)
	if err != nil {
		t.Fatalf("could not start test zk cluster: %s", err)
	}
	defer os.RemoveAll(tc.Path)
	defer tc.Stop()
	time.Sleep(time.Second)

	servers := []string{fmt.Sprintf("127.0.0.1:%d", tc.Servers[0].Port)}

	drv := Driver{}
	dsnBytes, err := json.Marshal(DSN{Servers: servers, Timeout: time.Second * 15})
	if err != nil {
		t.Fatalf("unexpected error creating zk DSN: %s", err)
	}
	dsn := string(dsnBytes)

	conn, err := drv.GetConnection(dsn, basePath)
	if err != nil {
		t.Fatal("unexpected error getting connection")
	}

	err = conn.CreateDir("/foo")
	if err != nil {
		t.Fatalf("creating /foo should work: %s", err)
	}

	_, w1, err := conn.ChildrenW("/foo")
	if err != nil {
		t.Fatalf("should be able to acquire watch for /foo: %s", err)
	}

	_, w2, err := conn.ChildrenW("/foo")
	if err != nil {
		t.Fatalf("should be able to acquire watch for /foo: %s", err)
	}

	go func() {
		for w1 != nil || w2 != nil {
			select {
			case e := <-w1:
				if e.Type != coordclient.EventNodeChildrenChanged {
					t.Errorf("expected %v; actual: %v (w1)", coordclient.EventNodeChildrenChanged, e.Type)
				}
				w1 = nil
			case e := <-w2:
				if e.Type != coordclient.EventNodeChildrenChanged {
					t.Errorf("expected %v; actual: %v (w2)", coordclient.EventNodeChildrenChanged, e.Type)
				}
				w2 = nil
			}
		}
	}()

	<-time.After(time.Second)
	testNode := &testNodeT{
		Name: "test",
	}
	err = conn.Create("/foo/bar", testNode)
	if err != nil {
		t.Fatalf("creating /foo/bar should work: %s", err)
	}
	t.Logf("testNode version: %v", testNode.Version())
}
Exemplo n.º 7
0
func TestZkDriver_Ephemeral(t *testing.T) {
	basePath := "/basePath"
	tc, err := zklib.StartTestCluster(1)
	if err != nil {
		t.Fatalf("could not start test zk cluster: %s", err)
	}
	defer os.RemoveAll(tc.Path)
	defer tc.Stop()
	time.Sleep(time.Second)

	servers := []string{fmt.Sprintf("127.0.0.1:%d", tc.Servers[0].Port)}

	drv := Driver{}
	dsnBytes, err := json.Marshal(DSN{Servers: servers, Timeout: time.Second * 15})
	if err != nil {
		t.Fatalf("unexpected error creating zk DSN: %s", err)
	}

	dsn := string(dsnBytes)

	conn, err := drv.GetConnection(dsn, basePath)
	defer conn.Close()
	if err != nil {
		t.Fatal("unexpected error getting connection")
	}

	node := &testNodeT{Name: "ephemeral"}
	epath, err := conn.CreateEphemeral("/ephemeral", node)
	if err != nil {
		t.Fatalf("creating /ephemeral should work: %s", err)
	}
	// The returned is from the root, so it has to be trimmed down to the
	// relative location
	ename := "/" + path.Base(epath)

	if ok, err := conn.Exists(ename); err != nil {
		t.Fatalf("could not find path to ephemeral %s: %s", ename, err)
	} else if !ok {
		t.Fatalf("ephemeral %s not created", ename)
	}

	// Close connection and verify the node was deleted
	conn.Close()
	conn, err = drv.GetConnection(dsn, basePath)

	if err != nil {
		t.Fatal("unexpected error getting connection")
	}

	if ok, err := conn.Exists(ename); err != nil && err != coordclient.ErrNoNode {
		t.Fatalf("should be able to check path %s: %s", ename, err)
	} else if ok {
		t.Errorf("ephemeral %s should have been deleted", ename)
	}

	// Adding and deleting
	node = &testNodeT{Name: "ephemeral"}
	epath, err = conn.CreateEphemeral("/ephemeral", node)
	if err != nil {
		t.Fatalf("creating /ephemeral should work: %s", err)
	}
	ename = "/" + path.Base(epath)

	if ok, err := conn.Exists(ename); err != nil {
		t.Fatalf("could not find path to ephemeral %s: %s", ename, err)
	} else if !ok {
		t.Fatalf("ephemeral %s not created", ename)
	}
	if err := conn.Delete(ename); err != nil {
		t.Fatalf("could not delete path %s to ephemeral: %s", err)
	}

	if ok, err := conn.Exists(ename); err != nil && err != coordclient.ErrNoNode {
		t.Fatalf("should be able to check path %s: %s", ename, err)
	} else if ok {
		t.Errorf("ephemeral %s should have been deleted", ename)
	}
}