-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OVA: Change the scanning for ova and ovf files inside the NFS directory
Signed-off-by: Bella Khizgiyaev <[email protected]>
- Loading branch information
Showing
3 changed files
with
163 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/onsi/gomega" | ||
) | ||
|
||
func TestFindOVAFiles(t *testing.T) { | ||
g := gomega.NewGomegaWithT(t) | ||
|
||
tests := []struct { | ||
name string | ||
setup func(directory string) | ||
expectedOVAs []string | ||
expectedOVFs []string | ||
expectError bool | ||
}{ | ||
{ | ||
name: "basic structure", | ||
setup: func(directory string) { | ||
ioutil.WriteFile(filepath.Join(directory, "test.ova"), []byte{}, 0644) | ||
ioutil.WriteFile(filepath.Join(directory, "test.ovf"), []byte{}, 0644) | ||
os.MkdirAll(filepath.Join(directory, "subdir1"), 0755) | ||
ioutil.WriteFile(filepath.Join(directory, "subdir1", "test1.ova"), []byte{}, 0644) | ||
ioutil.WriteFile(filepath.Join(directory, "subdir1", "test1.ovf"), []byte{}, 0644) | ||
os.MkdirAll(filepath.Join(directory, "subdir1", "subdir2"), 0755) | ||
ioutil.WriteFile(filepath.Join(directory, "subdir1", "subdir2", "test2.ovf"), []byte{}, 0644) | ||
}, | ||
expectedOVAs: []string{"test.ova", "subdir1/test1.ova"}, | ||
expectedOVFs: []string{"test.ovf", "subdir1/test1.ovf", "subdir1/subdir2/test2.ovf"}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "non-existent directory", | ||
setup: func(directory string) { | ||
os.RemoveAll(directory) | ||
}, | ||
expectedOVAs: nil, | ||
expectedOVFs: nil, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "non-ova/ovf files", | ||
setup: func(directory string) { | ||
ioutil.WriteFile(filepath.Join(directory, "test.txt"), []byte{}, 0644) | ||
}, | ||
expectedOVAs: nil, | ||
expectedOVFs: nil, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "incorrect depth", | ||
setup: func(directory string) { | ||
os.MkdirAll(filepath.Join(directory, "subdir1", "subdir2"), 0755) | ||
ioutil.WriteFile(filepath.Join(directory, "subdir1", "subdir2", "test3.ova"), []byte{}, 0644) | ||
}, | ||
expectedOVAs: nil, | ||
expectedOVFs: nil, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "folder with extension", | ||
setup: func(directory string) { | ||
os.MkdirAll(filepath.Join(directory, "subdir1.ova"), 0755) | ||
os.MkdirAll(filepath.Join(directory, "subdir2.ovf"), 0755) | ||
}, | ||
expectedOVAs: nil, | ||
expectedOVFs: nil, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "files inside folders with extension", | ||
setup: func(directory string) { | ||
os.MkdirAll(filepath.Join(directory, "subdir1.ova"), 0755) | ||
os.MkdirAll(filepath.Join(directory, "subdir2.ovf"), 0755) | ||
ioutil.WriteFile(filepath.Join(directory, "subdir1.ova", "test.ova"), []byte{}, 0644) | ||
ioutil.WriteFile(filepath.Join(directory, "subdir2.ovf", "test.ovf"), []byte{}, 0644) | ||
}, | ||
expectedOVAs: []string{"subdir1.ova/test.ova"}, | ||
expectedOVFs: []string{"subdir2.ovf/test.ovf"}, | ||
expectError: false, | ||
}, | ||
} | ||
|
||
for _, testCase := range tests { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
testDir, err := ioutil.TempDir("", "ova_test") | ||
g.Expect(err).NotTo(gomega.HaveOccurred()) | ||
|
||
testCase.setup(testDir) | ||
|
||
for i, relPath := range testCase.expectedOVAs { | ||
testCase.expectedOVAs[i] = filepath.Join(testDir, relPath) | ||
} | ||
for i, relPath := range testCase.expectedOVFs { | ||
testCase.expectedOVFs[i] = filepath.Join(testDir, relPath) | ||
} | ||
|
||
ovaFiles, ovfFiles, err := findOVAFiles(testDir) | ||
if testCase.expectError { | ||
g.Expect(err).To(gomega.HaveOccurred()) | ||
} else { | ||
g.Expect(err).To(gomega.BeNil()) | ||
g.Expect(ovaFiles).To(gomega.ConsistOf(testCase.expectedOVAs)) | ||
g.Expect(ovfFiles).To(gomega.ConsistOf(testCase.expectedOVFs)) | ||
} | ||
_ = os.RemoveAll(testDir) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters