@@ -24,100 +24,104 @@ import (
24
24
"github.com/spf13/afero"
25
25
)
26
26
27
- func newTestFs (fixturePath string ) afero.Fs {
27
+ func newFixtureFs (fixturePath string ) afero.Fs {
28
28
baseFs := afero .NewBasePathFs (afero .NewOsFs (), filepath .Join ("../.." , fixturePath ))
29
29
p , _ := baseFs .(* afero.BasePathFs ).RealPath ("/" )
30
30
fmt .Println (filepath .Abs (p ))
31
31
fs := afero .NewCopyOnWriteFs (baseFs , afero .NewMemMapFs ())
32
32
return fs
33
33
}
34
34
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
-
44
35
func TestConfig_AddRuntime (t * testing.T ) {
36
+ type fields struct {
37
+ hostFs afero.Fs
38
+ configPath string
39
+ }
45
40
type args struct {
46
41
shimPath string
47
42
}
48
43
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
56
50
}{
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"
63
75
` },
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"
80
104
` },
81
- // TODO: Add test cases.
82
105
}
83
106
for _ , tt := range tests {
84
107
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
-
98
108
c := & Config {
99
- configPath : "/etc/containerd/config.toml" ,
100
- fs : fs ,
109
+ hostFs : tt . fields . hostFs ,
110
+ configPath : tt . fields . configPath ,
101
111
}
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 {
104
113
t .Errorf ("Config.AddRuntime() error = %v, wantErr %v" , err , tt .wantErr )
105
- return
106
114
}
107
115
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 )
109
119
return
110
120
}
111
121
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 )
119
124
}
120
-
121
125
})
122
126
}
123
127
}
0 commit comments