Fix iso_path validation regex (#9855)

This commit is contained in:
Sylvia Moss 2020-09-01 10:11:48 +02:00 committed by GitHub
parent f4556fd3f1
commit 24dbd0a28b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 6 deletions

View File

@ -208,8 +208,9 @@ func (d *DatastoreIsoPath) Validate() bool {
// [datastore] /dir/subdir/file
// [datastore] dir/subdir/file
// [] /dir/subdir/file
// /dir/subdir/file or dir/subdir/file
matched, _ := regexp.MatchString(`^((\[\w*\])?\s*([^\[\]]+))$`, d.path)
// [data-store] /dir/subdir/file
// dir/subdir/file or dir/subdir/file
matched, _ := regexp.MatchString(`^\s*(\[[^\[\]\/]*\])?\s*[^\[\]]+\s*$`, d.path)
return matched
}
@ -219,7 +220,7 @@ func (d *DatastoreIsoPath) GetFilePath() string {
if len(parts) > 1 {
// removes datastore name from path
filePath = parts[1]
filePath = strings.TrimLeft(filePath, " ")
filePath = strings.TrimSpace(filePath)
}
return filePath
}

View File

@ -36,19 +36,54 @@ func TestDatastoreIsoPath(t *testing.T) {
isoPath: "[datastore][] /dir/subdir/file",
valid: false,
},
{
isoPath: "[data/store] /dir/subdir/file",
valid: false,
},
{
isoPath: "[data store] /dir/sub dir/file",
filePath: "/dir/sub dir/file",
valid: true,
},
{
isoPath: " [datastore] /dir/subdir/file",
filePath: "/dir/subdir/file",
valid: true,
},
{
isoPath: "[datastore] /dir/subdir/file",
filePath: "/dir/subdir/file",
valid: true,
},
{
isoPath: "[datastore] /dir/subdir/file ",
filePath: "/dir/subdir/file",
valid: true,
},
{
isoPath: "[привѣ́тъ] /привѣ́тъ/привѣ́тъ/привѣ́тъ",
filePath: "/привѣ́тъ/привѣ́тъ/привѣ́тъ",
valid: true,
},
// Test case for #9846
{
isoPath: "[ISO-StorageLun9] Linux/rhel-8.0-x86_64-dvd.iso",
filePath: "Linux/rhel-8.0-x86_64-dvd.iso",
valid: true,
},
}
for _, c := range tc {
for i, c := range tc {
dsIsoPath := &DatastoreIsoPath{path: c.isoPath}
if dsIsoPath.Validate() != c.valid {
t.Fatalf("Expecting %s to be %t but was %t", c.isoPath, c.valid, !c.valid)
t.Fatalf("%d Expecting %s to be %t but was %t", i, c.isoPath, c.valid, !c.valid)
}
if !c.valid {
continue
}
filePath := dsIsoPath.GetFilePath()
if filePath != c.filePath {
t.Fatalf("Expecting %s but got %s", c.filePath, filePath)
t.Fatalf("%d Expecting %s but got %s", i, c.filePath, filePath)
}
}
}