statping/utils/utils_test.go

207 lines
5.3 KiB
Go
Raw Normal View History

2018-07-27 04:45:42 +00:00
package utils
import (
2018-10-08 04:00:57 +00:00
"fmt"
2018-07-27 04:45:42 +00:00
"github.com/stretchr/testify/assert"
2020-03-22 07:09:45 +00:00
"github.com/stretchr/testify/require"
2018-07-28 01:50:13 +00:00
"net/http"
"net/http/httptest"
2020-03-22 07:09:45 +00:00
"os"
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())
Log.Infoln("this is a test")
assert.FileExists(t, Directory+"/logs/statping.log")
}
2018-07-28 01:50:13 +00:00
func TestDir(t *testing.T) {
2020-03-09 18:17:55 +00:00
assert.Contains(t, Directory, "github.com/statping/statping")
2018-07-28 01:50:13 +00:00
}
func TestCommand(t *testing.T) {
2020-04-15 10:27:12 +00:00
t.SkipNow()
2020-04-15 10:18:39 +00:00
_, out, err := Command("/bin/echo", "\"statping testing\"")
assert.Nil(t, err)
2020-04-15 10:18:39 +00:00
assert.Contains(t, out, "statping")
}
func TestReplaceTemplate(t *testing.T) {
type Object struct {
Id int64
String string
Online bool
Example string
}
example := &Object{
1, "this is an example", true, "it should work",
}
result := ReplaceTemplate(`{"id": {{.Object.Id}} }`, example)
assert.Equal(t, "{\"id\": 1 }", result)
}
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")))
}
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, Now().Format("Monday January _2, 2006 at 03:04PM"), ToString(Now()))
2019-02-06 21:08:01 +00:00
}
func ExampleToString() {
amount := 42
fmt.Print(ToString(amount))
// Output: 42
}
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")))
}
2020-03-29 01:21:32 +00:00
func TestOpenFile(t *testing.T) {
f, err := OpenFile(Directory + "/test.txt")
require.Nil(t, err)
assert.Equal(t, "testing saving a file", f)
}
2019-02-06 21:08:01 +00:00
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.NotNil(t, 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"
fmt.Print(ToString(amount))
2018-10-08 04:00:57 +00:00
// Output: 42
}
func TestTimezone(t *testing.T) {
2018-09-17 22:13:42 +00:00
zone := float32(-4.0)
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)
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())
assert.Equal(t, "2018-01-01 18:00:00 +0000 UTC", timezone.UTC().String())
}
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 TestHashPassword(t *testing.T) {
assert.Equal(t, 60, len(HashPassword("password123")))
}
func TestNewSHA1Hash(t *testing.T) {
2020-03-19 01:50:53 +00:00
hash := NewSHA256Hash()
assert.NotEmpty(t, hash)
assert.Len(t, hash, 64)
assert.Len(t, NewSHA256Hash(), 64)
assert.NotEqual(t, hash, NewSHA256Hash())
2018-07-27 04:45:42 +00:00
}
func TestRandomString(t *testing.T) {
assert.NotEmpty(t, RandomString(5))
}
func TestDeleteDirectory(t *testing.T) {
2018-08-16 02:22:10 +00:00
assert.Nil(t, DeleteDirectory(Directory+"/logs"))
}
2020-03-22 07:09:45 +00:00
func TestRenameDirectory(t *testing.T) {
assert.Nil(t, CreateDirectory(Directory+"/example"))
require.DirExists(t, Directory+"/example")
assert.Nil(t, RenameDirectory(Directory+"/example", Directory+"/renamed_example"))
require.DirExists(t, Directory+"/renamed_example")
assert.Nil(t, os.RemoveAll(Directory+"/renamed_example"))
}
func TestHttpRequest(t *testing.T) {
2020-03-19 02:57:59 +00:00
// Start a local HTTP server
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
// Test request parameters
assert.Equal(t, req.URL.String(), "/")
assert.Equal(t, req.Header["Aaa"], []string{"bbbb="})
assert.Equal(t, req.Header["Ccc"], []string{"ddd"})
// Send response to be tested
rw.Write([]byte(`OK`))
}))
// Close the server when test finishes
defer server.Close()
body, resp, err := HttpRequest(server.URL, "GET", "application/json", []string{"aaa=bbbb=", "ccc=ddd"}, nil, 2*time.Second, false)
assert.Nil(t, err)
assert.Equal(t, []byte("OK"), body)
assert.Equal(t, resp.StatusCode, 200)
}
2020-04-16 17:32:54 +00:00
func TestConfigLoad(t *testing.T) {
os.Setenv("DB_CONN", "sqlite")
InitCLI()
setDefaults()
s := Params.GetString
b := Params.GetBool
assert.Equal(t, "sqlite", s("DB_CONN"))
assert.Equal(t, Directory, s("STATPING_DIR"))
assert.True(t, b("SAMPLE_DATA"))
assert.False(t, b("DISABLE_LOGS"))
assert.False(t, b("ALLOW_REPORTS"))
}