Skip to content

Commit

Permalink
move metricbeat command definition out of init (#41816)
Browse files Browse the repository at this point in the history
  • Loading branch information
leehinman authored Dec 2, 2024
1 parent 3144d38 commit 0eb63c1
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 36 deletions.
9 changes: 1 addition & 8 deletions metricbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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(""))
}
2 changes: 1 addition & 1 deletion metricbeat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
20 changes: 14 additions & 6 deletions metricbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,40 @@ 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")
}

// Test started when the test binary is started. Only calls main.
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)
}
2 changes: 1 addition & 1 deletion x-pack/agentbeat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down
7 changes: 4 additions & 3 deletions x-pack/libbeat/management/tests/mbtest/metricbeat_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}()

Expand Down
14 changes: 6 additions & 8 deletions x-pack/metricbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,28 @@ 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{
"version": ecs.Version,
},
})

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))
}
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 {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/metricbeat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func main() {
if err := cmd.RootCmd.Execute(); err != nil {
if err := cmd.Initialize().Execute(); err != nil {
os.Exit(1)
}
}
20 changes: 14 additions & 6 deletions x-pack/metricbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,40 @@ 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")
}

// Test started when the test binary is started. Only calls main.
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)
}

0 comments on commit 0eb63c1

Please sign in to comment.