From a033686acfd332ea716eeac14e119b5cdfb034d9 Mon Sep 17 00:00:00 2001 From: cfc4n Date: Sat, 2 Feb 2019 14:06:54 +0800 Subject: [PATCH] clientv3/integration: return err if err == rpctypes.ErrAuthNotEnable --- clientv3/client.go | 4 +++ clientv3/integration/user_test.go | 42 +++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/clientv3/client.go b/clientv3/client.go index 3cc770e08a6..276b0f04239 100644 --- a/clientv3/client.go +++ b/clientv3/client.go @@ -310,6 +310,10 @@ func (c *Client) getToken(ctx context.Context) error { var resp *AuthenticateResponse resp, err = auth.authenticate(ctx, c.Username, c.Password) if err != nil { + // return err without retrying other endpoints + if err == rpctypes.ErrAuthNotEnabled { + return err + } continue } diff --git a/clientv3/integration/user_test.go b/clientv3/integration/user_test.go index cf32fc2288c..cff6c58bf17 100644 --- a/clientv3/integration/user_test.go +++ b/clientv3/integration/user_test.go @@ -110,3 +110,45 @@ func authSetupRoot(t *testing.T, auth clientv3.Auth) { t.Fatal(err) } } + +func TestGetTokenWithoutAuth(t *testing.T) { + defer testutil.AfterTest(t) + + clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 10}) + defer clus.Terminate(t) + + authapi := clus.RandClient() + + var err error + var client *clientv3.Client + + // make sure "auth" was disabled + if _, err = authapi.AuthDisable(context.TODO()); err != nil { + t.Fatal(err) + } + + // "Username" and "Password" must be used + cfg := clientv3.Config{ + Endpoints: authapi.Endpoints(), + DialTimeout: 1 * time.Second, // make sure all connection time of connect all endpoint must be more DialTimeout + Username: "root", + Password: "123", + } + + client, err = clientv3.New(cfg) + if err == nil { + defer client.Close() + } + + switch err { + case nil: + t.Log("passes as expected, but may be connection time less than DialTimeout") + case context.DeadlineExceeded: + t.Errorf("not expected result:%v with endpoint:%s", err, authapi.Endpoints()) + case rpctypes.ErrAuthNotEnabled: + t.Logf("passes as expected:%v", err) + default: + t.Errorf("other errors:%v", err) + } + +}