Skip to content

Commit 45f2558

Browse files
committed
add CheckSemVer, CheckRootfsPath, CheckPlatform unit test
Signed-off-by: liangchenye <liangchenye@huawei.com>
1 parent 476f1fb commit 45f2558

3 files changed

Lines changed: 87 additions & 4 deletions

File tree

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ before_install:
1111
install: true
1212

1313
script:
14-
- go vet -x ./...
15-
- $HOME/gopath/bin/golint ./...
1614
- $HOME/gopath/bin/git-validation -run DCO,short-subject -v -range ${TRAVIS_COMMIT_RANGE}
17-
- make
15+
- make test
16+
- make all
1817

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ $(RUNTIME_TOOLS_LINK):
3737

3838
.PHONY: test .gofmt .govet .golint
3939

40-
test: .gofmt .govet .golint
40+
test: .gofmt .govet .golint .gotest
4141

4242
.gofmt:
4343
go fmt ./...
@@ -48,3 +48,5 @@ test: .gofmt .govet .golint
4848
.golint:
4949
golint ./...
5050

51+
.gotest:
52+
go test -v ./...

validate/validate_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package validate
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
10+
rspec "github.com/opencontainers/runtime-spec/specs-go"
11+
)
12+
13+
func checkErrors(t *testing.T, title string, msgs []string, valid bool) {
14+
if valid && len(msgs) > 0 {
15+
t.Fatalf("%s: expected not to get error, but get %d errors:\n%s", title, len(msgs), strings.Join(msgs, "\n"))
16+
} else if !valid && len(msgs) == 0 {
17+
t.Fatalf("%s: expected to get error, but actually not", title)
18+
}
19+
}
20+
21+
func TestCheckRootfsPath(t *testing.T) {
22+
tmpBundle, err := ioutil.TempDir("", "oci-check-rootfspath")
23+
if err != nil {
24+
t.Fatalf("Failed to create a TempDir in 'CheckRootfsPath'")
25+
}
26+
defer os.RemoveAll(tmpBundle)
27+
28+
rootfsDir := "rootfs"
29+
rootfsNonDir := "rootfsfile"
30+
rootfsNonExists := "rootfsnil"
31+
os.MkdirAll(filepath.Join(tmpBundle, rootfsDir), 0700)
32+
os.Create(filepath.Join(tmpBundle, rootfsNonDir))
33+
34+
cases := []struct {
35+
val string
36+
expected bool
37+
}{
38+
{rootfsDir, true},
39+
{rootfsNonDir, false},
40+
{rootfsNonExists, false},
41+
{filepath.Join(tmpBundle, rootfsDir), true},
42+
{filepath.Join(tmpBundle, rootfsNonDir), false},
43+
{filepath.Join(tmpBundle, rootfsNonExists), false},
44+
}
45+
for _, c := range cases {
46+
v := NewValidator(&rspec.Spec{Root: rspec.Root{Path: c.val}}, tmpBundle, false)
47+
checkErrors(t, "CheckRootfsPath "+c.val, v.CheckRootfsPath(), c.expected)
48+
}
49+
}
50+
51+
func TestCheckSemVer(t *testing.T) {
52+
cases := []struct {
53+
val string
54+
expected bool
55+
}{
56+
{rspec.Version, true},
57+
//FIXME: validate currently only handles rpsec.Version
58+
{"0.0.1", false},
59+
{"invalid", false},
60+
}
61+
62+
for _, c := range cases {
63+
v := NewValidator(&rspec.Spec{Version: c.val}, "", false)
64+
checkErrors(t, "CheckSemVer "+c.val, v.CheckSemVer(), c.expected)
65+
}
66+
}
67+
68+
func TestCheckPlatform(t *testing.T) {
69+
cases := []struct {
70+
val rspec.Platform
71+
expected bool
72+
}{
73+
{rspec.Platform{OS: "darwin", Arch: "arm64"}, true},
74+
{rspec.Platform{OS: "hurd", Arch: "arm64"}, false},
75+
{rspec.Platform{OS: "darwin", Arch: "ppc64"}, false},
76+
}
77+
78+
for _, c := range cases {
79+
v := NewValidator(&rspec.Spec{Platform: c.val}, "", false)
80+
checkErrors(t, "CheckPlatform "+c.val.OS+" "+c.val.Arch, v.CheckPlatform(), c.expected)
81+
}
82+
}

0 commit comments

Comments
 (0)