v3_grpc_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. // Copyright 2016 CoreOS, Inc.
  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.package recipe
  14. package integration
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  21. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  22. "github.com/coreos/etcd/etcdserver/api/v3rpc"
  23. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  24. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  25. "github.com/coreos/etcd/pkg/testutil"
  26. )
  27. // TestV3PutOverwrite puts a key with the v3 api to a random cluster member,
  28. // overwrites it, then checks that the change was applied.
  29. func TestV3PutOverwrite(t *testing.T) {
  30. defer testutil.AfterTest(t)
  31. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  32. defer clus.Terminate(t)
  33. kvc := toGRPC(clus.RandClient()).KV
  34. key := []byte("foo")
  35. reqput := &pb.PutRequest{Key: key, Value: []byte("bar")}
  36. respput, err := kvc.Put(context.TODO(), reqput)
  37. if err != nil {
  38. t.Fatalf("couldn't put key (%v)", err)
  39. }
  40. // overwrite
  41. reqput.Value = []byte("baz")
  42. respput2, err := kvc.Put(context.TODO(), reqput)
  43. if err != nil {
  44. t.Fatalf("couldn't put key (%v)", err)
  45. }
  46. if respput2.Header.Revision <= respput.Header.Revision {
  47. t.Fatalf("expected newer revision on overwrite, got %v <= %v",
  48. respput2.Header.Revision, respput.Header.Revision)
  49. }
  50. reqrange := &pb.RangeRequest{Key: key}
  51. resprange, err := kvc.Range(context.TODO(), reqrange)
  52. if err != nil {
  53. t.Fatalf("couldn't get key (%v)", err)
  54. }
  55. if len(resprange.Kvs) != 1 {
  56. t.Fatalf("expected 1 key, got %v", len(resprange.Kvs))
  57. }
  58. kv := resprange.Kvs[0]
  59. if kv.ModRevision <= kv.CreateRevision {
  60. t.Errorf("expected modRev > createRev, got %d <= %d",
  61. kv.ModRevision, kv.CreateRevision)
  62. }
  63. if !reflect.DeepEqual(reqput.Value, kv.Value) {
  64. t.Errorf("expected value %v, got %v", reqput.Value, kv.Value)
  65. }
  66. }
  67. func TestV3TxnTooManyOps(t *testing.T) {
  68. defer testutil.AfterTest(t)
  69. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  70. defer clus.Terminate(t)
  71. kvc := toGRPC(clus.RandClient()).KV
  72. // unique keys
  73. i := new(int)
  74. keyf := func() []byte {
  75. *i++
  76. return []byte(fmt.Sprintf("key-%d", i))
  77. }
  78. addCompareOps := func(txn *pb.TxnRequest) {
  79. txn.Compare = append(txn.Compare,
  80. &pb.Compare{
  81. Result: pb.Compare_GREATER,
  82. Target: pb.Compare_CREATE,
  83. Key: keyf(),
  84. })
  85. }
  86. addSuccessOps := func(txn *pb.TxnRequest) {
  87. txn.Success = append(txn.Success,
  88. &pb.RequestUnion{
  89. Request: &pb.RequestUnion_RequestPut{
  90. RequestPut: &pb.PutRequest{
  91. Key: keyf(),
  92. Value: []byte("bar"),
  93. },
  94. },
  95. })
  96. }
  97. addFailureOps := func(txn *pb.TxnRequest) {
  98. txn.Failure = append(txn.Failure,
  99. &pb.RequestUnion{
  100. Request: &pb.RequestUnion_RequestPut{
  101. RequestPut: &pb.PutRequest{
  102. Key: keyf(),
  103. Value: []byte("bar"),
  104. },
  105. },
  106. })
  107. }
  108. tests := []func(txn *pb.TxnRequest){
  109. addCompareOps,
  110. addSuccessOps,
  111. addFailureOps,
  112. }
  113. for i, tt := range tests {
  114. txn := &pb.TxnRequest{}
  115. for j := 0; j < v3rpc.MaxOpsPerTxn+1; j++ {
  116. tt(txn)
  117. }
  118. _, err := kvc.Txn(context.Background(), txn)
  119. if err != rpctypes.ErrTooManyOps {
  120. t.Errorf("#%d: err = %v, want %v", i, err, rpctypes.ErrTooManyOps)
  121. }
  122. }
  123. }
  124. func TestV3TxnDuplicateKeys(t *testing.T) {
  125. defer testutil.AfterTest(t)
  126. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  127. defer clus.Terminate(t)
  128. putreq := &pb.RequestUnion{Request: &pb.RequestUnion_RequestPut{RequestPut: &pb.PutRequest{Key: []byte("abc"), Value: []byte("def")}}}
  129. delKeyReq := &pb.RequestUnion{Request: &pb.RequestUnion_RequestDeleteRange{
  130. RequestDeleteRange: &pb.DeleteRangeRequest{
  131. Key: []byte("abc"),
  132. },
  133. },
  134. }
  135. delInRangeReq := &pb.RequestUnion{Request: &pb.RequestUnion_RequestDeleteRange{
  136. RequestDeleteRange: &pb.DeleteRangeRequest{
  137. Key: []byte("a"), RangeEnd: []byte("b"),
  138. },
  139. },
  140. }
  141. delOutOfRangeReq := &pb.RequestUnion{Request: &pb.RequestUnion_RequestDeleteRange{
  142. RequestDeleteRange: &pb.DeleteRangeRequest{
  143. Key: []byte("abb"), RangeEnd: []byte("abc"),
  144. },
  145. },
  146. }
  147. kvc := toGRPC(clus.RandClient()).KV
  148. tests := []struct {
  149. txnSuccess []*pb.RequestUnion
  150. werr error
  151. }{
  152. {
  153. txnSuccess: []*pb.RequestUnion{putreq, putreq},
  154. werr: rpctypes.ErrDuplicateKey,
  155. },
  156. {
  157. txnSuccess: []*pb.RequestUnion{putreq, delKeyReq},
  158. werr: rpctypes.ErrDuplicateKey,
  159. },
  160. {
  161. txnSuccess: []*pb.RequestUnion{putreq, delInRangeReq},
  162. werr: rpctypes.ErrDuplicateKey,
  163. },
  164. {
  165. txnSuccess: []*pb.RequestUnion{delKeyReq, delInRangeReq, delKeyReq, delInRangeReq},
  166. werr: nil,
  167. },
  168. {
  169. txnSuccess: []*pb.RequestUnion{putreq, delOutOfRangeReq},
  170. werr: nil,
  171. },
  172. }
  173. for i, tt := range tests {
  174. txn := &pb.TxnRequest{Success: tt.txnSuccess}
  175. _, err := kvc.Txn(context.Background(), txn)
  176. if err != tt.werr {
  177. t.Errorf("#%d: err = %v, want %v", i, err, tt.werr)
  178. }
  179. }
  180. }
  181. // TestV3PutMissingLease ensures that a Put on a key with a bogus lease fails.
  182. func TestV3PutMissingLease(t *testing.T) {
  183. defer testutil.AfterTest(t)
  184. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  185. defer clus.Terminate(t)
  186. kvc := toGRPC(clus.RandClient()).KV
  187. key := []byte("foo")
  188. preq := &pb.PutRequest{Key: key, Lease: 123456}
  189. tests := []func(){
  190. // put case
  191. func() {
  192. if presp, err := kvc.Put(context.TODO(), preq); err == nil {
  193. t.Errorf("succeeded put key. req: %v. resp: %v", preq, presp)
  194. }
  195. },
  196. // txn success case
  197. func() {
  198. txn := &pb.TxnRequest{}
  199. txn.Success = append(txn.Success, &pb.RequestUnion{
  200. Request: &pb.RequestUnion_RequestPut{
  201. RequestPut: preq}})
  202. if tresp, err := kvc.Txn(context.TODO(), txn); err == nil {
  203. t.Errorf("succeeded txn success. req: %v. resp: %v", txn, tresp)
  204. }
  205. },
  206. // txn failure case
  207. func() {
  208. txn := &pb.TxnRequest{}
  209. txn.Failure = append(txn.Failure, &pb.RequestUnion{
  210. Request: &pb.RequestUnion_RequestPut{
  211. RequestPut: preq}})
  212. cmp := &pb.Compare{
  213. Result: pb.Compare_GREATER,
  214. Target: pb.Compare_CREATE,
  215. Key: []byte("bar"),
  216. }
  217. txn.Compare = append(txn.Compare, cmp)
  218. if tresp, err := kvc.Txn(context.TODO(), txn); err == nil {
  219. t.Errorf("succeeded txn failure. req: %v. resp: %v", txn, tresp)
  220. }
  221. },
  222. // ignore bad lease in failure on success txn
  223. func() {
  224. txn := &pb.TxnRequest{}
  225. rreq := &pb.RangeRequest{Key: []byte("bar")}
  226. txn.Success = append(txn.Success, &pb.RequestUnion{
  227. Request: &pb.RequestUnion_RequestRange{
  228. RequestRange: rreq}})
  229. txn.Failure = append(txn.Failure, &pb.RequestUnion{
  230. Request: &pb.RequestUnion_RequestPut{
  231. RequestPut: preq}})
  232. if tresp, err := kvc.Txn(context.TODO(), txn); err != nil {
  233. t.Errorf("failed good txn. req: %v. resp: %v", txn, tresp)
  234. }
  235. },
  236. }
  237. for i, f := range tests {
  238. f()
  239. // key shouldn't have been stored
  240. rreq := &pb.RangeRequest{Key: key}
  241. rresp, err := kvc.Range(context.TODO(), rreq)
  242. if err != nil {
  243. t.Errorf("#%d. could not rangereq (%v)", i, err)
  244. } else if len(rresp.Kvs) != 0 {
  245. t.Errorf("#%d. expected no keys, got %v", i, rresp)
  246. }
  247. }
  248. }
  249. // TestV3DeleteRange tests various edge cases in the DeleteRange API.
  250. func TestV3DeleteRange(t *testing.T) {
  251. defer testutil.AfterTest(t)
  252. tests := []struct {
  253. keySet []string
  254. begin string
  255. end string
  256. wantSet [][]byte
  257. deleted int64
  258. }{
  259. // delete middle
  260. {
  261. []string{"foo", "foo/abc", "fop"},
  262. "foo/", "fop",
  263. [][]byte{[]byte("foo"), []byte("fop")}, 1,
  264. },
  265. // no delete
  266. {
  267. []string{"foo", "foo/abc", "fop"},
  268. "foo/", "foo/",
  269. [][]byte{[]byte("foo"), []byte("foo/abc"), []byte("fop")}, 0,
  270. },
  271. // delete first
  272. {
  273. []string{"foo", "foo/abc", "fop"},
  274. "fo", "fop",
  275. [][]byte{[]byte("fop")}, 2,
  276. },
  277. // delete tail
  278. {
  279. []string{"foo", "foo/abc", "fop"},
  280. "foo/", "fos",
  281. [][]byte{[]byte("foo")}, 2,
  282. },
  283. // delete exact
  284. {
  285. []string{"foo", "foo/abc", "fop"},
  286. "foo/abc", "",
  287. [][]byte{[]byte("foo"), []byte("fop")}, 1,
  288. },
  289. // delete none, [x,x)
  290. {
  291. []string{"foo"},
  292. "foo", "foo",
  293. [][]byte{[]byte("foo")}, 0,
  294. },
  295. }
  296. for i, tt := range tests {
  297. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  298. kvc := toGRPC(clus.RandClient()).KV
  299. ks := tt.keySet
  300. for j := range ks {
  301. reqput := &pb.PutRequest{Key: []byte(ks[j]), Value: []byte{}}
  302. _, err := kvc.Put(context.TODO(), reqput)
  303. if err != nil {
  304. t.Fatalf("couldn't put key (%v)", err)
  305. }
  306. }
  307. dreq := &pb.DeleteRangeRequest{
  308. Key: []byte(tt.begin),
  309. RangeEnd: []byte(tt.end)}
  310. dresp, err := kvc.DeleteRange(context.TODO(), dreq)
  311. if err != nil {
  312. t.Fatalf("couldn't delete range on test %d (%v)", i, err)
  313. }
  314. if tt.deleted != dresp.Deleted {
  315. t.Errorf("expected %d on test %v, got %d", tt.deleted, i, dresp.Deleted)
  316. }
  317. rreq := &pb.RangeRequest{Key: []byte{0x0}, RangeEnd: []byte{0xff}}
  318. rresp, err := kvc.Range(context.TODO(), rreq)
  319. if err != nil {
  320. t.Errorf("couldn't get range on test %v (%v)", i, err)
  321. }
  322. if dresp.Header.Revision != rresp.Header.Revision {
  323. t.Errorf("expected revision %v, got %v",
  324. dresp.Header.Revision, rresp.Header.Revision)
  325. }
  326. keys := [][]byte{}
  327. for j := range rresp.Kvs {
  328. keys = append(keys, rresp.Kvs[j].Key)
  329. }
  330. if reflect.DeepEqual(tt.wantSet, keys) == false {
  331. t.Errorf("expected %v on test %v, got %v", tt.wantSet, i, keys)
  332. }
  333. // can't defer because tcp ports will be in use
  334. clus.Terminate(t)
  335. }
  336. }
  337. // TestV3TxnInvaildRange tests txn
  338. func TestV3TxnInvaildRange(t *testing.T) {
  339. defer testutil.AfterTest(t)
  340. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  341. defer clus.Terminate(t)
  342. kvc := toGRPC(clus.RandClient()).KV
  343. preq := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  344. for i := 0; i < 3; i++ {
  345. _, err := kvc.Put(context.Background(), preq)
  346. if err != nil {
  347. t.Fatalf("couldn't put key (%v)", err)
  348. }
  349. }
  350. _, err := kvc.Compact(context.Background(), &pb.CompactionRequest{Revision: 2})
  351. if err != nil {
  352. t.Fatalf("couldn't compact kv space (%v)", err)
  353. }
  354. // future rev
  355. txn := &pb.TxnRequest{}
  356. txn.Success = append(txn.Success, &pb.RequestUnion{
  357. Request: &pb.RequestUnion_RequestPut{
  358. RequestPut: preq}})
  359. rreq := &pb.RangeRequest{Key: []byte("foo"), Revision: 100}
  360. txn.Success = append(txn.Success, &pb.RequestUnion{
  361. Request: &pb.RequestUnion_RequestRange{
  362. RequestRange: rreq}})
  363. if _, err := kvc.Txn(context.TODO(), txn); err != rpctypes.ErrFutureRev {
  364. t.Errorf("err = %v, want %v", err, rpctypes.ErrFutureRev)
  365. }
  366. // compacted rev
  367. tv, _ := txn.Success[1].Request.(*pb.RequestUnion_RequestRange)
  368. tv.RequestRange.Revision = 1
  369. if _, err := kvc.Txn(context.TODO(), txn); err != rpctypes.ErrCompacted {
  370. t.Errorf("err = %v, want %v", err, rpctypes.ErrCompacted)
  371. }
  372. }
  373. func TestV3TooLargeRequest(t *testing.T) {
  374. defer testutil.AfterTest(t)
  375. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  376. defer clus.Terminate(t)
  377. kvc := toGRPC(clus.RandClient()).KV
  378. // 2MB request value
  379. largeV := make([]byte, 2*1024*1024)
  380. preq := &pb.PutRequest{Key: []byte("foo"), Value: largeV}
  381. _, err := kvc.Put(context.Background(), preq)
  382. if err != rpctypes.ErrRequestTooLarge {
  383. t.Errorf("err = %v, want %v", err, rpctypes.ErrRequestTooLarge)
  384. }
  385. }
  386. // TestV3Hash tests hash.
  387. func TestV3Hash(t *testing.T) {
  388. defer testutil.AfterTest(t)
  389. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  390. defer clus.Terminate(t)
  391. kvc := toGRPC(clus.RandClient()).KV
  392. preq := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  393. for i := 0; i < 3; i++ {
  394. _, err := kvc.Put(context.Background(), preq)
  395. if err != nil {
  396. t.Fatalf("couldn't put key (%v)", err)
  397. }
  398. }
  399. resp, err := kvc.Hash(context.Background(), &pb.HashRequest{})
  400. if err != nil || resp.Hash == 0 {
  401. t.Fatalf("couldn't hash (%v, hash %d)", err, resp.Hash)
  402. }
  403. }
  404. func TestV3RangeRequest(t *testing.T) {
  405. defer testutil.AfterTest(t)
  406. tests := []struct {
  407. putKeys []string
  408. reqs []pb.RangeRequest
  409. wresps [][]string
  410. wmores []bool
  411. }{
  412. // single key
  413. {
  414. []string{"foo", "bar"},
  415. []pb.RangeRequest{
  416. // exists
  417. {Key: []byte("foo")},
  418. // doesn't exist
  419. {Key: []byte("baz")},
  420. },
  421. [][]string{
  422. {"foo"},
  423. {},
  424. },
  425. []bool{false, false},
  426. },
  427. // multi-key
  428. {
  429. []string{"a", "b", "c", "d", "e"},
  430. []pb.RangeRequest{
  431. // all in range
  432. {Key: []byte("a"), RangeEnd: []byte("z")},
  433. // [b, d)
  434. {Key: []byte("b"), RangeEnd: []byte("d")},
  435. // out of range
  436. {Key: []byte("f"), RangeEnd: []byte("z")},
  437. // [c,c) = empty
  438. {Key: []byte("c"), RangeEnd: []byte("c")},
  439. // [d, b) = empty
  440. {Key: []byte("d"), RangeEnd: []byte("b")},
  441. // ["\0", "\0") => all in range
  442. {Key: []byte{0}, RangeEnd: []byte{0}},
  443. },
  444. [][]string{
  445. {"a", "b", "c", "d", "e"},
  446. {"b", "c"},
  447. {},
  448. {},
  449. {},
  450. {"a", "b", "c", "d", "e"},
  451. },
  452. []bool{false, false, false, false, false, false},
  453. },
  454. // revision
  455. {
  456. []string{"a", "b", "c", "d", "e"},
  457. []pb.RangeRequest{
  458. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 0},
  459. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 1},
  460. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 2},
  461. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 3},
  462. },
  463. [][]string{
  464. {"a", "b", "c", "d", "e"},
  465. {},
  466. {"a"},
  467. {"a", "b"},
  468. },
  469. []bool{false, false, false, false},
  470. },
  471. // limit
  472. {
  473. []string{"foo", "bar"},
  474. []pb.RangeRequest{
  475. // more
  476. {Key: []byte("a"), RangeEnd: []byte("z"), Limit: 1},
  477. // no more
  478. {Key: []byte("a"), RangeEnd: []byte("z"), Limit: 2},
  479. },
  480. [][]string{
  481. {"bar"},
  482. {"bar", "foo"},
  483. },
  484. []bool{true, false},
  485. },
  486. // sort
  487. {
  488. []string{"b", "a", "c", "d", "c"},
  489. []pb.RangeRequest{
  490. {
  491. Key: []byte("a"), RangeEnd: []byte("z"),
  492. Limit: 1,
  493. SortOrder: pb.RangeRequest_ASCEND,
  494. SortTarget: pb.RangeRequest_KEY,
  495. },
  496. {
  497. Key: []byte("a"), RangeEnd: []byte("z"),
  498. Limit: 1,
  499. SortOrder: pb.RangeRequest_DESCEND,
  500. SortTarget: pb.RangeRequest_KEY,
  501. },
  502. {
  503. Key: []byte("a"), RangeEnd: []byte("z"),
  504. Limit: 1,
  505. SortOrder: pb.RangeRequest_ASCEND,
  506. SortTarget: pb.RangeRequest_CREATE,
  507. },
  508. {
  509. Key: []byte("a"), RangeEnd: []byte("z"),
  510. Limit: 1,
  511. SortOrder: pb.RangeRequest_DESCEND,
  512. SortTarget: pb.RangeRequest_MOD,
  513. },
  514. {
  515. Key: []byte("z"), RangeEnd: []byte("z"),
  516. Limit: 1,
  517. SortOrder: pb.RangeRequest_DESCEND,
  518. SortTarget: pb.RangeRequest_CREATE,
  519. },
  520. },
  521. [][]string{
  522. {"a"},
  523. {"d"},
  524. {"b"},
  525. {"c"},
  526. {},
  527. },
  528. []bool{true, true, true, true, false},
  529. },
  530. }
  531. for i, tt := range tests {
  532. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  533. for _, k := range tt.putKeys {
  534. kvc := toGRPC(clus.RandClient()).KV
  535. req := &pb.PutRequest{Key: []byte(k), Value: []byte("bar")}
  536. if _, err := kvc.Put(context.TODO(), req); err != nil {
  537. t.Fatalf("#%d: couldn't put key (%v)", i, err)
  538. }
  539. }
  540. for j, req := range tt.reqs {
  541. kvc := toGRPC(clus.RandClient()).KV
  542. resp, err := kvc.Range(context.TODO(), &req)
  543. if err != nil {
  544. t.Errorf("#%d.%d: Range error: %v", i, j, err)
  545. continue
  546. }
  547. if len(resp.Kvs) != len(tt.wresps[j]) {
  548. t.Errorf("#%d.%d: bad len(resp.Kvs). got = %d, want = %d, ", i, j, len(resp.Kvs), len(tt.wresps[j]))
  549. continue
  550. }
  551. for k, wKey := range tt.wresps[j] {
  552. respKey := string(resp.Kvs[k].Key)
  553. if respKey != wKey {
  554. t.Errorf("#%d.%d: key[%d]. got = %v, want = %v, ", i, j, k, respKey, wKey)
  555. }
  556. }
  557. if resp.More != tt.wmores[j] {
  558. t.Errorf("#%d.%d: bad more. got = %v, want = %v, ", i, j, resp.More, tt.wmores[j])
  559. }
  560. wrev := int64(len(tt.putKeys) + 1)
  561. if resp.Header.Revision != wrev {
  562. t.Errorf("#%d.%d: bad header revision. got = %d. want = %d", i, j, resp.Header.Revision, wrev)
  563. }
  564. }
  565. clus.Terminate(t)
  566. }
  567. }
  568. func newClusterV3NoClients(t *testing.T, cfg *ClusterConfig) *ClusterV3 {
  569. cfg.UseV3 = true
  570. cfg.UseGRPC = true
  571. clus := &ClusterV3{cluster: NewClusterByConfig(t, cfg)}
  572. clus.Launch(t)
  573. return clus
  574. }
  575. // TestTLSGRPCRejectInsecureClient checks that connection is rejected if server is TLS but not client.
  576. func TestTLSGRPCRejectInsecureClient(t *testing.T) {
  577. defer testutil.AfterTest(t)
  578. cfg := ClusterConfig{Size: 3, ClientTLS: &testTLSInfo}
  579. clus := newClusterV3NoClients(t, &cfg)
  580. defer clus.Terminate(t)
  581. // nil out TLS field so client will use an insecure connection
  582. clus.Members[0].ClientTLSInfo = nil
  583. client, err := NewClientV3(clus.Members[0])
  584. if err != nil && err != grpc.ErrClientConnTimeout {
  585. t.Fatalf("unexpected error (%v)", err)
  586. } else if client == nil {
  587. // Ideally, no client would be returned. However, grpc will
  588. // return a connection without trying to handshake first so
  589. // the connection appears OK.
  590. return
  591. }
  592. defer client.Close()
  593. ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
  594. conn := client.ActiveConnection()
  595. st, err := conn.State()
  596. if err != nil {
  597. t.Fatal(err)
  598. } else if st != grpc.Ready {
  599. t.Fatalf("expected Ready, got %v", st)
  600. }
  601. // rpc will fail to handshake, triggering a connection state change
  602. donec := make(chan error, 1)
  603. go func() {
  604. reqput := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  605. _, perr := toGRPC(client).KV.Put(ctx, reqput)
  606. donec <- perr
  607. }()
  608. st, err = conn.WaitForStateChange(ctx, st)
  609. if err != nil {
  610. t.Fatalf("unexpected error waiting for change (%v)", err)
  611. } else if st == grpc.Ready {
  612. t.Fatalf("expected failure state, got %v", st)
  613. }
  614. cancel()
  615. if perr := <-donec; perr == nil {
  616. t.Fatalf("expected client error on put")
  617. }
  618. }
  619. // TestTLSGRPCRejectSecureClient checks that connection is rejected if client is TLS but not server.
  620. func TestTLSGRPCRejectSecureClient(t *testing.T) {
  621. defer testutil.AfterTest(t)
  622. cfg := ClusterConfig{Size: 3}
  623. clus := newClusterV3NoClients(t, &cfg)
  624. defer clus.Terminate(t)
  625. clus.Members[0].ClientTLSInfo = &testTLSInfo
  626. client, err := NewClientV3(clus.Members[0])
  627. if client != nil || err == nil {
  628. t.Fatalf("expected no client")
  629. } else if err != grpc.ErrClientConnTimeout {
  630. t.Fatalf("unexpected error (%v)", err)
  631. }
  632. }
  633. // TestTLSGRPCAcceptSecureAll checks that connection is accepted if both client and server are TLS
  634. func TestTLSGRPCAcceptSecureAll(t *testing.T) {
  635. defer testutil.AfterTest(t)
  636. cfg := ClusterConfig{Size: 3, ClientTLS: &testTLSInfo}
  637. clus := newClusterV3NoClients(t, &cfg)
  638. defer clus.Terminate(t)
  639. client, err := NewClientV3(clus.Members[0])
  640. if err != nil {
  641. t.Fatalf("expected tls client (%v)", err)
  642. }
  643. defer client.Close()
  644. reqput := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  645. if _, err := toGRPC(client).KV.Put(context.TODO(), reqput); err != nil {
  646. t.Fatalf("unexpected error on put over tls (%v)", err)
  647. }
  648. }