Skip to content

Commit

Permalink
Refactor: split up common, testutils
Browse files Browse the repository at this point in the history
Centralize testutils, split up common to avoid import cycles. Also fix
up some whitespace inconsistency

Signed-off-by: Peter Sabaini <[email protected]>
  • Loading branch information
sabaini committed Feb 12, 2024
1 parent 7bff436 commit 42ccaf1
Show file tree
Hide file tree
Showing 36 changed files with 257 additions and 206 deletions.
19 changes: 10 additions & 9 deletions microceph/api/client_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"database/sql"
"encoding/json"
"fmt"
"github.com/canonical/microceph/microceph/contants"
"github.com/canonical/microceph/microceph/interfaces"
"net/http"

"github.com/canonical/lxd/lxd/response"
"github.com/canonical/lxd/shared/logger"
"github.com/canonical/microceph/microceph/api/types"
"github.com/canonical/microceph/microceph/ceph"
"github.com/canonical/microceph/microceph/client"
"github.com/canonical/microceph/microceph/common"
"github.com/canonical/microceph/microceph/database"
"github.com/canonical/microcluster/rest"
"github.com/canonical/microcluster/state"
Expand Down Expand Up @@ -40,7 +41,7 @@ func cmdClientConfigsGet(s *state.State, r *http.Request) response.Response {
return response.InternalError(err)
}

