github.com/rochacon/deis@v1.0.2-0.20150903015341-6839b592a1ff/mesos/pkg/etcd/etcd_test.go (about)

     1  package etcd
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"os/exec"
     7  	"reflect"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func init() {
    13  	_, err := exec.Command("etcd", "--version").Output()
    14  	if err != nil {
    15  		log.Fatal(err)
    16  	}
    17  }
    18  
    19  var etcdServer *exec.Cmd
    20  
    21  func startEtcd() {
    22  	tmpDir, err := ioutil.TempDir(os.TempDir(), "etcd-test")
    23  	if err != nil {
    24  		log.Fatal("creating temp dir:", err)
    25  	}
    26  	log.Debugf("temp dir: %v", tmpDir)
    27  
    28  	etcdServer = exec.Command("etcd", "-data-dir="+tmpDir, "-name=default")
    29  	etcdServer.Start()
    30  	time.Sleep(1 * time.Second)
    31  }
    32  
    33  func stopEtcd() {
    34  	etcdServer.Process.Kill()
    35  }
    36  
    37  func TestGetSetEtcd(t *testing.T) {
    38  	startEtcd()
    39  	defer stopEtcd()
    40  
    41  	etcdClient := NewClient([]string{"http://localhost:4001"})
    42  	SetDefault(etcdClient, "/path", "value")
    43  	value := Get(etcdClient, "/path")
    44  
    45  	if value != "value" {
    46  		t.Fatalf("Expected '%v' but returned '%v'", "value", value)
    47  	}
    48  
    49  	Set(etcdClient, "/path", "", 0)
    50  	value = Get(etcdClient, "/path")
    51  
    52  	if value != "" {
    53  		t.Fatalf("Expected '%v' but returned '%v'", "", value)
    54  	}
    55  
    56  	Set(etcdClient, "/path", "value", uint64((1 * time.Second).Seconds()))
    57  	time.Sleep(2 * time.Second)
    58  	value = Get(etcdClient, "/path")
    59  
    60  	if value != "" {
    61  		t.Fatalf("Expected '%v' but returned '%v'", "", value)
    62  	}
    63  }
    64  
    65  func TestMkdirEtcd(t *testing.T) {
    66  	startEtcd()
    67  	defer stopEtcd()
    68  
    69  	etcdClient := NewClient([]string{"http://localhost:4001"})
    70  
    71  	Mkdir(etcdClient, "/directory")
    72  	values := GetList(etcdClient, "/directory")
    73  	if len(values) != 2 {
    74  		t.Fatalf("Expected '%v' but returned '%v'", 0, len(values))
    75  	}
    76  
    77  	Set(etcdClient, "/directory/item_1", "value", 0)
    78  	Set(etcdClient, "/directory/item_2", "value", 0)
    79  	values = GetList(etcdClient, "/directory")
    80  	if len(values) != 2 {
    81  		t.Fatalf("Expected '%v' but returned '%v'", 2, len(values))
    82  	}
    83  
    84  	lsResult := []string{"item_1", "item_2"}
    85  	if !reflect.DeepEqual(values, lsResult) {
    86  		t.Fatalf("Expected '%v'  but returned '%v'", lsResult, values)
    87  	}
    88  }
    89  
    90  func TestWaitForKeysEtcd(t *testing.T) {
    91  	startEtcd()
    92  	defer stopEtcd()
    93  
    94  	etcdClient := NewClient([]string{"http://localhost:4001"})
    95  	Set(etcdClient, "/key", "value", 0)
    96  	start := time.Now()
    97  	err := WaitForKeys(etcdClient, []string{"/key"}, (10 * time.Second))
    98  	if err != nil {
    99  		t.Fatalf("%v", err)
   100  	}
   101  	end := time.Since(start)
   102  	if end.Seconds() > (2 * time.Second).Seconds() {
   103  		t.Fatalf("Expected '%vs' but returned '%vs'", 2, end.Seconds())
   104  	}
   105  
   106  	err = WaitForKeys(etcdClient, []string{"/key2"}, (2 * time.Second))
   107  	if err == nil {
   108  		t.Fatalf("Expected an error")
   109  	}
   110  }