From ee329c0176bc94c657bf993974d5ff9adac1257b Mon Sep 17 00:00:00 2001 From: Sam Batschelet Date: Fri, 15 Dec 2017 17:31:55 -0500 Subject: [PATCH] e2e: reuse leaseTestTimeToLiveExpired logic --- e2e/ctl_v3_auth_test.go | 26 ++------------------------ e2e/ctl_v3_lease_test.go | 31 +++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/e2e/ctl_v3_auth_test.go b/e2e/ctl_v3_auth_test.go index 15f0c6bbc18a..d59461868feb 100644 --- a/e2e/ctl_v3_auth_test.go +++ b/e2e/ctl_v3_auth_test.go @@ -17,9 +17,7 @@ package e2e import ( "fmt" "os" - "strings" "testing" - "time" "github.com/coreos/etcd/clientv3" ) @@ -759,30 +757,10 @@ func authLeaseTestTimeToLiveExpired(cx ctlCtx) { cx.user, cx.pass = "root", "root" authSetupTestUser(cx) - leaseID, err := ctlV3LeaseGrant(cx, 3) - if err != nil { - cx.t.Fatal(err) - } - if err := ctlV3Put(cx, "key", "val", leaseID); err != nil { - cx.t.Fatalf("authLeaseTestTimeToLiveExpired: ctlV3Put error (%v)", err) - } - // eliminate false posative - time.Sleep(3 * time.Second) - cmdArgs := append(cx.PrefixArgs(), "lease", "timetolive", leaseID) - proc, err := spawnCmd(cmdArgs) - if err != nil { + ttl := 3 + if err := leaseTestTimeToLiveExpire(cx, ttl); err != nil { cx.t.Fatal(err) } - line, err := proc.Expect(" granted with TTL(") - if err != nil { - cx.t.Fatal(err) - } - if !strings.Contains(line, "TTL(3s), remaining(0s)") { - cx.t.Fatalf("expected 'TTL(3s), remaining(0s)', got %q", line) - } - if err := ctlV3Get(cx, []string{"key"}, kv{"key", ""}); err != nil { - cx.t.Fatalf("authLeaseTestTimeToLiveExpired: ctlV3Get error (%v)", err) - } } func authTestWatch(cx ctlCtx) { diff --git a/e2e/ctl_v3_lease_test.go b/e2e/ctl_v3_lease_test.go index dfb4cc221226..5599c3b3a96e 100644 --- a/e2e/ctl_v3_lease_test.go +++ b/e2e/ctl_v3_lease_test.go @@ -76,30 +76,45 @@ func leaseTestGrantLeasesList(cx ctlCtx) { } func leaseTestTimeToLiveExpired(cx ctlCtx) { - leaseID, err := ctlV3LeaseGrant(cx, 3) + err := leaseTestTimeToLiveExpire(cx, 3) if err != nil { cx.t.Fatal(err) } +} + +func leaseTestTimeToLiveExpire(cx ctlCtx, ttl int) error { + leaseID, err := ctlV3LeaseGrant(cx, ttl) + if err != nil { + return err + } + + // using auth? + r := 0 + if cx.pass != "" { + r = -1 + } if err := ctlV3Put(cx, "key", "val", leaseID); err != nil { cx.t.Fatalf("leaseTestTimeToLiveExpired: ctlV3Put error (%v)", err) } - // eliminate false posative - time.Sleep(3 * time.Second) + // eliminate false positive + time.Sleep(time.Duration(ttl+1) * time.Second) cmdArgs := append(cx.PrefixArgs(), "lease", "timetolive", leaseID) proc, err := spawnCmd(cmdArgs) if err != nil { - cx.t.Fatal(err) + return err } line, err := proc.Expect(" granted with TTL(") if err != nil { - cx.t.Fatal(err) + return err } - if !strings.Contains(line, "TTL(3s), remaining(0s)") { - cx.t.Fatalf("expected 'TTL(3s), remaining(0s)', got %q", line) + e := fmt.Sprintf("TTL(3s), remaining(%ds)", r) + if !strings.Contains(line, e) { + return fmt.Errorf(fmt.Sprintf("expected '%s', got %q", e, line)) } if err := ctlV3Get(cx, []string{"key"}, kv{"key", ""}); err != nil { - cx.t.Fatalf("leaseTestTimeToLiveExpired: ctlV3Get error (%v)", err) + fmt.Errorf("leaseTestTimeToLiveExpired: ctlV3Get error (%v)", err) } + return nil } func leaseTestKeepAlive(cx ctlCtx) {