From 5d15b6b8e9bb8acceb6815883185e6ea5e25980e Mon Sep 17 00:00:00 2001 From: balanza Date: Fri, 27 Dec 2024 18:08:00 +0100 Subject: [PATCH 01/12] add context to gather method --- README.md | 2 +- cmd/facts.go | 4 ++-- .../factsengine_integration_test.go | 2 +- .../factsengine/gatherers/_todo/crmmon.go | 4 +++- .../gatherers/_todo/crmmon_test.go | 7 +++--- .../factsengine/gatherers/ascsers_cluster.go | 2 +- .../gatherers/ascsers_cluster_test.go | 10 ++++---- internal/factsengine/gatherers/cibadmin.go | 4 +++- .../factsengine/gatherers/cibadmin_test.go | 13 ++++++----- .../factsengine/gatherers/corosynccmapctl.go | 3 ++- .../gatherers/corosynccmapctl_test.go | 9 ++++---- .../factsengine/gatherers/corosyncconf.go | 3 ++- .../gatherers/corosyncconf_test.go | 11 +++++---- internal/factsengine/gatherers/dir_scan.go | 3 ++- .../factsengine/gatherers/dir_scan_test.go | 7 +++--- internal/factsengine/gatherers/dispwork.go | 3 ++- .../factsengine/gatherers/dispwork_test.go | 5 ++-- internal/factsengine/gatherers/fstab.go | 3 ++- internal/factsengine/gatherers/fstab_test.go | 7 +++--- internal/factsengine/gatherers/gatherer.go | 4 +++- internal/factsengine/gatherers/groups.go | 3 ++- internal/factsengine/gatherers/groups_test.go | 7 +++--- internal/factsengine/gatherers/hostsfile.go | 3 ++- .../factsengine/gatherers/hostsfile_test.go | 7 +++--- .../gatherers/mocks/FactGatherer.go | 3 ++- internal/factsengine/gatherers/mountinfo.go | 3 ++- .../factsengine/gatherers/mountinfo_test.go | 7 +++--- internal/factsengine/gatherers/osrelease.go | 3 ++- .../factsengine/gatherers/osrelease_test.go | 7 +++--- .../factsengine/gatherers/packageversion.go | 3 ++- .../gatherers/packageversion_test.go | 7 +++--- internal/factsengine/gatherers/passwd.go | 3 ++- internal/factsengine/gatherers/passwd_test.go | 7 +++--- internal/factsengine/gatherers/plugin.go | 1 + internal/factsengine/gatherers/products.go | 3 ++- .../factsengine/gatherers/products_test.go | 7 +++--- internal/factsengine/gatherers/sapcontrol.go | 2 +- .../factsengine/gatherers/sapcontrol_test.go | 16 ++++++------- internal/factsengine/gatherers/saphostctrl.go | 3 ++- .../factsengine/gatherers/saphostctrl_test.go | 11 +++++---- .../gatherers/sapinstancehostnameresolver.go | 3 ++- .../sapinstancehostnameresolver_test.go | 7 +++--- internal/factsengine/gatherers/sapprofiles.go | 3 ++- .../factsengine/gatherers/sapprofiles_test.go | 7 +++--- internal/factsengine/gatherers/sapservices.go | 3 ++- .../factsengine/gatherers/sapservices_test.go | 17 +++++++------- internal/factsengine/gatherers/saptune.go | 3 ++- .../factsengine/gatherers/saptune_test.go | 23 ++++++++++--------- internal/factsengine/gatherers/sbd.go | 4 +++- internal/factsengine/gatherers/sbd_test.go | 9 ++++---- internal/factsengine/gatherers/sbddump.go | 3 ++- .../factsengine/gatherers/sbddump_test.go | 7 +++--- internal/factsengine/gatherers/sysctl.go | 3 ++- internal/factsengine/gatherers/sysctl_test.go | 13 ++++++----- internal/factsengine/gatherers/systemd.go | 2 +- .../factsengine/gatherers/systemd_test.go | 9 ++++---- internal/factsengine/gatherers/systemd_v2.go | 2 +- .../factsengine/gatherers/systemd_v2_test.go | 9 ++++---- .../factsengine/gatherers/verifypassword.go | 3 ++- .../gatherers/verifypassword_test.go | 15 ++++++------ internal/factsengine/gathering.go | 6 +++-- pkg/factsengine/plugininterface/interface.go | 3 ++- pkg/factsengine/plugininterface/rpc.go | 7 +++--- plugin_examples/dummy.go | 3 ++- 64 files changed, 218 insertions(+), 158 deletions(-) diff --git a/README.md b/README.md index a6003ee1..a33802a1 100644 --- a/README.md +++ b/README.md @@ -253,7 +253,7 @@ To create a new plugin (check the [example](plugin_examples/dummy.go) dummy plug - Create a new Golang package. This is as simple as creating a new folder (it can be created anywhere, it doesn't need to be in the Agent code directory) with `.go` file inside. Name the Golang file with a meaningful name (even though, it is not relevant for the usage itself). - The `.go` file implements the `main` package and imports the `go-plugin` package as seen in the example. -- Implement the gathering function with the `func (s exampleGatherer) Gather(factsRequests []gatherers.FactRequest) ([]gatherers.Fact, error)` signature. This function must gather the facts from the system where the Agent is running. +- Implement the gathering function with the `func (s exampleGatherer) Gather(ctx context.Context, factsRequests []gatherers.FactRequest) ([]gatherers.Fact, error)` signature. This function must gather the facts from the system where the Agent is running. - This function receives a list of fact gathering requests to gather, which entirely depends on the gathering code nature. - Copy the `main()` function from the [example](plugin_examples/dummy.go) file. Simply replace the gatherer struct name there. - Once the plugin is implemented, it must be compiled. Use the next command for that: `go build -o /usr/etc/trento/example ./your_plugin_folder/example.go`. The `-o` flag specifies the destination of the created binary, which the Agent needs to load. This folder is the same specified in the `--plugins-folder` flag in the Agent execution. In this case, the used name for the output in the `-o` flag is relevant, as this name is the gatherer name that must be used in the server side checks declaration. diff --git a/cmd/facts.go b/cmd/facts.go index 8a9aeb38..096f3fc7 100644 --- a/cmd/facts.go +++ b/cmd/facts.go @@ -71,7 +71,7 @@ func NewFactsListCmd() *cobra.Command { return gatherCmd } -func gather(*cobra.Command, []string) { +func gather(cmd *cobra.Command, _ []string) { var gatherer = viper.GetString("gatherer") var argument = viper.GetString("argument") var pluginsFolder = viper.GetString("plugins-folder") @@ -108,7 +108,7 @@ func gather(*cobra.Command, []string) { }, } - value, err := g.Gather(factRequest) + value, err := g.Gather(cmd.Context(), factRequest) if err != nil { log.Errorf("Error gathering fact \"%s\" with argument \"%s\"", gatherer, argument) cleanupAndFatal(err) diff --git a/internal/factsengine/factsengine_integration_test.go b/internal/factsengine/factsengine_integration_test.go index ed009d66..e24d3220 100644 --- a/internal/factsengine/factsengine_integration_test.go +++ b/internal/factsengine/factsengine_integration_test.go @@ -65,7 +65,7 @@ func NewFactsEngineIntegrationTestGatherer() *FactsEngineIntegrationTestGatherer return &FactsEngineIntegrationTestGatherer{} } -func (s *FactsEngineIntegrationTestGatherer) Gather(requests []entities.FactRequest) ([]entities.Fact, error) { +func (s *FactsEngineIntegrationTestGatherer) Gather(_ context.Context, requests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} for i, req := range requests { fact := entities.Fact{ diff --git a/internal/factsengine/gatherers/_todo/crmmon.go b/internal/factsengine/gatherers/_todo/crmmon.go index 9dad356f..1b672622 100644 --- a/internal/factsengine/gatherers/_todo/crmmon.go +++ b/internal/factsengine/gatherers/_todo/crmmon.go @@ -1,6 +1,8 @@ package gatherers import ( + "context" + log "github.com/sirupsen/logrus" "github.com/trento-project/agent/pkg/factsengine/entities" ) @@ -23,7 +25,7 @@ func NewCrmMonGatherer(executor CommandExecutor) *CrmMonGatherer { } } -func (g *CrmMonGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.FactsGatheredItem, error) { +func (g *CrmMonGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.FactsGatheredItem, error) { log.Infof("Starting crmmon facts gathering process") crmmon, err := g.executor.Exec("crm_mon", "--output-as", "xml") diff --git a/internal/factsengine/gatherers/_todo/crmmon_test.go b/internal/factsengine/gatherers/_todo/crmmon_test.go index d16a588d..a8b09b86 100644 --- a/internal/factsengine/gatherers/_todo/crmmon_test.go +++ b/internal/factsengine/gatherers/_todo/crmmon_test.go @@ -1,6 +1,7 @@ package gatherers // nolint import ( + "context" "errors" "io" "os" @@ -51,7 +52,7 @@ func (suite *CrmMonTestSuite) TestCrmMonGather() { }, } - factResults, err := p.Gather(factRequests) + factResults, err := p.Gather(context.Background(), actRequests) expectedResults := []entities.FactsGatheredItem{ { @@ -90,7 +91,7 @@ func (suite *CrmMonTestSuite) TestCrmMonGatherCmdNotFound() { }, } - _, err := p.Gather(factRequests) + _, err := p.Gather(context.Background(), actRequests) suite.EqualError(err, "crm_mon not found") } @@ -117,7 +118,7 @@ func (suite *CrmMonTestSuite) TestCrmMonGatherError() { }, } - factResults, err := p.Gather(factRequests) + factResults, err := p.Gather(context.Background(), actRequests) expectedResults := []entities.FactsGatheredItem{ { diff --git a/internal/factsengine/gatherers/ascsers_cluster.go b/internal/factsengine/gatherers/ascsers_cluster.go index c2abe11a..88424fb0 100644 --- a/internal/factsengine/gatherers/ascsers_cluster.go +++ b/internal/factsengine/gatherers/ascsers_cluster.go @@ -74,7 +74,7 @@ func (g *AscsErsClusterGatherer) SetCache(cache *factscache.FactsCache) { g.cache = cache } -func (g *AscsErsClusterGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *AscsErsClusterGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { log.Infof("Starting %s facts gathering process", AscsErsClusterGathererName) var cibdata cib.Root diff --git a/internal/factsengine/gatherers/ascsers_cluster_test.go b/internal/factsengine/gatherers/ascsers_cluster_test.go index 47ca0a88..25963cf8 100644 --- a/internal/factsengine/gatherers/ascsers_cluster_test.go +++ b/internal/factsengine/gatherers/ascsers_cluster_test.go @@ -50,7 +50,7 @@ func (suite *AscsErsClusterTestSuite) TestAscsErsClusterGatherCmdNotFound() { }, } - _, err := p.Gather(factRequests) + _, err := p.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: cibadmin-command-error - "+ "error running cibadmin command: cibadmin not found") @@ -74,7 +74,7 @@ func (suite *AscsErsClusterTestSuite) TestAscsErsClusterGatherCacheCastingError( }, } - _, err = p.Gather(factRequests) + _, err = p.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: ascsers-cluster-decoding-error - "+ "error decoding cibadmin output: error casting the command output") @@ -98,7 +98,7 @@ func (suite *AscsErsClusterTestSuite) TestAscsErsClusterGatherInvalidInstanceNam }, } - _, err := p.Gather(factRequests) + _, err := p.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: ascsers-cluster-cib-error - "+ "error parsing cibadmin output: incorrect InstanceName property value: PRD_ASCS00") @@ -123,7 +123,7 @@ func (suite *AscsErsClusterTestSuite) TestAscsErsClusterGatherInvalidInstanceNum }, } - _, err := p.Gather(factRequests) + _, err := p.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: ascsers-cluster-cib-error - "+ "error parsing cibadmin output: "+ @@ -195,7 +195,7 @@ func (suite *AscsErsClusterTestSuite) TestAscsErsClusterGather() { }, } - results, err := p.Gather(factRequests) + results, err := p.Gather(context.Background(), factRequests) // nolint:dupl expectedFacts := []entities.Fact{ diff --git a/internal/factsengine/gatherers/cibadmin.go b/internal/factsengine/gatherers/cibadmin.go index c2dfddab..cb871d8a 100644 --- a/internal/factsengine/gatherers/cibadmin.go +++ b/internal/factsengine/gatherers/cibadmin.go @@ -1,6 +1,8 @@ package gatherers import ( + "context" + log "github.com/sirupsen/logrus" "github.com/trento-project/agent/internal/factsengine/factscache" "github.com/trento-project/agent/pkg/factsengine/entities" @@ -53,7 +55,7 @@ func memoizeCibAdmin(args ...interface{}) (interface{}, error) { return executor.Exec("cibadmin", "--query", "--local") } -func (g *CibAdminGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *CibAdminGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { log.Infof("Starting %s facts gathering process", CibAdminGathererName) content, err := factscache.GetOrUpdate(g.cache, CibAdminGathererCache, memoizeCibAdmin, g.executor) diff --git a/internal/factsengine/gatherers/cibadmin_test.go b/internal/factsengine/gatherers/cibadmin_test.go index 92119eb0..6b934e2c 100644 --- a/internal/factsengine/gatherers/cibadmin_test.go +++ b/internal/factsengine/gatherers/cibadmin_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "errors" "io" "os" @@ -50,7 +51,7 @@ func (suite *CibAdminTestSuite) TestCibAdminGatherCmdNotFound() { }, } - _, err := p.Gather(factRequests) + _, err := p.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: cibadmin-command-error - "+ "error running cibadmin command: cibadmin not found") @@ -71,7 +72,7 @@ func (suite *CibAdminTestSuite) TestCibAdminInvalidXML() { }, } - _, err := p.Gather(factRequests) + _, err := p.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: cibadmin-decoding-error - "+ "error decoding cibadmin output: EOF") @@ -116,7 +117,7 @@ func (suite *CibAdminTestSuite) TestCibAdminGather() { }, } - factResults, err := p.Gather(factRequests) + factResults, err := p.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -234,11 +235,11 @@ func (suite *CibAdminTestSuite) TestCibAdminGatherWithCache() { }, } - factResults, err := p.Gather(factRequests) + factResults, err := p.Gather(context.Background(), factRequests) suite.NoError(err) suite.ElementsMatch(expectedResults, factResults) - _, err = p.Gather(factRequests) + _, err = p.Gather(context.Background(), factRequests) suite.NoError(err) entries := cache.Entries() @@ -263,7 +264,7 @@ func (suite *CibAdminTestSuite) TestCibAdminGatherCacheCastingError() { }, } - _, err = p.Gather(factRequests) + _, err = p.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: cibadmin-decoding-error - "+ "error decoding cibadmin output: error casting the command output") diff --git a/internal/factsengine/gatherers/corosynccmapctl.go b/internal/factsengine/gatherers/corosynccmapctl.go index 48a6bdd2..280488e3 100644 --- a/internal/factsengine/gatherers/corosynccmapctl.go +++ b/internal/factsengine/gatherers/corosynccmapctl.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "strings" log "github.com/sirupsen/logrus" @@ -80,7 +81,7 @@ func corosyncCmapctlOutputToMap(corosyncCmapctlOutput string) *entities.FactValu return outputMap } -func (s *CorosyncCmapctlGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s *CorosyncCmapctlGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", CorosyncCmapCtlGathererName) diff --git a/internal/factsengine/gatherers/corosynccmapctl_test.go b/internal/factsengine/gatherers/corosynccmapctl_test.go index c1136990..de72f657 100644 --- a/internal/factsengine/gatherers/corosynccmapctl_test.go +++ b/internal/factsengine/gatherers/corosynccmapctl_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "io" "os" "os/exec" @@ -45,7 +46,7 @@ func (suite *CorosyncCmapctlTestSuite) TestCorosyncCmapctlGathererNoArgumentProv }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -85,7 +86,7 @@ func (suite *CorosyncCmapctlTestSuite) TestCorosyncCmapctlGathererNonExistingKey }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -120,7 +121,7 @@ func (suite *CorosyncCmapctlTestSuite) TestCorosyncCmapctlCommandNotFound() { Type: "corosync-cmapctl-command-error", } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) suite.EqualError(err, expectedError.Error()) @@ -157,7 +158,7 @@ func (suite *CorosyncCmapctlTestSuite) TestCorosyncCmapctlGatherer() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { diff --git a/internal/factsengine/gatherers/corosyncconf.go b/internal/factsengine/gatherers/corosyncconf.go index 1931247a..f790e4c1 100644 --- a/internal/factsengine/gatherers/corosyncconf.go +++ b/internal/factsengine/gatherers/corosyncconf.go @@ -2,6 +2,7 @@ package gatherers import ( "bufio" + "context" "fmt" "os" "regexp" @@ -49,7 +50,7 @@ func NewCorosyncConfGatherer(configFile string) *CorosyncConfGatherer { } } -func (s *CorosyncConfGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s *CorosyncConfGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting corosync.conf file facts gathering process") diff --git a/internal/factsengine/gatherers/corosyncconf_test.go b/internal/factsengine/gatherers/corosyncconf_test.go index 9567252d..3eb1b81f 100644 --- a/internal/factsengine/gatherers/corosyncconf_test.go +++ b/internal/factsengine/gatherers/corosyncconf_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "testing" "github.com/stretchr/testify/suite" @@ -58,7 +59,7 @@ func (suite *CorosyncConfTestSuite) TestCorosyncConfBasic() { }, } - factsGathered, err := c.Gather(factsRequest) + factsGathered, err := c.Gather(context.Background(), factsRequest) expectedResults := []entities.Fact{ { @@ -134,7 +135,7 @@ func (suite *CorosyncConfTestSuite) TestCorosyncConfOneNode() { }, } - factsGathered, err := c.Gather(factsRequest) + factsGathered, err := c.Gather(context.Background(), factsRequest) expectedResults := []entities.Fact{ @@ -166,7 +167,7 @@ func (suite *CorosyncConfTestSuite) TestCorosyncConfThreeNodes() { }, } - factsGathered, err := c.Gather(factsRequest) + factsGathered, err := c.Gather(context.Background(), factsRequest) expectedResults := []entities.Fact{ @@ -208,7 +209,7 @@ func (suite *CorosyncConfTestSuite) TestCorosyncConfFileNotExists() { }, } - factsGathered, err := c.Gather(factsRequest) + factsGathered, err := c.Gather(context.Background(), factsRequest) expectedError := &entities.FactGatheringError{ Message: "error reading corosync.conf file: could not open corosync.conf file: " + @@ -231,7 +232,7 @@ func (suite *CorosyncConfTestSuite) TestCorosyncConfInvalid() { }, } - factsGathered, err := c.Gather(factsRequest) + factsGathered, err := c.Gather(context.Background(), factsRequest) expectedError := &entities.FactGatheringError{ Message: "error decoding corosync.conf file: invalid corosync file structure. " + diff --git a/internal/factsengine/gatherers/dir_scan.go b/internal/factsengine/gatherers/dir_scan.go index a30ae91a..25814522 100644 --- a/internal/factsengine/gatherers/dir_scan.go +++ b/internal/factsengine/gatherers/dir_scan.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "encoding/json" "fmt" "strconv" @@ -67,7 +68,7 @@ func NewDefaultDirScanGatherer() *DirScanGatherer { return NewDirScanGatherer(afero.NewOsFs(), &cf, &cf) } -func (d *DirScanGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (d *DirScanGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { log.Infof("Starting %s facts gathering process", DirScanGathererName) facts := []entities.Fact{} diff --git a/internal/factsengine/gatherers/dir_scan_test.go b/internal/factsengine/gatherers/dir_scan_test.go index 60501766..cc533721 100644 --- a/internal/factsengine/gatherers/dir_scan_test.go +++ b/internal/factsengine/gatherers/dir_scan_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "fmt" "testing" @@ -64,7 +65,7 @@ func (s *DirScanGathererSuite) TestDirScanningErrorDirScaningWithoutGlob() { Name: "dir_scan", }} - result, _ := g.Gather(fr) + result, _ := g.Gather(context.Background(), fr) expectedResult := []entities.Fact{{ Name: "dir_scan", CheckID: "check1", @@ -104,7 +105,7 @@ func (s *DirScanGathererSuite) TestDirScanningErrorNoArgument() { }, }} - result, _ := g.Gather(fr) + result, _ := g.Gather(context.Background(), fr) s.EqualValues(expectedResult, result) } @@ -162,7 +163,7 @@ func (s *DirScanGathererSuite) TestDirScanningSuccess() { }, }} - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) s.NoError(err) s.EqualValues(expectedResult, result) } diff --git a/internal/factsengine/gatherers/dispwork.go b/internal/factsengine/gatherers/dispwork.go index cdd73568..97b93851 100644 --- a/internal/factsengine/gatherers/dispwork.go +++ b/internal/factsengine/gatherers/dispwork.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "encoding/json" "fmt" "path/filepath" @@ -66,7 +67,7 @@ func NewDispWorkGatherer(fs afero.Fs, executor utils.CommandExecutor) *DispWorkG } } -func (g *DispWorkGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *DispWorkGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", DispWorkGathererName) diff --git a/internal/factsengine/gatherers/dispwork_test.go b/internal/factsengine/gatherers/dispwork_test.go index 00240927..fe8110a8 100644 --- a/internal/factsengine/gatherers/dispwork_test.go +++ b/internal/factsengine/gatherers/dispwork_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "errors" "io" "os" @@ -103,7 +104,7 @@ func (suite *DispWorkGathererTestSuite) TestDispWorkGatheringSuccess() { }, }} - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) suite.NoError(err) suite.EqualValues(expectedResults, result) } @@ -127,7 +128,7 @@ func (suite *DispWorkGathererTestSuite) TestDispWorkGatheringEmptyFileSystem() { }, }} - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) suite.NoError(err) suite.EqualValues(expectedResults, result) } diff --git a/internal/factsengine/gatherers/fstab.go b/internal/factsengine/gatherers/fstab.go index a752a931..1139f43c 100644 --- a/internal/factsengine/gatherers/fstab.go +++ b/internal/factsengine/gatherers/fstab.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "encoding/json" "strings" @@ -48,7 +49,7 @@ func NewDefaultFstabGatherer() *FstabGatherer { return &FstabGatherer{fstabFilePath: FstabFilePath} } -func (f *FstabGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (f *FstabGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { log.Infof("Starting %s facts gathering process", FstabGathererName) facts := []entities.Fact{} diff --git a/internal/factsengine/gatherers/fstab_test.go b/internal/factsengine/gatherers/fstab_test.go index 77c50b6c..da820669 100644 --- a/internal/factsengine/gatherers/fstab_test.go +++ b/internal/factsengine/gatherers/fstab_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "testing" "github.com/stretchr/testify/suite" @@ -28,7 +29,7 @@ func (s *FstabGathererTestSuite) TestFstabGatheringErrorInvalidFstab() { }, } - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) s.Nil(result) s.EqualError(err, "fact gathering error: fstab-file-error - error reading /etc/fstab file: Syntax error at line 4: ao is not a number") } @@ -44,7 +45,7 @@ func (s *FstabGathererTestSuite) TestFstabGatheringErrorFstabFileNotFound() { }, } - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) s.Nil(result) s.EqualError(err, "fact gathering error: fstab-file-error - error reading /etc/fstab file: open not found: no such file or directory") } @@ -153,7 +154,7 @@ func (s *FstabGathererTestSuite) TestFstabGatheringSuccess() { }, }} - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) s.NoError(err) s.EqualValues(expectedResults, result) } diff --git a/internal/factsengine/gatherers/gatherer.go b/internal/factsengine/gatherers/gatherer.go index 8c848c02..d27e0a8d 100644 --- a/internal/factsengine/gatherers/gatherer.go +++ b/internal/factsengine/gatherers/gatherer.go @@ -1,6 +1,8 @@ package gatherers import ( + "context" + "github.com/trento-project/agent/internal/factsengine/factscache" "github.com/trento-project/agent/pkg/factsengine/entities" ) @@ -12,7 +14,7 @@ var ImplementationError = entities.FactGatheringError{ } type FactGatherer interface { - Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) + Gather(context context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) } type FactGathererWithCache interface { diff --git a/internal/factsengine/gatherers/groups.go b/internal/factsengine/gatherers/groups.go index 3ecbaedb..76211fcc 100644 --- a/internal/factsengine/gatherers/groups.go +++ b/internal/factsengine/gatherers/groups.go @@ -2,6 +2,7 @@ package gatherers import ( "bufio" + "context" "encoding/json" "fmt" "io" @@ -49,7 +50,7 @@ func NewGroupsGatherer(groupsFilePath string) *GroupsGatherer { return &GroupsGatherer{groupsFilePath: groupsFilePath} } -func (g *GroupsGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *GroupsGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { log.Infof("Starting %s facts gathering process", GroupsGathererName) facts := []entities.Fact{} diff --git a/internal/factsengine/gatherers/groups_test.go b/internal/factsengine/gatherers/groups_test.go index 74c49ac9..43ed26ae 100644 --- a/internal/factsengine/gatherers/groups_test.go +++ b/internal/factsengine/gatherers/groups_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "testing" "github.com/stretchr/testify/suite" @@ -78,7 +79,7 @@ func (s *GroupsGathererSuite) TestGroupsParsingSuccess() { }, }, } - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) s.NoError(err) s.EqualValues(expectedFacts, results) } @@ -92,7 +93,7 @@ func (s *GroupsGathererSuite) TestGroupsParsingDecodeErrorInvalidGID() { CheckID: "checkone", }} - result, err := gatherer.Gather(fr) + result, err := gatherer.Gather(context.Background(), fr) s.Nil(result) s.EqualError(err, "fact gathering error: groups-decoding-error - error deconding groups file: could not convert group id to integer") } @@ -106,7 +107,7 @@ func (s *GroupsGathererSuite) TestGroupsParsingDecodeErrorInvalidFormat() { CheckID: "checkone", }} - result, err := gatherer.Gather(fr) + result, err := gatherer.Gather(context.Background(), fr) 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") } diff --git a/internal/factsengine/gatherers/hostsfile.go b/internal/factsengine/gatherers/hostsfile.go index e74b5b1f..88232dc2 100644 --- a/internal/factsengine/gatherers/hostsfile.go +++ b/internal/factsengine/gatherers/hostsfile.go @@ -2,6 +2,7 @@ package gatherers import ( "bufio" + "context" "fmt" "os" "regexp" @@ -53,7 +54,7 @@ func NewHostsFileGatherer(hostsFile string) *HostsFileGatherer { return &HostsFileGatherer{hostsFilePath: hostsFile} } -func (s *HostsFileGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s *HostsFileGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting /etc/hosts file facts gathering process") diff --git a/internal/factsengine/gatherers/hostsfile_test.go b/internal/factsengine/gatherers/hostsfile_test.go index f4664203..fb918de1 100644 --- a/internal/factsengine/gatherers/hostsfile_test.go +++ b/internal/factsengine/gatherers/hostsfile_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "testing" "github.com/stretchr/testify/suite" @@ -46,7 +47,7 @@ func (suite *HostsFileTestSuite) TestHostsFileBasic() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -118,7 +119,7 @@ func (suite *HostsFileTestSuite) TestHostsFileNotExists() { }, } - _, err := c.Gather(factRequests) + _, err := c.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: hosts-file-error - error reading /etc/hosts file: "+ "open non_existing_file: no such file or directory") @@ -136,7 +137,7 @@ func (suite *HostsFileTestSuite) TestHostsFileIgnoresCommentedHosts() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { diff --git a/internal/factsengine/gatherers/mocks/FactGatherer.go b/internal/factsengine/gatherers/mocks/FactGatherer.go index 8efcf5da..74c49425 100644 --- a/internal/factsengine/gatherers/mocks/FactGatherer.go +++ b/internal/factsengine/gatherers/mocks/FactGatherer.go @@ -3,6 +3,7 @@ package mocks import ( + "context" entities "github.com/trento-project/agent/pkg/factsengine/entities" mock "github.com/stretchr/testify/mock" @@ -14,7 +15,7 @@ type FactGatherer struct { } // Gather provides a mock function with given fields: factsRequests -func (_m *FactGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (_m *FactGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { ret := _m.Called(factsRequests) var r0 []entities.Fact diff --git a/internal/factsengine/gatherers/mountinfo.go b/internal/factsengine/gatherers/mountinfo.go index 14c21e12..6d319e24 100644 --- a/internal/factsengine/gatherers/mountinfo.go +++ b/internal/factsengine/gatherers/mountinfo.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "encoding/json" "strings" @@ -65,7 +66,7 @@ func NewMountInfoGatherer(mInfo MountParserInterface, executor utils.CommandExec return &MountInfoGatherer{mInfo: mInfo, executor: executor} } -func (g *MountInfoGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *MountInfoGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", MountInfoGathererName) mounts, err := g.mInfo.GetMounts(nil) diff --git a/internal/factsengine/gatherers/mountinfo_test.go b/internal/factsengine/gatherers/mountinfo_test.go index d0454ef3..dea271e3 100644 --- a/internal/factsengine/gatherers/mountinfo_test.go +++ b/internal/factsengine/gatherers/mountinfo_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "fmt" "testing" @@ -101,7 +102,7 @@ TYPE=xfs gatherer := gatherers.NewMountInfoGatherer(suite.mockMountParser, suite.mockExecutor) - factResults, err := gatherer.Gather(requestedFacts) + factResults, err := gatherer.Gather(context.Background(), requestedFacts) expectedResults := []entities.Fact{ { @@ -175,7 +176,7 @@ func (suite *MountInfoTestSuite) TestMountInfoParsingNoArgument() { gatherer := gatherers.NewMountInfoGatherer(suite.mockMountParser, suite.mockExecutor) - factResults, err := gatherer.Gather(requestedFacts) + factResults, err := gatherer.Gather(context.Background(), requestedFacts) expectedResults := []entities.Fact{{ Name: "no_argument", @@ -204,7 +205,7 @@ func (suite *MountInfoTestSuite) TestMountInfoParsingError() { gatherer := gatherers.NewMountInfoGatherer(suite.mockMountParser, suite.mockExecutor) - factResults, err := gatherer.Gather(requestedFacts) + factResults, err := gatherer.Gather(context.Background(), requestedFacts) suite.Empty(factResults) suite.EqualError(err, "fact gathering error: mount-info-parsing-error - "+ diff --git a/internal/factsengine/gatherers/osrelease.go b/internal/factsengine/gatherers/osrelease.go index f6d5dc4f..52d7d957 100644 --- a/internal/factsengine/gatherers/osrelease.go +++ b/internal/factsengine/gatherers/osrelease.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "os" "github.com/hashicorp/go-envparse" @@ -40,7 +41,7 @@ func NewOSReleaseGatherer(path string) *OSReleaseGatherer { } } -func (g *OSReleaseGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *OSReleaseGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", OSReleaseGathererName) diff --git a/internal/factsengine/gatherers/osrelease_test.go b/internal/factsengine/gatherers/osrelease_test.go index c4febc8d..2a7ea838 100644 --- a/internal/factsengine/gatherers/osrelease_test.go +++ b/internal/factsengine/gatherers/osrelease_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "testing" "github.com/stretchr/testify/suite" @@ -28,7 +29,7 @@ func (suite *OSReleaseGathererTestSuite) TestOSReleaseGathererSuccess() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -65,7 +66,7 @@ func (suite *OSReleaseGathererTestSuite) TestOSReleaseGathererFileNotExists() { }, } - _, err := c.Gather(factRequests) + _, err := c.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: os-release-file-error - error reading /etc/os-release file: "+ "open non_existing_file: no such file or directory") @@ -82,7 +83,7 @@ func (suite *OSReleaseGathererTestSuite) TestOSReleaseGathererErrorDecoding() { }, } - _, err := c.Gather(factRequests) + _, err := c.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: os-release-decoding-error - error decoding file content: error on line 3: missing =") } diff --git a/internal/factsengine/gatherers/packageversion.go b/internal/factsengine/gatherers/packageversion.go index 50da5771..26bb73cc 100644 --- a/internal/factsengine/gatherers/packageversion.go +++ b/internal/factsengine/gatherers/packageversion.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "fmt" "sort" "strconv" @@ -56,7 +57,7 @@ func NewPackageVersionGatherer(executor utils.CommandExecutor) *PackageVersionGa } } -func (g *PackageVersionGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *PackageVersionGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", PackageVersionGathererName) diff --git a/internal/factsengine/gatherers/packageversion_test.go b/internal/factsengine/gatherers/packageversion_test.go index e16dfe88..967e9b0f 100644 --- a/internal/factsengine/gatherers/packageversion_test.go +++ b/internal/factsengine/gatherers/packageversion_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "errors" "io" "os" @@ -48,7 +49,7 @@ func (suite *PackageVersionTestSuite) TestPackageVersionGathererNoArgumentProvid }, } - factResults, err := p.Gather(factRequests) + factResults, err := p.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -164,7 +165,7 @@ func (suite *PackageVersionTestSuite) TestPackageVersionGather() { }, } - factResults, err := p.Gather(factRequests) + factResults, err := p.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -302,7 +303,7 @@ func (suite *PackageVersionTestSuite) TestPackageVersionGatherErrors() { }, } - factResults, err := p.Gather(factRequests) + factResults, err := p.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { diff --git a/internal/factsengine/gatherers/passwd.go b/internal/factsengine/gatherers/passwd.go index 0d4e4043..17e66710 100644 --- a/internal/factsengine/gatherers/passwd.go +++ b/internal/factsengine/gatherers/passwd.go @@ -2,6 +2,7 @@ package gatherers import ( "bufio" + "context" "encoding/json" "fmt" "os" @@ -53,7 +54,7 @@ func NewPasswdGatherer(path string) *PasswdGatherer { } } -func (g *PasswdGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *PasswdGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", PasswdGathererName) diff --git a/internal/factsengine/gatherers/passwd_test.go b/internal/factsengine/gatherers/passwd_test.go index 0d15e8de..6d8b5f33 100644 --- a/internal/factsengine/gatherers/passwd_test.go +++ b/internal/factsengine/gatherers/passwd_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "testing" "github.com/stretchr/testify/suite" @@ -28,7 +29,7 @@ func (suite *PasswdTestSuite) TestPasswd() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -85,7 +86,7 @@ func (suite *PasswdTestSuite) TestPasswdFileNotExists() { }, } - _, err := c.Gather(factRequests) + _, err := c.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: passwd-file-error - error reading /etc/passwd file: "+ "open non_existing_file: no such file or directory") @@ -102,7 +103,7 @@ func (suite *PasswdTestSuite) TestPasswdErrorDecoding() { }, } - _, err := c.Gather(factRequests) + _, err := c.Gather(context.Background(), factRequests) 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") diff --git a/internal/factsengine/gatherers/plugin.go b/internal/factsengine/gatherers/plugin.go index 0a0f775f..850e69e3 100644 --- a/internal/factsengine/gatherers/plugin.go +++ b/internal/factsengine/gatherers/plugin.go @@ -45,6 +45,7 @@ func GetGatherersFromPlugins( name := path.Base(filePath) name = strings.TrimSuffix(name, path.Ext(name)) + pluginFactGatherers[name] = map[string]FactGatherer{ defaultPluginVersion: loadedPlugin, } diff --git a/internal/factsengine/gatherers/products.go b/internal/factsengine/gatherers/products.go index 20e3e183..0004566f 100644 --- a/internal/factsengine/gatherers/products.go +++ b/internal/factsengine/gatherers/products.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "path" "github.com/pkg/errors" @@ -56,7 +57,7 @@ func NewDefaultProductsGatherer() *ProductsGatherer { return &ProductsGatherer{fs: afero.NewOsFs(), productsPath: productsDefaultPath} } -func (g *ProductsGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *ProductsGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", ProductsGathererName) diff --git a/internal/factsengine/gatherers/products_test.go b/internal/factsengine/gatherers/products_test.go index 80489074..d9bc9a1a 100644 --- a/internal/factsengine/gatherers/products_test.go +++ b/internal/factsengine/gatherers/products_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "path" "testing" @@ -33,7 +34,7 @@ func (s *ProductsGathererSuite) TestProductsGathererFolderMissingError() { gatherer := gatherers.NewProductsGatherer(fs, testProductsPath) - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) s.Nil(results) s.EqualError(err, "fact gathering error: products-folder-missing-error - "+ "products folder does not exist: /etc/products.d/") @@ -63,7 +64,7 @@ func (s *ProductsGathererSuite) TestProductsGathererReadingError() { gatherer := gatherers.NewProductsGatherer(fs, testProductsPath) - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) s.Nil(results) s.EqualError(err, "fact gathering error: products-file-reading-error - "+ "error reading the products file: baseproduct: could not parse product file: "+ @@ -160,7 +161,7 @@ func (s *ProductsGathererSuite) TestProductsGathererSuccess() { }, } - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) s.NoError(err) s.EqualValues(expectedFacts, results) diff --git a/internal/factsengine/gatherers/sapcontrol.go b/internal/factsengine/gatherers/sapcontrol.go index b0fc2820..3b4795aa 100644 --- a/internal/factsengine/gatherers/sapcontrol.go +++ b/internal/factsengine/gatherers/sapcontrol.go @@ -141,7 +141,7 @@ func memoizeSapcontrol(args ...interface{}) (interface{}, error) { return webmethod(ctx, conn) } -func (s *SapControlGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s *SapControlGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { ctx := context.Background() log.Infof("Starting %s facts gathering process", SapControlGathererName) diff --git a/internal/factsengine/gatherers/sapcontrol_test.go b/internal/factsengine/gatherers/sapcontrol_test.go index 8d95f8e6..d86e5e3d 100644 --- a/internal/factsengine/gatherers/sapcontrol_test.go +++ b/internal/factsengine/gatherers/sapcontrol_test.go @@ -78,7 +78,7 @@ func (suite *SapControlGathererSuite) TestSapControlGathererArgumentErrors() { }, } - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) suite.NoError(err) suite.EqualValues(expectedFacts, results) } @@ -103,7 +103,7 @@ func (suite *SapControlGathererSuite) TestSapControlGathererEmptyFileSystem() { }, } - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) suite.NoError(err) suite.EqualValues(expectedFacts, results) } @@ -210,7 +210,7 @@ func (suite *SapControlGathererSuite) TestSapControlGathererCacheHit() { }, } - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) suite.NoError(err) suite.EqualValues(expectedFacts, results) suite.webService.AssertNumberOfCalls(suite.T(), "New", 1) @@ -344,7 +344,7 @@ func (suite *SapControlGathererSuite) TestSapControlGathererMultipleInstaces() { }, } - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) suite.NoError(err) suite.EqualValues(expectedFacts, results) @@ -423,7 +423,7 @@ func (suite *SapControlGathererSuite) TestSapControlGathererGetSystemInstanceLis }, } - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) suite.NoError(err) suite.EqualValues(expectedFacts, results) @@ -508,7 +508,7 @@ func (suite *SapControlGathererSuite) TestSapControlGathererGetVersionInfo() { }, } - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) suite.NoError(err) suite.EqualValues(expectedFacts, results) } @@ -576,7 +576,7 @@ func (suite *SapControlGathererSuite) TestSapControlGathererHACheckConfig() { }, } - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) suite.NoError(err) suite.EqualValues(expectedFacts, results) } @@ -638,7 +638,7 @@ func (suite *SapControlGathererSuite) TestSapControlGathererHAGetFailoverConfig( }, } - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) suite.NoError(err) suite.EqualValues(expectedFacts, results) } diff --git a/internal/factsengine/gatherers/saphostctrl.go b/internal/factsengine/gatherers/saphostctrl.go index 76cd4ad9..4847bd99 100644 --- a/internal/factsengine/gatherers/saphostctrl.go +++ b/internal/factsengine/gatherers/saphostctrl.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "regexp" "strings" @@ -64,7 +65,7 @@ func NewSapHostCtrlGatherer(executor utils.CommandExecutor) *SapHostCtrlGatherer } } -func (g *SapHostCtrlGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *SapHostCtrlGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting saphostctrl facts gathering process") diff --git a/internal/factsengine/gatherers/saphostctrl_test.go b/internal/factsengine/gatherers/saphostctrl_test.go index 71aa8205..e25b4da2 100644 --- a/internal/factsengine/gatherers/saphostctrl_test.go +++ b/internal/factsengine/gatherers/saphostctrl_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "errors" "testing" @@ -41,7 +42,7 @@ func (suite *SapHostCtrlTestSuite) TestSapHostCtrlGathererNoArgumentProvided() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -83,7 +84,7 @@ func (suite *SapHostCtrlTestSuite) TestSapHostCtrlGatherListInstances() { }, } - factResults, err := p.Gather(factRequests) + factResults, err := p.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -137,7 +138,7 @@ func (suite *SapHostCtrlTestSuite) TestSapHostCtrlGatherPingSuccess() { }, } - factResults, err := p.Gather(factRequests) + factResults, err := p.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -171,7 +172,7 @@ func (suite *SapHostCtrlTestSuite) TestSapHostCtrlGatherPingFailed() { }, } - factResults, err := p.Gather(factRequests) + factResults, err := p.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -219,7 +220,7 @@ func (suite *SapHostCtrlTestSuite) TestSapHostCtrlGatherError() { }, } - factResults, err := p.Gather(factRequests) + factResults, err := p.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { diff --git a/internal/factsengine/gatherers/sapinstancehostnameresolver.go b/internal/factsengine/gatherers/sapinstancehostnameresolver.go index 34c30df4..c53709db 100644 --- a/internal/factsengine/gatherers/sapinstancehostnameresolver.go +++ b/internal/factsengine/gatherers/sapinstancehostnameresolver.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "encoding/json" "net" "path/filepath" @@ -97,7 +98,7 @@ func NewSapInstanceHostnameResolverGatherer( return &SapInstanceHostnameResolverGatherer{fs: fs, hr: hr, hp: hp} } -func (r *SapInstanceHostnameResolverGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (r *SapInstanceHostnameResolverGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} details, err := r.getInstanceHostnameDetails() diff --git a/internal/factsengine/gatherers/sapinstancehostnameresolver_test.go b/internal/factsengine/gatherers/sapinstancehostnameresolver_test.go index c3b35196..99d7253e 100644 --- a/internal/factsengine/gatherers/sapinstancehostnameresolver_test.go +++ b/internal/factsengine/gatherers/sapinstancehostnameresolver_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "errors" "testing" @@ -56,7 +57,7 @@ func (suite *SapInstanceHostnameResolverTestSuite) TestSapInstanceHostnameResolv CheckID: "check1", }} - factResults, err := g.Gather(factRequests) + factResults, err := g.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -131,7 +132,7 @@ func (suite *SapInstanceHostnameResolverTestSuite) TestSapInstanceHostnameResolv CheckID: "check1", }} - factResults, err := g.Gather(factRequests) + factResults, err := g.Gather(context.Background(), factRequests) suite.Nil(factResults) suite.EqualError(err, "fact gathering error: sapinstance-hostname-resolver-details-error - error gathering details: open /sapmnt/QAS/profile: file does not exist") } @@ -181,7 +182,7 @@ func (suite *SapInstanceHostnameResolverTestSuite) TestSapInstanceHostnameResolv }, } - factResults, err := g.Gather(factRequests) + factResults, err := g.Gather(context.Background(), factRequests) suite.NoError(err) suite.Equal(expectedResults, factResults) diff --git a/internal/factsengine/gatherers/sapprofiles.go b/internal/factsengine/gatherers/sapprofiles.go index 380c62e4..89d36a35 100644 --- a/internal/factsengine/gatherers/sapprofiles.go +++ b/internal/factsengine/gatherers/sapprofiles.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "encoding/json" "path" "path/filepath" @@ -53,7 +54,7 @@ func NewSapProfilesGatherer(fs afero.Fs) *SapProfilesGatherer { return &SapProfilesGatherer{fs: fs} } -func (s *SapProfilesGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s *SapProfilesGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { log.Infof("Starting %s facts gathering process", SapProfilesGathererName) facts := []entities.Fact{} systems := make(SapSystemMap) diff --git a/internal/factsengine/gatherers/sapprofiles_test.go b/internal/factsengine/gatherers/sapprofiles_test.go index 0075f8d6..2226b0c8 100644 --- a/internal/factsengine/gatherers/sapprofiles_test.go +++ b/internal/factsengine/gatherers/sapprofiles_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "io" "os" "testing" @@ -330,7 +331,7 @@ func (suite *SapProfilesTestSuite) TestSapProfilesSuccess() { }, } - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) suite.NoError(err) suite.EqualValues(expectedFacts, results) } @@ -370,7 +371,7 @@ func (suite *SapProfilesTestSuite) TestSapProfilesNoProfiles() { }, } - results, err := gatherer.Gather(fr) + results, err := gatherer.Gather(context.Background(), fr) suite.NoError(err) suite.EqualValues(expectedFacts, results) } @@ -392,7 +393,7 @@ func (suite *SapProfilesTestSuite) TestSapProfilesInvalidProfile() { CheckID: "check1", }} - result, err := gatherer.Gather(fr) + result, err := gatherer.Gather(context.Background(), fr) suite.Nil(result) suite.EqualError(err, "fact gathering error: sap-profiles-file-system-error - "+ "error reading the sap profiles file system: could not parse profile file: error "+ diff --git a/internal/factsengine/gatherers/sapservices.go b/internal/factsengine/gatherers/sapservices.go index 709fce48..075e41a4 100644 --- a/internal/factsengine/gatherers/sapservices.go +++ b/internal/factsengine/gatherers/sapservices.go @@ -2,6 +2,7 @@ package gatherers import ( "bufio" + "context" "encoding/json" "fmt" "regexp" @@ -82,7 +83,7 @@ func NewDefaultSapServicesGatherer() *SapServices { return &SapServices{servicesFilePath: sapServicesDefaultPath, fs: afero.NewOsFs()} } -func (s *SapServices) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s *SapServices) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", SapServicesGathererName) diff --git a/internal/factsengine/gatherers/sapservices_test.go b/internal/factsengine/gatherers/sapservices_test.go index 1dcc4139..d38e58f9 100644 --- a/internal/factsengine/gatherers/sapservices_test.go +++ b/internal/factsengine/gatherers/sapservices_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "testing" "github.com/spf13/afero" @@ -29,7 +30,7 @@ func (s *SapServicesGathererSuite) TestSapServicesGathererFileNotFound() { }, } - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) s.Nil(result) s.EqualError(err, "fact gathering error: sap-services-reading-error - error reading the sapservices file: open /usr/sap/sapservices: file does not exist") } @@ -52,7 +53,7 @@ systemctl --no-ask-password start SAPS41_1 } g := gatherers.NewSapServicesGatherer("/usr/sap/sapservices", tFs) - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) s.Nil(result) s.EqualError(err, "fact gathering error: sap-services-parsing-error - error parsing the sapservices file: could not extract values from systemd SAP services entry: systemctl --no-ask-password start SAPS41_0 ") @@ -75,7 +76,7 @@ systemctl --no-ask-password start SADS41_41 } g := gatherers.NewSapServicesGatherer("/usr/sap/sapservices", tFs) - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) s.Nil(result) s.EqualError(err, "fact gathering error: sap-services-parsing-error - error parsing the sapservices file: could not extract values from systemd SAP services entry: systemctl --no-ask-password start SADS41_41") @@ -100,7 +101,7 @@ LD_LIBRARY_PATH=/usr/sap/S41/D40/exe:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH; / } g := gatherers.NewSapServicesGatherer("/usr/sap/sapservices", tFs) - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) s.Nil(result) s.EqualError(err, "fact gathering error: sap-services-parsing-error - error parsing the sapservices file: could not extract values from sapstartsrv SAP services entry: LD_LIBRARY_PATH=/usr/sap/HS1/HDB11/exe:$LD_LIBRARY_PATH;export LD_LIBRARY_PATH;/usr/sap/HS1/HDB11/exe/sapstartsrv pf=/usr/sap/HS1/SYS/profile/HS1HDB11_s41db -D -u hs1adm") } @@ -124,7 +125,7 @@ LD_LIBRARY_PATH=/usr/sap/S41/D40/exe:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH; / } g := gatherers.NewSapServicesGatherer("/usr/sap/sapservices", tFs) - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) s.Nil(result) s.EqualError(err, "fact gathering error: sap-services-parsing-error - error parsing the sapservices file: could not extract values from sapstartsrv SAP services entry: LD_LIBRARY_PATH=/usr/sap/HS1/HDB11/exe:$LD_LIBRARY_PATH;export LD_LIBRARY_PATH;/usr/sap/HS1/HDB11/exe/sapstartsrv pf=/usr/sap/HS1/SYS/profile/HS1_HDB1_s41db -D -u hs1adm") } @@ -173,7 +174,7 @@ LD_LIBRARY_PATH=/usr/sap/S41/ASCS41/exe:$LD_LIBRARY_PATH; export LD_LIBRARY_PATH }, } g := gatherers.NewSapServicesGatherer("/usr/sap/sapservices", tFs) - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) s.NoError(err) s.EqualValues(expectedFacts, result) } @@ -222,7 +223,7 @@ systemctl --no-ask-password start SAPS42_41 }, } g := gatherers.NewSapServicesGatherer("/usr/sap/sapservices", tFs) - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) s.NoError(err) s.EqualValues(expectedFacts, result) } @@ -272,7 +273,7 @@ systemctl --no-ask-password start SAPS42_41 }, } g := gatherers.NewSapServicesGatherer("/usr/sap/sapservices", tFs) - result, err := g.Gather(fr) + result, err := g.Gather(context.Background(), fr) s.NoError(err) s.EqualValues(expectedFacts, result) } diff --git a/internal/factsengine/gatherers/saptune.go b/internal/factsengine/gatherers/saptune.go index 5951f6f2..0c88d96e 100644 --- a/internal/factsengine/gatherers/saptune.go +++ b/internal/factsengine/gatherers/saptune.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "encoding/json" log "github.com/sirupsen/logrus" @@ -64,7 +65,7 @@ func NewSaptuneGatherer(executor utils.CommandExecutor) *SaptuneGatherer { } } -func (s *SaptuneGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s *SaptuneGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { cachedFacts := make(map[string]entities.Fact) facts := []entities.Fact{} diff --git a/internal/factsengine/gatherers/saptune_test.go b/internal/factsengine/gatherers/saptune_test.go index 914785b2..303d2d94 100644 --- a/internal/factsengine/gatherers/saptune_test.go +++ b/internal/factsengine/gatherers/saptune_test.go @@ -2,6 +2,7 @@ package gatherers_test import ( + "context" "errors" "io" "os" @@ -44,7 +45,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererStatus() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -141,7 +142,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererNoteVerify() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -263,7 +264,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererSolutionVerify() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -373,7 +374,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererSolutionList() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -459,7 +460,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererNoteList() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -559,7 +560,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererNoArgumentProvided() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -598,7 +599,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererUnsupportedArgument() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -629,7 +630,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererVersionUnsupported() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{} @@ -651,7 +652,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererNotInstalled() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{} @@ -676,7 +677,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererCommandError() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -713,7 +714,7 @@ func (suite *SaptuneTestSuite) TestSaptuneGathererCommandCaching() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { diff --git a/internal/factsengine/gatherers/sbd.go b/internal/factsengine/gatherers/sbd.go index 5c0575ab..2987dce4 100644 --- a/internal/factsengine/gatherers/sbd.go +++ b/internal/factsengine/gatherers/sbd.go @@ -1,6 +1,8 @@ package gatherers import ( + "context" + log "github.com/sirupsen/logrus" "github.com/trento-project/agent/internal/core/cluster" "github.com/trento-project/agent/pkg/factsengine/entities" @@ -42,7 +44,7 @@ func NewSBDGatherer(configFile string) *SBDGatherer { } } -func (g *SBDGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *SBDGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting SBD config Facts gathering") diff --git a/internal/factsengine/gatherers/sbd_test.go b/internal/factsengine/gatherers/sbd_test.go index 09870039..7daaf2e9 100644 --- a/internal/factsengine/gatherers/sbd_test.go +++ b/internal/factsengine/gatherers/sbd_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "testing" "github.com/stretchr/testify/suite" @@ -32,7 +33,7 @@ func (suite *SBDGathererTestSuite) TestConfigFileNoArgumentProvided() { gatherer := gatherers.NewSBDGatherer(helpers.GetFixturePath("discovery/cluster/sbd/sbd_config")) - gatheredFacts, err := gatherer.Gather(requestedFacts) + gatheredFacts, err := gatherer.Gather(context.Background(), requestedFacts) expectedFacts := []entities.Fact{ { @@ -62,7 +63,7 @@ func (suite *SBDGathererTestSuite) TestConfigFileCouldNotBeRead() { gatherer := gatherers.NewSBDGatherer("/path/to/some-non-existent-sbd-config") - gatheredFacts, err := gatherer.Gather(requestedFacts) + gatheredFacts, err := gatherer.Gather(context.Background(), requestedFacts) expectedError := entities.FactGatheringError{ Type: "sbd-config-file-error", @@ -77,7 +78,7 @@ func (suite *SBDGathererTestSuite) TestConfigFileCouldNotBeRead() { func (suite *SBDGathererTestSuite) TestInvalidConfigFile() { gatherer := gatherers.NewSBDGatherer(helpers.GetFixturePath("discovery/cluster/sbd/sbd_config_invalid")) - gatheredFacts, err := gatherer.Gather([]entities.FactRequest{}) + gatheredFacts, err := gatherer.Gather(context.Background(), []entities.FactRequest{}) expectedError := &entities.FactGatheringError{ Type: "sbd-config-file-error", @@ -119,7 +120,7 @@ func (suite *SBDGathererTestSuite) TestSBDGatherer() { gatherer := gatherers.NewSBDGatherer(helpers.GetFixturePath("discovery/cluster/sbd/sbd_config")) - gatheredFacts, err := gatherer.Gather(requestedFacts) + gatheredFacts, err := gatherer.Gather(context.Background(), requestedFacts) expectedFacts := []entities.Fact{ { diff --git a/internal/factsengine/gatherers/sbddump.go b/internal/factsengine/gatherers/sbddump.go index ef4957a8..41157bb6 100644 --- a/internal/factsengine/gatherers/sbddump.go +++ b/internal/factsengine/gatherers/sbddump.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "errors" "fmt" "regexp" @@ -47,7 +48,7 @@ func NewSBDDumpGatherer(executor utils.CommandExecutor, sbdConfigFile string) *S } } -func (gatherer *SBDDumpGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (gatherer *SBDDumpGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", SBDDumpGathererName) diff --git a/internal/factsengine/gatherers/sbddump_test.go b/internal/factsengine/gatherers/sbddump_test.go index c0f24166..997cc8e9 100644 --- a/internal/factsengine/gatherers/sbddump_test.go +++ b/internal/factsengine/gatherers/sbddump_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "errors" "io" "os" @@ -35,7 +36,7 @@ func (suite *SBDDumpTestSuite) TestSBDDumpUnableToLoadDevices() { }, } - gatheredFacts, err := sbdDumpGatherer.Gather(factRequests) + gatheredFacts, err := sbdDumpGatherer.Gather(context.Background(), factRequests) expectedError := &entities.FactGatheringError{ Message: "error loading the configured sbd devices: could not parse sbd config file: error on line 1: missing =", @@ -70,7 +71,7 @@ func (suite *SBDDumpTestSuite) TestSBDDumpUnableToDumpDevice() { }, } - gatheredFacts, err := sbdDumpGatherer.Gather(factRequests) + gatheredFacts, err := sbdDumpGatherer.Gather(context.Background(), factRequests) expectedFacts := []entities.Fact{ { @@ -124,7 +125,7 @@ func (suite *SBDDumpTestSuite) TestSBDDumpGatherer() { }, } - factResults, err := sbdDumpGatherer.Gather(factRequests) + factResults, err := sbdDumpGatherer.Gather(context.Background(), factRequests) deviceVDBDump := &entities.FactValueMap{Value: map[string]entities.FactValue{ "header_version": &entities.FactValueFloat{Value: 2.1}, diff --git a/internal/factsengine/gatherers/sysctl.go b/internal/factsengine/gatherers/sysctl.go index 84808cfa..aea5d478 100644 --- a/internal/factsengine/gatherers/sysctl.go +++ b/internal/factsengine/gatherers/sysctl.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "strings" log "github.com/sirupsen/logrus" @@ -44,7 +45,7 @@ func NewSysctlGatherer(executor utils.CommandExecutor) *SysctlGatherer { } } -func (s *SysctlGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s *SysctlGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", SysctlGathererName) diff --git a/internal/factsengine/gatherers/sysctl_test.go b/internal/factsengine/gatherers/sysctl_test.go index 9a0d5fd2..26814359 100644 --- a/internal/factsengine/gatherers/sysctl_test.go +++ b/internal/factsengine/gatherers/sysctl_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "io" "os" "os/exec" @@ -45,7 +46,7 @@ func (suite *SysctlTestSuite) TestSysctlGathererNoArgumentProvided() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -85,7 +86,7 @@ func (suite *SysctlTestSuite) TestSysctlGathererNonExistingKey() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -120,7 +121,7 @@ func (suite *SysctlTestSuite) TestSysctlCommandNotFound() { Type: "sysctl-cmd-error", } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) suite.EqualError(err, expectedError.Error()) @@ -142,7 +143,7 @@ func (suite *SysctlTestSuite) TestSysctlGatherer() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -170,7 +171,7 @@ func (suite *SysctlTestSuite) TestSysctlGathererPartialKey() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -203,7 +204,7 @@ func (suite *SysctlTestSuite) TestSysctlGathererEmptyValue() { }, } - factResults, err := c.Gather(factRequests) + factResults, err := c.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { diff --git a/internal/factsengine/gatherers/systemd.go b/internal/factsengine/gatherers/systemd.go index a88a0f5f..3fd36912 100644 --- a/internal/factsengine/gatherers/systemd.go +++ b/internal/factsengine/gatherers/systemd.go @@ -63,7 +63,7 @@ func NewSystemDGatherer(conn DbusConnector, initialized bool) *SystemDGatherer { } } -func (g *SystemDGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *SystemDGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", SystemDGathererName) diff --git a/internal/factsengine/gatherers/systemd_test.go b/internal/factsengine/gatherers/systemd_test.go index 03ae2a32..9a8d0328 100644 --- a/internal/factsengine/gatherers/systemd_test.go +++ b/internal/factsengine/gatherers/systemd_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "errors" "testing" @@ -43,7 +44,7 @@ func (suite *SystemDTestSuite) TestSystemDNoArgumentProvided() { }, } - factResults, err := s.Gather(factRequests) + factResults, err := s.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -104,7 +105,7 @@ func (suite *SystemDTestSuite) TestSystemDGather() { }, } - factResults, err := s.Gather(factRequests) + factResults, err := s.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -143,7 +144,7 @@ func (suite *SystemDTestSuite) TestSystemDGatherNotInitialized() { }, } - _, err := s.Gather(factRequests) + _, err := s.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: systemd-dbus-not-initialized - "+ "systemd gatherer not initialized properly") @@ -172,7 +173,7 @@ func (suite *SystemDTestSuite) TestSystemDGatherError() { }, } - _, err := s.Gather(factRequests) + _, err := s.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: systemd-list-units-error - "+ "error getting unit states: error listing") diff --git a/internal/factsengine/gatherers/systemd_v2.go b/internal/factsengine/gatherers/systemd_v2.go index 5fb6aa5e..f5f62daa 100644 --- a/internal/factsengine/gatherers/systemd_v2.go +++ b/internal/factsengine/gatherers/systemd_v2.go @@ -59,7 +59,7 @@ func NewSystemDGathererV2(conn DbusConnector, initialized bool) *SystemDGatherer } } -func (g *SystemDGathererV2) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *SystemDGathererV2) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s v2 facts gathering process", SystemDGathererName) diff --git a/internal/factsengine/gatherers/systemd_v2_test.go b/internal/factsengine/gatherers/systemd_v2_test.go index 86267fe4..c96f88fc 100644 --- a/internal/factsengine/gatherers/systemd_v2_test.go +++ b/internal/factsengine/gatherers/systemd_v2_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "errors" "testing" @@ -39,7 +40,7 @@ func (suite *SystemDTestSuite) TestSystemDV2NoArgumentProvided() { }, } - factResults, err := s.Gather(factRequests) + factResults, err := s.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -112,7 +113,7 @@ func (suite *SystemDTestSuite) TestSystemDV2Gather() { }, } - factResults, err := s.Gather(factRequests) + factResults, err := s.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -167,7 +168,7 @@ func (suite *SystemDTestSuite) TestSystemDV2GatherNotInitialized() { }, } - _, err := s.Gather(factRequests) + _, err := s.Gather(context.Background(), factRequests) suite.EqualError(err, "fact gathering error: systemd-dbus-not-initialized - "+ "systemd gatherer not initialized properly") @@ -190,7 +191,7 @@ func (suite *SystemDTestSuite) TestSystemDV2GatherError() { }, } - factResults, err := s.Gather(factRequests) + factResults, err := s.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { diff --git a/internal/factsengine/gatherers/verifypassword.go b/internal/factsengine/gatherers/verifypassword.go index 628256ee..9dfe7c37 100644 --- a/internal/factsengine/gatherers/verifypassword.go +++ b/internal/factsengine/gatherers/verifypassword.go @@ -1,6 +1,7 @@ package gatherers import ( + "context" "fmt" "strings" @@ -68,7 +69,7 @@ func NewVerifyPasswordGatherer(executor utils.CommandExecutor) *VerifyPasswordGa /* This gatherer expects only the username for which the password will be verified */ -func (g *VerifyPasswordGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *VerifyPasswordGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting password verifying facts gathering process") diff --git a/internal/factsengine/gatherers/verifypassword_test.go b/internal/factsengine/gatherers/verifypassword_test.go index 211b35e0..6b9f2329 100644 --- a/internal/factsengine/gatherers/verifypassword_test.go +++ b/internal/factsengine/gatherers/verifypassword_test.go @@ -1,6 +1,7 @@ package gatherers_test import ( + "context" "testing" "github.com/stretchr/testify/suite" @@ -41,7 +42,7 @@ func (suite *PasswordTestSuite) TestPasswordGatherEqual() { }, } - factResults, err := verifyPasswordGatherer.Gather(factRequests) + factResults, err := verifyPasswordGatherer.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -74,7 +75,7 @@ func (suite *PasswordTestSuite) TestPasswordGatherNotEqual() { }, } - factResults, err := verifyPasswordGatherer.Gather(factRequests) + factResults, err := verifyPasswordGatherer.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -122,7 +123,7 @@ func (suite *PasswordTestSuite) TestPasswordGatherBloquedPassword() { }, } - factResults, err := verifyPasswordGatherer.Gather(factRequests) + factResults, err := verifyPasswordGatherer.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -175,7 +176,7 @@ func (suite *PasswordTestSuite) TestPasswordGatherNoPassword() { }, } - factResults, err := verifyPasswordGatherer.Gather(factRequests) + factResults, err := verifyPasswordGatherer.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -212,7 +213,7 @@ func (suite *PasswordTestSuite) TestPasswordGatherDifferentEncType() { }, } - factResults, err := verifyPasswordGatherer.Gather(factRequests) + factResults, err := verifyPasswordGatherer.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -247,7 +248,7 @@ func (suite *PasswordTestSuite) TestPasswordGatherInvalidShadowOutput() { }, } - factResults, err := verifyPasswordGatherer.Gather(factRequests) + factResults, err := verifyPasswordGatherer.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { @@ -277,7 +278,7 @@ func (suite *PasswordTestSuite) TestPasswordGatherWrongArguments() { }, } - factResults, err := verifyPasswordGatherer.Gather(factRequests) + factResults, err := verifyPasswordGatherer.Gather(context.Background(), factRequests) expectedResults := []entities.Fact{ { diff --git a/internal/factsengine/gathering.go b/internal/factsengine/gathering.go index db784eef..ed6d3a3e 100644 --- a/internal/factsengine/gathering.go +++ b/internal/factsengine/gathering.go @@ -1,6 +1,8 @@ package factsengine import ( + "context" + "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/trento-project/agent/internal/factsengine/factscache" @@ -24,7 +26,7 @@ func gatherFacts( } groupedFactsRequest := groupFactsRequestByGatherer(agentFacts) factsCh := make(chan []entities.Fact, len(groupedFactsRequest.FactRequests)) - g := new(errgroup.Group) + g, groupCtx := errgroup.WithContext(context.Background()) cache := factscache.NewFactsCache() log.Infof("Starting facts gathering process") @@ -48,7 +50,7 @@ func gatherFacts( g.Go(func() error { var gatheringError *entities.FactGatheringError - newFacts, err := gatherer.Gather(factsRequest) + newFacts, err := gatherer.Gather(groupCtx, factsRequest) switch { case err == nil: factsCh <- newFacts diff --git a/pkg/factsengine/plugininterface/interface.go b/pkg/factsengine/plugininterface/interface.go index 4cb3ff11..4b2dafdc 100644 --- a/pkg/factsengine/plugininterface/interface.go +++ b/pkg/factsengine/plugininterface/interface.go @@ -1,6 +1,7 @@ package plugininterface import ( + "context" "encoding/gob" "net/rpc" @@ -21,7 +22,7 @@ func init() { // Gatherer is the interface exposed as a plugin. type Gatherer interface { - Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) + Gather(ctx context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) } // This is the implementation of plugin.Plugin diff --git a/pkg/factsengine/plugininterface/rpc.go b/pkg/factsengine/plugininterface/rpc.go index 2852469d..8a93a0dc 100644 --- a/pkg/factsengine/plugininterface/rpc.go +++ b/pkg/factsengine/plugininterface/rpc.go @@ -1,6 +1,7 @@ package plugininterface import ( + "context" "net/rpc" "github.com/trento-project/agent/pkg/factsengine/entities" @@ -8,7 +9,7 @@ import ( type GathererRPC struct{ client *rpc.Client } -func (g *GathererRPC) Gather(factsRequest []entities.FactRequest) ([]entities.Fact, error) { +func (g *GathererRPC) Gather(_ context.Context, factsRequest []entities.FactRequest) ([]entities.Fact, error) { var resp []entities.Fact err := g.client.Call("Plugin.Gather", factsRequest, &resp) @@ -20,8 +21,8 @@ type GathererRPCServer struct { Impl Gatherer } -func (s *GathererRPCServer) Gather(args []entities.FactRequest, resp *[]entities.Fact) error { +func (s *GathererRPCServer) Gather(ctx context.Context, args []entities.FactRequest, resp *[]entities.Fact) error { var err error - *resp, err = s.Impl.Gather(args) + *resp, err = s.Impl.Gather(ctx, args) return err } diff --git a/plugin_examples/dummy.go b/plugin_examples/dummy.go index 938b0cc0..dc5920e5 100644 --- a/plugin_examples/dummy.go +++ b/plugin_examples/dummy.go @@ -3,6 +3,7 @@ package main // go build -o /usr/etc/trento/dummy ./plugin_examples/dummy.go import ( + "context" "fmt" "math/rand" @@ -15,7 +16,7 @@ import ( type dummyGatherer struct { } -func (s dummyGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s dummyGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting dummy plugin facts gathering process") From 519608513912fe50e7f4fad023237fb805335947 Mon Sep 17 00:00:00 2001 From: balanza Date: Tue, 31 Dec 2024 09:53:24 +0100 Subject: [PATCH 02/12] make plugins work --- internal/factsengine/gatherers/rcp_plugin.go | 16 +++++++++++++++- pkg/factsengine/plugininterface/interface.go | 3 +-- pkg/factsengine/plugininterface/rpc.go | 7 +++---- plugin_examples/dummy.go | 3 +-- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/internal/factsengine/gatherers/rcp_plugin.go b/internal/factsengine/gatherers/rcp_plugin.go index a4e9af4e..59680efe 100644 --- a/internal/factsengine/gatherers/rcp_plugin.go +++ b/internal/factsengine/gatherers/rcp_plugin.go @@ -1,12 +1,14 @@ package gatherers import ( + "context" "os/exec" "github.com/hashicorp/go-hclog" "github.com/pkg/errors" goplugin "github.com/hashicorp/go-plugin" + "github.com/trento-project/agent/pkg/factsengine/entities" "github.com/trento-project/agent/pkg/factsengine/plugininterface" ) @@ -50,5 +52,17 @@ func (l *RPCPluginLoader) Load(pluginPath string) (FactGatherer, error) { return nil, errors.Wrap(err, "Error asserting Gatherer type") } - return g, nil + p := &PluggedGatherer{ + plugin: g, + } + + return p, nil +} + +type PluggedGatherer struct { + plugin plugininterface.Gatherer +} + +func (g *PluggedGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { + return g.plugin.Gather(factsRequests) } diff --git a/pkg/factsengine/plugininterface/interface.go b/pkg/factsengine/plugininterface/interface.go index 4b2dafdc..4cb3ff11 100644 --- a/pkg/factsengine/plugininterface/interface.go +++ b/pkg/factsengine/plugininterface/interface.go @@ -1,7 +1,6 @@ package plugininterface import ( - "context" "encoding/gob" "net/rpc" @@ -22,7 +21,7 @@ func init() { // Gatherer is the interface exposed as a plugin. type Gatherer interface { - Gather(ctx context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) + Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) } // This is the implementation of plugin.Plugin diff --git a/pkg/factsengine/plugininterface/rpc.go b/pkg/factsengine/plugininterface/rpc.go index 8a93a0dc..2852469d 100644 --- a/pkg/factsengine/plugininterface/rpc.go +++ b/pkg/factsengine/plugininterface/rpc.go @@ -1,7 +1,6 @@ package plugininterface import ( - "context" "net/rpc" "github.com/trento-project/agent/pkg/factsengine/entities" @@ -9,7 +8,7 @@ import ( type GathererRPC struct{ client *rpc.Client } -func (g *GathererRPC) Gather(_ context.Context, factsRequest []entities.FactRequest) ([]entities.Fact, error) { +func (g *GathererRPC) Gather(factsRequest []entities.FactRequest) ([]entities.Fact, error) { var resp []entities.Fact err := g.client.Call("Plugin.Gather", factsRequest, &resp) @@ -21,8 +20,8 @@ type GathererRPCServer struct { Impl Gatherer } -func (s *GathererRPCServer) Gather(ctx context.Context, args []entities.FactRequest, resp *[]entities.Fact) error { +func (s *GathererRPCServer) Gather(args []entities.FactRequest, resp *[]entities.Fact) error { var err error - *resp, err = s.Impl.Gather(ctx, args) + *resp, err = s.Impl.Gather(args) return err } diff --git a/plugin_examples/dummy.go b/plugin_examples/dummy.go index dc5920e5..938b0cc0 100644 --- a/plugin_examples/dummy.go +++ b/plugin_examples/dummy.go @@ -3,7 +3,6 @@ package main // go build -o /usr/etc/trento/dummy ./plugin_examples/dummy.go import ( - "context" "fmt" "math/rand" @@ -16,7 +15,7 @@ import ( type dummyGatherer struct { } -func (s dummyGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s dummyGatherer) Gather(factsRequests []entities.FactRequest) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting dummy plugin facts gathering process") From 266d5e8a9bcc5dc81fe31d035122a6903774af20 Mon Sep 17 00:00:00 2001 From: balanza Date: Tue, 31 Dec 2024 10:36:41 +0100 Subject: [PATCH 03/12] fix lint --- internal/factsengine/gatherers/ascsers_cluster.go | 5 ++++- internal/factsengine/gatherers/corosynccmapctl.go | 5 ++++- internal/factsengine/gatherers/corosyncconf.go | 5 ++++- internal/factsengine/gatherers/packageversion.go | 5 ++++- .../factsengine/gatherers/sapinstancehostnameresolver.go | 5 ++++- internal/factsengine/gatherers/sbddump.go | 5 ++++- internal/factsengine/gatherers/verifypassword.go | 5 ++++- 7 files changed, 28 insertions(+), 7 deletions(-) diff --git a/internal/factsengine/gatherers/ascsers_cluster.go b/internal/factsengine/gatherers/ascsers_cluster.go index 88424fb0..1964d26c 100644 --- a/internal/factsengine/gatherers/ascsers_cluster.go +++ b/internal/factsengine/gatherers/ascsers_cluster.go @@ -74,7 +74,10 @@ func (g *AscsErsClusterGatherer) SetCache(cache *factscache.FactsCache) { g.cache = cache } -func (g *AscsErsClusterGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *AscsErsClusterGatherer) Gather( + _ context.Context, + factsRequests []entities.FactRequest, +) ([]entities.Fact, error) { log.Infof("Starting %s facts gathering process", AscsErsClusterGathererName) var cibdata cib.Root diff --git a/internal/factsengine/gatherers/corosynccmapctl.go b/internal/factsengine/gatherers/corosynccmapctl.go index 280488e3..eaca3037 100644 --- a/internal/factsengine/gatherers/corosynccmapctl.go +++ b/internal/factsengine/gatherers/corosynccmapctl.go @@ -81,7 +81,10 @@ func corosyncCmapctlOutputToMap(corosyncCmapctlOutput string) *entities.FactValu return outputMap } -func (s *CorosyncCmapctlGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s *CorosyncCmapctlGatherer) Gather( + _ context.Context, + factsRequests []entities.FactRequest, +) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", CorosyncCmapCtlGathererName) diff --git a/internal/factsengine/gatherers/corosyncconf.go b/internal/factsengine/gatherers/corosyncconf.go index f790e4c1..64d1f928 100644 --- a/internal/factsengine/gatherers/corosyncconf.go +++ b/internal/factsengine/gatherers/corosyncconf.go @@ -50,7 +50,10 @@ func NewCorosyncConfGatherer(configFile string) *CorosyncConfGatherer { } } -func (s *CorosyncConfGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (s *CorosyncConfGatherer) Gather( + _ context.Context, + factsRequests []entities.FactRequest, +) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting corosync.conf file facts gathering process") diff --git a/internal/factsengine/gatherers/packageversion.go b/internal/factsengine/gatherers/packageversion.go index 26bb73cc..4d7397e1 100644 --- a/internal/factsengine/gatherers/packageversion.go +++ b/internal/factsengine/gatherers/packageversion.go @@ -57,7 +57,10 @@ func NewPackageVersionGatherer(executor utils.CommandExecutor) *PackageVersionGa } } -func (g *PackageVersionGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *PackageVersionGatherer) Gather( + _ context.Context, + factsRequests []entities.FactRequest, +) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", PackageVersionGathererName) diff --git a/internal/factsengine/gatherers/sapinstancehostnameresolver.go b/internal/factsengine/gatherers/sapinstancehostnameresolver.go index c53709db..437933ec 100644 --- a/internal/factsengine/gatherers/sapinstancehostnameresolver.go +++ b/internal/factsengine/gatherers/sapinstancehostnameresolver.go @@ -98,7 +98,10 @@ func NewSapInstanceHostnameResolverGatherer( return &SapInstanceHostnameResolverGatherer{fs: fs, hr: hr, hp: hp} } -func (r *SapInstanceHostnameResolverGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (r *SapInstanceHostnameResolverGatherer) Gather( + _ context.Context, + factsRequests []entities.FactRequest, +) ([]entities.Fact, error) { facts := []entities.Fact{} details, err := r.getInstanceHostnameDetails() diff --git a/internal/factsengine/gatherers/sbddump.go b/internal/factsengine/gatherers/sbddump.go index 41157bb6..eff97afd 100644 --- a/internal/factsengine/gatherers/sbddump.go +++ b/internal/factsengine/gatherers/sbddump.go @@ -48,7 +48,10 @@ func NewSBDDumpGatherer(executor utils.CommandExecutor, sbdConfigFile string) *S } } -func (gatherer *SBDDumpGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (gatherer *SBDDumpGatherer) Gather( + _ context.Context, + factsRequests []entities.FactRequest, +) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting %s facts gathering process", SBDDumpGathererName) diff --git a/internal/factsengine/gatherers/verifypassword.go b/internal/factsengine/gatherers/verifypassword.go index 9dfe7c37..04de4337 100644 --- a/internal/factsengine/gatherers/verifypassword.go +++ b/internal/factsengine/gatherers/verifypassword.go @@ -69,7 +69,10 @@ func NewVerifyPasswordGatherer(executor utils.CommandExecutor) *VerifyPasswordGa /* This gatherer expects only the username for which the password will be verified */ -func (g *VerifyPasswordGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { +func (g *VerifyPasswordGatherer) Gather( + _ context.Context, + factsRequests []entities.FactRequest, +) ([]entities.Fact, error) { facts := []entities.Fact{} log.Infof("Starting password verifying facts gathering process") From 0a1380f50ce0a73a8a572e84abb3dd62b7506ab0 Mon Sep 17 00:00:00 2001 From: balanza Date: Thu, 2 Jan 2025 12:58:33 +0100 Subject: [PATCH 04/12] add lint exception --- internal/factsengine/gatherers/corosynccmapctl_test.go | 1 + internal/factsengine/gatherers/sysctl_test.go | 1 + 2 files changed, 2 insertions(+) diff --git a/internal/factsengine/gatherers/corosynccmapctl_test.go b/internal/factsengine/gatherers/corosynccmapctl_test.go index de72f657..91c3a39d 100644 --- a/internal/factsengine/gatherers/corosynccmapctl_test.go +++ b/internal/factsengine/gatherers/corosynccmapctl_test.go @@ -27,6 +27,7 @@ func (suite *CorosyncCmapctlTestSuite) SetupTest() { suite.mockExecutor = new(utilsMocks.CommandExecutor) } +// nolint:dupl func (suite *CorosyncCmapctlTestSuite) TestCorosyncCmapctlGathererNoArgumentProvided() { mockOutputFile, _ := os.Open(helpers.GetFixturePath("gatherers/corosynccmap-ctl.output")) mockOutput, _ := io.ReadAll(mockOutputFile) diff --git a/internal/factsengine/gatherers/sysctl_test.go b/internal/factsengine/gatherers/sysctl_test.go index 26814359..4f810ba3 100644 --- a/internal/factsengine/gatherers/sysctl_test.go +++ b/internal/factsengine/gatherers/sysctl_test.go @@ -27,6 +27,7 @@ func (suite *SysctlTestSuite) SetupTest() { suite.mockExecutor = new(utilsMocks.CommandExecutor) } +// nolint:dupl func (suite *SysctlTestSuite) TestSysctlGathererNoArgumentProvided() { mockOutputFile, _ := os.Open(helpers.GetFixturePath("gatherers/sysctl.output")) mockOutput, _ := io.ReadAll(mockOutputFile) From 00e6ced411f74646a4254823b99db6a56ec8cc44 Mon Sep 17 00:00:00 2001 From: balanza Date: Tue, 7 Jan 2025 13:00:01 +0100 Subject: [PATCH 05/12] typo --- internal/factsengine/gatherers/_todo/crmmon_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/factsengine/gatherers/_todo/crmmon_test.go b/internal/factsengine/gatherers/_todo/crmmon_test.go index a8b09b86..69e57029 100644 --- a/internal/factsengine/gatherers/_todo/crmmon_test.go +++ b/internal/factsengine/gatherers/_todo/crmmon_test.go @@ -52,7 +52,7 @@ func (suite *CrmMonTestSuite) TestCrmMonGather() { }, } - factResults, err := p.Gather(context.Background(), actRequests) + factResults, err := p.Gather(context.Background(), factRequests) expectedResults := []entities.FactsGatheredItem{ { From fdf1b9fe9e7d41db25e5e1098ff30701338e89a9 Mon Sep 17 00:00:00 2001 From: balanza Date: Wed, 8 Jan 2025 17:33:33 +0100 Subject: [PATCH 06/12] regenerate mocks --- .../gatherers/mocks/FactGatherer.go | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/internal/factsengine/gatherers/mocks/FactGatherer.go b/internal/factsengine/gatherers/mocks/FactGatherer.go index 74c49425..435b68ed 100644 --- a/internal/factsengine/gatherers/mocks/FactGatherer.go +++ b/internal/factsengine/gatherers/mocks/FactGatherer.go @@ -1,9 +1,10 @@ -// Code generated by mockery v2.14.0. DO NOT EDIT. +// Code generated by mockery v2.32.3. DO NOT EDIT. package mocks import ( - "context" + context "context" + entities "github.com/trento-project/agent/pkg/factsengine/entities" mock "github.com/stretchr/testify/mock" @@ -14,22 +15,25 @@ type FactGatherer struct { mock.Mock } -// Gather provides a mock function with given fields: factsRequests -func (_m *FactGatherer) Gather(_ context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { - ret := _m.Called(factsRequests) +// Gather provides a mock function with given fields: _a0, factsRequests +func (_m *FactGatherer) Gather(_a0 context.Context, factsRequests []entities.FactRequest) ([]entities.Fact, error) { + ret := _m.Called(_a0, factsRequests) var r0 []entities.Fact - if rf, ok := ret.Get(0).(func([]entities.FactRequest) []entities.Fact); ok { - r0 = rf(factsRequests) + var r1 error + if rf, ok := ret.Get(0).(func(context.Context, []entities.FactRequest) ([]entities.Fact, error)); ok { + return rf(_a0, factsRequests) + } + if rf, ok := ret.Get(0).(func(context.Context, []entities.FactRequest) []entities.Fact); ok { + r0 = rf(_a0, factsRequests) } else { if ret.Get(0) != nil { r0 = ret.Get(0).([]entities.Fact) } } - var r1 error - if rf, ok := ret.Get(1).(func([]entities.FactRequest) error); ok { - r1 = rf(factsRequests) + if rf, ok := ret.Get(1).(func(context.Context, []entities.FactRequest) error); ok { + r1 = rf(_a0, factsRequests) } else { r1 = ret.Error(1) } @@ -37,13 +41,12 @@ func (_m *FactGatherer) Gather(_ context.Context, factsRequests []entities.FactR return r0, r1 } -type mockConstructorTestingTNewFactGatherer interface { +// NewFactGatherer creates a new instance of FactGatherer. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. +// The first argument is typically a *testing.T value. +func NewFactGatherer(t interface { mock.TestingT Cleanup(func()) -} - -// NewFactGatherer creates a new instance of FactGatherer. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewFactGatherer(t mockConstructorTestingTNewFactGatherer) *FactGatherer { +}) *FactGatherer { mock := &FactGatherer{} mock.Mock.Test(t) From be1cbcc28d6033c093ad5c5c10c85ac7c417eeb0 Mon Sep 17 00:00:00 2001 From: balanza Date: Wed, 8 Jan 2025 18:46:17 +0100 Subject: [PATCH 07/12] handle cancellation on agent stop --- internal/factsengine/factsengine.go | 2 +- internal/factsengine/gathering.go | 20 +++- .../factsengine/gathering_internal_test.go | 113 ++++++++++++++++-- internal/factsengine/policy.go | 19 ++- internal/factsengine/policy_internal_test.go | 9 +- 5 files changed, 142 insertions(+), 21 deletions(-) diff --git a/internal/factsengine/factsengine.go b/internal/factsengine/factsengine.go index 34b91508..ec7e7d3d 100644 --- a/internal/factsengine/factsengine.go +++ b/internal/factsengine/factsengine.go @@ -69,7 +69,7 @@ func (c *FactsEngine) Listen(ctx context.Context) error { } }() queue := fmt.Sprintf(agentsQueue, c.agentID) - if err := c.factsServiceAdapter.Listen(queue, exchange, agentsEventsRoutingKey, c.handleEvent); err != nil { + if err := c.factsServiceAdapter.Listen(queue, exchange, agentsEventsRoutingKey, c.makeEventHandler(ctx)); err != nil { return err } diff --git a/internal/factsengine/gathering.go b/internal/factsengine/gathering.go index ed6d3a3e..415e9def 100644 --- a/internal/factsengine/gathering.go +++ b/internal/factsengine/gathering.go @@ -2,8 +2,8 @@ package factsengine import ( "context" + "errors" - "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/trento-project/agent/internal/factsengine/factscache" "github.com/trento-project/agent/internal/factsengine/gatherers" @@ -12,6 +12,7 @@ import ( ) func gatherFacts( + ctx context.Context, executionID, agentID string, groupID string, @@ -26,9 +27,10 @@ func gatherFacts( } groupedFactsRequest := groupFactsRequestByGatherer(agentFacts) factsCh := make(chan []entities.Fact, len(groupedFactsRequest.FactRequests)) - g, groupCtx := errgroup.WithContext(context.Background()) cache := factscache.NewFactsCache() + g := new(errgroup.Group) + log.Infof("Starting facts gathering process") // Gather facts asynchronously @@ -50,7 +52,15 @@ func gatherFacts( g.Go(func() error { var gatheringError *entities.FactGatheringError - newFacts, err := gatherer.Gather(groupCtx, factsRequest) + newFacts, err := gatherer.Gather(ctx, factsRequest) + + select { + case <-ctx.Done(): + return ctx.Err() + default: + // Do nothing + } + switch { case err == nil: factsCh <- newFacts @@ -62,10 +72,12 @@ func gatherFacts( } return nil + }) } - if err := g.Wait(); err != nil { + err := g.Wait() + if err != nil { return factsResults, err } diff --git a/internal/factsengine/gathering_internal_test.go b/internal/factsengine/gathering_internal_test.go index 90028ae4..f8708bda 100644 --- a/internal/factsengine/gathering_internal_test.go +++ b/internal/factsengine/gathering_internal_test.go @@ -1,7 +1,10 @@ package factsengine import ( + "context" + "errors" "testing" + "time" "github.com/google/uuid" "github.com/stretchr/testify/mock" @@ -48,7 +51,7 @@ func (suite *GatheringTestSuite) TestGatheringGatherFacts() { } dummyGathererOne := &mocks.FactGatherer{} - dummyGathererOne.On("Gather", mock.Anything). + dummyGathererOne.On("Gather", mock.Anything, mock.Anything). Return([]entities.Fact{ { Name: "dummy1", @@ -58,7 +61,7 @@ func (suite *GatheringTestSuite) TestGatheringGatherFacts() { }, nil).Times(1) dummyGathererTwo := &mocks.FactGatherer{} - dummyGathererTwo.On("Gather", mock.Anything). + dummyGathererTwo.On("Gather", mock.Anything, mock.Anything). Return([]entities.Fact{ { Name: "dummy2", @@ -76,7 +79,7 @@ func (suite *GatheringTestSuite) TestGatheringGatherFacts() { }, }) - factResults, err := gatherFacts(suite.executionID, suite.agentID, suite.groupID, &factsRequest, *registry) + factResults, err := gatherFacts(context.Background(), suite.executionID, suite.agentID, suite.groupID, &factsRequest, *registry) expectedFacts := []entities.Fact{ { @@ -118,7 +121,7 @@ func (suite *GatheringTestSuite) TestFactsEngineGatherFactsGathererNotFound() { } dummyGathererOne := &mocks.FactGatherer{} - dummyGathererOne.On("Gather", mock.Anything). + dummyGathererOne.On("Gather", mock.Anything, mock.Anything). Return([]entities.Fact{ { Name: "dummy1", @@ -128,7 +131,7 @@ func (suite *GatheringTestSuite) TestFactsEngineGatherFactsGathererNotFound() { }, nil).Times(1) dummyGathererTwo := &mocks.FactGatherer{} - dummyGathererTwo.On("Gather", mock.Anything). + dummyGathererTwo.On("Gather", mock.Anything, mock.Anything). Return([]entities.Fact{ { Name: "dummy2", @@ -146,7 +149,7 @@ func (suite *GatheringTestSuite) TestFactsEngineGatherFactsGathererNotFound() { }, }) - factResults, err := gatherFacts(suite.executionID, suite.agentID, suite.groupID, &factsRequest, *registry) + factResults, err := gatherFacts(context.Background(), suite.executionID, suite.agentID, suite.groupID, &factsRequest, *registry) expectedFacts := []entities.Fact{ { @@ -183,7 +186,7 @@ func (suite *GatheringTestSuite) TestFactsEngineGatherFactsErrorGathering() { } dummyGathererOne := &mocks.FactGatherer{} - dummyGathererOne.On("Gather", mock.Anything). + dummyGathererOne.On("Gather", mock.Anything, mock.Anything). Return([]entities.Fact{ { Name: "dummy1", @@ -193,7 +196,7 @@ func (suite *GatheringTestSuite) TestFactsEngineGatherFactsErrorGathering() { }, nil).Times(1) errorGatherer := &mocks.FactGatherer{} - errorGatherer.On("Gather", mock.Anything). + errorGatherer.On("Gather", mock.Anything, mock.Anything). Return(nil, &entities.FactGatheringError{Type: "dummy-type", Message: "some error"}).Times(1) registry := gatherers.NewRegistry(gatherers.FactGatherersTree{ @@ -205,7 +208,7 @@ func (suite *GatheringTestSuite) TestFactsEngineGatherFactsErrorGathering() { }, }) - factResults, err := gatherFacts(suite.executionID, suite.agentID, suite.groupID, &factsRequest, *registry) + factResults, err := gatherFacts(context.Background(), suite.executionID, suite.agentID, suite.groupID, &factsRequest, *registry) expectedFacts := []entities.Fact{ { @@ -231,3 +234,95 @@ func (suite *GatheringTestSuite) TestFactsEngineGatherFactsErrorGathering() { suite.Equal(suite.groupID, factResults.GroupID) suite.ElementsMatch(expectedFacts, factResults.FactsGathered) } + +func (suite *GatheringTestSuite) TestParentContextIsNotCancelledWhenGatherFails() { + factsRequest := entities.FactsGatheringRequestedTarget{ + AgentID: suite.agentID, + FactRequests: []entities.FactRequest{ + { + Name: "dummy1", + Gatherer: "dummyGatherer1", + Argument: "dummy1", + CheckID: "check1", + }, + }, + } + + dummyGathererOne := &mocks.FactGatherer{} + dummyGathererOne. + On("Gather", mock.Anything, mock.Anything). + Return(nil, errors.New("Gatherer error")) + + registry := gatherers.NewRegistry(gatherers.FactGatherersTree{ + "dummyGatherer1": map[string]gatherers.FactGatherer{ + "v1": dummyGathererOne, + }, + }) + + ctx := context.Background() + + _, _ = gatherFacts(ctx, suite.executionID, suite.agentID, suite.groupID, &factsRequest, *registry) + + select { + case <-ctx.Done(): + suite.Fail("Parent context should not be cancelled") + default: + suite.T().Log("Parent context is not cancelled") + } +} + +func (suite *GatheringTestSuite) TestGatherIsCancelledWhenParentContextIsCancelled() { + factsRequest := entities.FactsGatheringRequestedTarget{ + AgentID: suite.agentID, + FactRequests: []entities.FactRequest{ + { + Name: "dummy1", + Gatherer: "dummyGatherer1", + Argument: "dummy1", + CheckID: "check1", + }, + }, + } + + dummyGathererOne := &mocks.FactGatherer{} + dummyGathererOne. + On("Gather", mock.Anything, mock.Anything). + Run(func(args mock.Arguments) { + // nolint:forcetypeassert + innerCtx := args.Get(0).(context.Context) + select { + case <-innerCtx.Done(): + suite.T().Log("Gather receives context cancellation") + case <-time.After(3 * time.Second): + suite.Fail("Gather should receive context cancellation") + } + }). + Return([]entities.Fact{ + { + Name: "dummy1", + Value: &entities.FactValueInt{Value: 1}, + CheckID: "check1", + }, + }, nil) + + registry := gatherers.NewRegistry(gatherers.FactGatherersTree{ + "dummyGatherer1": map[string]gatherers.FactGatherer{ + "v1": dummyGathererOne, + }, + }) + + ctx, cancel := context.WithCancel(context.Background()) + go func() { + cancel() + }() + _, err := gatherFacts(ctx, suite.executionID, suite.agentID, suite.groupID, &factsRequest, *registry) + + <-ctx.Done() + + if err == nil { + suite.Fail("Error should not be nil") + } + + suite.Equal(context.Canceled, err) + +} diff --git a/internal/factsengine/policy.go b/internal/factsengine/policy.go index 3fe071b9..1460120a 100644 --- a/internal/factsengine/policy.go +++ b/internal/factsengine/policy.go @@ -1,6 +1,7 @@ package factsengine import ( + "context" "fmt" "github.com/pkg/errors" @@ -13,14 +14,22 @@ const ( FactsGatheringRequested = "Trento.Checks.V1.FactsGatheringRequested" ) -func (c *FactsEngine) handleEvent(_ string, request []byte) error { +type eventHandler func(name string, event []byte) error + +func (c *FactsEngine) makeEventHandler(ctx context.Context) eventHandler { + return func(name string, event []byte) error { + return c.handleEvent(ctx, name, event) + } +} + +func (c *FactsEngine) handleEvent(ctx context.Context, _ string, request []byte) error { eventType, err := events.EventType(request) if err != nil { return errors.Wrap(err, "Error getting event type") } switch eventType { case FactsGatheringRequested: - err := c.handleFactsGatheringRequestedEvent(request) + err := c.handleFactsGatheringRequestedEvent(ctx, request) if err != nil { return errors.Wrap(err, "Error handling facts request") } @@ -30,7 +39,10 @@ func (c *FactsEngine) handleEvent(_ string, request []byte) error { return nil } -func (c *FactsEngine) handleFactsGatheringRequestedEvent(factsRequestByte []byte) error { +func (c *FactsEngine) handleFactsGatheringRequestedEvent( + ctx context.Context, + factsRequestByte []byte, +) error { factsRequest, err := FactsGatheringRequestedFromEvent(factsRequestByte) if err != nil { return err @@ -44,6 +56,7 @@ func (c *FactsEngine) handleFactsGatheringRequestedEvent(factsRequestByte []byte } gatheredFacts, err := gatherFacts( + ctx, factsRequest.ExecutionID, c.agentID, factsRequest.GroupID, diff --git a/internal/factsengine/policy_internal_test.go b/internal/factsengine/policy_internal_test.go index 4fa421ab..d3e3bcf3 100644 --- a/internal/factsengine/policy_internal_test.go +++ b/internal/factsengine/policy_internal_test.go @@ -2,6 +2,7 @@ package factsengine import ( + "context" "testing" "github.com/google/uuid" @@ -38,7 +39,7 @@ func (suite *PolicyTestSuite) SetupTest() { } func (suite *PolicyTestSuite) TestPolicyHandleEventWrongMessage() { - err := suite.factsEngine.handleEvent("", []byte("")) + err := suite.factsEngine.handleEvent(context.Background(), "", []byte("")) suite.ErrorContains(err, "Error getting event type") } @@ -50,7 +51,7 @@ func (suite *PolicyTestSuite) TestPolicyHandleEventInvalideEvent() { ) suite.NoError(err) - err = suite.factsEngine.handleEvent("", event) + err = suite.factsEngine.handleEvent(context.Background(), "", event) suite.EqualError(err, "Invalid event type: Trento.Checks.V1.FactsGathered") } @@ -72,7 +73,7 @@ func (suite *PolicyTestSuite) TestPolicyHandleEventDiscardAgent() { ) // nolint suite.NoError(err) - err = suite.factsEngine.handleEvent("", event) + err = suite.factsEngine.handleEvent(context.Background(), "", event) suite.NoError(err) suite.mockAdapter.AssertNumberOfCalls(suite.T(), "Publish", 0) } @@ -99,7 +100,7 @@ func (suite *PolicyTestSuite) TestPolicyHandleEvent() { events.ContentType(), mock.Anything).Return(nil) - err = suite.factsEngine.handleEvent("", event) + err = suite.factsEngine.handleEvent(context.Background(), "", event) suite.NoError(err) suite.mockAdapter.AssertNumberOfCalls(suite.T(), "Publish", 1) } From 4243bf384051cbc4ab0f08799a7ae1104e3c38d3 Mon Sep 17 00:00:00 2001 From: balanza Date: Wed, 8 Jan 2025 18:52:26 +0100 Subject: [PATCH 08/12] handle cancellation on facts commands --- cmd/facts.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/cmd/facts.go b/cmd/facts.go index 096f3fc7..1d344a42 100644 --- a/cmd/facts.go +++ b/cmd/facts.go @@ -1,6 +1,11 @@ package cmd import ( + "context" + "os" + "os/signal" + "syscall" + "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -108,7 +113,27 @@ func gather(cmd *cobra.Command, _ []string) { }, } - value, err := g.Gather(cmd.Context(), factRequest) + ctx, cancel := context.WithCancel(cmd.Context()) + + signals := make(chan os.Signal, 1) + signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) + cancelled := false + go func() { + <-signals + log.Info("Caught signal!") + cancelled = true + cancel() + + }() + + value, err := g.Gather(ctx, factRequest) + + if cancelled { + gatherers.CleanupPlugins() + log.Info("Gathering cancelled") + return + } + if err != nil { log.Errorf("Error gathering fact \"%s\" with argument \"%s\"", gatherer, argument) cleanupAndFatal(err) From 1d7198c4eedddd271b6b626afd65b23a03f5a745 Mon Sep 17 00:00:00 2001 From: balanza Date: Thu, 9 Jan 2025 10:38:59 +0100 Subject: [PATCH 09/12] typo --- internal/factsengine/gatherers/_todo/crmmon_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/factsengine/gatherers/_todo/crmmon_test.go b/internal/factsengine/gatherers/_todo/crmmon_test.go index 69e57029..8b5c2fe8 100644 --- a/internal/factsengine/gatherers/_todo/crmmon_test.go +++ b/internal/factsengine/gatherers/_todo/crmmon_test.go @@ -91,7 +91,7 @@ func (suite *CrmMonTestSuite) TestCrmMonGatherCmdNotFound() { }, } - _, err := p.Gather(context.Background(), actRequests) + _, err := p.Gather(context.Background(), factRequests) suite.EqualError(err, "crm_mon not found") } @@ -118,7 +118,7 @@ func (suite *CrmMonTestSuite) TestCrmMonGatherError() { }, } - factResults, err := p.Gather(context.Background(), actRequests) + factResults, err := p.Gather(context.Background(), factRequests) expectedResults := []entities.FactsGatheredItem{ { From c66e25b4bf909fb4aebf7830690e27b186833970 Mon Sep 17 00:00:00 2001 From: balanza Date: Thu, 9 Jan 2025 10:40:10 +0100 Subject: [PATCH 10/12] restore onliner --- internal/factsengine/gathering.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/factsengine/gathering.go b/internal/factsengine/gathering.go index 415e9def..e4ef34b0 100644 --- a/internal/factsengine/gathering.go +++ b/internal/factsengine/gathering.go @@ -76,8 +76,7 @@ func gatherFacts( }) } - err := g.Wait() - if err != nil { + if err := g.Wait(); err != nil { return factsResults, err } From ab5a951135e076bc44c808f9b1d907c0e2a7d420 Mon Sep 17 00:00:00 2001 From: balanza Date: Thu, 9 Jan 2025 10:44:12 +0100 Subject: [PATCH 11/12] simplify select --- internal/factsengine/gathering.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/internal/factsengine/gathering.go b/internal/factsengine/gathering.go index e4ef34b0..2ddf131a 100644 --- a/internal/factsengine/gathering.go +++ b/internal/factsengine/gathering.go @@ -53,15 +53,11 @@ func gatherFacts( var gatheringError *entities.FactGatheringError newFacts, err := gatherer.Gather(ctx, factsRequest) - - select { - case <-ctx.Done(): - return ctx.Err() - default: - // Do nothing - } + ctxErr := ctx.Err() switch { + case ctxErr != nil: + return ctxErr case err == nil: factsCh <- newFacts case errors.As(err, &gatheringError): From 055f17694b0ea4be8d0830642af22f52fe343efb Mon Sep 17 00:00:00 2001 From: balanza Date: Thu, 9 Jan 2025 12:26:25 +0100 Subject: [PATCH 12/12] remove double cleanup --- cmd/facts.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/facts.go b/cmd/facts.go index 1d344a42..3678abee 100644 --- a/cmd/facts.go +++ b/cmd/facts.go @@ -129,7 +129,6 @@ func gather(cmd *cobra.Command, _ []string) { value, err := g.Gather(ctx, factRequest) if cancelled { - gatherers.CleanupPlugins() log.Info("Gathering cancelled") return }