2016-05-02 11:42:21 +00:00
|
|
|
package config
|
2016-04-01 03:01:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2016-07-18 01:50:18 +00:00
|
|
|
"runtime"
|
2016-04-01 03:01:54 +00:00
|
|
|
"testing"
|
2017-01-19 08:56:30 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2016-04-01 03:01:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Test file is missing
|
|
|
|
func TestMissingFile(t *testing.T) {
|
|
|
|
filename := "test"
|
2017-10-23 03:06:30 +00:00
|
|
|
_, err := LoadConf(filename)
|
2016-04-01 03:01:54 +00:00
|
|
|
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
2016-07-18 01:50:18 +00:00
|
|
|
type ConfigTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
ConfGorushDefault ConfYaml
|
|
|
|
ConfGorush ConfYaml
|
|
|
|
}
|
2016-04-01 03:01:54 +00:00
|
|
|
|
2016-07-18 01:50:18 +00:00
|
|
|
func (suite *ConfigTestSuite) SetupTest() {
|
|
|
|
var err error
|
2017-10-23 03:06:30 +00:00
|
|
|
suite.ConfGorushDefault, err = LoadConf("")
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to load default config.yml")
|
|
|
|
}
|
|
|
|
suite.ConfGorush, err = LoadConf("config.yml")
|
2016-07-18 01:50:18 +00:00
|
|
|
if err != nil {
|
2017-10-23 03:06:30 +00:00
|
|
|
panic("failed to load config.yml from file")
|
2016-07-18 01:50:18 +00:00
|
|
|
}
|
2016-04-01 03:01:54 +00:00
|
|
|
}
|
2016-05-04 01:40:48 +00:00
|
|
|
|
2016-07-18 01:50:18 +00:00
|
|
|
func (suite *ConfigTestSuite) TestValidateConfDefault() {
|
|
|
|
// Core
|
2017-07-31 08:31:07 +00:00
|
|
|
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Core.Address)
|
2016-07-18 01:50:18 +00:00
|
|
|
assert.Equal(suite.T(), "8088", suite.ConfGorushDefault.Core.Port)
|
2017-07-27 03:19:36 +00:00
|
|
|
assert.Equal(suite.T(), true, suite.ConfGorushDefault.Core.Enabled)
|
2016-09-02 07:55:47 +00:00
|
|
|
assert.Equal(suite.T(), int64(runtime.NumCPU()), suite.ConfGorushDefault.Core.WorkerNum)
|
|
|
|
assert.Equal(suite.T(), int64(8192), suite.ConfGorushDefault.Core.QueueNum)
|
2016-07-18 01:50:18 +00:00
|
|
|
assert.Equal(suite.T(), "release", suite.ConfGorushDefault.Core.Mode)
|
2017-04-06 07:00:49 +00:00
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Core.Sync)
|
2016-07-18 01:50:18 +00:00
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Core.SSL)
|
|
|
|
assert.Equal(suite.T(), "cert.pem", suite.ConfGorushDefault.Core.CertPath)
|
|
|
|
assert.Equal(suite.T(), "key.pem", suite.ConfGorushDefault.Core.KeyPath)
|
2016-09-02 07:55:47 +00:00
|
|
|
assert.Equal(suite.T(), int64(100), suite.ConfGorushDefault.Core.MaxNotification)
|
2016-07-29 01:38:06 +00:00
|
|
|
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Core.HTTPProxy)
|
2016-08-19 13:16:10 +00:00
|
|
|
// Pid
|
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Core.PID.Enabled)
|
|
|
|
assert.Equal(suite.T(), "gorush.pid", suite.ConfGorushDefault.Core.PID.Path)
|
2017-10-23 03:06:30 +00:00
|
|
|
assert.Equal(suite.T(), true, suite.ConfGorushDefault.Core.PID.Override)
|
2017-04-05 04:03:43 +00:00
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Core.AutoTLS.Enabled)
|
|
|
|
assert.Equal(suite.T(), ".cache", suite.ConfGorushDefault.Core.AutoTLS.Folder)
|
|
|
|
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Core.AutoTLS.Host)
|
2016-07-18 01:50:18 +00:00
|
|
|
|
|
|
|
// Api
|
|
|
|
assert.Equal(suite.T(), "/api/push", suite.ConfGorushDefault.API.PushURI)
|
|
|
|
assert.Equal(suite.T(), "/api/stat/go", suite.ConfGorushDefault.API.StatGoURI)
|
|
|
|
assert.Equal(suite.T(), "/api/stat/app", suite.ConfGorushDefault.API.StatAppURI)
|
|
|
|
assert.Equal(suite.T(), "/api/config", suite.ConfGorushDefault.API.ConfigURI)
|
|
|
|
assert.Equal(suite.T(), "/sys/stats", suite.ConfGorushDefault.API.SysStatURI)
|
2017-01-19 08:56:30 +00:00
|
|
|
assert.Equal(suite.T(), "/metrics", suite.ConfGorushDefault.API.MetricURI)
|
2017-10-26 02:19:58 +00:00
|
|
|
assert.Equal(suite.T(), "/healthz", suite.ConfGorushDefault.API.HealthURI)
|
2016-07-18 01:50:18 +00:00
|
|
|
|
|
|
|
// Android
|
2017-10-23 03:06:30 +00:00
|
|
|
assert.Equal(suite.T(), true, suite.ConfGorushDefault.Android.Enabled)
|
|
|
|
assert.Equal(suite.T(), "YOUR_API_KEY", suite.ConfGorushDefault.Android.APIKey)
|
2016-10-23 10:19:51 +00:00
|
|
|
assert.Equal(suite.T(), 0, suite.ConfGorushDefault.Android.MaxRetry)
|
2016-07-18 01:50:18 +00:00
|
|
|
|
|
|
|
// iOS
|
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Ios.Enabled)
|
|
|
|
assert.Equal(suite.T(), "key.pem", suite.ConfGorushDefault.Ios.KeyPath)
|
|
|
|
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Ios.Password)
|
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Ios.Production)
|
2016-10-23 10:19:51 +00:00
|
|
|
assert.Equal(suite.T(), 0, suite.ConfGorushDefault.Ios.MaxRetry)
|
2017-10-25 08:49:23 +00:00
|
|
|
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Ios.KeyID)
|
|
|
|
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Ios.TeamID)
|
2016-07-18 01:50:18 +00:00
|
|
|
|
|
|
|
// log
|
|
|
|
assert.Equal(suite.T(), "string", suite.ConfGorushDefault.Log.Format)
|
|
|
|
assert.Equal(suite.T(), "stdout", suite.ConfGorushDefault.Log.AccessLog)
|
|
|
|
assert.Equal(suite.T(), "debug", suite.ConfGorushDefault.Log.AccessLevel)
|
|
|
|
assert.Equal(suite.T(), "stderr", suite.ConfGorushDefault.Log.ErrorLog)
|
|
|
|
assert.Equal(suite.T(), "error", suite.ConfGorushDefault.Log.ErrorLevel)
|
|
|
|
assert.Equal(suite.T(), true, suite.ConfGorushDefault.Log.HideToken)
|
|
|
|
|
|
|
|
assert.Equal(suite.T(), "memory", suite.ConfGorushDefault.Stat.Engine)
|
|
|
|
assert.Equal(suite.T(), "localhost:6379", suite.ConfGorushDefault.Stat.Redis.Addr)
|
|
|
|
assert.Equal(suite.T(), "", suite.ConfGorushDefault.Stat.Redis.Password)
|
2016-09-19 09:00:14 +00:00
|
|
|
assert.Equal(suite.T(), 0, suite.ConfGorushDefault.Stat.Redis.DB)
|
2016-07-18 01:50:18 +00:00
|
|
|
|
2016-09-19 13:52:40 +00:00
|
|
|
assert.Equal(suite.T(), "bolt.db", suite.ConfGorushDefault.Stat.BoltDB.Path)
|
2016-07-18 01:50:18 +00:00
|
|
|
assert.Equal(suite.T(), "gorush", suite.ConfGorushDefault.Stat.BoltDB.Bucket)
|
2016-08-02 07:35:28 +00:00
|
|
|
|
2016-09-19 13:52:40 +00:00
|
|
|
assert.Equal(suite.T(), "bunt.db", suite.ConfGorushDefault.Stat.BuntDB.Path)
|
|
|
|
assert.Equal(suite.T(), "level.db", suite.ConfGorushDefault.Stat.LevelDB.Path)
|
2017-07-24 10:58:30 +00:00
|
|
|
|
|
|
|
// gRPC
|
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorushDefault.GRPC.Enabled)
|
2017-10-19 01:24:47 +00:00
|
|
|
assert.Equal(suite.T(), "9000", suite.ConfGorushDefault.GRPC.Port)
|
2016-07-18 01:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ConfigTestSuite) TestValidateConf() {
|
|
|
|
// Core
|
|
|
|
assert.Equal(suite.T(), "8088", suite.ConfGorush.Core.Port)
|
2017-07-27 03:19:36 +00:00
|
|
|
assert.Equal(suite.T(), true, suite.ConfGorush.Core.Enabled)
|
2017-01-25 08:29:35 +00:00
|
|
|
assert.Equal(suite.T(), int64(runtime.NumCPU()), suite.ConfGorush.Core.WorkerNum)
|
2016-09-02 07:55:47 +00:00
|
|
|
assert.Equal(suite.T(), int64(8192), suite.ConfGorush.Core.QueueNum)
|
2016-07-18 01:50:18 +00:00
|
|
|
assert.Equal(suite.T(), "release", suite.ConfGorush.Core.Mode)
|
2017-04-06 07:00:49 +00:00
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorush.Core.Sync)
|
2016-07-18 01:50:18 +00:00
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorush.Core.SSL)
|
|
|
|
assert.Equal(suite.T(), "cert.pem", suite.ConfGorush.Core.CertPath)
|
|
|
|
assert.Equal(suite.T(), "key.pem", suite.ConfGorush.Core.KeyPath)
|
2016-09-02 07:55:47 +00:00
|
|
|
assert.Equal(suite.T(), int64(100), suite.ConfGorush.Core.MaxNotification)
|
2016-07-29 01:38:06 +00:00
|
|
|
assert.Equal(suite.T(), "", suite.ConfGorush.Core.HTTPProxy)
|
2016-08-19 13:16:10 +00:00
|
|
|
// Pid
|
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorush.Core.PID.Enabled)
|
|
|
|
assert.Equal(suite.T(), "gorush.pid", suite.ConfGorush.Core.PID.Path)
|
|
|
|
assert.Equal(suite.T(), true, suite.ConfGorush.Core.PID.Override)
|
2017-04-05 04:03:43 +00:00
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorush.Core.AutoTLS.Enabled)
|
|
|
|
assert.Equal(suite.T(), ".cache", suite.ConfGorush.Core.AutoTLS.Folder)
|
|
|
|
assert.Equal(suite.T(), "", suite.ConfGorush.Core.AutoTLS.Host)
|
2016-07-18 01:50:18 +00:00
|
|
|
|
|
|
|
// Api
|
|
|
|
assert.Equal(suite.T(), "/api/push", suite.ConfGorush.API.PushURI)
|
|
|
|
assert.Equal(suite.T(), "/api/stat/go", suite.ConfGorush.API.StatGoURI)
|
|
|
|
assert.Equal(suite.T(), "/api/stat/app", suite.ConfGorush.API.StatAppURI)
|
|
|
|
assert.Equal(suite.T(), "/api/config", suite.ConfGorush.API.ConfigURI)
|
|
|
|
assert.Equal(suite.T(), "/sys/stats", suite.ConfGorush.API.SysStatURI)
|
2017-01-19 08:56:30 +00:00
|
|
|
assert.Equal(suite.T(), "/metrics", suite.ConfGorush.API.MetricURI)
|
2017-10-26 02:19:58 +00:00
|
|
|
assert.Equal(suite.T(), "/healthz", suite.ConfGorush.API.HealthURI)
|
2016-07-18 01:50:18 +00:00
|
|
|
|
|
|
|
// Android
|
|
|
|
assert.Equal(suite.T(), true, suite.ConfGorush.Android.Enabled)
|
|
|
|
assert.Equal(suite.T(), "YOUR_API_KEY", suite.ConfGorush.Android.APIKey)
|
2016-10-23 10:19:51 +00:00
|
|
|
assert.Equal(suite.T(), 0, suite.ConfGorush.Android.MaxRetry)
|
2016-07-18 01:50:18 +00:00
|
|
|
|
|
|
|
// iOS
|
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorush.Ios.Enabled)
|
|
|
|
assert.Equal(suite.T(), "key.pem", suite.ConfGorush.Ios.KeyPath)
|
|
|
|
assert.Equal(suite.T(), "", suite.ConfGorush.Ios.Password)
|
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorush.Ios.Production)
|
2016-10-23 10:19:51 +00:00
|
|
|
assert.Equal(suite.T(), 0, suite.ConfGorush.Ios.MaxRetry)
|
2017-10-25 08:49:23 +00:00
|
|
|
assert.Equal(suite.T(), "", suite.ConfGorush.Ios.KeyID)
|
|
|
|
assert.Equal(suite.T(), "", suite.ConfGorush.Ios.TeamID)
|
2016-07-18 01:50:18 +00:00
|
|
|
|
|
|
|
// log
|
|
|
|
assert.Equal(suite.T(), "string", suite.ConfGorush.Log.Format)
|
|
|
|
assert.Equal(suite.T(), "stdout", suite.ConfGorush.Log.AccessLog)
|
|
|
|
assert.Equal(suite.T(), "debug", suite.ConfGorush.Log.AccessLevel)
|
|
|
|
assert.Equal(suite.T(), "stderr", suite.ConfGorush.Log.ErrorLog)
|
|
|
|
assert.Equal(suite.T(), "error", suite.ConfGorush.Log.ErrorLevel)
|
|
|
|
assert.Equal(suite.T(), true, suite.ConfGorush.Log.HideToken)
|
|
|
|
|
|
|
|
assert.Equal(suite.T(), "memory", suite.ConfGorush.Stat.Engine)
|
|
|
|
assert.Equal(suite.T(), "localhost:6379", suite.ConfGorush.Stat.Redis.Addr)
|
|
|
|
assert.Equal(suite.T(), "", suite.ConfGorush.Stat.Redis.Password)
|
2016-09-19 09:00:14 +00:00
|
|
|
assert.Equal(suite.T(), 0, suite.ConfGorush.Stat.Redis.DB)
|
2016-07-18 01:50:18 +00:00
|
|
|
|
2016-09-19 13:52:40 +00:00
|
|
|
assert.Equal(suite.T(), "bolt.db", suite.ConfGorush.Stat.BoltDB.Path)
|
2016-07-18 01:50:18 +00:00
|
|
|
assert.Equal(suite.T(), "gorush", suite.ConfGorush.Stat.BoltDB.Bucket)
|
2016-08-02 07:35:28 +00:00
|
|
|
|
2016-09-19 13:52:40 +00:00
|
|
|
assert.Equal(suite.T(), "bunt.db", suite.ConfGorush.Stat.BuntDB.Path)
|
|
|
|
assert.Equal(suite.T(), "level.db", suite.ConfGorush.Stat.LevelDB.Path)
|
2017-07-24 10:58:30 +00:00
|
|
|
|
|
|
|
// gRPC
|
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorush.GRPC.Enabled)
|
2017-10-19 01:24:47 +00:00
|
|
|
assert.Equal(suite.T(), "9000", suite.ConfGorush.GRPC.Port)
|
2016-07-18 01:50:18 +00:00
|
|
|
}
|
2016-05-04 01:40:48 +00:00
|
|
|
|
2016-07-18 01:50:18 +00:00
|
|
|
func TestConfigTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ConfigTestSuite))
|
2016-05-04 01:40:48 +00:00
|
|
|
}
|
2017-10-23 03:06:30 +00:00
|
|
|
|
|
|
|
func TestLoadConfigFromEnv(t *testing.T) {
|
|
|
|
os.Setenv("GORUSH_CORE_PORT", "9001")
|
|
|
|
os.Setenv("GORUSH_GRPC_ENABLED", "true")
|
|
|
|
os.Setenv("GORUSH_CORE_MAX_NOTIFICATION", "200")
|
2017-10-25 08:49:23 +00:00
|
|
|
os.Setenv("GORUSH_IOS_KEY_ID", "ABC123DEFG")
|
|
|
|
os.Setenv("GORUSH_IOS_TEAM_ID", "DEF123GHIJ")
|
2017-10-26 02:19:58 +00:00
|
|
|
os.Setenv("GORUSH_API_HEALTH_URI", "/healthz")
|
2017-10-23 03:06:30 +00:00
|
|
|
ConfGorush, err := LoadConf("config.yml")
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to load config.yml from file")
|
|
|
|
}
|
|
|
|
assert.Equal(t, "9001", ConfGorush.Core.Port)
|
|
|
|
assert.Equal(t, int64(200), ConfGorush.Core.MaxNotification)
|
|
|
|
assert.True(t, ConfGorush.GRPC.Enabled)
|
2017-10-25 08:49:23 +00:00
|
|
|
assert.Equal(t, "ABC123DEFG", ConfGorush.Ios.KeyID)
|
|
|
|
assert.Equal(t, "DEF123GHIJ", ConfGorush.Ios.TeamID)
|
2017-10-26 02:19:58 +00:00
|
|
|
assert.Equal(t, "/healthz", ConfGorush.API.HealthURI)
|
2017-10-23 03:06:30 +00:00
|
|
|
}
|