Skip to content

Commit

Permalink
Tests for config input
Browse files Browse the repository at this point in the history
  • Loading branch information
chemamartinez committed Nov 28, 2023
1 parent 335d366 commit 0e34d50
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions x-pack/filebeat/input/etw/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

//go:build windows

package etw

import (
"testing"

"github.com/stretchr/testify/assert"

confpkg "github.com/elastic/elastic-agent-libs/config"
)

func Test_validateConfig(t *testing.T) {
testCases := []struct {
name string // Sub-test name.
config config // Load config parameters.
wantError string // Expected error
}{
{
name: "valid config",
config: config{
ProviderName: "Microsoft-Windows-DNSServer",
SessionName: "MySession-DNSServer",
TraceLevel: "verbose",
MatchAnyKeyword: 0xffffffffffffffff,
MatchAllKeyword: 0,
},
},
{
name: "minimal config",
config: config{
ProviderName: "Microsoft-Windows-DNSServer",
},
},
{
name: "missing source config",
config: config{
TraceLevel: "verbose",
MatchAnyKeyword: 0xffffffffffffffff,
},
wantError: "provider, existing logfile or running session must be set",
},
{
name: "invalid trace level",
config: config{
ProviderName: "Microsoft-Windows-DNSServer",
TraceLevel: "failed",
MatchAnyKeyword: 0xffffffffffffffff,
},
wantError: "invalid Trace Level value 'failed'",
},
{
name: "conflict provider GUID and name",
config: config{
ProviderGUID: "{eb79061a-a566-4698-1234-3ed2807033a0}",
ProviderName: "Microsoft-Windows-DNSServer",
TraceLevel: "verbose",
MatchAnyKeyword: 0xffffffffffffffff,
},
wantError: "configuration constraint error: provider GUID and provider name cannot be defined together",
},
{
name: "conflict provider GUID and logfile",
config: config{
ProviderGUID: "{eb79061a-a566-4698-1234-3ed2807033a0}",
Logfile: "C:\\Windows\\System32\\winevt\\File.etl",
TraceLevel: "verbose",
MatchAnyKeyword: 0xffffffffffffffff,
},
wantError: "configuration constraint error: provider GUID and file cannot be defined together",
},
{
name: "conflict provider GUID and session",
config: config{
ProviderGUID: "{eb79061a-a566-4698-1234-3ed2807033a0}",
Session: "EventLog-Application",
TraceLevel: "verbose",
MatchAnyKeyword: 0xffffffffffffffff,
},
wantError: "configuration constraint error: provider GUID and existing session cannot be defined together",
},
{
name: "conflict provider name and logfile",
config: config{
ProviderName: "Microsoft-Windows-DNSServer",
Logfile: "C:\\Windows\\System32\\winevt\\File.etl",
TraceLevel: "verbose",
MatchAnyKeyword: 0xffffffffffffffff,
},
wantError: "configuration constraint error: provider name and file cannot be defined together",
},
{
name: "conflict provider name and session",
config: config{
ProviderName: "Microsoft-Windows-DNSServer",
Session: "EventLog-Application",
TraceLevel: "verbose",
MatchAnyKeyword: 0xffffffffffffffff,
},
wantError: "configuration constraint error: provider name and existing session cannot be defined together",
},
{
name: "conflict logfile and session",
config: config{
Logfile: "C:\\Windows\\System32\\winevt\\File.etl",
Session: "EventLog-Application",
TraceLevel: "verbose",
MatchAnyKeyword: 0xffffffffffffffff,
},
wantError: "configuration constraint error: file and existing session cannot be defined together",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
c := confpkg.MustNewConfigFrom(tc.config)
config := defaultConfig()
err := c.Unpack(&config)

// Validate responses
if assert.Error(t, err) {
assert.Contains(t, err.Error(), tc.wantError)
}
})
}
}

0 comments on commit 0e34d50

Please sign in to comment.