Exemple #1
0
// Ensures a unique value is added to the key's children.
//
//   $ curl -X POST localhost:4001/v2/keys/foo/bar
//   $ curl -X POST localhost:4001/v2/keys/foo/bar
//   $ curl -X POST localhost:4001/v2/keys/foo/baz
//
func TestV2CreateUnique(t *testing.T) {
	tests.RunServer(func(s *server.Server) {
		// POST should add index to list.
		fullURL := fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/bar")
		resp, _ := tests.PostForm(fullURL, nil)
		assert.Equal(t, resp.StatusCode, http.StatusCreated)
		body := tests.ReadBodyJSON(resp)
		assert.Equal(t, body["action"], "create", "")

		node := body["node"].(map[string]interface{})
		assert.Equal(t, node["key"], "/foo/bar/3", "")
		assert.Nil(t, node["dir"], "")
		assert.Equal(t, node["modifiedIndex"], 3, "")

		// Second POST should add next index to list.
		resp, _ = tests.PostForm(fullURL, nil)
		assert.Equal(t, resp.StatusCode, http.StatusCreated)
		body = tests.ReadBodyJSON(resp)

		node = body["node"].(map[string]interface{})
		assert.Equal(t, node["key"], "/foo/bar/4", "")

		// POST to a different key should add index to that list.
		resp, _ = tests.PostForm(fmt.Sprintf("%s%s", s.URL(), "/v2/keys/foo/baz"), nil)
		assert.Equal(t, resp.StatusCode, http.StatusCreated)
		body = tests.ReadBodyJSON(resp)

		node = body["node"].(map[string]interface{})
		assert.Equal(t, node["key"], "/foo/baz/5", "")
	})
}
Exemple #2
0
// Ensures a unique value is added to the key's children.
//
//   $ curl -X POST localhost:4001/v2/keys/foo/bar
//   $ curl -X POST localhost:4001/v2/keys/foo/bar
//   $ curl -X POST localhost:4001/v2/keys/foo/baz
//
func TestV2CreateUnique(t *testing.T) {
	tests.RunServer(func(s *server.Server) {
		// POST should add index to list.
		resp, _ := tests.PostForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), nil)
		body := tests.ReadBodyJSON(resp)
		assert.Equal(t, body["action"], "create", "")
		assert.Equal(t, body["key"], "/foo/bar/1", "")
		assert.Equal(t, body["dir"], true, "")
		assert.Equal(t, body["modifiedIndex"], 1, "")

		// Second POST should add next index to list.
		resp, _ = tests.PostForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/bar"), nil)
		body = tests.ReadBodyJSON(resp)
		assert.Equal(t, body["key"], "/foo/bar/2", "")

		// POST to a different key should add index to that list.
		resp, _ = tests.PostForm(fmt.Sprintf("http://%s%s", s.URL(), "/v2/keys/foo/baz"), nil)
		body = tests.ReadBodyJSON(resp)
		assert.Equal(t, body["key"], "/foo/baz/3", "")
	})
}
Exemple #3
0
// Ensures that a watcher can wait for a value to be set after a given index.
//
//   $ curl -X POST localhost:4001/v1/watch/foo/bar -d index=4
//   $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=XXX
//   $ curl -X PUT localhost:4001/v1/keys/foo/bar -d value=YYY
//
func TestV1WatchKeyWithIndex(t *testing.T) {
	tests.RunServer(func(s *server.Server) {
		var body map[string]interface{}
		c := make(chan bool)
		go func() {
			v := url.Values{}
			v.Set("index", "3")
			resp, _ := tests.PostForm(fmt.Sprintf("%s%s", s.URL(), "/v1/watch/foo/bar"), v)
			body = tests.ReadBodyJSON(resp)
			c <- true
		}()

		// Make sure response didn't fire early.
		time.Sleep(1 * time.Millisecond)
		assert.Nil(t, body, "")

		// Set a value (before given index).
		v := url.Values{}
		v.Set("value", "XXX")
		resp, _ := tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
		tests.ReadBody(resp)

		// Make sure response didn't fire early.
		time.Sleep(1 * time.Millisecond)
		assert.Nil(t, body, "")

		// Set a value (before given index).
		v.Set("value", "YYY")
		resp, _ = tests.PutForm(fmt.Sprintf("%s%s", s.URL(), "/v1/keys/foo/bar"), v)
		tests.ReadBody(resp)

		// A response should follow from the GET above.
		time.Sleep(1 * time.Millisecond)

		select {
		case <-c:

		default:
			t.Fatal("cannot get watch result")
		}

		assert.NotNil(t, body, "")
		assert.Equal(t, body["action"], "set", "")

		assert.Equal(t, body["key"], "/foo/bar", "")
		assert.Equal(t, body["value"], "YYY", "")
		assert.Equal(t, body["index"], 3, "")
	})
}
func testAcquireLock(s *server.Server, key string, value string, ttl int) (string, int, error) {
	resp, err := tests.PostForm(fmt.Sprintf("%s/mod/v2/lock/%s?value=%s&ttl=%d", s.URL(), key, value, ttl), nil)
	ret := tests.ReadBody(resp)
	return string(ret), resp.StatusCode, err
}
Exemple #5
0
func testAcquireLock(s *server.Server, key string, ttl int) (string, error) {
	resp, err := tests.PostForm(fmt.Sprintf("%s/mod/v2/lock/%s?ttl=%d", s.URL(), key, ttl), nil)
	ret := tests.ReadBody(resp)
	return string(ret), err
}