Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/cli/localnet/observability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package localnet

import (
"bytes"
"context"
"encoding/json"
"errors"
"strings"
Expand Down Expand Up @@ -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()
Expand Down
27 changes: 26 additions & 1 deletion internal/localnet/shared_observability.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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},
Expand All @@ -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:
// <registry-root>/_observability (honours CANTON_DEVKIT_REGISTRY via
// registry.Root(), so tests are isolated). Holds the materialised compose
Expand Down
49 changes: 44 additions & 5 deletions internal/localnet/shared_observability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions internal/localnet/up_profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package localnet
import (
"bytes"
"context"
"errors"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -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)

Expand Down
Loading