chore(inputs.win_wmi): Cleanup unit-tests (#14955)
This commit is contained in:
parent
99b15da330
commit
e8efe4e7b7
|
|
@ -13,44 +13,60 @@ import (
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// initialize test data
|
|
||||||
var sysDrive = os.Getenv("SystemDrive") + `\` // C:\
|
var sysDrive = os.Getenv("SystemDrive") + `\` // C:\
|
||||||
|
|
||||||
// include Name as a tag, FreeSpace as a field, and Purpose as a known-null class property
|
func TestBuildWqlStatements(t *testing.T) {
|
||||||
var testQuery = Query{
|
plugin := &Wmi{
|
||||||
Namespace: "ROOT\\cimv2",
|
Queries: []Query{
|
||||||
ClassName: "Win32_Volume",
|
{
|
||||||
Properties: []string{"Name", "FreeSpace", "Purpose"},
|
Namespace: "ROOT\\cimv2",
|
||||||
//nolint:gocritic // sprintfQuotedString - "%s" used by purpose, string escaping is done by special function
|
ClassName: "Win32_Volume",
|
||||||
Filter: fmt.Sprintf(`NOT Name LIKE "\\\\?\\%%" AND Name LIKE "%s"`, regexp.QuoteMeta(sysDrive)),
|
Properties: []string{"Name", "FreeSpace", "Purpose"},
|
||||||
TagPropertiesInclude: []string{"Name"},
|
//nolint:gocritic // sprintfQuotedString - "%s" used by purpose, string escaping is done by special function
|
||||||
tagFilter: nil, // this is filled in by CompileInputs()
|
Filter: fmt.Sprintf(`NOT Name LIKE "\\\\?\\%%" AND Name LIKE "%s"`, regexp.QuoteMeta(sysDrive)),
|
||||||
}
|
TagPropertiesInclude: []string{"Name"},
|
||||||
|
},
|
||||||
//nolint:gocritic // sprintfQuotedString - "%s" used by purpose, string escaping is done by special function
|
},
|
||||||
var expectedWql = fmt.Sprintf(
|
Log: testutil.Logger{},
|
||||||
`SELECT Name, FreeSpace, Purpose FROM Win32_Volume WHERE NOT Name LIKE "\\\\?\\%%" AND Name LIKE "%s"`,
|
|
||||||
regexp.QuoteMeta(sysDrive),
|
|
||||||
)
|
|
||||||
|
|
||||||
// test buildWqlStatements
|
|
||||||
func TestWmi_buildWqlStatements(t *testing.T) {
|
|
||||||
var logger = new(testutil.Logger)
|
|
||||||
plugin := Wmi{Queries: []Query{testQuery}, Log: logger}
|
|
||||||
require.NoError(t, compileInputs(&plugin))
|
|
||||||
require.Equal(t, expectedWql, plugin.Queries[0].query)
|
|
||||||
}
|
|
||||||
|
|
||||||
// test DoQuery
|
|
||||||
func TestWmi_DoQuery(t *testing.T) {
|
|
||||||
var logger = new(testutil.Logger)
|
|
||||||
var acc = new(testutil.Accumulator)
|
|
||||||
plugin := Wmi{Queries: []Query{testQuery}, Log: logger}
|
|
||||||
require.NoError(t, compileInputs(&plugin))
|
|
||||||
for _, q := range plugin.Queries {
|
|
||||||
require.NoError(t, q.doQuery(acc))
|
|
||||||
}
|
}
|
||||||
// no errors in accumulator
|
require.NoError(t, plugin.Init())
|
||||||
|
require.NotEmpty(t, plugin.Queries)
|
||||||
|
|
||||||
|
//nolint:gocritic // sprintfQuotedString - "%s" used by purpose, string escaping is done by special function
|
||||||
|
expected := fmt.Sprintf(
|
||||||
|
`SELECT Name, FreeSpace, Purpose FROM Win32_Volume WHERE NOT Name LIKE "\\\\?\\%%" AND Name LIKE "%s"`,
|
||||||
|
regexp.QuoteMeta(sysDrive),
|
||||||
|
)
|
||||||
|
require.Equal(t, expected, plugin.Queries[0].query)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInit(t *testing.T) {
|
||||||
|
plugin := &Wmi{}
|
||||||
|
require.NoError(t, plugin.Init())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQuery(t *testing.T) {
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping integration test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin := &Wmi{
|
||||||
|
Queries: []Query{
|
||||||
|
{
|
||||||
|
Namespace: "ROOT\\cimv2",
|
||||||
|
ClassName: "Win32_Volume",
|
||||||
|
Properties: []string{"Name", "FreeSpace", "Purpose"},
|
||||||
|
//nolint:gocritic // sprintfQuotedString - "%s" used by purpose, string escaping is done by special function
|
||||||
|
Filter: fmt.Sprintf(`NOT Name LIKE "\\\\?\\%%" AND Name LIKE "%s"`, regexp.QuoteMeta(sysDrive)),
|
||||||
|
TagPropertiesInclude: []string{"Name"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Log: testutil.Logger{},
|
||||||
|
}
|
||||||
|
require.NoError(t, plugin.Init())
|
||||||
|
|
||||||
|
var acc testutil.Accumulator
|
||||||
|
require.NoError(t, plugin.Gather(&acc))
|
||||||
require.Empty(t, acc.Errors)
|
require.Empty(t, acc.Errors)
|
||||||
// Only one metric was returned (because we filtered for SystemDrive)
|
// Only one metric was returned (because we filtered for SystemDrive)
|
||||||
require.Len(t, acc.Metrics, 1)
|
require.Len(t, acc.Metrics, 1)
|
||||||
|
|
@ -59,21 +75,3 @@ func TestWmi_DoQuery(t *testing.T) {
|
||||||
// FreeSpace property was collected as a field
|
// FreeSpace property was collected as a field
|
||||||
require.NotEmpty(t, acc.Metrics[0].Fields["FreeSpace"])
|
require.NotEmpty(t, acc.Metrics[0].Fields["FreeSpace"])
|
||||||
}
|
}
|
||||||
|
|
||||||
// test Init function
|
|
||||||
func TestWmi_Init(t *testing.T) {
|
|
||||||
var logger = new(testutil.Logger)
|
|
||||||
plugin := Wmi{Queries: []Query{testQuery}, Log: logger}
|
|
||||||
require.NoError(t, plugin.Init())
|
|
||||||
}
|
|
||||||
|
|
||||||
// test Gather function
|
|
||||||
func TestWmi_Gather(t *testing.T) {
|
|
||||||
var logger = new(testutil.Logger)
|
|
||||||
var acc = new(testutil.Accumulator)
|
|
||||||
plugin := Wmi{Queries: []Query{testQuery}, Log: logger}
|
|
||||||
require.NoError(t, plugin.Init())
|
|
||||||
require.NoError(t, plugin.Gather(acc))
|
|
||||||
// no errors in accumulator
|
|
||||||
require.Empty(t, acc.Errors)
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue