feat: add support for running mocks in detached/background mode#125
Merged
Conversation
Adds `--detach`/`-d` to background a running mock and return the terminal. By default it waits for the healthcheck to pass before detaching; `--no-await` returns immediately. Detached process-engine logs go to ~/.imposter/logs/imposter-<port>.log (overridable with `--log-file`). Auto-restart is disabled when detaching since the config-dir watcher lives in the foreground process. Process engines are started in their own session so they survive the CLI exiting; docker containers already run independently in dockerd. Existing `imposter ls` / `imposter down` rediscovery is unchanged. https://claude.ai/code/session_0176yoDmpbo3wwm7i4z4BfPY
Replace the separate --detach/--no-await booleans with a single --detach=MODE string flag (NoOptDefVal "healthy"), accepting 'healthy' (default) and 'now'. https://claude.ai/code/session_0176yoDmpbo3wwm7i4z4BfPY
The detach test asserted ListAllManaged returned exactly one mock and StopAllManaged returned exactly one. On CI runners with a system imposter installed at /opt/imposter, the JVM matcher also picks up that process, so the count is 2. Verify instead that our mock (by port) is in the list and that at least one mock was stopped. https://claude.ai/code/session_0176yoDmpbo3wwm7i4z4BfPY
81efc47 to
beca4bb
Compare
GetID() returned the full 64-char container ID, which made the backgrounded-mock log line in `imposter up --detach` unwieldy. Match the 12-char short ID that `imposter ls` already uses for docker containers; docker accepts the short form for rm/logs/etc. https://claude.ai/code/session_0176yoDmpbo3wwm7i4z4BfPY
`imposter ls`: - list mocks across all engine types by default (was opt-in via -a/--all) - `-t/--engine-type` filters to a single engine - drop the `-a/--all` flag and its mutual exclusivity rule `imposter down`: - accept a single mock ID as a positional argument (matching the ID shown by `imposter ls`); an unknown ID is fatal - `-a/--all` stops every managed mock across all engine types - the bare `imposter down` form is now an error - drop `-t/--engine-type` — IDs identify the engine implicitly To support per-ID stopping, add `StopManaged(id) (bool, error)` to the MockEngine interface. Docker resolves via ContainerInspect and checks the imposter-managed label before removing. Process engines (jvm, native) use a new `procutil.StopManagedProcess` helper that kills only the PID matching both the id and the engine's matcher. https://claude.ai/code/session_0176yoDmpbo3wwm7i4z4BfPY
The previous down/ls rewrite touched the table for those rows; flag the new background mode in the `imposter up` row to keep the front page in line with the branch's user-visible changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds the ability to run Imposter mocks in the background (detached mode) via new
--detachand--log-fileflags on theupcommand. When detached, the CLI returns control to the terminal while the mock continues running as a managed process.Key Changes
New CLI flags: Added
--detach(with modeshealthyandnow) and--log-fileoptions to theupcommandhealthymode (default): waits for the healthcheck to pass before returningnowmode: returns immediately without waiting for healthDetach mode API: Introduced
DetachModeenum and related types inengine/api.goStartOptionsnow includesDetachandDetachLogfieldsIsDetached()helper andDefaultDetachLogPath()functionProcess engine support: Updated JVM and Golang engines to handle detached startup
SysProcAttrto detach child processes from parent process groupDocker engine support: Updated Docker engine to skip log streaming and process reaping in detached modes
Process utilities: Added new
procutilpackage with platform-specific detach helperslog.go: Opens log files with append mode to preserve prior outputdetach_unix.go: SetsSetsidfor Unix-like systemsdetach_windows.go: UsesCREATE_NEW_PROCESS_GROUPandDETACHED_PROCESSflagsEngine interface: Added
GetID()method toMockEngineinterface to expose container/process identifiersComprehensive testing: Added unit tests and integration tests
cmd/up_test.go: Tests forapplyDetachOptions()functionengine/detach_test.go: Tests for detach mode helpersengine/procutil/log_test.go: Tests for log file handlingStartDetached()test helper inenginetests/common.goNotable Implementation Details
DetachHealthymode, Ctrl+C during the healthcheck wait will abort and stop the mock;DetachNowreturns immediately so there's nothing to interrupt--log-fileflag (usesdocker logsinstead)