Skip to content

Commit

Permalink
Added unit tests :D
Browse files Browse the repository at this point in the history
Signed-off-by: Utkarsh Bhatt <[email protected]>
  • Loading branch information
UtkarshBhatthere committed Oct 9, 2024
1 parent 9e5a6f8 commit 70deef7
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
95 changes: 95 additions & 0 deletions microceph/ceph/rbd_mirror_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package ceph

import (
"os"
"testing"

"github.com/canonical/microceph/microceph/api/types"
"github.com/canonical/microceph/microceph/mocks"
"github.com/canonical/microceph/microceph/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type RbdMirrorSuite struct {
tests.BaseSuite
TestStateInterface *mocks.StateInterface
}

func TestRbdMirror(t *testing.T) {
suite.Run(t, new(RbdMirrorSuite))
}

func (ks *RbdMirrorSuite) SetupTest() {
ks.BaseSuite.SetupTest()
ks.CopyCephConfigs()
}

func (ks *RbdMirrorSuite) TestVerbosePoolStatus() {
r := mocks.NewRunner(ks.T())

output, _ := os.ReadFile("./test_assets/rbd_mirror_verbose_pool_status.json")

// mocks and expectations
r.On("RunCommand", []interface{}{
"rbd", "mirror", "pool", "status", "pool", "--verbose", "--format", "json"}...).Return(string(output), nil).Once()
processExec = r

// Method call
resp, err := GetRbdMirrorVerbosePoolStatus("pool", "", "")
assert.NoError(ks.T(), err)
assert.Equal(ks.T(), resp.Name, "pool")
}

func (ks *RbdMirrorSuite) TestPoolStatus() {
r := mocks.NewRunner(ks.T())

output, _ := os.ReadFile("./test_assets/rbd_mirror_pool_status.json")

// mocks and expectations
r.On("RunCommand", []interface{}{
"rbd", "mirror", "pool", "status", "pool", "--format", "json"}...).Return(string(output), nil).Once()
processExec = r

// Method call
resp, err := GetRbdMirrorPoolStatus("pool", "", "")
assert.NoError(ks.T(), err)
assert.Equal(ks.T(), resp.Health, RbdReplicationHealth("OK"))
assert.Equal(ks.T(), resp.DaemonHealth, RbdReplicationHealth("OK"))
assert.Equal(ks.T(), resp.ImageHealth, RbdReplicationHealth("OK"))
}

func (ks *RbdMirrorSuite) TestImageStatus() {
r := mocks.NewRunner(ks.T())

output, _ := os.ReadFile("./test_assets/rbd_mirror_image_status.json")

// mocks and expectations
r.On("RunCommand", []interface{}{
"rbd", "mirror", "image", "status", "pool/image_one", "--format", "json"}...).Return(string(output), nil).Once()
processExec = r

// Method call
resp, err := GetRbdMirrorImageStatus("pool", "image_one", "", "")
assert.NoError(ks.T(), err)
assert.Equal(ks.T(), resp.Name, "image_one")
assert.Equal(ks.T(), resp.IsPrimary, true)
}

