v3_grpc_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. },
  437. [][]string{
  438. {"a", "b", "c", "d", "e"},
  439. {"b", "c"},
  440. {},
  441. {},
  442. {},
  443. },
  444. []bool{false, false, false, false, false},
  445. },
  446. // revision
  447. {
  448. []string{"a", "b", "c", "d", "e"},
  449. []pb.RangeRequest{
  450. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 0},
  451. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 1},
  452. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 2},
  453. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 3},
  454. },
  455. [][]string{
  456. {"a", "b", "c", "d", "e"},
  457. {},
  458. {"a"},
  459. {"a", "b"},
  460. },
  461. []bool{false, false, false, false},
  462. },
  463. // limit
  464. {
  465. []string{"foo", "bar"},
  466. []pb.RangeRequest{
  467. // more
  468. {Key: []byte("a"), RangeEnd: []byte("z"), Limit: 1},
  469. // no more
  470. {Key: []byte("a"), RangeEnd: []byte("z"), Limit: 2},
  471. },
  472. [][]string{
  473. {"bar"},
  474. {"bar", "foo"},
  475. },
  476. []bool{true, false},
  477. },
  478. // sort
  479. {
  480. []string{"b", "a", "c", "d", "c"},
  481. []pb.RangeRequest{
  482. {
  483. Key: []byte("a"), RangeEnd: []byte("z"),
  484. Limit: 1,
  485. SortOrder: pb.RangeRequest_ASCEND,
  486. SortTarget: pb.RangeRequest_KEY,
  487. },
  488. {
  489. Key: []byte("a"), RangeEnd: []byte("z"),
  490. Limit: 1,
  491. SortOrder: pb.RangeRequest_DESCEND,
  492. SortTarget: pb.RangeRequest_KEY,
  493. },
  494. {
  495. Key: []byte("a"), RangeEnd: []byte("z"),
  496. Limit: 1,
  497. SortOrder: pb.RangeRequest_ASCEND,
  498. SortTarget: pb.RangeRequest_CREATE,
  499. },
  500. {
  501. Key: []byte("a"), RangeEnd: []byte("z"),
  502. Limit: 1,
  503. SortOrder: pb.RangeRequest_DESCEND,
  504. SortTarget: pb.RangeRequest_MOD,
  505. },
  506. {
  507. Key: []byte("z"), RangeEnd: []byte("z"),
  508. Limit: 1,
  509. SortOrder: pb.RangeRequest_DESCEND,
  510. SortTarget: pb.RangeRequest_CREATE,
  511. },
  512. },
  513. [][]string{
  514. {"a"},
  515. {"d"},
  516. {"b"},
  517. {"c"},
  518. {},
  519. },
  520. []bool{true, true, true, true, false},
  521. },
  522. }
  523. for i, tt := range tests {
  524. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  525. for _, k := range tt.putKeys {
  526. kvc := clus.RandClient().KV
  527. req := &pb.PutRequest{Key: []byte(k), Value: []byte("bar")}
  528. if _, err := kvc.Put(context.TODO(), req); err != nil {
  529. t.Fatalf("#%d: couldn't put key (%v)", i, err)
  530. }
  531. }
  532. for j, req := range tt.reqs {
  533. kvc := clus.RandClient().KV
  534. resp, err := kvc.Range(context.TODO(), &req)
  535. if err != nil {
  536. t.Errorf("#%d.%d: Range error: %v", i, j, err)
  537. continue
  538. }
  539. if len(resp.Kvs) != len(tt.wresps[j]) {
  540. t.Errorf("#%d.%d: bad len(resp.Kvs). got = %d, want = %d, ", i, j, len(resp.Kvs), len(tt.wresps[j]))
  541. continue
  542. }
  543. for k, wKey := range tt.wresps[j] {
  544. respKey := string(resp.Kvs[k].Key)
  545. if respKey != wKey {
  546. t.Errorf("#%d.%d: key[%d]. got = %v, want = %v, ", i, j, k, respKey, wKey)
  547. }
  548. }
  549. if resp.More != tt.wmores[j] {
  550. t.Errorf("#%d.%d: bad more. got = %v, want = %v, ", i, j, resp.More, tt.wmores[j])
  551. }
  552. wrev := int64(len(tt.putKeys) + 1)
  553. if resp.Header.Revision != wrev {
  554. t.Errorf("#%d.%d: bad header revision. got = %d. want = %d", i, j, resp.Header.Revision, wrev)
  555. }
  556. }
  557. clus.Terminate(t)
  558. }
  559. }
  560. func newClusterV3NoClients(t *testing.T, cfg *ClusterConfig) *ClusterV3 {
  561. cfg.UseV3 = true
  562. cfg.UseGRPC = true
  563. clus := &ClusterV3{cluster: NewClusterByConfig(t, cfg)}
  564. clus.Launch(t)
  565. return clus
  566. }
  567. // TestTLSGRPCRejectInsecureClient checks that connection is rejected if server is TLS but not client.
  568. func TestTLSGRPCRejectInsecureClient(t *testing.T) {
  569. defer testutil.AfterTest(t)
  570. cfg := ClusterConfig{Size: 3, ClientTLS: &testTLSInfo}
  571. clus := newClusterV3NoClients(t, &cfg)
  572. defer clus.Terminate(t)
  573. // nil out TLS field so client will use an insecure connection
  574. clus.Members[0].ClientTLSInfo = nil
  575. client, err := NewClientV3(clus.Members[0])
  576. if err != nil && err != grpc.ErrClientConnTimeout {
  577. t.Fatalf("unexpected error (%v)", err)
  578. } else if client == nil {
  579. // Ideally, no client would be returned. However, grpc will
  580. // return a connection without trying to handshake first so
  581. // the connection appears OK.
  582. return
  583. }
  584. defer client.Close()
  585. ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
  586. conn := client.ActiveConnection()
  587. st, err := conn.State()
  588. if err != nil {
  589. t.Fatal(err)
  590. } else if st != grpc.Ready {
  591. t.Fatalf("expected Ready, got %v", st)
  592. }
  593. // rpc will fail to handshake, triggering a connection state change
  594. donec := make(chan error, 1)
  595. go func() {
  596. reqput := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  597. _, perr := client.KV.Put(ctx, reqput)
  598. donec <- perr
  599. }()
  600. st, err = conn.WaitForStateChange(ctx, st)
  601. if err != nil {
  602. t.Fatalf("unexpected error waiting for change (%v)", err)
  603. } else if st != grpc.Connecting && st != grpc.TransientFailure {
  604. t.Fatalf("expected connecting or transient failure state, got %v", st)
  605. }
  606. cancel()
  607. if perr := <-donec; perr == nil {
  608. t.Fatalf("expected client error on put")
  609. }
  610. }
  611. // TestTLSGRPCRejectSecureClient checks that connection is rejected if client is TLS but not server.
  612. func TestTLSGRPCRejectSecureClient(t *testing.T) {
  613. defer testutil.AfterTest(t)
  614. cfg := ClusterConfig{Size: 3}
  615. clus := newClusterV3NoClients(t, &cfg)
  616. defer clus.Terminate(t)
  617. clus.Members[0].ClientTLSInfo = &testTLSInfo
  618. client, err := NewClientV3(clus.Members[0])
  619. if client != nil || err == nil {
  620. t.Fatalf("expected no client")
  621. } else if err != grpc.ErrClientConnTimeout {
  622. t.Fatalf("unexpected error (%v)", err)
  623. }
  624. }
  625. // TestTLSGRPCAcceptSecureAll checks that connection is accepted if both client and server are TLS
  626. func TestTLSGRPCAcceptSecureAll(t *testing.T) {
  627. defer testutil.AfterTest(t)
  628. cfg := ClusterConfig{Size: 3, ClientTLS: &testTLSInfo}
  629. clus := newClusterV3NoClients(t, &cfg)
  630. defer clus.Terminate(t)
  631. client, err := NewClientV3(clus.Members[0])
  632. if err != nil {
  633. t.Fatalf("expected tls client (%v)", err)
  634. }
  635. defer client.Close()
  636. reqput := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  637. if _, err := client.KV.Put(context.TODO(), reqput); err != nil {
  638. t.Fatalf("unexpected error on put over tls (%v)", err)
  639. }
  640. }