if req.Host == common.ClientConfigGlobalHostConst {
if req.Host == contants.ClientConfigGlobalHostConst {
configs, err = database.ClientConfigQuery.GetAll(s)
} else {
configs, err = database.ClientConfigQuery.GetAllForHost(s, req.Host)
Expand Down Expand Up @@ -70,7 +71,7 @@ func cmdClientConfigsPut(s *state.State, r *http.Request) response.Response {
return response.BadRequest(err)
}

err = ceph.UpdateConfig(common.CephState{State: s})
err = ceph.UpdateConfig(interfaces.CephState{State: s})
if err != nil {
logger.Error(err.Error())
response.InternalError(err)
Expand All @@ -97,7 +98,7 @@ func clientConfigsKeyGet(s *state.State, r *http.Request) response.Response {
return response.InternalError(err)
}

if req.Host == common.ClientConfigGlobalHostConst {
if req.Host == contants.ClientConfigGlobalHostConst {
configs, err = database.ClientConfigQuery.GetAllForKey(s, req.Key)
} else {
configs, err = database.ClientConfigQuery.GetAllForKeyAndHost(s, req.Key, req.Host)
Expand Down Expand Up @@ -142,7 +143,7 @@ func clientConfigsKeyDelete(s *state.State, r *http.Request) response.Response {
return response.InternalError(err)
}

if req.Host == common.ClientConfigGlobalHostConst {
if req.Host == contants.ClientConfigGlobalHostConst {
err = database.ClientConfigQuery.RemoveAllForKey(s, req.Key)
} else {
err = database.ClientConfigQuery.RemoveOneForKeyAndHost(s, req.Key, req.Host)
Expand All @@ -161,20 +162,20 @@ func clientConfigsKeyDelete(s *state.State, r *http.Request) response.Response {
func clientConfigUpdate(s *state.State, wait bool) error {
if wait {
// Execute update conf synchronously
err := client.SendUpdateClientConfRequestToClusterMembers(common.CephState{State: s})
err := client.SendUpdateClientConfRequestToClusterMembers(interfaces.CephState{State: s})
if err != nil {
return err
}

// Update on current host.
err = ceph.UpdateConfig(common.CephState{State: s})
err = ceph.UpdateConfig(interfaces.CephState{State: s})
if err != nil {
return err
}
} else { // Execute update asynchronously
go func() {
client.SendUpdateClientConfRequestToClusterMembers(common.CephState{State: s})
ceph.UpdateConfig(common.CephState{State: s}) // Restart on current host.
client.SendUpdateClientConfRequestToClusterMembers(interfaces.CephState{State: s})
ceph.UpdateConfig(interfaces.CephState{State: s}) // Restart on current host.
}()
}

Expand Down
4 changes: 2 additions & 2 deletions microceph/api/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package api
import (
"encoding/json"
"fmt"
"github.com/canonical/microceph/microceph/interfaces"
"net/http"
"net/url"
"strconv"
"sync"

"github.com/canonical/lxd/shared/logger"
"github.com/canonical/microceph/microceph/common"
"github.com/gorilla/mux"

"github.com/canonical/lxd/lxd/response"
Expand Down Expand Up @@ -115,7 +115,7 @@ func cmdDisksDelete(s *state.State, r *http.Request) response.Response {
mu.Lock()
defer mu.Unlock()

cs := common.CephState{State: s}
cs := interfaces.CephState{State: s}
needDowngrade, err := ceph.IsDowngradeNeeded(cs, osdid)
if err != nil {
return response.InternalError(err)
Expand Down
11 changes: 5 additions & 6 deletions microceph/api/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package api
import (
"encoding/json"
"fmt"
"github.com/canonical/microceph/microceph/interfaces"
"net/http"
"path"

"github.com/canonical/microceph/microceph/api/types"
"github.com/canonical/microceph/microceph/common"

"github.com/canonical/lxd/lxd/response"
"github.com/canonical/lxd/shared/logger"
"github.com/canonical/microceph/microceph/api/types"
"github.com/canonical/microcluster/rest"
"github.com/canonical/microcluster/state"

Expand Down Expand Up @@ -64,7 +63,7 @@ func cmdEnableServicePut(s *state.State, r *http.Request) response.Response {
return response.InternalError(err)
}

err = ceph.ServicePlacementHandler(common.CephState{State: s}, payload)
err = ceph.ServicePlacementHandler(interfaces.CephState{State: s}, payload)
if err != nil {
return response.SyncResponse(false, err)
}
Expand Down Expand Up @@ -119,7 +118,7 @@ func cmdDeleteService(s *state.State, r *http.Request) response.Response {
return response.InternalError(err)
}

err := ceph.DeleteService(common.CephState{State: s}, which)
err := ceph.DeleteService(interfaces.CephState{State: s}, which)
if err != nil {
return response.SyncResponse(false, err)
}
Expand All @@ -128,7 +127,7 @@ func cmdDeleteService(s *state.State, r *http.Request) response.Response {
}

func cmdRGWServiceDelete(s *state.State, r *http.Request) response.Response {
err := ceph.DisableRGW(common.CephState{State: s})
err := ceph.DisableRGW(interfaces.CephState{State: s})
if err != nil {
logger.Errorf("Failed disabling RGW: %v", err)
return response.SmartError(err)
Expand Down
22 changes: 12 additions & 10 deletions microceph/ceph/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"context"
"database/sql"
"fmt"
"github.com/canonical/microceph/microceph/contants"
"github.com/canonical/microceph/microceph/interfaces"
"os"
"path/filepath"
"strings"
Expand All @@ -17,9 +19,9 @@ import (
)

// Bootstrap will initialize a new Ceph deployment.
func Bootstrap(s common.StateInterface, data common.BootstrapConfig) error {
pathConsts := common.GetPathConst()
pathFileMode := common.GetPathFileMode()
func Bootstrap(s interfaces.StateInterface, data common.BootstrapConfig) error {
pathConsts := contants.GetPathConst()
pathFileMode := contants.GetPathFileMode()

// Create our various paths.
for path, perm := range pathFileMode {
Expand Down Expand Up @@ -141,7 +143,7 @@ func setDefaultNetwork(cn string) error {
return nil
}

func prepareCephBootstrapData(s common.StateInterface, data *common.BootstrapConfig) error {
func prepareCephBootstrapData(s interfaces.StateInterface, data *common.BootstrapConfig) error {
var err error

// if no mon-ip is provided, either deduce from public network or fallback to default.
Expand Down Expand Up @@ -205,7 +207,7 @@ func createKeyrings(confPath string) (string, error) {
return path, nil
}

func createMonMap(s common.StateInterface, path string, fsid string, address string) error {
func createMonMap(s interfaces.StateInterface, path string, fsid string, address string) error {
// Generate initial monitor map.
err := genMonmap(filepath.Join(path, "mon.map"), fsid)
if err != nil {
Expand All @@ -220,7 +222,7 @@ func createMonMap(s common.StateInterface, path string, fsid string, address str
return nil
}

func initMon(s common.StateInterface, dataPath string, path string) error {
func initMon(s interfaces.StateInterface, dataPath string, path string) error {
// Bootstrap the initial monitor.
monDataPath := filepath.Join(dataPath, "mon", fmt.Sprintf("ceph-%s", s.ClusterState().Name()))

Expand All @@ -241,7 +243,7 @@ func initMon(s common.StateInterface, dataPath string, path string) error {
return nil
}

func initMgr(s common.StateInterface, dataPath string) error {
func initMgr(s interfaces.StateInterface, dataPath string) error {
// Bootstrap the initial manager.
mgrDataPath := filepath.Join(dataPath, "mgr", fmt.Sprintf("ceph-%s", s.ClusterState().Name()))

Expand All @@ -263,7 +265,7 @@ func initMgr(s common.StateInterface, dataPath string) error {
}

// populateDatabase injects the bootstrap entries to the internal database.
func populateDatabase(s common.StateInterface, fsid string, adminKey string, data common.BootstrapConfig) error {
func populateDatabase(s interfaces.StateInterface, fsid string, adminKey string, data common.BootstrapConfig) error {
if s.ClusterState().Database == nil {
return fmt.Errorf("no database")
}
Expand Down Expand Up @@ -320,7 +322,7 @@ func enableMsgr2() error {
return nil
}

func startOSDs(s common.StateInterface, path string) error {
func startOSDs(s interfaces.StateInterface, path string) error {
// Start OSD service.
err := snapStart("osd", true)
if err != nil {
Expand All @@ -329,7 +331,7 @@ func startOSDs(s common.StateInterface, path string) error {
return nil
}

func initMds(s common.StateInterface, dataPath string) error {
func initMds(s interfaces.StateInterface, dataPath string) error {
// Bootstrap the initial metadata server.
mdsDataPath := filepath.Join(dataPath, "mds", fmt.Sprintf("ceph-%s", s.ClusterState().Name()))

Expand Down
38 changes: 20 additions & 18 deletions microceph/ceph/bootstrap_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ceph

import (
"github.com/canonical/microceph/microceph/interfaces"
"github.com/canonical/microceph/microceph/testutil"
"testing"

"github.com/canonical/lxd/shared/api"
Expand All @@ -13,7 +15,7 @@ import (
)

type bootstrapSuite struct {
baseSuite
testutil.BaseSuite
TestStateInterface *mocks.StateInterface
}

Expand All @@ -23,48 +25,48 @@ func TestBootstrap(t *testing.T) {

// Expect: run ceph-authtool 3 times
func addCreateKeyringExpectations(r *mocks.Runner) {
r.On("RunCommand", cmdAny("ceph-authtool", 8)...).Return("ok", nil).Once()
r.On("RunCommand", cmdAny("ceph-authtool", 17)...).Return("ok", nil).Once()
r.On("RunCommand", cmdAny("ceph-authtool", 3)...).Return("ok", nil).Once()
r.On("RunCommand", testutil.CmdAny("ceph-authtool", 8)...).Return("ok", nil).Once()
r.On("RunCommand", testutil.CmdAny("ceph-authtool", 17)...).Return("ok", nil).Once()
r.On("RunCommand", testutil.CmdAny("ceph-authtool", 3)...).Return("ok", nil).Once()
}

// Expect: run monmaptool 2 times
func addCreateMonMapExpectations(r *mocks.Runner) {
r.On("RunCommand", cmdAny("monmaptool", 8)...).Return("ok", nil).Once()
r.On("RunCommand", cmdAny("monmaptool", 17)...).Return("ok", nil).Once()
r.On("RunCommand", testutil.CmdAny("monmaptool", 8)...).Return("ok", nil).Once()
r.On("RunCommand", testutil.CmdAny("monmaptool", 17)...).Return("ok", nil).Once()
}

// Expect: run ceph-mon and snap start
func addInitMonExpectations(r *mocks.Runner) {
r.On("RunCommand", cmdAny("ceph-mon", 9)...).Return("ok", nil).Once()
r.On("RunCommand", cmdAny("snapctl", 3)...).Return("ok", nil).Once()
r.On("RunCommand", testutil.CmdAny("ceph-mon", 9)...).Return("ok", nil).Once()
r.On("RunCommand", testutil.CmdAny("snapctl", 3)...).Return("ok", nil).Once()
}

// Expect: run ceph and snap start
func addInitMgrExpectations(r *mocks.Runner) {
r.On("RunCommand", cmdAny("ceph", 11)...).Return("ok", nil).Once()
r.On("RunCommand", cmdAny("snapctl", 3)...).Return("ok", nil).Once()
r.On("RunCommand", testutil.CmdAny("ceph", 11)...).Return("ok", nil).Once()
r.On("RunCommand", testutil.CmdAny("snapctl", 3)...).Return("ok", nil).Once()
}

// Expect: run ceph and snap start
func addInitMdsExpectations(r *mocks.Runner) {
r.On("RunCommand", cmdAny("ceph", 13)...).Return("ok", nil).Once()
r.On("RunCommand", cmdAny("snapctl", 3)...).Return("ok", nil).Once()
r.On("RunCommand", testutil.CmdAny("ceph", 13)...).Return("ok", nil).Once()
r.On("RunCommand", testutil.CmdAny("snapctl", 3)...).Return("ok", nil).Once()
}

// Expect: run ceph and snap start
func addEnableMsgr2Expectations(r *mocks.Runner) {
r.On("RunCommand", cmdAny("ceph", 2)...).Return("ok", nil).Once()
r.On("RunCommand", cmdAny("snapctl", 3)...).Return("ok", nil).Once()
r.On("RunCommand", testutil.CmdAny("ceph", 2)...).Return("ok", nil).Once()
r.On("RunCommand", testutil.CmdAny("snapctl", 3)...).Return("ok", nil).Once()
}

// Expect: check network coherency
func addNetworkExpectationsBootstrap(nw *mocks.NetworkIntf, s common.StateInterface) {
func addNetworkExpectationsBootstrap(nw *mocks.NetworkIntf, s interfaces.StateInterface) {
nw.On("IsIpOnSubnet", "1.1.1.1", "1.1.1.1/24").Return(true)
}

// Expect: check Bootstrap data prep
func addNetworkExpectations(nw *mocks.NetworkIntf, s common.StateInterface) {
func addNetworkExpectations(nw *mocks.NetworkIntf, s interfaces.StateInterface) {
nw.On("IsIpOnSubnet", "1.1.1.1", "1.1.1.1/24").Return(true)
nw.On("FindNetworkAddress", "1.1.1.1").Return("1.1.1.1/24", nil)
nw.On("FindIpOnSubnet", "1.1.1.1/24").Return("1.1.1.1", nil)
Expand All @@ -74,8 +76,8 @@ func addNetworkExpectations(nw *mocks.NetworkIntf, s common.StateInterface) {

func (s *bootstrapSuite) SetupTest() {

s.baseSuite.SetupTest()
s.copyCephConfigs()
s.BaseSuite.SetupTest()
s.CopyCephConfigs()

s.TestStateInterface = mocks.NewStateInterface(s.T())
u := api.NewURL()
Expand Down
3 changes: 2 additions & 1 deletion microceph/ceph/client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ceph

import (
"fmt"
"github.com/canonical/microceph/microceph/interfaces"
"reflect"

"github.com/canonical/microceph/microceph/common"
Expand All @@ -21,7 +22,7 @@ type ClientConfigT struct {
}

// GetClientConfigForHost fetches all the applicable client configurations for the provided host.
func GetClientConfigForHost(s common.StateInterface, hostname string) (ClientConfigT, error) {
func GetClientConfigForHost(s interfaces.StateInterface, hostname string) (ClientConfigT, error) {
retval := ClientConfigT{}

// Get all client configs for the current host.
Expand Down
5 changes: 3 additions & 2 deletions microceph/ceph/client_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ceph

import (
"fmt"
"github.com/canonical/microceph/microceph/testutil"
"reflect"
"testing"

Expand All @@ -13,7 +14,7 @@ import (
)

type ClientConfigSuite struct {
baseSuite
testutil.BaseSuite
TestStateInterface *mocks.StateInterface
}

Expand All @@ -22,7 +23,7 @@ func TestClientConfig(t *testing.T) {
}

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

ccs.TestStateInterface = mocks.NewStateInterface(ccs.T())
state := &state.State{}
Expand Down
3 changes: 2 additions & 1 deletion microceph/ceph/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/json"
"fmt"
"github.com/canonical/microceph/microceph/interfaces"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -162,7 +163,7 @@ func ListConfigs() (types.Configs, error) {
}

// updates the ceph config file.
func UpdateConfig(s common.StateInterface) error {
func UpdateConfig(s interfaces.StateInterface) error {
confPath := filepath.Join(os.Getenv("SNAP_DATA"), "conf")
runPath := filepath.Join(os.Getenv("SNAP_DATA"), "run")

Expand Down
Loading

0 comments on commit 42ccaf1

Please sign in to comment.