forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
217 lines (181 loc) · 5.75 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package utils
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
)
func TestCreateLog(t *testing.T) {
Directory = os.Getenv("STATPING_DIR")
err := createLog(Directory)
assert.Nil(t, err)
}
func TestReplaceValue(t *testing.T) {
assert.Equal(t, true, replaceVal(true))
assert.Equal(t, 42, replaceVal(42))
assert.Equal(t, "hello world", replaceVal("hello world"))
assert.Equal(t, "5s", replaceVal(5*time.Second))
}
func TestInitLogs(t *testing.T) {
assert.Nil(t, InitLogs())
require.NotEmpty(t, Params.GetString("STATPING_DIR"))
require.False(t, Params.GetBool("DISABLE_LOGS"))
Log.Infoln("this is a test")
assert.FileExists(t, Directory+"/logs/statping.log")
}
func TestDir(t *testing.T) {
assert.Contains(t, Directory, "statping/statping")
}
func TestCommand(t *testing.T) {
t.SkipNow()
_, out, err := Command("/bin/echo", "\"statping testing\"")
assert.Nil(t, err)
assert.Contains(t, out, "statping")
}
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")))
}
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()))
}
func ExampleToString() {
amount := 42
fmt.Print(ToString(amount))
// Output: 42
}
func TestSaveFile(t *testing.T) {
assert.Nil(t, SaveFile(Directory+"/test.txt", []byte("testing saving a file")))
}
func TestOpenFile(t *testing.T) {
f, err := OpenFile(Directory + "/test.txt")
require.Nil(t, err)
assert.Equal(t, "testing saving a file", f)
}
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"))
assert.Error(t, DeleteFile(Directory+"/missingfilehere.txt"))
}
func TestFormatDuration(t *testing.T) {
dur, _ := time.ParseDuration("158s")
assert.Equal(t, "2 minutes 38 seconds", FormatDuration(dur))
dur, _ = time.ParseDuration("-65s")
assert.Equal(t, "-1 minute 5 seconds", 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))
}
func ExampleDurationReadable() {
dur, _ := time.ParseDuration("25m")
readable := DurationReadable(dur)
fmt.Print(readable)
// Output: 25 minutes
}
func TestLogHTTP(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
assert.Nil(t, err)
assert.NotNil(t, req)
}
func TestStringInt(t *testing.T) {
assert.Equal(t, "1", ToString("1"))
}
func ExampleStringInt() {
amount := "42"
fmt.Print(ToString(amount))
// Output: 42
}
func TestHashPassword(t *testing.T) {
pass := HashPassword("password123")
assert.Equal(t, 60, len(pass))
assert.True(t, CheckHash("password123", pass))
assert.False(t, CheckHash("wrongpasswd", pass))
}
func TestHuman(t *testing.T) {
assert.Equal(t, "10 seconds", Duration{10 * time.Second}.Human())
assert.Equal(t, "1 day 12 hours", Duration{36 * time.Hour}.Human())
assert.Equal(t, "45 minutes", Duration{45 * time.Minute}.Human())
}
func TestSha256Hash(t *testing.T) {
assert.Equal(t, "ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f", Sha256Hash("password123"))
}
func TestNotNumbber(t *testing.T) {
assert.True(t, NotNumber("notint"))
assert.True(t, NotNumber("1293notanint922"))
assert.False(t, NotNumber("0"))
assert.False(t, NotNumber("5"))
}
func TestNewSHA1Hash(t *testing.T) {
hash := NewSHA256Hash()
assert.NotEmpty(t, hash)
assert.Len(t, hash, 64)
assert.Len(t, NewSHA256Hash(), 64)
assert.NotEqual(t, hash, NewSHA256Hash())
}
func TestRandomString(t *testing.T) {
assert.NotEmpty(t, RandomString(5))
}
func TestDeleteDirectory(t *testing.T) {
assert.Nil(t, DeleteDirectory(Directory+"/logs"))
}
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) {
// 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, nil)
assert.Nil(t, err)
assert.Equal(t, []byte("OK"), body)
assert.Equal(t, resp.StatusCode, 200)
}
func TestConfigLoad(t *testing.T) {
err := InitLogs()
require.Nil(t, err)
InitEnvs()
s := Params.GetString
b := Params.GetBool
Params.Set("DB_CONN", "sqlite")
assert.Equal(t, "sqlite", s("DB_CONN"))
assert.Equal(t, Directory, s("STATPING_DIR"))
assert.True(t, b("SAMPLE_DATA"))
assert.True(t, b("ALLOW_REPORTS"))
}
func TestPerlin(t *testing.T) {
p := NewPerlin(2, 2, 5, Now().UnixNano())
require.NotNil(t, p)
for hi := 1.; hi <= 100.; hi++ {
assert.NotZero(t, p.Noise1D(hi/500))
}
}