v3_curl_test.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. "encoding/json"
  17. "path"
  18. "testing"
  19. epb "github.com/coreos/etcd/etcdserver/api/v3election/v3electionpb"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. "github.com/grpc-ecosystem/grpc-gateway/runtime"
  23. )
  24. func TestV3CurlPutGetNoTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configNoTLS) }
  25. func TestV3CurlPutGetAutoTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configAutoTLS) }
  26. func TestV3CurlPutGetAllTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configTLS) }
  27. func TestV3CurlPutGetPeerTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configPeerTLS) }
  28. func TestV3CurlPutGetClientTLS(t *testing.T) { testCurlPutGetGRPCGateway(t, &configClientTLS) }
  29. func testCurlPutGetGRPCGateway(t *testing.T, cfg *etcdProcessClusterConfig) {
  30. defer testutil.AfterTest(t)
  31. epc, err := newEtcdProcessCluster(cfg)
  32. if err != nil {
  33. t.Fatalf("could not start etcd process cluster (%v)", err)
  34. }
  35. defer func() {
  36. if cerr := epc.Close(); err != nil {
  37. t.Fatalf("error closing etcd processes (%v)", cerr)
  38. }
  39. }()
  40. var (
  41. key = []byte("foo")
  42. value = []byte("bar") // this will be automatically base64-encoded by Go
  43. expectPut = `"revision":"`
  44. expectGet = `"value":"`
  45. )
  46. putData, err := json.Marshal(&pb.PutRequest{
  47. Key: key,
  48. Value: value,
  49. })
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. rangeData, err := json.Marshal(&pb.RangeRequest{
  54. Key: key,
  55. })
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. if err := cURLPost(epc, cURLReq{endpoint: "/v3alpha/kv/put", value: string(putData), expected: expectPut}); err != nil {
  60. t.Fatalf("failed put with curl (%v)", err)
  61. }
  62. if err := cURLPost(epc, cURLReq{endpoint: "/v3alpha/kv/range", value: string(rangeData), expected: expectGet}); err != nil {
  63. t.Fatalf("failed get with curl (%v)", err)
  64. }
  65. if cfg.clientTLS == clientTLSAndNonTLS {
  66. if err := cURLPost(epc, cURLReq{endpoint: "/v3alpha/kv/range", value: string(rangeData), expected: expectGet, isTLS: true}); err != nil {
  67. t.Fatalf("failed get with curl (%v)", err)
  68. }
  69. }
  70. }
  71. func TestV3CurlWatch(t *testing.T) {
  72. defer testutil.AfterTest(t)
  73. epc, err := newEtcdProcessCluster(&configNoTLS)
  74. if err != nil {
  75. t.Fatalf("could not start etcd process cluster (%v)", err)
  76. }
  77. defer func() {
  78. if cerr := epc.Close(); err != nil {
  79. t.Fatalf("error closing etcd processes (%v)", cerr)
  80. }
  81. }()
  82. // store "bar" into "foo"
  83. putreq, err := json.Marshal(&pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")})
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. if err = cURLPost(epc, cURLReq{endpoint: "/v3alpha/kv/put", value: string(putreq), expected: "revision"}); err != nil {
  88. t.Fatalf("failed put with curl (%v)", err)
  89. }
  90. // watch for first update to "foo"
  91. wcr := &pb.WatchCreateRequest{Key: []byte("foo"), StartRevision: 1}
  92. wreq, err := json.Marshal(wcr)
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. // marshaling the grpc to json gives:
  97. // "{"RequestUnion":{"CreateRequest":{"key":"Zm9v","start_revision":1}}}"
  98. // but the gprc-gateway expects a different format..
  99. wstr := `{"create_request" : ` + string(wreq) + "}"
  100. // expects "bar", timeout after 2 seconds since stream waits forever
  101. if err = cURLPost(epc, cURLReq{endpoint: "/v3alpha/watch", value: wstr, expected: `"YmFy"`, timeout: 2}); err != nil {
  102. t.Fatal(err)
  103. }
  104. }
  105. func TestV3CurlTxn(t *testing.T) {
  106. defer testutil.AfterTest(t)
  107. epc, err := newEtcdProcessCluster(&configNoTLS)
  108. if err != nil {
  109. t.Fatalf("could not start etcd process cluster (%v)", err)
  110. }
  111. defer func() {
  112. if cerr := epc.Close(); err != nil {
  113. t.Fatalf("error closing etcd processes (%v)", cerr)
  114. }
  115. }()
  116. txn := &pb.TxnRequest{
  117. Compare: []*pb.Compare{
  118. {
  119. Key: []byte("foo"),
  120. Result: pb.Compare_EQUAL,
  121. Target: pb.Compare_CREATE,
  122. TargetUnion: &pb.Compare_CreateRevision{0},
  123. },
  124. },
  125. Success: []*pb.RequestOp{
  126. {
  127. Request: &pb.RequestOp_RequestPut{
  128. RequestPut: &pb.PutRequest{
  129. Key: []byte("foo"),
  130. Value: []byte("bar"),
  131. },
  132. },
  133. },
  134. },
  135. }
  136. m := &runtime.JSONPb{}
  137. jsonDat, jerr := m.Marshal(txn)
  138. if jerr != nil {
  139. t.Fatal(jerr)
  140. }
  141. expected := `"succeeded":true,"responses":[{"response_put":{"header":{"revision":"2"}}}]`
  142. if err = cURLPost(epc, cURLReq{endpoint: "/v3alpha/kv/txn", value: string(jsonDat), expected: expected}); err != nil {
  143. t.Fatalf("failed txn with curl (%v)", err)
  144. }
  145. // was crashing etcd server
  146. malformed := `{"compare":[{"result":0,"target":1,"key":"Zm9v","TargetUnion":null}],"success":[{"Request":{"RequestPut":{"key":"Zm9v","value":"YmFy"}}}]}`
  147. if err = cURLPost(epc, cURLReq{endpoint: "/v3alpha/kv/txn", value: malformed, expected: "error"}); err != nil {
  148. t.Fatalf("failed put with curl (%v)", err)
  149. }
  150. }
  151. func TestV3CurlAuthAlpha(t *testing.T) { testV3CurlAuth(t, "/v3alpha") }
  152. func testV3CurlAuth(t *testing.T, pathPrefix string) {
  153. defer testutil.AfterTest(t)
  154. epc, err := newEtcdProcessCluster(&configNoTLS)
  155. if err != nil {
  156. t.Fatalf("could not start etcd process cluster (%v)", err)
  157. }
  158. defer func() {
  159. if cerr := epc.Close(); err != nil {
  160. t.Fatalf("error closing etcd processes (%v)", cerr)
  161. }
  162. }()
  163. // create root user
  164. userreq, err := json.Marshal(&pb.AuthUserAddRequest{Name: string("root"), Password: string("toor")})
  165. testutil.AssertNil(t, err)
  166. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/auth/user/add"), value: string(userreq), expected: "revision"}); err != nil {
  167. t.Fatalf("failed add user with curl (%v)", err)
  168. }
  169. // create root role
  170. rolereq, err := json.Marshal(&pb.AuthRoleAddRequest{Name: string("root")})
  171. testutil.AssertNil(t, err)
  172. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/auth/role/add"), value: string(rolereq), expected: "revision"}); err != nil {
  173. t.Fatalf("failed create role with curl (%v)", err)
  174. }
  175. // grant root role
  176. grantrolereq, err := json.Marshal(&pb.AuthUserGrantRoleRequest{User: string("root"), Role: string("root")})
  177. testutil.AssertNil(t, err)
  178. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/auth/user/grant"), value: string(grantrolereq), expected: "revision"}); err != nil {
  179. t.Fatalf("failed grant role with curl (%v)", err)
  180. }
  181. // enable auth
  182. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/auth/enable"), value: string("{}"), expected: "revision"}); err != nil {
  183. t.Fatalf("failed enable auth with curl (%v)", err)
  184. }
  185. // put "bar" into "foo"
  186. putreq, err := json.Marshal(&pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")})
  187. testutil.AssertNil(t, err)
  188. // fail put no auth
  189. if err = cURLPost(epc, cURLReq{endpoint: path.Join(pathPrefix, "/kv/put"), value: string(putreq), expected: "error"}); err != nil {
  190. t.Fatalf("failed no auth put with curl (%v)", err)
  191. }
  192. // auth request
  193. authreq, err := json.Marshal(&pb.AuthenticateRequest{Name: string("root"), Password: string("toor")})
  194. testutil.AssertNil(t, err)
  195. var (
  196. cmdArgs []string
  197. lineFunc = func(txt string) bool { return true }
  198. )
  199. cmdArgs = cURLPrefixArgs(epc, "POST", cURLReq{endpoint: path.Join(pathPrefix, "/auth/authenticate"), value: string(authreq)})
  200. proc, err := spawnCmd(cmdArgs)
  201. testutil.AssertNil(t, err)
  202. cURLRes, err := proc.ExpectFunc(lineFunc)
  203. testutil.AssertNil(t, err)
  204. authRes := make(map[string]interface{})
  205. testutil.AssertNil(t, json.Unmarshal([]byte(cURLRes), &authRes))
  206. }
  207. func TestV3CurlProclaimMissiongLeaderKeyNoTLS(t *testing.T) {
  208. testCtl(t, testV3CurlProclaimMissiongLeaderKey, withCfg(configNoTLS))
  209. }
  210. func testV3CurlProclaimMissiongLeaderKey(cx ctlCtx) {
  211. pdata, err := json.Marshal(&epb.ProclaimRequest{Value: []byte("v2")})
  212. if err != nil {
  213. cx.t.Fatal(err)
  214. }
  215. if err != nil {
  216. cx.t.Fatal(err)
  217. }
  218. if err = cURLPost(cx.epc, cURLReq{
  219. endpoint: path.Join("/v3alpha", "/election/proclaim"),
  220. value: string(pdata),
  221. expected: `{"error":"\"leader\" field must be provided","code":2}`,
  222. }); err != nil {
  223. cx.t.Fatalf("failed post proclaim request (%s) (%v)", "/v3alpha", err)
  224. }
  225. }
  226. func TestV3CurlResignMissiongLeaderKeyNoTLS(t *testing.T) {
  227. testCtl(t, testV3CurlResignMissiongLeaderKey, withCfg(configNoTLS))
  228. }
  229. func testV3CurlResignMissiongLeaderKey(cx ctlCtx) {
  230. if err := cURLPost(cx.epc, cURLReq{
  231. endpoint: path.Join("/v3alpha", "/election/resign"),
  232. value: `{}`,
  233. expected: `{"error":"\"leader\" field must be provided","code":2}`,
  234. }); err != nil {
  235. cx.t.Fatalf("failed post resign request (%s) (%v)", "/v3alpha", err)
  236. }
  237. }
  238. // to manually decode; JSON marshals integer fields with
  239. // string types, so can't unmarshal with epb.CampaignResponse
  240. type campaignResponse struct {
  241. Leader struct {
  242. Name string `json:"name,omitempty"`
  243. Key string `json:"key,omitempty"`
  244. Rev string `json:"rev,omitempty"`
  245. Lease string `json:"lease,omitempty"`
  246. } `json:"leader,omitempty"`
  247. }