Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle context cancellation in fact gatherers #384

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion internal/factsengine/gatherers/corosyncconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions internal/factsengine/gatherers/corosyncconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 5 additions & 1 deletion internal/factsengine/gatherers/fstab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions internal/factsengine/gatherers/fstab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 5 additions & 1 deletion internal/factsengine/gatherers/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions internal/factsengine/gatherers/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 5 additions & 1 deletion internal/factsengine/gatherers/hostsfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
}
Expand Down
19 changes: 19 additions & 0 deletions internal/factsengine/gatherers/hostsfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 5 additions & 1 deletion internal/factsengine/gatherers/osrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 18 additions & 0 deletions internal/factsengine/gatherers/osrelease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 5 additions & 1 deletion internal/factsengine/gatherers/passwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
}
Expand Down
18 changes: 18 additions & 0 deletions internal/factsengine/gatherers/passwd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading
Loading