Skip to content

Commit d3953c1

Browse files
committed
add CheckSemVer, CheckRootfsPath, CheckPlatform unit test
Signed-off-by: liangchenye <[email protected]>
1 parent 5dbf60b commit d3953c1

3 files changed

Lines changed: 75 additions & 3 deletions

File tree

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,3 @@ script:
1414
- $HOME/gopath/bin/git-validation -run DCO,short-subject -v -range ${TRAVIS_COMMIT_RANGE}
1515
- make
1616
- make test
17-

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PREFIX ?= $(DESTDIR)/usr
1+
22
BINDIR ?= $(DESTDIR)/usr/bin
33

44
BUILDTAGS=
@@ -42,7 +42,7 @@ localvalidation:
4242

4343
.PHONY: test .gofmt .govet .golint
4444

45-
test: .gofmt .govet .golint
45+
test: .gofmt .govet .golint .gotest
4646

4747
.gofmt:
4848
OUT=$$(go fmt ./...); if test -n "$${OUT}"; then echo "$${OUT}" && exit 1; fi
@@ -53,3 +53,6 @@ test: .gofmt .govet .golint
5353
.golint:
5454
golint -set_exit_status ./...
5555

56+
UTDIRS = ./validate/...
57+
.gotest:
58+
go test $(UTDIRS)

validate/validate_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
if err := os.MkdirAll(filepath.Join(tmpBundle, rootfsDir), 0700); err != nil {
32+
t.Fatalf("Failed to create a rootfs directory in 'CheckRootfsPath'")
33+
}
34+
if _, err := os.Create(filepath.Join(tmpBundle, rootfsNonDir)); err != nil {
35+
t.Fatalf("Failed to create a non-directory rootfs in 'CheckRootfsPath'")
36+
}
37+
38+
cases := []struct {
39+
val string
40+
expected bool
41+
}{
42+
{rootfsDir, true},
43+
{rootfsNonDir, false},
44+
{rootfsNonExists, false},
45+
{filepath.Join(tmpBundle, rootfsDir), true},
46+
{filepath.Join(tmpBundle, rootfsNonDir), false},
47+
{filepath.Join(tmpBundle, rootfsNonExists), false},
48+
}
49+
for _, c := range cases {
50+
v := NewValidator(&rspec.Spec{Root: rspec.Root{Path: c.val}}, tmpBundle, false)
51+
checkErrors(t, "CheckRootfsPath "+c.val, v.CheckRootfsPath(), c.expected)
52+
}
53+
}
54+
55+
func TestCheckSemVer(t *testing.T) {
56+
cases := []struct {
57+
val string
58+
expected bool
59+
}{
60+
{rspec.Version, true},
61+
//FIXME: validate currently only handles rpsec.Version
62+
{"0.0.1", false},
63+
{"invalid", false},
64+
}
65+
66+
for _, c := range cases {
67+
v := NewValidator(&rspec.Spec{Version: c.val}, "", false)
68+
checkErrors(t, "CheckSemVer "+c.val, v.CheckSemVer(), c.expected)
69+
}
70+
}

0 commit comments

Comments
 (0)