-
Notifications
You must be signed in to change notification settings - Fork 0
engine: add Engine.Shutdown honoring shutdownAction #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| package devcontainer | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/crunchloop/devcontainer/config" | ||
| "github.com/crunchloop/devcontainer/runtime" | ||
| ) | ||
|
|
||
| func newTestWorkspace(action config.ShutdownAction, compose bool) *Workspace { | ||
| labels := map[string]string{} | ||
| if compose { | ||
| labels["com.docker.compose.project"] = "dc-test" | ||
| } | ||
| return &Workspace{ | ||
| ID: "test", | ||
| Config: &config.ResolvedConfig{ | ||
| ShutdownAction: action, | ||
| }, | ||
| Container: &runtime.ContainerDetails{ | ||
| Container: runtime.Container{ | ||
| ID: "container-1", | ||
| Name: "test", | ||
| State: runtime.StateRunning, | ||
| }, | ||
| Labels: labels, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func TestShutdown_NoneIsNoop(t *testing.T) { | ||
| rt := newFakeRuntime() | ||
| rt.containersByID["container-1"] = &runtime.ContainerDetails{ | ||
| Container: runtime.Container{ID: "container-1", State: runtime.StateRunning}, | ||
| } | ||
| eng := &Engine{runtime: rt} | ||
|
|
||
| if err := eng.Shutdown(context.Background(), newTestWorkspace(config.ShutdownNone, false)); err != nil { | ||
| t.Fatalf("Shutdown: %v", err) | ||
| } | ||
| if len(rt.stoppedIDs) != 0 { | ||
| t.Errorf("expected no stops for ShutdownNone, got %v", rt.stoppedIDs) | ||
| } | ||
| if len(rt.removedIDs) != 0 { | ||
| t.Errorf("expected no removes for ShutdownNone, got %v", rt.removedIDs) | ||
| } | ||
| } | ||
|
|
||
| func TestShutdown_StopContainerStops(t *testing.T) { | ||
| rt := newFakeRuntime() | ||
| rt.containersByID["container-1"] = &runtime.ContainerDetails{ | ||
| Container: runtime.Container{ID: "container-1", State: runtime.StateRunning}, | ||
| } | ||
| eng := &Engine{runtime: rt} | ||
|
|
||
| if err := eng.Shutdown(context.Background(), newTestWorkspace(config.ShutdownStopContainer, false)); err != nil { | ||
| t.Fatalf("Shutdown: %v", err) | ||
| } | ||
| if len(rt.stoppedIDs) != 1 || rt.stoppedIDs[0] != "container-1" { | ||
| t.Errorf("expected stop on container-1, got %v", rt.stoppedIDs) | ||
| } | ||
| if len(rt.removedIDs) != 0 { | ||
| t.Errorf("Shutdown must not remove, got %v", rt.removedIDs) | ||
| } | ||
| } | ||
|
|
||
| func TestShutdown_DefaultStopsImageWorkspace(t *testing.T) { | ||
| // Empty ShutdownAction → spec default is "stop the container" for | ||
| // non-compose workspaces. | ||
| rt := newFakeRuntime() | ||
| rt.containersByID["container-1"] = &runtime.ContainerDetails{ | ||
| Container: runtime.Container{ID: "container-1", State: runtime.StateRunning}, | ||
| } | ||
| eng := &Engine{runtime: rt} | ||
|
|
||
| if err := eng.Shutdown(context.Background(), newTestWorkspace("", false)); err != nil { | ||
| t.Fatalf("Shutdown: %v", err) | ||
| } | ||
| if len(rt.stoppedIDs) != 1 { | ||
| t.Errorf("expected default stop, got %v", rt.stoppedIDs) | ||
| } | ||
| } | ||
|
|
||
| func TestShutdown_StopComposeStopsPrimary(t *testing.T) { | ||
| rt := newFakeRuntime() | ||
| rt.containersByID["container-1"] = &runtime.ContainerDetails{ | ||
| Container: runtime.Container{ID: "container-1", State: runtime.StateRunning}, | ||
| } | ||
| eng := &Engine{runtime: rt} | ||
|
|
||
| if err := eng.Shutdown(context.Background(), newTestWorkspace(config.ShutdownStopCompose, true)); err != nil { | ||
| t.Fatalf("Shutdown: %v", err) | ||
| } | ||
| if len(rt.stoppedIDs) != 1 { | ||
| t.Errorf("expected primary stop for stopCompose (no project Stop yet), got %v", rt.stoppedIDs) | ||
| } | ||
| } | ||
|
|
||
| func TestShutdown_StopComposeOnNonComposeFallsBack(t *testing.T) { | ||
| rt := newFakeRuntime() | ||
| rt.containersByID["container-1"] = &runtime.ContainerDetails{ | ||
| Container: runtime.Container{ID: "container-1", State: runtime.StateRunning}, | ||
| } | ||
| eng := &Engine{runtime: rt} | ||
|
|
||
| if err := eng.Shutdown(context.Background(), newTestWorkspace(config.ShutdownStopCompose, false)); err != nil { | ||
| t.Fatalf("Shutdown: %v", err) | ||
| } | ||
| if len(rt.stoppedIDs) != 1 { | ||
| t.Errorf("expected single-container stop fallback, got %v", rt.stoppedIDs) | ||
| } | ||
| } | ||
|
|
||
| func TestShutdown_NilWorkspaceRejected(t *testing.T) { | ||
| eng := &Engine{runtime: newFakeRuntime()} | ||
| if err := eng.Shutdown(context.Background(), nil); err == nil { | ||
| t.Error("expected error on nil workspace") | ||
| } | ||
| } | ||
|
|
||
| func TestShutdown_NilContainerRejected(t *testing.T) { | ||
| eng := &Engine{runtime: newFakeRuntime()} | ||
| ws := &Workspace{Config: &config.ResolvedConfig{ShutdownAction: config.ShutdownStopContainer}} | ||
| if err := eng.Shutdown(context.Background(), ws); err == nil { | ||
| t.Error("expected error on workspace with nil Container") | ||
| } | ||
| } | ||
|
|
||
| func TestShutdown_EmptyContainerIDRejected(t *testing.T) { | ||
| eng := &Engine{runtime: newFakeRuntime()} | ||
| ws := &Workspace{ | ||
| Config: &config.ResolvedConfig{ShutdownAction: config.ShutdownStopContainer}, | ||
| Container: &runtime.ContainerDetails{}, | ||
| } | ||
| if err := eng.Shutdown(context.Background(), ws); err == nil { | ||
| t.Error("expected error on workspace with empty Container.ID") | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| //go:build integration | ||
|
|
||
| package integration | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| devcontainer "github.com/crunchloop/devcontainer" | ||
| "github.com/crunchloop/devcontainer/runtime" | ||
| ) | ||
|
|
||
| // TestShutdownAction_NoneLeavesContainerRunning verifies that | ||
| // Engine.Shutdown honors `shutdownAction: none` by leaving the | ||
| // container running. Engine.Down (the explicit teardown call) is | ||
| // unaffected and still stops + removes — that's tested in the | ||
| // existing TestImageSource_FullLifecycle teardown path. | ||
| func TestShutdownAction_NoneLeavesContainerRunning(t *testing.T) { | ||
| if testing.Short() { | ||
| t.Skip("integration tests skipped with -short") | ||
| } | ||
|
|
||
| eng, rt := newEngine(t) | ||
| defer rt.Close() | ||
|
|
||
| ws := writeWorkspace(t, `{ | ||
| "image": "`+testImage+`", | ||
| "shutdownAction": "none" | ||
| }`) | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) | ||
| defer cancel() | ||
|
|
||
| wsObj, err := eng.Up(ctx, devcontainer.UpOptions{ | ||
| LocalWorkspaceFolder: ws, | ||
| Recreate: true, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("Up: %v", err) | ||
| } | ||
| // Cleanup with explicit Down — Shutdown wouldn't tear it down here | ||
| // since we set the action to "none". | ||
| defer func() { _ = eng.Down(context.Background(), wsObj, devcontainer.DownOptions{Remove: true}) }() | ||
|
|
||
| if err := eng.Shutdown(ctx, wsObj); err != nil { | ||
| t.Fatalf("Shutdown: %v", err) | ||
| } | ||
|
|
||
| details, err := rt.InspectContainer(ctx, wsObj.Container.ID) | ||
| if err != nil { | ||
| t.Fatalf("InspectContainer: %v", err) | ||
| } | ||
| if details.State != runtime.StateRunning { | ||
| t.Errorf("container state after Shutdown(none) = %q, want %q", details.State, runtime.StateRunning) | ||
| } | ||
| } | ||
|
|
||
| // TestShutdownAction_StopContainerStops verifies the spec default for | ||
| // non-compose workspaces: Shutdown with `stopContainer` (or unset) | ||
| // stops the container without removing it. | ||
| func TestShutdownAction_StopContainerStops(t *testing.T) { | ||
| if testing.Short() { | ||
| t.Skip("integration tests skipped with -short") | ||
| } | ||
|
|
||
| eng, rt := newEngine(t) | ||
| defer rt.Close() | ||
|
|
||
| ws := writeWorkspace(t, `{ | ||
| "image": "`+testImage+`", | ||
| "shutdownAction": "stopContainer" | ||
| }`) | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) | ||
| defer cancel() | ||
|
|
||
| wsObj, err := eng.Up(ctx, devcontainer.UpOptions{ | ||
| LocalWorkspaceFolder: ws, | ||
| Recreate: true, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("Up: %v", err) | ||
| } | ||
| defer func() { _ = eng.Down(context.Background(), wsObj, devcontainer.DownOptions{Remove: true}) }() | ||
|
|
||
| if err := eng.Shutdown(ctx, wsObj); err != nil { | ||
| t.Fatalf("Shutdown: %v", err) | ||
| } | ||
|
|
||
| details, err := rt.InspectContainer(ctx, wsObj.Container.ID) | ||
| if err != nil { | ||
| t.Fatalf("InspectContainer: %v", err) | ||
| } | ||
| // State should be exited; the container record itself must persist | ||
| // (Shutdown does not remove). | ||
| if details.State == runtime.StateRunning { | ||
| t.Errorf("container still running after Shutdown(stopContainer)") | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.