From d00cb3d2d4ef52127de475153454ab263218ed8a Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Sun, 29 Dec 2019 19:34:49 -0800 Subject: [PATCH] fixed testing --- core/notifier/notifiers_test.go | 2 + handlers/api_test.go | 2 +- notifiers/notifiers_test.go | 4 +- source/source.go | 8 ++-- source/source_test.go | 3 +- utils/log.go | 83 +++++++-------------------------- utils/utils.go | 13 +++++- 7 files changed, 40 insertions(+), 75 deletions(-) diff --git a/core/notifier/notifiers_test.go b/core/notifier/notifiers_test.go index 03f1edeb..0b32df20 100644 --- a/core/notifier/notifiers_test.go +++ b/core/notifier/notifiers_test.go @@ -18,6 +18,7 @@ package notifier import ( "fmt" "github.com/hunterlong/statping/types" + "github.com/hunterlong/statping/utils" "github.com/jinzhu/gorm" _ "github.com/jinzhu/gorm/dialects/sqlite" "github.com/stretchr/testify/assert" @@ -55,6 +56,7 @@ var core = &types.Core{ } func injectDatabase() { + utils.DeleteFile(dir + "/notifier.db") db, _ = gorm.Open("sqlite3", dir+"/notifier.db") db.CreateTable(&Notification{}) } diff --git a/handlers/api_test.go b/handlers/api_test.go index b78f7558..aa1d1974 100644 --- a/handlers/api_test.go +++ b/handlers/api_test.go @@ -73,7 +73,7 @@ func TestSetupRoutes(t *testing.T) { Body: form.Encode(), ExpectedStatus: 303, HttpHeaders: []string{"Content-Type=application/x-www-form-urlencoded"}, - ExpectedFiles: []string{dir + "/config.yml", dir + "/" + types.SqliteFilename}, + ExpectedFiles: []string{dir + "/config.yml", dir + "/tmp/" + types.SqliteFilename}, }} for _, v := range tests { diff --git a/notifiers/notifiers_test.go b/notifiers/notifiers_test.go index 52924948..e4a3938e 100644 --- a/notifiers/notifiers_test.go +++ b/notifiers/notifiers_test.go @@ -74,8 +74,8 @@ func init() { } func injectDatabase() { - utils.DeleteFile(dir + types.SqliteFilename) - db, err := gorm.Open("sqlite3", dir+"notifiers.db") + utils.DeleteFile(dir + "/notifiers.db") + db, err := gorm.Open("sqlite3", dir+"/notifiers.db") if err != nil { panic(err) } diff --git a/source/source.go b/source/source.go index f4c07967..3e1278fa 100644 --- a/source/source.go +++ b/source/source.go @@ -28,7 +28,7 @@ import ( ) var ( - log = utils.Log.WithField("type", "source") + log = utils.Log.WithField("type", "source") CssBox *rice.Box // CSS files from the 'source/css' directory, this will be loaded into '/assets/css' ScssBox *rice.Box // SCSS files from the 'source/scss' directory, this will be loaded into '/assets/scss' JsBox *rice.Box // JS files from the 'source/js' directory, this will be loaded into '/assets/js' @@ -153,7 +153,7 @@ func CreateAllAssets(folder string) error { // DeleteAllAssets will delete the '/assets' folder func DeleteAllAssets(folder string) error { - err := os.RemoveAll(folder + "/assets") + err := utils.DeleteDirectory(folder + "/assets") if err != nil { log.Infoln(fmt.Sprintf("There was an issue deleting Statping Assets, %v", err)) return err @@ -202,8 +202,8 @@ func CopyToPublic(box *rice.Box, folder, file string) error { // MakePublicFolder will create a new folder func MakePublicFolder(folder string) error { log.Infoln(fmt.Sprintf("Creating folder '%v'", folder)) - if _, err := os.Stat(folder); os.IsNotExist(err) { - err = os.MkdirAll(folder, 0777) + if !utils.FolderExists(folder) { + err := utils.CreateDirectory(folder) if err != nil { log.Errorln(fmt.Sprintf("Failed to created %v directory, %v", folder, err)) return err diff --git a/source/source_test.go b/source/source_test.go index 9bf34bcc..12df5dbc 100644 --- a/source/source_test.go +++ b/source/source_test.go @@ -18,7 +18,6 @@ package source import ( "github.com/hunterlong/statping/utils" "github.com/stretchr/testify/assert" - "os" "testing" ) @@ -30,7 +29,7 @@ func init() { dir = utils.Directory utils.InitLogs() Assets() - os.RemoveAll(dir + "/assets") + utils.DeleteDirectory(dir + "/assets") } func TestCore_UsingAssets(t *testing.T) { diff --git a/utils/log.go b/utils/log.go index fa81e764..2be555c1 100644 --- a/utils/log.go +++ b/utils/log.go @@ -24,21 +24,17 @@ import ( "io" "os" "reflect" - "runtime" "strings" "sync" "time" ) var ( - Log = Logger.StandardLogger() - ljLogger *lumberjack.Logger - LastLines []*LogRow - LockLines sync.Mutex - VerboseMode int - callerInitOnce sync.Once - logrusPackage string - minimumCallerDepth = 1 + Log = Logger.StandardLogger() + ljLogger *lumberjack.Logger + LastLines []*logRow + LockLines sync.Mutex + VerboseMode int ) const logFilePath = "/logs/statping.log" @@ -57,52 +53,13 @@ func (t *hook) Levels() []Logger.Level { return Logger.AllLevels } -// LogCaller retrieves the name of the first non-logrus calling function -func LogCaller(min int) *runtime.Frame { - const maximumCallerDepth = 25 - var minimumCallerDepth = min - // Restrict the lookback frames to avoid runaway lookups - pcs := make([]uintptr, maximumCallerDepth) - depth := runtime.Callers(minimumCallerDepth, pcs) - frames := runtime.CallersFrames(pcs[:depth]) - - // cache this package's fully-qualified name - callerInitOnce.Do(func() { - logrusPackage = getPackageName(runtime.FuncForPC(pcs[0]).Name()) - - fmt.Println("caller once", logrusPackage, minimumCallerDepth, frames) - }) - - for f, again := frames.Next(); again; f, again = frames.Next() { - fmt.Println(f.Func, f.File, f.Line) - return &f - } - - // if we got here, we failed to find the caller's context - return nil -} - -func getPackageName(f string) string { - for { - lastPeriod := strings.LastIndex(f, ".") - lastSlash := strings.LastIndex(f, "/") - if lastPeriod > lastSlash { - f = f[:lastPeriod] - } else { - break - } - } - - return f -} - // ToFields accepts any amount of interfaces to create a new mapping for log.Fields. You will need to // turn on verbose mode by starting Statping with "-v". This function will convert a struct of to the // base struct name, and each field into it's own mapping, for example: // type "*types.Service", on string field "Name" converts to "service_name=value". There is also an // additional field called "_pointer" that will return the pointer hex value. func ToFields(d ...interface{}) map[string]interface{} { - if VerboseMode == 1 { + if VerboseMode <= 1 { return nil } fieldKey := make(map[string]interface{}) @@ -150,16 +107,12 @@ func replaceVal(d interface{}) interface{} { // createLog will create the '/logs' directory based on a directory func createLog(dir string) error { - var err error - _, err = os.Stat(dir + "/logs") - if err != nil { - if os.IsNotExist(err) { - os.Mkdir(dir+"/logs", 0777) - } else { + if !FolderExists(dir + "/logs") { + if err := CreateDirectory(dir + "/logs"); err != nil { return err } } - return err + return nil } // InitLogs will create the '/logs' directory and creates a file '/logs/statup.log' for application logging @@ -183,7 +136,7 @@ func InitLogs() error { }) checkVerboseMode() - LastLines = make([]*LogRow, 0) + LastLines = make([]*logRow, 0) return err } @@ -226,7 +179,7 @@ func pushLastLine(line interface{}) { } // GetLastLine returns 1 line for a recent log entry -func GetLastLine() *LogRow { +func GetLastLine() *logRow { LockLines.Lock() defer LockLines.Unlock() if len(LastLines) > 0 { @@ -235,19 +188,19 @@ func GetLastLine() *LogRow { return nil } -type LogRow struct { +type logRow struct { Date time.Time Line interface{} } -func newLogRow(line interface{}) (logRow *LogRow) { - logRow = new(LogRow) - logRow.Date = time.Now() - logRow.Line = line +func newLogRow(line interface{}) (lgRow *logRow) { + lgRow = new(logRow) + lgRow.Date = time.Now() + lgRow.Line = line return } -func (o *LogRow) lineAsString() string { +func (o *logRow) lineAsString() string { switch v := o.Line.(type) { case string: return v @@ -259,6 +212,6 @@ func (o *LogRow) lineAsString() string { return "" } -func (o *LogRow) FormatForHtml() string { +func (o *logRow) FormatForHtml() string { return fmt.Sprintf("%s: %s", o.Date.Format("2006-01-02 15:04:05"), o.lineAsString()) } diff --git a/utils/utils.go b/utils/utils.go index a188fcfb..ebef85ea 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -199,7 +199,18 @@ func DeleteDirectory(directory string) error { // CreateDirectory("assets") func CreateDirectory(directory string) error { Log.Infoln("creating directory: " + directory) - return os.Mkdir(directory, os.ModePerm) + if err := os.Mkdir(directory, os.ModePerm); err != os.ErrExist { + return err + } + return nil +} + +// FolderExists will return true if the folder exists +func FolderExists(folder string) bool { + if _, err := os.Stat(folder); os.IsExist(err) { + return true + } + return false } // CopyFile will copy a file to a new directory