Browse Source

clientv3/integration: return err if err == rpctypes.ErrAuthNotEnable

cfc4n 6 years ago
parent
commit
a033686acf
2 changed files with 46 additions and 0 deletions
  1. 4 0
      clientv3/client.go
  2. 42 0
      clientv3/integration/user_test.go

+ 4 - 0
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
 		}
 

+ 42 - 0
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)
+	}
+
+}