Skip to content

Commit

Permalink
Refactor cluster package tests (#292)
Browse files Browse the repository at this point in the history
* Refactor crm and cibadmin code tests

* Refactor sbd code tests

* Refactor cluster code tests

* Update cluster object mock
  • Loading branch information
arbulu89 authored Nov 20, 2023
1 parent 9db3a3d commit a7d0405
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 236 deletions.
10 changes: 3 additions & 7 deletions internal/core/cluster/cib/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//nolint:lll
package cib
package cib_test

import (
"testing"

"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/core/cluster/cib"
"github.com/trento-project/agent/test/helpers"
)

Expand All @@ -16,13 +17,8 @@ func TestParserTestSuite(t *testing.T) {
suite.Run(t, new(ParserTestSuite))
}

func (suite *ParserTestSuite) TestConstructor() {
p := NewCibAdminParser("foo")
suite.Equal("foo", p.cibAdminPath)
}

func (suite *ParserTestSuite) TestParse() {
p := NewCibAdminParser(helpers.GetFixturePath("discovery/cluster/fake_cibadmin.sh"))
p := cib.NewCibAdminParser(helpers.GetFixturePath("discovery/cluster/fake_cibadmin.sh"))
data, err := p.Parse()
suite.NoError(err)
suite.Equal(2, len(data.Configuration.Nodes))
Expand Down
11 changes: 6 additions & 5 deletions internal/core/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type DiscoveryTools struct {
CorosyncKeyPath string
SBDPath string
SBDConfigPath string
CommandExecutor utils.CommandExecutor
}

type Cluster struct {
Expand Down Expand Up @@ -68,11 +69,11 @@ func NewCluster() (*Cluster, error) {
CorosyncKeyPath: corosyncKeyPath,
SBDPath: SBDPath,
SBDConfigPath: SBDConfigPath,
CommandExecutor: utils.Executor{},
})
}

func NewClusterWithDiscoveryTools(discoveryTools *DiscoveryTools) (*Cluster, error) {
commandExecutor := utils.Executor{}
cibParser := cib.NewCibAdminParser(discoveryTools.CibAdmPath)

cibConfig, err := cibParser.Parse()
Expand Down Expand Up @@ -110,17 +111,17 @@ func NewClusterWithDiscoveryTools(discoveryTools *DiscoveryTools) (*Cluster, err
cluster.Name = getName(cluster)

if cluster.IsFencingSBD() {
sbdData, err := NewSBD(commandExecutor, cluster.ID, discoveryTools.SBDPath, discoveryTools.SBDConfigPath)
sbdData, err := NewSBD(discoveryTools.CommandExecutor, discoveryTools.SBDPath, discoveryTools.SBDConfigPath)
if err != nil {
return nil, err
}

cluster.SBD = sbdData
}

cluster.DC = isDC(cluster)
cluster.DC = cluster.IsDC()

cloudIdentifier := cloud.NewIdentifier(commandExecutor)
cloudIdentifier := cloud.NewIdentifier(discoveryTools.CommandExecutor)
provider, err := cloudIdentifier.IdentifyCloudProvider()
if err != nil {
log.Warn(errors.Wrap(err, "Cloud provider not identified"))
Expand All @@ -146,7 +147,7 @@ func getName(c *Cluster) string {
return ""
}

func isDC(c *Cluster) bool {
func (c *Cluster) IsDC() bool {
host, _ := os.Hostname()

for _, nodes := range c.Crmmon.Nodes {
Expand Down
66 changes: 40 additions & 26 deletions internal/core/cluster/cluster_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//nolint:exhaustruct
package cluster
package cluster_test

import (
"os"
"testing"

"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/core/cluster"
"github.com/trento-project/agent/internal/core/cluster/cib"
"github.com/trento-project/agent/internal/core/cluster/crmmon"
"github.com/trento-project/agent/pkg/utils/mocks"
"github.com/trento-project/agent/test/helpers"
)

Expand All @@ -19,18 +21,30 @@ func TestClusterTestSuite(t *testing.T) {
suite.Run(t, new(ClusterTestSuite))
}

func (suite *ClusterTestSuite) TestClusterId() {
root := new(cib.Root)

c := Cluster{
Cib: *root,
Name: "sculpin",
ID: "47d1190ffb4f781974c8356d7f863b03",
}

authkey, _ := getCorosyncAuthkeyMd5(helpers.GetFixturePath("discovery/cluster/authkey"))

suite.Equal(c.ID, authkey)
func (suite *ClusterTestSuite) TestNewClusterWithDiscoveryTools() {
mockCommand := new(mocks.CommandExecutor)
mockCommand.On("Exec", "dmidecode", "-s", "chassis-asset-tag").
Return([]byte("7783-7084-3265-9085-8269-3286-77"), nil)
mockCommand.On("Exec", "/usr/sbin/sbd", "-d", "/dev/vdb", "dump").Return(mockSbdDump(), nil)
mockCommand.On("Exec", "/usr/sbin/sbd", "-d", "/dev/vdb", "list").Return(mockSbdList(), nil)
mockCommand.On("Exec", "/usr/sbin/sbd", "-d", "/dev/vdc", "dump").Return(mockSbdDump(), nil)
mockCommand.On("Exec", "/usr/sbin/sbd", "-d", "/dev/vdc", "list").Return(mockSbdList(), nil)

c, err := cluster.NewClusterWithDiscoveryTools(&cluster.DiscoveryTools{
CibAdmPath: helpers.GetFixturePath("discovery/cluster/fake_cibadmin.sh"),
CrmmonAdmPath: helpers.GetFixturePath("discovery/cluster/fake_crm_mon.sh"),
CorosyncKeyPath: helpers.GetFixturePath("discovery/cluster/authkey"),
SBDPath: "/usr/sbin/sbd",
SBDConfigPath: helpers.GetFixturePath("discovery/cluster/sbd/sbd_config"),
CommandExecutor: mockCommand,
})

suite.Equal("hana_cluster", c.Name)
suite.Equal("47d1190ffb4f781974c8356d7f863b03", c.ID)
suite.Equal(false, c.DC)
suite.Equal("azure", c.Provider)
suite.Equal("/dev/vdc;/dev/vdb", c.SBD.Config["SBD_DEVICE"])
suite.NoError(err)
}

func (suite *ClusterTestSuite) TestClusterName() {
Expand All @@ -49,7 +63,7 @@ func (suite *ClusterTestSuite) TestClusterName() {

root.Configuration.CrmConfig = crmConfig

c := Cluster{
c := cluster.Cluster{
Cib: *root,
Crmmon: crmmon.Root{
Version: "1.2.3",
Expand Down Expand Up @@ -87,7 +101,7 @@ func (suite *ClusterTestSuite) TestIsDC() {

root.Configuration.CrmConfig = crmConfig

c := &Cluster{
c := &cluster.Cluster{
Cib: *root,
Crmmon: crmmon.Root{
Version: "1.2.3",
Expand All @@ -104,9 +118,9 @@ func (suite *ClusterTestSuite) TestIsDC() {
},
}

suite.Equal(true, isDC(c))
suite.Equal(true, c.IsDC())

c = &Cluster{
c = &cluster.Cluster{
Cib: *root,
Crmmon: crmmon.Root{
Version: "1.2.3",
Expand All @@ -123,7 +137,7 @@ func (suite *ClusterTestSuite) TestIsDC() {
},
}

suite.Equal(false, isDC(c))
suite.Equal(false, c.IsDC())
}

func (suite *ClusterTestSuite) TestIsFencingEnabled() {
Expand All @@ -142,7 +156,7 @@ func (suite *ClusterTestSuite) TestIsFencingEnabled() {

root.Configuration.CrmConfig = crmConfig

c := Cluster{
c := cluster.Cluster{
Cib: *root,
}

Expand All @@ -161,15 +175,15 @@ func (suite *ClusterTestSuite) TestIsFencingEnabled() {

root.Configuration.CrmConfig = crmConfig

c = Cluster{
c = cluster.Cluster{
Cib: *root,
}

suite.Equal(false, c.IsFencingEnabled())
}

func (suite *ClusterTestSuite) TestFencingType() {
c := Cluster{
c := cluster.Cluster{
Crmmon: crmmon.Root{
Version: "1.2.3",
Resources: []crmmon.Resource{
Expand All @@ -182,7 +196,7 @@ func (suite *ClusterTestSuite) TestFencingType() {

suite.Equal("myfencing", c.FencingType())

c = Cluster{
c = cluster.Cluster{
Crmmon: crmmon.Root{
Version: "1.2.3",
Resources: []crmmon.Resource{
Expand All @@ -197,7 +211,7 @@ func (suite *ClusterTestSuite) TestFencingType() {
}

func (suite *ClusterTestSuite) TestFencingResourceExists() {
c := Cluster{
c := cluster.Cluster{
Crmmon: crmmon.Root{
Version: "1.2.3",
Resources: []crmmon.Resource{
Expand All @@ -210,7 +224,7 @@ func (suite *ClusterTestSuite) TestFencingResourceExists() {

suite.Equal(true, c.FencingResourceExists())

c = Cluster{
c = cluster.Cluster{
Crmmon: crmmon.Root{
Version: "1.2.3",
Resources: []crmmon.Resource{
Expand All @@ -225,7 +239,7 @@ func (suite *ClusterTestSuite) TestFencingResourceExists() {
}

func (suite *ClusterTestSuite) TestIsFencingSBD() {
c := Cluster{
c := cluster.Cluster{
Crmmon: crmmon.Root{
Version: "1.2.3",
Resources: []crmmon.Resource{
Expand All @@ -238,7 +252,7 @@ func (suite *ClusterTestSuite) TestIsFencingSBD() {

suite.Equal(true, c.IsFencingSBD())

c = Cluster{
c = cluster.Cluster{
Crmmon: crmmon.Root{
Version: "1.2.3",
Resources: []crmmon.Resource{
Expand Down
16 changes: 6 additions & 10 deletions internal/core/cluster/crmmon/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package crmmon
package crmmon_test

import (
"testing"

"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/core/cluster/crmmon"
"github.com/trento-project/agent/test/helpers"
)

Expand All @@ -15,13 +16,8 @@ func TestParserTestSuite(t *testing.T) {
suite.Run(t, new(ParserTestSuite))
}

func (suite *ParserTestSuite) TestConstructor() {
p := NewCrmMonParser("foo")
suite.Equal("foo", p.crmMonPath)
}

func (suite *ParserTestSuite) TestParse() {
p := NewCrmMonParser(helpers.GetFixturePath("discovery/cluster/fake_crm_mon.sh"))
p := crmmon.NewCrmMonParser(helpers.GetFixturePath("discovery/cluster/fake_crm_mon.sh"))
data, err := p.Parse()
suite.NoError(err)
suite.Equal("2.0.0", data.Version)
Expand Down Expand Up @@ -63,7 +59,7 @@ func (suite *ParserTestSuite) TestParse() {
}

func (suite *ParserTestSuite) TestParseClones() {
p := NewCrmMonParser(helpers.GetFixturePath("discovery/cluster/fake_crm_mon.sh"))
p := crmmon.NewCrmMonParser(helpers.GetFixturePath("discovery/cluster/fake_crm_mon.sh"))
data, err := p.Parse()
suite.NoError(err)
suite.Equal(3, len(data.Clones))
Expand All @@ -79,7 +75,7 @@ func (suite *ParserTestSuite) TestParseClones() {
}

func (suite *ParserTestSuite) TestParseGroups() {
p := NewCrmMonParser(helpers.GetFixturePath("discovery/cluster/fake_crm_mon.sh"))
p := crmmon.NewCrmMonParser(helpers.GetFixturePath("discovery/cluster/fake_crm_mon.sh"))
data, err := p.Parse()
suite.NoError(err)
suite.Equal(2, len(data.Groups))
Expand All @@ -98,7 +94,7 @@ func (suite *ParserTestSuite) TestParseGroups() {
}

func (suite *ParserTestSuite) TestParseNodeAttributes() {
p := NewCrmMonParser(helpers.GetFixturePath("discovery/cluster/fake_crm_mon.sh"))
p := crmmon.NewCrmMonParser(helpers.GetFixturePath("discovery/cluster/fake_crm_mon.sh"))
data, err := p.Parse()
suite.NoError(err)
suite.Len(data.NodeAttributes.Nodes, 2)
Expand Down
4 changes: 1 addition & 3 deletions internal/core/cluster/sbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const (
)

type SBD struct {
cluster string
Devices []*SBDDevice
Config map[string]string
}
Expand Down Expand Up @@ -54,9 +53,8 @@ type SBDNode struct {
Status string
}

func NewSBD(executor utils.CommandExecutor, cluster, sbdPath, sbdConfigPath string) (SBD, error) {
func NewSBD(executor utils.CommandExecutor, sbdPath, sbdConfigPath string) (SBD, error) {
var s = SBD{
cluster: cluster,
Devices: nil, // TODO check me, no slice of pointers needed
Config: map[string]string{},
}
Expand Down
Loading

0 comments on commit a7d0405

Please sign in to comment.