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 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
80 changes: 80 additions & 0 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,7 @@
package program

import (
"flag"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -21,6 +22,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 @@ -490,6 +495,81 @@ func TestConfiguration(t *testing.T) {
}
}

func TestUseCases(t *testing.T) {
defer os.Remove("fleet.yml")

useCasesPath := filepath.Join("testdata", "usecases")
useCases, err := filepath.Glob(filepath.Join(useCasesPath, "*.yml"))
require.NoError(t, err)

generatedFilesDir := filepath.Join(useCasesPath, "generated")

// Cleanup all generated files to make sure not having any left overs
if *generateFlag {
err := os.RemoveAll(generatedFilesDir)
require.NoError(t, err)
}

for _, usecase := range useCases {
t.Run(usecase, func(t *testing.T) {

useCaseName := strings.TrimSuffix(filepath.Base(usecase), ".yml")
singleConfig, err := ioutil.ReadFile(usecase)
require.NoError(t, err)

var m map[string]interface{}
err = yaml.Unmarshal(singleConfig, &m)
require.NoError(t, err)

ast, err := transpiler.NewAST(m)
require.NoError(t, err)

programs, err := Programs(&fakeAgentInfo{}, ast)
require.NoError(t, err)

require.Equal(t, 1, len(programs))

defPrograms, ok := programs["default"]
require.True(t, ok)

for _, program := range defPrograms {
generatedPath := filepath.Join(
useCasesPath, "generated",
useCaseName+"."+strings.ToLower(program.Spec.Cmd)+".golden.yml",
)

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

// Generate new golden file for programm
if *generateFlag {
d, err := yaml.Marshal(&compareMap.Content)
require.NoError(t, err)

err = os.MkdirAll(generatedFilesDir, 0755)
require.NoError(t, err)
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()))

if !assert.True(t, cmp.Equal(m, compareMap.Content)) {
diff := cmp.Diff(m, compareMap.Content)
if diff != "" {
t.Errorf("%s-%s mismatch (-want +got):\n%s", usecase, program.Spec.Name, diff)
}
}
}
})
}
}

type fakeAgentInfo struct{}

func (*fakeAgentInfo) AgentID() string {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
inputs:
- type: event/file
streams:
- paths:
- /var/log/hello1.log
- /var/log/hello2.log
management:
host: "localhost"
config:
reload: 123
outputs:
default:
type: elasticsearch
enabled: true
hosts: [127.0.0.1:9200, 127.0.0.1:9300]
username: elastic
password: changeme
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Production Website DB Servers
fleet:
kibana_url: https://kibana.mydomain.com:5601
ca_hash: 7HIpactkIAq2Y49orFOOQKurWxmmSFZhBCoQYcRhJ3Y=
checkin_interval: 5m
inputs:
- type: event/file
streams:
- enabled: true
paths:
- /var/log/hello1.log
- /var/log/hello2.log
management:
host: "localhost"
config:
reload: 123
outputs:
default:
type: elasticsearch
hosts: [127.0.0.1:9200, 127.0.0.1:9300]
username: elastic
password: changeme
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
revision: 5
name: Endpoint Host
fleet:
access_api_key: VuaCfGcBCdbkQm-e5aOx:ui2lp2axTNmsyakw9tvNnw
protocol: https
hosts: [ localhost:5601 ]
timeout: 30s
agent:
id: fleet-agent-id
logging.level: error
host:
id: host-agent-id

outputs:
default:
type: elasticsearch
hosts: [127.0.0.1:9200, 127.0.0.1:9300]
username: elastic
password: changeme
api_key: TiNAGG4BaaMdaH1tRfuU:KnR6yE41RrSowb0kQ0HWoA
ca_sha256: 7HIpactkIAq2Y49orFOOQKurWxmmSFZhBCoQYcRhJ3Y=

inputs:
- id: endpoint-id
type: endpoint
name: endpoint-1
enabled: true
package:
name: endpoint
version: 0.3.0
data_stream:
namespace: default
artifact_manifest:
schema_version: v22
manifest_version: v21
artifacts:
- endpoint-allowlist-windows:
sha256: 1234
size: 2
url: /relative/path/to/endpoint-allowlist-windows
- endpoint-allowlist-macos:
sha256: 1234
size: 2
url: /relative/path/to/endpoint-allowlist-macos
- endpoint-allowlist-linux:
sha256: 1234
size: 2
url: /relative/path/to/endpoint-allowlist-linux
policy:
linux:
advanced:
free-form: free-form-value
indices:
network: logs-endpoint.events.network-default
file: logs-endpoint.events.file-default
process: logs-endpoint.events.process-default
metadata: metrics-endpoint.metadata-default
policy: metrics-endpoint.policy-default
telemetry: metrics-endpoint.telemetry-default
logging:
file: info
stdout: debug
events:
process: true
file: true
network: true
windows:
malware:
mode: prevent
advanced:
free-form: free-form-value
indices:
network: logs-endpoint.events.network-default
file: logs-endpoint.events.file-default
registry: logs-endpoint.events.registry-default
process: logs-endpoint.events.process-default
driver: logs-endpoint.events.driver-default
library: logs-endpoint.events.library-default
alerts: logs-endpoint.alerts-default
metadata: metrics-endpoint.metadata-default
policy: metrics-endpoint.policy-default
telemetry: metrics-endpoint.telemetry-default
logging:
file: info
stdout: debug
events:
registry: true
process: true
security: true
file: true
dns: false
dll_and_driver_load: false
network: true
mac:
malware:
mode: prevent
advanced:
free-form: free-form-value
indices:
network: logs-endpoint.events.network-default
file: logs-endpoint.events.file-default
process: logs-endpoint.events.process-default
alerts: logs-endpoint.alerts-default
metadata: metrics-endpoint.metadata-default
policy: metrics-endpoint.policy-default
telemetry: metrics-endpoint.telemetry-default
logging:
file: info
stdout: debug
events:
process: true
file: true
network: true
runtime:
arch: x86_64
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Fleet Server Only
fleet:
enabled: true
access_api_key: VuaCfGcBCdbkQm-e5aOx:ui2lp2axTNmsyakw9tvNnw
protocol: https
hosts: [ localhost:5601 ]
timeout: 30s
agent:
id: fleet-agent-id
logging.level: error
host:
id: host-agent-id
server:
host: 127.0.0.1
port: 8822
ssl:
verification_mode: none
policy:
id: copy-policy-id
output:
elasticsearch:
hosts: [ 127.0.0.1:9200, 127.0.0.1:9300 ]
username: fleet
password: fleetpassword

outputs:
default:
type: elasticsearch
hosts: [127.0.0.1:9200, 127.0.0.1:9300]
username: elastic
password: changeme
api_key: TiNAGG4BaaMdaH1tRfuU:KnR6yE41RrSowb0kQ0HWoA
ca_sha256: 7HIpactkIAq2Y49orFOOQKurWxmmSFZhBCoQYcRhJ3Y=

inputs:
- id: fleet-server-id
type: fleet-server
use_output: default
data_stream:
namespace: default
name: fleet_server-1
revision: 6
server:
host: 0.0.0.0
port: 8220
limits:
max_connections: 40
runtime:
gc_percent: 50
timeouts:
read: 5m
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
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
Loading