golangci-lint linter gosec showing more errors than gosec gh. This commit resolve new errors. And, removing nosec comments from autogenerated files. Signed-off-by: subhamkrai <srai@redhat.com>
159 lines
6.1 KiB
Go
159 lines
6.1 KiB
Go
/*
|
|
Copyright 2020 The Rook Authors. All rights reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package client
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/pkg/errors"
|
|
cephv1 "github.com/rook/rook/pkg/apis/ceph.rook.io/v1"
|
|
"github.com/rook/rook/pkg/clusterd"
|
|
exectest "github.com/rook/rook/pkg/util/exec/test"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
var (
|
|
bootstrapPeerToken = `eyJmc2lkIjoiYzZiMDg3ZjItNzgyOS00ZGJiLWJjZmMtNTNkYzM0ZTBiMzVkIiwiY2xpZW50X2lkIjoicmJkLW1pcnJvci1wZWVyIiwia2V5IjoiQVFBV1lsWmZVQ1Q2RGhBQVBtVnAwbGtubDA5YVZWS3lyRVV1NEE9PSIsIm1vbl9ob3N0IjoiW3YyOjE5Mi4xNjguMTExLjEwOjMzMDAsdjE6MTkyLjE2OC4xMTEuMTA6Njc4OV0sW3YyOjE5Mi4xNjguMTExLjEyOjMzMDAsdjE6MTkyLjE2OC4xMTEuMTI6Njc4OV0sW3YyOjE5Mi4xNjguMTExLjExOjMzMDAsdjE6MTkyLjE2OC4xMTEuMTE6Njc4OV0ifQ==` //nolint:gosec // This is just a var name, not a real token
|
|
mirrorStatus = `{"summary":{"health":"WARNING","daemon_health":"OK","image_health":"WARNING","states":{"starting_replay":1,"replaying":1}}}`
|
|
mirrorInfo = `{"mode":"image","site_name":"39074576-5884-4ef3-8a4d-8a0c5ed33031","peers":[{"uuid":"4a6983c0-3c9d-40f5-b2a9-2334a4659827","direction":"rx-tx","site_name":"ocs","mirror_uuid":"","client_name":"client.rbd-mirror-peer"}]}`
|
|
)
|
|
|
|
func TestCreateRBDMirrorBootstrapPeer(t *testing.T) {
|
|
pool := "pool-test"
|
|
executor := &exectest.MockExecutor{}
|
|
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
|
|
if args[0] == "mirror" {
|
|
assert.Equal(t, "pool", args[1])
|
|
assert.Equal(t, "peer", args[2])
|
|
assert.Equal(t, "bootstrap", args[3])
|
|
assert.Equal(t, "create", args[4])
|
|
assert.Equal(t, pool, args[5])
|
|
assert.Equal(t, "--site-name", args[6])
|
|
assert.Equal(t, "4fe04ebb-ec0c-46c2-ac55-9eb52ebbfb82-mycluster", args[7])
|
|
return bootstrapPeerToken, nil
|
|
}
|
|
return "", errors.New("unknown command")
|
|
}
|
|
context := &clusterd.Context{Executor: executor}
|
|
c := AdminClusterInfo("mycluster")
|
|
c.FSID = "4fe04ebb-ec0c-46c2-ac55-9eb52ebbfb82"
|
|
|
|
token, err := CreateRBDMirrorBootstrapPeer(context, c, pool)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, bootstrapPeerToken, string(token))
|
|
}
|
|
func TestEnablePoolMirroring(t *testing.T) {
|
|
pool := "pool-test"
|
|
poolSpec := cephv1.PoolSpec{Mirroring: cephv1.MirroringSpec{Mode: "image"}}
|
|
executor := &exectest.MockExecutor{}
|
|
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
|
|
if args[0] == "mirror" {
|
|
assert.Equal(t, "pool", args[1])
|
|
assert.Equal(t, "enable", args[2])
|
|
assert.Equal(t, pool, args[3])
|
|
assert.Equal(t, poolSpec.Mirroring.Mode, args[4])
|
|
return "", nil
|
|
}
|
|
return "", errors.New("unknown command")
|
|
}
|
|
context := &clusterd.Context{Executor: executor}
|
|
|
|
err := EnablePoolMirroring(context, AdminClusterInfo("mycluster"), poolSpec, pool)
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestGetPoolMirroringStatus(t *testing.T) {
|
|
pool := "pool-test"
|
|
executor := &exectest.MockExecutor{}
|
|
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
|
|
if args[0] == "mirror" {
|
|
assert.Equal(t, "pool", args[1])
|
|
assert.Equal(t, "status", args[2])
|
|
assert.Equal(t, pool, args[3])
|
|
return mirrorStatus, nil
|
|
}
|
|
return "", errors.New("unknown command")
|
|
}
|
|
context := &clusterd.Context{Executor: executor}
|
|
|
|
poolMirrorStatus, err := GetPoolMirroringStatus(context, AdminClusterInfo("mycluster"), pool)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "WARNING", poolMirrorStatus.Summary.Health)
|
|
assert.Equal(t, "OK", poolMirrorStatus.Summary.DaemonHealth)
|
|
}
|
|
|
|
func TestImportRBDMirrorBootstrapPeer(t *testing.T) {
|
|
pool := "pool-test"
|
|
executor := &exectest.MockExecutor{}
|
|
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
|
|
if args[0] == "mirror" {
|
|
assert.Equal(t, "pool", args[1])
|
|
assert.Equal(t, "peer", args[2])
|
|
assert.Equal(t, "bootstrap", args[3])
|
|
assert.Equal(t, "import", args[4])
|
|
assert.Equal(t, pool, args[5])
|
|
assert.Equal(t, "/tmp/rbd-mirror-token-pool-test", args[6])
|
|
assert.Equal(t, 11, len(args))
|
|
return mirrorStatus, nil
|
|
}
|
|
return "", errors.New("unknown command")
|
|
}
|
|
context := &clusterd.Context{Executor: executor}
|
|
|
|
err := ImportRBDMirrorBootstrapPeer(context, AdminClusterInfo("mycluster"), pool, "", []byte(bootstrapPeerToken))
|
|
assert.NoError(t, err)
|
|
|
|
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
|
|
if args[0] == "mirror" {
|
|
assert.Equal(t, "pool", args[1])
|
|
assert.Equal(t, "peer", args[2])
|
|
assert.Equal(t, "bootstrap", args[3])
|
|
assert.Equal(t, "import", args[4])
|
|
assert.Equal(t, pool, args[5])
|
|
assert.Equal(t, "/tmp/rbd-mirror-token-pool-test", args[6])
|
|
assert.Equal(t, "--direction", args[7])
|
|
assert.Equal(t, "rx-tx", args[8])
|
|
assert.Equal(t, 13, len(args))
|
|
return mirrorStatus, nil
|
|
}
|
|
return "", errors.New("unknown command")
|
|
}
|
|
context = &clusterd.Context{Executor: executor}
|
|
err = ImportRBDMirrorBootstrapPeer(context, AdminClusterInfo("mycluster"), pool, "rx-tx", []byte(bootstrapPeerToken))
|
|
assert.NoError(t, err)
|
|
}
|
|
|
|
func TestGetPoolMirroringInfo(t *testing.T) {
|
|
pool := "pool-test"
|
|
executor := &exectest.MockExecutor{}
|
|
executor.MockExecuteCommandWithOutput = func(command string, args ...string) (string, error) {
|
|
if args[0] == "mirror" {
|
|
assert.Equal(t, "pool", args[1])
|
|
assert.Equal(t, "info", args[2])
|
|
assert.Equal(t, pool, args[3])
|
|
return mirrorInfo, nil
|
|
}
|
|
return "", errors.New("unknown command")
|
|
}
|
|
context := &clusterd.Context{Executor: executor}
|
|
|
|
poolMirrorInfo, err := GetPoolMirroringInfo(context, AdminClusterInfo("mycluster"), pool)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "image", poolMirrorInfo.Mode)
|
|
assert.Equal(t, 1, len(poolMirrorInfo.Peers))
|
|
}
|