v3_grpc_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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 := 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 := 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 := 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 := 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. }{
  257. // delete middle
  258. {
  259. []string{"foo", "foo/abc", "fop"},
  260. "foo/", "fop",
  261. [][]byte{[]byte("foo"), []byte("fop")},
  262. },
  263. // no delete
  264. {
  265. []string{"foo", "foo/abc", "fop"},
  266. "foo/", "foo/",
  267. [][]byte{[]byte("foo"), []byte("foo/abc"), []byte("fop")},
  268. },
  269. // delete first
  270. {
  271. []string{"foo", "foo/abc", "fop"},
  272. "fo", "fop",
  273. [][]byte{[]byte("fop")},
  274. },
  275. // delete tail
  276. {
  277. []string{"foo", "foo/abc", "fop"},
  278. "foo/", "fos",
  279. [][]byte{[]byte("foo")},
  280. },
  281. // delete exact
  282. {
  283. []string{"foo", "foo/abc", "fop"},
  284. "foo/abc", "",
  285. [][]byte{[]byte("foo"), []byte("fop")},
  286. },
  287. // delete none, [x,x)
  288. {
  289. []string{"foo"},
  290. "foo", "foo",
  291. [][]byte{[]byte("foo")},
  292. },
  293. }
  294. for i, tt := range tests {
  295. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  296. kvc := clus.RandClient().KV
  297. ks := tt.keySet
  298. for j := range ks {
  299. reqput := &pb.PutRequest{Key: []byte(ks[j]), Value: []byte{}}
  300. _, err := kvc.Put(context.TODO(), reqput)
  301. if err != nil {
  302. t.Fatalf("couldn't put key (%v)", err)
  303. }
  304. }
  305. dreq := &pb.DeleteRangeRequest{
  306. Key: []byte(tt.begin),
  307. RangeEnd: []byte(tt.end)}
  308. dresp, err := kvc.DeleteRange(context.TODO(), dreq)
  309. if err != nil {
  310. t.Fatalf("couldn't delete range on test %d (%v)", i, err)
  311. }
  312. rreq := &pb.RangeRequest{Key: []byte{0x0}, RangeEnd: []byte{0xff}}
  313. rresp, err := kvc.Range(context.TODO(), rreq)
  314. if err != nil {
  315. t.Errorf("couldn't get range on test %v (%v)", i, err)
  316. }
  317. if dresp.Header.Revision != rresp.Header.Revision {
  318. t.Errorf("expected revision %v, got %v",
  319. dresp.Header.Revision, rresp.Header.Revision)
  320. }
  321. keys := [][]byte{}
  322. for j := range rresp.Kvs {
  323. keys = append(keys, rresp.Kvs[j].Key)
  324. }
  325. if reflect.DeepEqual(tt.wantSet, keys) == false {
  326. t.Errorf("expected %v on test %v, got %v", tt.wantSet, i, keys)
  327. }
  328. // can't defer because tcp ports will be in use
  329. clus.Terminate(t)
  330. }
  331. }
  332. // TestV3TxnInvaildRange tests txn
  333. func TestV3TxnInvaildRange(t *testing.T) {
  334. defer testutil.AfterTest(t)
  335. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  336. defer clus.Terminate(t)
  337. kvc := clus.RandClient().KV
  338. preq := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  339. for i := 0; i < 3; i++ {
  340. _, err := kvc.Put(context.Background(), preq)
  341. if err != nil {
  342. t.Fatalf("couldn't put key (%v)", err)
  343. }
  344. }
  345. _, err := kvc.Compact(context.Background(), &pb.CompactionRequest{Revision: 2})
  346. if err != nil {
  347. t.Fatalf("couldn't compact kv space (%v)", err)
  348. }
  349. // future rev
  350. txn := &pb.TxnRequest{}
  351. txn.Success = append(txn.Success, &pb.RequestUnion{
  352. Request: &pb.RequestUnion_RequestPut{
  353. RequestPut: preq}})
  354. rreq := &pb.RangeRequest{Key: []byte("foo"), Revision: 100}
  355. txn.Success = append(txn.Success, &pb.RequestUnion{
  356. Request: &pb.RequestUnion_RequestRange{
  357. RequestRange: rreq}})
  358. if _, err := kvc.Txn(context.TODO(), txn); err != v3rpc.ErrFutureRev {
  359. t.Errorf("err = %v, want %v", err, v3rpc.ErrFutureRev)
  360. }
  361. // compacted rev
  362. tv, _ := txn.Success[1].Request.(*pb.RequestUnion_RequestRange)
  363. tv.RequestRange.Revision = 1
  364. if _, err := kvc.Txn(context.TODO(), txn); err != v3rpc.ErrCompacted {
  365. t.Errorf("err = %v, want %v", err, v3rpc.ErrCompacted)
  366. }
  367. }
  368. func TestV3TooLargeRequest(t *testing.T) {
  369. defer testutil.AfterTest(t)
  370. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  371. defer clus.Terminate(t)
  372. kvc := clus.RandClient().KV
  373. // 2MB request value
  374. largeV := make([]byte, 2*1024*1024)
  375. preq := &pb.PutRequest{Key: []byte("foo"), Value: largeV}
  376. _, err := kvc.Put(context.Background(), preq)
  377. if err != v3rpc.ErrRequestTooLarge {
  378. t.Errorf("err = %v, want %v", err, v3rpc.ErrRequestTooLarge)
  379. }
  380. }
  381. // TestV3Hash tests hash.
  382. func TestV3Hash(t *testing.T) {
  383. defer testutil.AfterTest(t)
  384. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  385. defer clus.Terminate(t)
  386. kvc := clus.RandClient().KV
  387. preq := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  388. for i := 0; i < 3; i++ {
  389. _, err := kvc.Put(context.Background(), preq)
  390. if err != nil {
  391. t.Fatalf("couldn't put key (%v)", err)
  392. }
  393. }
  394. resp, err := kvc.Hash(context.Background(), &pb.HashRequest{})
  395. if err != nil || resp.Hash == 0 {
  396. t.Fatalf("couldn't hash (%v, hash %d)", err, resp.Hash)
  397. }
  398. }
  399. func TestV3RangeRequest(t *testing.T) {
  400. defer testutil.AfterTest(t)
  401. tests := []struct {
  402. putKeys []string
  403. reqs []pb.RangeRequest
  404. wresps [][]string
  405. wmores []bool
  406. }{
  407. // single key
  408. {
  409. []string{"foo", "bar"},
  410. []pb.RangeRequest{
  411. // exists
  412. {Key: []byte("foo")},
  413. // doesn't exist
  414. {Key: []byte("baz")},
  415. },
  416. [][]string{
  417. {"foo"},
  418. {},
  419. },
  420. []bool{false, false},
  421. },
  422. // multi-key
  423. {
  424. []string{"a", "b", "c", "d", "e"},
  425. []pb.RangeRequest{
  426. // all in range
  427. {Key: []byte("a"), RangeEnd: []byte("z")},
  428. // [b, d)
  429. {Key: []byte("b"), RangeEnd: []byte("d")},
  430. // out of range
  431. {Key: []byte("f"), RangeEnd: []byte("z")},
  432. // [c,c) = empty
  433. {Key: []byte("c"), RangeEnd: []byte("c")},
  434. // [d, b) = empty
  435. {Key: []byte("d"), RangeEnd: []byte("b")},
  436. // ["\0", "\0") => all in range
  437. {Key: []byte{0}, RangeEnd: []byte{0}},
  438. },
  439. [][]string{
  440. {"a", "b", "c", "d", "e"},
  441. {"b", "c"},
  442. {},
  443. {},
  444. {},
  445. {"a", "b", "c", "d", "e"},
  446. },
  447. []bool{false, false, false, false, false, false},
  448. },
  449. // revision
  450. {
  451. []string{"a", "b", "c", "d", "e"},
  452. []pb.RangeRequest{
  453. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 0},
  454. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 1},
  455. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 2},
  456. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 3},
  457. },
  458. [][]string{
  459. {"a", "b", "c", "d", "e"},
  460. {},
  461. {"a"},
  462. {"a", "b"},
  463. },
  464. []bool{false, false, false, false},
  465. },
  466. // limit
  467. {
  468. []string{"foo", "bar"},
  469. []pb.RangeRequest{
  470. // more
  471. {Key: []byte("a"), RangeEnd: []byte("z"), Limit: 1},
  472. // no more
  473. {Key: []byte("a"), RangeEnd: []byte("z"), Limit: 2},
  474. },
  475. [][]string{
  476. {"bar"},
  477. {"bar", "foo"},
  478. },
  479. []bool{true, false},
  480. },
  481. // sort
  482. {
  483. []string{"b", "a", "c", "d", "c"},
  484. []pb.RangeRequest{
  485. {
  486. Key: []byte("a"), RangeEnd: []byte("z"),
  487. Limit: 1,
  488. SortOrder: pb.RangeRequest_ASCEND,
  489. SortTarget: pb.RangeRequest_KEY,
  490. },
  491. {
  492. Key: []byte("a"), RangeEnd: []byte("z"),
  493. Limit: 1,
  494. SortOrder: pb.RangeRequest_DESCEND,
  495. SortTarget: pb.RangeRequest_KEY,
  496. },
  497. {
  498. Key: []byte("a"), RangeEnd: []byte("z"),
  499. Limit: 1,
  500. SortOrder: pb.RangeRequest_ASCEND,
  501. SortTarget: pb.RangeRequest_CREATE,
  502. },
  503. {
  504. Key: []byte("a"), RangeEnd: []byte("z"),
  505. Limit: 1,
  506. SortOrder: pb.RangeRequest_DESCEND,
  507. SortTarget: pb.RangeRequest_MOD,
  508. },
  509. {
  510. Key: []byte("z"), RangeEnd: []byte("z"),
  511. Limit: 1,
  512. SortOrder: pb.RangeRequest_DESCEND,
  513. SortTarget: pb.RangeRequest_CREATE,
  514. },
  515. },
  516. [][]string{
  517. {"a"},
  518. {"d"},
  519. {"b"},
  520. {"c"},
  521. {},
  522. },
  523. []bool{true, true, true, true, false},
  524. },
  525. }
  526. for i, tt := range tests {
  527. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  528. for _, k := range tt.putKeys {
  529. kvc := clus.RandClient().KV
  530. req := &pb.PutRequest{Key: []byte(k), Value: []byte("bar")}
  531. if _, err := kvc.Put(context.TODO(), req); err != nil {
  532. t.Fatalf("#%d: couldn't put key (%v)", i, err)
  533. }
  534. }
  535. for j, req := range tt.reqs {
  536. kvc := clus.RandClient().KV
  537. resp, err := kvc.Range(context.TODO(), &req)
  538. if err != nil {
  539. t.Errorf("#%d.%d: Range error: %v", i, j, err)
  540. continue
  541. }
  542. if len(resp.Kvs) != len(tt.wresps[j]) {
  543. t.Errorf("#%d.%d: bad len(resp.Kvs). got = %d, want = %d, ", i, j, len(resp.Kvs), len(tt.wresps[j]))
  544. continue
  545. }
  546. for k, wKey := range tt.wresps[j] {
  547. respKey := string(resp.Kvs[k].Key)
  548. if respKey != wKey {
  549. t.Errorf("#%d.%d: key[%d]. got = %v, want = %v, ", i, j, k, respKey, wKey)
  550. }
  551. }
  552. if resp.More != tt.wmores[j] {
  553. t.Errorf("#%d.%d: bad more. got = %v, want = %v, ", i, j, resp.More, tt.wmores[j])
  554. }
  555. wrev := int64(len(tt.putKeys) + 1)
  556. if resp.Header.Revision != wrev {
  557. t.Errorf("#%d.%d: bad header revision. got = %d. want = %d", i, j, resp.Header.Revision, wrev)
  558. }
  559. }
  560. clus.Terminate(t)
  561. }
  562. }
  563. func newClusterV3NoClients(t *testing.T, cfg *ClusterConfig) *ClusterV3 {
  564. cfg.UseV3 = true
  565. cfg.UseGRPC = true
  566. clus := &ClusterV3{cluster: NewClusterByConfig(t, cfg)}
  567. clus.Launch(t)
  568. return clus
  569. }
  570. // TestTLSGRPCRejectInsecureClient checks that connection is rejected if server is TLS but not client.
  571. func TestTLSGRPCRejectInsecureClient(t *testing.T) {
  572. defer testutil.AfterTest(t)
  573. cfg := ClusterConfig{Size: 3, ClientTLS: &testTLSInfo}
  574. clus := newClusterV3NoClients(t, &cfg)
  575. defer clus.Terminate(t)
  576. // nil out TLS field so client will use an insecure connection
  577. clus.Members[0].ClientTLSInfo = nil
  578. client, err := NewClientV3(clus.Members[0])
  579. if err != nil && err != grpc.ErrClientConnTimeout {
  580. t.Fatalf("unexpected error (%v)", err)
  581. } else if client == nil {
  582. // Ideally, no client would be returned. However, grpc will
  583. // return a connection without trying to handshake first so
  584. // the connection appears OK.
  585. return
  586. }
  587. defer client.Close()
  588. ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
  589. conn := client.ActiveConnection()
  590. st, err := conn.State()
  591. if err != nil {
  592. t.Fatal(err)
  593. } else if st != grpc.Ready {
  594. t.Fatalf("expected Ready, got %v", st)
  595. }
  596. // rpc will fail to handshake, triggering a connection state change
  597. donec := make(chan error, 1)
  598. go func() {
  599. reqput := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  600. _, perr := client.KV.Put(ctx, reqput)
  601. donec <- perr
  602. }()
  603. st, err = conn.WaitForStateChange(ctx, st)
  604. if err != nil {
  605. t.Fatalf("unexpected error waiting for change (%v)", err)
  606. } else if st != grpc.Connecting && st != grpc.TransientFailure {
  607. t.Fatalf("expected connecting or transient failure state, got %v", st)
  608. }
  609. cancel()
  610. if perr := <-donec; perr == nil {
  611. t.Fatalf("expected client error on put")
  612. }
  613. }
  614. // TestTLSGRPCRejectSecureClient checks that connection is rejected if client is TLS but not server.
  615. func TestTLSGRPCRejectSecureClient(t *testing.T) {
  616. defer testutil.AfterTest(t)
  617. cfg := ClusterConfig{Size: 3}
  618. clus := newClusterV3NoClients(t, &cfg)
  619. defer clus.Terminate(t)
  620. clus.Members[0].ClientTLSInfo = &testTLSInfo
  621. client, err := NewClientV3(clus.Members[0])
  622. if client != nil || err == nil {
  623. t.Fatalf("expected no client")
  624. } else if err != grpc.ErrClientConnTimeout {
  625. t.Fatalf("unexpected error (%v)", err)
  626. }
  627. }
  628. // TestTLSGRPCAcceptSecureAll checks that connection is accepted if both client and server are TLS
  629. func TestTLSGRPCAcceptSecureAll(t *testing.T) {
  630. defer testutil.AfterTest(t)
  631. cfg := ClusterConfig{Size: 3, ClientTLS: &testTLSInfo}
  632. clus := newClusterV3NoClients(t, &cfg)
  633. defer clus.Terminate(t)
  634. client, err := NewClientV3(clus.Members[0])
  635. if err != nil {
  636. t.Fatalf("expected tls client (%v)", err)
  637. }
  638. defer client.Close()
  639. reqput := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  640. if _, err := client.KV.Put(context.TODO(), reqput); err != nil {
  641. t.Fatalf("unexpected error on put over tls (%v)", err)
  642. }
  643. }