From 8b31c587a56c88971a2788fb0368eed7f5e20204 Mon Sep 17 00:00:00 2001 From: balanza Date: Thu, 30 Jan 2025 20:25:16 +0100 Subject: [PATCH 1/3] handle context --- .../factsengine/gatherers/corosyncconf.go | 6 +- .../gatherers/corosyncconf_test.go | 20 +++ internal/factsengine/gatherers/fstab.go | 6 +- internal/factsengine/gatherers/fstab_test.go | 21 +++ internal/factsengine/gatherers/groups.go | 6 +- internal/factsengine/gatherers/groups_test.go | 18 +++ internal/factsengine/gatherers/hostsfile.go | 6 +- .../factsengine/gatherers/hostsfile_test.go | 19 +++ internal/factsengine/gatherers/osrelease.go | 6 +- .../factsengine/gatherers/osrelease_test.go | 18 +++ internal/factsengine/gatherers/passwd.go | 6 +- internal/factsengine/gatherers/passwd_test.go | 18 +++ internal/factsengine/gatherers/sapcontrol.go | 137 ++++++++++-------- .../factsengine/gatherers/sapcontrol_test.go | 22 +++ .../gatherers/sapinstancehostnameresolver.go | 6 +- .../sapinstancehostnameresolver_test.go | 19 +++ internal/factsengine/gatherers/sapprofiles.go | 10 +- .../factsengine/gatherers/sapprofiles_test.go | 19 +++ 18 files changed, 295 insertions(+), 68 deletions(-) diff --git a/internal/factsengine/gatherers/corosyncconf.go b/internal/factsengine/gatherers/corosyncconf.go index 64d1f928..ac8670e1 100644 --- a/internal/factsengine/gatherers/corosyncconf.go +++ b/internal/factsengine/gatherers/corosyncconf.go @@ -51,7 +51,7 @@ func NewCorosyncConfGatherer(configFile string) *CorosyncConfGatherer { } func (s *CorosyncConfGatherer) Gather( - _ context.Context, + ctx context.Context, factsRequests []entities.FactRequest, ) ([]entities.Fact, error) { facts := []entities.Fact{} @@ -82,6 +82,10 @@ func (s *CorosyncConfGatherer) Gather( facts = append(facts, fact) } + if ctx.Err() != nil { + return nil, ctx.Err() + } + log.Infof("Requested corosync.conf file facts gathered") return facts, nil } diff --git a/internal/factsengine/gatherers/corosyncconf_test.go b/internal/factsengine/gatherers/corosyncconf_test.go index 3eb1b81f..84be6450 100644 --- a/internal/factsengine/gatherers/corosyncconf_test.go +++ b/internal/factsengine/gatherers/corosyncconf_test.go @@ -243,3 +243,23 @@ func (suite *CorosyncConfTestSuite) TestCorosyncConfInvalid() { suite.EqualError(err, expectedError.Error()) suite.Empty(factsGathered) } + +func (suite *CorosyncCmapctlTestSuite) TestCorosyncConfContextCancelled() { + c := gatherers.NewCorosyncConfGatherer(helpers.GetFixturePath("gatherers/corosync.conf.one_node")) + + factsRequest := []entities.FactRequest{ + { + Name: "corosync_nodes", + Gatherer: "corosync.conf", + Argument: "nodelist.node", + }, + } + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + factResults, err := c.Gather(ctx, factsRequest) + + suite.Error(err) + suite.Empty(factResults) +} diff --git a/internal/factsengine/gatherers/fstab.go b/internal/factsengine/gatherers/fstab.go index 1139f43c..4a90b9c3 100644 --- a/internal/factsengine/gatherers/fstab.go +++ b/internal/factsengine/gatherers/fstab.go @@ -49,7 +49,7 @@ func NewDefaultFstabGatherer() *FstabGatherer { return &FstabGatherer{fstabFilePath: FstabFilePath} } -func (f *FstabGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (f *FstabGatherer) Gather(ctx context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { log.Infof("Starting %s facts gathering process", FstabGathererName) facts := []entities.Fact{} @@ -80,6 +80,10 @@ func (f *FstabGatherer) Gather(_ context.Context, factsRequests []entities.FactR facts = append(facts, entities.NewFactGatheredWithRequest(requestedFact, factValues)) } + if ctx.Err() != nil { + return nil, ctx.Err() + } + log.Infof("Requested %s facts gathered", FstabGathererName) return facts, nil diff --git a/internal/factsengine/gatherers/fstab_test.go b/internal/factsengine/gatherers/fstab_test.go index da820669..dfbc2ee8 100644 --- a/internal/factsengine/gatherers/fstab_test.go +++ b/internal/factsengine/gatherers/fstab_test.go @@ -158,3 +158,24 @@ func (s *FstabGathererTestSuite) TestFstabGatheringSuccess() { s.NoError(err) s.EqualValues(expectedResults, result) } + +func (suite *SapInstanceHostnameResolverTestSuite) TestFstabContextCancelled() { + + gatherer := gatherers.NewFstabGatherer(helpers.GetFixturePath("gatherers/fstab.valid")) + + factsRequest := []entities.FactRequest{ + { + Name: "fstab", + CheckID: "check1", + Gatherer: "fstab", + }, + } + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + factResults, err := gatherer.Gather(ctx, factsRequest) + + suite.Error(err) + suite.Empty(factResults) +} diff --git a/internal/factsengine/gatherers/groups.go b/internal/factsengine/gatherers/groups.go index 76211fcc..d7557d78 100644 --- a/internal/factsengine/gatherers/groups.go +++ b/internal/factsengine/gatherers/groups.go @@ -50,7 +50,7 @@ func NewGroupsGatherer(groupsFilePath string) *GroupsGatherer { return &GroupsGatherer{groupsFilePath: groupsFilePath} } -func (g *GroupsGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *GroupsGatherer) Gather(ctx context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { log.Infof("Starting %s facts gathering process", GroupsGathererName) facts := []entities.Fact{} @@ -80,6 +80,10 @@ func (g *GroupsGatherer) Gather(_ context.Context, factsRequests []entities.Fact facts = append(facts, entities.NewFactGatheredWithRequest(requestedFact, factValues)) } + if ctx.Err() != nil { + return nil, ctx.Err() + } + log.Infof("Requested %s facts gathered", GroupsGathererName) return facts, nil diff --git a/internal/factsengine/gatherers/groups_test.go b/internal/factsengine/gatherers/groups_test.go index 43ed26ae..e3c52012 100644 --- a/internal/factsengine/gatherers/groups_test.go +++ b/internal/factsengine/gatherers/groups_test.go @@ -111,3 +111,21 @@ func (s *GroupsGathererSuite) TestGroupsParsingDecodeErrorInvalidFormat() { s.Nil(result) s.EqualError(err, "fact gathering error: groups-decoding-error - error deconding groups file: could not decode groups file line daemon:x:1, entry are less then 4") } + +func (s *GroupsGathererSuite) TestGroupsContextCancelled() { + gatherer := gatherers.NewGroupsGatherer(helpers.GetFixturePath("gatherers/groups.valid")) + + factsRequest := []entities.FactRequest{{ + Name: "groups", + Gatherer: "groups", + CheckID: "checkone", + }} + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + factResults, err := gatherer.Gather(ctx, factsRequest) + + s.Error(err) + s.Empty(factResults) +} diff --git a/internal/factsengine/gatherers/hostsfile.go b/internal/factsengine/gatherers/hostsfile.go index 88232dc2..a74545d7 100644 --- a/internal/factsengine/gatherers/hostsfile.go +++ b/internal/factsengine/gatherers/hostsfile.go @@ -54,7 +54,7 @@ func NewHostsFileGatherer(hostsFile string) *HostsFileGatherer { return &HostsFileGatherer{hostsFilePath: hostsFile} } -func (s *HostsFileGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s *HostsFileGatherer) Gather(ctx context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting /etc/hosts file facts gathering process") @@ -85,6 +85,10 @@ func (s *HostsFileGatherer) Gather(_ context.Context, factsRequests []entities.F facts = append(facts, fact) } + if ctx.Err() != nil { + return nil, ctx.Err() + } + log.Infof("Requested /etc/hosts file facts gathered") return facts, nil } diff --git a/internal/factsengine/gatherers/hostsfile_test.go b/internal/factsengine/gatherers/hostsfile_test.go index fb918de1..e78c3627 100644 --- a/internal/factsengine/gatherers/hostsfile_test.go +++ b/internal/factsengine/gatherers/hostsfile_test.go @@ -153,3 +153,22 @@ func (suite *HostsFileTestSuite) TestHostsFileIgnoresCommentedHosts() { suite.NoError(err) suite.ElementsMatch(expectedResults, factResults) } + +func (suite *HostsFileTestSuite) TestHostsFileContextCancelled() { + gatherer := gatherers.NewHostsFileGatherer(helpers.GetFixturePath("gatherers/hosts.basic")) + + factsRequest := []entities.FactRequest{{ + Name: "hosts_localhost", + Gatherer: "hosts", + Argument: "localhost", + CheckID: "check1", + }} + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + factResults, err := gatherer.Gather(ctx, factsRequest) + + suite.Error(err) + suite.Empty(factResults) +} diff --git a/internal/factsengine/gatherers/osrelease.go b/internal/factsengine/gatherers/osrelease.go index 52d7d957..23113b0c 100644 --- a/internal/factsengine/gatherers/osrelease.go +++ b/internal/factsengine/gatherers/osrelease.go @@ -41,7 +41,7 @@ func NewOSReleaseGatherer(path string) *OSReleaseGatherer { } } -func (g *OSReleaseGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *OSReleaseGatherer) Gather(ctx context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", OSReleaseGathererName) @@ -74,6 +74,10 @@ func (g *OSReleaseGatherer) Gather(_ context.Context, factsRequests []entities.F facts = append(facts, fact) } + if ctx.Err() != nil { + return nil, ctx.Err() + } + log.Infof("Requested %s facts gathered", OSReleaseGathererName) return facts, nil } diff --git a/internal/factsengine/gatherers/osrelease_test.go b/internal/factsengine/gatherers/osrelease_test.go index 2a7ea838..7bfa0798 100644 --- a/internal/factsengine/gatherers/osrelease_test.go +++ b/internal/factsengine/gatherers/osrelease_test.go @@ -87,3 +87,21 @@ func (suite *OSReleaseGathererTestSuite) TestOSReleaseGathererErrorDecoding() { suite.EqualError(err, "fact gathering error: os-release-decoding-error - error decoding file content: error on line 3: missing =") } + +func (suite *OSReleaseGathererTestSuite) TestOSReleaseContextCancelled() { + gatherer := gatherers.NewOSReleaseGatherer(helpers.GetFixturePath("gatherers/os-release.basic")) + + factsRequest := []entities.FactRequest{{ + Name: "os-release", + Gatherer: "os-release@v1", + CheckID: "check1", + }} + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + factResults, err := gatherer.Gather(ctx, factsRequest) + + suite.Error(err) + suite.Empty(factResults) +} diff --git a/internal/factsengine/gatherers/passwd.go b/internal/factsengine/gatherers/passwd.go index 17e66710..e40d798d 100644 --- a/internal/factsengine/gatherers/passwd.go +++ b/internal/factsengine/gatherers/passwd.go @@ -54,7 +54,7 @@ func NewPasswdGatherer(path string) *PasswdGatherer { } } -func (g *PasswdGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *PasswdGatherer) Gather(ctx context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", PasswdGathererName) @@ -74,6 +74,10 @@ func (g *PasswdGatherer) Gather(_ context.Context, factsRequests []entities.Fact facts = append(facts, fact) } + if ctx.Err() != nil { + return nil, ctx.Err() + } + log.Infof("Requested %s facts gathered", PasswdGathererName) return facts, nil } diff --git a/internal/factsengine/gatherers/passwd_test.go b/internal/factsengine/gatherers/passwd_test.go index 6d8b5f33..adb7077f 100644 --- a/internal/factsengine/gatherers/passwd_test.go +++ b/internal/factsengine/gatherers/passwd_test.go @@ -108,3 +108,21 @@ func (suite *PasswdTestSuite) TestPasswdErrorDecoding() { suite.EqualError(err, "fact gathering error: passwd-file-error - error reading /etc/passwd file: "+ "invalid passwd file: line 1 entry does not have 7 values") } + +func (suite *PasswdTestSuite) TestPasswdContextCancelled() { + gatherer := gatherers.NewPasswdGatherer(helpers.GetFixturePath("gatherers/passwd.basic")) + + factsRequest := []entities.FactRequest{{ + Name: "passwd", + Gatherer: "passwd", + CheckID: "check1", + }} + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + factResults, err := gatherer.Gather(ctx, factsRequest) + + suite.Error(err) + suite.Empty(factResults) +} diff --git a/internal/factsengine/gatherers/sapcontrol.go b/internal/factsengine/gatherers/sapcontrol.go index 3b4795aa..43e1a449 100644 --- a/internal/factsengine/gatherers/sapcontrol.go +++ b/internal/factsengine/gatherers/sapcontrol.go @@ -141,82 +141,101 @@ func memoizeSapcontrol(args ...interface{}) (interface{}, error) { return webmethod(ctx, conn) } -func (s *SapControlGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { - ctx := context.Background() - - log.Infof("Starting %s facts gathering process", SapControlGathererName) - facts := []entities.Fact{} +func (s *SapControlGatherer) Gather( + ctx context.Context, + factsRequests []entities.FactRequest, +) ([]entities.Fact, error) { foundSystems, err := initSystemsMap(s.fs) if err != nil { return nil, SapcontrolFileSystemError.Wrap(err.Error()) } - for _, factReq := range factsRequests { - if len(factReq.Argument) == 0 { - log.Error(SapcontrolMissingArgument.Error()) - facts = append(facts, entities.NewFactGatheredWithError(factReq, &SapcontrolMissingArgument)) - continue - } + results := make(chan []entities.Fact) + go func() { - webmethod, ok := whitelistedSapControlArguments[factReq.Argument] + log.Infof("Starting %s facts gathering process", SapControlGathererName) + facts := []entities.Fact{} - if !ok { - gatheringError := SapcontrolArgumentUnsupported.Wrap(factReq.Argument) - log.Error(gatheringError) - facts = append(facts, entities.NewFactGatheredWithError(factReq, gatheringError)) - continue + for _, factReq := range factsRequests { + fact := s.gatherSingle(ctx, factReq, foundSystems) + facts = append(facts, fact) } - sapControlMap := make(SapControlMap) - for sid, instances := range foundSystems { - sapControlInstance := []SapControlInstance{} - for _, instanceData := range instances { - instanceName, instanceNumber := instanceData[0], instanceData[1] - cacheEntry := fmt.Sprintf("%s:%s:%s:%s", SapControlGathererCache, factReq.Argument, sid, instanceNumber) - output, err := factscache.GetOrUpdate( - s.cache, - cacheEntry, - memoizeSapcontrol, - ctx, - s.webService, - instanceNumber, - webmethod, - ) - - if err != nil { - log.Error(SapcontrolWebmethodError. - Wrap(fmt.Sprintf("argument %s for %s/%s", factReq.Argument, sid, instanceName)). - Wrap(err.Error())) - continue - } - sapControlInstance = append(sapControlInstance, SapControlInstance{ - Name: instanceName, - InstanceNr: instanceNumber, - Output: output, - }) - sapControlMap[sid] = sapControlInstance - } - } + log.Infof("Requested %s facts gathered", SapControlGathererName) - var fact entities.Fact + results <- facts + }() - if factValue, err := outputToFactValue(sapControlMap); err != nil { - gatheringError := SapcontrolDecodingError. - Wrap(fmt.Sprintf("argument: %s", factReq.Argument)). - Wrap(err.Error()) - log.Error(gatheringError) - fact = entities.NewFactGatheredWithError(factReq, gatheringError) - } else { - fact = entities.NewFactGatheredWithRequest(factReq, factValue) + select { + case <-ctx.Done(): + return nil, ctx.Err() + case facts := <-results: + return facts, nil + } +} + +func (s *SapControlGatherer) gatherSingle( + ctx context.Context, + factReq entities.FactRequest, + foundSystems map[string][][]string, +) entities.Fact { + + if len(factReq.Argument) == 0 { + log.Error(SapcontrolMissingArgument.Error()) + return entities.NewFactGatheredWithError(factReq, &SapcontrolMissingArgument) + } + + webmethod, ok := whitelistedSapControlArguments[factReq.Argument] + + if !ok { + gatheringError := SapcontrolArgumentUnsupported.Wrap(factReq.Argument) + log.Error(gatheringError) + return entities.NewFactGatheredWithError(factReq, gatheringError) + } + + sapControlMap := make(SapControlMap) + for sid, instances := range foundSystems { + sapControlInstance := []SapControlInstance{} + for _, instanceData := range instances { + instanceName, instanceNumber := instanceData[0], instanceData[1] + cacheEntry := fmt.Sprintf("%s:%s:%s:%s", SapControlGathererCache, factReq.Argument, sid, instanceNumber) + output, err := factscache.GetOrUpdate( + s.cache, + cacheEntry, + memoizeSapcontrol, + ctx, + s.webService, + instanceNumber, + webmethod, + ) + + if err != nil { + log.Error(SapcontrolWebmethodError. + Wrap(fmt.Sprintf("argument %s for %s/%s", factReq.Argument, sid, instanceName)). + Wrap(err.Error())) + continue + } + sapControlInstance = append(sapControlInstance, SapControlInstance{ + Name: instanceName, + InstanceNr: instanceNumber, + Output: output, + }) + sapControlMap[sid] = sapControlInstance } + } - facts = append(facts, fact) + factValue, err := outputToFactValue(sapControlMap) + if err != nil { + gatheringError := SapcontrolDecodingError. + Wrap(fmt.Sprintf("argument: %s", factReq.Argument)). + Wrap(err.Error()) + log.Error(gatheringError) + return entities.NewFactGatheredWithError(factReq, gatheringError) } - log.Infof("Requested %s facts gathered", SapControlGathererName) + return entities.NewFactGatheredWithRequest(factReq, factValue) - return facts, nil } func initSystemsMap(fs afero.Fs) (map[string][][]string, error) { diff --git a/internal/factsengine/gatherers/sapcontrol_test.go b/internal/factsengine/gatherers/sapcontrol_test.go index d86e5e3d..c82f53b8 100644 --- a/internal/factsengine/gatherers/sapcontrol_test.go +++ b/internal/factsengine/gatherers/sapcontrol_test.go @@ -642,3 +642,25 @@ func (suite *SapControlGathererSuite) TestSapControlGathererHAGetFailoverConfig( suite.NoError(err) suite.EqualValues(expectedFacts, results) } + +func (suite *SapControlGathererSuite) TestSapControlGathererContextCancelled() { + + gatherer := gatherers.NewSapControlGatherer(suite.webService, suite.testFS, nil) + + factsRequest := []entities.FactRequest{ + { + Name: "missing_argument", + Gatherer: "sapcontrol", + CheckID: "check1", + Argument: "", + }, + } + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + factResults, err := gatherer.Gather(ctx, factsRequest) + + suite.Error(err) + suite.Empty(factResults) +} diff --git a/internal/factsengine/gatherers/sapinstancehostnameresolver.go b/internal/factsengine/gatherers/sapinstancehostnameresolver.go index 437933ec..91bf6fe6 100644 --- a/internal/factsengine/gatherers/sapinstancehostnameresolver.go +++ b/internal/factsengine/gatherers/sapinstancehostnameresolver.go @@ -99,7 +99,7 @@ func NewSapInstanceHostnameResolverGatherer( } func (r *SapInstanceHostnameResolverGatherer) Gather( - _ context.Context, + ctx context.Context, factsRequests []entities.FactRequest, ) ([]entities.Fact, error) { facts := []entities.Fact{} @@ -122,6 +122,10 @@ func (r *SapInstanceHostnameResolverGatherer) Gather( facts = append(facts, fact) } + if ctx.Err() != nil { + return nil, ctx.Err() + } + return facts, nil } diff --git a/internal/factsengine/gatherers/sapinstancehostnameresolver_test.go b/internal/factsengine/gatherers/sapinstancehostnameresolver_test.go index 99d7253e..87ead32d 100644 --- a/internal/factsengine/gatherers/sapinstancehostnameresolver_test.go +++ b/internal/factsengine/gatherers/sapinstancehostnameresolver_test.go @@ -187,3 +187,22 @@ func (suite *SapInstanceHostnameResolverTestSuite) TestSapInstanceHostnameResolv suite.NoError(err) suite.Equal(expectedResults, factResults) } + +func (suite *SapInstanceHostnameResolverTestSuite) TestSapInstanceHostnameResolverContextCancelled() { + appFS := afero.NewMemMapFs() + gatherer := gatherers.NewSapInstanceHostnameResolverGatherer(appFS, suite.mockResolver, suite.mockPinger) + + factsRequest := []entities.FactRequest{{ + Name: "sapinstance_hostname_resolver", + Gatherer: "sapinstance_hostname_resolver", + CheckID: "check1", + }} + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + factResults, err := gatherer.Gather(ctx, factsRequest) + + suite.Error(err) + suite.Empty(factResults) +} diff --git a/internal/factsengine/gatherers/sapprofiles.go b/internal/factsengine/gatherers/sapprofiles.go index 89d36a35..b1d1c391 100644 --- a/internal/factsengine/gatherers/sapprofiles.go +++ b/internal/factsengine/gatherers/sapprofiles.go @@ -54,7 +54,10 @@ func NewSapProfilesGatherer(fs afero.Fs) *SapProfilesGatherer { return &SapProfilesGatherer{fs: fs} } -func (s *SapProfilesGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s *SapProfilesGatherer) Gather( + ctx context.Context, + factsRequests []entities.FactRequest, +) ([]entities.Fact, error) { log.Infof("Starting %s facts gathering process", SapProfilesGathererName) facts := []entities.Fact{} systems := make(SapSystemMap) @@ -86,8 +89,11 @@ func (s *SapProfilesGatherer) Gather(_ context.Context, factsRequests []entities facts = append(facts, entities.NewFactGatheredWithRequest(requestedFact, factValues)) } - log.Infof("Requested %s facts gathered", SapProfilesGathererName) + if ctx.Err() != nil { + return nil, ctx.Err() + } + log.Infof("Requested %s facts gathered", SapProfilesGathererName) return facts, nil } diff --git a/internal/factsengine/gatherers/sapprofiles_test.go b/internal/factsengine/gatherers/sapprofiles_test.go index 2226b0c8..de540aad 100644 --- a/internal/factsengine/gatherers/sapprofiles_test.go +++ b/internal/factsengine/gatherers/sapprofiles_test.go @@ -399,3 +399,22 @@ func (suite *SapProfilesTestSuite) TestSapProfilesInvalidProfile() { "error reading the sap profiles file system: could not parse profile file: error "+ "on line 1: missing =") } + +func (suite *SapProfilesTestSuite) TestSapProfilesContextCancelled() { + appFS := afero.NewMemMapFs() + gatherer := gatherers.NewSapProfilesGatherer(appFS) + + factsRequest := []entities.FactRequest{{ + Name: "sap_profiles", + Gatherer: "sap_profiles", + CheckID: "check1", + }} + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + factResults, err := gatherer.Gather(ctx, factsRequest) + + suite.Error(err) + suite.Empty(factResults) +} From c0f3a90481dc8274df12fde2110d385725e00a22 Mon Sep 17 00:00:00 2001 From: balanza Date: Wed, 5 Feb 2025 09:54:31 +0100 Subject: [PATCH 2/3] fix --- internal/factsengine/gatherers/sapcontrol.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/factsengine/gatherers/sapcontrol.go b/internal/factsengine/gatherers/sapcontrol.go index 43e1a449..5e0f3a52 100644 --- a/internal/factsengine/gatherers/sapcontrol.go +++ b/internal/factsengine/gatherers/sapcontrol.go @@ -210,6 +210,12 @@ func (s *SapControlGatherer) gatherSingle( webmethod, ) + // If the context is done, avoid processing all the instances + // The error will be handled by the caller anyway + if ctx.Err() != nil { + return entities.Fact{} + } + if err != nil { log.Error(SapcontrolWebmethodError. Wrap(fmt.Sprintf("argument %s for %s/%s", factReq.Argument, sid, instanceName)). From 017f74a46a84f4f025025d654e4bfbd659053d8c Mon Sep 17 00:00:00 2001 From: balanza Date: Wed, 5 Feb 2025 10:04:53 +0100 Subject: [PATCH 3/3] fix --- internal/factsengine/gatherers/sapcontrol.go | 26 +++++++------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/internal/factsengine/gatherers/sapcontrol.go b/internal/factsengine/gatherers/sapcontrol.go index 5e0f3a52..85af2801 100644 --- a/internal/factsengine/gatherers/sapcontrol.go +++ b/internal/factsengine/gatherers/sapcontrol.go @@ -151,28 +151,20 @@ func (s *SapControlGatherer) Gather( return nil, SapcontrolFileSystemError.Wrap(err.Error()) } - results := make(chan []entities.Fact) - go func() { + log.Infof("Starting %s facts gathering process", SapControlGathererName) + facts := []entities.Fact{} - log.Infof("Starting %s facts gathering process", SapControlGathererName) - facts := []entities.Fact{} - - for _, factReq := range factsRequests { - fact := s.gatherSingle(ctx, factReq, foundSystems) - facts = append(facts, fact) - } - - log.Infof("Requested %s facts gathered", SapControlGathererName) + for _, factReq := range factsRequests { + fact := s.gatherSingle(ctx, factReq, foundSystems) + facts = append(facts, fact) + } - results <- facts - }() + log.Infof("Requested %s facts gathered", SapControlGathererName) - select { - case <-ctx.Done(): + if ctx.Err() != nil { return nil, ctx.Err() - case facts := <-results: - return facts, nil } + return facts, nil } func (s *SapControlGatherer) gatherSingle(