Resolves #45220 (one of several PRs to achieve removing "testing" package as dependency in production binary) ## Testing - [x] QA'd all new/changed functionality manually. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Tests** * Switched many tests to use a dedicated MySQL test helper package and consolidated test-only utilities for datastore setup, cleanup, ad‑hoc SQL, certificate generation, and activity/aggregation helpers. * Added expanded test utilities for replication, DB connections and test data seeding to improve integration-test reliability. * **Chores** * No production behavior or user-facing APIs were changed. <!-- review_stack_entry_start --> [](https://app.coderabbit.ai/change-stack/fleetdm/fleet/pull/45406) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
71 lines
2.2 KiB
Go
71 lines
2.2 KiB
Go
package testing_utils
|
|
|
|
import (
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/asn1"
|
|
"math/big"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/dev_mode"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// TestingT is the subset of *testing.T that this package needs. It combines
|
|
// what testify's require functions require (Errorf, FailNow) with t.Cleanup
|
|
// and t.Setenv used by dev_mode.SetOverride. *testing.T satisfies it without
|
|
// an explicit conversion, so callers continue to pass `t` as-is. Defining
|
|
// this interface locally keeps the "testing" package out of this package's
|
|
// production import graph.
|
|
type TestingT interface {
|
|
Errorf(format string, args ...any)
|
|
FailNow()
|
|
Cleanup(f func())
|
|
Setenv(key, value string)
|
|
}
|
|
|
|
func NewTestMDMAppleCertTemplate() *x509.Certificate {
|
|
return &x509.Certificate{
|
|
SerialNumber: big.NewInt(1),
|
|
Subject: pkix.Name{
|
|
Organization: []string{"Test Org"},
|
|
ExtraNames: []pkix.AttributeTypeAndValue{
|
|
{
|
|
Type: asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 1},
|
|
Value: "com.apple.mgmt.Example",
|
|
},
|
|
},
|
|
},
|
|
NotBefore: time.Now(),
|
|
NotAfter: time.Now().Add(365 * 24 * time.Hour),
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
BasicConstraintsValid: true,
|
|
}
|
|
}
|
|
|
|
// StartNewAppleGDMFTestServer creates a new test server that serves the GDMF data from the testdata
|
|
// file. It also sets the necessary dev mode overrides to point to the test server and disable
|
|
// caching. It closes the server and clears the underlying overrides when the test finishes.
|
|
func StartNewAppleGDMFTestServer(t TestingT) {
|
|
_, thisFile, _, _ := runtime.Caller(0)
|
|
gdmfTestDataPath := filepath.Join(filepath.Dir(thisFile), "../apple/gdmf/testdata/gdmf.json")
|
|
|
|
appleGDMFSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
// load the test data from the file
|
|
b, err := os.ReadFile(gdmfTestDataPath)
|
|
require.NoError(t, err)
|
|
_, err = w.Write(b)
|
|
require.NoError(t, err)
|
|
}))
|
|
t.Cleanup(appleGDMFSrv.Close)
|
|
|
|
dev_mode.SetOverride("FLEET_DEV_GDMF_URL", appleGDMFSrv.URL, t)
|
|
}
|