diff --git a/internal/factsengine/gatherers/gatherer.go b/internal/factsengine/gatherers/gatherer.go index 54fb588e..68e45ef3 100644 --- a/internal/factsengine/gatherers/gatherer.go +++ b/internal/factsengine/gatherers/gatherer.go @@ -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(), } } diff --git a/internal/factsengine/gatherers/packageversion.go b/internal/factsengine/gatherers/packageversion.go index 24dd5b45..cc010a48 100644 --- a/internal/factsengine/gatherers/packageversion.go +++ b/internal/factsengine/gatherers/packageversion.go @@ -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 { @@ -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) } diff --git a/internal/factsengine/gatherers/packageversion_test.go b/internal/factsengine/gatherers/packageversion_test.go index 18f36165..454a9280 100644 --- a/internal/factsengine/gatherers/packageversion_test.go +++ b/internal/factsengine/gatherers/packageversion_test.go @@ -6,12 +6,12 @@ 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) { @@ -19,7 +19,7 @@ func TestPackageVersionTestSuite(t *testing.T) { } func (suite *PackageVersionTestSuite) SetupTest() { - suite.mockExecutor = new(mocks.CommandExecutor) + suite.mockExecutor = new(utilsMocks.CommandExecutor) } func (suite *PackageVersionTestSuite) TestPackageVersionGather() { @@ -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", }, } @@ -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) @@ -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", }, }