mon_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Copyright 2018 The Rook Authors. All rights reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "fmt"
  16. "testing"
  17. "time"
  18. "github.com/pkg/errors"
  19. "github.com/rook/rook/pkg/clusterd"
  20. "github.com/rook/rook/pkg/util/exec"
  21. exectest "github.com/rook/rook/pkg/util/exec/test"
  22. "github.com/stretchr/testify/assert"
  23. )
  24. func TestCephArgs(t *testing.T) {
  25. // cluster a under /etc
  26. args := []string{}
  27. clusterInfo := AdminTestClusterInfo("a")
  28. exec.CephCommandsTimeout = 15 * time.Second
  29. command, args := FinalizeCephCommandArgs(CephTool, clusterInfo, args, "/etc")
  30. assert.Equal(t, CephTool, command)
  31. assert.Equal(t, 5, len(args))
  32. assert.Equal(t, "--connect-timeout=15", args[0])
  33. assert.Equal(t, "--cluster=a", args[1])
  34. assert.Equal(t, "--conf=/etc/a/a.config", args[2])
  35. assert.Equal(t, "--name=client.admin", args[3])
  36. assert.Equal(t, "--keyring=/etc/a/client.admin.keyring", args[4])
  37. RunAllCephCommandsInToolboxPod = "rook-ceph-tools"
  38. args = []string{}
  39. command, args = FinalizeCephCommandArgs(CephTool, clusterInfo, args, "/etc")
  40. assert.Equal(t, Kubectl, command)
  41. assert.Equal(t, 10, len(args), fmt.Sprintf("%+v", args))
  42. assert.Equal(t, "exec", args[0])
  43. assert.Equal(t, "-i", args[1])
  44. assert.Equal(t, "rook-ceph-tools", args[2])
  45. assert.Equal(t, "-n", args[3])
  46. assert.Equal(t, clusterInfo.Namespace, args[4])
  47. assert.Equal(t, "--", args[5])
  48. assert.Equal(t, CephTool, args[8])
  49. assert.Equal(t, "--connect-timeout=15", args[9])
  50. RunAllCephCommandsInToolboxPod = ""
  51. // cluster under /var/lib/rook
  52. args = []string{"myarg"}
  53. command, args = FinalizeCephCommandArgs(RBDTool, clusterInfo, args, "/var/lib/rook")
  54. assert.Equal(t, RBDTool, command)
  55. assert.Equal(t, 5, len(args))
  56. assert.Equal(t, "myarg", args[0])
  57. assert.Equal(t, "--cluster="+clusterInfo.Namespace, args[1])
  58. assert.Equal(t, "--conf=/var/lib/rook/a/a.config", args[2])
  59. assert.Equal(t, "--name=client.admin", args[3])
  60. assert.Equal(t, "--keyring=/var/lib/rook/a/client.admin.keyring", args[4])
  61. }
  62. func TestStretchElectionStrategy(t *testing.T) {
  63. executor := &exectest.MockExecutor{}
  64. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  65. logger.Infof("Command: %s %v", command, args)
  66. if args[0] == "mon" && args[1] == "set" && args[2] == "election_strategy" {
  67. assert.Equal(t, "connectivity", args[3])
  68. return "", nil
  69. }
  70. return "", errors.Errorf("unexpected ceph command %q", args)
  71. }
  72. context := &clusterd.Context{Executor: executor}
  73. clusterInfo := AdminTestClusterInfo("mycluster")
  74. err := EnableStretchElectionStrategy(context, clusterInfo)
  75. assert.NoError(t, err)
  76. }
  77. func TestStretchClusterMonTiebreaker(t *testing.T) {
  78. monName := "a"
  79. failureDomain := "rack"
  80. setTiebreaker := false
  81. enabledStretch := false
  82. executor := &exectest.MockExecutor{}
  83. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  84. logger.Infof("Command: %s %v", command, args)
  85. switch {
  86. case args[0] == "mon" && args[1] == "enable_stretch_mode":
  87. enabledStretch = true
  88. assert.Equal(t, monName, args[2])
  89. assert.Equal(t, defaultStretchCrushRuleName, args[3])
  90. assert.Equal(t, failureDomain, args[4])
  91. return "", nil
  92. case args[0] == "mon" && args[1] == "set_new_tiebreaker":
  93. setTiebreaker = true
  94. assert.Equal(t, monName, args[2])
  95. return "", nil
  96. }
  97. return "", errors.Errorf("unexpected ceph command %q", args)
  98. }
  99. context := &clusterd.Context{Executor: executor}
  100. clusterInfo := AdminTestClusterInfo("mycluster")
  101. err := SetMonStretchTiebreaker(context, clusterInfo, monName, failureDomain)
  102. assert.NoError(t, err)
  103. assert.True(t, enabledStretch)
  104. assert.False(t, setTiebreaker)
  105. enabledStretch = false
  106. err = SetNewTiebreaker(context, clusterInfo, monName)
  107. assert.NoError(t, err)
  108. assert.True(t, setTiebreaker)
  109. assert.False(t, enabledStretch)
  110. }
  111. func TestMonDump(t *testing.T) {
  112. executor := &exectest.MockExecutor{}
  113. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  114. logger.Infof("Command: %s %v", command, args)
  115. switch {
  116. case args[0] == "mon" && args[1] == "dump":
  117. return `{"epoch":3,"fsid":"6a31a264-9090-4048-8d95-4b8c3cde909d","modified":"2020-12-09T18:13:36.346150Z","created":"2020-12-09T18:13:13.014270Z","min_mon_release":15,"min_mon_release_name":"pacific",
  118. "features":{"persistent":["kraken","luminous","mimic","osdmap-prune","nautilus","octopus","pacific","quincy"],"optional":[]},
  119. "election_strategy":1,"mons":[
  120. {"rank":0,"name":"a","crush_location":"{zone=a}","public_addrs":{"addrvec":[{"type":"v2","addr":"10.109.80.104:3300","nonce":0},{"type":"v1","addr":"10.109.80.104:6789","nonce":0}]},"addr":"10.109.80.104:6789/0","public_addr":"10.109.80.104:6789/0","priority":0,"weight":0},
  121. {"rank":1,"name":"b","crush_location":"{zone=b}","public_addrs":{"addrvec":[{"type":"v2","addr":"10.107.12.199:3300","nonce":0},{"type":"v1","addr":"10.107.12.199:6789","nonce":0}]},"addr":"10.107.12.199:6789/0","public_addr":"10.107.12.199:6789/0","priority":0,"weight":0},
  122. {"rank":2,"name":"c","crush_location":"{zone=c}","public_addrs":{"addrvec":[{"type":"v2","addr":"10.107.5.207:3300","nonce":0},{"type":"v1","addr":"10.107.5.207:6789","nonce":0}]},"addr":"10.107.5.207:6789/0","public_addr":"10.107.5.207:6789/0","priority":0,"weight":0}],
  123. "quorum":[0,1,2]}`, nil
  124. }
  125. return "", errors.Errorf("unexpected ceph command %q", args)
  126. }
  127. context := &clusterd.Context{Executor: executor}
  128. clusterInfo := AdminTestClusterInfo("mycluster")
  129. dump, err := GetMonDump(context, clusterInfo)
  130. assert.NoError(t, err)
  131. assert.Equal(t, 1, dump.ElectionStrategy)
  132. assert.Equal(t, "{zone=a}", dump.Mons[0].CrushLocation)
  133. assert.Equal(t, "a", dump.Mons[0].Name)
  134. assert.Equal(t, 0, dump.Mons[0].Rank)
  135. assert.Equal(t, "b", dump.Mons[1].Name)
  136. assert.Equal(t, 1, dump.Mons[1].Rank)
  137. assert.Equal(t, 3, len(dump.Mons))
  138. assert.Equal(t, 3, len(dump.Quorum))
  139. }