mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 13:37:30 +00:00
Fixes #31444 The changes are primarily in tests. The only changes in production code are a couple validations/checks for invalid values in: - mysql/apple_mdm.go - mysql/hosts.go - mysql/queries.go # Checklist for submitter If some of the following don't apply, delete the relevant line. - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved handling of timestamp and default values across various features to prevent database errors and warnings. * Enhanced validation and data consistency for Apple Business Manager tokens and MDM profiles. * Updated test data and logic to comply with stricter database constraints and realistic scenarios, including date handling and field lengths. * **Chores** * Updated test setups to reflect schema changes, improve data integrity, and avoid future compatibility issues. * Standardized SQL mode and timestamp usage in test environments. * Refined test data for VPP apps, software installers, and device enrollments for better reliability. * **Tests** * Expanded and updated tests to cover new fields, stricter validation, and more accurate simulation of real-world conditions. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
54 lines
2.2 KiB
Go
54 lines
2.2 KiB
Go
package mysql
|
|
|
|
import (
|
|
"os"
|
|
"path"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/datastore/mysql/common_mysql"
|
|
"github.com/fleetdm/fleet/v4/server/datastore/mysql/common_mysql/testing_utils"
|
|
"github.com/go-kit/log"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// Android MySQL testing utilities. This file should contain VERY LITTLE code since it is also compiled into the production binary.
|
|
// Whenever possible, new code should go into a dedicated testing package (e.g. mdm/android/mysql/tests/testing_utils.go).
|
|
// These utilities are used to create a MySQL Datastore for testing the Android MDM MySQL implementation.
|
|
// They are located in the same package as the implementation to prevent a circular dependency. If put it in a different package,
|
|
// the circular dependency would be: mysql -> testing_utils -> mysql
|
|
|
|
func CreateMySQLDS(t testing.TB) *Datastore {
|
|
return createMySQLDSWithOptions(t, nil)
|
|
}
|
|
|
|
func createMySQLDSWithOptions(t testing.TB, opts *testing_utils.DatastoreTestOptions) *Datastore {
|
|
cleanTestName, opts := testing_utils.ProcessOptions(t, opts)
|
|
ds := InitializeDatabase(t, cleanTestName, opts)
|
|
t.Cleanup(func() { Close(ds) })
|
|
return ds
|
|
}
|
|
|
|
// InitializeDatabase loads the dumped schema into a newly created database in MySQL.
|
|
// This is much faster than running the full set of migrations on each test.
|
|
func InitializeDatabase(t testing.TB, testName string, opts *testing_utils.DatastoreTestOptions) *Datastore {
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
schemaPath := path.Join(path.Dir(filename), "schema.sql")
|
|
testing_utils.LoadSchema(t, testName, opts, schemaPath)
|
|
return connectMySQL(t, testName)
|
|
}
|
|
|
|
func connectMySQL(t testing.TB, testName string) *Datastore {
|
|
// Import TestSQLMode from main MySQL testing utils to ensure consistent SQL modes across all tests
|
|
// This ensures Android tests catch the same data integrity issues as other MySQL tests
|
|
dbWriter, err := common_mysql.NewDB(testing_utils.MysqlTestConfig(testName), &common_mysql.DBOptions{
|
|
SqlMode: common_mysql.TestSQLMode,
|
|
}, "")
|
|
require.NoError(t, err)
|
|
ds := New(log.NewLogfmtLogger(os.Stdout), dbWriter, dbWriter)
|
|
return ds.(*Datastore)
|
|
}
|
|
|
|
func Close(ds *Datastore) {
|
|
_ = ds.primary.Close()
|
|
}
|