command_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. Copyright 2019 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. "context"
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "testing"
  20. "time"
  21. "github.com/pkg/errors"
  22. "github.com/rook/rook/pkg/clusterd"
  23. "github.com/rook/rook/pkg/operator/test"
  24. "github.com/rook/rook/pkg/util/exec"
  25. exectest "github.com/rook/rook/pkg/util/exec/test"
  26. "github.com/stretchr/testify/assert"
  27. )
  28. func TestFinalizeCephCommandArgs(t *testing.T) {
  29. RunAllCephCommandsInToolboxPod = ""
  30. configDir := "/var/lib/rook/rook-ceph"
  31. expectedCommand := "ceph"
  32. args := []string{"quorum_status"}
  33. expectedArgs := []string{
  34. "quorum_status",
  35. "--connect-timeout=" + strconv.Itoa(int(exec.CephCommandsTimeout.Seconds())),
  36. "--cluster=rook",
  37. "--conf=/var/lib/rook/rook-ceph/rook/rook.config",
  38. "--name=client.admin",
  39. "--keyring=/var/lib/rook/rook-ceph/rook/client.admin.keyring",
  40. }
  41. clusterInfo := AdminTestClusterInfo("rook")
  42. cmd, args := FinalizeCephCommandArgs(expectedCommand, clusterInfo, args, configDir)
  43. assert.Exactly(t, expectedCommand, cmd)
  44. assert.Exactly(t, expectedArgs, args)
  45. }
  46. func TestFinalizeRadosGWAdminCommandArgs(t *testing.T) {
  47. RunAllCephCommandsInToolboxPod = ""
  48. configDir := "/var/lib/rook/rook-ceph"
  49. expectedCommand := "radosgw-admin"
  50. args := []string{
  51. "realm",
  52. "create",
  53. "--default",
  54. "--rgw-realm=default-rook",
  55. "--rgw-zonegroup=default-rook",
  56. }
  57. expectedArgs := []string{
  58. "realm",
  59. "create",
  60. "--default",
  61. "--rgw-realm=default-rook",
  62. "--rgw-zonegroup=default-rook",
  63. "--cluster=rook",
  64. "--conf=/var/lib/rook/rook-ceph/rook/rook.config",
  65. "--name=client.admin",
  66. "--keyring=/var/lib/rook/rook-ceph/rook/client.admin.keyring",
  67. }
  68. clusterInfo := AdminTestClusterInfo("rook")
  69. cmd, args := FinalizeCephCommandArgs(expectedCommand, clusterInfo, args, configDir)
  70. assert.Exactly(t, expectedCommand, cmd)
  71. assert.Exactly(t, expectedArgs, args)
  72. }
  73. func TestFinalizeCephCommandArgsToolBox(t *testing.T) {
  74. RunAllCephCommandsInToolboxPod = "rook-ceph-tools"
  75. configDir := "/var/lib/rook/rook-ceph"
  76. expectedCommand := "ceph"
  77. args := []string{"health"}
  78. expectedArgs := []string{
  79. "exec",
  80. "-i",
  81. "rook-ceph-tools",
  82. "-n",
  83. "rook",
  84. "--",
  85. "timeout",
  86. "15",
  87. "ceph",
  88. "health",
  89. "--connect-timeout=15",
  90. }
  91. clusterInfo := AdminTestClusterInfo("rook")
  92. exec.CephCommandsTimeout = 15 * time.Second
  93. cmd, args := FinalizeCephCommandArgs(expectedCommand, clusterInfo, args, configDir)
  94. assert.Exactly(t, "kubectl", cmd)
  95. assert.Exactly(t, expectedArgs, args)
  96. RunAllCephCommandsInToolboxPod = ""
  97. }
  98. func TestNewRBDCommand(t *testing.T) {
  99. args := []string{"create", "--size", "1G", "myvol"}
  100. t.Run("rbd command with no multus", func(t *testing.T) {
  101. clusterInfo := AdminTestClusterInfo("rook")
  102. executor := &exectest.MockExecutor{}
  103. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  104. switch {
  105. case command == "rbd" && args[0] == "create":
  106. assert.Len(t, args, 8)
  107. return "success", nil
  108. }
  109. return "", errors.Errorf("unexpected ceph command %q", args)
  110. }
  111. context := &clusterd.Context{Executor: executor}
  112. cmd := NewRBDCommand(context, clusterInfo, args)
  113. assert.False(t, cmd.RemoteExecution)
  114. output, err := cmd.Run()
  115. assert.NoError(t, err)
  116. assert.Equal(t, "success", string(output))
  117. })
  118. t.Run("rbd command with multus", func(t *testing.T) {
  119. clusterInfo := AdminTestClusterInfo("rook")
  120. clusterInfo.NetworkSpec.Provider = "multus"
  121. executor := &exectest.MockExecutor{}
  122. context := &clusterd.Context{Executor: executor, RemoteExecutor: exec.RemotePodCommandExecutor{ClientSet: test.New(t, 3)}}
  123. cmd := NewRBDCommand(context, clusterInfo, args)
  124. assert.True(t, cmd.RemoteExecution)
  125. _, err := cmd.Run()
  126. assert.Error(t, err)
  127. assert.Len(t, cmd.args, 4)
  128. // This is not the best but it shows we go through the right codepath
  129. assert.Contains(t, err.Error(), "no pods found with selector \"rook-ceph-mgr\"")
  130. })
  131. t.Run("context canceled nothing to run", func(t *testing.T) {
  132. clusterInfo := AdminTestClusterInfo("rook")
  133. ctx, cancel := context.WithCancel(context.TODO())
  134. clusterInfo.Context = ctx
  135. cancel()
  136. executor := &exectest.MockExecutor{}
  137. context := &clusterd.Context{Executor: executor, RemoteExecutor: exec.RemotePodCommandExecutor{ClientSet: test.New(t, 3)}}
  138. cmd := NewRBDCommand(context, clusterInfo, args)
  139. _, err := cmd.Run()
  140. assert.Error(t, err)
  141. // This is not the best but it shows we go through the right codepath
  142. assert.EqualError(t, err, "context canceled")
  143. })
  144. }
  145. func TestNewGaneshaRadosGraceCommand(t *testing.T) {
  146. anyArgContains := func(substr string, args []string) bool {
  147. for _, arg := range args {
  148. if strings.Contains(arg, substr) {
  149. return true
  150. }
  151. }
  152. return false
  153. }
  154. args := []string{"--pool", ".nfs", "--ns", "my-nfs", "add", "node"}
  155. timeout := time.Second
  156. ctx := func() *clusterd.Context {
  157. return &clusterd.Context{
  158. Executor: &exectest.MockExecutor{
  159. MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, arg ...string) (string, error) {
  160. t.Logf("command: %s %v", command, arg)
  161. assert.Equal(t, time.Second, timeout)
  162. assert.Equal(t, "ganesha-rados-grace", command)
  163. assert.Equal(t, []string{"--pool", ".nfs", "--ns", "my-nfs", "add", "node"}, arg[0:6])
  164. // ganesha-rados-grace's conf flag is --cephconf
  165. assert.True(t, anyArgContains("--cephconf=", arg))
  166. // ganesha-rados-grace accepts NO standard flags
  167. assert.False(t, anyArgContains("--cluster", arg))
  168. assert.False(t, anyArgContains("--conf", arg))
  169. assert.False(t, anyArgContains("--name", arg))
  170. assert.False(t, anyArgContains("--keyring", arg))
  171. assert.False(t, anyArgContains("--format", arg))
  172. return "", nil
  173. },
  174. },
  175. }
  176. }
  177. clusterInfo := func() *ClusterInfo {
  178. return &ClusterInfo{
  179. Namespace: "rook-ceph",
  180. Context: context.Background(),
  181. }
  182. }
  183. t.Run("normal call", func(t *testing.T) {
  184. cmd := NewGaneshaRadosGraceCommand(ctx(), clusterInfo(), args)
  185. assert.Equal(t, false, cmd.JsonOutput)
  186. _, err := cmd.RunWithTimeout(timeout)
  187. assert.NoError(t, err)
  188. })
  189. t.Run("error call", func(t *testing.T) {
  190. ctx := ctx()
  191. ctx.Executor = &exectest.MockExecutor{
  192. MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, arg ...string) (string, error) {
  193. return "", fmt.Errorf("induced error")
  194. },
  195. }
  196. cmd := NewGaneshaRadosGraceCommand(ctx, clusterInfo(), args)
  197. _, err := cmd.RunWithTimeout(timeout)
  198. assert.Error(t, err)
  199. })
  200. }