filesystem_mirror_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Copyright 2021 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. "encoding/base64"
  16. "testing"
  17. "github.com/pkg/errors"
  18. "github.com/rook/rook/pkg/clusterd"
  19. cephver "github.com/rook/rook/pkg/operator/ceph/version"
  20. exectest "github.com/rook/rook/pkg/util/exec/test"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. var (
  24. // response of "ceph fs snapshot mirror peer_bootstrap create myfs2 client.mirror test"
  25. //nolint:gosec // since this is not leaking any credentials
  26. fsMirrorToken = `{"token": "eyJmc2lkIjogIjgyYjdlZDkyLTczYjAtNGIyMi1hOGI3LWVkOTQ4M2UyODc1NiIsICJmaWxlc3lzdGVtIjogIm15ZnMyIiwgInVzZXIiOiAiY2xpZW50Lm1pcnJvciIsICJzaXRlX25hbWUiOiAidGVzdCIsICJrZXkiOiAiQVFEVVAxSmdqM3RYQVJBQWs1cEU4cDI1ZUhld2lQK0ZXRm9uOVE9PSIsICJtb25faG9zdCI6ICJbdjI6MTAuOTYuMTQyLjIxMzozMzAwLHYxOjEwLjk2LjE0Mi4yMTM6Njc4OV0sW3YyOjEwLjk2LjIxNy4yMDc6MzMwMCx2MToxMC45Ni4yMTcuMjA3OjY3ODldLFt2MjoxMC45OS4xMC4xNTc6MzMwMCx2MToxMC45OS4xMC4xNTc6Njc4OV0ifQ=="}`
  27. // response of "ceph fs snapshot mirror daemon status myfs"
  28. // fsMirrorDaemonStatus = `{ "daemon_id": "444607", "filesystems": [ { "filesystem_id": "1", "name": "myfs", "directory_count": 0, "peers": [ { "uuid": "4a6983c0-3c9d-40f5-b2a9-2334a4659827", "remote": { "client_name": "client.mirror_remote", "cluster_name": "site-remote", "fs_name": "backup_fs" }, "stats": { "failure_count": 0, "recovery_count": 0 } } ] } ] }`
  29. fsMirrorDaemonStatus = `[{"daemon_id":25103, "filesystems": [{"filesystem_id": 1, "name": "myfs", "directory_count": 0, "peers": []}]}]`
  30. // response of "ceph fs snapshot mirror daemon status"
  31. fsMirrorDaemonStatusNew = `[{"daemon_id":23102, "filesystems": [{"filesystem_id": 2, "name": "myfsNew", "directory_count": 0, "peers": []}]}]`
  32. )
  33. func TestEnableFilesystemSnapshotMirror(t *testing.T) {
  34. fs := "myfs"
  35. executor := &exectest.MockExecutor{}
  36. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  37. if args[0] == "fs" {
  38. assert.Equal(t, "snapshot", args[1])
  39. assert.Equal(t, "mirror", args[2])
  40. assert.Equal(t, "enable", args[3])
  41. assert.Equal(t, fs, args[4])
  42. return "", nil
  43. }
  44. return "", errors.New("unknown command")
  45. }
  46. context := &clusterd.Context{Executor: executor}
  47. err := EnableFilesystemSnapshotMirror(context, AdminTestClusterInfo("mycluster"), fs)
  48. assert.NoError(t, err)
  49. }
  50. func TestDisableFilesystemSnapshotMirror(t *testing.T) {
  51. fs := "myfs"
  52. executor := &exectest.MockExecutor{}
  53. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  54. if args[0] == "fs" {
  55. assert.Equal(t, "snapshot", args[1])
  56. assert.Equal(t, "mirror", args[2])
  57. assert.Equal(t, "disable", args[3])
  58. assert.Equal(t, fs, args[4])
  59. return "", nil
  60. }
  61. return "", errors.New("unknown command")
  62. }
  63. context := &clusterd.Context{Executor: executor}
  64. err := DisableFilesystemSnapshotMirror(context, AdminTestClusterInfo("mycluster"), fs)
  65. assert.NoError(t, err)
  66. }
  67. func TestImportFilesystemMirrorPeer(t *testing.T) {
  68. fs := "myfs"
  69. token := "my-token"
  70. executor := &exectest.MockExecutor{}
  71. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  72. if args[0] == "fs" {
  73. assert.Equal(t, "snapshot", args[1])
  74. assert.Equal(t, "mirror", args[2])
  75. assert.Equal(t, "peer_bootstrap", args[3])
  76. assert.Equal(t, "import", args[4])
  77. assert.Equal(t, fs, args[5])
  78. assert.Equal(t, token, args[6])
  79. return "", nil
  80. }
  81. return "", errors.New("unknown command")
  82. }
  83. context := &clusterd.Context{Executor: executor}
  84. err := ImportFSMirrorBootstrapPeer(context, AdminTestClusterInfo("mycluster"), fs, token)
  85. assert.NoError(t, err)
  86. }
  87. func TestCreateFSMirrorBootstrapPeer(t *testing.T) {
  88. fs := "myfs"
  89. executor := &exectest.MockExecutor{}
  90. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  91. if args[0] == "fs" {
  92. assert.Equal(t, "snapshot", args[1])
  93. assert.Equal(t, "mirror", args[2])
  94. assert.Equal(t, "peer_bootstrap", args[3])
  95. assert.Equal(t, "create", args[4])
  96. assert.Equal(t, fs, args[5])
  97. return fsMirrorToken, nil
  98. }
  99. return "", errors.New("unknown command")
  100. }
  101. context := &clusterd.Context{Executor: executor}
  102. token, err := CreateFSMirrorBootstrapPeer(context, AdminTestClusterInfo("mycluster"), fs)
  103. assert.NoError(t, err)
  104. _, err = base64.StdEncoding.DecodeString(string(token))
  105. assert.NoError(t, err)
  106. }
  107. func TestRemoveFilesystemMirrorPeer(t *testing.T) {
  108. peerUUID := "peer-uuid"
  109. executor := &exectest.MockExecutor{}
  110. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  111. logger.Infof("Command: %s %v", command, args)
  112. if args[0] == "fs" {
  113. assert.Equal(t, "snapshot", args[1])
  114. assert.Equal(t, "mirror", args[2])
  115. assert.Equal(t, "peer_remove", args[3])
  116. assert.Equal(t, peerUUID, args[4])
  117. return "", nil
  118. }
  119. return "", errors.New("unknown command")
  120. }
  121. context := &clusterd.Context{Executor: executor}
  122. err := RemoveFilesystemMirrorPeer(context, AdminTestClusterInfo("mycluster"), peerUUID)
  123. assert.NoError(t, err)
  124. }
  125. func TestFSMirrorDaemonStatus(t *testing.T) {
  126. fs := "myfs"
  127. executor := &exectest.MockExecutor{}
  128. t.Run("snapshot status command with fsName - test for Ceph v16.2.6 and earlier", func(t *testing.T) {
  129. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  130. if args[0] == "fs" {
  131. assert.Equal(t, "snapshot", args[1])
  132. assert.Equal(t, "mirror", args[2])
  133. assert.Equal(t, "daemon", args[3])
  134. assert.Equal(t, "status", args[4])
  135. assert.Equal(t, fs, args[5]) // fs-name needed for Ceph v16.2.6 and earlier
  136. return fsMirrorDaemonStatus, nil
  137. }
  138. return "", errors.New("unknown command")
  139. }
  140. context := &clusterd.Context{Executor: executor}
  141. clusterInfo := AdminTestClusterInfo("mycluster")
  142. clusterInfo.CephVersion = cephver.CephVersion{Major: 16, Minor: 2, Extra: 6}
  143. s, err := GetFSMirrorDaemonStatus(context, clusterInfo, fs)
  144. assert.NoError(t, err)
  145. assert.Equal(t, 25103, s[0].DaemonID)
  146. assert.Equal(t, "myfs", s[0].Filesystems[0].Name)
  147. })
  148. t.Run("snapshot status command without fsName - test for Ceph v16.2.7 and above", func(t *testing.T) {
  149. executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
  150. if args[0] == "fs" {
  151. assert.Equal(t, "snapshot", args[1])
  152. assert.Equal(t, "mirror", args[2])
  153. assert.Equal(t, "daemon", args[3])
  154. assert.Equal(t, "status", args[4])
  155. assert.NotEqual(t, fs, args[5])
  156. return fsMirrorDaemonStatusNew, nil
  157. }
  158. return "", errors.New("unknown command")
  159. }
  160. context := &clusterd.Context{Executor: executor}
  161. clusterInfo := AdminTestClusterInfo("mycluster")
  162. clusterInfo.CephVersion = cephver.CephVersion{Major: 16, Minor: 2, Extra: 7}
  163. s, err := GetFSMirrorDaemonStatus(context, clusterInfo, fs)
  164. assert.NoError(t, err)
  165. assert.Equal(t, 23102, s[0].DaemonID)
  166. assert.Equal(t, "myfsNew", s[0].Filesystems[0].Name)
  167. })
  168. }