From 20606a0043fc7b2cfe0c648db6e4c1809dac52e7 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Sun, 29 Dec 2019 23:27:32 +0100 Subject: [PATCH] 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 --- dataprovider/dataprovider.go | 6 +++--- sftpd/sftpd_test.go | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/dataprovider/dataprovider.go b/dataprovider/dataprovider.go index f7ff468f..eb48971a 100644 --- a/dataprovider/dataprovider.go +++ b/dataprovider/dataprovider.go @@ -353,6 +353,9 @@ func buildUserHomeDir(user *User) { } 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) if _, ok := user.Permissions["/"]; !ok { 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 { 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) { return &ValidationError{err: fmt.Sprintf("home_dir must be an absolute path, actual value: %v", user.HomeDir)} } diff --git a/sftpd/sftpd_test.go b/sftpd/sftpd_test.go index ae6eb943..92c36fa0 100644 --- a/sftpd/sftpd_test.go +++ b/sftpd/sftpd_test.go @@ -1,6 +1,7 @@ package sftpd_test import ( + "bufio" "bytes" "crypto/rand" "crypto/sha256" @@ -94,14 +95,15 @@ var ( pubKeyPath string privateKeyPath string gitWrapPath string + logFilePath string ) func TestMain(m *testing.M) { - logfilePath := filepath.Join(configDir, "sftpgo_sftpd_test.log") + logFilePath = filepath.Join(configDir, "sftpgo_sftpd_test.log") loginBannerFileName := "login_banner" loginBannerFile := filepath.Join(configDir, loginBannerFileName) 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, "") providerConf := config.GetProviderConf() @@ -195,7 +197,7 @@ func TestMain(m *testing.M) { waitTCPListening(fmt.Sprintf("%s:%d", httpdConf.BindAddress, httpdConf.BindPort)) exitCode := m.Run() - os.Remove(logfilePath) + os.Remove(logFilePath) os.Remove(loginBannerFile) os.Remove(pubKeyPath) os.Remove(privateKeyPath) @@ -2736,6 +2738,9 @@ func TestBasicGitCommands(t *testing.T) { out, err = pushToGitRepo(clonePath) if err != nil { 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() if err != nil { @@ -3919,3 +3924,26 @@ func addFileToGitRepo(repoPath string, fileSize int64) ([]byte, error) { cmd.Dir = repoPath 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) + } +}