-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshutdown_test.go
More file actions
139 lines (123 loc) · 4.31 KB
/
shutdown_test.go
File metadata and controls
139 lines (123 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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")
}
}