Skip to content

Commit

Permalink
Port the packageversion gatherer to the new engine
Browse files Browse the repository at this point in the history
  • Loading branch information
rtorrero committed Nov 16, 2022
1 parent 7865867 commit 356a20f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
5 changes: 3 additions & 2 deletions internal/factsengine/gatherers/gatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ type FactGatherer interface {

func StandardGatherers() map[string]FactGatherer {
return map[string]FactGatherer{
CorosyncFactKey: NewDefaultCorosyncConfGatherer(),
HostsFileFactKey: NewDefaultHostsFileGatherer(),
CorosyncFactKey: NewDefaultCorosyncConfGatherer(),
HostsFileFactKey: NewDefaultHostsFileGatherer(),
PackageVersionFactKey: NewDefaultPackageVersionGatherer(),
}
}
17 changes: 13 additions & 4 deletions internal/factsengine/gatherers/packageversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import (
)

const (
PackageVersionGathererName = "package_version"
PackageVersionFactKey = "package_version"
)

var (
PackageVersionCMDError = entities.FactGatheringError{
Type: "package-version-cmd-error",
Message: "error getting version of package",
}
)

type PackageVersionGatherer struct {
Expand All @@ -29,14 +36,16 @@ func (g *PackageVersionGatherer) Gather(factsRequests []entities.FactRequest) ([
log.Infof("Starting Package versions facts gathering process")

for _, factReq := range factsRequests {
var fact entities.Fact
version, err := g.executor.Exec(
"rpm", "-q", "--qf", "%{VERSION}", factReq.Argument)
if err != nil {
// TODO: Decide together with Wanda how to deal with errors. `err` field in the fact result?
log.Errorf("Error getting version of package: %s", factReq.Argument)
gatheringError := PackageVersionCMDError.Wrap(factReq.Argument)
fact = entities.NewFactGatheredWithError(factReq, gatheringError)
} else {
fact = entities.NewFactGatheredWithRequest(factReq, entities.ParseStringToFactValue(string(version)))
}

fact := entities.NewFactGatheredWithRequest(factReq, entities.ParseStringToFactValue(string(version)))
facts = append(facts, fact)
}

Expand Down
28 changes: 16 additions & 12 deletions internal/factsengine/gatherers/packageversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import (

"github.com/stretchr/testify/suite"
"github.com/trento-project/agent/internal/factsengine/entities"
mocks "github.com/trento-project/agent/internal/factsengine/gatherers/mocks"
utilsMocks "github.com/trento-project/agent/internal/utils/mocks"
)

type PackageVersionTestSuite struct {
suite.Suite
mockExecutor *mocks.CommandExecutor
mockExecutor *utilsMocks.CommandExecutor
}

func TestPackageVersionTestSuite(t *testing.T) {
suite.Run(t, new(PackageVersionTestSuite))
}

func (suite *PackageVersionTestSuite) SetupTest() {
suite.mockExecutor = new(mocks.CommandExecutor)
suite.mockExecutor = new(utilsMocks.CommandExecutor)
}

func (suite *PackageVersionTestSuite) TestPackageVersionGather() {
Expand Down Expand Up @@ -47,15 +47,15 @@ func (suite *PackageVersionTestSuite) TestPackageVersionGather() {

factResults, err := p.Gather(factRequests)

expectedResults := []entities.FactsGatheredItem{
expectedResults := []entities.Fact{
{
Name: "corosync",
Value: "2.4.5",
Value: &entities.FactValueString{Value: "2.4.5"},
CheckID: "check1",
},
{
Name: "pacemaker",
Value: "2.0.5+20201202.ba59be712",
Value: &entities.FactValueString{Value: "2.0.5+20201202.ba59be712"},
CheckID: "check2",
},
}
Expand All @@ -64,11 +64,11 @@ func (suite *PackageVersionTestSuite) TestPackageVersionGather() {
suite.ElementsMatch(expectedResults, factResults)
}

func (suite *PackageVersionTestSuite) TestPackageVersionGatherError() {
func (suite *PackageVersionTestSuite) TestPackageVersionGatherMissingPackageError() {
suite.mockExecutor.On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "corosync").Return(
[]byte("2.4.5"), nil)
suite.mockExecutor.On("Exec", "rpm", "-q", "--qf", "%{VERSION}", "pacemake").Return(
[]byte("package pacemake is not installed\n"), errors.New("some error"))
[]byte("Error getting version of package: pacemake\n"), errors.New("some error"))

p := NewPackageVersionGatherer(suite.mockExecutor)

Expand All @@ -89,15 +89,19 @@ func (suite *PackageVersionTestSuite) TestPackageVersionGatherError() {

factResults, err := p.Gather(factRequests)

expectedResults := []entities.FactsGatheredItem{
expectedResults := []entities.Fact{
{
Name: "corosync",
Value: "2.4.5",
Value: &entities.FactValueString{Value: "2.4.5"},
CheckID: "check1",
},
{
Name: "pacemaker",
Value: "package pacemake is not installed\n",
Name: "pacemaker",
Value: nil,
Error: &entities.FactGatheringError{
Message: "error getting version of package: pacemake",
Type: "package-version-cmd-error",
},
CheckID: "check2",
},
}
Expand Down

0 comments on commit 356a20f

Please sign in to comment.