func (ks *RbdMirrorSuite) TestPoolInfo() {
r := mocks.NewRunner(ks.T())

output, _ := os.ReadFile("./test_assets/rbd_mirror_pool_info.json")

// mocks and expectations
r.On("RunCommand", []interface{}{
"rbd", "mirror", "pool", "info", "pool", "--format", "json"}...).Return(string(output), nil).Once()
processExec = r

// Method call
resp, err := GetRbdMirrorPoolInfo("pool", "", "")
assert.NoError(ks.T(), err)
assert.Equal(ks.T(), resp.Mode, types.RbdResourcePool)
assert.Equal(ks.T(), resp.LocalSiteName, "magical")
assert.Equal(ks.T(), resp.Peers[0].RemoteName, "simple")
}
22 changes: 22 additions & 0 deletions microceph/ceph/test_assets/rbd_mirror_image_status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "image_one",
"global_id": "c6e4ea14-be4a-41f4-ba76-dbb64595e600",
"state": "up+stopped",
"description": "local image is primary",
"daemon_service": {
"service_id": "14177",
"instance_id": "14221",
"daemon_id": "magical-reindeer",
"hostname": "magical-reindeer"
},
"last_update": "2024-10-09 07:56:24",
"peer_sites": [
{
"site_name": "simple",
"mirror_uuids": "84f58bda-4eea-45b1-9a5a-296cf1b82a65",
"state": "up+replaying",
"description": "replaying, {\"bytes_per_second\":0.0,\"entries_behind_primary\":0,\"entries_per_second\":0.0,\"non_primary_position\":{\"entry_tid\":3,\"object_number\":3,\"tag_tid\":1},\"primary_position\":{\"entry_tid\":3,\"object_number\":3,\"tag_tid\":1}}",
"last_update": "2024-10-09 07:56:28"
}
]
}
13 changes: 13 additions & 0 deletions microceph/ceph/test_assets/rbd_mirror_pool_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"mode": "pool",
"site_name": "magical",
"peers": [
{
"uuid": "f3ee5939-66a6-494f-849a-a4402ddb4d18",
"direction": "rx-tx",
"site_name": "simple",
"mirror_uuid": "84f58bda-4eea-45b1-9a5a-296cf1b82a65",
"client_name": "client.rbd-mirror-peer"
}
]
}
10 changes: 10 additions & 0 deletions microceph/ceph/test_assets/rbd_mirror_pool_status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"summary": {
"health": "OK",
"daemon_health": "OK",
"image_health": "OK",
"states": {
"replaying": 2
}
}
}
67 changes: 67 additions & 0 deletions microceph/ceph/test_assets/rbd_mirror_verbose_pool_status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"summary": {
"health": "OK",
"daemon_health": "OK",
"image_health": "OK",
"states": {
"replaying": 2
}
},
"daemons": [
{
"service_id": "14173",
"instance_id": "14198",
"client_id": "magical-reindeer",
"hostname": "magical-reindeer",
"ceph_version": "19.2.0~git20240301.4c76c50",
"leader": true,
"health": "OK"
}
],
"images": [
{
"name": "image_one",
"global_id": "ebbea3fc-78c5-41e7-a796-d2fc59c691c6",
"state": "up+stopped",
"description": "local image is primary",
"daemon_service": {
"service_id": "14173",
"instance_id": "14198",
"daemon_id": "magical-reindeer",
"hostname": "magical-reindeer"
},
"last_update": "2024-10-09 05:55:27",
"peer_sites": [
{
"site_name": "simple",
"mirror_uuids": "ced68f5f-f982-4ca2-b823-c68be7b86c93",
"state": "up+replaying",
"description": "replaying, {\"bytes_per_second\":0.0,\"entries_behind_primary\":0,\"entries_per_second\":0.0,\"non_primary_position\":{\"entry_tid\":3,\"object_number\":3,\"tag_tid\":1},\"primary_position\":{\"entry_tid\":3,\"object_number\":3,\"tag_tid\":1}}",
"last_update": "2024-10-09 05:55:27"
}
]
},
{
"name": "image_two",
"global_id": "0f35d44b-60fd-4294-adc9-eb7a65815db9",
"state": "up+stopped",
"description": "local image is primary",
"daemon_service": {
"service_id": "14173",
"instance_id": "14198",
"daemon_id": "magical-reindeer",
"hostname": "magical-reindeer"
},
"last_update": "2024-10-09 05:55:27",
"peer_sites": [
{
"site_name": "simple",
"mirror_uuids": "ced68f5f-f982-4ca2-b823-c68be7b86c93",
"state": "up+replaying",
"description": "replaying, {\"bytes_per_second\":0.0,\"entries_behind_primary\":0,\"entries_per_second\":0.0,\"non_primary_position\":{\"entry_tid\":3,\"object_number\":3,\"tag_tid\":1},\"primary_position\":{\"entry_tid\":3,\"object_number\":3,\"tag_tid\":1}}",
"last_update": "2024-10-09 05:55:27"
}
]
}
]
}

0 comments on commit 70deef7

Please sign in to comment.