diff --git a/microceph/ceph/client_config_test.go b/microceph/ceph/client_config_test.go new file mode 100644 index 00000000..91bbfc8c --- /dev/null +++ b/microceph/ceph/client_config_test.go @@ -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) + } +} diff --git a/microceph/mocks/ClientConfigQueryIntf.go b/microceph/mocks/ClientConfigQueryIntf.go new file mode 100644 index 00000000..8208492c --- /dev/null +++ b/microceph/mocks/ClientConfigQueryIntf.go @@ -0,0 +1,174 @@ +// Code generated by mockery v2.30.10. DO NOT EDIT. + +package mocks + +import ( + state "github.com/canonical/microcluster/state" + mock "github.com/stretchr/testify/mock" + database "github.com/canonical/microceph/microceph/database" +) + +// ClientConfigQueryIntf is an autogenerated mock type for the ClientConfigQueryIntf type +type ClientConfigQueryIntf struct { + mock.Mock +} + +// AddNew provides a mock function with given fields: s, key, value, host +func (_m *ClientConfigQueryIntf) AddNew(s *state.State, key string, value string, host string) error { + ret := _m.Called(s, key, value, host) + + var r0 error + if rf, ok := ret.Get(0).(func(*state.State, string, string, string) error); ok { + r0 = rf(s, key, value, host) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// GetAll provides a mock function with given fields: s +func (_m *ClientConfigQueryIntf) GetAll(s *state.State) (database.ClientConfigItems, error) { + ret := _m.Called(s) + + var r0 database.ClientConfigItems + var r1 error + if rf, ok := ret.Get(0).(func(*state.State) (database.ClientConfigItems, error)); ok { + return rf(s) + } + if rf, ok := ret.Get(0).(func(*state.State) database.ClientConfigItems); ok { + r0 = rf(s) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(database.ClientConfigItems) + } + } + + if rf, ok := ret.Get(1).(func(*state.State) error); ok { + r1 = rf(s) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetAllForHost provides a mock function with given fields: s, host +func (_m *ClientConfigQueryIntf) GetAllForHost(s *state.State, host string) (database.ClientConfigItems, error) { + ret := _m.Called(s, host) + + var r0 database.ClientConfigItems + var r1 error + if rf, ok := ret.Get(0).(func(*state.State, string) (database.ClientConfigItems, error)); ok { + return rf(s, host) + } + if rf, ok := ret.Get(0).(func(*state.State, string) database.ClientConfigItems); ok { + r0 = rf(s, host) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(database.ClientConfigItems) + } + } + + if rf, ok := ret.Get(1).(func(*state.State, string) error); ok { + r1 = rf(s, host) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetAllForKey provides a mock function with given fields: s, key +func (_m *ClientConfigQueryIntf) GetAllForKey(s *state.State, key string) (database.ClientConfigItems, error) { + ret := _m.Called(s, key) + + var r0 database.ClientConfigItems + var r1 error + if rf, ok := ret.Get(0).(func(*state.State, string) (database.ClientConfigItems, error)); ok { + return rf(s, key) + } + if rf, ok := ret.Get(0).(func(*state.State, string) database.ClientConfigItems); ok { + r0 = rf(s, key) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(database.ClientConfigItems) + } + } + + if rf, ok := ret.Get(1).(func(*state.State, string) error); ok { + r1 = rf(s, key) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// GetAllForKeyAndHost provides a mock function with given fields: s, key, host +func (_m *ClientConfigQueryIntf) GetAllForKeyAndHost(s *state.State, key string, host string) (database.ClientConfigItems, error) { + ret := _m.Called(s, key, host) + + var r0 database.ClientConfigItems + var r1 error + if rf, ok := ret.Get(0).(func(*state.State, string, string) (database.ClientConfigItems, error)); ok { + return rf(s, key, host) + } + if rf, ok := ret.Get(0).(func(*state.State, string, string) database.ClientConfigItems); ok { + r0 = rf(s, key, host) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(database.ClientConfigItems) + } + } + + if rf, ok := ret.Get(1).(func(*state.State, string, string) error); ok { + r1 = rf(s, key, host) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// RemoveAllForKey provides a mock function with given fields: s, key +func (_m *ClientConfigQueryIntf) RemoveAllForKey(s *state.State, key string) error { + ret := _m.Called(s, key) + + var r0 error + if rf, ok := ret.Get(0).(func(*state.State, string) error); ok { + r0 = rf(s, key) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// RemoveOneForKeyAndHost provides a mock function with given fields: s, key, host +func (_m *ClientConfigQueryIntf) RemoveOneForKeyAndHost(s *state.State, key string, host string) error { + ret := _m.Called(s, key, host) + + var r0 error + if rf, ok := ret.Get(0).(func(*state.State, string, string) error); ok { + r0 = rf(s, key, host) + } else { + r0 = ret.Error(0) + } + + return r0 +} + +// NewClientConfigQueryIntf creates a new instance of ClientConfigQueryIntf. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewClientConfigQueryIntf(t interface { + mock.TestingT + Cleanup(func()) +}) *ClientConfigQueryIntf { + mock := &ClientConfigQueryIntf{} + mock.Mock.Test(t) + + t.Cleanup(func() { mock.AssertExpectations(t) }) + + return mock +}