ctl_v3_lease_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 TestCtlV3LeaseKeepAlive(t *testing.T) { testCtl(t, leaseTestKeepAlive) }
  22. func TestCtlV3LeaseRevoke(t *testing.T) { testCtl(t, leaseTestRevoke) }
  23. func leaseTestKeepAlive(cx ctlCtx) {
  24. // put with TTL 10 seconds and keep-alive
  25. leaseID, err := ctlV3LeaseGrant(cx, 10)
  26. if err != nil {
  27. cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseGrant error (%v)", err)
  28. }
  29. if err := ctlV3Put(cx, "key", "val", leaseID); err != nil {
  30. cx.t.Fatalf("leaseTestKeepAlive: ctlV3Put error (%v)", err)
  31. }
  32. if err := ctlV3LeaseKeepAlive(cx, leaseID); err != nil {
  33. cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseKeepAlive error (%v)", err)
  34. }
  35. if err := ctlV3Get(cx, []string{"key"}, kv{"key", "val"}); err != nil {
  36. cx.t.Fatalf("leaseTestKeepAlive: ctlV3Get error (%v)", err)
  37. }
  38. }
  39. func leaseTestRevoke(cx ctlCtx) {
  40. // put with TTL 10 seconds and revoke
  41. leaseID, err := ctlV3LeaseGrant(cx, 10)
  42. if err != nil {
  43. cx.t.Fatalf("leaseTestRevoke: ctlV3LeaseGrant error (%v)", err)
  44. }
  45. if err := ctlV3Put(cx, "key", "val", leaseID); err != nil {
  46. cx.t.Fatalf("leaseTestRevoke: ctlV3Put error (%v)", err)
  47. }
  48. if err := ctlV3LeaseRevoke(cx, leaseID); err != nil {
  49. cx.t.Fatalf("leaseTestRevoke: ctlV3LeaseRevok error (%v)", err)
  50. }
  51. if err := ctlV3Get(cx, []string{"key"}); err != nil { // expect no output
  52. cx.t.Fatalf("leaseTestRevoke: ctlV3Get error (%v)", err)
  53. }
  54. }
  55. func ctlV3LeaseGrant(cx ctlCtx, ttl int) (string, error) {
  56. cmdArgs := append(cx.PrefixArgs(), "lease", "grant", strconv.Itoa(ttl))
  57. proc, err := spawnCmd(cmdArgs)
  58. if err != nil {
  59. return "", err
  60. }
  61. line, err := proc.Expect(" granted with TTL(")
  62. if err != nil {
  63. return "", err
  64. }
  65. if err = proc.Close(); err != nil {
  66. return "", err
  67. }
  68. // parse 'line LEASE_ID granted with TTL(5s)' to get lease ID
  69. hs := strings.Split(line, " ")
  70. if len(hs) < 2 {
  71. return "", fmt.Errorf("lease grant failed with %q", line)
  72. }
  73. return hs[1], nil
  74. }
  75. func ctlV3LeaseKeepAlive(cx ctlCtx, leaseID string) error {
  76. cmdArgs := append(cx.PrefixArgs(), "lease", "keep-alive", leaseID)
  77. proc, err := spawnCmd(cmdArgs)
  78. if err != nil {
  79. return err
  80. }
  81. if _, err = proc.Expect(fmt.Sprintf("lease %s keepalived with TTL(", leaseID)); err != nil {
  82. return err
  83. }
  84. return proc.Stop()
  85. }
  86. func ctlV3LeaseRevoke(cx ctlCtx, leaseID string) error {
  87. cmdArgs := append(cx.PrefixArgs(), "lease", "revoke", leaseID)
  88. return spawnWithExpect(cmdArgs, fmt.Sprintf("lease %s revoked", leaseID))
  89. }