123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- // Copyright 2016 The etcd Authors
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package e2e
- import (
- "fmt"
- "strconv"
- "strings"
- "testing"
- "time"
- )
- func TestCtlV3LeaseGrantTimeToLive(t *testing.T) { testCtl(t, leaseTestGrantTimeToLive) }
- func TestCtlV3LeaseGrantTimeToLiveNoTLS(t *testing.T) {
- testCtl(t, leaseTestGrantTimeToLive, withCfg(configNoTLS))
- }
- func TestCtlV3LeaseGrantTimeToLiveClientTLS(t *testing.T) {
- testCtl(t, leaseTestGrantTimeToLive, withCfg(configClientTLS))
- }
- func TestCtlV3LeaseGrantTimeToLiveClientAutoTLS(t *testing.T) {
- testCtl(t, leaseTestGrantTimeToLive, withCfg(configClientAutoTLS))
- }
- func TestCtlV3LeaseGrantTimeToLivePeerTLS(t *testing.T) {
- testCtl(t, leaseTestGrantTimeToLive, withCfg(configPeerTLS))
- }
- func TestCtlV3LeaseGrantLeases(t *testing.T) { testCtl(t, leaseTestGrantLeaseListed) }
- func TestCtlV3LeaseGrantLeasesNoTLS(t *testing.T) {
- testCtl(t, leaseTestGrantLeaseListed, withCfg(configNoTLS))
- }
- func TestCtlV3LeaseGrantLeasesClientTLS(t *testing.T) {
- testCtl(t, leaseTestGrantLeaseListed, withCfg(configClientTLS))
- }
- func TestCtlV3LeaseGrantLeasesClientAutoTLS(t *testing.T) {
- testCtl(t, leaseTestGrantLeaseListed, withCfg(configClientAutoTLS))
- }
- func TestCtlV3LeaseGrantLeasesPeerTLS(t *testing.T) {
- testCtl(t, leaseTestGrantLeaseListed, withCfg(configPeerTLS))
- }
- func TestCtlV3LeaseTestTimeToLiveExpired(t *testing.T) { testCtl(t, leaseTestTimeToLiveExpired) }
- func TestCtlV3LeaseTestTimeToLiveExpiredNoTLS(t *testing.T) {
- testCtl(t, leaseTestTimeToLiveExpired, withCfg(configNoTLS))
- }
- func TestCtlV3LeaseTestTimeToLiveExpiredClientTLS(t *testing.T) {
- testCtl(t, leaseTestTimeToLiveExpired, withCfg(configClientTLS))
- }
- func TestCtlV3LeaseTestTimeToLiveExpiredClientAutoTLS(t *testing.T) {
- testCtl(t, leaseTestTimeToLiveExpired, withCfg(configClientAutoTLS))
- }
- func TestCtlV3LeaseTestTimeToLiveExpiredPeerTLS(t *testing.T) {
- testCtl(t, leaseTestTimeToLiveExpired, withCfg(configPeerTLS))
- }
- func TestCtlV3LeaseKeepAlive(t *testing.T) { testCtl(t, leaseTestKeepAlive) }
- func TestCtlV3LeaseKeepAliveNoTLS(t *testing.T) { testCtl(t, leaseTestKeepAlive, withCfg(configNoTLS)) }
- func TestCtlV3LeaseKeepAliveClientTLS(t *testing.T) {
- testCtl(t, leaseTestKeepAlive, withCfg(configClientTLS))
- }
- func TestCtlV3LeaseKeepAliveClientAutoTLS(t *testing.T) {
- testCtl(t, leaseTestKeepAlive, withCfg(configClientAutoTLS))
- }
- func TestCtlV3LeaseKeepAlivePeerTLS(t *testing.T) {
- testCtl(t, leaseTestKeepAlive, withCfg(configPeerTLS))
- }
- func TestCtlV3LeaseKeepAliveOnce(t *testing.T) { testCtl(t, leaseTestKeepAliveOnce) }
- func TestCtlV3LeaseKeepAliveOnceNoTLS(t *testing.T) {
- testCtl(t, leaseTestKeepAliveOnce, withCfg(configNoTLS))
- }
- func TestCtlV3LeaseKeepAliveOnceClientTLS(t *testing.T) {
- testCtl(t, leaseTestKeepAliveOnce, withCfg(configClientTLS))
- }
- func TestCtlV3LeaseKeepAliveOnceClientAutoTLS(t *testing.T) {
- testCtl(t, leaseTestKeepAliveOnce, withCfg(configClientAutoTLS))
- }
- func TestCtlV3LeaseKeepAliveOncePeerTLS(t *testing.T) {
- testCtl(t, leaseTestKeepAliveOnce, withCfg(configPeerTLS))
- }
- func TestCtlV3LeaseRevoke(t *testing.T) { testCtl(t, leaseTestRevoked) }
- func TestCtlV3LeaseRevokeNoTLS(t *testing.T) { testCtl(t, leaseTestRevoked, withCfg(configNoTLS)) }
- func TestCtlV3LeaseRevokeClientTLS(t *testing.T) {
- testCtl(t, leaseTestRevoked, withCfg(configClientTLS))
- }
- func TestCtlV3LeaseRevokeClientAutoTLS(t *testing.T) {
- testCtl(t, leaseTestRevoked, withCfg(configClientAutoTLS))
- }
- func TestCtlV3LeaseRevokePeerTLS(t *testing.T) { testCtl(t, leaseTestRevoked, withCfg(configPeerTLS)) }
- func leaseTestGrantTimeToLive(cx ctlCtx) {
- id, err := ctlV3LeaseGrant(cx, 10)
- if err != nil {
- cx.t.Fatalf("leaseTestGrantTimeToLive: ctlV3LeaseGrant error (%v)", err)
- }
- cmdArgs := append(cx.PrefixArgs(), "lease", "timetolive", id, "--keys")
- proc, err := spawnCmd(cmdArgs)
- if err != nil {
- cx.t.Fatalf("leaseTestGrantTimeToLive: error (%v)", err)
- }
- line, err := proc.Expect(" granted with TTL(")
- if err != nil {
- cx.t.Fatalf("leaseTestGrantTimeToLive: error (%v)", err)
- }
- if err = proc.Close(); err != nil {
- cx.t.Fatalf("leaseTestGrantTimeToLive: error (%v)", err)
- }
- if !strings.Contains(line, ", attached keys") {
- cx.t.Fatalf("leaseTestGrantTimeToLive: expected 'attached keys', got %q", line)
- }
- if !strings.Contains(line, id) {
- cx.t.Fatalf("leaseTestGrantTimeToLive: expected leaseID %q, got %q", id, line)
- }
- }
- func leaseTestGrantLeaseListed(cx ctlCtx) {
- err := leaseTestGrantLeasesList(cx)
- if err != nil {
- cx.t.Fatalf("leaseTestGrantLeasesList: (%v)", err)
- }
- }
- func leaseTestGrantLeasesList(cx ctlCtx) error {
- id, err := ctlV3LeaseGrant(cx, 10)
- if err != nil {
- return fmt.Errorf("ctlV3LeaseGrant error (%v)", err)
- }
- cmdArgs := append(cx.PrefixArgs(), "lease", "list")
- proc, err := spawnCmd(cmdArgs)
- if err != nil {
- return fmt.Errorf("lease list failed (%v)", err)
- }
- _, err = proc.Expect(id)
- if err != nil {
- return fmt.Errorf("lease id not in returned list (%v)", err)
- }
- return proc.Close()
- }
- func leaseTestTimeToLiveExpired(cx ctlCtx) {
- err := leaseTestTimeToLiveExpire(cx, 3)
- if err != nil {
- cx.t.Fatalf("leaseTestTimeToLiveExpire: (%v)", err)
- }
- }
- func leaseTestTimeToLiveExpire(cx ctlCtx, ttl int) error {
- leaseID, err := ctlV3LeaseGrant(cx, ttl)
- if err != nil {
- return fmt.Errorf("ctlV3LeaseGrant error (%v)", err)
- }
- if err = ctlV3Put(cx, "key", "val", leaseID); err != nil {
- return fmt.Errorf("ctlV3Put error (%v)", err)
- }
- // eliminate false positive
- time.Sleep(time.Duration(ttl+1) * time.Second)
- cmdArgs := append(cx.PrefixArgs(), "lease", "timetolive", leaseID)
- exp := fmt.Sprintf("lease %s already expired", leaseID)
- if err = spawnWithExpect(cmdArgs, exp); err != nil {
- return fmt.Errorf("lease not properly expired: (%v)", err)
- }
- if err := ctlV3Get(cx, []string{"key"}); err != nil {
- return fmt.Errorf("ctlV3Get error (%v)", err)
- }
- return nil
- }
- func leaseTestKeepAlive(cx ctlCtx) {
- // put with TTL 10 seconds and keep-alive
- leaseID, err := ctlV3LeaseGrant(cx, 10)
- if err != nil {
- cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseGrant error (%v)", err)
- }
- if err := ctlV3Put(cx, "key", "val", leaseID); err != nil {
- cx.t.Fatalf("leaseTestKeepAlive: ctlV3Put error (%v)", err)
- }
- if err := ctlV3LeaseKeepAlive(cx, leaseID); err != nil {
- cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseKeepAlive error (%v)", err)
- }
- if err := ctlV3Get(cx, []string{"key"}, kv{"key", "val"}); err != nil {
- cx.t.Fatalf("leaseTestKeepAlive: ctlV3Get error (%v)", err)
- }
- }
- func leaseTestKeepAliveOnce(cx ctlCtx) {
- // put with TTL 10 seconds and keep-alive once
- leaseID, err := ctlV3LeaseGrant(cx, 10)
- if err != nil {
- cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseGrant error (%v)", err)
- }
- if err := ctlV3Put(cx, "key", "val", leaseID); err != nil {
- cx.t.Fatalf("leaseTestKeepAlive: ctlV3Put error (%v)", err)
- }
- if err := ctlV3LeaseKeepAliveOnce(cx, leaseID); err != nil {
- cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseKeepAliveOnce error (%v)", err)
- }
- if err := ctlV3Get(cx, []string{"key"}, kv{"key", "val"}); err != nil {
- cx.t.Fatalf("leaseTestKeepAlive: ctlV3Get error (%v)", err)
- }
- }
- func leaseTestRevoked(cx ctlCtx) {
- err := leaseTestRevoke(cx)
- if err != nil {
- cx.t.Fatalf("leaseTestRevoke: (%v)", err)
- }
- }
- func leaseTestRevoke(cx ctlCtx) error {
- // put with TTL 10 seconds and revoke
- leaseID, err := ctlV3LeaseGrant(cx, 10)
- if err != nil {
- return fmt.Errorf("ctlV3LeaseGrant error (%v)", err)
- }
- if err := ctlV3Put(cx, "key", "val", leaseID); err != nil {
- return fmt.Errorf("ctlV3Put error (%v)", err)
- }
- if err := ctlV3LeaseRevoke(cx, leaseID); err != nil {
- return fmt.Errorf("ctlV3LeaseRevoke error (%v)", err)
- }
- if err := ctlV3Get(cx, []string{"key"}); err != nil { // expect no output
- return fmt.Errorf("ctlV3Get error (%v)", err)
- }
- return nil
- }
- func ctlV3LeaseGrant(cx ctlCtx, ttl int) (string, error) {
- cmdArgs := append(cx.PrefixArgs(), "lease", "grant", strconv.Itoa(ttl))
- proc, err := spawnCmd(cmdArgs)
- if err != nil {
- return "", err
- }
- line, err := proc.Expect(" granted with TTL(")
- if err != nil {
- return "", err
- }
- if err = proc.Close(); err != nil {
- return "", err
- }
- // parse 'line LEASE_ID granted with TTL(5s)' to get lease ID
- hs := strings.Split(line, " ")
- if len(hs) < 2 {
- return "", fmt.Errorf("lease grant failed with %q", line)
- }
- return hs[1], nil
- }
- func ctlV3LeaseKeepAlive(cx ctlCtx, leaseID string) error {
- cmdArgs := append(cx.PrefixArgs(), "lease", "keep-alive", leaseID)
- proc, err := spawnCmd(cmdArgs)
- if err != nil {
- return err
- }
- if _, err = proc.Expect(fmt.Sprintf("lease %s keepalived with TTL(", leaseID)); err != nil {
- return err
- }
- return proc.Stop()
- }
- func ctlV3LeaseKeepAliveOnce(cx ctlCtx, leaseID string) error {
- cmdArgs := append(cx.PrefixArgs(), "lease", "keep-alive", "--once", leaseID)
- proc, err := spawnCmd(cmdArgs)
- if err != nil {
- return err
- }
- if _, err = proc.Expect(fmt.Sprintf("lease %s keepalived with TTL(", leaseID)); err != nil {
- return err
- }
- return proc.Stop()
- }
- func ctlV3LeaseRevoke(cx ctlCtx, leaseID string) error {
- cmdArgs := append(cx.PrefixArgs(), "lease", "revoke", leaseID)
- return spawnWithExpect(cmdArgs, fmt.Sprintf("lease %s revoked", leaseID))
- }
|