Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Utkarsh Bhatt <utkarsh.bhatt@canonical.com>
  • Loading branch information
UtkarshBhatthere committed Oct 13, 2023
1 parent c0d7217 commit 751f2b4
Show file tree
Hide file tree
Showing 2 changed files with 239 additions and 0 deletions.
65 changes: 65 additions & 0 deletions microceph/ceph/client_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package ceph

import (
"fmt"
"reflect"
"testing"

"github.com/canonical/microceph/microceph/database"
"github.com/canonical/microceph/microceph/mocks"
"github.com/canonical/microcluster/state"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type ClientConfigSuite struct {
BaseSuite
TestStateInterface *mocks.StateInterface
}

func TestClientConfig(t *testing.T) {
suite.Run(t, new(ClientConfigSuite))
}

func (ccs *ClientConfigSuite) SetupTest() {
ccs.BaseSuite.SetupTest()

ccs.TestStateInterface = mocks.NewStateInterface(ccs.T())
state := &state.State{}

ccs.TestStateInterface.On("ClusterState").Return(state)
}

func addGetHostConfigsExpectation(mci *mocks.ClientConfigQueryIntf, cs *state.State, hostname string) {
output := database.ClientConfigItems{}
count := 0
for configKey, field := range GetClientConfigSet() {
count++
output = append(output, database.ClientConfigItem{
ID: count,
Host: hostname,
Key: configKey,
Value: fmt.Sprintf("%v", field),
})
}

mci.On("GetAllForHost", cs, hostname).Return(output, nil)
}

func (ccs *ClientConfigSuite) TestFetchHostConfig() {
hostname := "testHostname"

// Mock Client config query interface.
ccq := mocks.NewClientConfigQueryIntf(ccs.T())
addGetHostConfigsExpectation(ccq, ccs.TestStateInterface.ClusterState(), hostname)
database.ClientConfigQuery = ccq

configs, err := GetClientConfigForHost(ccs.TestStateInterface, hostname)
assert.NoError(ccs.T(), err)

// check fields
metaConfigs := reflect.ValueOf(configs)
for i := 0; i < metaConfigs.NumField(); i++ {
assert.Equal(ccs.T(), metaConfigs.Field(i).Interface(), metaConfigs.Type().Field(i).Name)
}
}
174 changes: 174 additions & 0 deletions microceph/mocks/ClientConfigQueryIntf.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 751f2b4

Please sign in to comment.