File: client_test.go

package info (click to toggle)
golang-etcd 0.2.0~rc1%2Bgit20140717-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-backports, jessie-kfreebsd
  • size: 188 kB
  • ctags: 165
  • sloc: makefile: 5
file content (96 lines) | stat: -rw-r--r-- 1,777 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package etcd

import (
	"encoding/json"
	"fmt"
	"net"
	"net/url"
	"os"
	"testing"
)

// To pass this test, we need to create a cluster of 3 machines
// The server should be listening on 127.0.0.1:4001, 4002, 4003
func TestSync(t *testing.T) {
	fmt.Println("Make sure there are three nodes at 0.0.0.0:4001-4003")

	// Explicit trailing slash to ensure this doesn't reproduce:
	// https://github.com/coreos/go-etcd/issues/82
	c := NewClient([]string{"http://127.0.0.1:4001/"})

	success := c.SyncCluster()
	if !success {
		t.Fatal("cannot sync machines")
	}

	for _, m := range c.GetCluster() {
		u, err := url.Parse(m)
		if err != nil {
			t.Fatal(err)
		}
		if u.Scheme != "http" {
			t.Fatal("scheme must be http")
		}

		host, _, err := net.SplitHostPort(u.Host)
		if err != nil {
			t.Fatal(err)
		}
		if host != "127.0.0.1" {
			t.Fatal("Host must be 127.0.0.1")
		}
	}

	badMachines := []string{"abc", "edef"}

	success = c.SetCluster(badMachines)

	if success {
		t.Fatal("should not sync on bad machines")
	}

	goodMachines := []string{"127.0.0.1:4002"}

	success = c.SetCluster(goodMachines)

	if !success {
		t.Fatal("cannot sync machines")
	} else {
		fmt.Println(c.cluster.Machines)
	}

}

func TestPersistence(t *testing.T) {
	c := NewClient(nil)
	c.SyncCluster()

	fo, err := os.Create("config.json")
	if err != nil {
		t.Fatal(err)
	}
	defer func() {
		if err := fo.Close(); err != nil {
			panic(err)
		}
	}()

	c.SetPersistence(fo)
	err = c.saveConfig()
	if err != nil {
		t.Fatal(err)
	}

	c2, err := NewClientFromFile("config.json")
	if err != nil {
		t.Fatal(err)
	}

	// Verify that the two clients have the same config
	b1, _ := json.Marshal(c)
	b2, _ := json.Marshal(c2)

	if string(b1) != string(b2) {
		t.Fatalf("The two configs should be equal!")
	}
}