From 7b7f19d355c223f66fc50d0c6a04c0b4ddf539d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Torrero=20Marijnissen?= Date: Thu, 18 Aug 2022 10:01:12 +0100 Subject: [PATCH] Use DI for the CommandExecutor --- internal/factsengine/factsengine.go | 9 ++++---- internal/factsengine/gatherers/cibadmin.go | 4 ++-- .../factsengine/gatherers/cibadmin_test.go | 22 ++++++++---------- .../factsengine/gatherers/corosynccmapctl.go | 4 ++-- .../gatherers/corosynccmapctl_test.go | 23 +++++++++---------- internal/factsengine/gatherers/crmmon.go | 4 ++-- internal/factsengine/gatherers/crmmon_test.go | 22 ++++++++---------- .../factsengine/gatherers/packageversion.go | 4 ++-- .../gatherers/packageversion_test.go | 21 +++++++++-------- 9 files changed, 53 insertions(+), 60 deletions(-) diff --git a/internal/factsengine/factsengine.go b/internal/factsengine/factsengine.go index f5816ccc..18405690 100644 --- a/internal/factsengine/factsengine.go +++ b/internal/factsengine/factsengine.go @@ -21,16 +21,17 @@ type FactsEngine struct { } func NewFactsEngine(agentID, factsEngineService string) *FactsEngine { + commandExecutor := gatherers.Executor{} return &FactsEngine{ agentID: agentID, factsEngineService: factsEngineService, factsServiceAdapter: nil, factGatherers: map[string]gatherers.FactGatherer{ gatherers.CorosyncFactKey: gatherers.NewCorosyncConfGatherer(), - gatherers.CorosyncCmapCtlFactKey: gatherers.NewCorosyncCmapctlGatherer(), - gatherers.PackageVersionGathererName: gatherers.NewPackageVersionGatherer(), - gatherers.CrmMonGathererName: gatherers.NewCrmMonGatherer(), - gatherers.CibAdminGathererName: gatherers.NewCibAdminGatherer(), + gatherers.CorosyncCmapCtlFactKey: gatherers.NewCorosyncCmapctlGatherer(commandExecutor), + gatherers.PackageVersionGathererName: gatherers.NewPackageVersionGatherer(commandExecutor), + gatherers.CrmMonGathererName: gatherers.NewCrmMonGatherer(commandExecutor), + gatherers.CibAdminGathererName: gatherers.NewCibAdminGatherer(commandExecutor), gatherers.SystemDGathererName: gatherers.NewSystemDGatherer(), }, pluginLoaders: NewPluginLoaders(), diff --git a/internal/factsengine/gatherers/cibadmin.go b/internal/factsengine/gatherers/cibadmin.go index 0cdcfe36..5d6d156c 100644 --- a/internal/factsengine/gatherers/cibadmin.go +++ b/internal/factsengine/gatherers/cibadmin.go @@ -12,9 +12,9 @@ type CibAdminGatherer struct { executor CommandExecutor } -func NewCibAdminGatherer() *CibAdminGatherer { +func NewCibAdminGatherer(executor Executor) *CibAdminGatherer { return &CibAdminGatherer{ - executor: Executor{}, + executor: executor, } } diff --git a/internal/factsengine/gatherers/cibadmin_test.go b/internal/factsengine/gatherers/cibadmin_test.go index 42599a90..3e4d5a23 100644 --- a/internal/factsengine/gatherers/cibadmin_test.go +++ b/internal/factsengine/gatherers/cibadmin_test.go @@ -12,6 +12,7 @@ import ( type CibAdminTestSuite struct { suite.Suite + executor *mocks.CommandExecutor cibAdminOutput []byte } @@ -19,7 +20,8 @@ func TestCibAdminTestSuite(t *testing.T) { suite.Run(t, new(CibAdminTestSuite)) } -func (suite *CibAdminTestSuite) SetupSuite() { +func (suite *CibAdminTestSuite) SetupTest() { + suite.executor = new(mocks.CommandExecutor) lFile, _ := os.Open("../../../test/fixtures/gatherers/cibadmin.xml") content, _ := ioutil.ReadAll(lFile) @@ -27,13 +29,11 @@ func (suite *CibAdminTestSuite) SetupSuite() { } func (suite *CibAdminTestSuite) TestCibAdminGather() { - mockExecutor := new(mocks.CommandExecutor) - - mockExecutor.On("Exec", "cibadmin", "--query", "--local").Return( + suite.executor.On("Exec", "cibadmin", "--query", "--local").Return( suite.cibAdminOutput, nil) p := &CibAdminGatherer{ - executor: mockExecutor, + executor: suite.executor, } factRequests := []FactRequest{ @@ -71,13 +71,11 @@ func (suite *CibAdminTestSuite) TestCibAdminGather() { } func (suite *CibAdminTestSuite) TestCibAdminGatherCmdNotFound() { - mockExecutor := new(mocks.CommandExecutor) - - mockExecutor.On("Exec", "cibadmin", "--query", "--local").Return( + suite.executor.On("Exec", "cibadmin", "--query", "--local").Return( suite.cibAdminOutput, errors.New("cibadmin not found")) p := &CibAdminGatherer{ - executor: mockExecutor, + executor: suite.executor, } factRequests := []FactRequest{ @@ -101,13 +99,11 @@ func (suite *CibAdminTestSuite) TestCibAdminGatherCmdNotFound() { } func (suite *CibAdminTestSuite) TestCibAdminGatherError() { - mockExecutor := new(mocks.CommandExecutor) - - mockExecutor.On("Exec", "cibadmin", "--query", "--local").Return( + suite.executor.On("Exec", "cibadmin", "--query", "--local").Return( suite.cibAdminOutput, nil) p := &CibAdminGatherer{ - executor: mockExecutor, + executor: suite.executor, } factRequests := []FactRequest{ diff --git a/internal/factsengine/gatherers/corosynccmapctl.go b/internal/factsengine/gatherers/corosynccmapctl.go index b5ff0a9f..145e27d9 100644 --- a/internal/factsengine/gatherers/corosynccmapctl.go +++ b/internal/factsengine/gatherers/corosynccmapctl.go @@ -15,9 +15,9 @@ type CorosyncCmapctlGatherer struct { executor CommandExecutor } -func NewCorosyncCmapctlGatherer() *CorosyncCmapctlGatherer { +func NewCorosyncCmapctlGatherer(executor Executor) *CorosyncCmapctlGatherer { return &CorosyncCmapctlGatherer{ - executor: Executor{}, + executor: executor, } } diff --git a/internal/factsengine/gatherers/corosynccmapctl_test.go b/internal/factsengine/gatherers/corosynccmapctl_test.go index 603e5b99..3bc59451 100644 --- a/internal/factsengine/gatherers/corosynccmapctl_test.go +++ b/internal/factsengine/gatherers/corosynccmapctl_test.go @@ -12,21 +12,24 @@ import ( type CorosyncCmapctlTestSuite struct { suite.Suite + executor *mocks.CommandExecutor } func TestCorosyncCmapctlTestSuite(t *testing.T) { suite.Run(t, new(CorosyncCmapctlTestSuite)) } -func (suite *CorosyncCmapctlTestSuite) TestCorosyncCmapctlGathererMissingFact() { - mockExecutor := new(mocks.CommandExecutor) +func (suite *CorosyncCmapctlTestSuite) SetupTest() { + suite.executor = new(mocks.CommandExecutor) +} +func (suite *CorosyncCmapctlTestSuite) TestCorosyncCmapctlGathererMissingFact() { mockOutputFile, _ := os.Open("../../../test/fixtures/gatherers/corosynccmap-ctl.output") mockOutput, _ := ioutil.ReadAll(mockOutputFile) - mockExecutor.On("Exec", "corosync-cmapctl", "-b").Return(mockOutput, nil) + suite.executor.On("Exec", "corosync-cmapctl", "-b").Return(mockOutput, nil) c := &CorosyncCmapctlGatherer{ - executor: mockExecutor, + executor: suite.executor, } factRequests := []FactRequest{ @@ -46,14 +49,12 @@ func (suite *CorosyncCmapctlTestSuite) TestCorosyncCmapctlGathererMissingFact() } func (suite *CorosyncCmapctlTestSuite) TestCorosyncCmapctlGatherer() { - mockExecutor := new(mocks.CommandExecutor) - mockOutputFile, _ := os.Open("../../../test/fixtures/gatherers/corosynccmap-ctl.output") mockOutput, _ := ioutil.ReadAll(mockOutputFile) - mockExecutor.On("Exec", "corosync-cmapctl", "-b").Return(mockOutput, nil) + suite.executor.On("Exec", "corosync-cmapctl", "-b").Return(mockOutput, nil) c := &CorosyncCmapctlGatherer{ - executor: mockExecutor, + executor: suite.executor, } factRequests := []FactRequest{ @@ -114,12 +115,10 @@ func (suite *CorosyncCmapctlTestSuite) TestCorosyncCmapctlGatherer() { } func (suite *CorosyncCmapctlTestSuite) TestCorosyncCmapctlCommandNotFound() { - mockExecutor := new(mocks.CommandExecutor) - - mockExecutor.On("Exec", "corosync-cmapctl", "-b").Return(nil, exec.ErrNotFound) + suite.executor.On("Exec", "corosync-cmapctl", "-b").Return(nil, exec.ErrNotFound) c := &CorosyncCmapctlGatherer{ - executor: mockExecutor, + executor: suite.executor, } factRequests := []FactRequest{ diff --git a/internal/factsengine/gatherers/crmmon.go b/internal/factsengine/gatherers/crmmon.go index fceb1afb..93f33fec 100644 --- a/internal/factsengine/gatherers/crmmon.go +++ b/internal/factsengine/gatherers/crmmon.go @@ -12,9 +12,9 @@ type CrmMonGatherer struct { executor CommandExecutor } -func NewCrmMonGatherer() *CrmMonGatherer { +func NewCrmMonGatherer(executor Executor) *CrmMonGatherer { return &CrmMonGatherer{ - executor: Executor{}, + executor: executor, } } diff --git a/internal/factsengine/gatherers/crmmon_test.go b/internal/factsengine/gatherers/crmmon_test.go index 4588a1be..8ec8600a 100644 --- a/internal/factsengine/gatherers/crmmon_test.go +++ b/internal/factsengine/gatherers/crmmon_test.go @@ -12,6 +12,7 @@ import ( type CrmMonTestSuite struct { suite.Suite + executor *mocks.CommandExecutor crmMonOutput []byte } @@ -19,7 +20,8 @@ func TestCrmMonTestSuite(t *testing.T) { suite.Run(t, new(CrmMonTestSuite)) } -func (suite *CrmMonTestSuite) SetupSuite() { +func (suite *CrmMonTestSuite) SetupTest() { + suite.executor = new(mocks.CommandExecutor) lFile, _ := os.Open("../../../test/fixtures/gatherers/crmmon.xml") content, _ := ioutil.ReadAll(lFile) @@ -27,13 +29,11 @@ func (suite *CrmMonTestSuite) SetupSuite() { } func (suite *CrmMonTestSuite) TestCrmMonGather() { - mockExecutor := new(mocks.CommandExecutor) - - mockExecutor.On("Exec", "crm_mon", "--output-as", "xml").Return( + suite.executor.On("Exec", "crm_mon", "--output-as", "xml").Return( suite.crmMonOutput, nil) p := &CrmMonGatherer{ - executor: mockExecutor, + executor: suite.executor, } factRequests := []FactRequest{ @@ -71,13 +71,10 @@ func (suite *CrmMonTestSuite) TestCrmMonGather() { } func (suite *CrmMonTestSuite) TestCrmMonGatherCmdNotFound() { - mockExecutor := new(mocks.CommandExecutor) - - mockExecutor.On("Exec", "crm_mon", "--output-as", "xml").Return( + suite.executor.On("Exec", "crm_mon", "--output-as", "xml").Return( suite.crmMonOutput, errors.New("crm_mon not found")) - p := &CrmMonGatherer{ - executor: mockExecutor, + executor: suite.executor, } factRequests := []FactRequest{ @@ -101,13 +98,12 @@ func (suite *CrmMonTestSuite) TestCrmMonGatherCmdNotFound() { } func (suite *CrmMonTestSuite) TestCrmMonGatherError() { - mockExecutor := new(mocks.CommandExecutor) - mockExecutor.On("Exec", "crm_mon", "--output-as", "xml").Return( + suite.executor.On("Exec", "crm_mon", "--output-as", "xml").Return( suite.crmMonOutput, nil) p := &CrmMonGatherer{ - executor: mockExecutor, + executor: suite.executor, } factRequests := []FactRequest{ diff --git a/internal/factsengine/gatherers/packageversion.go b/internal/factsengine/gatherers/packageversion.go index 23fba2d0..a430537e 100644 --- a/internal/factsengine/gatherers/packageversion.go +++ b/internal/factsengine/gatherers/packageversion.go @@ -12,9 +12,9 @@ type PackageVersionGatherer struct { executor CommandExecutor } -func NewPackageVersionGatherer() *PackageVersionGatherer { +func NewPackageVersionGatherer(executor Executor) *PackageVersionGatherer { return &PackageVersionGatherer{ - executor: Executor{}, + executor: executor, } } diff --git a/internal/factsengine/gatherers/packageversion_test.go b/internal/factsengine/gatherers/packageversion_test.go index eeec82d6..25c515be 100644 --- a/internal/factsengine/gatherers/packageversion_test.go +++ b/internal/factsengine/gatherers/packageversion_test.go @@ -10,22 +10,25 @@ import ( type PackageVersionTestSuite struct { suite.Suite + executor *mocks.CommandExecutor } func TestPackageVersionTestSuite(t *testing.T) { suite.Run(t, new(PackageVersionTestSuite)) } -func (suite *PackageVersionTestSuite) TestPackageVersionGather() { - mockExecutor := new(mocks.CommandExecutor) +func (suite *PackageVersionTestSuite) SetupTest() { + suite.executor = new(mocks.CommandExecutor) +} - mockExecutor.On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "corosync").Return( +func (suite *PackageVersionTestSuite) TestPackageVersionGather() { + suite.executor.On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "corosync").Return( []byte("2.4.5"), nil) - mockExecutor.On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "pacemaker").Return( + suite.executor.On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "pacemaker").Return( []byte("2.0.5+20201202.ba59be712"), nil) p := &PackageVersionGatherer{ - executor: mockExecutor, + executor: suite.executor, } factRequests := []FactRequest{ @@ -63,15 +66,13 @@ func (suite *PackageVersionTestSuite) TestPackageVersionGather() { } func (suite *PackageVersionTestSuite) TestPackageVersionGatherError() { - mockExecutor := new(mocks.CommandExecutor) - - mockExecutor.On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "corosync").Return( + suite.executor.On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "corosync").Return( []byte("2.4.5"), nil) - mockExecutor.On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "pacemake").Return( + suite.executor.On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "pacemake").Return( []byte("package pacemake is not installed\n"), errors.New("some error")) p := &PackageVersionGatherer{ - executor: mockExecutor, + executor: suite.executor, } factRequests := []FactRequest{