diff --git a/internal/factsengine/gatherers/saplocalhostresolver.go b/internal/factsengine/gatherers/saplocalhostresolver.go index e972689e..228154af 100644 --- a/internal/factsengine/gatherers/saplocalhostresolver.go +++ b/internal/factsengine/gatherers/saplocalhostresolver.go @@ -44,6 +44,10 @@ var ( Type: "saplocalhost_resolver-decoding-error", Message: "error decoding output to FactValue", } + SapLocalhostResolverFileSystemError = entities.FactGatheringError{ + Type: "saplocalhost_resolver-file-system-error", + Message: "error reading the sap profiles file system", + } ) type SapLocalhostResolverGatherer struct { @@ -71,7 +75,7 @@ func (r *SapLocalhostResolverGatherer) Gather(factsRequests []entities.FactReque details, err := r.getInstanceHostnameDetails() if err != nil { log.Error(err) - return nil, SapLocalhostResolverHostnameResolutionError.Wrap(err.Error()) + return nil, err } for _, factReq := range factsRequests { @@ -100,7 +104,7 @@ func (r *SapLocalhostResolverGatherer) getInstanceHostnameDetails() (map[string] sid := filepath.Base(system) profileFiles, err := sapsystem.FindProfiles(r.fs, sid) if err != nil { - return nil, err + return nil, SapLocalhostResolverFileSystemError.Wrap(err.Error()) } for _, profileFile := range profileFiles { @@ -114,7 +118,7 @@ func (r *SapLocalhostResolverGatherer) getInstanceHostnameDetails() (map[string] } addresses, err := r.hr.LookupHost(match[3]) if err != nil { - return nil, err + return nil, SapLocalhostResolverHostnameResolutionError.Wrap(err.Error()) } details := ResolvabilityDetails{ diff --git a/internal/factsengine/gatherers/saplocalhostresolver_test.go b/internal/factsengine/gatherers/saplocalhostresolver_test.go index 51614f12..f4815a59 100644 --- a/internal/factsengine/gatherers/saplocalhostresolver_test.go +++ b/internal/factsengine/gatherers/saplocalhostresolver_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "errors" "io" "os" "testing" @@ -29,12 +30,7 @@ func (suite *SapLocalhostResolverTestSuite) SetupTest() { func (suite *SapLocalhostResolverTestSuite) TestSapLocalhostResolverSuccess() { appFS := afero.NewMemMapFs() - err := appFS.MkdirAll("/usr/sap/PRD", 0644) - suite.NoError(err) - err = appFS.MkdirAll("/usr/sap/QAS", 0644) - suite.NoError(err) - - err = afero.WriteFile(appFS, "/sapmnt/PRD/profile/DEFAULT.PFL", []byte{}, 0644) + err := appFS.MkdirAll("/usr/sap/QAS", 0644) suite.NoError(err) ascsProfileFile, _ := os.Open(helpers.GetFixturePath("gatherers/sap_profile.ascs")) @@ -83,3 +79,76 @@ func (suite *SapLocalhostResolverTestSuite) TestSapLocalhostResolverSuccess() { suite.NoError(err) suite.Equal(expectedResults, factResults) } + +func (suite *SapLocalhostResolverTestSuite) TestSapLocalhostResolverNoProfiles() { + appFS := afero.NewMemMapFs() + + err := appFS.MkdirAll("/usr/sap/QAS", 0644) + suite.NoError(err) + + g := gatherers.NewSapLocalhostResolver(appFS, suite.mockResolver) + + factRequests := []entities.FactRequest{{ + Name: "sap_localhost_resolver", + Gatherer: "sap_localhost_resolver", + CheckID: "check1", + }} + + factResults, err := g.Gather(factRequests) + suite.Nil(factResults) + suite.EqualError(err, "fact gathering error: saplocalhost_resolver-file-system-error - error reading the sap profiles file system: open /sapmnt/QAS/profile: file does not exist") +} + +func (suite *SapLocalhostResolverTestSuite) TestSapLocalhostResolverLookupHostError() { + appFS := afero.NewMemMapFs() + + err := appFS.MkdirAll("/usr/sap/QAS", 0644) + suite.NoError(err) + + ascsProfileFile, _ := os.Open(helpers.GetFixturePath("gatherers/sap_profile.ascs")) + ascsProfileConcent, _ := io.ReadAll(ascsProfileFile) + err = afero.WriteFile(appFS, "/sapmnt/QAS/profile/QAS_ASCS00_sapqasas", ascsProfileConcent, 0644) + suite.NoError(err) + + suite.mockResolver.On("LookupHost", "sapqasas").Return([]string{}, errors.New("lookup sapqasas on 169.254.169.254:53: dial udp 169.254.169.254:53: connect: no route to host")) + + g := gatherers.NewSapLocalhostResolver(appFS, suite.mockResolver) + + factRequests := []entities.FactRequest{{ + Name: "sap_localhost_resolver", + Gatherer: "sap_localhost_resolver", + CheckID: "check1", + }} + + factResults, err := g.Gather(factRequests) + + suite.Nil(factResults) + suite.EqualError(err, "fact gathering error: saplocalhost_resolver-resolution-error - error resolving hostname: lookup sapqasas on 169.254.169.254:53: dial udp 169.254.169.254:53: connect: no route to host") +} + +func (suite *SapLocalhostResolverTestSuite) TestSapLocalhostResolverLookupHostErrorInvalidProfile() { + appFS := afero.NewMemMapFs() + + err := appFS.MkdirAll("/usr/sap/QAS", 0644) + suite.NoError(err) + + ascsProfileFile, _ := os.Open(helpers.GetFixturePath("gatherers/sap_profile.ascs")) + ascsProfileConcent, _ := io.ReadAll(ascsProfileFile) + err = afero.WriteFile(appFS, "/sapmnt/QAS/profile/QAS_ASCS00_sapqasas", ascsProfileConcent, 0644) + suite.NoError(err) + + suite.mockResolver.On("LookupHost", "sapqasas").Return([]byte{0x80}, nil) + + g := gatherers.NewSapLocalhostResolver(appFS, suite.mockResolver) + + factRequests := []entities.FactRequest{{ + Name: "sap_localhost_resolver", + Gatherer: "sap_localhost_resolver", + CheckID: "check1", + }} + + factResults, err := g.Gather(factRequests) + + suite.Nil(factResults) + suite.EqualError(err, "fact gathering error: saplocalhost_resolver-file-system-error - error reading the sap profiles file system: open /sapmnt/QAS/profile: file does not exist") +}