ctl_v3_alarm_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2016 The etcd Authors
  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 e2e
  15. import (
  16. "os"
  17. "strings"
  18. "testing"
  19. "time"
  20. "github.com/coreos/etcd/clientv3"
  21. "golang.org/x/net/context"
  22. )
  23. func TestCtlV3Alarm(t *testing.T) {
  24. // The boltdb minimum working set is six pages.
  25. testCtl(t, alarmTest, withQuota(int64(13*os.Getpagesize())))
  26. }
  27. func alarmTest(cx ctlCtx) {
  28. // test small put still works
  29. smallbuf := strings.Repeat("a", 64)
  30. if err := ctlV3Put(cx, "1st_test", smallbuf, ""); err != nil {
  31. cx.t.Fatal(err)
  32. }
  33. // write some chunks to fill up the database
  34. buf := strings.Repeat("b", int(os.Getpagesize()))
  35. for {
  36. if err := ctlV3Put(cx, "2nd_test", buf, ""); err != nil {
  37. if !strings.Contains(err.Error(), "etcdserver: mvcc: database space exceeded") {
  38. cx.t.Fatal(err)
  39. }
  40. break
  41. }
  42. }
  43. // quota alarm should now be on
  44. if err := ctlV3Alarm(cx, "list", "alarm:NOSPACE"); err != nil {
  45. cx.t.Fatal(err)
  46. }
  47. // check that Put is rejected when alarm is on
  48. if err := ctlV3Put(cx, "3rd_test", smallbuf, ""); err != nil {
  49. if !strings.Contains(err.Error(), "etcdserver: mvcc: database space exceeded") {
  50. cx.t.Fatal(err)
  51. }
  52. }
  53. eps := cx.epc.grpcEndpoints()
  54. // get latest revision to compact
  55. cli, err := clientv3.New(clientv3.Config{
  56. Endpoints: eps,
  57. DialTimeout: 3 * time.Second,
  58. })
  59. if err != nil {
  60. cx.t.Fatal(err)
  61. }
  62. defer cli.Close()
  63. sresp, err := cli.Status(context.TODO(), eps[0])
  64. if err != nil {
  65. cx.t.Fatal(err)
  66. }
  67. // make some space
  68. if err := ctlV3Compact(cx, sresp.Header.Revision, true); err != nil {
  69. cx.t.Fatal(err)
  70. }
  71. if err := ctlV3Defrag(cx); err != nil {
  72. cx.t.Fatal(err)
  73. }
  74. // turn off alarm
  75. if err := ctlV3Alarm(cx, "disarm", "alarm:NOSPACE"); err != nil {
  76. cx.t.Fatal(err)
  77. }
  78. // put one more key below quota
  79. if err := ctlV3Put(cx, "4th_test", smallbuf, ""); err != nil {
  80. cx.t.Fatal(err)
  81. }
  82. }
  83. func ctlV3Alarm(cx ctlCtx, cmd string, as ...string) error {
  84. cmdArgs := append(cx.PrefixArgs(), "alarm", cmd)
  85. return spawnWithExpects(cmdArgs, as...)
  86. }