github.com/macb/etcd@v0.3.1-0.20140227003422-a60481c6b1a0/tests/functional/v1_migration_test.go (about)

     1  package test
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  	"os/exec"
     9  	"path/filepath"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/coreos/etcd/tests"
    14  	"github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert"
    15  )
    16  
    17  // Ensure that we can start a v2 node from the log of a v1 node.
    18  func TestV1SoloMigration(t *testing.T) {
    19  	path, _ := ioutil.TempDir("", "etcd-")
    20  	os.MkdirAll(path, 0777)
    21  	defer os.RemoveAll(path)
    22  
    23  	nodepath := filepath.Join(path, "node0")
    24  	fixturepath, _ := filepath.Abs("../fixtures/v1.solo/node0")
    25  	fmt.Println("DATA_DIR =", nodepath)
    26  
    27  	// Copy over fixture files.
    28  	c := exec.Command("cp", "-rf", fixturepath, nodepath)
    29  	if out, err := c.CombinedOutput(); err != nil {
    30  		fmt.Println(">>>>>>\n", string(out), "<<<<<<")
    31  		panic("Fixture initialization error:" + err.Error())
    32  	}
    33  
    34  	procAttr := new(os.ProcAttr)
    35  	procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
    36  
    37  	args := []string{"etcd", fmt.Sprintf("-data-dir=%s", nodepath)}
    38  	args = append(args, "-addr", "127.0.0.1:4001")
    39  	args = append(args, "-peer-addr", "127.0.0.1:7001")
    40  	args = append(args, "-name", "v1")
    41  	process, err := os.StartProcess(EtcdBinPath, args, procAttr)
    42  	if err != nil {
    43  		t.Fatal("start process failed:" + err.Error())
    44  		return
    45  	}
    46  	defer process.Kill()
    47  	time.Sleep(time.Second)
    48  
    49  	// Ensure deleted message is removed.
    50  	resp, err := tests.Get("http://localhost:4001/v2/keys/message")
    51  	tests.ReadBody(resp)
    52  	assert.Nil(t, err, "")
    53  	assert.Equal(t, resp.StatusCode, 200, "")
    54  }
    55  
    56  // Ensure that we can start a v2 cluster from the logs of a v1 cluster.
    57  func TestV1ClusterMigration(t *testing.T) {
    58  	path, _ := ioutil.TempDir("", "etcd-")
    59  	os.RemoveAll(path)
    60  	defer os.RemoveAll(path)
    61  
    62  	nodes := []string{"node0", "node2"}
    63  	for i, node := range nodes {
    64  		nodepath := filepath.Join(path, node)
    65  		fixturepath, _ := filepath.Abs(filepath.Join("../fixtures/v1.cluster/", node))
    66  		fmt.Println("FIXPATH  =", fixturepath)
    67  		fmt.Println("NODEPATH =", nodepath)
    68  		os.MkdirAll(filepath.Dir(nodepath), 0777)
    69  
    70  		// Copy over fixture files.
    71  		c := exec.Command("cp", "-rf", fixturepath, nodepath)
    72  		if out, err := c.CombinedOutput(); err != nil {
    73  			fmt.Println(">>>>>>\n", string(out), "<<<<<<")
    74  			panic("Fixture initialization error:" + err.Error())
    75  		}
    76  
    77  		procAttr := new(os.ProcAttr)
    78  		procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
    79  
    80  		args := []string{"etcd", fmt.Sprintf("-data-dir=%s", nodepath)}
    81  		args = append(args, "-addr", fmt.Sprintf("127.0.0.1:%d", 4001+i))
    82  		args = append(args, "-peer-addr", fmt.Sprintf("127.0.0.1:%d", 7001+i))
    83  		args = append(args, "-name", node)
    84  		process, err := os.StartProcess(EtcdBinPath, args, procAttr)
    85  		if err != nil {
    86  			t.Fatal("start process failed:" + err.Error())
    87  			return
    88  		}
    89  		defer process.Kill()
    90  		time.Sleep(time.Second)
    91  	}
    92  
    93  	// Ensure deleted message is removed.
    94  	resp, err := tests.Get("http://localhost:4001/v2/keys/message")
    95  	body := tests.ReadBody(resp)
    96  	assert.Nil(t, err, "")
    97  	assert.Equal(t, resp.StatusCode, http.StatusNotFound)
    98  	assert.Equal(t, string(body), `{"errorCode":100,"message":"Key not found","cause":"/message","index":11}`+"\n")
    99  
   100  	// Ensure TTL'd message is removed.
   101  	resp, err = tests.Get("http://localhost:4001/v2/keys/foo")
   102  	body = tests.ReadBody(resp)
   103  	assert.Nil(t, err, "")
   104  	assert.Equal(t, resp.StatusCode, 200, "")
   105  	assert.Equal(t, string(body), `{"action":"get","node":{"key":"/foo","value":"one","modifiedIndex":9,"createdIndex":9}}`)
   106  }