mirror of https://github.com/statping/statping
testing coverage - notifiers - removed old
parent
62f159695a
commit
ec3287e08d
|
@ -380,8 +380,8 @@ func isEnabled(n interface{}) bool {
|
|||
}
|
||||
|
||||
// inLimits will return true if the notifier is within sending limits
|
||||
func inLimits(n Notifier) bool {
|
||||
notifier := n.Select()
|
||||
func inLimits(n interface{}) bool {
|
||||
notifier := n.(Notifier).Select()
|
||||
ok, _ := notifier.WithinLimits()
|
||||
return ok
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ func Assets() {
|
|||
TmplBox = rice.MustFindBox("tmpl")
|
||||
}
|
||||
|
||||
// HelpMarkdown will return the Markdown of help.md into HTML
|
||||
func HelpMarkdown() string {
|
||||
helpSrc, err := TmplBox.Bytes("help.md")
|
||||
if err != nil {
|
||||
|
|
|
@ -17,7 +17,6 @@ package utils
|
|||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"math/rand"
|
||||
|
@ -52,11 +51,3 @@ func RandomString(n int) string {
|
|||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// Sha256 returns a SHA256 hash as string from []byte
|
||||
func Sha256(data []byte) string {
|
||||
h := sha1.New()
|
||||
h.Write(data)
|
||||
sha1_hash := hex.EncodeToString(h.Sum(nil))
|
||||
return sha1_hash
|
||||
}
|
||||
|
|
35
utils/log.go
35
utils/log.go
|
@ -24,6 +24,7 @@ import (
|
|||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -34,10 +35,6 @@ var (
|
|||
LockLines sync.Mutex
|
||||
)
|
||||
|
||||
func Logger() *lumberjack.Logger {
|
||||
return ljLogger
|
||||
}
|
||||
|
||||
// createLog will create the '/logs' directory based on a directory
|
||||
func createLog(dir string) error {
|
||||
var err error
|
||||
|
@ -137,7 +134,7 @@ func Http(r *http.Request) string {
|
|||
func pushLastLine(line interface{}) {
|
||||
LockLines.Lock()
|
||||
defer LockLines.Unlock()
|
||||
LastLines = append(LastLines, NewLogRow(line))
|
||||
LastLines = append(LastLines, newLogRow(line))
|
||||
// We want to store max 1000 lines in memory (for /logs page).
|
||||
for len(LastLines) > 1000 {
|
||||
LastLines = LastLines[1:]
|
||||
|
@ -153,3 +150,31 @@ func GetLastLine() *LogRow {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type LogRow struct {
|
||||
Date time.Time
|
||||
Line interface{}
|
||||
}
|
||||
|
||||
func newLogRow(line interface{}) (logRow *LogRow) {
|
||||
logRow = new(LogRow)
|
||||
logRow.Date = time.Now()
|
||||
logRow.Line = line
|
||||
return
|
||||
}
|
||||
|
||||
func (o *LogRow) lineAsString() string {
|
||||
switch v := o.Line.(type) {
|
||||
case string:
|
||||
return v
|
||||
case error:
|
||||
return v.Error()
|
||||
case []byte:
|
||||
return string(v)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (o *LogRow) FormatForHtml() string {
|
||||
return fmt.Sprintf("%s: %s", o.Date.Format("2006-01-02 15:04:05"), o.lineAsString())
|
||||
}
|
||||
|
|
|
@ -1,49 +0,0 @@
|
|||
// Statup
|
||||
// Copyright (C) 2018. Hunter Long and the project contributors
|
||||
// Written by Hunter Long <info@socialeck.com> and the project contributors
|
||||
//
|
||||
// https://github.com/hunterlong/statup
|
||||
//
|
||||
// The licenses for most software and other practical works are designed
|
||||
// to take away your freedom to share and change the works. By contrast,
|
||||
// the GNU General Public License is intended to guarantee your freedom to
|
||||
// share and change all versions of a program--to make sure it remains free
|
||||
// software for all its users.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type LogRow struct {
|
||||
Date time.Time
|
||||
Line interface{}
|
||||
}
|
||||
|
||||
func NewLogRow(line interface{}) (logRow *LogRow) {
|
||||
logRow = new(LogRow)
|
||||
logRow.Date = time.Now()
|
||||
logRow.Line = line
|
||||
return
|
||||
}
|
||||
|
||||
func (o *LogRow) LineAsString() string {
|
||||
switch v := o.Line.(type) {
|
||||
case string:
|
||||
return v
|
||||
case error:
|
||||
return v.Error()
|
||||
case []byte:
|
||||
return string(v)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (o *LogRow) FormatForHtml() string {
|
||||
return fmt.Sprintf("%s: %s", o.Date.Format("2006-01-02 15:04:05"), o.LineAsString())
|
||||
}
|
|
@ -33,10 +33,27 @@ func TestInitLogs(t *testing.T) {
|
|||
assert.FileExists(t, "../logs/statup.log")
|
||||
}
|
||||
|
||||
func TestFileExists(t *testing.T) {
|
||||
assert.True(t, FileExists("../logs/statup.log"))
|
||||
}
|
||||
|
||||
func TestDir(t *testing.T) {
|
||||
assert.Contains(t, Directory, "github.com/hunterlong/statup")
|
||||
}
|
||||
|
||||
func TestCommand(t *testing.T) {
|
||||
in, out, err := Command("pwd")
|
||||
assert.Nil(t, err)
|
||||
assert.Contains(t, in, "statup")
|
||||
assert.Empty(t, out)
|
||||
}
|
||||
|
||||
func TestDurationReadable(t *testing.T) {
|
||||
dur, _ := time.ParseDuration("1505s")
|
||||
readable := DurationReadable(dur)
|
||||
assert.Equal(t, "25 minutes", readable)
|
||||
}
|
||||
|
||||
func TestLog(t *testing.T) {
|
||||
assert.Nil(t, Log(0, errors.New("this is a 0 level error")))
|
||||
assert.Nil(t, Log(1, errors.New("this is a 1 level error")))
|
||||
|
@ -46,6 +63,15 @@ func TestLog(t *testing.T) {
|
|||
assert.Nil(t, Log(5, errors.New("this is a 5 level error")))
|
||||
}
|
||||
|
||||
func TestFormatDuration(t *testing.T) {
|
||||
dur, _ := time.ParseDuration("158s")
|
||||
formatted := FormatDuration(dur)
|
||||
assert.Equal(t, "3 minutes", formatted)
|
||||
dur, _ = time.ParseDuration("-65s")
|
||||
formatted = FormatDuration(dur)
|
||||
assert.Equal(t, "1 minute", formatted)
|
||||
}
|
||||
|
||||
func TestDeleteFile(t *testing.T) {
|
||||
assert.Nil(t, DeleteFile(Directory+"/logs/statup.log"))
|
||||
}
|
||||
|
@ -102,10 +128,6 @@ func TestRandomString(t *testing.T) {
|
|||
assert.NotEmpty(t, RandomString(5))
|
||||
}
|
||||
|
||||
func TestSha256(t *testing.T) {
|
||||
assert.Equal(t, "dc724af18fbdd4e59189f5fe768a5f8311527050", Sha256([]byte("testing")))
|
||||
}
|
||||
|
||||
func TestDeleteDirectory(t *testing.T) {
|
||||
assert.Nil(t, DeleteDirectory(Directory+"/logs"))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue