From 0eb63c16f21a5120b380c9ab51d4d3ba7a78d5da Mon Sep 17 00:00:00 2001 From: Lee E Hinman <57081003+leehinman@users.noreply.github.com> Date: Mon, 2 Dec 2024 08:46:30 -0600 Subject: [PATCH] move metricbeat command definition out of init (#41816) --- metricbeat/cmd/root.go | 9 +-------- metricbeat/main.go | 2 +- metricbeat/main_test.go | 20 +++++++++++++------ x-pack/agentbeat/main.go | 2 +- .../tests/mbtest/metricbeat_v2_test.go | 7 ++++--- .../mbtest/system/process_integration_test.go | 5 +++-- x-pack/metricbeat/cmd/root.go | 14 ++++++------- x-pack/metricbeat/main.go | 2 +- x-pack/metricbeat/main_test.go | 20 +++++++++++++------ 9 files changed, 45 insertions(+), 36 deletions(-) diff --git a/metricbeat/cmd/root.go b/metricbeat/cmd/root.go index 497b71bed8a..872f05b8b35 100644 --- a/metricbeat/cmd/root.go +++ b/metricbeat/cmd/root.go @@ -43,9 +43,6 @@ const ( Name = "metricbeat" ) -// RootCmd to handle beats cli -var RootCmd *cmd.BeatsRootCmd - // withECSVersion is a modifier that adds ecs.version to events. var withECSVersion = processing.WithFields(mapstr.M{ "ecs": mapstr.M{ @@ -60,7 +57,7 @@ func MetricbeatSettings(moduleNameSpace string) instance.Settings { if moduleNameSpace == "" { moduleNameSpace = "module" } - var runFlags = pflag.NewFlagSet(Name, pflag.ExitOnError) + runFlags := pflag.NewFlagSet(Name, pflag.ExitOnError) runFlags.AddGoFlag(flag.CommandLine.Lookup("system.hostfs")) cfgfile.AddAllowedBackwardsCompatibleFlag("system.hostfs") return instance.Settings{ @@ -82,7 +79,3 @@ func Initialize(settings instance.Settings) *cmd.BeatsRootCmd { rootCmd.TestCmd.AddCommand(test.GenTestModulesCmd(Name, "", beater.DefaultTestModulesCreator())) return rootCmd } - -func init() { - RootCmd = Initialize(MetricbeatSettings("")) -} diff --git a/metricbeat/main.go b/metricbeat/main.go index 5dcea740b21..8515bca79d8 100644 --- a/metricbeat/main.go +++ b/metricbeat/main.go @@ -32,7 +32,7 @@ import ( ) func main() { - if err := cmd.RootCmd.Execute(); err != nil { + if err := cmd.Initialize(cmd.MetricbeatSettings("")).Execute(); err != nil { os.Exit(1) } } diff --git a/metricbeat/main_test.go b/metricbeat/main_test.go index 495ce5787e0..be50210575e 100644 --- a/metricbeat/main_test.go +++ b/metricbeat/main_test.go @@ -21,21 +21,27 @@ package main import ( "flag" + "os" "testing" "github.com/elastic/beats/v7/libbeat/cfgfile" + cmd "github.com/elastic/beats/v7/libbeat/cmd" "github.com/elastic/beats/v7/libbeat/tests/system/template" - "github.com/elastic/beats/v7/metricbeat/cmd" + mbcmd "github.com/elastic/beats/v7/metricbeat/cmd" ) -var systemTest *bool +var ( + systemTest *bool + mbCommand *cmd.BeatsRootCmd +) func init() { testing.Init() systemTest = flag.Bool("systemTest", false, "Set to true when running system tests") - cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest")) + mbCommand = mbcmd.Initialize(mbcmd.MetricbeatSettings("")) + mbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest")) cfgfile.AddAllowedBackwardsCompatibleFlag("systemTest") - cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile")) + mbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile")) cfgfile.AddAllowedBackwardsCompatibleFlag("test.coverprofile") } @@ -43,10 +49,12 @@ func init() { func TestSystem(t *testing.T) { cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { - main() + if err := mbCommand.Execute(); err != nil { + os.Exit(1) + } } } func TestTemplate(t *testing.T) { - template.TestTemplate(t, cmd.Name, false) + template.TestTemplate(t, mbCommand.Name(), false) } diff --git a/x-pack/agentbeat/main.go b/x-pack/agentbeat/main.go index f96031ec081..44cc6ce3306 100644 --- a/x-pack/agentbeat/main.go +++ b/x-pack/agentbeat/main.go @@ -42,7 +42,7 @@ into a single agentbeat binary.`, prepareCommand(auditbeat.RootCmd), prepareCommand(filebeat.Filebeat()), prepareCommand(heartbeat.RootCmd), - prepareCommand(metricbeat.RootCmd), + prepareCommand(metricbeat.Initialize()), prepareCommand(osquerybeat.RootCmd), prepareCommand(packetbeat.RootCmd), ) diff --git a/x-pack/libbeat/management/tests/mbtest/metricbeat_v2_test.go b/x-pack/libbeat/management/tests/mbtest/metricbeat_v2_test.go index fb8542514c2..4ffa583120b 100644 --- a/x-pack/libbeat/management/tests/mbtest/metricbeat_v2_test.go +++ b/x-pack/libbeat/management/tests/mbtest/metricbeat_v2_test.go @@ -34,8 +34,9 @@ var expectedMBStreams = &proto.UnitExpectedConfig{ } func TestSingleMetricbeatMetricsetWithProcessors(t *testing.T) { - tests.InitBeatsForTest(t, cmd.RootCmd) - var mbStreams = []*proto.Stream{ + mbCmd := cmd.Initialize() + tests.InitBeatsForTest(t, mbCmd) + mbStreams := []*proto.Stream{ { Id: "system/metrics-system.cpu-default-system", DataStream: &proto.DataStream{ @@ -79,7 +80,7 @@ func TestSingleMetricbeatMetricsetWithProcessors(t *testing.T) { go func() { t.Logf("Running beats...") - err := cmd.RootCmd.Execute() + err := mbCmd.Execute() require.NoError(t, err) }() diff --git a/x-pack/libbeat/management/tests/mbtest/system/process_integration_test.go b/x-pack/libbeat/management/tests/mbtest/system/process_integration_test.go index 660e9525558..3f936182333 100644 --- a/x-pack/libbeat/management/tests/mbtest/system/process_integration_test.go +++ b/x-pack/libbeat/management/tests/mbtest/system/process_integration_test.go @@ -37,7 +37,8 @@ func TestProcessStatusReporter(t *testing.T) { unitOutID := mock.NewID() token := mock.NewID() - tests.InitBeatsForTest(t, cmd.RootCmd) + mbCmd := cmd.Initialize() + tests.InitBeatsForTest(t, mbCmd) filename := fmt.Sprintf("test-%d", time.Now().Unix()) outPath := filepath.Join(t.TempDir(), filename) @@ -122,7 +123,7 @@ func TestProcessStatusReporter(t *testing.T) { go func() { t.Logf("Running beats...") - err := cmd.RootCmd.Execute() + err := mbCmd.Execute() require.NoError(t, err) }() diff --git a/x-pack/metricbeat/cmd/root.go b/x-pack/metricbeat/cmd/root.go index 47e169b1c95..76ca40ddf13 100644 --- a/x-pack/metricbeat/cmd/root.go +++ b/x-pack/metricbeat/cmd/root.go @@ -33,9 +33,6 @@ const ( Name = "metricbeat" ) -// RootCmd to handle beats cli -var RootCmd *cmd.BeatsRootCmd - // withECSVersion is a modifier that adds ecs.version to events. var withECSVersion = processing.WithFields(mapstr.M{ "ecs": mapstr.M{ @@ -43,7 +40,7 @@ var withECSVersion = processing.WithFields(mapstr.M{ }, }) -func init() { +func Initialize() *cmd.BeatsRootCmd { globalProcs, err := processors.NewPluginConfigFromList(defaultProcessors()) if err != nil { // these are hard-coded, shouldn't fail panic(fmt.Errorf("error creating global processors: %w", err)) @@ -51,12 +48,13 @@ func init() { settings := mbcmd.MetricbeatSettings("") settings.ElasticLicensed = true settings.Processing = processing.MakeDefaultSupport(true, globalProcs, withECSVersion, processing.WithHost, processing.WithAgentMeta()) - RootCmd = cmd.GenRootCmdWithSettings(beater.DefaultCreator(), settings) - RootCmd.AddCommand(cmd.GenModulesCmd(Name, "", mbcmd.BuildModulesManager)) - RootCmd.TestCmd.AddCommand(test.GenTestModulesCmd(Name, "", beater.DefaultTestModulesCreator())) - RootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) { + rootCmd := cmd.GenRootCmdWithSettings(beater.DefaultCreator(), settings) + rootCmd.AddCommand(cmd.GenModulesCmd(Name, "", mbcmd.BuildModulesManager)) + rootCmd.TestCmd.AddCommand(test.GenTestModulesCmd(Name, "", beater.DefaultTestModulesCreator())) + rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) { management.ConfigTransform.SetTransform(metricbeatCfg) } + return rootCmd } func defaultProcessors() []mapstr.M { diff --git a/x-pack/metricbeat/main.go b/x-pack/metricbeat/main.go index 92469da9c17..08afded3254 100644 --- a/x-pack/metricbeat/main.go +++ b/x-pack/metricbeat/main.go @@ -19,7 +19,7 @@ import ( ) func main() { - if err := cmd.RootCmd.Execute(); err != nil { + if err := cmd.Initialize().Execute(); err != nil { os.Exit(1) } } diff --git a/x-pack/metricbeat/main_test.go b/x-pack/metricbeat/main_test.go index e96a9932765..906782e1f5a 100644 --- a/x-pack/metricbeat/main_test.go +++ b/x-pack/metricbeat/main_test.go @@ -6,21 +6,27 @@ package main // This file is mandatory as otherwise the metricbeat.test binary is not generated correctly. import ( "flag" + "os" "testing" "github.com/elastic/beats/v7/libbeat/cfgfile" + cmd "github.com/elastic/beats/v7/libbeat/cmd" "github.com/elastic/beats/v7/libbeat/tests/system/template" - "github.com/elastic/beats/v7/x-pack/metricbeat/cmd" + mbcmd "github.com/elastic/beats/v7/x-pack/metricbeat/cmd" ) -var systemTest *bool +var ( + systemTest *bool + mbCommand *cmd.BeatsRootCmd +) func init() { testing.Init() systemTest = flag.Bool("systemTest", false, "Set to true when running system tests") - cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest")) + mbCommand = mbcmd.Initialize() + mbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest")) cfgfile.AddAllowedBackwardsCompatibleFlag("systemTest") - cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile")) + mbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile")) cfgfile.AddAllowedBackwardsCompatibleFlag("test.coverprofile") } @@ -28,10 +34,12 @@ func init() { func TestSystem(t *testing.T) { cfgfile.ConvertFlagsForBackwardsCompatibility() if *systemTest { - main() + if err := mbCommand.Execute(); err != nil { + os.Exit(1) + } } } func TestTemplate(t *testing.T) { - template.TestTemplate(t, cmd.Name, true) + template.TestTemplate(t, mbCommand.Name(), true) }