rados_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Copyright 2022 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. "errors"
  17. "fmt"
  18. "testing"
  19. "time"
  20. "github.com/rook/rook/pkg/clusterd"
  21. "github.com/rook/rook/pkg/util/exec"
  22. "github.com/rook/rook/pkg/util/exec/test"
  23. "github.com/stretchr/testify/assert"
  24. )
  25. func TestRadosRemoveObject(t *testing.T) {
  26. newTest := func(mockExec *test.MockExecutor) (*clusterd.Context, *ClusterInfo) {
  27. ctx := &clusterd.Context{
  28. Executor: mockExec,
  29. }
  30. info := &ClusterInfo{
  31. Context: context.Background(),
  32. }
  33. return ctx, info
  34. }
  35. t.Run("timeout stat-ing", func(t *testing.T) {
  36. me := &test.MockExecutor{
  37. MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, arg ...string) (string, error) {
  38. return "", test.FakeTimeoutError("")
  39. },
  40. }
  41. c, i := newTest(me)
  42. err := RadosRemoveObject(c, i, "mypool", "myns", "someobj")
  43. assert.Error(t, err)
  44. assert.True(t, exec.IsTimeout(err))
  45. })
  46. t.Run("stat err means the object is already gone", func(t *testing.T) {
  47. me := &test.MockExecutor{
  48. MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, arg ...string) (string, error) {
  49. subCommand := arg[4]
  50. assert.Equal(t, subCommand, "stat")
  51. return "", errors.New("induced stat error")
  52. },
  53. }
  54. c, i := newTest(me)
  55. err := RadosRemoveObject(c, i, "mypool", "myns", "someobj")
  56. assert.NoError(t, err)
  57. })
  58. t.Run("rm err is an error", func(t *testing.T) {
  59. me := &test.MockExecutor{
  60. MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, arg ...string) (string, error) {
  61. assert.Equal(t, arg[0], "--pool")
  62. assert.Equal(t, arg[1], "mypool")
  63. assert.Equal(t, arg[2], "--namespace")
  64. assert.Equal(t, arg[3], "myns")
  65. subCommand := arg[4]
  66. assert.Equal(t, arg[5], "someobj")
  67. if subCommand == "stat" {
  68. return "", nil // successful stat means obj exists
  69. } else if subCommand == "rm" {
  70. return "", errors.New("induced rm error")
  71. } else {
  72. panic(fmt.Sprintf("unhandled subcommand %q", subCommand))
  73. }
  74. },
  75. }
  76. c, i := newTest(me)
  77. err := RadosRemoveObject(c, i, "mypool", "myns", "someobj")
  78. assert.Error(t, err)
  79. })
  80. t.Run("rm successful", func(t *testing.T) {
  81. me := &test.MockExecutor{
  82. MockExecuteCommandWithTimeout: func(timeout time.Duration, command string, arg ...string) (string, error) {
  83. assert.Equal(t, arg[0], "--pool")
  84. assert.Equal(t, arg[1], "mypool")
  85. assert.Equal(t, arg[2], "--namespace")
  86. assert.Equal(t, arg[3], "myns")
  87. subCommand := arg[4]
  88. assert.Equal(t, arg[5], "someobj")
  89. if subCommand == "stat" || subCommand == "rm" {
  90. return "", nil
  91. } else {
  92. panic(fmt.Sprintf("unhandled subcommand %q", subCommand))
  93. }
  94. },
  95. }
  96. c, i := newTest(me)
  97. err := RadosRemoveObject(c, i, "mypool", "myns", "someobj")
  98. assert.NoError(t, err)
  99. })
  100. }