Skip to content

Commit a9f3746

Browse files
authored
test(validation): Migrate Test file /utils/version/version_test.go (#5593)
Signed-off-by: adity1raut <araut7798@gmail.com>
1 parent a7aaac5 commit a9f3746

2 files changed

Lines changed: 93 additions & 82 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package validation
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
)
9+
10+
func TestValidation(t *testing.T) {
11+
RegisterFailHandler(Fail)
12+
RunSpecs(t, "Validation Suite")
13+
}

pkg/utils/validation/validation_test.go

Lines changed: 80 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -20,93 +20,91 @@ import (
2020
"fmt"
2121
"path/filepath"
2222
"strings"
23-
"testing"
23+
24+
. "github.com/onsi/ginkgo/v2"
25+
. "github.com/onsi/gomega"
2426
)
2527

26-
func FuzzIsSafePath(f *testing.F) {
27-
// f.Add()
28-
f.Fuzz(func(t *testing.T, input string) {
29-
err := IsValidMountRoot(input)
30-
if err == nil {
31-
// valid input must start with "/"
32-
if !strings.HasPrefix(input, string(filepath.Separator)) {
33-
t.Errorf("testcase %s failed, expect the input starts with '/'", input)
34-
}
28+
var _ = Describe("IsValidMountRoot", func() {
29+
Describe("Safe paths", func() {
30+
type testCase struct {
31+
name string
32+
input string
33+
}
34+
testCases := []testCase{
35+
{
36+
name: "validPath-1",
37+
input: "/runtime-mnt//alluxio/default/hbase",
38+
},
39+
{
40+
name: "validPath-2",
41+
input: "/opt/20-Runtime-Mnt_1/./alluxio/default/hbase",
42+
},
43+
}
44+
for _, test := range testCases {
45+
tc := test
46+
It(fmt.Sprintf("should accept %s", tc.name), func() {
47+
tt := filepath.Clean(tc.input)
48+
GinkgoWriter.Println(tt)
49+
got := IsValidMountRoot(tc.input)
50+
Expect(got).To(BeNil())
51+
})
3552
}
3653
})
37-
}
3854

39-
func TestIsSafePathWithSafePath(t *testing.T) {
40-
41-
type testCase struct {
42-
name string
43-
input string
44-
}
45-
46-
testCases := []testCase{
47-
{
48-
name: "validPath-1",
49-
input: "/runtime-mnt//alluxio/default/hbase",
50-
},
51-
{
52-
name: "validPath-2",
53-
input: "/opt/20-Runtime-Mnt_1/./alluxio/default/hbase",
54-
},
55-
}
56-
57-
for _, test := range testCases {
58-
tt := filepath.Clean(test.input)
59-
print(tt)
60-
got := IsValidMountRoot(test.input)
61-
if got != nil {
62-
t.Errorf("testcase %s failed, expect no error happened, but got an error: %s", test.name, got.Error())
55+
Describe("Invalid paths", func() {
56+
type testCase struct {
57+
name string
58+
input string
59+
expect error
6360
}
64-
}
65-
}
66-
67-
func TestIsSafePathWithInvalidPath(t *testing.T) {
68-
69-
type testCase struct {
70-
name string
71-
input string
72-
expect error
73-
}
74-
75-
testCases := []testCase{
76-
{
77-
name: "invalidPath-1",
78-
input: "/$test/alluxio/default/hbase",
79-
expect: fmt.Errorf(invalidMountRootErrMsgFmt, "/$test/alluxio/default/hbase", "every directory name in the mount root path shuold follow the relaxed DNS (RFC 1123) rule which additionally allows upper case alphabetic character and character '_'"),
80-
},
81-
{
82-
name: "invalidPath-2",
83-
input: "/test/(alluxio)/default/hbase",
84-
expect: fmt.Errorf(invalidMountRootErrMsgFmt, "/test/(alluxio)/default/hbase", "every directory name in the mount root path shuold follow the relaxed DNS (RFC 1123) rule which additionally allows upper case alphabetic character and character '_'"),
85-
},
86-
{
87-
name: "invalidPath-3",
88-
input: "/test/alluxio/def;ault/hbase",
89-
expect: fmt.Errorf(invalidMountRootErrMsgFmt, "/test/alluxio/def;ault/hbase", "every directory name in the mount root path shuold follow the relaxed DNS (RFC 1123) rule which additionally allows upper case alphabetic character and character '_'"),
90-
},
91-
{
92-
name: "invalidPath-4",
93-
input: "",
94-
expect: fmt.Errorf(invalidMountRootErrMsgFmt, "", "the mount root path is empty"),
95-
},
96-
{
97-
name: "invalidPath-5",
98-
input: "runtime-mnt/default",
99-
expect: fmt.Errorf(invalidMountRootErrMsgFmt, "runtime-mnt/default", "the mount root path must be an absolute path"),
100-
},
101-
}
102-
103-
for _, test := range testCases {
104-
got := IsValidMountRoot(test.input)
105-
if got == nil {
106-
t.Errorf("testcase %s failed, expect an error happened, but got no error", test.name)
61+
testCases := []testCase{
62+
{
63+
name: "invalidPath-1",
64+
input: "/$test/alluxio/default/hbase",
65+
expect: fmt.Errorf(invalidMountRootErrMsgFmt, "/$test/alluxio/default/hbase", "every directory name in the mount root path shuold follow the relaxed DNS (RFC 1123) rule which additionally allows upper case alphabetic character and character '_'"),
66+
},
67+
{
68+
name: "invalidPath-2",
69+
input: "/test/(alluxio)/default/hbase",
70+
expect: fmt.Errorf(invalidMountRootErrMsgFmt, "/test/(alluxio)/default/hbase", "every directory name in the mount root path shuold follow the relaxed DNS (RFC 1123) rule which additionally allows upper case alphabetic character and character '_'"),
71+
},
72+
{
73+
name: "invalidPath-3",
74+
input: "/test/alluxio/def;ault/hbase",
75+
expect: fmt.Errorf(invalidMountRootErrMsgFmt, "/test/alluxio/def;ault/hbase", "every directory name in the mount root path shuold follow the relaxed DNS (RFC 1123) rule which additionally allows upper case alphabetic character and character '_'"),
76+
},
77+
{
78+
name: "invalidPath-4",
79+
input: "",
80+
expect: fmt.Errorf(invalidMountRootErrMsgFmt, "", "the mount root path is empty"),
81+
},
82+
{
83+
name: "invalidPath-5",
84+
input: "runtime-mnt/default",
85+
expect: fmt.Errorf(invalidMountRootErrMsgFmt, "runtime-mnt/default", "the mount root path must be an absolute path"),
86+
},
10787
}
108-
if got.Error() != test.expect.Error() {
109-
t.Errorf("testcase %s failed, expect error: %v, but got error: %v", test.name, test.expect, got)
88+
for _, test := range testCases {
89+
tc := test
90+
It(fmt.Sprintf("should reject %s", tc.name), func() {
91+
got := IsValidMountRoot(tc.input)
92+
Expect(got).To(HaveOccurred())
93+
Expect(got.Error()).To(Equal(tc.expect.Error()))
94+
})
11095
}
111-
}
112-
}
96+
})
97+
98+
Describe("Fuzz: path must start with / if valid", func() {
99+
It("should only accept valid paths starting with /", func() {
100+
// Example fuzz-like check for a few cases
101+
inputs := []string{"", "foo", "/foo", "foo/bar", "/foo/bar"}
102+
for _, input := range inputs {
103+
err := IsValidMountRoot(input)
104+
if err == nil {
105+
Expect(strings.HasPrefix(input, string(filepath.Separator))).To(BeTrue(), "valid input must start with '/'")
106+
}
107+
}
108+
})
109+
})
110+
})

0 commit comments

Comments
 (0)