ctl_v3_lease_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package e2e
  15. import (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "testing"
  20. )
  21. func TestCtlV3LeaseGrantTimeToLive(t *testing.T) { testCtl(t, leaseTestGrantTimeToLive) }
  22. func TestCtlV3LeaseKeepAlive(t *testing.T) { testCtl(t, leaseTestKeepAlive) }
  23. func TestCtlV3LeaseRevoke(t *testing.T) { testCtl(t, leaseTestRevoke) }
  24. func leaseTestGrantTimeToLive(cx ctlCtx) {
  25. id, err := ctlV3LeaseGrant(cx, 10)
  26. if err != nil {
  27. cx.t.Fatal(err)
  28. }
  29. cmdArgs := append(cx.PrefixArgs(), "lease", "timetolive", id, "--keys")
  30. proc, err := spawnCmd(cmdArgs)
  31. if err != nil {
  32. cx.t.Fatal(err)
  33. }
  34. line, err := proc.Expect(" granted with TTL(")
  35. if err != nil {
  36. cx.t.Fatal(err)
  37. }
  38. if err = proc.Close(); err != nil {
  39. cx.t.Fatal(err)
  40. }
  41. if !strings.Contains(line, ", attached keys") {
  42. cx.t.Fatalf("expected 'attached keys', got %q", line)
  43. }
  44. if !strings.Contains(line, id) {
  45. cx.t.Fatalf("expected leaseID %q, got %q", id, line)
  46. }
  47. }
  48. func leaseTestKeepAlive(cx ctlCtx) {
  49. // put with TTL 10 seconds and keep-alive
  50. leaseID, err := ctlV3LeaseGrant(cx, 10)
  51. if err != nil {
  52. cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseGrant error (%v)", err)
  53. }
  54. if err := ctlV3Put(cx, "key", "val", leaseID); err != nil {
  55. cx.t.Fatalf("leaseTestKeepAlive: ctlV3Put error (%v)", err)
  56. }
  57. if err := ctlV3LeaseKeepAlive(cx, leaseID); err != nil {
  58. cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseKeepAlive error (%v)", err)
  59. }
  60. if err := ctlV3Get(cx, []string{"key"}, kv{"key", "val"}); err != nil {
  61. cx.t.Fatalf("leaseTestKeepAlive: ctlV3Get error (%v)", err)
  62. }
  63. }
  64. func leaseTestRevoke(cx ctlCtx) {
  65. // put with TTL 10 seconds and revoke
  66. leaseID, err := ctlV3LeaseGrant(cx, 10)
  67. if err != nil {
  68. cx.t.Fatalf("leaseTestRevoke: ctlV3LeaseGrant error (%v)", err)
  69. }
  70. if err := ctlV3Put(cx, "key", "val", leaseID); err != nil {
  71. cx.t.Fatalf("leaseTestRevoke: ctlV3Put error (%v)", err)
  72. }
  73. if err := ctlV3LeaseRevoke(cx, leaseID); err != nil {
  74. cx.t.Fatalf("leaseTestRevoke: ctlV3LeaseRevok error (%v)", err)
  75. }
  76. if err := ctlV3Get(cx, []string{"key"}); err != nil { // expect no output
  77. cx.t.Fatalf("leaseTestRevoke: ctlV3Get error (%v)", err)
  78. }
  79. }
  80. func ctlV3LeaseGrant(cx ctlCtx, ttl int) (string, error) {
  81. cmdArgs := append(cx.PrefixArgs(), "lease", "grant", strconv.Itoa(ttl))
  82. proc, err := spawnCmd(cmdArgs)
  83. if err != nil {
  84. return "", err
  85. }
  86. line, err := proc.Expect(" granted with TTL(")
  87. if err != nil {
  88. return "", err
  89. }
  90. if err = proc.Close(); err != nil {
  91. return "", err
  92. }
  93. // parse 'line LEASE_ID granted with TTL(5s)' to get lease ID
  94. hs := strings.Split(line, " ")
  95. if len(hs) < 2 {
  96. return "", fmt.Errorf("lease grant failed with %q", line)
  97. }
  98. return hs[1], nil
  99. }
  100. func ctlV3LeaseKeepAlive(cx ctlCtx, leaseID string) error {
  101. cmdArgs := append(cx.PrefixArgs(), "lease", "keep-alive", leaseID)
  102. proc, err := spawnCmd(cmdArgs)
  103. if err != nil {
  104. return err
  105. }
  106. if _, err = proc.Expect(fmt.Sprintf("lease %s keepalived with TTL(", leaseID)); err != nil {
  107. return err
  108. }
  109. return proc.Stop()
  110. }
  111. func ctlV3LeaseRevoke(cx ctlCtx, leaseID string) error {
  112. cmdArgs := append(cx.PrefixArgs(), "lease", "revoke", leaseID)
  113. return spawnWithExpect(cmdArgs, fmt.Sprintf("lease %s revoked", leaseID))
  114. }