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