From cfdee4659b6a18626aa9b8ee743dc127b751d08e Mon Sep 17 00:00:00 2001 From: hunterlong Date: Thu, 4 Jun 2020 07:08:53 -0700 Subject: [PATCH] tests --- cmd/cli_test.go | 2 +- cmd/main.go | 2 +- dev/postman.json | 120 ++++++++++++++++++++++++++++++ types/checkins/checkins_test.go | 2 + types/configs/config_test.go | 5 ++ types/groups/groups_test.go | 3 + types/incidents/incidents_test.go | 3 + types/messages/messages_test.go | 2 + types/services/services_test.go | 3 + types/users/users_test.go | 3 + utils/configs.go | 6 +- utils/log.go | 7 +- utils/utils_test.go | 5 +- 13 files changed, 156 insertions(+), 7 deletions(-) diff --git a/cmd/cli_test.go b/cmd/cli_test.go index 43165ba2..94de537d 100644 --- a/cmd/cli_test.go +++ b/cmd/cli_test.go @@ -14,7 +14,7 @@ var ( ) func init() { - utils.InitCLI() + utils.InitLogs() } func TestStatpingDirectory(t *testing.T) { diff --git a/cmd/main.go b/cmd/main.go index bffac4d6..2969aafc 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -35,7 +35,7 @@ func init() { rootCmd.AddCommand(onceCmd) rootCmd.AddCommand(envCmd) rootCmd.AddCommand(resetCmd) - utils.InitCLI() + utils.InitLogs() parseFlags(rootCmd) } diff --git a/dev/postman.json b/dev/postman.json index eb07a2ad..1e736e95 100644 --- a/dev/postman.json +++ b/dev/postman.json @@ -126,6 +126,11 @@ "key": "sample_data", "value": "true", "type": "text" + }, + { + "key": "newletter", + "value": "true", + "type": "text" } ], "options": { @@ -259,6 +264,121 @@ } ] }, + { + "name": "Newletter", + "event": [ + { + "listen": "test", + "script": { + "id": "7caef526-8b4b-4dbe-a36c-98ce06c77309", + "exec": [ + "" + ], + "type": "text/javascript" + } + } + ], + "request": { + "auth": { + "type": "noauth" + }, + "method": "POST", + "header": [], + "body": { + "mode": "urlencoded", + "urlencoded": [ + { + "key": "email", + "value": "example@gmail.com", + "type": "text" + }, + { + "key": "domain", + "value": "http://localhost:8080", + "type": "text" + }, + { + "key": "timezone", + "value": "PST", + "type": "text" + } + ] + }, + "url": { + "raw": "https://news.statping.com/new", + "protocol": "https", + "host": [ + "news", + "statping", + "com" + ], + "path": [ + "new" + ] + }, + "description": "The API endpoint for users to add their email to the Statping newsletter." + }, + "response": [ + { + "name": "Newletter", + "originalRequest": { + "method": "POST", + "header": [], + "body": { + "mode": "urlencoded", + "urlencoded": [ + { + "key": "email", + "value": "example@gmail.com", + "type": "text" + }, + { + "key": "domain", + "value": "http://localhost:8080", + "type": "text" + }, + { + "key": "timezone", + "value": "PST", + "type": "text" + } + ] + }, + "url": { + "raw": "https://news.statping.com/new", + "protocol": "https", + "host": [ + "news", + "statping", + "com" + ], + "path": [ + "new" + ] + } + }, + "status": "OK", + "code": 200, + "_postman_previewlanguage": "json", + "header": [ + { + "key": "Content-Length", + "value": "27" + }, + { + "key": "Content-Type", + "value": "application/json" + }, + { + "key": "Date", + "value": "Fri, 29 May 2020 22:01:45 GMT" + } + ], + "cookie": [], + "body": "{\n \"data\": {\n \"id\": \"Q21FaAQvH\"\n }\n}" + } + ] + }, { "name": "Logs", "event": [ diff --git a/types/checkins/checkins_test.go b/types/checkins/checkins_test.go index 9ea61a53..28728a71 100644 --- a/types/checkins/checkins_test.go +++ b/types/checkins/checkins_test.go @@ -34,6 +34,8 @@ var testCheckinHits = []*CheckinHit{{ var testApiKey string func TestInit(t *testing.T) { + err := utils.InitLogs() + require.Nil(t, err) db, err := database.OpenTester() require.Nil(t, err) db.AutoMigrate(&Checkin{}, &CheckinHit{}, &failures.Failure{}) diff --git a/types/configs/config_test.go b/types/configs/config_test.go index b211a22b..946b052e 100644 --- a/types/configs/config_test.go +++ b/types/configs/config_test.go @@ -1,10 +1,15 @@ package configs import ( + "github.com/statping/statping/utils" "github.com/stretchr/testify/require" "testing" ) +func init() { + utils.InitLogs() +} + func TestSQLiteConfig(t *testing.T) { sqlite := &DbConfig{ DbConn: "sqlite", diff --git a/types/groups/groups_test.go b/types/groups/groups_test.go index dfdc0436..c0100d37 100644 --- a/types/groups/groups_test.go +++ b/types/groups/groups_test.go @@ -4,6 +4,7 @@ import ( "github.com/statping/statping/database" "github.com/statping/statping/types/null" "github.com/statping/statping/types/services" + "github.com/statping/statping/utils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "testing" @@ -31,6 +32,8 @@ var s2 = &services.Service{ } func TestInit(t *testing.T) { + err := utils.InitLogs() + require.Nil(t, err) db, err := database.OpenTester() require.Nil(t, err) db.CreateTable(&Group{}, &services.Service{}) diff --git a/types/incidents/incidents_test.go b/types/incidents/incidents_test.go index 115427ce..377680a4 100644 --- a/types/incidents/incidents_test.go +++ b/types/incidents/incidents_test.go @@ -2,6 +2,7 @@ package incidents import ( "github.com/statping/statping/database" + "github.com/statping/statping/utils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "testing" @@ -26,6 +27,8 @@ var update2 = &IncidentUpdate{ } func TestInit(t *testing.T) { + err := utils.InitLogs() + require.Nil(t, err) db, err := database.OpenTester() require.Nil(t, err) db.AutoMigrate(&Incident{}, &IncidentUpdate{}) diff --git a/types/messages/messages_test.go b/types/messages/messages_test.go index fda3e71f..a47ab6e6 100644 --- a/types/messages/messages_test.go +++ b/types/messages/messages_test.go @@ -18,6 +18,8 @@ var example = &Message{ } func TestInit(t *testing.T) { + err := utils.InitLogs() + require.Nil(t, err) db, err := database.OpenTester() require.Nil(t, err) db.CreateTable(&Message{}) diff --git a/types/services/services_test.go b/types/services/services_test.go index 3bd4531f..dc44f467 100644 --- a/types/services/services_test.go +++ b/types/services/services_test.go @@ -93,6 +93,9 @@ func (s *exampleGRPC) GetFeature(ctx context.Context, point *pb.Point) (*pb.Feat } func TestStartExampleEndpoints(t *testing.T) { + err := utils.InitLogs() + require.Nil(t, err) + // root CA for Linux: /etc/ssl/certs/ca-certificates.crt // root CA for MacOSX: /opt/local/share/curl/curl-ca-bundle.crt diff --git a/types/users/users_test.go b/types/users/users_test.go index ca1e4f57..c3104539 100644 --- a/types/users/users_test.go +++ b/types/users/users_test.go @@ -3,6 +3,7 @@ package users import ( "github.com/statping/statping/database" "github.com/statping/statping/types/null" + "github.com/statping/statping/utils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "testing" @@ -16,6 +17,8 @@ var example = &User{ } func TestInit(t *testing.T) { + err := utils.InitLogs() + require.Nil(t, err) db, err := database.OpenTester() require.Nil(t, err) db.CreateTable(&User{}) diff --git a/utils/configs.go b/utils/configs.go index 420b5144..88f4bcf0 100644 --- a/utils/configs.go +++ b/utils/configs.go @@ -12,7 +12,7 @@ var ( configLog = Log.WithField("type", "configs") ) -func InitCLI() { +func initCLI() { Params = viper.New() Params.AutomaticEnv() setDefaults() @@ -43,6 +43,7 @@ func InitCLI() { } func setDefaults() { + var err error defaultDir, err := os.Getwd() if err != nil { configLog.Errorln(err) @@ -69,6 +70,9 @@ func setDefaults() { Params.SetDefault("REMOVE_AFTER", 2160*time.Hour) Params.SetDefault("CLEANUP_INTERVAL", 1*time.Hour) Params.SetDefault("LANGUAGE", "en") + Params.SetDefault("LOGS_MAX_COUNT", 5) + Params.SetDefault("LOGS_MAX_AGE", 28) + Params.SetDefault("LOGS_MAX_SIZE", 16) dbConn := Params.GetString("DB_CONN") dbInt := Params.GetInt("DB_PORT") diff --git a/utils/log.go b/utils/log.go index 71dabbcf..4088e146 100644 --- a/utils/log.go +++ b/utils/log.go @@ -146,6 +146,7 @@ func createLog(dir string) error { // InitLogs will create the '/logs' directory and creates a file '/logs/statup.log' for application logging func InitLogs() error { + initCLI() if Params.GetBool("DISABLE_LOGS") { return nil } @@ -154,9 +155,9 @@ func InitLogs() error { } ljLogger = &lumberjack.Logger{ Filename: Directory + logFilePath, - MaxSize: 16, - MaxBackups: 5, - MaxAge: 28, + MaxSize: Params.GetInt("LOGS_MAX_SIZE"), + MaxBackups: Params.GetInt("LOGS_MAX_COUNT"), + MaxAge: Params.GetInt("LOGS_MAX_AGE"), } mw := io.MultiWriter(os.Stdout, ljLogger) diff --git a/utils/utils_test.go b/utils/utils_test.go index 4aec3132..15088251 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -25,6 +25,9 @@ func TestReplaceValue(t *testing.T) { 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") } @@ -182,7 +185,7 @@ func TestHttpRequest(t *testing.T) { } func TestConfigLoad(t *testing.T) { - InitCLI() + initCLI() setDefaults() s := Params.GetString