added test case that checks quota for files inside virtual folders

This commit is contained in:
Nicola Murino 2020-03-29 11:10:03 +02:00
parent b52d078986
commit 075bbe2aef
2 changed files with 70 additions and 2 deletions

View file

@ -2316,6 +2316,74 @@ func TestVirtualFolders(t *testing.T) {
os.RemoveAll(mappedPath)
}
func TestVirtualFoldersQuota(t *testing.T) {
usePubKey := false
u := getTestUser(usePubKey)
u.QuotaFiles = 100
mappedPath1 := filepath.Join(os.TempDir(), "vdir1")
vdirPath1 := "/vdir1"
mappedPath2 := filepath.Join(os.TempDir(), "vdir2")
vdirPath2 := "/vdir2"
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
VirtualPath: vdirPath1,
MappedPath: mappedPath1,
})
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
VirtualPath: vdirPath2,
MappedPath: mappedPath2,
})
os.MkdirAll(mappedPath1, 0777)
os.MkdirAll(mappedPath2, 0777)
user, _, err := httpd.AddUser(u, http.StatusOK)
if err != nil {
t.Errorf("unable to add user: %v", err)
}
client, err := getSftpClient(user, usePubKey)
if err != nil {
t.Errorf("unable to create sftp client: %v", err)
} else {
defer client.Close()
testFileName := "test_file.dat"
testFileSize := int64(131072)
testFilePath := filepath.Join(homeBasePath, testFileName)
err = createTestFile(testFilePath, testFileSize)
if err != nil {
t.Errorf("unable to create test file: %v", err)
}
err = sftpUploadFile(testFilePath, testFileName, testFileSize, client)
if err != nil {
t.Errorf("file upload error: %v", err)
}
err = sftpUploadFile(testFilePath, path.Join(vdirPath1, testFileName), testFileSize, client)
if err != nil {
t.Errorf("file upload error: %v", err)
}
err = sftpUploadFile(testFilePath, path.Join(vdirPath2, testFileName), testFileSize, client)
if err != nil {
t.Errorf("file upload error: %v", err)
}
expectedQuotaFiles := 3
expectedQuotaSize := testFileSize * 3
user, _, err = httpd.GetUserByID(user.ID, http.StatusOK)
if err != nil {
t.Errorf("error getting user: %v", err)
}
if expectedQuotaFiles != user.UsedQuotaFiles {
t.Errorf("quota files does not match, expected: %v, actual: %v", expectedQuotaFiles, user.UsedQuotaFiles)
}
if expectedQuotaSize != user.UsedQuotaSize {
t.Errorf("quota size does not match, expected: %v, actual: %v", expectedQuotaSize, user.UsedQuotaSize)
}
}
_, err = httpd.RemoveUser(user, http.StatusOK)
if err != nil {
t.Errorf("unable to remove user: %v", err)
}
os.RemoveAll(user.GetHomeDir())
os.RemoveAll(mappedPath1)
os.RemoveAll(mappedPath2)
}
func TestMissingFile(t *testing.T) {
usePubKey := false
u := getTestUser(usePubKey)

View file

@ -178,7 +178,7 @@ func (fs OsFs) ScanRootDirContents() (int, int64, error) {
num, s, err := fs.getDirSize(v.MappedPath)
if err != nil {
if fs.IsNotExist(err) {
fsLog(fs, logger.LevelWarn, "unable to scan contents for not existent mapped path: %#v", v.MappedPath)
fsLog(fs, logger.LevelWarn, "unable to scan contents for non-existent mapped path: %#v", v.MappedPath)
continue
}
return numFiles, size, err
@ -237,7 +237,7 @@ func (fs OsFs) ResolvePath(sftpPath string) (string, error) {
// path chain until we hit a directory that _does_ exist and can be validated.
_, err = fs.findFirstExistingDir(r, basePath)
if err != nil {
fsLog(fs, logger.LevelWarn, "error resolving not existent path: %#v", err)
fsLog(fs, logger.LevelWarn, "error resolving non-existent path: %#v", err)
}
return r, err
}