ctl_v3_alarm_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. )
  20. func TestCtlV3Alarm(t *testing.T) {
  21. // The boltdb minimum working set is six pages.
  22. testCtl(t, alarmTest, withQuota(int64(13*os.Getpagesize())))
  23. }
  24. func alarmTest(cx ctlCtx) {
  25. // test small put still works
  26. smallbuf := strings.Repeat("a", 64)
  27. if err := ctlV3Put(cx, "1st_test", smallbuf, ""); err != nil {
  28. cx.t.Fatal(err)
  29. }
  30. // write some chunks to fill up the database
  31. buf := strings.Repeat("b", int(os.Getpagesize()))
  32. var rev int64
  33. for ; ; rev++ {
  34. if err := ctlV3Put(cx, "2nd_test", buf, ""); err != nil {
  35. if !strings.Contains(err.Error(), "etcdserver: mvcc: database space exceeded") {
  36. cx.t.Fatal(err)
  37. }
  38. break
  39. }
  40. }
  41. // quota alarm should now be on
  42. if err := ctlV3Alarm(cx, "list", "alarm:NOSPACE"); err != nil {
  43. cx.t.Fatal(err)
  44. }
  45. // check that Put is rejected when alarm is on
  46. if err := ctlV3Put(cx, "3rd_test", smallbuf, ""); err != nil {
  47. if !strings.Contains(err.Error(), "etcdserver: mvcc: database space exceeded") {
  48. cx.t.Fatal(err)
  49. }
  50. }
  51. // make some space
  52. if err := ctlV3Compact(cx, rev, true); err != nil {
  53. cx.t.Fatal(err)
  54. }
  55. if err := ctlV3Defrag(cx); err != nil {
  56. cx.t.Fatal(err)
  57. }
  58. // turn off alarm
  59. if err := ctlV3Alarm(cx, "disarm", "alarm:NOSPACE"); err != nil {
  60. cx.t.Fatal(err)
  61. }
  62. // put one more key below quota
  63. if err := ctlV3Put(cx, "4th_test", smallbuf, ""); err != nil {
  64. cx.t.Fatal(err)
  65. }
  66. }
  67. func ctlV3Alarm(cx ctlCtx, cmd string, as ...string) error {
  68. cmdArgs := append(cx.PrefixArgs(), "alarm", cmd)
  69. return spawnWithExpects(cmdArgs, as...)
  70. }