Skip to content

Commit

Permalink
Adjust gatherer name
Browse files Browse the repository at this point in the history
  • Loading branch information
rtorrero committed Oct 18, 2023
1 parent f693b15 commit 7558959
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
4 changes: 2 additions & 2 deletions internal/factsengine/gatherers/gatherer.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ func StandardGatherers() FactGatherersTree {
SapHostCtrlGathererName: map[string]FactGatherer{
"v1": NewDefaultSapHostCtrlGatherer(),
},
SapLocalhostResolverGathererName: map[string]FactGatherer{
"v1": NewDefaultSapLocalhostResolverGatherer(),
SapInstanceHostnameResolverGathererName: map[string]FactGatherer{
"v1": NewDefaultSapInstanceHostnameResolverGatherer(),
},
SapProfilesGathererName: map[string]FactGatherer{
"v1": NewDefaultSapProfilesGatherer(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ import (
)

const (
SapLocalhostResolverGathererName = "saplocalhost_resolver"
SapInstanceHostnameResolverGathererName = "sapinstance_hostname_resolver"
)

// nolint:gochecknoglobals
var (
hostnameRegexCompiled = regexp.MustCompile(`(.+)_(.+)_(.+)`) // <SID>_<InstanceNumber>_<Hostname>
regexSubgroupsCount = 4
SapLocalhostResolverDetailsError = entities.FactGatheringError{
Type: "saplocalhost_resolver-details-error",
hostnameRegexCompiled = regexp.MustCompile(`(.+)_(.+)_(.+)`) // <SID>_<InstanceNumber>_<Hostname>
regexSubgroupsCount = 4
SapInstanceHostnameResolverDetailsError = entities.FactGatheringError{
Type: "sapinstance_hostname_resolver-details-error",
Message: "error gathering details",
}
SapLocalhostResolverGathererDecodingError = entities.FactGatheringError{
Type: "saplocalhost_resolver-decoding-error",
SapInstanceHostnameResolverGathererDecodingError = entities.FactGatheringError{
Type: "sapinstance_hostname_resolver-decoding-error",
Message: "error decoding output to FactValue",
}
)
Expand All @@ -41,7 +41,7 @@ type HostPinger interface {
Ping(host string) bool
}

type SapLocalhostResolverGatherer struct {
type SapInstanceHostnameResolverGatherer struct {
fs afero.Fs
hr HostnameResolver
hp HostPinger
Expand Down Expand Up @@ -72,28 +72,32 @@ func (p *Pinger) Ping(host string) bool {
return err == nil
}

func NewDefaultSapLocalhostResolverGatherer() *SapLocalhostResolverGatherer {
return NewSapLocalhostResolver(afero.NewOsFs(), &Resolver{}, &Pinger{})
func NewDefaultSapInstanceHostnameResolverGatherer() *SapInstanceHostnameResolverGatherer {
return NewSapInstanceHostnameResolverGatherer(afero.NewOsFs(), &Resolver{}, &Pinger{})
}

func NewSapLocalhostResolver(fs afero.Fs, hr HostnameResolver, hp HostPinger) *SapLocalhostResolverGatherer {
return &SapLocalhostResolverGatherer{fs: fs, hr: hr, hp: hp}
func NewSapInstanceHostnameResolverGatherer(
fs afero.Fs,
hr HostnameResolver,
hp HostPinger) *SapInstanceHostnameResolverGatherer {

return &SapInstanceHostnameResolverGatherer{fs: fs, hr: hr, hp: hp}
}

func (r *SapLocalhostResolverGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) {
func (r *SapInstanceHostnameResolverGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) {
facts := []entities.Fact{}

details, err := r.getInstanceHostnameDetails()
if err != nil {
log.Error(err)
return nil, SapLocalhostResolverDetailsError.Wrap(err.Error())
return nil, SapInstanceHostnameResolverDetailsError.Wrap(err.Error())
}

var fact entities.Fact
factValue, err := mapReachabilityDetailsToFactValue(details)
if err != nil {
log.Error(err)
return facts, &SapLocalhostResolverGathererDecodingError
return facts, &SapInstanceHostnameResolverGathererDecodingError
}

for _, factReq := range factsRequests {
Expand All @@ -104,7 +108,7 @@ func (r *SapLocalhostResolverGatherer) Gather(factsRequests []entities.FactReque
return facts, nil
}

func (r *SapLocalhostResolverGatherer) getInstanceHostnameDetails() (map[string][]ResolvabilityDetails, error) {
func (r *SapInstanceHostnameResolverGatherer) getInstanceHostnameDetails() (map[string][]ResolvabilityDetails, error) {
systems, err := sapsystem.FindSystems(r.fs)
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ import (
"github.com/trento-project/agent/pkg/factsengine/entities"
)

type SapLocalhostResolverTestSuite struct {
type SapInstanceHostnameResolverTestSuite struct {
suite.Suite
mockResolver *mocks.HostnameResolver
mockPinger *mocks.HostPinger
}

func TestSapLocalhostResolverTestSuite(t *testing.T) {
suite.Run(t, new(SapLocalhostResolverTestSuite))
func TestSapInstanceHostnameResolverTestSuite(t *testing.T) {
suite.Run(t, new(SapInstanceHostnameResolverTestSuite))
}

func (suite *SapLocalhostResolverTestSuite) SetupTest() {
func (suite *SapInstanceHostnameResolverTestSuite) SetupTest() {
suite.mockResolver = new(mocks.HostnameResolver)
suite.mockPinger = new(mocks.HostPinger)
}

func (suite *SapLocalhostResolverTestSuite) TestSapLocalhostResolverSuccess() {
func (suite *SapInstanceHostnameResolverTestSuite) TestSapInstanceHostnameResolverSuccess() {
appFS := afero.NewMemMapFs()

err := appFS.MkdirAll("/usr/sap/QAS", 0644)
Expand All @@ -48,19 +48,19 @@ func (suite *SapLocalhostResolverTestSuite) TestSapLocalhostResolverSuccess() {
suite.mockResolver.On("LookupHost", "sapnwper").Return([]string{"10.1.1.7"}, nil)
suite.mockPinger.On("Ping", "sapnwper").Return(false, nil)

g := gatherers.NewSapLocalhostResolver(appFS, suite.mockResolver, suite.mockPinger)
g := gatherers.NewSapInstanceHostnameResolverGatherer(appFS, suite.mockResolver, suite.mockPinger)

factRequests := []entities.FactRequest{{
Name: "sap_localhost_resolver",
Gatherer: "sap_localhost_resolver",
Name: "sapinstance_hostname_resolver",
Gatherer: "sapinstance_hostname_resolver",
CheckID: "check1",
}}

factResults, err := g.Gather(factRequests)

expectedResults := []entities.Fact{
{
Name: "sap_localhost_resolver",
Name: "sapinstance_hostname_resolver",
CheckID: "check1",
Value: &entities.FactValueMap{
Value: map[string]entities.FactValue{
Expand Down Expand Up @@ -117,26 +117,26 @@ func (suite *SapLocalhostResolverTestSuite) TestSapLocalhostResolverSuccess() {
suite.Equal(expectedResults, factResults)
}

func (suite *SapLocalhostResolverTestSuite) TestSapLocalhostResolverNoProfiles() {
func (suite *SapInstanceHostnameResolverTestSuite) TestSapInstanceHostnameResolverNoProfiles() {
appFS := afero.NewMemMapFs()

err := appFS.MkdirAll("/usr/sap/QAS", 0644)
suite.NoError(err)

g := gatherers.NewSapLocalhostResolver(appFS, suite.mockResolver, suite.mockPinger)
g := gatherers.NewSapInstanceHostnameResolverGatherer(appFS, suite.mockResolver, suite.mockPinger)

factRequests := []entities.FactRequest{{
Name: "sap_localhost_resolver",
Gatherer: "sap_localhost_resolver",
Name: "sapinstance_hostname_resolver",
Gatherer: "sapinstance_hostname_resolver",
CheckID: "check1",
}}

factResults, err := g.Gather(factRequests)
suite.Nil(factResults)
suite.EqualError(err, "fact gathering error: saplocalhost_resolver-details-error - error gathering details: open /sapmnt/QAS/profile: file does not exist")
suite.EqualError(err, "fact gathering error: sapinstance_hostname_resolver-details-error - error gathering details: open /sapmnt/QAS/profile: file does not exist")
}

func (suite *SapLocalhostResolverTestSuite) TestSapLocalhostResolverLookupHostError() {
func (suite *SapInstanceHostnameResolverTestSuite) TestSapInstanceHostnameResolverLookupHostError() {
appFS := afero.NewMemMapFs()

err := appFS.MkdirAll("/usr/sap/QAS", 0644)
Expand All @@ -148,17 +148,17 @@ func (suite *SapLocalhostResolverTestSuite) TestSapLocalhostResolverLookupHostEr
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"))
suite.mockPinger.On("Ping", "sapqasas").Return(false, nil)

g := gatherers.NewSapLocalhostResolver(appFS, suite.mockResolver, suite.mockPinger)
g := gatherers.NewSapInstanceHostnameResolverGatherer(appFS, suite.mockResolver, suite.mockPinger)

factRequests := []entities.FactRequest{{
Name: "sap_localhost_resolver",
Gatherer: "sap_localhost_resolver",
Name: "sapinstance_hostname_resolver",
Gatherer: "sapinstance_hostname_resolver",
CheckID: "check1",
}}

expectedResults := []entities.Fact{
{
Name: "sap_localhost_resolver",
Name: "sapinstance_hostname_resolver",
CheckID: "check1",
Value: &entities.FactValueMap{
Value: map[string]entities.FactValue{
Expand Down

0 comments on commit 7558959

Please sign in to comment.