예제 #1
0
// TestV2RetryRefuse tests destructive api calls will retry if a connection is refused.
func TestV2RetryRefuse(t *testing.T) {
	defer testutil.AfterTest(t)
	cl := integration.NewCluster(t, 1)
	cl.Launch(t)
	defer cl.Terminate(t)
	// test connection refused; expect no error failover
	cli := integration.MustNewHTTPClient(t, []string{integration.UrlScheme + "://refuseconn:123", cl.URL(0)}, nil)
	kapi := client.NewKeysAPI(cli)
	if _, err := kapi.Set(context.Background(), "/delkey", "def", nil); err != nil {
		t.Fatal(err)
	}
	for i, f := range noRetryList(kapi) {
		if err := f(); err != nil {
			t.Errorf("#%d: unexpected retry failure (%v)", i, err)
		}
	}
}
예제 #2
0
func TestRegistrar(t *testing.T) {
	ts := etcdi.NewCluster(t, 1)
	ts.Launch(t)
	kitClient, err := NewClient(context.Background(), []string{ts.URL(0)}, kitClientOptions)

	// Valid registrar should pass
	registrar := NewRegistrar(kitClient, Service{
		Key:   "somekey",
		Value: "somevalue",
		DeleteOptions: &etcdc.DeleteOptions{
			PrevValue: "",
			PrevIndex: 0,
			Recursive: true,
			Dir:       false,
		},
	}, log.NewNopLogger())

	registrar.Register()
	r1, err := kitClient.GetEntries(registrar.service.Key)
	if err != nil {
		t.Fatalf("unexpected error when getting value for deregistered key: %v", err)
	}

	if want, have := registrar.service.Value, r1[0]; want != have {
		t.Fatalf("want %q, have %q", want, have)
	}

	registrar.Deregister()
	r2, err := kitClient.GetEntries(registrar.service.Key)
	if len(r2) > 0 {
		t.Fatalf("unexpected value found for deregistered key: %s", r2)
	}

	// Registrar with no key should register but value will be blank
	registrarNoKey := NewRegistrar(kitClient, Service{
		Key:   "",
		Value: "somevalue",
		DeleteOptions: &etcdc.DeleteOptions{
			PrevValue: "",
			PrevIndex: 0,
			Recursive: true,
			Dir:       false,
		},
	}, log.NewNopLogger())

	registrarNoKey.Register()
	r3, err := kitClient.GetEntries(registrarNoKey.service.Key)
	if err != nil {
		t.Errorf("unexpected error when getting value for entry with no key: %v", err)
	}

	if want, have := "", r3[0]; want != have {
		t.Fatalf("want %q, have %q", want, have)
	}

	// Registrar with no value should not register anything
	registrarNoValue := NewRegistrar(kitClient, Service{
		Key:   "somekey",
		Value: "",
		DeleteOptions: &etcdc.DeleteOptions{
			PrevValue: "",
			PrevIndex: 0,
			Recursive: true,
			Dir:       false,
		},
	}, log.NewNopLogger())

	registrarNoValue.Register()
	r4, err := kitClient.GetEntries(registrarNoValue.service.Key)
	if err == nil {
		t.Errorf("expected error when getting value for entry key which attempted to register with no value")
	}

	if len(r4) > 0 {
		t.Fatalf("unexpected value retreived when getting value for entry with no value")
	}

	ts.Terminate(t)
}