Skip to content

Commit 1c87e9a

Browse files
committed
use test fixtures in containerd config tests
1 parent 57dfbbd commit 1c87e9a

File tree

6 files changed

+100
-100
lines changed

6 files changed

+100
-100
lines changed

pkg/containerd/configure.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ import (
2828
)
2929

3030
type Config struct {
31-
fs afero.Fs
31+
hostFs afero.Fs
3232
configPath string
3333
}
3434

35-
func NewConfig(fs afero.Fs, configPath string) *Config {
35+
func NewConfig(hostFs afero.Fs, configPath string) *Config {
3636
return &Config{
37-
fs: fs,
37+
hostFs: hostFs,
3838
configPath: configPath,
3939
}
4040
}
@@ -45,7 +45,7 @@ func (c *Config) AddRuntime(shimPath string) error {
4545
cfg := generateConfig(shimPath, runtimeName)
4646

4747
// Containerd config file needs to exist, otherwise return the error
48-
data, err := afero.ReadFile(c.fs, c.configPath)
48+
data, err := afero.ReadFile(c.hostFs, c.configPath)
4949
if err != nil {
5050
return err
5151
}
@@ -60,7 +60,7 @@ func (c *Config) AddRuntime(shimPath string) error {
6060
}
6161

6262
// Open file in append mode
63-
file, err := c.fs.OpenFile(c.configPath, os.O_APPEND|os.O_WRONLY, 0644)
63+
file, err := c.hostFs.OpenFile(c.configPath, os.O_APPEND|os.O_WRONLY, 0644)
6464
if err != nil {
6565
return err
6666
}
@@ -81,7 +81,7 @@ func (c *Config) RemoveRuntime(shimPath string) error {
8181
cfg := generateConfig(shimPath, runtimeName)
8282

8383
// Containerd config file needs to exist, otherwise return the error
84-
data, err := afero.ReadFile(c.fs, c.configPath)
84+
data, err := afero.ReadFile(c.hostFs, c.configPath)
8585
if err != nil {
8686
return err
8787
}
@@ -95,7 +95,7 @@ func (c *Config) RemoveRuntime(shimPath string) error {
9595
modifiedData := strings.Replace(string(data), cfg, "", -1)
9696

9797
// Write the modified data back to the file.
98-
err = afero.WriteFile(c.fs, c.configPath, []byte(modifiedData), 0644)
98+
err = afero.WriteFile(c.hostFs, c.configPath, []byte(modifiedData), 0644)
9999
if err != nil {
100100
return err
101101
}

pkg/containerd/configure_test.go

+71-67
Original file line numberDiff line numberDiff line change
@@ -24,100 +24,104 @@ import (
2424
"github.com/spf13/afero"
2525
)
2626

27-
func newTestFs(fixturePath string) afero.Fs {
27+
func newFixtureFs(fixturePath string) afero.Fs {
2828
baseFs := afero.NewBasePathFs(afero.NewOsFs(), filepath.Join("../..", fixturePath))
2929
p, _ := baseFs.(*afero.BasePathFs).RealPath("/")
3030
fmt.Println(filepath.Abs(p))
3131
fs := afero.NewCopyOnWriteFs(baseFs, afero.NewMemMapFs())
3232
return fs
3333
}
3434

35-
func TestFs(t *testing.T) {
36-
fs := newTestFs("testdata/containerd/valid")
37-
38-
_, err := fs.Stat("/etc/containerd/config.toml")
39-
if err != nil {
40-
t.Error(err)
41-
}
42-
}
43-
4435
func TestConfig_AddRuntime(t *testing.T) {
36+
type fields struct {
37+
hostFs afero.Fs
38+
configPath string
39+
}
4540
type args struct {
4641
shimPath string
4742
}
4843
tests := []struct {
49-
name string
50-
args args
51-
configFile string
52-
initialConfigFileContent string
53-
createFile bool
54-
wantErr bool
55-
wantFileContent string
44+
name string
45+
fields fields
46+
args args
47+
wantErr bool
48+
wantFileErr bool
49+
wantFileContent string
5650
}{
57-
{"foobar", args{"/assets/foobar"}, "/etc/containerd/config.toml", "Hello World\n", true, false,
58-
`Hello World
59-
60-
# KWASM runtime config for foobar
61-
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.foobar]
62-
runtime_type = "/assets/foobar"
51+
{"missing shim config", fields{
52+
hostFs: newFixtureFs("testdata/containerd/missing-containerd-shim-config"),
53+
configPath: "/etc/containerd/config.toml",
54+
}, args{"/opt/kwasm/bin/containerd-shim-spin-v1"}, false, false, `[plugins]
55+
[plugins."io.containerd.monitor.v1.cgroups"]
56+
no_prometheus = false
57+
[plugins."io.containerd.service.v1.diff-service"]
58+
default = ["walking"]
59+
[plugins."io.containerd.gc.v1.scheduler"]
60+
pause_threshold = 0.02
61+
deletion_threshold = 0
62+
mutation_threshold = 100
63+
schedule_delay = 0
64+
startup_delay = "100ms"
65+
[plugins."io.containerd.runtime.v2.task"]
66+
platforms = ["linux/amd64"]
67+
sched_core = true
68+
[plugins."io.containerd.service.v1.tasks-service"]
69+
blockio_config_file = ""
70+
rdt_config_file = ""
71+
72+
# KWASM runtime config for spin-v1
73+
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.spin-v1]
74+
runtime_type = "/opt/kwasm/bin/containerd-shim-spin-v1"
6375
`},
64-
{"foobar", args{"/assets/foobar"}, "/etc/config.toml", "", false, true, ``},
65-
{"foobar", args{"/assets/foobar"}, "/etc/containerd/config.toml", `Hello World
66-
67-
# KWASM runtime config for foobar
68-
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.foobar]
69-
runtime_type = "/assets/foobar"
70-
71-
Foobar
72-
`, true, false,
73-
`Hello World
74-
75-
# KWASM runtime config for foobar
76-
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.foobar]
77-
runtime_type = "/assets/foobar"
78-
79-
Foobar
76+
{"missing config", fields{
77+
hostFs: newFixtureFs("testdata/containerd/missing-containerd-config"),
78+
configPath: "/etc/containerd/config.toml",
79+
}, args{"/opt/kwasm/bin/containerd-shim-spin-v1"}, true, true, ``},
80+
{"existing shim config", fields{
81+
hostFs: newFixtureFs("testdata/containerd/existing-containerd-shim-config"),
82+
configPath: "/etc/containerd/config.toml",
83+
}, args{"/opt/kwasm/bin/containerd-shim-spin-v1"}, false, false, `[plugins]
84+
[plugins."io.containerd.monitor.v1.cgroups"]
85+
no_prometheus = false
86+
[plugins."io.containerd.service.v1.diff-service"]
87+
default = ["walking"]
88+
[plugins."io.containerd.gc.v1.scheduler"]
89+
pause_threshold = 0.02
90+
deletion_threshold = 0
91+
mutation_threshold = 100
92+
schedule_delay = 0
93+
startup_delay = "100ms"
94+
[plugins."io.containerd.runtime.v2.task"]
95+
platforms = ["linux/amd64"]
96+
sched_core = true
97+
[plugins."io.containerd.service.v1.tasks-service"]
98+
blockio_config_file = ""
99+
rdt_config_file = ""
100+
101+
# KWASM runtime config for spin-v1
102+
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.spin-v1]
103+
runtime_type = "/opt/kwasm/bin/containerd-shim-spin-v1"
80104
`},
81-
// TODO: Add test cases.
82105
}
83106
for _, tt := range tests {
84107
t.Run(tt.name, func(t *testing.T) {
85-
fs := afero.NewMemMapFs()
86-
if tt.createFile {
87-
file, err := fs.Create(tt.configFile)
88-
if err != nil {
89-
t.Fatal(err)
90-
}
91-
92-
_, err = file.WriteString(tt.initialConfigFileContent)
93-
if err != nil {
94-
t.Fatal(err)
95-
}
96-
}
97-
98108
c := &Config{
99-
configPath: "/etc/containerd/config.toml",
100-
fs: fs,
109+
hostFs: tt.fields.hostFs,
110+
configPath: tt.fields.configPath,
101111
}
102-
err := c.AddRuntime(tt.args.shimPath)
103-
if (err != nil) != tt.wantErr {
112+
if err := c.AddRuntime(tt.args.shimPath); (err != nil) != tt.wantErr {
104113
t.Errorf("Config.AddRuntime() error = %v, wantErr %v", err, tt.wantErr)
105-
return
106114
}
107115

108-
if tt.wantErr {
116+
gotContent, err := afero.ReadFile(c.hostFs, c.configPath)
117+
if (err != nil) != tt.wantFileErr {
118+
t.Errorf("read %s error = %v, wantFileErr %v", c.configPath, err, tt.wantFileErr)
109119
return
110120
}
111121

112-
gotFileContent, err := afero.ReadFile(fs, tt.configFile)
113-
if err != nil {
114-
t.Fatal(err)
115-
}
116-
117-
if string(gotFileContent) != tt.wantFileContent {
118-
t.Errorf("runtimeConfigFile content: %v, want %v", string(gotFileContent), tt.wantFileContent)
122+
if string(gotContent) != tt.wantFileContent {
123+
t.Errorf("file content %s got = %s, want = %s", c.configPath, string(gotContent), tt.wantFileContent)
119124
}
120-
121125
})
122126
}
123127
}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,3 @@
1-
version = 2
2-
3-
root = "/var/lib/containerd"
4-
state = "/run/containerd"
5-
oom_score = 0
6-
imports = ["/etc/containerd/runtime_*.toml", "./debug.toml"]
7-
8-
[grpc]
9-
address = "/run/containerd/containerd.sock"
10-
uid = 0
11-
gid = 0
12-
13-
[debug]
14-
address = "/run/containerd/debug.sock"
15-
uid = 0
16-
gid = 0
17-
level = "info"
18-
19-
[metrics]
20-
address = ""
21-
grpc_histogram = false
22-
23-
[cgroup]
24-
path = ""
25-
261
[plugins]
272
[plugins."io.containerd.monitor.v1.cgroups"]
283
no_prometheus = false
@@ -39,4 +14,8 @@ imports = ["/etc/containerd/runtime_*.toml", "./debug.toml"]
3914
sched_core = true
4015
[plugins."io.containerd.service.v1.tasks-service"]
4116
blockio_config_file = ""
42-
rdt_config_file = ""
17+
rdt_config_file = ""
18+
19+
# KWASM runtime config for spin-v1
20+
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.spin-v1]
21+
runtime_type = "/opt/kwasm/bin/containerd-shim-spin-v1"

testdata/containerd/missing-containerd-config/etc/containerd/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[plugins]
2+
[plugins."io.containerd.monitor.v1.cgroups"]
3+
no_prometheus = false
4+
[plugins."io.containerd.service.v1.diff-service"]
5+
default = ["walking"]
6+
[plugins."io.containerd.gc.v1.scheduler"]
7+
pause_threshold = 0.02
8+
deletion_threshold = 0
9+
mutation_threshold = 100
10+
schedule_delay = 0
11+
startup_delay = "100ms"
12+
[plugins."io.containerd.runtime.v2.task"]
13+
platforms = ["linux/amd64"]
14+
sched_core = true
15+
[plugins."io.containerd.service.v1.tasks-service"]
16+
blockio_config_file = ""
17+
rdt_config_file = ""

0 commit comments

Comments
 (0)