2016-05-02 11:42:21 +00:00
|
|
|
package config
|
2016-04-01 03:01:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"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"
|
|
|
|
_, err := LoadConfYaml(filename)
|
|
|
|
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test wrong json format
|
|
|
|
func TestWrongYAMLormat(t *testing.T) {
|
|
|
|
content := []byte(`Wrong format`)
|
|
|
|
|
|
|
|
filename := "tempfile"
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filename, content, 0644); err != nil {
|
|
|
|
log.Fatalf("WriteFile %s: %v", filename, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
defer os.Remove(filename)
|
|
|
|
|
|
|
|
// parse JSON format error
|
|
|
|
_, err := LoadConfYaml(filename)
|
|
|
|
|
|
|
|
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() {
|
|
|
|
suite.ConfGorushDefault = BuildDefaultPushConf()
|
|
|
|
var err error
|
|
|
|
suite.ConfGorush, err = LoadConfYaml("config.yml")
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to load config.yml")
|
|
|
|
}
|
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
|
|
|
|
assert.Equal(suite.T(), "8088", suite.ConfGorushDefault.Core.Port)
|
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)
|
|
|
|
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)
|
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Core.PID.Override)
|
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)
|
2016-07-18 01:50:18 +00:00
|
|
|
|
|
|
|
// Android
|
|
|
|
assert.Equal(suite.T(), false, suite.ConfGorushDefault.Android.Enabled)
|
|
|
|
assert.Equal(suite.T(), "", 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)
|
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)
|
2016-07-18 01:50:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *ConfigTestSuite) TestValidateConf() {
|
|
|
|
// Core
|
|
|
|
assert.Equal(suite.T(), "8088", suite.ConfGorush.Core.Port)
|
2016-09-02 07:55:47 +00:00
|
|
|
assert.Equal(suite.T(), int64(8), suite.ConfGorush.Core.WorkerNum)
|
|
|
|
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)
|
|
|
|
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)
|
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)
|
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)
|
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)
|
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
|
|
|
}
|