Skip to content

Commit

Permalink
Use DI for the CommandExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
rtorrero committed Aug 18, 2022
1 parent a85ba07 commit 7b7f19d
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 60 deletions.
9 changes: 5 additions & 4 deletions internal/factsengine/factsengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions internal/factsengine/gatherers/cibadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type CibAdminGatherer struct {
executor CommandExecutor
}

func NewCibAdminGatherer() *CibAdminGatherer {
func NewCibAdminGatherer(executor Executor) *CibAdminGatherer {
return &CibAdminGatherer{
executor: Executor{},
executor: executor,
}
}

Expand Down
22 changes: 9 additions & 13 deletions internal/factsengine/gatherers/cibadmin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ import (

type CibAdminTestSuite struct {
suite.Suite
executor *mocks.CommandExecutor
cibAdminOutput []byte
}

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)

suite.cibAdminOutput = content
}

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{
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions internal/factsengine/gatherers/corosynccmapctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ type CorosyncCmapctlGatherer struct {
executor CommandExecutor
}

func NewCorosyncCmapctlGatherer() *CorosyncCmapctlGatherer {
func NewCorosyncCmapctlGatherer(executor Executor) *CorosyncCmapctlGatherer {
return &CorosyncCmapctlGatherer{
executor: Executor{},
executor: executor,
}
}

Expand Down
23 changes: 11 additions & 12 deletions internal/factsengine/gatherers/corosynccmapctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions internal/factsengine/gatherers/crmmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type CrmMonGatherer struct {
executor CommandExecutor
}

func NewCrmMonGatherer() *CrmMonGatherer {
func NewCrmMonGatherer(executor Executor) *CrmMonGatherer {
return &CrmMonGatherer{
executor: Executor{},
executor: executor,
}
}

Expand Down
22 changes: 9 additions & 13 deletions internal/factsengine/gatherers/crmmon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ import (

type CrmMonTestSuite struct {
suite.Suite
executor *mocks.CommandExecutor
crmMonOutput []byte
}

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)

suite.crmMonOutput = content
}

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{
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions internal/factsengine/gatherers/packageversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type PackageVersionGatherer struct {
executor CommandExecutor
}

func NewPackageVersionGatherer() *PackageVersionGatherer {
func NewPackageVersionGatherer(executor Executor) *PackageVersionGatherer {
return &PackageVersionGatherer{
executor: Executor{},
executor: executor,
}
}

Expand Down
21 changes: 11 additions & 10 deletions internal/factsengine/gatherers/packageversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 7b7f19d

Please sign in to comment.