diff --git a/agent/client_test.go b/agent/client_test.go index f7df455f5..dc5de2e2f 100644 --- a/agent/client_test.go +++ b/agent/client_test.go @@ -236,8 +236,11 @@ func TestWebSocketClient_TLSVerification(t *testing.T) { t.Run("custom CA trusts self-signed certificate", func(t *testing.T) { systemRoots, err := x509.SystemCertPool() require.NoError(t, err) + require.True(t, systemRoots.AppendCertsFromPEM(serverCertPEM)) client := newClient(t, caCertFile) - assert.Greater(t, len(client.getOptions().TlsConfig.RootCAs.Subjects()), len(systemRoots.Subjects())) + tlsConfig := client.getOptions().TlsConfig + require.NotNil(t, tlsConfig) + assert.True(t, tlsConfig.RootCAs.Equal(systemRoots)) conn, _, err := gws.NewClient(&gws.BuiltinEventHandler{}, client.getOptions()) require.NoError(t, err) require.NoError(t, conn.NetConn().Close()) diff --git a/agent/disk_test.go b/agent/disk_test.go index b36f91b41..8ee0c12d1 100644 --- a/agent/disk_test.go +++ b/agent/disk_test.go @@ -7,7 +7,6 @@ import ( "os" "path/filepath" "runtime" - "strings" "testing" "time" @@ -770,82 +769,6 @@ func TestIsDockerSpecialMountpoint(t *testing.T) { } } -func TestInitializeDiskInfoWithCustomNames(t *testing.T) { - // Test with custom names - t.Setenv("EXTRA_FILESYSTEMS", "sda1__my-storage,/dev/sdb1__backup-drive,nvme0n1p2") - - // Mock disk partitions (we'll just test the parsing logic) - // Since the actual disk operations are system-dependent, we'll focus on the parsing - testCases := []struct { - envValue string - expectedFs []string - expectedNames map[string]string - }{ - { - envValue: "sda1__my-storage,sdb1__backup-drive", - expectedFs: []string{"sda1", "sdb1"}, - expectedNames: map[string]string{ - "sda1": "my-storage", - "sdb1": "backup-drive", - }, - }, - { - envValue: "sda1,nvme0n1p2__fast-ssd", - expectedFs: []string{"sda1", "nvme0n1p2"}, - expectedNames: map[string]string{ - "nvme0n1p2": "fast-ssd", - }, - }, - } - - for _, tc := range testCases { - t.Run("env_"+tc.envValue, func(t *testing.T) { - t.Setenv("EXTRA_FILESYSTEMS", tc.envValue) - - // Create mock partitions that would match our test cases - partitions := []disk.PartitionStat{} - for _, fs := range tc.expectedFs { - if strings.HasPrefix(fs, "/dev/") { - partitions = append(partitions, disk.PartitionStat{ - Device: fs, - Mountpoint: fs, - }) - } else { - partitions = append(partitions, disk.PartitionStat{ - Device: "/dev/" + fs, - Mountpoint: "/" + fs, - }) - } - } - - // Test the parsing logic by calling the relevant part - // We'll create a simplified version to test just the parsing - extraFilesystems := tc.envValue - for fsEntry := range strings.SplitSeq(extraFilesystems, ",") { - // Parse the entry - fsEntry = strings.TrimSpace(fsEntry) - var fs, customName string - if parts := strings.SplitN(fsEntry, "__", 2); len(parts) == 2 { - fs = strings.TrimSpace(parts[0]) - customName = strings.TrimSpace(parts[1]) - } else { - fs = fsEntry - } - - // Verify the device is in our expected list - assert.Contains(t, tc.expectedFs, fs, "parsed device should be in expected list") - - // Check if custom name should exist - if expectedName, exists := tc.expectedNames[fs]; exists { - assert.Equal(t, expectedName, customName, "custom name should match expected") - } else { - assert.Empty(t, customName, "custom name should be empty when not expected") - } - } - }) - } -} - func TestFsStatsWithCustomNames(t *testing.T) { // Test that FsStats properly stores custom names fsStats := &system.FsStats{ diff --git a/internal/alerts/alerts_api_test.go b/internal/alerts/alerts_api_test.go index 44a931d77..4897b1811 100644 --- a/internal/alerts/alerts_api_test.go +++ b/internal/alerts/alerts_api_test.go @@ -20,6 +20,7 @@ import ( "github.com/pocketbase/pocketbase/core" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // marshal to json and return an io.Reader (for use in ApiScenario.Body) @@ -184,7 +185,7 @@ func TestUserAlertsApi(t *testing.T) { "min": 10, }), BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) { - beszelTests.ClearCollection(t, app, "alerts") + require.NoError(t, beszelTests.ClearCollection(t, app, "alerts")) }, AfterTestFunc: func(t testing.TB, app *pbTests.TestApp, res *http.Response) { alerts, _ := app.CountRecords("alerts") @@ -209,7 +210,7 @@ func TestUserAlertsApi(t *testing.T) { "overwrite": false, }), BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) { - beszelTests.ClearCollection(t, app, "alerts") + require.NoError(t, beszelTests.ClearCollection(t, app, "alerts")) beszelTests.CreateRecord(app, "alerts", map[string]any{ "name": "CPU", "system": system1.Id, @@ -243,7 +244,7 @@ func TestUserAlertsApi(t *testing.T) { "overwrite": true, }), BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) { - beszelTests.ClearCollection(t, app, "alerts") + require.NoError(t, beszelTests.ClearCollection(t, app, "alerts")) beszelTests.CreateRecord(app, "alerts", map[string]any{ "name": "CPU", "system": system2.Id, @@ -271,7 +272,7 @@ func TestUserAlertsApi(t *testing.T) { "systems": []string{system1.Id}, }), BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) { - beszelTests.ClearCollection(t, app, "alerts") + require.NoError(t, beszelTests.ClearCollection(t, app, "alerts")) beszelTests.CreateRecord(app, "alerts", map[string]any{ "name": "CPU", "system": system1.Id, @@ -300,7 +301,7 @@ func TestUserAlertsApi(t *testing.T) { "systems": []string{system1.Id}, }), BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) { - beszelTests.ClearCollection(t, app, "alerts") + require.NoError(t, beszelTests.ClearCollection(t, app, "alerts")) beszelTests.CreateRecord(app, "alerts", map[string]any{ "name": "CPU", "system": system1.Id, @@ -329,7 +330,7 @@ func TestUserAlertsApi(t *testing.T) { "systems": []string{system1.Id, system2.Id}, }), BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) { - beszelTests.ClearCollection(t, app, "alerts") + require.NoError(t, beszelTests.ClearCollection(t, app, "alerts")) for _, systemId := range []string{system1.Id, system2.Id} { _, err := beszelTests.CreateRecord(app, "alerts", map[string]any{ "name": "Memory", @@ -363,7 +364,7 @@ func TestUserAlertsApi(t *testing.T) { "systems": []string{system1.Id}, }), BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) { - beszelTests.ClearCollection(t, app, "alerts") + require.NoError(t, beszelTests.ClearCollection(t, app, "alerts")) beszelTests.CreateRecord(app, "alerts", map[string]any{ "name": "CPU", "system": system1.Id, "user": user2.Id, "value": 80, }) @@ -388,7 +389,7 @@ func TestUserAlertsApi(t *testing.T) { "systems": []string{system2.Id}, }), BeforeTestFunc: func(t testing.TB, app *pbTests.TestApp, e *core.ServeEvent) { - beszelTests.ClearCollection(t, app, "alerts") + require.NoError(t, beszelTests.ClearCollection(t, app, "alerts")) for _, user := range []string{user1.Id, user2.Id} { beszelTests.CreateRecord(app, "alerts", map[string]any{ "name": "CPU", @@ -430,19 +431,21 @@ func TestSendTestNotification(t *testing.T) { localURL := "generic+" + server.URL readonlyUser, err := beszelTests.CreateUserWithRole(hub, "readonly@example.com", "password123", "readonly") - assert.NoError(t, err) + require.NoError(t, err) readonlyToken, err := readonlyUser.NewAuthToken() - assert.NoError(t, err) + require.NoError(t, err) userToken, err := user.NewAuthToken() + require.NoError(t, err, "Failed to create user auth token") adminUser, err := beszelTests.CreateUserWithRole(hub, "admin@example.com", "password123", "admin") - assert.NoError(t, err, "Failed to create admin user") + require.NoError(t, err, "Failed to create admin user") adminUserToken, err := adminUser.NewAuthToken() + require.NoError(t, err, "Failed to create admin auth token") superuser, err := beszelTests.CreateSuperuser(hub, "superuser@example.com", "password123") - assert.NoError(t, err, "Failed to create superuser") + require.NoError(t, err, "Failed to create superuser") superuserToken, err := superuser.NewAuthToken() - assert.NoError(t, err, "Failed to create superuser auth token") + require.NoError(t, err, "Failed to create superuser auth token") testAppFactory := func(t testing.TB) *pbTests.TestApp { return hub.TestApp diff --git a/internal/hub/api_test.go b/internal/hub/api_test.go index cbb08a12e..639ca284e 100644 --- a/internal/hub/api_test.go +++ b/internal/hub/api_test.go @@ -99,6 +99,7 @@ func TestApiRoutesAuthentication(t *testing.T) { adminUser, err := beszelTests.CreateUserWithRole(hub, "admin@example.com", "password123", "admin") require.NoError(t, err, "Failed to create admin user") adminUserToken, err := adminUser.NewAuthToken() + require.NoError(t, err, "Failed to create admin auth token") readOnlyUser, err := beszelTests.CreateUserWithRole(hub, "readonly@example.com", "password123", "readonly") require.NoError(t, err, "Failed to create readonly user") diff --git a/internal/records/records_deletion_test.go b/internal/records/records_deletion_test.go index 5caece64a..77255119a 100644 --- a/internal/records/records_deletion_test.go +++ b/internal/records/records_deletion_test.go @@ -318,6 +318,7 @@ func TestDeleteOldAlertsHistoryEdgeCases(t *testing.T) { "status": "up", "users": []string{user.Id}, }) + require.NoError(t, err) // Create only 5 alerts (well below threshold) for i := range 5 { diff --git a/internal/tests/hub.go b/internal/tests/hub.go index c68587e6b..48eb40637 100644 --- a/internal/tests/hub.go +++ b/internal/tests/hub.go @@ -112,6 +112,9 @@ func CreateRecord(app core.App, collectionName string, fields map[string]any) (* func ClearCollection(t testing.TB, app core.App, collectionName string) error { _, err := app.DB().NewQuery(fmt.Sprintf("DELETE from %s", collectionName)).Execute() + if err != nil { + return err + } recordCount, err := app.CountRecords(collectionName) assert.EqualValues(t, recordCount, 0, "should have 0 records after clearing") return err diff --git a/internal/tests/hub_test.go b/internal/tests/hub_test.go new file mode 100644 index 000000000..dd791d9f1 --- /dev/null +++ b/internal/tests/hub_test.go @@ -0,0 +1,26 @@ +//go:build testing + +package tests + +import ( + "testing" + + "github.com/pocketbase/pocketbase/core" + "github.com/stretchr/testify/require" +) + +func TestClearCollection(t *testing.T) { + hub, err := NewTestHub(t.TempDir()) + require.NoError(t, err) + defer hub.Cleanup() + + _, err = CreateUser(hub, "clear@example.com", "password123") + require.NoError(t, err) + require.NoError(t, ClearCollection(t, hub, "users")) + + // An empty view can be counted, but SQLite rejects DELETE against it. + view := core.NewViewCollection("empty_users") + view.ViewQuery = "SELECT id FROM users" + require.NoError(t, hub.Save(view)) + require.ErrorContains(t, ClearCollection(t, hub, "empty_users"), "cannot modify empty_users because it is a view") +}