Skip to content

Commit

Permalink
Added unit tests for client config
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 291c10d commit ae46a9f
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 ae46a9f

Please sign in to comment.