2018-12-04 05:57:11 +00:00
|
|
|
// Statping
|
2018-08-16 06:22:20 +00:00
|
|
|
// Copyright (C) 2018. Hunter Long and the project contributors
|
|
|
|
// Written by Hunter Long <info@socialeck.com> and the project contributors
|
|
|
|
//
|
2018-12-04 04:17:29 +00:00
|
|
|
// https://github.com/hunterlong/statping
|
2018-08-16 06:22:20 +00:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
2018-07-27 04:45:42 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2018-10-06 22:46:26 +00:00
|
|
|
"errors"
|
2018-10-08 04:00:57 +00:00
|
|
|
"fmt"
|
2018-07-27 04:45:42 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-07-28 01:50:13 +00:00
|
|
|
"net/http"
|
2018-07-27 04:45:42 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-08-16 02:22:10 +00:00
|
|
|
func TestCreateLog(t *testing.T) {
|
|
|
|
err := createLog(Directory)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
|
2018-07-27 04:45:42 +00:00
|
|
|
func TestInitLogs(t *testing.T) {
|
|
|
|
assert.Nil(t, InitLogs())
|
2019-01-17 04:59:07 +00:00
|
|
|
assert.FileExists(t, Directory+"/logs/statup.log")
|
2018-10-06 04:31:51 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 01:50:13 +00:00
|
|
|
func TestDir(t *testing.T) {
|
2018-12-04 04:17:29 +00:00
|
|
|
assert.Contains(t, Directory, "github.com/hunterlong/statping")
|
2018-07-28 01:50:13 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 04:31:51 +00:00
|
|
|
func TestCommand(t *testing.T) {
|
2019-02-12 19:13:53 +00:00
|
|
|
t.SkipNow()
|
2018-10-06 04:31:51 +00:00
|
|
|
in, out, err := Command("pwd")
|
|
|
|
assert.Nil(t, err)
|
2019-02-06 21:08:01 +00:00
|
|
|
assert.Contains(t, in, "statping")
|
2018-10-06 04:31:51 +00:00
|
|
|
assert.Empty(t, out)
|
|
|
|
}
|
|
|
|
|
2018-07-28 01:50:13 +00:00
|
|
|
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")))
|
|
|
|
assert.Nil(t, Log(2, errors.New("this is a 2 level error")))
|
|
|
|
assert.Nil(t, Log(3, errors.New("this is a 3 level error")))
|
|
|
|
assert.Nil(t, Log(4, errors.New("this is a 4 level error")))
|
|
|
|
assert.Nil(t, Log(5, errors.New("this is a 5 level error")))
|
|
|
|
}
|
|
|
|
|
2019-02-06 21:08:01 +00:00
|
|
|
func TestToInt(t *testing.T) {
|
|
|
|
assert.Equal(t, int64(55), ToInt("55"))
|
|
|
|
assert.Equal(t, int64(55), ToInt(55))
|
|
|
|
assert.Equal(t, int64(55), ToInt(55.0))
|
|
|
|
assert.Equal(t, int64(55), ToInt([]byte("55")))
|
2018-10-06 04:31:51 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 21:08:01 +00:00
|
|
|
func TestToString(t *testing.T) {
|
|
|
|
assert.Equal(t, "55", ToString(55))
|
|
|
|
assert.Equal(t, "55.000000", ToString(55.0))
|
|
|
|
assert.Equal(t, "55", ToString([]byte("55")))
|
|
|
|
dir, _ := time.ParseDuration("55s")
|
|
|
|
assert.Equal(t, "55s", ToString(dir))
|
|
|
|
assert.Equal(t, "true", ToString(true))
|
|
|
|
assert.Equal(t, time.Now().Format("Monday January _2, 2006 at 03:04PM"), ToString(time.Now()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleToString() {
|
|
|
|
amount := 42
|
|
|
|
fmt.Print(ToString(amount))
|
|
|
|
// Output: 42
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 21:08:01 +00:00
|
|
|
func TestSaveFile(t *testing.T) {
|
|
|
|
assert.Nil(t, SaveFile(Directory+"/test.txt", []byte("testing saving a file")))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFileExists(t *testing.T) {
|
|
|
|
assert.True(t, FileExists(Directory+"/test.txt"))
|
|
|
|
assert.False(t, FileExists(Directory+"fake.txt"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteFile(t *testing.T) {
|
|
|
|
assert.Nil(t, DeleteFile(Directory+"/test.txt"))
|
2018-08-16 02:22:10 +00:00
|
|
|
assert.Error(t, DeleteFile(Directory+"/missingfilehere.txt"))
|
2018-07-28 03:24:54 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 21:08:01 +00:00
|
|
|
func TestFormatDuration(t *testing.T) {
|
|
|
|
dur, _ := time.ParseDuration("158s")
|
|
|
|
assert.Equal(t, "3 minutes", FormatDuration(dur))
|
|
|
|
dur, _ = time.ParseDuration("-65s")
|
|
|
|
assert.Equal(t, "1 minute", FormatDuration(dur))
|
|
|
|
dur, _ = time.ParseDuration("3s")
|
|
|
|
assert.Equal(t, "3 seconds", FormatDuration(dur))
|
|
|
|
dur, _ = time.ParseDuration("48h")
|
|
|
|
assert.Equal(t, "2 days", FormatDuration(dur))
|
|
|
|
dur, _ = time.ParseDuration("12h")
|
|
|
|
assert.Equal(t, "12 hours", FormatDuration(dur))
|
2018-07-28 01:50:13 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 21:08:01 +00:00
|
|
|
func ExampleDurationReadable() {
|
|
|
|
dur, _ := time.ParseDuration("25m")
|
|
|
|
readable := DurationReadable(dur)
|
|
|
|
fmt.Print(readable)
|
|
|
|
// Output: 25 minutes
|
2018-07-27 04:45:42 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 21:08:01 +00:00
|
|
|
func TestLogHTTP(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/", nil)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotEmpty(t, Http(req))
|
2018-10-08 04:00:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 04:45:42 +00:00
|
|
|
func TestStringInt(t *testing.T) {
|
2018-11-21 09:14:31 +00:00
|
|
|
assert.Equal(t, "1", ToString("1"))
|
2018-07-27 04:45:42 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 04:00:57 +00:00
|
|
|
func ExampleStringInt() {
|
|
|
|
amount := "42"
|
2018-11-21 08:45:55 +00:00
|
|
|
fmt.Print(ToString(amount))
|
2018-10-08 04:00:57 +00:00
|
|
|
// Output: 42
|
|
|
|
}
|
|
|
|
|
2018-09-16 07:48:34 +00:00
|
|
|
func TestTimezone(t *testing.T) {
|
2018-09-17 22:13:42 +00:00
|
|
|
zone := float32(-4.0)
|
2018-09-16 07:48:34 +00:00
|
|
|
loc, _ := time.LoadLocation("America/Los_Angeles")
|
2018-09-17 22:13:42 +00:00
|
|
|
timestamp := time.Date(2018, 1, 1, 10, 0, 0, 0, loc)
|
2018-09-16 07:48:34 +00:00
|
|
|
timezone := Timezoner(timestamp, zone)
|
2018-09-17 22:13:42 +00:00
|
|
|
assert.Equal(t, "2018-01-01 10:00:00 -0800 PST", timestamp.String())
|
2018-11-19 07:13:53 +00:00
|
|
|
assert.Equal(t, "2018-01-01 18:00:00 +0000 UTC", timezone.UTC().String())
|
2018-09-16 07:48:34 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 04:45:42 +00:00
|
|
|
func TestTimestamp_Ago(t *testing.T) {
|
|
|
|
now := Timestamp(time.Now())
|
|
|
|
assert.Equal(t, "Just now", now.Ago())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnderScoreString(t *testing.T) {
|
|
|
|
assert.Equal(t, "this_is_a_test", UnderScoreString("this is a test"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHashPassword(t *testing.T) {
|
|
|
|
assert.Equal(t, 60, len(HashPassword("password123")))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewSHA1Hash(t *testing.T) {
|
|
|
|
assert.NotEmpty(t, NewSHA1Hash(5))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRandomString(t *testing.T) {
|
|
|
|
assert.NotEmpty(t, RandomString(5))
|
|
|
|
}
|
|
|
|
|
2018-08-16 01:07:02 +00:00
|
|
|
func TestDeleteDirectory(t *testing.T) {
|
2018-08-16 02:22:10 +00:00
|
|
|
assert.Nil(t, DeleteDirectory(Directory+"/logs"))
|
2018-08-16 01:07:02 +00:00
|
|
|
}
|