From ad7e43610eeec019cf329d3a46ef7ac96d25f477 Mon Sep 17 00:00:00 2001 From: srikanth-bitdynamics <259878899+srikanth-bitdynamics@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:44:43 +0400 Subject: [PATCH] fix(localnet): keep tests from booting the real shared observability stack TestRunUp_InheritsOptInProfilesOnReup re-ups an instance whose stored profiles include observability, so RunUp probes the shared stack via EnsureSharedStack. The test stubbed the instance compose runner but not newSharedRunner, so every `go test ./internal/localnet/...` ran a real `docker compose up` and left canton-devkit-observability-prometheus and -grafana running from the test's TempDir. Because the shared project name is fixed, any unstubbed test can also discover (DiscoverPort) or tear down (Stop is `-p`-only) a developer's real shared stack. So beyond stubbing the two tests that reached it: - newSharedRunner now returns a refusing runner when testing.Testing() is true, unless CANTON_DEVKIT_TEST_ALLOW_SHARED_STACK=1. Callers treat the refusal like a host without Docker (best-effort paths already handle that). - Add a stubSharedRunner helper and use it in the re-up test (shared stack reported unreachable, so RunUp takes the stubbed per-instance overlay path) and in the refcount test. - Stub sharedGrafanaURL in the CLI TestObservabilityStatus_TextOff, which was running `docker compose port` against the shared project. --- internal/cli/localnet/observability_test.go | 4 ++ internal/localnet/shared_observability.go | 27 +++++++++- .../localnet/shared_observability_test.go | 49 +++++++++++++++++-- internal/localnet/up_profiles_test.go | 4 ++ 4 files changed, 78 insertions(+), 6 deletions(-) diff --git a/internal/cli/localnet/observability_test.go b/internal/cli/localnet/observability_test.go index 5321c89a..6d5a8f65 100644 --- a/internal/cli/localnet/observability_test.go +++ b/internal/cli/localnet/observability_test.go @@ -2,6 +2,7 @@ package localnet import ( "bytes" + "context" "encoding/json" "errors" "strings" @@ -56,6 +57,9 @@ func seedObsInstance(t *testing.T, name string, status registry.Status, ports ma // sidecars shows both off and the enable hint. func TestObservabilityStatus_TextOff(t *testing.T) { t.Setenv("CANTON_DEVKIT_REGISTRY", t.TempDir()) + old := sharedGrafanaURL + sharedGrafanaURL = func(context.Context, string) string { return "" } + t.Cleanup(func() { sharedGrafanaURL = old }) seedObsInstance(t, "demo", registry.StatusRunning, map[string]int{}) cmd := buildObservability() diff --git a/internal/localnet/shared_observability.go b/internal/localnet/shared_observability.go index 7f82ed90..3048b994 100644 --- a/internal/localnet/shared_observability.go +++ b/internal/localnet/shared_observability.go @@ -11,6 +11,7 @@ import ( "path/filepath" "strconv" "strings" + "testing" "github.com/bitdynamics-ab/canton-devkit/assets" "github.com/bitdynamics-ab/canton-devkit/internal/docker" @@ -61,9 +62,24 @@ type sharedComposeOps interface { DiscoverPort(ctx context.Context, service string, containerPort int) (int, error) } +// allowSharedStackInTestsEnv opts a `go test` binary into the real shared +// stack (e.g. an integration test). Unset, newSharedRunner refuses Docker. +const allowSharedStackInTestsEnv = "CANTON_DEVKIT_TEST_ALLOW_SHARED_STACK" + +// errSharedStackRefusedInTest is what the refused runner returns under +// `go test`: callers then behave as on a host without Docker. +var errSharedStackRefusedInTest = errors.New("shared observability stack refused under go test: " + + "stub newSharedRunner, or set " + allowSharedStackInTestsEnv + "=1") + // newSharedRunner builds the production runner for the shared stack. A -// test seam (package var) so unit tests inject a stub. +// test seam (package var) so unit tests inject a stub. Under `go test` it +// refuses Docker unless allowSharedStackInTestsEnv=1: the project name is +// fixed, so an unstubbed test would boot real containers from its TempDir +// or tear down the developer's own shared stack (Stop is `-p`-only). var newSharedRunner = func(composeFile string, env []string, workDir string, logw io.Writer) sharedComposeOps { + if testing.Testing() && os.Getenv(allowSharedStackInTestsEnv) != "1" { + return refusedSharedRunner{} + } return &docker.ComposeRunner{ ProjectName: SharedObservabilityProject, ComposeFiles: []string{composeFile}, @@ -73,6 +89,15 @@ var newSharedRunner = func(composeFile string, env []string, workDir string, log } } +// refusedSharedRunner is newSharedRunner's stand-in under `go test`. +type refusedSharedRunner struct{} + +func (refusedSharedRunner) Up(context.Context) error { return errSharedStackRefusedInTest } +func (refusedSharedRunner) Stop(context.Context, bool) error { return errSharedStackRefusedInTest } +func (refusedSharedRunner) DiscoverPort(context.Context, string, int) (int, error) { + return 0, errSharedStackRefusedInTest +} + // SharedObservabilityRoot is the on-disk home of the shared stack: // /_observability (honours CANTON_DEVKIT_REGISTRY via // registry.Root(), so tests are isolated). Holds the materialised compose diff --git a/internal/localnet/shared_observability_test.go b/internal/localnet/shared_observability_test.go index 18f229bc..7c98c2ab 100644 --- a/internal/localnet/shared_observability_test.go +++ b/internal/localnet/shared_observability_test.go @@ -3,12 +3,14 @@ package localnet import ( "context" "encoding/json" + "errors" "io" "os" "path/filepath" "strings" "testing" + "github.com/bitdynamics-ab/canton-devkit/internal/docker" "github.com/bitdynamics-ab/canton-devkit/internal/registry" ) @@ -157,12 +159,14 @@ func TestDeregisterInstanceTargets_Idempotent(t *testing.T) { } // recordingSharedRunner is a sharedComposeOps stub that records whether -// Up/Stop were called, for the refcount decision tests. +// Up/Stop were called, for the refcount decision tests. upErr makes Up +// fail, i.e. the shared stack is unreachable. type recordingSharedRunner struct { upCalled, stopCalled bool + upErr error } -func (r *recordingSharedRunner) Up(context.Context) error { r.upCalled = true; return nil } +func (r *recordingSharedRunner) Up(context.Context) error { r.upCalled = true; return r.upErr } func (r *recordingSharedRunner) Stop(context.Context, bool) error { r.stopCalled = true return nil @@ -171,15 +175,50 @@ func (r *recordingSharedRunner) DiscoverPort(context.Context, string, int) (int, return 39090, nil } +// stubSharedRunner swaps newSharedRunner for rec until the test ends. +func stubSharedRunner(t *testing.T, rec sharedComposeOps) { + t.Helper() + orig := newSharedRunner + newSharedRunner = func(string, []string, string, io.Writer) sharedComposeOps { return rec } + t.Cleanup(func() { newSharedRunner = orig }) +} + +// TestNewSharedRunner_RefusesDockerUnderGoTest: the default runner must +// not reach Docker from a test binary unless explicitly allowed, so an +// unstubbed test can't boot (or tear down) the fixed-name shared project. +func TestNewSharedRunner_RefusesDockerUnderGoTest(t *testing.T) { + t.Setenv(allowSharedStackInTestsEnv, "") + // Type-check before calling anything: on a regression, Stop would run + // the real `-p` teardown against the developer's shared stack. + got := newSharedRunner("compose.yaml", nil, t.TempDir(), io.Discard) + r, ok := got.(refusedSharedRunner) + if !ok { + t.Fatalf("newSharedRunner under go test = %T, want refusedSharedRunner", got) + } + ctx := context.Background() + if err := r.Up(ctx); !errors.Is(err, errSharedStackRefusedInTest) { + t.Errorf("Up = %v, want errSharedStackRefusedInTest", err) + } + if err := r.Stop(ctx, true); !errors.Is(err, errSharedStackRefusedInTest) { + t.Errorf("Stop = %v, want errSharedStackRefusedInTest", err) + } + if port, err := r.DiscoverPort(ctx, "prometheus", 9090); port != 0 || !errors.Is(err, errSharedStackRefusedInTest) { + t.Errorf("DiscoverPort = (%d, %v), want (0, errSharedStackRefusedInTest)", port, err) + } + + t.Setenv(allowSharedStackInTestsEnv, "1") + if _, ok := newSharedRunner("compose.yaml", nil, t.TempDir(), io.Discard).(*docker.ComposeRunner); !ok { + t.Errorf("with %s=1 newSharedRunner should build the real *docker.ComposeRunner", allowSharedStackInTestsEnv) + } +} + // TestTeardownSharedStackIfIdle_RefcountGate: the shared stack is torn // down ONLY when no instance still references it (no target files). This // is the refcount that keeps the stack alive while any instance uses it. func TestTeardownSharedStackIfIdle_RefcountGate(t *testing.T) { t.Setenv("CANTON_DEVKIT_REGISTRY", t.TempDir()) rec := &recordingSharedRunner{} - orig := newSharedRunner - newSharedRunner = func(string, []string, string, io.Writer) sharedComposeOps { return rec } - defer func() { newSharedRunner = orig }() + stubSharedRunner(t, rec) // Materialise so the compose file exists (teardown short-circuits if not). if err := materializeSharedAssets(); err != nil { diff --git a/internal/localnet/up_profiles_test.go b/internal/localnet/up_profiles_test.go index 96faf1ca..d24badb0 100644 --- a/internal/localnet/up_profiles_test.go +++ b/internal/localnet/up_profiles_test.go @@ -3,6 +3,7 @@ package localnet import ( "bytes" "context" + "errors" "io" "os" "path/filepath" @@ -135,6 +136,9 @@ func TestRunUp_RejectsUnknownProfile(t *testing.T) { // full set, with no double-counting. func TestRunUp_InheritsOptInProfilesOnReup(t *testing.T) { t.Setenv("CANTON_DEVKIT_REGISTRY", t.TempDir()) + // The inherited observability profile probes the shared stack; report it + // unreachable so RunUp falls back to the (stubbed) per-instance overlay. + stubSharedRunner(t, &recordingSharedRunner{upErr: errors.New("shared stack stubbed out")}) projectDir := fakeSpliceProjectDir(t) base := adapterBaseProfiles(t)