ctl_v3_lease_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. "time"
  21. )
  22. func TestCtlV3LeaseGrantTimeToLive(t *testing.T) { testCtl(t, leaseTestGrantTimeToLive) }
  23. func TestCtlV3LeaseGrantTimeToLiveNoTLS(t *testing.T) {
  24. testCtl(t, leaseTestGrantTimeToLive, withCfg(configNoTLS))
  25. }
  26. func TestCtlV3LeaseGrantTimeToLiveClientTLS(t *testing.T) {
  27. testCtl(t, leaseTestGrantTimeToLive, withCfg(configClientTLS))
  28. }
  29. func TestCtlV3LeaseGrantTimeToLiveClientAutoTLS(t *testing.T) {
  30. testCtl(t, leaseTestGrantTimeToLive, withCfg(configClientAutoTLS))
  31. }
  32. func TestCtlV3LeaseGrantTimeToLivePeerTLS(t *testing.T) {
  33. testCtl(t, leaseTestGrantTimeToLive, withCfg(configPeerTLS))
  34. }
  35. func TestCtlV3LeaseGrantLeases(t *testing.T) { testCtl(t, leaseTestGrantLeaseListed) }
  36. func TestCtlV3LeaseGrantLeasesNoTLS(t *testing.T) {
  37. testCtl(t, leaseTestGrantLeaseListed, withCfg(configNoTLS))
  38. }
  39. func TestCtlV3LeaseGrantLeasesClientTLS(t *testing.T) {
  40. testCtl(t, leaseTestGrantLeaseListed, withCfg(configClientTLS))
  41. }
  42. func TestCtlV3LeaseGrantLeasesClientAutoTLS(t *testing.T) {
  43. testCtl(t, leaseTestGrantLeaseListed, withCfg(configClientAutoTLS))
  44. }
  45. func TestCtlV3LeaseGrantLeasesPeerTLS(t *testing.T) {
  46. testCtl(t, leaseTestGrantLeaseListed, withCfg(configPeerTLS))
  47. }
  48. func TestCtlV3LeaseTestTimeToLiveExpired(t *testing.T) { testCtl(t, leaseTestTimeToLiveExpired) }
  49. func TestCtlV3LeaseTestTimeToLiveExpiredNoTLS(t *testing.T) {
  50. testCtl(t, leaseTestTimeToLiveExpired, withCfg(configNoTLS))
  51. }
  52. func TestCtlV3LeaseTestTimeToLiveExpiredClientTLS(t *testing.T) {
  53. testCtl(t, leaseTestTimeToLiveExpired, withCfg(configClientTLS))
  54. }
  55. func TestCtlV3LeaseTestTimeToLiveExpiredClientAutoTLS(t *testing.T) {
  56. testCtl(t, leaseTestTimeToLiveExpired, withCfg(configClientAutoTLS))
  57. }
  58. func TestCtlV3LeaseTestTimeToLiveExpiredPeerTLS(t *testing.T) {
  59. testCtl(t, leaseTestTimeToLiveExpired, withCfg(configPeerTLS))
  60. }
  61. func TestCtlV3LeaseKeepAlive(t *testing.T) { testCtl(t, leaseTestKeepAlive) }
  62. func TestCtlV3LeaseKeepAliveNoTLS(t *testing.T) { testCtl(t, leaseTestKeepAlive, withCfg(configNoTLS)) }
  63. func TestCtlV3LeaseKeepAliveClientTLS(t *testing.T) {
  64. testCtl(t, leaseTestKeepAlive, withCfg(configClientTLS))
  65. }
  66. func TestCtlV3LeaseKeepAliveClientAutoTLS(t *testing.T) {
  67. testCtl(t, leaseTestKeepAlive, withCfg(configClientAutoTLS))
  68. }
  69. func TestCtlV3LeaseKeepAlivePeerTLS(t *testing.T) {
  70. testCtl(t, leaseTestKeepAlive, withCfg(configPeerTLS))
  71. }
  72. func TestCtlV3LeaseKeepAliveOnce(t *testing.T) { testCtl(t, leaseTestKeepAliveOnce) }
  73. func TestCtlV3LeaseKeepAliveOnceNoTLS(t *testing.T) {
  74. testCtl(t, leaseTestKeepAliveOnce, withCfg(configNoTLS))
  75. }
  76. func TestCtlV3LeaseKeepAliveOnceClientTLS(t *testing.T) {
  77. testCtl(t, leaseTestKeepAliveOnce, withCfg(configClientTLS))
  78. }
  79. func TestCtlV3LeaseKeepAliveOnceClientAutoTLS(t *testing.T) {
  80. testCtl(t, leaseTestKeepAliveOnce, withCfg(configClientAutoTLS))
  81. }
  82. func TestCtlV3LeaseKeepAliveOncePeerTLS(t *testing.T) {
  83. testCtl(t, leaseTestKeepAliveOnce, withCfg(configPeerTLS))
  84. }
  85. func TestCtlV3LeaseRevoke(t *testing.T) { testCtl(t, leaseTestRevoked) }
  86. func TestCtlV3LeaseRevokeNoTLS(t *testing.T) { testCtl(t, leaseTestRevoked, withCfg(configNoTLS)) }
  87. func TestCtlV3LeaseRevokeClientTLS(t *testing.T) {
  88. testCtl(t, leaseTestRevoked, withCfg(configClientTLS))
  89. }
  90. func TestCtlV3LeaseRevokeClientAutoTLS(t *testing.T) {
  91. testCtl(t, leaseTestRevoked, withCfg(configClientAutoTLS))
  92. }
  93. func TestCtlV3LeaseRevokePeerTLS(t *testing.T) { testCtl(t, leaseTestRevoked, withCfg(configPeerTLS)) }
  94. func leaseTestGrantTimeToLive(cx ctlCtx) {
  95. id, err := ctlV3LeaseGrant(cx, 10)
  96. if err != nil {
  97. cx.t.Fatalf("leaseTestGrantTimeToLive: ctlV3LeaseGrant error (%v)", err)
  98. }
  99. cmdArgs := append(cx.PrefixArgs(), "lease", "timetolive", id, "--keys")
  100. proc, err := spawnCmd(cmdArgs)
  101. if err != nil {
  102. cx.t.Fatalf("leaseTestGrantTimeToLive: error (%v)", err)
  103. }
  104. line, err := proc.Expect(" granted with TTL(")
  105. if err != nil {
  106. cx.t.Fatalf("leaseTestGrantTimeToLive: error (%v)", err)
  107. }
  108. if err = proc.Close(); err != nil {
  109. cx.t.Fatalf("leaseTestGrantTimeToLive: error (%v)", err)
  110. }
  111. if !strings.Contains(line, ", attached keys") {
  112. cx.t.Fatalf("leaseTestGrantTimeToLive: expected 'attached keys', got %q", line)
  113. }
  114. if !strings.Contains(line, id) {
  115. cx.t.Fatalf("leaseTestGrantTimeToLive: expected leaseID %q, got %q", id, line)
  116. }
  117. }
  118. func leaseTestGrantLeaseListed(cx ctlCtx) {
  119. err := leaseTestGrantLeasesList(cx)
  120. if err != nil {
  121. cx.t.Fatalf("leaseTestGrantLeasesList: (%v)", err)
  122. }
  123. }
  124. func leaseTestGrantLeasesList(cx ctlCtx) error {
  125. id, err := ctlV3LeaseGrant(cx, 10)
  126. if err != nil {
  127. return fmt.Errorf("ctlV3LeaseGrant error (%v)", err)
  128. }
  129. cmdArgs := append(cx.PrefixArgs(), "lease", "list")
  130. proc, err := spawnCmd(cmdArgs)
  131. if err != nil {
  132. return fmt.Errorf("lease list failed (%v)", err)
  133. }
  134. _, err = proc.Expect(id)
  135. if err != nil {
  136. return fmt.Errorf("lease id not in returned list (%v)", err)
  137. }
  138. return proc.Close()
  139. }
  140. func leaseTestTimeToLiveExpired(cx ctlCtx) {
  141. err := leaseTestTimeToLiveExpire(cx, 3)
  142. if err != nil {
  143. cx.t.Fatalf("leaseTestTimeToLiveExpire: (%v)", err)
  144. }
  145. }
  146. func leaseTestTimeToLiveExpire(cx ctlCtx, ttl int) error {
  147. leaseID, err := ctlV3LeaseGrant(cx, ttl)
  148. if err != nil {
  149. return fmt.Errorf("ctlV3LeaseGrant error (%v)", err)
  150. }
  151. if err = ctlV3Put(cx, "key", "val", leaseID); err != nil {
  152. return fmt.Errorf("ctlV3Put error (%v)", err)
  153. }
  154. // eliminate false positive
  155. time.Sleep(time.Duration(ttl+1) * time.Second)
  156. cmdArgs := append(cx.PrefixArgs(), "lease", "timetolive", leaseID)
  157. exp := fmt.Sprintf("lease %s already expired", leaseID)
  158. if err = spawnWithExpect(cmdArgs, exp); err != nil {
  159. return fmt.Errorf("lease not properly expired: (%v)", err)
  160. }
  161. if err := ctlV3Get(cx, []string{"key"}); err != nil {
  162. return fmt.Errorf("ctlV3Get error (%v)", err)
  163. }
  164. return nil
  165. }
  166. func leaseTestKeepAlive(cx ctlCtx) {
  167. // put with TTL 10 seconds and keep-alive
  168. leaseID, err := ctlV3LeaseGrant(cx, 10)
  169. if err != nil {
  170. cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseGrant error (%v)", err)
  171. }
  172. if err := ctlV3Put(cx, "key", "val", leaseID); err != nil {
  173. cx.t.Fatalf("leaseTestKeepAlive: ctlV3Put error (%v)", err)
  174. }
  175. if err := ctlV3LeaseKeepAlive(cx, leaseID); err != nil {
  176. cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseKeepAlive error (%v)", err)
  177. }
  178. if err := ctlV3Get(cx, []string{"key"}, kv{"key", "val"}); err != nil {
  179. cx.t.Fatalf("leaseTestKeepAlive: ctlV3Get error (%v)", err)
  180. }
  181. }
  182. func leaseTestKeepAliveOnce(cx ctlCtx) {
  183. // put with TTL 10 seconds and keep-alive once
  184. leaseID, err := ctlV3LeaseGrant(cx, 10)
  185. if err != nil {
  186. cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseGrant error (%v)", err)
  187. }
  188. if err := ctlV3Put(cx, "key", "val", leaseID); err != nil {
  189. cx.t.Fatalf("leaseTestKeepAlive: ctlV3Put error (%v)", err)
  190. }
  191. if err := ctlV3LeaseKeepAliveOnce(cx, leaseID); err != nil {
  192. cx.t.Fatalf("leaseTestKeepAlive: ctlV3LeaseKeepAliveOnce error (%v)", err)
  193. }
  194. if err := ctlV3Get(cx, []string{"key"}, kv{"key", "val"}); err != nil {
  195. cx.t.Fatalf("leaseTestKeepAlive: ctlV3Get error (%v)", err)
  196. }
  197. }
  198. func leaseTestRevoked(cx ctlCtx) {
  199. err := leaseTestRevoke(cx)
  200. if err != nil {
  201. cx.t.Fatalf("leaseTestRevoke: (%v)", err)
  202. }
  203. }
  204. func leaseTestRevoke(cx ctlCtx) error {
  205. // put with TTL 10 seconds and revoke
  206. leaseID, err := ctlV3LeaseGrant(cx, 10)
  207. if err != nil {
  208. return fmt.Errorf("ctlV3LeaseGrant error (%v)", err)
  209. }
  210. if err := ctlV3Put(cx, "key", "val", leaseID); err != nil {
  211. return fmt.Errorf("ctlV3Put error (%v)", err)
  212. }
  213. if err := ctlV3LeaseRevoke(cx, leaseID); err != nil {
  214. return fmt.Errorf("ctlV3LeaseRevoke error (%v)", err)
  215. }
  216. if err := ctlV3Get(cx, []string{"key"}); err != nil { // expect no output
  217. return fmt.Errorf("ctlV3Get error (%v)", err)
  218. }
  219. return nil
  220. }
  221. func ctlV3LeaseGrant(cx ctlCtx, ttl int) (string, error) {
  222. cmdArgs := append(cx.PrefixArgs(), "lease", "grant", strconv.Itoa(ttl))
  223. proc, err := spawnCmd(cmdArgs)
  224. if err != nil {
  225. return "", err
  226. }
  227. line, err := proc.Expect(" granted with TTL(")
  228. if err != nil {
  229. return "", err
  230. }
  231. if err = proc.Close(); err != nil {
  232. return "", err
  233. }
  234. // parse 'line LEASE_ID granted with TTL(5s)' to get lease ID
  235. hs := strings.Split(line, " ")
  236. if len(hs) < 2 {
  237. return "", fmt.Errorf("lease grant failed with %q", line)
  238. }
  239. return hs[1], nil
  240. }
  241. func ctlV3LeaseKeepAlive(cx ctlCtx, leaseID string) error {
  242. cmdArgs := append(cx.PrefixArgs(), "lease", "keep-alive", leaseID)
  243. proc, err := spawnCmd(cmdArgs)
  244. if err != nil {
  245. return err
  246. }
  247. if _, err = proc.Expect(fmt.Sprintf("lease %s keepalived with TTL(", leaseID)); err != nil {
  248. return err
  249. }
  250. return proc.Stop()
  251. }
  252. func ctlV3LeaseKeepAliveOnce(cx ctlCtx, leaseID string) error {
  253. cmdArgs := append(cx.PrefixArgs(), "lease", "keep-alive", "--once", leaseID)
  254. proc, err := spawnCmd(cmdArgs)
  255. if err != nil {
  256. return err
  257. }
  258. if _, err = proc.Expect(fmt.Sprintf("lease %s keepalived with TTL(", leaseID)); err != nil {
  259. return err
  260. }
  261. return proc.Stop()
  262. }
  263. func ctlV3LeaseRevoke(cx ctlCtx, leaseID string) error {
  264. cmdArgs := append(cx.PrefixArgs(), "lease", "revoke", leaseID)
  265. return spawnWithExpect(cmdArgs, fmt.Sprintf("lease %s revoked", leaseID))
  266. }