Skip to content

Commit 0adbedc

Browse files
committed
More unit tests
1 parent bf46c25 commit 0adbedc

2 files changed

Lines changed: 55 additions & 7 deletions

File tree

tables/energyimpact/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ go_test(
2020
embedsrcs = ["test_powermetrics_output.plist"],
2121
deps = [
2222
"//pkg/utils",
23+
"@com_github_osquery_osquery_go//plugin/table",
2324
"@com_github_stretchr_testify//assert",
2425
],
2526
)

tables/energyimpact/energy_impact_test.go

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,73 @@
11
package energyimpact
22

33
import (
4+
"context"
45
_ "embed"
56
"errors"
67
"testing"
78

89
"github.com/macadmins/osquery-extension/pkg/utils"
10+
"github.com/osquery/osquery-go/plugin/table"
911
"github.com/stretchr/testify/assert"
1012
)
1113

1214
//go:embed test_powermetrics_output.plist
1315
var testPlist string
1416

17+
func TestEnergyImpactColumns(t *testing.T) {
18+
columns := EnergyImpactColumns()
19+
20+
// Should return 20 columns
21+
assert.Len(t, columns, 20)
22+
23+
// Verify column names exist
24+
columnNames := make(map[string]bool)
25+
for _, col := range columns {
26+
columnNames[col.Name] = true
27+
}
28+
29+
expectedColumns := []string{
30+
"pid", "name", "energy_impact", "energy_impact_per_s",
31+
"cputime_ns", "cputime_ms_per_s", "cputime_userland_ratio",
32+
"intr_wakeups", "intr_wakeups_per_s", "idle_wakeups", "idle_wakeups_per_s",
33+
"diskio_bytesread", "diskio_bytesread_per_s",
34+
"diskio_byteswritten", "diskio_byteswritten_per_s",
35+
"packets_received", "packets_sent", "bytes_received", "bytes_sent",
36+
"interval",
37+
}
38+
39+
for _, name := range expectedColumns {
40+
assert.True(t, columnNames[name], "Expected column %s not found", name)
41+
}
42+
}
43+
44+
func TestEnergyImpactGenerate(t *testing.T) {
45+
// This test verifies that the generate function works with the default context
46+
// Since it requires actual powermetrics execution, we only test the function signature
47+
ctx := context.Background()
48+
queryContext := table.QueryContext{
49+
Constraints: make(map[string]table.ConstraintList),
50+
}
51+
52+
// Call the function - it may return empty results if powermetrics isn't available
53+
// or require root, but it shouldn't panic
54+
results, err := EnergyImpactGenerate(ctx, queryContext)
55+
56+
// The function should return without panicking
57+
// Results may be empty if not running as root or powermetrics doesn't exist
58+
assert.NotNil(t, results)
59+
_ = err // Error is acceptable if powermetrics can't run
60+
}
61+
1562
func TestRunPowermetrics(t *testing.T) {
1663
tests := []struct {
17-
name string
18-
mockCmd utils.MockCmdRunner
19-
fileExist bool
20-
interval int
21-
wantErr bool
22-
wantTasks int
23-
checkFirst func(t *testing.T, tasks []task)
64+
name string
65+
mockCmd utils.MockCmdRunner
66+
fileExist bool
67+
interval int
68+
wantErr bool
69+
wantTasks int
70+
checkFirst func(t *testing.T, tasks []task)
2471
}{
2572
{
2673
name: "Binary not present",

0 commit comments

Comments
 (0)