Skip to content

Commit

Permalink
tests: a test case for watch with auth token expiration
Browse files Browse the repository at this point in the history
Signed-off-by: Hitoshi Mitake <h.mitake@gmail.com>
  • Loading branch information
mitake committed Oct 4, 2022
1 parent 0c6e466 commit 9136517
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
8 changes: 7 additions & 1 deletion integration/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ type ClusterConfig struct {

DiscoveryURL string

AuthToken string
AuthToken string
AuthTokenTTL uint

UseGRPC bool

Expand Down Expand Up @@ -285,6 +286,7 @@ func (c *cluster) mustNewMember(t testing.TB) *member {
memberConfig{
name: c.name(rand.Int()),
authToken: c.cfg.AuthToken,
authTokenTTL: c.cfg.AuthTokenTTL,
peerTLS: c.cfg.PeerTLS,
clientTLS: c.cfg.ClientTLS,
quotaBackendBytes: c.cfg.QuotaBackendBytes,
Expand Down Expand Up @@ -579,6 +581,7 @@ type memberConfig struct {
peerTLS *transport.TLSInfo
clientTLS *transport.TLSInfo
authToken string
authTokenTTL uint
quotaBackendBytes int64
maxTxnOps uint
maxRequestBytes uint
Expand Down Expand Up @@ -665,6 +668,9 @@ func mustNewMember(t testing.TB, mcfg memberConfig) *member {
if mcfg.authToken != "" {
m.AuthToken = mcfg.authToken
}
if mcfg.authTokenTTL != 0 {
m.TokenTTL = mcfg.authTokenTTL
}

m.BcryptCost = uint(bcrypt.MinCost) // use min bcrypt cost to speedy up integration testing

Expand Down
33 changes: 33 additions & 0 deletions integration/v3_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,36 @@ func TestV3AuthRestartMember(t *testing.T) {
_, err = c2.Put(context.TODO(), "foo", "bar2")
testutil.AssertNil(t, err)
}

func TestV3AuthWatchAndTokenExpire(t *testing.T) {
defer testutil.AfterTest(t)
clus := NewClusterV3(t, &ClusterConfig{Size: 1, AuthTokenTTL: 3})
defer clus.Terminate(t)

ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()

authSetupRoot(t, toGRPC(clus.Client(0)).Auth)

c, cerr := clientv3.New(clientv3.Config{Endpoints: clus.Client(0).Endpoints(), Username: "root", Password: "123"})
if cerr != nil {
t.Fatal(cerr)
}
defer c.Close()

_, err := c.Put(ctx, "key", "val")
if err != nil {
t.Fatalf("Unexpected error from Put: %v", err)
}

// The first watch gets a valid auth token through watcher.newWatcherGrpcStream()
// We should discard the first one by waiting TTL after the first watch.
wChan := c.Watch(ctx, "key", clientv3.WithRev(1))
watchResponse := <-wChan

time.Sleep(5 * time.Second)

wChan = c.Watch(ctx, "key", clientv3.WithRev(1))
watchResponse = <-wChan
testutil.AssertNil(t, watchResponse.Err())
}

0 comments on commit 9136517

Please sign in to comment.