mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-03 18:43:38 +01:00
Backend tests (api-0..4) finished last on post-#5350 Pulse Build and Test runs (46109093360, ~1,726 runner-seconds across 5 shards), with the slowest unclaimed internal/api tests executing isolated subtest matrices sequentially, seeding the full default public demo estate where compact mock topologies suffice, or bypassing TestMain's PULSE_TEST_BCRYPT_COST via direct bcrypt.DefaultCost calls. - Parallelize isolated subtest matrices across 14 unclaimed internal/api test files (host_agent_credential_handoff_test.go, agent_snapshot_authority_test.go, resource_read_authority_test.go, token_stream_lifetime_test.go, token_stream_shutdown_test.go, assistant_stream_lifetime_test.go, assistant_json_lifetime_test.go, agent_events_auth_test.go, reporting_boundary_test.go, docker_agents_report_size_test.go, security_setup_fixture_isolation_test.go, websocket_auth_test.go, security_token_withdrawal_test.go, and log_stream_auth_test.go), lifting shared env/session setup out of per-subtest helpers, serializing only the temporary AI alert-analysis binder swap during router construction, and caching read-only source file bytes once in reporting_boundary_test.go. - Use the existing compact 2-node mock integration fixture (newIntegrationServer) for TestStateEndpointReturnsMockData, TestConfigNodesUsesMockTopology, and TestMockModeToggleEndpoint in router_integration_test.go, while preserving newFleetIntegrationServer for multi-page recovery rollup/point tests. - Route four test fixtures (change_password_session_admin_test.go, oidc_only_admin_parity_test.go, platform_admin_session_parity_test.go, and security_status_capability_enforcement_test.go) through pkg/auth.HashPassword so they honor TestMain's PULSE_TEST_BCRYPT_COST instead of bcrypt.DefaultCost. Contract-Neutral: Test-only parallelization and fixture optimization across 19 internal/api test files with zero production code, API behavior, or assertion changes. Change-source: pulse-maintainer
189 lines
7.3 KiB
Go
189 lines
7.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"io/fs"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Exercise the production Go classifiers, not a second copy of their patterns.
|
|
// The shell classifier is exercised in scripts/repo-boundary-regression.py,
|
|
// where the boundary workflow supplies its declared ripgrep dependency.
|
|
// An exact shared-pipeline exemption must not admit neighbouring implementation
|
|
// files or let the exempt helper import private licensing code.
|
|
func TestReportingSubjectBoundaryExemptionIsExact(t *testing.T) {
|
|
binary, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := filepath.Join("..", "..")
|
|
type boundaryVariant struct {
|
|
name, file, content, goMarker string
|
|
}
|
|
variants := []boundaryVariant{
|
|
{name: "shared subject pipeline"},
|
|
{
|
|
name: "OIDC callback admission stays in tracked implementation", file: "oidc_callback_admission.go", content: "package api\n",
|
|
goMarker: "untracked paid-domain file oidc_callback_admission.go",
|
|
},
|
|
{
|
|
name: "OIDC callback admission has no prefix exemption", file: "oidc_callback_admission.go.extra.go", content: "package api\n",
|
|
goMarker: "untracked paid-domain file oidc_callback_admission.go.extra.go",
|
|
},
|
|
{
|
|
name: "SSO configuration adapter exemption is exact", file: "sso_config_preservation_extra.go", content: "package api\n",
|
|
goMarker: "untracked paid-domain file sso_config_preservation_extra.go",
|
|
},
|
|
{
|
|
name: "SSO configuration adapter private root import denied", file: "sso_config_preservation.go",
|
|
content: "package api\nimport _ \"github.com/rcourtman/pulse-go-rewrite/internal/license\"\n",
|
|
goMarker: "direct internal/license import",
|
|
},
|
|
{
|
|
name: "SSO configuration adapter private implementation import denied", file: "sso_config_preservation.go",
|
|
content: "package api\nimport _ \"github.com/rcourtman/pulse-go-rewrite/internal/license/entitlements\"\n",
|
|
goMarker: "direct internal/license import",
|
|
},
|
|
{
|
|
name: "SSO configuration adapter licensing bridge required", file: "sso_config_preservation.go",
|
|
content: "package api\nimport _ \"github.com/rcourtman/pulse-go-rewrite/pkg/licensing\"\n",
|
|
goMarker: "sso_config_preservation.go",
|
|
},
|
|
{
|
|
name: "activity observer exemption is exact", file: "reporting_activity_extra.go", content: "package api\n",
|
|
goMarker: "untracked paid-domain file reporting_activity_extra.go",
|
|
},
|
|
{
|
|
name: "activity observer private import denied", file: "reporting_activity.go",
|
|
content: "package api\nimport _ \"github.com/rcourtman/pulse-go-rewrite/internal/license\"\n",
|
|
goMarker: "direct internal/license import",
|
|
},
|
|
{
|
|
name: "activity observer licensing bridge required", file: "reporting_activity.go",
|
|
content: "package api\nimport _ \"github.com/rcourtman/pulse-go-rewrite/pkg/licensing\"\n",
|
|
goMarker: "reporting_activity.go",
|
|
},
|
|
{
|
|
name: "backup adapter exemption is exact", file: "reporting_backup_health_extra.go", content: "package api\n",
|
|
goMarker: "untracked paid-domain file reporting_backup_health_extra.go",
|
|
},
|
|
{
|
|
name: "backup adapter private import denied", file: "reporting_backup_health.go",
|
|
content: "package api\nimport _ \"github.com/rcourtman/pulse-go-rewrite/internal/license\"\n",
|
|
goMarker: "direct internal/license import",
|
|
},
|
|
{
|
|
name: "new reporting implementation", file: "reporting_subject_alerts_extra.go", content: "package api\n",
|
|
goMarker: "untracked paid-domain file reporting_subject_alerts_extra.go",
|
|
},
|
|
{
|
|
name: "exact path not prefix", file: "reporting_subject_alerts.go.extra.go", content: "package api\n",
|
|
goMarker: "untracked paid-domain file reporting_subject_alerts.go.extra.go",
|
|
},
|
|
{
|
|
name: "private root import still denied", file: "reporting_subject_alerts.go",
|
|
content: "package api\nimport _ \"github.com/rcourtman/pulse-go-rewrite/internal/license\"\n",
|
|
goMarker: "direct internal/license import",
|
|
},
|
|
{
|
|
name: "licensing bridge still required", file: "reporting_subject_alerts.go",
|
|
content: "package api\nimport _ \"github.com/rcourtman/pulse-go-rewrite/pkg/licensing\"\n",
|
|
goMarker: "reporting_subject_alerts.go",
|
|
},
|
|
}
|
|
for _, adapter := range []string{"sso_load_state", "sso_management_admission"} {
|
|
for _, variant := range []struct {
|
|
name, suffix, content, marker string
|
|
}{
|
|
{"exact path", "_extra.go", "package api\n", "untracked paid-domain file " + adapter + "_extra.go"},
|
|
{"not path prefix", ".go.extra.go", "package api\n", "untracked paid-domain file " + adapter + ".go.extra.go"},
|
|
{"private root import denied", ".go", "package api\nimport _ \"github.com/rcourtman/pulse-go-rewrite/internal/license\"\n", "direct internal/license import"},
|
|
{"private implementation import denied", ".go", "package api\nimport _ \"github.com/rcourtman/pulse-go-rewrite/internal/license/entitlements\"\n", "direct internal/license import"},
|
|
{"licensing bridge required", ".go", "package api\nimport _ \"github.com/rcourtman/pulse-go-rewrite/pkg/licensing\"\n", adapter + ".go"},
|
|
} {
|
|
variants = append(variants, boundaryVariant{
|
|
name: adapter + "/" + variant.name, file: adapter + variant.suffix,
|
|
content: variant.content, goMarker: variant.marker,
|
|
})
|
|
}
|
|
}
|
|
sourceFiles := make(map[string][]byte)
|
|
for _, dir := range []string{"internal/api"} {
|
|
err := filepath.WalkDir(filepath.Join(repo, dir), func(path string, entry fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if entry.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
|
|
return nil
|
|
}
|
|
rel, err := filepath.Rel(repo, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
sourceFiles[rel] = data
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, tc := range variants {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
root := t.TempDir()
|
|
for rel, data := range sourceFiles {
|
|
writeReportingBoundaryFile(t, filepath.Join(root, rel), data)
|
|
}
|
|
apiDir := filepath.Join(root, "internal/api")
|
|
if tc.file != "" {
|
|
if err := os.WriteFile(filepath.Join(apiDir, tc.file), []byte(tc.content), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
// The running test binary scans fixture bytes; injected files are
|
|
// never compiled or executed. The anchored selector prevents recursion.
|
|
runReportingBoundaryCommand(t, apiDir, tc.goMarker, binary,
|
|
"-test.run=^(TestPaidDomainBoundaryAudit|TestNoInternalLicenseImportFromAPIProd|TestPkgLicensingImportBoundary)$", "-test.v")
|
|
})
|
|
}
|
|
}
|
|
|
|
func writeReportingBoundaryFile(t *testing.T, target string, data []byte) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(target), 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(target, data, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func runReportingBoundaryCommand(t *testing.T, dir, rejection, command string, args ...string) {
|
|
t.Helper()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, command, args...)
|
|
cmd.Dir = dir
|
|
output, err := cmd.CombinedOutput()
|
|
if rejection == "" {
|
|
if err != nil {
|
|
t.Fatalf("shared pipeline rejected by %s: %v\n%s", command, err, output)
|
|
}
|
|
return
|
|
}
|
|
exit, ok := err.(*exec.ExitError)
|
|
if !ok || exit.ExitCode() != 1 || !strings.Contains(string(output), rejection) {
|
|
t.Fatalf("%s rejection = %v / %s, want exit 1 containing %q", command, err, output, rejection)
|
|
}
|
|
}
|