v3_curl_lease_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright 2018 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. "testing"
  18. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  19. )
  20. func TestV3CurlLeaseGrantNoTLS(t *testing.T) {
  21. for _, p := range apiPrefix {
  22. testCtl(t, testV3CurlLeaseGrant, withApiPrefix(p), withCfg(configNoTLS))
  23. }
  24. }
  25. func TestV3CurlLeaseRevokeNoTLS(t *testing.T) {
  26. for _, p := range apiPrefix {
  27. testCtl(t, testV3CurlLeaseRevoke, withApiPrefix(p), withCfg(configNoTLS))
  28. }
  29. }
  30. func TestV3CurlLeaseLeasesNoTLS(t *testing.T) {
  31. for _, p := range apiPrefix {
  32. testCtl(t, testV3CurlLeaseLeases, withApiPrefix(p), withCfg(configNoTLS))
  33. }
  34. }
  35. func TestV3CurlLeaseKeepAliveNoTLS(t *testing.T) {
  36. for _, p := range apiPrefix {
  37. testCtl(t, testV3CurlLeaseKeepAlive, withApiPrefix(p), withCfg(configNoTLS))
  38. }
  39. }
  40. type v3cURLTest struct {
  41. endpoint string
  42. value string
  43. expected string
  44. }
  45. // TODO remove /kv/lease/timetolive, /kv/lease/revoke, /kv/lease/leases tests in 3.5 release
  46. func testV3CurlLeaseGrant(cx ctlCtx) {
  47. leaseID := randomLeaseID()
  48. tests := []v3cURLTest{
  49. {
  50. endpoint: "/lease/grant",
  51. value: gwLeaseGrant(cx, leaseID, 0),
  52. expected: gwLeaseIDExpected(leaseID),
  53. },
  54. {
  55. endpoint: "/lease/grant",
  56. value: gwLeaseGrant(cx, 0, 20),
  57. expected: `"TTL":"20"`,
  58. },
  59. {
  60. endpoint: "/kv/put",
  61. value: gwKVPutLease(cx, "foo", "bar", leaseID),
  62. expected: `"revision":"`,
  63. },
  64. {
  65. endpoint: "/lease/timetolive",
  66. value: gwLeaseTTLWithKeys(cx, leaseID),
  67. expected: `"grantedTTL"`,
  68. },
  69. {
  70. endpoint: "/kv/lease/timetolive",
  71. value: gwLeaseTTLWithKeys(cx, leaseID),
  72. expected: `"grantedTTL"`,
  73. },
  74. }
  75. if err := cURLWithExpected(cx, tests); err != nil {
  76. cx.t.Fatalf("testV3CurlLeaseGrant: %v", err)
  77. }
  78. }
  79. func testV3CurlLeaseRevoke(cx ctlCtx) {
  80. leaseID := randomLeaseID()
  81. tests := []v3cURLTest{
  82. {
  83. endpoint: "/lease/grant",
  84. value: gwLeaseGrant(cx, leaseID, 0),
  85. expected: gwLeaseIDExpected(leaseID),
  86. },
  87. {
  88. endpoint: "/lease/revoke",
  89. value: gwLeaseRevoke(cx, leaseID),
  90. expected: `"revision":"`,
  91. },
  92. {
  93. endpoint: "/kv/lease/revoke",
  94. value: gwLeaseRevoke(cx, leaseID),
  95. expected: `etcdserver: requested lease not found`,
  96. },
  97. }
  98. if err := cURLWithExpected(cx, tests); err != nil {
  99. cx.t.Fatalf("testV3CurlLeaseRevoke: %v", err)
  100. }
  101. }
  102. func testV3CurlLeaseLeases(cx ctlCtx) {
  103. leaseID := randomLeaseID()
  104. tests := []v3cURLTest{
  105. {
  106. endpoint: "/lease/grant",
  107. value: gwLeaseGrant(cx, leaseID, 0),
  108. expected: gwLeaseIDExpected(leaseID),
  109. },
  110. {
  111. endpoint: "/lease/leases",
  112. value: "{}",
  113. expected: gwLeaseIDExpected(leaseID),
  114. },
  115. {
  116. endpoint: "/kv/lease/leases",
  117. value: "{}",
  118. expected: gwLeaseIDExpected(leaseID),
  119. },
  120. }
  121. if err := cURLWithExpected(cx, tests); err != nil {
  122. cx.t.Fatalf("testV3CurlLeaseGrant: %v", err)
  123. }
  124. }
  125. func testV3CurlLeaseKeepAlive(cx ctlCtx) {
  126. leaseID := randomLeaseID()
  127. tests := []v3cURLTest{
  128. {
  129. endpoint: "/lease/grant",
  130. value: gwLeaseGrant(cx, leaseID, 0),
  131. expected: gwLeaseIDExpected(leaseID),
  132. },
  133. {
  134. endpoint: "/lease/keepalive",
  135. value: gwLeaseKeepAlive(cx, leaseID),
  136. expected: gwLeaseIDExpected(leaseID),
  137. },
  138. }
  139. if err := cURLWithExpected(cx, tests); err != nil {
  140. cx.t.Fatalf("testV3CurlLeaseGrant: %v", err)
  141. }
  142. }
  143. func gwLeaseIDExpected(leaseID int64) string {
  144. return fmt.Sprintf(`"ID":"%d"`, leaseID)
  145. }
  146. func gwLeaseTTLWithKeys(cx ctlCtx, leaseID int64) string {
  147. d := &pb.LeaseTimeToLiveRequest{ID: leaseID, Keys: true}
  148. s, err := dataMarshal(d)
  149. if err != nil {
  150. cx.t.Fatalf("gwLeaseTTLWithKeys: error (%v)", err)
  151. }
  152. return s
  153. }
  154. func gwLeaseKeepAlive(cx ctlCtx, leaseID int64) string {
  155. d := &pb.LeaseKeepAliveRequest{ID: leaseID}
  156. s, err := dataMarshal(d)
  157. if err != nil {
  158. cx.t.Fatalf("gwLeaseKeepAlive: Marshal error (%v)", err)
  159. }
  160. return s
  161. }
  162. func gwLeaseGrant(cx ctlCtx, leaseID int64, ttl int64) string {
  163. d := &pb.LeaseGrantRequest{ID: leaseID, TTL: ttl}
  164. s, err := dataMarshal(d)
  165. if err != nil {
  166. cx.t.Fatalf("gwLeaseGrant: Marshal error (%v)", err)
  167. }
  168. return s
  169. }
  170. func gwLeaseRevoke(cx ctlCtx, leaseID int64) string {
  171. d := &pb.LeaseRevokeRequest{ID: leaseID}
  172. s, err := dataMarshal(d)
  173. if err != nil {
  174. cx.t.Fatalf("gwLeaseRevoke: Marshal error (%v)", err)
  175. }
  176. return s
  177. }
  178. func gwKVPutLease(cx ctlCtx, k string, v string, leaseID int64) string {
  179. d := pb.PutRequest{Key: []byte(k), Value: []byte(v), Lease: leaseID}
  180. s, err := dataMarshal(d)
  181. if err != nil {
  182. cx.t.Fatalf("gwKVPutLease: Marshal error (%v)", err)
  183. }
  184. return s
  185. }