This commit is contained in:
iratusmachina 2024-06-24 01:55:08 -04:00
parent a976a26fec
commit 96e877b0c2
6 changed files with 188 additions and 71 deletions

View File

@ -2,8 +2,6 @@ package main
import (
"fmt"
"os"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
@ -127,76 +125,30 @@ func TestGenerateConfigFile(t *testing.T) {
}
func TestLoadMainConfigFile(t *testing.T) {
func TestLoadMainConfigFileFromProvidedLocation(t *testing.T) {
tmpDir := t.TempDir()
t.Run("load the main config file correctly - default load", func(t *testing.T) {
tmpDir := t.TempDir()
tempConfigDir := fmt.Sprintf("%s/tmp", tmpDir)
tempLogsDir := fmt.Sprintf("%s/tmp/logs", tmpDir)
tempConfigDir := fmt.Sprintf("%s/tmp/config", tmpDir)
mkDirForTest(t, tempConfigDir)
mkDirForTest(t, tempConfigDir)
defaultConfigJson := map[string]any{
"rulesPath": fmt.Sprintf("%s/rules.json", tempConfigDir),
"logPath": fmt.Sprintf("%s/app.log", tempLogsDir),
"port": "9005",
"compress": false,
"sizeToRotate": "50MB",
}
hmDir, _ := os.UserConfigDir()
tempConfigFile := fmt.Sprintf("%s/confg.json", tempConfigDir)
oldConfigFile := fmt.Sprintf("%s/gocustomurls/config.json", hmDir)
t.Log("oldConfigFile: ", oldConfigFile)
newConfigFile := fmt.Sprintf("%s/config.json", tempConfigDir)
writeJsonForTest(t, defaultConfigJson, tempConfigFile)
ok := doesFileExist(oldConfigFile)
if ok {
t.Log("cp old -> new")
cpFileForTest(t, oldConfigFile, newConfigFile)
}
t.Cleanup(func() {
found := doesFileExist(newConfigFile)
if found {
t.Log("cp new -> old")
cpFileForTest(t, newConfigFile, oldConfigFile)
} else {
// Add a loop to wait until you find the old ConfigFile
ok := doesFileExist(oldConfigFile)
if ok {
t.Log("remove old")
removeFileForTest(t, oldConfigFile)
}
}
})
cfg := &Config{}
assert.Equal(t, cfg.MappingFilePath, "")
conf, err := cfg.LoadMainConfigFile("")
// time.Sleep(4 * time.Second)
assert.Equal(t, err, nil)
assert.NotEmpty(t, conf)
assert.NotEmpty(t, cfg.MappingFilePath)
})
t.Run("load the main config file correctly - load from a location", func(t *testing.T) {
t.SkipNow()
tmpDir := t.TempDir()
tempConfigDir := fmt.Sprintf("%s/tmp", tmpDir)
tempLogsDir := fmt.Sprintf("%s/tmp/logs", tmpDir)
mkDirForTest(t, tempConfigDir)
defaultConfigJson := map[string]string{
"rulesFp": fmt.Sprintf("%s/rules.json", tempConfigDir),
"logFp": fmt.Sprintf("%s/app.log", tempLogsDir),
"Port": "9005",
"Compression": strconv.FormatBool(false),
"SizeToRotate": "50MB",
}
writeJsonForTest(t, defaultConfigJson, fmt.Sprintf("%s/confg.json", tempConfigDir))
cfg := &Config{}
assert.Equal(t, cfg.MappingFilePath, "")
conf, err := cfg.LoadMainConfigFile(fmt.Sprintf("%s/confg.json", tempConfigDir))
assert.Equal(t, err, nil)
assert.NotEmpty(t, conf)
assert.NotEmpty(t, cfg.MappingFilePath)
})
cfg := &Config{}
assert.Equal(t, cfg.MappingFilePath, "")
conf, err := cfg.LoadMainConfigFile(tempConfigFile)
assert.Equal(t, err, nil)
assert.NotEmpty(t, conf)
assert.NotEmpty(t, cfg.MappingFilePath)
}

24
logger_test.go Normal file
View File

@ -0,0 +1,24 @@
package main
import (
"fmt"
"testing"
)
func TestTruncate(t *testing.T) {
tmpDir := t.TempDir()
mkDirForTest(t, fmt.Sprintf("%s/tmp", tmpDir))
rulesJsonFp := "testData/app_over_size.log"
expected := fmt.Sprintf("%s/tmp/app.log", tmpDir)
cpFileForTest(t, rulesJsonFp, expected)
cfg := &LogFile{
path: expected,
}
// Continue from here
}

View File

View File

@ -0,0 +1,66 @@
Stopped serving new connections.
Graceful shutdown complete.
Stopped serving new connections.
Graceful shutdown complete.
gocustomurls: Starting
Stopped serving new connections.
Graceful shutdown complete.
gocustomurls: Starting
Stopped serving new connections.
Graceful shutdown complete.
2024-05-28T14:29:35.767371211-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:42408","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T14:33:59.491487682-04:00 Stopped serving new connections.
2024-05-28T14:33:59.491691358-04:00 Graceful shutdown complete.
2024-05-28T14:48:29.818981344-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:39974","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T15:17:42.692146237-04:00 Stopped serving new connections.
2024-05-28T15:17:42.692268027-04:00 Graceful shutdown complete.
2024-05-28T15:18:11.625875598-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:38550","method":"GET","requestUri":"/x/touche?go-get=1"}
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:49398","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T15:40:09.870951532-04:00 Stopped serving new connections.
2024-05-28T15:40:09.871085499-04:00 Graceful shutdown complete.
2024-05-28T15:40:24.154320042-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:58664","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T16:52:38.449545322-04:00 Stopped serving new connections.
2024-05-28T16:52:38.449782355-04:00 Graceful shutdown complete.
2024-05-28T16:52:47.19006644-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:35120","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T16:54:39.537910226-04:00 Stopped serving new connections.
2024-05-28T16:54:39.538042526-04:00 Graceful shutdown complete.
2024-05-28T16:54:43.991755643-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:42694","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:00:37.549759919-04:00 Stopped serving new connections.
2024-05-28T17:00:37.549895593-04:00 Graceful shutdown complete.
2024-05-28T17:00:39.564002213-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:40190","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:01:53.902149407-04:00 Stopped serving new connections.
2024-05-28T17:01:53.902326423-04:00 Graceful shutdown complete.
2024-05-28T17:01:55.853543897-04:00 Starting
2024-05-28T17:01:57.187935277-04:00 Stopped serving new connections.
2024-05-28T17:01:57.188065162-04:00 Graceful shutdown complete.
2024-05-28T17:02:02.342827445-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:55876","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:04:36.661802337-04:00 Stopped serving new connections.
2024-05-28T17:04:36.661918402-04:00 Graceful shutdown complete.
2024-05-28T17:04:42.004255484-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:42466","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:13:11.636985677-04:00 Stopped serving new connections.
2024-05-28T17:13:11.637150614-04:00 Graceful shutdown complete.
2024-05-28T17:13:19.225477323-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:35882","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:13:37.590697118-04:00 Stopped serving new connections.
2024-05-28T17:13:37.59075443-04:00 Graceful shutdown complete.
2024-05-28T17:14:30.964387887-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:35074","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:14:40.412222772-04:00 Stopped serving new connections.
2024-05-28T17:14:40.412407892-04:00 Graceful shutdown complete.
2024-05-28T17:19:09.579082129-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:52204","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:19:32.491870213-04:00 Stopped serving new connections.
2024-05-28T17:19:32.492012026-04:00 Graceful shutdown complete.
2024-05-28T17:20:12.700323661-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:42938","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T18:08:26.484225491-04:00 Stopped serving new connections.
2024-05-28T18:08:26.484389145-04:00 Graceful shutdown complete.

View File

@ -0,0 +1,56 @@
2024-05-28T14:29:35.767371211-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:42408","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T14:33:59.491487682-04:00 Stopped serving new connections.
2024-05-28T14:33:59.491691358-04:00 Graceful shutdown complete.
2024-05-28T14:48:29.818981344-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:39974","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T15:17:42.692146237-04:00 Stopped serving new connections.
2024-05-28T15:17:42.692268027-04:00 Graceful shutdown complete.
2024-05-28T15:18:11.625875598-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:38550","method":"GET","requestUri":"/x/touche?go-get=1"}
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:49398","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T15:40:09.870951532-04:00 Stopped serving new connections.
2024-05-28T15:40:09.871085499-04:00 Graceful shutdown complete.
2024-05-28T15:40:24.154320042-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:58664","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T16:52:38.449545322-04:00 Stopped serving new connections.
2024-05-28T16:52:38.449782355-04:00 Graceful shutdown complete.
2024-05-28T16:52:47.19006644-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:35120","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T16:54:39.537910226-04:00 Stopped serving new connections.
2024-05-28T16:54:39.538042526-04:00 Graceful shutdown complete.
2024-05-28T16:54:43.991755643-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:42694","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:00:37.549759919-04:00 Stopped serving new connections.
2024-05-28T17:00:37.549895593-04:00 Graceful shutdown complete.
2024-05-28T17:00:39.564002213-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:40190","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:01:53.902149407-04:00 Stopped serving new connections.
2024-05-28T17:01:53.902326423-04:00 Graceful shutdown complete.
2024-05-28T17:01:55.853543897-04:00 Starting
2024-05-28T17:01:57.187935277-04:00 Stopped serving new connections.
2024-05-28T17:01:57.188065162-04:00 Graceful shutdown complete.
2024-05-28T17:02:02.342827445-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:55876","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:04:36.661802337-04:00 Stopped serving new connections.
2024-05-28T17:04:36.661918402-04:00 Graceful shutdown complete.
2024-05-28T17:04:42.004255484-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:42466","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:13:11.636985677-04:00 Stopped serving new connections.
2024-05-28T17:13:11.637150614-04:00 Graceful shutdown complete.
2024-05-28T17:13:19.225477323-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:35882","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:13:37.590697118-04:00 Stopped serving new connections.
2024-05-28T17:13:37.59075443-04:00 Graceful shutdown complete.
2024-05-28T17:14:30.964387887-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:35074","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:14:40.412222772-04:00 Stopped serving new connections.
2024-05-28T17:14:40.412407892-04:00 Graceful shutdown complete.
2024-05-28T17:19:09.579082129-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:52204","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T17:19:32.491870213-04:00 Stopped serving new connections.
2024-05-28T17:19:32.492012026-04:00 Graceful shutdown complete.
2024-05-28T17:20:12.700323661-04:00 Starting
{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"localhost:7070","User-Agent":"HTTPie/3.2.1","ipAddr":"[::1]:42938","method":"GET","requestUri":"/x/touche?go-get=1"}
2024-05-28T18:08:26.484225491-04:00 Stopped serving new connections.
2024-05-28T18:08:26.484389145-04:00 Graceful shutdown complete.

View File

@ -76,10 +76,16 @@ func IsDirEmpty(t *testing.T, name string) bool {
}
func doesFileExist(name string) bool {
_, err := os.Stat(name)
return !errors.Is(err, fs.ErrNotExist)
_, err := os.ReadFile(name)
// defer fp.Close()
return err == nil
}
// func doesFileExist(name string) bool {
// _, err := os.Stat(name)
// return !errors.Is(err, fs.ErrNotExist)
// }
func removeFileForTest(t *testing.T, name string) {
err := os.Remove(name)
if err != nil {
@ -87,10 +93,23 @@ func removeFileForTest(t *testing.T, name string) {
}
}
func writeJsonForTest(t *testing.T, data map[string]string, fp string) {
func writeJsonForTest(t *testing.T, data map[string]any, fp string) {
jsonString, _ := json.Marshal(data)
err := os.WriteFile(fp, jsonString, os.ModePerm)
if err != nil {
t.Fatal(err)
}
}
func isFileEmpty(t *testing.T, name string) bool {
fd, err := os.Open(name)
if err != nil {
t.Fatal(err)
}
defer fd.Close()
finfo, err := fd.Stat()
if err != nil {
t.Fatal(err)
}
return finfo.Size() < 1
}