Skip to content

Commit 4aa14c0

Browse files
committed
add tests for RemoveRuntime
1 parent 1c87e9a commit 4aa14c0

File tree

2 files changed

+90
-6
lines changed

2 files changed

+90
-6
lines changed

pkg/containerd/configure.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ func (c *Config) AddRuntime(shimPath string) error {
5050
return err
5151
}
5252

53-
// Fail if config.toml already contains the runtimeName
54-
// Prevents corrupt config but could lead to unexpcted fails for the user.
55-
// Maybe skipping existing config?
53+
// Warn if config.toml already contains runtimeName
5654
if strings.Contains(string(data), runtimeName) {
57-
//return configPath, fmt.Errorf("config file %s already contains runtime config for '%s'", configPath, runtimeName)
5855
slog.Info(fmt.Sprintf("config for runtime '%s' already exists, skipping", runtimeName))
5956
return nil
6057
}
@@ -86,9 +83,10 @@ func (c *Config) RemoveRuntime(shimPath string) error {
8683
return err
8784
}
8885

89-
// Fail if config.toml does not contain the runtimeName
86+
// Warn if config.toml does not contain the runtimeName
9087
if !strings.Contains(string(data), runtimeName) {
91-
return fmt.Errorf("config file %s does not contain a runtime config for '%s'", c.configPath, runtimeName)
88+
slog.Warn(fmt.Sprintf("config for runtime '%s' does not exist, skipping", runtimeName))
89+
return nil
9290
}
9391

9492
// Convert the file data to a string and replace the target string with an empty string.

pkg/containerd/configure_test.go

+86
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,89 @@ runtime_type = "/opt/kwasm/bin/containerd-shim-spin-v1"
125125
})
126126
}
127127
}
128+
129+
func TestConfig_RemoveRuntime(t *testing.T) {
130+
type fields struct {
131+
hostFs afero.Fs
132+
configPath string
133+
}
134+
type args struct {
135+
shimPath string
136+
}
137+
tests := []struct {
138+
name string
139+
fields fields
140+
args args
141+
wantErr bool
142+
wantFileErr bool
143+
wantFileContent string
144+
}{
145+
{"missing shim config", fields{
146+
hostFs: newFixtureFs("testdata/containerd/missing-containerd-shim-config"),
147+
configPath: "/etc/containerd/config.toml",
148+
}, args{"/opt/kwasm/bin/containerd-shim-spin-v1"}, false, false, `[plugins]
149+
[plugins."io.containerd.monitor.v1.cgroups"]
150+
no_prometheus = false
151+
[plugins."io.containerd.service.v1.diff-service"]
152+
default = ["walking"]
153+
[plugins."io.containerd.gc.v1.scheduler"]
154+
pause_threshold = 0.02
155+
deletion_threshold = 0
156+
mutation_threshold = 100
157+
schedule_delay = 0
158+
startup_delay = "100ms"
159+
[plugins."io.containerd.runtime.v2.task"]
160+
platforms = ["linux/amd64"]
161+
sched_core = true
162+
[plugins."io.containerd.service.v1.tasks-service"]
163+
blockio_config_file = ""
164+
rdt_config_file = ""
165+
`},
166+
{"missing config", fields{
167+
hostFs: newFixtureFs("testdata/containerd/missing-containerd-config"),
168+
configPath: "/etc/containerd/config.toml",
169+
}, args{"/opt/kwasm/bin/containerd-shim-spin-v1"}, true, true, ``},
170+
{"existing shim config", fields{
171+
hostFs: newFixtureFs("testdata/containerd/existing-containerd-shim-config"),
172+
configPath: "/etc/containerd/config.toml",
173+
}, args{"/opt/kwasm/bin/containerd-shim-spin-v1"}, false, false, `[plugins]
174+
[plugins."io.containerd.monitor.v1.cgroups"]
175+
no_prometheus = false
176+
[plugins."io.containerd.service.v1.diff-service"]
177+
default = ["walking"]
178+
[plugins."io.containerd.gc.v1.scheduler"]
179+
pause_threshold = 0.02
180+
deletion_threshold = 0
181+
mutation_threshold = 100
182+
schedule_delay = 0
183+
startup_delay = "100ms"
184+
[plugins."io.containerd.runtime.v2.task"]
185+
platforms = ["linux/amd64"]
186+
sched_core = true
187+
[plugins."io.containerd.service.v1.tasks-service"]
188+
blockio_config_file = ""
189+
rdt_config_file = ""
190+
`},
191+
}
192+
for _, tt := range tests {
193+
t.Run(tt.name, func(t *testing.T) {
194+
c := &Config{
195+
hostFs: tt.fields.hostFs,
196+
configPath: tt.fields.configPath,
197+
}
198+
if err := c.RemoveRuntime(tt.args.shimPath); (err != nil) != tt.wantErr {
199+
t.Errorf("Config.RemoveRuntime() error = %v, wantErr %v", err, tt.wantErr)
200+
}
201+
202+
gotContent, err := afero.ReadFile(c.hostFs, c.configPath)
203+
if (err != nil) != tt.wantFileErr {
204+
t.Errorf("read %s error = %v, wantFileErr %v", c.configPath, err, tt.wantFileErr)
205+
return
206+
}
207+
208+
if string(gotContent) != tt.wantFileContent {
209+
t.Errorf("file content %s got = %s, want = %s", c.configPath, string(gotContent), tt.wantFileContent)
210+
}
211+
})
212+
}
213+
}

0 commit comments

Comments
 (0)