sftpd test: add a debug log

The git push test sometime fails when running on travis.
The issue cannot be replicated locally so print the logs to try to
understand what is happening
This commit is contained in:
Nicola Murino 2019-12-29 23:27:32 +01:00
parent 80e9902324
commit 20606a0043
2 changed files with 34 additions and 6 deletions

View file

@ -353,6 +353,9 @@ func buildUserHomeDir(user *User) {
} }
func validatePermissions(user *User) error { func validatePermissions(user *User) error {
if len(user.Permissions) == 0 {
return &ValidationError{err: "Please grant some permissions to this user"}
}
permissions := make(map[string][]string) permissions := make(map[string][]string)
if _, ok := user.Permissions["/"]; !ok { if _, ok := user.Permissions["/"]; !ok {
return &ValidationError{err: fmt.Sprintf("Permissions for the root dir \"/\" must be set")} return &ValidationError{err: fmt.Sprintf("Permissions for the root dir \"/\" must be set")}
@ -391,9 +394,6 @@ func validateUser(user *User) error {
if len(user.Password) == 0 && len(user.PublicKeys) == 0 { if len(user.Password) == 0 && len(user.PublicKeys) == 0 {
return &ValidationError{err: "Please set a password or at least a public_key"} return &ValidationError{err: "Please set a password or at least a public_key"}
} }
if len(user.Permissions) == 0 {
return &ValidationError{err: "Please grant some permissions to this user"}
}
if !filepath.IsAbs(user.HomeDir) { if !filepath.IsAbs(user.HomeDir) {
return &ValidationError{err: fmt.Sprintf("home_dir must be an absolute path, actual value: %v", user.HomeDir)} return &ValidationError{err: fmt.Sprintf("home_dir must be an absolute path, actual value: %v", user.HomeDir)}
} }

View file

@ -1,6 +1,7 @@
package sftpd_test package sftpd_test
import ( import (
"bufio"
"bytes" "bytes"
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
@ -94,14 +95,15 @@ var (
pubKeyPath string pubKeyPath string
privateKeyPath string privateKeyPath string
gitWrapPath string gitWrapPath string
logFilePath string
) )
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
logfilePath := filepath.Join(configDir, "sftpgo_sftpd_test.log") logFilePath = filepath.Join(configDir, "sftpgo_sftpd_test.log")
loginBannerFileName := "login_banner" loginBannerFileName := "login_banner"
loginBannerFile := filepath.Join(configDir, loginBannerFileName) loginBannerFile := filepath.Join(configDir, loginBannerFileName)
ioutil.WriteFile(loginBannerFile, []byte("simple login banner\n"), 0777) ioutil.WriteFile(loginBannerFile, []byte("simple login banner\n"), 0777)
logger.InitLogger(logfilePath, 5, 1, 28, false, zerolog.DebugLevel) logger.InitLogger(logFilePath, 5, 1, 28, false, zerolog.DebugLevel)
config.LoadConfig(configDir, "") config.LoadConfig(configDir, "")
providerConf := config.GetProviderConf() providerConf := config.GetProviderConf()
@ -195,7 +197,7 @@ func TestMain(m *testing.M) {
waitTCPListening(fmt.Sprintf("%s:%d", httpdConf.BindAddress, httpdConf.BindPort)) waitTCPListening(fmt.Sprintf("%s:%d", httpdConf.BindAddress, httpdConf.BindPort))
exitCode := m.Run() exitCode := m.Run()
os.Remove(logfilePath) os.Remove(logFilePath)
os.Remove(loginBannerFile) os.Remove(loginBannerFile)
os.Remove(pubKeyPath) os.Remove(pubKeyPath)
os.Remove(privateKeyPath) os.Remove(privateKeyPath)
@ -2736,6 +2738,9 @@ func TestBasicGitCommands(t *testing.T) {
out, err = pushToGitRepo(clonePath) out, err = pushToGitRepo(clonePath)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v out: %v", err, string(out)) t.Errorf("unexpected error: %v out: %v", err, string(out))
printLatestLogs(10)
out, err = pushToGitRepo(clonePath)
logger.DebugToConsole("new push out: %v, err: %v", string(out), err)
} }
err = waitQuotaScans() err = waitQuotaScans()
if err != nil { if err != nil {
@ -3919,3 +3924,26 @@ func addFileToGitRepo(repoPath string, fileSize int64) ([]byte, error) {
cmd.Dir = repoPath cmd.Dir = repoPath
return cmd.CombinedOutput() return cmd.CombinedOutput()
} }
func printLatestLogs(maxNumberOfLines int) {
var lines []string
f, err := os.Open(logFilePath)
if err != nil {
return
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines = append(lines, scanner.Text()+"\r\n")
for len(lines) > maxNumberOfLines {
lines = lines[1:]
}
}
if scanner.Err() != nil {
logger.WarnToConsole("Unable to print latest logs: %v", scanner.Err())
return
}
for _, line := range lines {
logger.DebugToConsole(line)
}
}