Skip to content

Commit d7043d3

Browse files
committed
add tests for state.Get
1 parent 318aeb6 commit d7043d3

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

pkg/state/state_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package state
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
"reflect"
7+
"testing"
8+
9+
"github.com/spf13/afero"
10+
)
11+
12+
func newFixtureFs(fixturePath string) afero.Fs {
13+
baseFs := afero.NewBasePathFs(afero.NewOsFs(), filepath.Join("../..", fixturePath))
14+
p, _ := baseFs.(*afero.BasePathFs).RealPath("/")
15+
fmt.Println(filepath.Abs(p))
16+
fs := afero.NewCopyOnWriteFs(baseFs, afero.NewMemMapFs())
17+
return fs
18+
}
19+
20+
func TestGet(t *testing.T) {
21+
type args struct {
22+
fs afero.Fs
23+
kwasmPath string
24+
}
25+
tests := []struct {
26+
name string
27+
args args
28+
want *state
29+
wantErr bool
30+
}{
31+
{"existing state", args{newFixtureFs("testdata/containerd/existing-containerd-shim-config"), "/opt/kwasm"}, &state{Shims: map[string]*Shim{
32+
"spin-v1": {Sha256: []byte{109, 165, 232, 241, 122, 155, 250, 156, 176, 76, 242, 44, 135, 182, 71, 83, 148, 236, 236, 58, 244, 253, 195, 55, 247, 45, 109, 191, 51, 25, 234, 82}, Path: "/opt/kwasm/bin/containerd-shim-spin-v1"},
33+
}}, false},
34+
{"missing state", args{newFixtureFs("testdata/containerd/missing-containerd-shim-config"), "/opt/kwasm"}, &state{Shims: map[string]*Shim{}}, false},
35+
}
36+
for _, tt := range tests {
37+
t.Run(tt.name, func(t *testing.T) {
38+
got, err := Get(tt.args.fs, tt.args.kwasmPath)
39+
if (err != nil) != tt.wantErr {
40+
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
41+
return
42+
}
43+
if !reflect.DeepEqual(got.Shims, tt.want.Shims) {
44+
t.Errorf("Get() = %v, want %v", got.Shims, tt.want.Shims)
45+
}
46+
})
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env sh
2+
3+
echo "Hello from spin shim"
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+
}

0 commit comments

Comments
 (0)