v3_grpc_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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.
  14. package integration
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/etcdserver/api/v3rpc"
  21. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  22. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  23. "github.com/coreos/etcd/pkg/testutil"
  24. "golang.org/x/net/context"
  25. "google.golang.org/grpc"
  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. // Testv3TxnRevision tests that the transaction header revision is set as expected.
  182. func TestV3TxnRevision(t *testing.T) {
  183. defer testutil.AfterTest(t)
  184. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  185. defer clus.Terminate(t)
  186. kvc := toGRPC(clus.RandClient()).KV
  187. pr := &pb.PutRequest{Key: []byte("abc"), Value: []byte("def")}
  188. presp, err := kvc.Put(context.TODO(), pr)
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. txnget := &pb.RequestUnion{Request: &pb.RequestUnion_RequestRange{RequestRange: &pb.RangeRequest{Key: []byte("abc")}}}
  193. txn := &pb.TxnRequest{Success: []*pb.RequestUnion{txnget}}
  194. tresp, err := kvc.Txn(context.TODO(), txn)
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. // did not update revision
  199. if presp.Header.Revision != tresp.Header.Revision {
  200. t.Fatalf("got rev %d, wanted rev %d", tresp.Header.Revision, presp.Header.Revision)
  201. }
  202. txnput := &pb.RequestUnion{Request: &pb.RequestUnion_RequestPut{RequestPut: &pb.PutRequest{Key: []byte("abc"), Value: []byte("123")}}}
  203. txn = &pb.TxnRequest{Success: []*pb.RequestUnion{txnput}}
  204. tresp, err = kvc.Txn(context.TODO(), txn)
  205. if err != nil {
  206. t.Fatal(err)
  207. }
  208. // updated revision
  209. if tresp.Header.Revision != presp.Header.Revision+1 {
  210. t.Fatalf("got rev %d, wanted rev %d", tresp.Header.Revision, presp.Header.Revision+1)
  211. }
  212. }
  213. // TestV3PutMissingLease ensures that a Put on a key with a bogus lease fails.
  214. func TestV3PutMissingLease(t *testing.T) {
  215. defer testutil.AfterTest(t)
  216. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  217. defer clus.Terminate(t)
  218. kvc := toGRPC(clus.RandClient()).KV
  219. key := []byte("foo")
  220. preq := &pb.PutRequest{Key: key, Lease: 123456}
  221. tests := []func(){
  222. // put case
  223. func() {
  224. if presp, err := kvc.Put(context.TODO(), preq); err == nil {
  225. t.Errorf("succeeded put key. req: %v. resp: %v", preq, presp)
  226. }
  227. },
  228. // txn success case
  229. func() {
  230. txn := &pb.TxnRequest{}
  231. txn.Success = append(txn.Success, &pb.RequestUnion{
  232. Request: &pb.RequestUnion_RequestPut{
  233. RequestPut: preq}})
  234. if tresp, err := kvc.Txn(context.TODO(), txn); err == nil {
  235. t.Errorf("succeeded txn success. req: %v. resp: %v", txn, tresp)
  236. }
  237. },
  238. // txn failure case
  239. func() {
  240. txn := &pb.TxnRequest{}
  241. txn.Failure = append(txn.Failure, &pb.RequestUnion{
  242. Request: &pb.RequestUnion_RequestPut{
  243. RequestPut: preq}})
  244. cmp := &pb.Compare{
  245. Result: pb.Compare_GREATER,
  246. Target: pb.Compare_CREATE,
  247. Key: []byte("bar"),
  248. }
  249. txn.Compare = append(txn.Compare, cmp)
  250. if tresp, err := kvc.Txn(context.TODO(), txn); err == nil {
  251. t.Errorf("succeeded txn failure. req: %v. resp: %v", txn, tresp)
  252. }
  253. },
  254. // ignore bad lease in failure on success txn
  255. func() {
  256. txn := &pb.TxnRequest{}
  257. rreq := &pb.RangeRequest{Key: []byte("bar")}
  258. txn.Success = append(txn.Success, &pb.RequestUnion{
  259. Request: &pb.RequestUnion_RequestRange{
  260. RequestRange: rreq}})
  261. txn.Failure = append(txn.Failure, &pb.RequestUnion{
  262. Request: &pb.RequestUnion_RequestPut{
  263. RequestPut: preq}})
  264. if tresp, err := kvc.Txn(context.TODO(), txn); err != nil {
  265. t.Errorf("failed good txn. req: %v. resp: %v", txn, tresp)
  266. }
  267. },
  268. }
  269. for i, f := range tests {
  270. f()
  271. // key shouldn't have been stored
  272. rreq := &pb.RangeRequest{Key: key}
  273. rresp, err := kvc.Range(context.TODO(), rreq)
  274. if err != nil {
  275. t.Errorf("#%d. could not rangereq (%v)", i, err)
  276. } else if len(rresp.Kvs) != 0 {
  277. t.Errorf("#%d. expected no keys, got %v", i, rresp)
  278. }
  279. }
  280. }
  281. // TestV3DeleteRange tests various edge cases in the DeleteRange API.
  282. func TestV3DeleteRange(t *testing.T) {
  283. defer testutil.AfterTest(t)
  284. tests := []struct {
  285. keySet []string
  286. begin string
  287. end string
  288. wantSet [][]byte
  289. deleted int64
  290. }{
  291. // delete middle
  292. {
  293. []string{"foo", "foo/abc", "fop"},
  294. "foo/", "fop",
  295. [][]byte{[]byte("foo"), []byte("fop")}, 1,
  296. },
  297. // no delete
  298. {
  299. []string{"foo", "foo/abc", "fop"},
  300. "foo/", "foo/",
  301. [][]byte{[]byte("foo"), []byte("foo/abc"), []byte("fop")}, 0,
  302. },
  303. // delete first
  304. {
  305. []string{"foo", "foo/abc", "fop"},
  306. "fo", "fop",
  307. [][]byte{[]byte("fop")}, 2,
  308. },
  309. // delete tail
  310. {
  311. []string{"foo", "foo/abc", "fop"},
  312. "foo/", "fos",
  313. [][]byte{[]byte("foo")}, 2,
  314. },
  315. // delete exact
  316. {
  317. []string{"foo", "foo/abc", "fop"},
  318. "foo/abc", "",
  319. [][]byte{[]byte("foo"), []byte("fop")}, 1,
  320. },
  321. // delete none, [x,x)
  322. {
  323. []string{"foo"},
  324. "foo", "foo",
  325. [][]byte{[]byte("foo")}, 0,
  326. },
  327. }
  328. for i, tt := range tests {
  329. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  330. kvc := toGRPC(clus.RandClient()).KV
  331. ks := tt.keySet
  332. for j := range ks {
  333. reqput := &pb.PutRequest{Key: []byte(ks[j]), Value: []byte{}}
  334. _, err := kvc.Put(context.TODO(), reqput)
  335. if err != nil {
  336. t.Fatalf("couldn't put key (%v)", err)
  337. }
  338. }
  339. dreq := &pb.DeleteRangeRequest{
  340. Key: []byte(tt.begin),
  341. RangeEnd: []byte(tt.end)}
  342. dresp, err := kvc.DeleteRange(context.TODO(), dreq)
  343. if err != nil {
  344. t.Fatalf("couldn't delete range on test %d (%v)", i, err)
  345. }
  346. if tt.deleted != dresp.Deleted {
  347. t.Errorf("expected %d on test %v, got %d", tt.deleted, i, dresp.Deleted)
  348. }
  349. rreq := &pb.RangeRequest{Key: []byte{0x0}, RangeEnd: []byte{0xff}}
  350. rresp, err := kvc.Range(context.TODO(), rreq)
  351. if err != nil {
  352. t.Errorf("couldn't get range on test %v (%v)", i, err)
  353. }
  354. if dresp.Header.Revision != rresp.Header.Revision {
  355. t.Errorf("expected revision %v, got %v",
  356. dresp.Header.Revision, rresp.Header.Revision)
  357. }
  358. keys := [][]byte{}
  359. for j := range rresp.Kvs {
  360. keys = append(keys, rresp.Kvs[j].Key)
  361. }
  362. if !reflect.DeepEqual(tt.wantSet, keys) {
  363. t.Errorf("expected %v on test %v, got %v", tt.wantSet, i, keys)
  364. }
  365. // can't defer because tcp ports will be in use
  366. clus.Terminate(t)
  367. }
  368. }
  369. // TestV3TxnInvaildRange tests txn
  370. func TestV3TxnInvaildRange(t *testing.T) {
  371. defer testutil.AfterTest(t)
  372. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  373. defer clus.Terminate(t)
  374. kvc := toGRPC(clus.RandClient()).KV
  375. preq := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  376. for i := 0; i < 3; i++ {
  377. _, err := kvc.Put(context.Background(), preq)
  378. if err != nil {
  379. t.Fatalf("couldn't put key (%v)", err)
  380. }
  381. }
  382. _, err := kvc.Compact(context.Background(), &pb.CompactionRequest{Revision: 2})
  383. if err != nil {
  384. t.Fatalf("couldn't compact kv space (%v)", err)
  385. }
  386. // future rev
  387. txn := &pb.TxnRequest{}
  388. txn.Success = append(txn.Success, &pb.RequestUnion{
  389. Request: &pb.RequestUnion_RequestPut{
  390. RequestPut: preq}})
  391. rreq := &pb.RangeRequest{Key: []byte("foo"), Revision: 100}
  392. txn.Success = append(txn.Success, &pb.RequestUnion{
  393. Request: &pb.RequestUnion_RequestRange{
  394. RequestRange: rreq}})
  395. if _, err := kvc.Txn(context.TODO(), txn); err != rpctypes.ErrFutureRev {
  396. t.Errorf("err = %v, want %v", err, rpctypes.ErrFutureRev)
  397. }
  398. // compacted rev
  399. tv, _ := txn.Success[1].Request.(*pb.RequestUnion_RequestRange)
  400. tv.RequestRange.Revision = 1
  401. if _, err := kvc.Txn(context.TODO(), txn); err != rpctypes.ErrCompacted {
  402. t.Errorf("err = %v, want %v", err, rpctypes.ErrCompacted)
  403. }
  404. }
  405. func TestV3TooLargeRequest(t *testing.T) {
  406. defer testutil.AfterTest(t)
  407. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  408. defer clus.Terminate(t)
  409. kvc := toGRPC(clus.RandClient()).KV
  410. // 2MB request value
  411. largeV := make([]byte, 2*1024*1024)
  412. preq := &pb.PutRequest{Key: []byte("foo"), Value: largeV}
  413. _, err := kvc.Put(context.Background(), preq)
  414. if err != rpctypes.ErrRequestTooLarge {
  415. t.Errorf("err = %v, want %v", err, rpctypes.ErrRequestTooLarge)
  416. }
  417. }
  418. // TestV3Hash tests hash.
  419. func TestV3Hash(t *testing.T) {
  420. defer testutil.AfterTest(t)
  421. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  422. defer clus.Terminate(t)
  423. cli := clus.RandClient()
  424. kvc := toGRPC(cli).KV
  425. m := toGRPC(cli).Maintenance
  426. preq := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  427. for i := 0; i < 3; i++ {
  428. _, err := kvc.Put(context.Background(), preq)
  429. if err != nil {
  430. t.Fatalf("couldn't put key (%v)", err)
  431. }
  432. }
  433. resp, err := m.Hash(context.Background(), &pb.HashRequest{})
  434. if err != nil || resp.Hash == 0 {
  435. t.Fatalf("couldn't hash (%v, hash %d)", err, resp.Hash)
  436. }
  437. }
  438. // TestV3StorageQuotaAPI tests the V3 server respects quotas at the API layer
  439. func TestV3StorageQuotaAPI(t *testing.T) {
  440. defer testutil.AfterTest(t)
  441. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  442. clus.Members[0].QuotaBackendBytes = 64 * 1024
  443. clus.Members[0].Stop(t)
  444. clus.Members[0].Restart(t)
  445. defer clus.Terminate(t)
  446. kvc := toGRPC(clus.Client(0)).KV
  447. key := []byte("abc")
  448. // test small put that fits in quota
  449. smallbuf := make([]byte, 512)
  450. if _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: key, Value: smallbuf}); err != nil {
  451. t.Fatal(err)
  452. }
  453. // test big put
  454. bigbuf := make([]byte, 64*1024)
  455. _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: key, Value: bigbuf})
  456. if err == nil || err != rpctypes.ErrNoSpace {
  457. t.Fatalf("big put got %v, expected %v", err, rpctypes.ErrNoSpace)
  458. }
  459. // test big txn
  460. puttxn := &pb.RequestUnion{
  461. Request: &pb.RequestUnion_RequestPut{
  462. RequestPut: &pb.PutRequest{
  463. Key: key,
  464. Value: bigbuf,
  465. },
  466. },
  467. }
  468. txnreq := &pb.TxnRequest{}
  469. txnreq.Success = append(txnreq.Success, puttxn)
  470. _, txnerr := kvc.Txn(context.TODO(), txnreq)
  471. if txnerr == nil || err != rpctypes.ErrNoSpace {
  472. t.Fatalf("big txn got %v, expected %v", err, rpctypes.ErrNoSpace)
  473. }
  474. }
  475. // TestV3StorageQuotaApply tests the V3 server respects quotas during apply
  476. func TestV3StorageQuotaApply(t *testing.T) {
  477. testutil.AfterTest(t)
  478. clus := NewClusterV3(t, &ClusterConfig{Size: 2})
  479. defer clus.Terminate(t)
  480. kvc0 := toGRPC(clus.Client(0)).KV
  481. kvc1 := toGRPC(clus.Client(1)).KV
  482. // force a node to have a different quota
  483. clus.Members[0].QuotaBackendBytes = 64 * 1024
  484. clus.Members[0].Stop(t)
  485. clus.Members[0].Restart(t)
  486. clus.waitLeader(t, clus.Members)
  487. key := []byte("abc")
  488. // test small put still works
  489. smallbuf := make([]byte, 1024)
  490. _, serr := kvc0.Put(context.TODO(), &pb.PutRequest{Key: key, Value: smallbuf})
  491. if serr != nil {
  492. t.Fatal(serr)
  493. }
  494. // test big put
  495. bigbuf := make([]byte, 64*1024)
  496. _, err := kvc1.Put(context.TODO(), &pb.PutRequest{Key: key, Value: bigbuf})
  497. if err != nil {
  498. t.Fatal(err)
  499. }
  500. // small quota machine should reject put
  501. // first, synchronize with the cluster via quorum get
  502. kvc0.Range(context.TODO(), &pb.RangeRequest{Key: []byte("foo")})
  503. if _, err := kvc0.Put(context.TODO(), &pb.PutRequest{Key: key, Value: smallbuf}); err == nil {
  504. t.Fatalf("past-quota instance should reject put")
  505. }
  506. // large quota machine should reject put
  507. if _, err := kvc1.Put(context.TODO(), &pb.PutRequest{Key: key, Value: smallbuf}); err == nil {
  508. t.Fatalf("past-quota instance should reject put")
  509. }
  510. // reset large quota node to ensure alarm persisted
  511. clus.Members[1].Stop(t)
  512. clus.Members[1].Restart(t)
  513. clus.waitLeader(t, clus.Members)
  514. if _, err := kvc1.Put(context.TODO(), &pb.PutRequest{Key: key, Value: smallbuf}); err == nil {
  515. t.Fatalf("alarmed instance should reject put after reset")
  516. }
  517. }
  518. // TestV3AlarmDeactivate ensures that space alarms can be deactivated so puts go through.
  519. func TestV3AlarmDeactivate(t *testing.T) {
  520. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  521. defer clus.Terminate(t)
  522. kvc := toGRPC(clus.RandClient()).KV
  523. mt := toGRPC(clus.RandClient()).Maintenance
  524. alarmReq := &pb.AlarmRequest{
  525. MemberID: 123,
  526. Action: pb.AlarmRequest_ACTIVATE,
  527. Alarm: pb.AlarmType_NOSPACE,
  528. }
  529. if _, err := mt.Alarm(context.TODO(), alarmReq); err != nil {
  530. t.Fatal(err)
  531. }
  532. key := []byte("abc")
  533. smallbuf := make([]byte, 512)
  534. _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: key, Value: smallbuf})
  535. if err == nil && err != rpctypes.ErrNoSpace {
  536. t.Fatalf("put got %v, expected %v", err, rpctypes.ErrNoSpace)
  537. }
  538. alarmReq.Action = pb.AlarmRequest_DEACTIVATE
  539. if _, err = mt.Alarm(context.TODO(), alarmReq); err != nil {
  540. t.Fatal(err)
  541. }
  542. if _, err = kvc.Put(context.TODO(), &pb.PutRequest{Key: key, Value: smallbuf}); err != nil {
  543. t.Fatal(err)
  544. }
  545. }
  546. func TestV3RangeRequest(t *testing.T) {
  547. defer testutil.AfterTest(t)
  548. tests := []struct {
  549. putKeys []string
  550. reqs []pb.RangeRequest
  551. wresps [][]string
  552. wmores []bool
  553. }{
  554. // single key
  555. {
  556. []string{"foo", "bar"},
  557. []pb.RangeRequest{
  558. // exists
  559. {Key: []byte("foo")},
  560. // doesn't exist
  561. {Key: []byte("baz")},
  562. },
  563. [][]string{
  564. {"foo"},
  565. {},
  566. },
  567. []bool{false, false},
  568. },
  569. // multi-key
  570. {
  571. []string{"a", "b", "c", "d", "e"},
  572. []pb.RangeRequest{
  573. // all in range
  574. {Key: []byte("a"), RangeEnd: []byte("z")},
  575. // [b, d)
  576. {Key: []byte("b"), RangeEnd: []byte("d")},
  577. // out of range
  578. {Key: []byte("f"), RangeEnd: []byte("z")},
  579. // [c,c) = empty
  580. {Key: []byte("c"), RangeEnd: []byte("c")},
  581. // [d, b) = empty
  582. {Key: []byte("d"), RangeEnd: []byte("b")},
  583. // ["\0", "\0") => all in range
  584. {Key: []byte{0}, RangeEnd: []byte{0}},
  585. },
  586. [][]string{
  587. {"a", "b", "c", "d", "e"},
  588. {"b", "c"},
  589. {},
  590. {},
  591. {},
  592. {"a", "b", "c", "d", "e"},
  593. },
  594. []bool{false, false, false, false, false, false},
  595. },
  596. // revision
  597. {
  598. []string{"a", "b", "c", "d", "e"},
  599. []pb.RangeRequest{
  600. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 0},
  601. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 1},
  602. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 2},
  603. {Key: []byte("a"), RangeEnd: []byte("z"), Revision: 3},
  604. },
  605. [][]string{
  606. {"a", "b", "c", "d", "e"},
  607. {},
  608. {"a"},
  609. {"a", "b"},
  610. },
  611. []bool{false, false, false, false},
  612. },
  613. // limit
  614. {
  615. []string{"foo", "bar"},
  616. []pb.RangeRequest{
  617. // more
  618. {Key: []byte("a"), RangeEnd: []byte("z"), Limit: 1},
  619. // no more
  620. {Key: []byte("a"), RangeEnd: []byte("z"), Limit: 2},
  621. },
  622. [][]string{
  623. {"bar"},
  624. {"bar", "foo"},
  625. },
  626. []bool{true, false},
  627. },
  628. // sort
  629. {
  630. []string{"b", "a", "c", "d", "c"},
  631. []pb.RangeRequest{
  632. {
  633. Key: []byte("a"), RangeEnd: []byte("z"),
  634. Limit: 1,
  635. SortOrder: pb.RangeRequest_ASCEND,
  636. SortTarget: pb.RangeRequest_KEY,
  637. },
  638. {
  639. Key: []byte("a"), RangeEnd: []byte("z"),
  640. Limit: 1,
  641. SortOrder: pb.RangeRequest_DESCEND,
  642. SortTarget: pb.RangeRequest_KEY,
  643. },
  644. {
  645. Key: []byte("a"), RangeEnd: []byte("z"),
  646. Limit: 1,
  647. SortOrder: pb.RangeRequest_ASCEND,
  648. SortTarget: pb.RangeRequest_CREATE,
  649. },
  650. {
  651. Key: []byte("a"), RangeEnd: []byte("z"),
  652. Limit: 1,
  653. SortOrder: pb.RangeRequest_DESCEND,
  654. SortTarget: pb.RangeRequest_MOD,
  655. },
  656. {
  657. Key: []byte("z"), RangeEnd: []byte("z"),
  658. Limit: 1,
  659. SortOrder: pb.RangeRequest_DESCEND,
  660. SortTarget: pb.RangeRequest_CREATE,
  661. },
  662. },
  663. [][]string{
  664. {"a"},
  665. {"d"},
  666. {"b"},
  667. {"c"},
  668. {},
  669. },
  670. []bool{true, true, true, true, false},
  671. },
  672. }
  673. for i, tt := range tests {
  674. clus := NewClusterV3(t, &ClusterConfig{Size: 3})
  675. for _, k := range tt.putKeys {
  676. kvc := toGRPC(clus.RandClient()).KV
  677. req := &pb.PutRequest{Key: []byte(k), Value: []byte("bar")}
  678. if _, err := kvc.Put(context.TODO(), req); err != nil {
  679. t.Fatalf("#%d: couldn't put key (%v)", i, err)
  680. }
  681. }
  682. for j, req := range tt.reqs {
  683. kvc := toGRPC(clus.RandClient()).KV
  684. resp, err := kvc.Range(context.TODO(), &req)
  685. if err != nil {
  686. t.Errorf("#%d.%d: Range error: %v", i, j, err)
  687. continue
  688. }
  689. if len(resp.Kvs) != len(tt.wresps[j]) {
  690. t.Errorf("#%d.%d: bad len(resp.Kvs). got = %d, want = %d, ", i, j, len(resp.Kvs), len(tt.wresps[j]))
  691. continue
  692. }
  693. for k, wKey := range tt.wresps[j] {
  694. respKey := string(resp.Kvs[k].Key)
  695. if respKey != wKey {
  696. t.Errorf("#%d.%d: key[%d]. got = %v, want = %v, ", i, j, k, respKey, wKey)
  697. }
  698. }
  699. if resp.More != tt.wmores[j] {
  700. t.Errorf("#%d.%d: bad more. got = %v, want = %v, ", i, j, resp.More, tt.wmores[j])
  701. }
  702. wrev := int64(len(tt.putKeys) + 1)
  703. if resp.Header.Revision != wrev {
  704. t.Errorf("#%d.%d: bad header revision. got = %d. want = %d", i, j, resp.Header.Revision, wrev)
  705. }
  706. }
  707. clus.Terminate(t)
  708. }
  709. }
  710. func newClusterV3NoClients(t *testing.T, cfg *ClusterConfig) *ClusterV3 {
  711. cfg.UseGRPC = true
  712. clus := &ClusterV3{cluster: NewClusterByConfig(t, cfg)}
  713. clus.Launch(t)
  714. return clus
  715. }
  716. // TestTLSGRPCRejectInsecureClient checks that connection is rejected if server is TLS but not client.
  717. func TestTLSGRPCRejectInsecureClient(t *testing.T) {
  718. defer testutil.AfterTest(t)
  719. cfg := ClusterConfig{Size: 3, ClientTLS: &testTLSInfo}
  720. clus := newClusterV3NoClients(t, &cfg)
  721. defer clus.Terminate(t)
  722. // nil out TLS field so client will use an insecure connection
  723. clus.Members[0].ClientTLSInfo = nil
  724. client, err := NewClientV3(clus.Members[0])
  725. if err != nil && err != grpc.ErrClientConnTimeout {
  726. t.Fatalf("unexpected error (%v)", err)
  727. } else if client == nil {
  728. // Ideally, no client would be returned. However, grpc will
  729. // return a connection without trying to handshake first so
  730. // the connection appears OK.
  731. return
  732. }
  733. defer client.Close()
  734. ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
  735. conn := client.ActiveConnection()
  736. st, err := conn.State()
  737. if err != nil {
  738. t.Fatal(err)
  739. } else if st != grpc.Ready {
  740. t.Fatalf("expected Ready, got %v", st)
  741. }
  742. // rpc will fail to handshake, triggering a connection state change
  743. donec := make(chan error, 1)
  744. go func() {
  745. reqput := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  746. _, perr := toGRPC(client).KV.Put(ctx, reqput)
  747. donec <- perr
  748. }()
  749. st, err = conn.WaitForStateChange(ctx, st)
  750. if err != nil {
  751. t.Fatalf("unexpected error waiting for change (%v)", err)
  752. } else if st == grpc.Ready {
  753. t.Fatalf("expected failure state, got %v", st)
  754. }
  755. cancel()
  756. if perr := <-donec; perr == nil {
  757. t.Fatalf("expected client error on put")
  758. }
  759. }
  760. // TestTLSGRPCRejectSecureClient checks that connection is rejected if client is TLS but not server.
  761. func TestTLSGRPCRejectSecureClient(t *testing.T) {
  762. defer testutil.AfterTest(t)
  763. cfg := ClusterConfig{Size: 3}
  764. clus := newClusterV3NoClients(t, &cfg)
  765. defer clus.Terminate(t)
  766. clus.Members[0].ClientTLSInfo = &testTLSInfo
  767. client, err := NewClientV3(clus.Members[0])
  768. if client != nil || err == nil {
  769. t.Fatalf("expected no client")
  770. } else if err != grpc.ErrClientConnTimeout {
  771. t.Fatalf("unexpected error (%v)", err)
  772. }
  773. }
  774. // TestTLSGRPCAcceptSecureAll checks that connection is accepted if both client and server are TLS
  775. func TestTLSGRPCAcceptSecureAll(t *testing.T) {
  776. defer testutil.AfterTest(t)
  777. cfg := ClusterConfig{Size: 3, ClientTLS: &testTLSInfo}
  778. clus := newClusterV3NoClients(t, &cfg)
  779. defer clus.Terminate(t)
  780. client, err := NewClientV3(clus.Members[0])
  781. if err != nil {
  782. t.Fatalf("expected tls client (%v)", err)
  783. }
  784. defer client.Close()
  785. reqput := &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}
  786. if _, err := toGRPC(client).KV.Put(context.TODO(), reqput); err != nil {
  787. t.Fatalf("unexpected error on put over tls (%v)", err)
  788. }
  789. }