From fbc36ce87abb208835f0fed0c6d724668969fca8 Mon Sep 17 00:00:00 2001 From: Graham Gilbert Date: Wed, 13 May 2026 22:28:08 -0700 Subject: [PATCH 1/2] Improve Munki table tests --- tables/munki/munki_test.go | 98 ++++++++++++++++++++++++++++++++++---- 1 file changed, 90 insertions(+), 8 deletions(-) diff --git a/tables/munki/munki_test.go b/tables/munki/munki_test.go index f6bbba9..53889b5 100644 --- a/tables/munki/munki_test.go +++ b/tables/munki/munki_test.go @@ -3,12 +3,16 @@ package munki import ( "context" _ "embed" + "errors" "os" "path/filepath" "testing" + "time" + "github.com/macadmins/osquery-extension/pkg/utils" "github.com/osquery/osquery-go/plugin/table" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) //go:embed test_ManagedInstallReport.plist @@ -20,9 +24,19 @@ var testManagedInstallReportMunki7 []byte //go:embed test_ManagedInstallReport_with_pending.plist var testManagedInstallReportWithPending []byte -func TestMunkiInstallsGenerate(t *testing.T) { +func withReportPath(t *testing.T) string { + t.Helper() + original := reportPath reportPath = filepath.Join(t.TempDir(), "ManagedInstallReport.plist") - err := os.WriteFile(reportPath, testManagedInstallReport, 0600) + t.Cleanup(func() { + reportPath = original + }) + return reportPath +} + +func TestMunkiInstallsGenerate(t *testing.T) { + path := withReportPath(t) + err := os.WriteFile(path, testManagedInstallReport, 0600) if err != nil { t.Fatal(err) } @@ -54,8 +68,8 @@ func TestMunkiInstallsGenerate(t *testing.T) { } func TestMunkiInstallsGenerateMunki7(t *testing.T) { - reportPath = filepath.Join(t.TempDir(), "ManagedInstallReport.plist") - err := os.WriteFile(reportPath, testManagedInstallReportMunki7, 0600) + path := withReportPath(t) + err := os.WriteFile(path, testManagedInstallReportMunki7, 0600) if err != nil { t.Fatal(err) } @@ -78,8 +92,8 @@ func TestMunkiInstallsGenerateMunki7(t *testing.T) { } func TestMunkiInfoGenerateMunki7(t *testing.T) { - reportPath = filepath.Join(t.TempDir(), "ManagedInstallReport.plist") - err := os.WriteFile(reportPath, testManagedInstallReportMunki7, 0600) + path := withReportPath(t) + err := os.WriteFile(path, testManagedInstallReportMunki7, 0600) if err != nil { t.Fatal(err) } @@ -105,8 +119,8 @@ func TestMunkiInfoGenerateMunki7(t *testing.T) { } func TestMunkiInstallsGenerateWithPendingVersions(t *testing.T) { - reportPath = filepath.Join(t.TempDir(), "ManagedInstallReport.plist") - err := os.WriteFile(reportPath, testManagedInstallReportWithPending, 0600) + path := withReportPath(t) + err := os.WriteFile(path, testManagedInstallReportWithPending, 0600) if err != nil { t.Fatal(err) } @@ -150,3 +164,71 @@ func TestMunkiInstallsColumnsIncludesVersionToInstall(t *testing.T) { } assert.True(t, found, "MunkiInstallsColumns should include version_to_install") } + +func TestMunkiInfoColumns(t *testing.T) { + assert.Equal(t, []table.ColumnDefinition{ + table.TextColumn("version"), + table.TextColumn("start_time"), + table.TextColumn("end_time"), + table.TextColumn("success"), + table.TextColumn("errors"), + table.TextColumn("warnings"), + table.TextColumn("console_user"), + table.TextColumn("problem_installs"), + table.TextColumn("manifest_name"), + }, MunkiInfoColumns()) +} + +func TestMunkiInstallsColumns(t *testing.T) { + assert.Equal(t, []table.ColumnDefinition{ + table.TextColumn("installed_version"), + table.TextColumn("version_to_install"), + table.TextColumn("installed"), + table.TextColumn("name"), + table.TextColumn("end_time"), + table.TextColumn("display_name"), + }, MunkiInstallsColumns()) +} + +func TestMunkiInfoGenerateMissingReport(t *testing.T) { + withReportPath(t) + rows, err := MunkiInfoGenerate(context.Background(), table.QueryContext{}) + require.NoError(t, err) + assert.Nil(t, rows) +} + +func TestMunkiInstallsGenerateMissingReport(t *testing.T) { + withReportPath(t) + rows, err := MunkiInstallsGenerate(context.Background(), table.QueryContext{}) + require.NoError(t, err) + assert.Nil(t, rows) +} + +func TestLoadMunkiReportInvalidPlist(t *testing.T) { + path := withReportPath(t) + err := os.WriteFile(path, []byte("not plist"), 0600) + require.NoError(t, err) + + report, err := loadMunkiReport(utils.MockFileSystem{FileExists: true}) + assert.Error(t, err) + assert.NotNil(t, report) + assert.ErrorContains(t, err, "decode ManagedInstallReport plist") +} + +func TestMunkiDateUnmarshalInvalidString(t *testing.T) { + var md MunkiDate + err := md.UnmarshalPlist(func(v interface{}) error { + switch target := v.(type) { + case *time.Time: + return errors.New("not a date") + case *string: + *target = "not a date" + return nil + default: + return errors.New("unexpected target") + } + }) + assert.Error(t, err) + assert.ErrorContains(t, err, "unable to parse date") + assert.Empty(t, md.String()) +} From 2d5a549697a49dd0be5d9e97989117ecc87267d8 Mon Sep 17 00:00:00 2001 From: Graham Gilbert Date: Wed, 3 Jun 2026 11:20:34 -0700 Subject: [PATCH 2/2] Fix munki Bazel test deps --- tables/munki/BUILD.bazel | 1 + 1 file changed, 1 insertion(+) diff --git a/tables/munki/BUILD.bazel b/tables/munki/BUILD.bazel index ae18a06..36c4641 100644 --- a/tables/munki/BUILD.bazel +++ b/tables/munki/BUILD.bazel @@ -25,5 +25,6 @@ go_test( deps = [ "@com_github_osquery_osquery_go//plugin/table", "@com_github_stretchr_testify//assert", + "@com_github_stretchr_testify//require", ], )