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

[Elastic Agent] Golden files for program tests #27862

Merged
merged 9 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
40 changes: 34 additions & 6 deletions x-pack/elastic-agent/pkg/agent/program/program_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package program

import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -21,6 +23,10 @@ import (
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/transpiler"
)

var (
generateFlag = flag.Bool("generate", false, "Write golden files")
)

func TestGroupBy(t *testing.T) {
t.Run("only named output", func(t *testing.T) {
sConfig := map[string]interface{}{
Expand Down Expand Up @@ -436,6 +442,16 @@ func TestConfiguration(t *testing.T) {
},
}

// Cleanup all generated files to make sure not having any left overs
if *generateFlag {
generatedFiles, err := filepath.Glob(filepath.Join("testdata", "*.generated.yml"))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better name? Maybe .golden.yml?

require.NoError(t, err)

for _, file := range generatedFiles {
fmt.Println(file)
}
}

for name, test := range testcases {
t.Run(name, func(t *testing.T) {
singleConfig, err := ioutil.ReadFile(filepath.Join("testdata", name+".yml"))
Expand Down Expand Up @@ -465,20 +481,31 @@ func TestConfiguration(t *testing.T) {
require.True(t, ok)
require.Equal(t, test.expected, len(defPrograms))

// TODO: If generate, remove all generated files first
for _, program := range defPrograms {
programConfig, err := ioutil.ReadFile(filepath.Join(
generatedPath := filepath.Join(
"testdata",
name+"-"+strings.ToLower(program.Spec.Cmd)+".yml",
))
name+"-"+strings.ToLower(program.Spec.Cmd)+".generated.yml",
)

compareMap := &transpiler.MapVisitor{}
program.Config.Accept(compareMap)

// Generate new file file for programm
if *generateFlag {
d, _ := yaml.Marshal(&compareMap.Content)
fmt.Println(string(d))
err := ioutil.WriteFile(generatedPath, d, 0644)
require.NoError(t, err)
}

programConfig, err := ioutil.ReadFile(generatedPath)
require.NoError(t, err)

var m map[string]interface{}
err = yamltest.FromYAML(programConfig, &m)
require.NoError(t, errors.Wrap(err, program.Cmd()))

compareMap := &transpiler.MapVisitor{}
program.Config.Accept(compareMap)

if !assert.True(t, cmp.Equal(m, compareMap.Content)) {
diff := cmp.Diff(m, compareMap.Content)
if diff != "" {
Expand All @@ -487,6 +514,7 @@ func TestConfiguration(t *testing.T) {
}
}
})

}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
filebeat:
inputs:
- index: logs-generic-default
paths:
- /var/log/hello1.log
- /var/log/hello2.log
processors:
- add_fields:
fields:
dataset: generic
namespace: default
type: logs
target: data_stream
- add_fields:
fields:
dataset: generic
target: event
- add_fields:
fields:
id: agent-id
snapshot: false
version: 8.0.0
target: elastic_agent
- add_fields:
fields:
id: agent-id
target: agent
type: log
output:
elasticsearch:
enabled: true
headers:
h1: test-header
hosts:
- 127.0.0.1:9200
- 127.0.0.1:9300
password: changeme
username: elastic

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
filebeat:
inputs:
- enabled: true
index: logs-generic-default
paths:
- /var/log/hello1.log
- /var/log/hello2.log
processors:
- add_fields:
fields:
dataset: generic
namespace: default
type: logs
target: data_stream
- add_fields:
fields:
dataset: generic
target: event
- add_fields:
fields:
id: agent-id
snapshot: false
version: 8.0.0
target: elastic_agent
- add_fields:
fields:
id: agent-id
target: agent
type: log
output:
elasticsearch:
headers:
h1: test-header
hosts:
- 127.0.0.1:9200
- 127.0.0.1:9300
password: changeme
username: elastic

This file was deleted.

Loading