Skip to content

Commit c65f356

Browse files
authored
Merge pull request #43 from spinkube/shim-tests
Shim tests
2 parents 16ed22e + bbe5cc1 commit c65f356

File tree

5 files changed

+142
-3
lines changed

5 files changed

+142
-3
lines changed

internal/shim/install_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ func TestConfig_Install(t *testing.T) {
4646
want wants
4747
wantErr bool
4848
}{
49+
{
50+
"successful shim installation",
51+
fields{
52+
tests.FixtureFs("../../testdata/node-installer"),
53+
afero.NewMemMapFs(),
54+
"/assets",
55+
"/opt/kwasm",
56+
},
57+
args{"containerd-shim-slight-v1"},
58+
wants{
59+
"/opt/kwasm/bin/containerd-shim-slight-v1",
60+
true,
61+
},
62+
false,
63+
},
4964
{
5065
"no changes to shim",
5166
fields{

internal/shim/uninstall.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ func (c *Config) Uninstall(shimName string) (string, error) {
2727
}
2828
slog.Warn("shim binary did not exist, nothing to delete")
2929
}
30-
3130
st.RemoveShim(shimName)
32-
if err := st.Write(); err != nil {
31+
if err = st.Write(); err != nil {
3332
return "", err
3433
}
3534
return filePath, err

internal/shim/uninstall_test.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Copyright The KWasm Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package shim //nolint:testpackage // whitebox test
18+
19+
import (
20+
"testing"
21+
22+
"github.com/spf13/afero"
23+
tests "github.com/spinkube/runtime-class-manager/tests/node-installer"
24+
)
25+
26+
func TestConfig_Uninstall(t *testing.T) {
27+
type fields struct {
28+
hostFs afero.Fs
29+
kwasmPath string
30+
}
31+
type args struct {
32+
shimName string
33+
}
34+
tests := []struct {
35+
name string
36+
fields fields
37+
args args
38+
want string
39+
wantErr bool
40+
}{
41+
{
42+
"shim not installed",
43+
fields{
44+
tests.FixtureFs("../../testdata/node-installer/shim"),
45+
"/opt/kwasm",
46+
},
47+
args{"not-existing-shim"},
48+
"",
49+
false,
50+
},
51+
{
52+
"missing shim binary",
53+
fields{
54+
tests.FixtureFs("../../testdata/node-installer/shim-missing-binary"),
55+
"/opt/kwasm",
56+
},
57+
args{"spin-v1"},
58+
"/opt/kwasm/bin/containerd-shim-spin-v1",
59+
false,
60+
},
61+
{
62+
"successful shim uninstallation",
63+
fields{
64+
tests.FixtureFs("../../testdata/node-installer/shim"),
65+
"/opt/kwasm",
66+
},
67+
args{"spin-v1"},
68+
"/opt/kwasm/bin/containerd-shim-spin-v1",
69+
false,
70+
},
71+
}
72+
for _, tt := range tests {
73+
t.Run(tt.name, func(t *testing.T) {
74+
c := &Config{
75+
hostFs: tt.fields.hostFs,
76+
kwasmPath: tt.fields.kwasmPath,
77+
}
78+
79+
got, err := c.Uninstall(tt.args.shimName)
80+
81+
if (err != nil) != tt.wantErr {
82+
t.Errorf("Config.Uninstall() error = %v, wantErr %v", err, tt.wantErr)
83+
return
84+
}
85+
if got != tt.want {
86+
t.Errorf("Config.Uninstall() = %v, want %v", got, tt.want)
87+
}
88+
})
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"shims": {
3+
"spin-v1": {
4+
"sha256": "6da5e8f17a9bfa9cb04cf22c87b6475394ecec3af4fdc337f72d6dbf3319ea52",
5+
"path": "/opt/kwasm/bin/containerd-shim-spin-v1"
6+
}
7+
}
8+
}

tests/node-installer/fs.go

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
package tests
22

33
import (
4+
"io"
5+
"os"
6+
47
"github.com/spf13/afero"
58
)
69

710
func FixtureFs(fixturePath string) afero.Fs {
811
baseFs := afero.NewBasePathFs(afero.NewOsFs(), fixturePath)
9-
fs := afero.NewCopyOnWriteFs(baseFs, afero.NewMemMapFs())
12+
fs := afero.NewMemMapFs()
13+
err := afero.Walk(baseFs, "/", func(path string, info os.FileInfo, err error) error {
14+
if err != nil {
15+
return err
16+
}
17+
if info.IsDir() {
18+
return fs.MkdirAll(path, 0755) //nolint:gomnd // file permissions
19+
}
20+
src, err := baseFs.Open(path)
21+
if err != nil {
22+
return err
23+
}
24+
dst, err := fs.Create(path)
25+
if err != nil {
26+
return err
27+
}
28+
_, err = io.Copy(dst, src)
29+
if err != nil {
30+
return err
31+
}
32+
return nil
33+
})
34+
if err != nil {
35+
panic(err)
36+
}
1037
return fs
1138
}

0 commit comments

Comments
 (0)