Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Metricbeat test modules #4656

Merged
merged 7 commits into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ https://github.com/elastic/beats/compare/v6.0.0-alpha2...master[Check the HEAD d
- Add the ability to configure kernel's audit failure mode. {pull}4516[4516]
- Add experimental Aerospike module. {pull}4560[4560]
- Vsphere module: collect custom fields from virtual machines. {issue}4464[4464]
- Add `test modules` command, to test modules expected output. {pull}4656[4656]
- Add `processors` setting to metricbeat modules. {pull}4699[4699]

*Packetbeat*
Expand Down
76 changes: 38 additions & 38 deletions libbeat/cfgfile/glob_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ type GlobManager struct {
glob string
enabledExtension string
disabledExtension string
files []*cfgfile
files []*CfgFile
}

type cfgfile struct {
name string
path string
enabled bool
type CfgFile struct {
Name string
Path string
Enabled bool
}

// NewGlobManager takes a glob and enabled/disabled extensions and returns a GlobManager object.
Expand Down Expand Up @@ -58,10 +58,10 @@ func (g *GlobManager) load() error {

for _, path := range files {
// Trim cfg file name
g.files = append(g.files, &cfgfile{
name: strings.TrimSuffix(filepath.Base(path), g.enabledExtension),
enabled: true,
path: path,
g.files = append(g.files, &CfgFile{
Name: strings.TrimSuffix(filepath.Base(path), g.enabledExtension),
Enabled: true,
Path: path,
})
}

Expand All @@ -74,45 +74,45 @@ func (g *GlobManager) load() error {

for _, path := range files {
// Trim cfg file name
g.files = append(g.files, &cfgfile{
name: strings.TrimSuffix(filepath.Base(path), g.enabledExtension+g.disabledExtension),
enabled: false,
path: path,
g.files = append(g.files, &CfgFile{
Name: strings.TrimSuffix(filepath.Base(path), g.enabledExtension+g.disabledExtension),
Enabled: false,
Path: path,
})
}

return nil
}

// ListEnabled conf files
func (g *GlobManager) ListEnabled() []string {
var names []string
func (g *GlobManager) ListEnabled() []*CfgFile {
var enabled []*CfgFile
for _, file := range g.files {
if file.enabled {
names = append(names, file.name)
if file.Enabled {
enabled = append(enabled, file)
}
}

return names
return enabled
}

// ListDisabled conf files
func (g *GlobManager) ListDisabled() []string {
var names []string
func (g *GlobManager) ListDisabled() []*CfgFile {
var disabled []*CfgFile
for _, file := range g.files {
if !file.enabled {
names = append(names, file.name)
if !file.Enabled {
disabled = append(disabled, file)
}
}

return names
return disabled
}

// Enabled returns true if given conf file is enabled
func (g *GlobManager) Enabled(name string) bool {
for _, file := range g.files {
if name == file.name {
return file.enabled
if name == file.Name {
return file.Enabled
}
}
return false
Expand All @@ -121,7 +121,7 @@ func (g *GlobManager) Enabled(name string) bool {
// Exists return true if the given conf exists (enabled or disabled)
func (g *GlobManager) Exists(name string) bool {
for _, file := range g.files {
if name == file.name {
if name == file.Name {
return true
}
}
Expand All @@ -131,14 +131,14 @@ func (g *GlobManager) Exists(name string) bool {
// Enable given conf file, does nothing if it's enabled already
func (g *GlobManager) Enable(name string) error {
for _, file := range g.files {
if name == file.name {
if !file.enabled {
newPath := strings.TrimSuffix(file.path, g.disabledExtension)
if err := os.Rename(file.path, newPath); err != nil {
if name == file.Name {
if !file.Enabled {
newPath := strings.TrimSuffix(file.Path, g.disabledExtension)
if err := os.Rename(file.Path, newPath); err != nil {
return errors.Wrap(err, "enable failed")
}
file.enabled = true
file.path = newPath
file.Enabled = true
file.Path = newPath
}
return nil
}
Expand All @@ -150,14 +150,14 @@ func (g *GlobManager) Enable(name string) error {
// Disable given conf file, does nothing if it's disabled already
func (g *GlobManager) Disable(name string) error {
for _, file := range g.files {
if name == file.name {
if file.enabled {
newPath := file.path + g.disabledExtension
if err := os.Rename(file.path, newPath); err != nil {
if name == file.Name {
if file.Enabled {
newPath := file.Path + g.disabledExtension
if err := os.Rename(file.Path, newPath); err != nil {
return errors.Wrap(err, "disable failed")
}
file.enabled = false
file.path = newPath
file.Enabled = false
file.Path = newPath
}
return nil
}
Expand Down
8 changes: 6 additions & 2 deletions libbeat/cfgfile/glob_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ func TestGlobManager(t *testing.T) {
assert.Equal(t, len(manager.ListEnabled()), 1)
assert.Equal(t, len(manager.ListDisabled()), 2)

assert.Equal(t, manager.ListEnabled(), []string{"config1"})
enabled := manager.ListEnabled()
assert.Equal(t, enabled[0].Name, "config1")
assert.Equal(t, enabled[0].Enabled, true)

// Test enable
if err = manager.Enable("config3"); err != nil {
Expand All @@ -70,7 +72,9 @@ func TestGlobManager(t *testing.T) {
assert.Equal(t, len(manager.ListEnabled()), 2)
assert.Equal(t, len(manager.ListDisabled()), 1)

assert.Equal(t, manager.ListDisabled(), []string{"config2"})
disabled := manager.ListDisabled()
assert.Equal(t, disabled[0].Name, "config2")
assert.Equal(t, disabled[0].Enabled, false)

// Check correct files layout:
files, err := filepath.Glob(dir + "/*")
Expand Down
7 changes: 3 additions & 4 deletions libbeat/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ package cmd
import (
"github.com/spf13/cobra"

"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/cmd/export"
)

func genExportCmd(name, beatVersion string, beatCreator beat.Creator) *cobra.Command {
func genExportCmd(name, beatVersion string) *cobra.Command {
exportCmd := &cobra.Command{
Use: "export",
Short: "Export current config or index template",
}

exportCmd.AddCommand(export.GenExportConfigCmd(name, beatVersion, beatCreator))
exportCmd.AddCommand(export.GenTemplateConfigCmd(name, beatVersion, beatCreator))
exportCmd.AddCommand(export.GenExportConfigCmd(name, beatVersion))
exportCmd.AddCommand(export.GenTemplateConfigCmd(name, beatVersion))

return exportCmd
}
2 changes: 1 addition & 1 deletion libbeat/cmd/export/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/elastic/beats/libbeat/beat"
)

func GenExportConfigCmd(name, beatVersion string, beatCreator beat.Creator) *cobra.Command {
func GenExportConfigCmd(name, beatVersion string) *cobra.Command {
return &cobra.Command{
Use: "config",
Short: "Export current config to stdout",
Expand Down
2 changes: 1 addition & 1 deletion libbeat/cmd/export/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/elastic/beats/libbeat/template"
)

func GenTemplateConfigCmd(name, beatVersion string, beatCreator beat.Creator) *cobra.Command {
func GenTemplateConfigCmd(name, beatVersion string) *cobra.Command {
genTemplateConfigCmd := &cobra.Command{
Use: "template",
Short: "Export index template to stdout",
Expand Down
9 changes: 5 additions & 4 deletions libbeat/cmd/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
"github.com/spf13/cobra"

"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/cfgfile"
)

// ModulesManager interface provides all actions needed to implement modules command
// (to list, enable & disable modules)
type ModulesManager interface {
ListEnabled() []string
ListDisabled() []string
ListEnabled() []*cfgfile.CfgFile
ListDisabled() []*cfgfile.CfgFile
Exists(name string) bool
Enabled(name string) bool
Enable(name string) error
Expand Down Expand Up @@ -69,12 +70,12 @@ func genListModulesCmd(name, version string, modulesFactory ModulesManagerFactor

fmt.Println("Enabled:")
for _, module := range modules.ListEnabled() {
fmt.Println(module)
fmt.Println(module.Name)
}

fmt.Println("\nDisabled:")
for _, module := range modules.ListDisabled() {
fmt.Println(module)
fmt.Println(module.Name)
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion libbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func GenRootCmdWithRunFlags(name, version string, beatCreator beat.Creator, runF
rootCmd.SetupCmd = genSetupCmd(name, version, beatCreator)
rootCmd.VersionCmd = genVersionCmd(name, version)
rootCmd.CompletionCmd = genCompletionCmd(name, version, rootCmd)
rootCmd.ExportCmd = genExportCmd(name, version, beatCreator)
rootCmd.ExportCmd = genExportCmd(name, version)
rootCmd.TestCmd = genTestCmd(name, version, beatCreator)

// Root command is an alias for run
Expand Down
73 changes: 53 additions & 20 deletions libbeat/testing/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/fatih/color"
)
Expand All @@ -14,6 +15,7 @@ type ConsoleDriver struct {
level int
reported bool
killer func()
result string
}

// NewConsoleDriver initializes and returns a new console driver with output to given file
Expand All @@ -25,14 +27,18 @@ func NewConsoleDriver(stdout io.Writer) *ConsoleDriver {
// Killer function will be called on fatal errors
func NewConsoleDriverWithKiller(stdout io.Writer, killer func()) *ConsoleDriver {
return &ConsoleDriver{
Stdout: stdout,
level: 0,
killer: killer,
Stdout: stdout,
level: 0,
killer: killer,
reported: true,
}
}

func (d *ConsoleDriver) Run(name string, f func(Driver)) {
d.printf("%s...\n", name)
if !d.reported {
fmt.Fprintln(d.Stdout, "")
}
d.printf("%s...", name)

// Run sub func
driver := &ConsoleDriver{
Expand All @@ -43,59 +49,86 @@ func (d *ConsoleDriver) Run(name string, f func(Driver)) {
f(driver)

if !driver.reported {
driver.ok()
color.New(color.FgGreen).Fprintf(driver.Stdout, "OK\n")
driver.reported = true
}

if driver.result != "" {
driver.Info("result", driver.indent(driver.result))
}

d.reported = true
}

func (d *ConsoleDriver) Info(field, value string) {
if !d.reported {
fmt.Fprintln(d.Stdout, "")
}
d.printf("%s: %s\n", field, value)
d.reported = true
}

func (d *ConsoleDriver) Warn(field, reason string) {
if !d.reported {
fmt.Fprintln(d.Stdout, "")
}
d.printf("%s... ", field)
d.warn(reason)
color.New(color.FgYellow).Fprintf(d.Stdout, "WARN ")
fmt.Fprintln(d.Stdout, reason)
d.reported = true
}

func (d *ConsoleDriver) Error(field string, err error) {
d.printf("%s... ", field)
if err == nil {
d.ok()
d.ok(field)
return
}
d.error(err)
d.error(field, err)
}

func (d *ConsoleDriver) Fatal(field string, err error) {
d.printf("%s... ", field)
if err == nil {
d.ok()
d.ok(field)
return
}
d.error(err)
d.error(field, err)
d.killer()
}

func (d *ConsoleDriver) ok() {
func (d *ConsoleDriver) Result(data string) {
d.result = data
}

func (d *ConsoleDriver) ok(field string) {
if !d.reported {
fmt.Fprintln(d.Stdout, "")
}
d.printf("%s... ", field)
color.New(color.FgGreen).Fprintf(d.Stdout, "OK\n")
d.reported = true
}

func (d *ConsoleDriver) error(err error) {
func (d *ConsoleDriver) error(field string, err error) {
if !d.reported {
fmt.Fprintln(d.Stdout, "")
}
d.printf("%s... ", field)
color.New(color.FgRed).Fprintf(d.Stdout, "ERROR ")
fmt.Fprintln(d.Stdout, err.Error())
d.reported = true
}

func (d *ConsoleDriver) warn(reason string) {
color.New(color.FgYellow).Fprintf(d.Stdout, "WARN ")
fmt.Fprintln(d.Stdout, reason)
d.reported = true
}

func (d *ConsoleDriver) printf(format string, args ...interface{}) {
for i := 0; i < d.level; i++ {
fmt.Fprint(d.Stdout, " ")
}
fmt.Fprintf(d.Stdout, format, args...)
}

func (d *ConsoleDriver) indent(data string) string {
res := "\n"
for _, line := range strings.Split(data, "\n") {
res += strings.Repeat(" ", d.level+2) + line + "\n"
}
return res
}
Loading