|
1 | 1 | package energyimpact |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | _ "embed" |
5 | 6 | "errors" |
6 | 7 | "testing" |
7 | 8 |
|
8 | 9 | "github.com/macadmins/osquery-extension/pkg/utils" |
| 10 | + "github.com/osquery/osquery-go/plugin/table" |
9 | 11 | "github.com/stretchr/testify/assert" |
10 | 12 | ) |
11 | 13 |
|
12 | 14 | //go:embed test_powermetrics_output.plist |
13 | 15 | var testPlist string |
14 | 16 |
|
| 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 | + |
15 | 62 | func TestRunPowermetrics(t *testing.T) { |
16 | 63 | 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) |
24 | 71 | }{ |
25 | 72 | { |
26 | 73 | name: "Binary not present", |
|
0 commit comments