diff --git a/internal/cluster/cluster.go b/internal/cluster/cluster.go index 3bf918ed..763f8a4d 100644 --- a/internal/cluster/cluster.go +++ b/internal/cluster/cluster.go @@ -82,6 +82,7 @@ func NewClusterWithDiscoveryTools(discoveryTools *DiscoveryTools) (Cluster, erro Provider: "", } + commandExecutor := utils.Executor{} cibParser := cib.NewCibAdminParser(discoveryTools.CibAdmPath) cibConfig, err := cibParser.Parse() @@ -109,7 +110,7 @@ func NewClusterWithDiscoveryTools(discoveryTools *DiscoveryTools) (Cluster, erro cluster.Name = getName(cluster) if cluster.IsFencingSBD() { - sbdData, err := NewSBD(cluster.ID, discoveryTools.SBDPath, discoveryTools.SBDConfigPath) + sbdData, err := NewSBD(commandExecutor, cluster.ID, discoveryTools.SBDPath, discoveryTools.SBDConfigPath) if err != nil { return cluster, err } @@ -119,7 +120,7 @@ func NewClusterWithDiscoveryTools(discoveryTools *DiscoveryTools) (Cluster, erro cluster.DC = isDC(&cluster) - cloudIdentifier := cloud.NewIdentifier(utils.Executor{}) + cloudIdentifier := cloud.NewIdentifier(commandExecutor) provider, err := cloudIdentifier.IdentifyCloudProvider() if err != nil { log.Warn(errors.Wrap(err, "Cloud provider not identified")) diff --git a/internal/cluster/sbd.go b/internal/cluster/sbd.go index 0d0f1b7d..d248ee15 100644 --- a/internal/cluster/sbd.go +++ b/internal/cluster/sbd.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "os" - "os/exec" "regexp" "strconv" "strings" @@ -30,11 +29,12 @@ type SBD struct { } type SBDDevice struct { - sbdPath string - Device string `mapstructure:"device,omitempty"` - Status string `mapstructure:"status,omitempty"` - Dump SBDDump `mapstructure:"dump,omitempty"` - List []*SBDNode `mapstructure:"list,omitempty"` + executor utils.CommandExecutor + sbdPath string + Device string `mapstructure:"device,omitempty"` + Status string `mapstructure:"status,omitempty"` + Dump SBDDump `mapstructure:"dump,omitempty"` + List []*SBDNode `mapstructure:"list,omitempty"` } type SBDDump struct { @@ -54,10 +54,7 @@ type SBDNode struct { Status string `mapstructure:"status,omitempty"` } -var sbdDumpExecCommand = exec.Command //nolint -var sbdListExecCommand = exec.Command //nolint - -func NewSBD(cluster, sbdPath, sbdConfigPath string) (SBD, error) { +func NewSBD(executor utils.CommandExecutor, cluster, sbdPath, sbdConfigPath string) (SBD, error) { var s = SBD{ cluster: cluster, Devices: nil, // TODO check me, no slice of pointers needed @@ -75,10 +72,10 @@ func NewSBD(cluster, sbdPath, sbdConfigPath string) (SBD, error) { sbdDevice, ok := c["SBD_DEVICE"].(string) if !ok { - return s, fmt.Errorf("could not cast sdb device to string, %v", c["SBD_DEVICE"]) + return s, fmt.Errorf("could not cast sbd device to string, %v", c["SBD_DEVICE"]) } for _, device := range strings.Split(strings.Trim(sbdDevice, "\""), ";") { - sbdDevice := NewSBDDevice(sbdPath, device) + sbdDevice := NewSBDDevice(executor, sbdPath, device) err := sbdDevice.LoadDeviceData() if err != nil { log.Printf("Error getting sbd information: %s", err) @@ -108,18 +105,19 @@ func getSBDConfig(sbdConfigPath string) (map[string]interface{}, error) { return configMap, nil } -func NewSBDDevice(sbdPath string, device string) SBDDevice { +func NewSBDDevice(executor utils.CommandExecutor, sbdPath, device string) SBDDevice { return SBDDevice{ //nolint - sbdPath: sbdPath, - Device: device, - Status: SBDStatusUnknown, + executor: executor, + sbdPath: sbdPath, + Device: device, + Status: SBDStatusUnknown, } } func (s *SBDDevice) LoadDeviceData() error { var sbdErrors []string - dump, err := sbdDump(s.sbdPath, s.Device) + dump, err := sbdDump(s.executor, s.sbdPath, s.Device) s.Dump = dump if err != nil { @@ -129,7 +127,7 @@ func (s *SBDDevice) LoadDeviceData() error { s.Status = SBDStatusHealthy } - list, err := sbdList(s.sbdPath, s.Device) + list, err := sbdList(s.executor, s.sbdPath, s.Device) s.List = list if err != nil { @@ -164,37 +162,62 @@ func assignPatternResult(text string, pattern string) string { // Timeout (loop) : 1 // Timeout (msgwait) : 10 // ==Header on disk /dev/vdc is dumped -func sbdDump(sbdPath string, device string) (SBDDump, error) { - var dump = SBDDump{} //nolint - - sbdDump, err := sbdDumpExecCommand(sbdPath, "-d", device, "dump").Output() - sbdDumpStr := string(sbdDump) - - // FIXME: declarative assignment and error checking on the atoi - dump.Header = assignPatternResult(sbdDumpStr, `Header version *: (.*)`) - dump.UUID = assignPatternResult(sbdDumpStr, `UUID *: (.*)`) - dump.Slots, _ = strconv.Atoi(assignPatternResult(sbdDumpStr, `Number of slots *: (.*)`)) - dump.SectorSize, _ = strconv.Atoi(assignPatternResult(sbdDumpStr, `Sector size *: (.*)`)) - dump.TimeoutWatchdog, _ = strconv.Atoi(assignPatternResult(sbdDumpStr, `Timeout \(watchdog\) *: (.*)`)) - dump.TimeoutAllocate, _ = strconv.Atoi(assignPatternResult(sbdDumpStr, `Timeout \(allocate\) *: (.*)`)) - dump.TimeoutLoop, _ = strconv.Atoi(assignPatternResult(sbdDumpStr, `Timeout \(loop\) *: (.*)`)) - dump.TimeoutMsgwait, _ = strconv.Atoi(assignPatternResult(sbdDumpStr, `Timeout \(msgwait\) *: (.*)`)) +func sbdDump(executor utils.CommandExecutor, sbdPath string, device string) (SBDDump, error) { + sbdDumpOutput, dumpErr := executor.Exec(sbdPath, "-d", device, "dump") + sbdDumpStr := string(sbdDumpOutput) - // Sanity check at the end, even in error case the sbd command can output some information + header := assignPatternResult(sbdDumpStr, `Header version *: (.*)`) + uuid := assignPatternResult(sbdDumpStr, `UUID *: (.*)`) + slots, err := strconv.Atoi(assignPatternResult(sbdDumpStr, `Number of slots *: (.*)`)) + if err != nil { + log.Error("Error parsing Number of slots value as integer") + } + sectorSize, err := strconv.Atoi(assignPatternResult(sbdDumpStr, `Sector size *: (.*)`)) + if err != nil { + log.Error("Error parsing Sector size value as integer") + } + timeoutWatchdog, err := strconv.Atoi(assignPatternResult(sbdDumpStr, `Timeout \(watchdog\) *: (.*)`)) + if err != nil { + log.Error("Error parsing Timeout watchdog value as integer") + } + timeoutAllocate, err := strconv.Atoi(assignPatternResult(sbdDumpStr, `Timeout \(allocate\) *: (.*)`)) if err != nil { - return dump, errors.Wrap(err, "sbd dump command error") + log.Error("Error parsing Tiemout allocate value as integer") + } + timeoutLoop, err := strconv.Atoi(assignPatternResult(sbdDumpStr, `Timeout \(loop\) *: (.*)`)) + if err != nil { + log.Error("Error parsing Timeout loop value as integer") + } + timeoutMsgwait, err := strconv.Atoi(assignPatternResult(sbdDumpStr, `Timeout \(msgwait\) *: (.*)`)) + if err != nil { + log.Error("Error parsing Timeout msgwait value as integer") + } + + sbdDump := SBDDump{ + Header: header, + UUID: uuid, + Slots: slots, + SectorSize: sectorSize, + TimeoutWatchdog: timeoutWatchdog, + TimeoutAllocate: timeoutAllocate, + TimeoutLoop: timeoutLoop, + TimeoutMsgwait: timeoutMsgwait, + } + + if dumpErr != nil { + return sbdDump, errors.Wrap(dumpErr, "sbd dump command error") } - return dump, nil + return sbdDump, nil } // Possible output // 0 hana01 clear // 1 hana02 clear -func sbdList(sbdPath string, device string) ([]*SBDNode, error) { +func sbdList(executor utils.CommandExecutor, sbdPath, device string) ([]*SBDNode, error) { var list = []*SBDNode{} - output, err := sbdListExecCommand(sbdPath, "-d", device, "list").Output() + output, err := executor.Exec(sbdPath, "-d", device, "list") // Loop through sbd list output and find for matches r := regexp.MustCompile(`(\d+)\s+(\S+)\s+(\S+)`) diff --git a/internal/cluster/sbd_test.go b/internal/cluster/sbd_test.go index 3e68f87b..00b9ebf1 100644 --- a/internal/cluster/sbd_test.go +++ b/internal/cluster/sbd_test.go @@ -1,13 +1,20 @@ package cluster import ( + "errors" "fmt" - "os/exec" "testing" "github.com/stretchr/testify/suite" + "github.com/trento-project/agent/internal/utils/mocks" ) +const fixturesFolder = "../../test/fixtures/discovery/cluster/sbd/" + +func getFixtureFile(name string) string { + return fmt.Sprintf("%s/%s", fixturesFolder, name) +} + type SbdTestSuite struct { suite.Suite } @@ -16,8 +23,8 @@ func TestSbdTestSuite(t *testing.T) { suite.Run(t, new(SbdTestSuite)) } -func mockSbdDump(command string, args ...string) *exec.Cmd { - cmd := `==Dumping header on disk /dev/vdc +func mockSbdDump() []byte { + output := `==Dumping header on disk /dev/vdc Header version : 2.1 UUID : 541bdcea-16af-44a4-8ab9-6a98602e65ca Number of slots : 255 @@ -27,40 +34,37 @@ Timeout (allocate) : 2 Timeout (loop) : 1 Timeout (msgwait) : 10 ==Header on disk /dev/vdc is dumped` - return exec.Command("echo", cmd) + return []byte(output) } -func mockSbdDumpErr(command string, args ...string) *exec.Cmd { - cmd := `==Dumping header on disk /dev/vdc +func mockSbdDumpErr() []byte { + output := `==Dumping header on disk /dev/vdc Header version : 2.1 UUID : 541bdcea-16af-44a4-8ab9-6a98602e65ca ==Number of slots on disk /dev/vdb NOT dumped sbd failed; please check the logs.` - script := fmt.Sprintf("echo \"%s\" && exit 1", cmd) - - return exec.Command("bash", "-c", script) + return []byte(output) } -func mockSbdList(command string, args ...string) *exec.Cmd { - cmd := `0 hana01 clear +func mockSbdList() []byte { + output := `0 hana01 clear 1 hana02 clear` - return exec.Command("echo", cmd) + return []byte(output) } -func mockSbdListErr(command string, args ...string) *exec.Cmd { - cmd := `== disk /dev/vdxx unreadable! +func mockSbdListErr() []byte { + output := `== disk /dev/vdxx unreadable! sbd failed; please check the logs.` - script := fmt.Sprintf("echo \"%s\" && exit 1", cmd) - - return exec.Command("bash", "-c", script) + return []byte(output) } func (suite *SbdTestSuite) TestSbdDump() { - sbdDumpExecCommand = mockSbdDump + mockCommand := new(mocks.CommandExecutor) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "dump").Return(mockSbdDump(), nil) - dump, err := sbdDump("/bin/sbd", "/dev/vdc") + dump, err := sbdDump(mockCommand, "/bin/sbd", "/dev/vdc") expectedDump := SBDDump{ Header: "2.1", @@ -78,9 +82,10 @@ func (suite *SbdTestSuite) TestSbdDump() { } func (suite *SbdTestSuite) TestSbdDumpError() { - sbdDumpExecCommand = mockSbdDumpErr + mockCommand := new(mocks.CommandExecutor) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "dump").Return(mockSbdDumpErr(), errors.New("error")) - dump, err := sbdDump("/bin/sbd", "/dev/vdc") + dump, err := sbdDump(mockCommand, "/bin/sbd", "/dev/vdc") expectedDump := SBDDump{ Header: "2.1", @@ -94,13 +99,14 @@ func (suite *SbdTestSuite) TestSbdDumpError() { } suite.Equal(expectedDump, dump) - suite.EqualError(err, "sbd dump command error: exit status 1") + suite.EqualError(err, "sbd dump command error: error") } func (suite *SbdTestSuite) TestSbdList() { - sbdListExecCommand = mockSbdList + mockCommand := new(mocks.CommandExecutor) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "list").Return(mockSbdList(), nil) - list, err := sbdList("/bin/sbd", "/dev/vdc") + list, err := sbdList(mockCommand, "/bin/sbd", "/dev/vdc") expectedList := []*SBDNode{ { @@ -120,46 +126,52 @@ func (suite *SbdTestSuite) TestSbdList() { } func (suite *SbdTestSuite) TestSbdListError() { - sbdListExecCommand = mockSbdListErr + mockCommand := new(mocks.CommandExecutor) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "list").Return(mockSbdListErr(), errors.New("error")) - list, err := sbdList("/bin/sbd", "/dev/vdc") + list, err := sbdList(mockCommand, "/bin/sbd", "/dev/vdc") expectedList := []*SBDNode{} suite.Equal(expectedList, list) - suite.EqualError(err, "sbd list command error: exit status 1") + suite.EqualError(err, "sbd list command error: error") } func (suite *SbdTestSuite) TestLoadDeviceData() { - s := NewSBDDevice("/bin/sbd", "/dev/vdc") + mockCommand := new(mocks.CommandExecutor) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "dump").Return(mockSbdDump(), nil) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "list").Return(mockSbdList(), nil) - sbdDumpExecCommand = mockSbdDump - sbdListExecCommand = mockSbdList + s := NewSBDDevice(mockCommand, "/bin/sbd", "/dev/vdc") err := s.LoadDeviceData() - expectedDevice := NewSBDDevice("/bin/sbd", "/dev/vdc") - expectedDevice.Status = "healthy" - expectedDevice.Dump = SBDDump{ - Header: "2.1", - UUID: "541bdcea-16af-44a4-8ab9-6a98602e65ca", - Slots: 255, - SectorSize: 512, - TimeoutWatchdog: 5, - TimeoutAllocate: 2, - TimeoutLoop: 1, - TimeoutMsgwait: 10, - } - expectedDevice.List = []*SBDNode{ - { - ID: 0, - Name: "hana01", - Status: "clear", + expectedDevice := SBDDevice{ + executor: mockCommand, + sbdPath: "/bin/sbd", + Device: "/dev/vdc", + Status: "healthy", + Dump: SBDDump{ + Header: "2.1", + UUID: "541bdcea-16af-44a4-8ab9-6a98602e65ca", + Slots: 255, + SectorSize: 512, + TimeoutWatchdog: 5, + TimeoutAllocate: 2, + TimeoutLoop: 1, + TimeoutMsgwait: 10, }, - { - ID: 1, - Name: "hana02", - Status: "clear", + List: []*SBDNode{ + { + ID: 0, + Name: "hana01", + Status: "clear", + }, + { + ID: 1, + Name: "hana02", + Status: "clear", + }, }, } @@ -168,100 +180,111 @@ func (suite *SbdTestSuite) TestLoadDeviceData() { } func (suite *SbdTestSuite) TestLoadDeviceDataDumpError() { - s := NewSBDDevice("/bin/sbdErr", "/dev/vdc") + mockCommand := new(mocks.CommandExecutor) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "dump").Return(mockSbdDumpErr(), errors.New("error")) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "list").Return(mockSbdList(), nil) - sbdDumpExecCommand = mockSbdDumpErr + s := NewSBDDevice(mockCommand, "/bin/sbd", "/dev/vdc") err := s.LoadDeviceData() - expectedDevice := NewSBDDevice("/bin/sbdErr", "/dev/vdc") - expectedDevice.Status = "unhealthy" - - expectedDevice.Dump = SBDDump{ - Header: "2.1", - UUID: "541bdcea-16af-44a4-8ab9-6a98602e65ca", - Slots: 0, - SectorSize: 0, - TimeoutWatchdog: 0, - TimeoutAllocate: 0, - TimeoutLoop: 0, - TimeoutMsgwait: 0, - } - - expectedDevice.List = []*SBDNode{ - { - ID: 0, - Name: "hana01", - Status: "clear", + expectedDevice := SBDDevice{ + executor: mockCommand, + sbdPath: "/bin/sbd", + Device: "/dev/vdc", + Status: "unhealthy", + Dump: SBDDump{ + Header: "2.1", + UUID: "541bdcea-16af-44a4-8ab9-6a98602e65ca", + Slots: 0, + SectorSize: 0, + TimeoutWatchdog: 0, + TimeoutAllocate: 0, + TimeoutLoop: 0, + TimeoutMsgwait: 0, }, - { - ID: 1, - Name: "hana02", - Status: "clear", + List: []*SBDNode{ + { + ID: 0, + Name: "hana01", + Status: "clear", + }, + { + ID: 1, + Name: "hana02", + Status: "clear", + }, }, } suite.Equal(expectedDevice, s) - suite.EqualError(err, "sbd dump command error: exit status 1") + suite.EqualError(err, "sbd dump command error: error") } func (suite *SbdTestSuite) TestLoadDeviceDataListError() { - s := NewSBDDevice("/bin/sbdErr", "/dev/vdc") + mockCommand := new(mocks.CommandExecutor) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "dump").Return(mockSbdDump(), nil) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "list").Return(mockSbdListErr(), errors.New("error")) - sbdDumpExecCommand = mockSbdDump - sbdListExecCommand = mockSbdListErr + s := NewSBDDevice(mockCommand, "/bin/sbd", "/dev/vdc") err := s.LoadDeviceData() - expectedDevice := NewSBDDevice("/bin/sbdErr", "/dev/vdc") - expectedDevice.Status = "healthy" - expectedDevice.Dump = SBDDump{ - Header: "2.1", - UUID: "541bdcea-16af-44a4-8ab9-6a98602e65ca", - Slots: 255, - SectorSize: 512, - TimeoutWatchdog: 5, - TimeoutAllocate: 2, - TimeoutLoop: 1, - TimeoutMsgwait: 10, + expectedDevice := SBDDevice{ + executor: mockCommand, + sbdPath: "/bin/sbd", + Device: "/dev/vdc", + Status: "healthy", + Dump: SBDDump{ + Header: "2.1", + UUID: "541bdcea-16af-44a4-8ab9-6a98602e65ca", + Slots: 255, + SectorSize: 512, + TimeoutWatchdog: 5, + TimeoutAllocate: 2, + TimeoutLoop: 1, + TimeoutMsgwait: 10, + }, + List: []*SBDNode{}, } - expectedDevice.List = []*SBDNode{} - suite.Equal(expectedDevice, s) - suite.EqualError(err, "sbd list command error: exit status 1") + suite.EqualError(err, "sbd list command error: error") } func (suite *SbdTestSuite) TestLoadDeviceDataError() { - s := NewSBDDevice("/bin/sbdErr", "/dev/vdc") + mockCommand := new(mocks.CommandExecutor) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "dump").Return(mockSbdDumpErr(), errors.New("error")) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "list").Return(mockSbdListErr(), errors.New("error")) - sbdDumpExecCommand = mockSbdDumpErr - sbdListExecCommand = mockSbdListErr + s := NewSBDDevice(mockCommand, "/bin/sbd", "/dev/vdc") err := s.LoadDeviceData() - expectedDevice := NewSBDDevice("/bin/sbdErr", "/dev/vdc") - expectedDevice.Status = "unhealthy" - - expectedDevice.Dump = SBDDump{ - Header: "2.1", - UUID: "541bdcea-16af-44a4-8ab9-6a98602e65ca", - Slots: 0, - SectorSize: 0, - TimeoutWatchdog: 0, - TimeoutAllocate: 0, - TimeoutLoop: 0, - TimeoutMsgwait: 0, + expectedDevice := SBDDevice{ + executor: mockCommand, + sbdPath: "/bin/sbd", + Device: "/dev/vdc", + Status: "unhealthy", + Dump: SBDDump{ + Header: "2.1", + UUID: "541bdcea-16af-44a4-8ab9-6a98602e65ca", + Slots: 0, + SectorSize: 0, + TimeoutWatchdog: 0, + TimeoutAllocate: 0, + TimeoutLoop: 0, + TimeoutMsgwait: 0, + }, + List: []*SBDNode{}, } - expectedDevice.List = []*SBDNode{} - suite.Equal(expectedDevice, s) - suite.EqualError(err, "sbd dump command error: exit status 1;sbd list command error: exit status 1") + suite.EqualError(err, "sbd dump command error: error;sbd list command error: error") } func (suite *SbdTestSuite) TestGetSBDConfig() { - sbdConfig, err := getSBDConfig("../../test/sbd_config") + sbdConfig, err := getSBDConfig(getFixtureFile("sbd_config")) expectedConfig := map[string]interface{}{ "SBD_PACEMAKER": "yes", @@ -290,10 +313,13 @@ func (suite *SbdTestSuite) TestGetSBDConfigError() { } func (suite *SbdTestSuite) TestNewSBD() { - sbdDumpExecCommand = mockSbdDump - sbdListExecCommand = mockSbdList + mockCommand := new(mocks.CommandExecutor) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "dump").Return(mockSbdDump(), nil) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "list").Return(mockSbdList(), nil) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdb", "dump").Return(mockSbdDump(), nil) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdb", "list").Return(mockSbdList(), nil) - s, err := NewSBD("mycluster", "/bin/sbd", "../../test/sbd_config") + s, err := NewSBD(mockCommand, "mycluster", "/bin/sbd", getFixtureFile("sbd_config")) expectedSbd := SBD{ cluster: "mycluster", @@ -311,9 +337,10 @@ func (suite *SbdTestSuite) TestNewSBD() { }, Devices: []*SBDDevice{ { - sbdPath: "/bin/sbd", - Device: "/dev/vdc", - Status: "healthy", + executor: mockCommand, + sbdPath: "/bin/sbd", + Device: "/dev/vdc", + Status: "healthy", Dump: SBDDump{ Header: "2.1", UUID: "541bdcea-16af-44a4-8ab9-6a98602e65ca", @@ -338,9 +365,10 @@ func (suite *SbdTestSuite) TestNewSBD() { }, }, { - sbdPath: "/bin/sbd", - Device: "/dev/vdb", - Status: "healthy", + executor: mockCommand, + sbdPath: "/bin/sbd", + Device: "/dev/vdb", + Status: "healthy", Dump: SBDDump{ Header: "2.1", UUID: "541bdcea-16af-44a4-8ab9-6a98602e65ca", @@ -372,7 +400,9 @@ func (suite *SbdTestSuite) TestNewSBD() { } func (suite *SbdTestSuite) TestNewSBDError() { - s, err := NewSBD("mycluster", "/bin/sbd", "../../test/sbd_config_no_device") + mockCommand := new(mocks.CommandExecutor) + s, err := NewSBD( + mockCommand, "mycluster", "/bin/sbd", getFixtureFile("sbd_config_no_device")) expectedSbd := SBD{ //nolint cluster: "mycluster", @@ -392,10 +422,13 @@ func (suite *SbdTestSuite) TestNewSBDError() { } func (suite *SbdTestSuite) TestNewSBDUnhealthyDevices() { - sbdDumpExecCommand = mockSbdDumpErr - sbdListExecCommand = mockSbdListErr + mockCommand := new(mocks.CommandExecutor) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "dump").Return(mockSbdDumpErr(), errors.New("error")) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "list").Return(mockSbdListErr(), errors.New("error")) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdb", "dump").Return(mockSbdDumpErr(), errors.New("error")) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdb", "list").Return(mockSbdListErr(), errors.New("error")) - s, err := NewSBD("mycluster", "/bin/sbd", "../../test/sbd_config") + s, err := NewSBD(mockCommand, "mycluster", "/bin/sbd", getFixtureFile("sbd_config")) expectedSbd := SBD{ cluster: "mycluster", @@ -413,9 +446,10 @@ func (suite *SbdTestSuite) TestNewSBDUnhealthyDevices() { }, Devices: []*SBDDevice{ { - sbdPath: "/bin/sbd", - Device: "/dev/vdc", - Status: "unhealthy", + executor: mockCommand, + sbdPath: "/bin/sbd", + Device: "/dev/vdc", + Status: "unhealthy", Dump: SBDDump{ Header: "2.1", UUID: "541bdcea-16af-44a4-8ab9-6a98602e65ca", @@ -429,9 +463,10 @@ func (suite *SbdTestSuite) TestNewSBDUnhealthyDevices() { List: []*SBDNode{}, }, { - sbdPath: "/bin/sbd", - Device: "/dev/vdb", - Status: "unhealthy", + executor: mockCommand, + sbdPath: "/bin/sbd", + Device: "/dev/vdb", + Status: "unhealthy", Dump: SBDDump{ Header: "2.1", UUID: "541bdcea-16af-44a4-8ab9-6a98602e65ca", @@ -452,10 +487,14 @@ func (suite *SbdTestSuite) TestNewSBDUnhealthyDevices() { } func (suite *SbdTestSuite) TestNewSBDQuotedDevices() { - sbdDumpExecCommand = mockSbdDump - sbdListExecCommand = mockSbdList - - s, err := NewSBD("mycluster", "/bin/sbd", "../../test/sbd_config_quoted_devices") + mockCommand := new(mocks.CommandExecutor) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "dump").Return(mockSbdDump(), nil) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdc", "list").Return(mockSbdList(), nil) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdb", "dump").Return(mockSbdDump(), nil) + mockCommand.On("Exec", "/bin/sbd", "-d", "/dev/vdb", "list").Return(mockSbdList(), nil) + + s, err := NewSBD( + mockCommand, "mycluster", "/bin/sbd", getFixtureFile("sbd_config_quoted_devices")) suite.Equal(len(s.Devices), 2) suite.Equal("/dev/vdc", s.Devices[0].Device) diff --git a/internal/discovery/mocks/discovered_cluster_mock.go b/internal/discovery/mocks/discovered_cluster_mock.go index b2b68028..c1bc333a 100644 --- a/internal/discovery/mocks/discovered_cluster_mock.go +++ b/internal/discovery/mocks/discovered_cluster_mock.go @@ -11,7 +11,7 @@ func NewDiscoveredClusterMock() cluster.Cluster { CrmmonAdmPath: "./test/fake_crm_mon.sh", CorosyncKeyPath: "./test/authkey", SBDPath: "./test/fake_sbd.sh", - SBDConfigPath: "./test/sbd_config", + SBDConfigPath: "./test/fixtures/discovery/cluster/sbd/sbd_config", }) cluster.Provider = cloud.Azure diff --git a/internal/factsengine/gatherers/_todo/sbd_test.go b/internal/factsengine/gatherers/_todo/sbd_test.go index dd6ffee1..ef1ad9ab 100644 --- a/internal/factsengine/gatherers/_todo/sbd_test.go +++ b/internal/factsengine/gatherers/_todo/sbd_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "fmt" "testing" "github.com/stretchr/testify/suite" @@ -8,6 +9,12 @@ import ( "github.com/trento-project/agent/internal/factsengine/gatherers" ) +const fixturesFolder = "../../../test/fixtures/discovery/cluster/sbd/" + +func getFixtureFile(name string) string { + return fmt.Sprintf("%s/%s", fixturesFolder, name) +} + type SBDGathererTestSuite struct { suite.Suite configurationFile string @@ -15,16 +22,14 @@ type SBDGathererTestSuite struct { func TestSBDGathererTestSuite(t *testing.T) { sbdSuite := new(SBDGathererTestSuite) - sbdSuite.configurationFile = "../../../test/sbd_config" + sbdSuite.configurationFile = getFixtureFile("sbd_config") suite.Run(t, sbdSuite) } func (suite *SBDGathererTestSuite) TestConfigFileCouldNotBeRead() { - const testSBDConfig = "../../../test/some-non-existent-sbd-config" - requestedFacts := []entities.FactRequest{} - gatherer := gatherers.NewSBDGatherer(testSBDConfig) + gatherer := gatherers.NewSBDGatherer(getFixtureFile("some-non-existent-sbd-config")) gatheredFacts, err := gatherer.Gather(requestedFacts) diff --git a/test/sbd_config b/test/fixtures/discovery/cluster/sbd/sbd_config similarity index 100% rename from test/sbd_config rename to test/fixtures/discovery/cluster/sbd/sbd_config diff --git a/test/sbd_config_no_device b/test/fixtures/discovery/cluster/sbd/sbd_config_no_device similarity index 100% rename from test/sbd_config_no_device rename to test/fixtures/discovery/cluster/sbd/sbd_config_no_device diff --git a/test/sbd_config_quoted_devices b/test/fixtures/discovery/cluster/sbd/sbd_config_quoted_devices similarity index 100% rename from test/sbd_config_quoted_devices rename to test/fixtures/discovery/cluster/sbd/sbd_config_quoted_devices