Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rtorrero committed Oct 16, 2023
1 parent ef73390 commit ed99aea
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 9 deletions.
10 changes: 7 additions & 3 deletions internal/factsengine/gatherers/saplocalhostresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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{
Expand Down
81 changes: 75 additions & 6 deletions internal/factsengine/gatherers/saplocalhostresolver_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gatherers_test

import (
"errors"
"io"
"os"
"testing"
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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")
}

0 comments on commit ed99aea

Please sign in to comment.