diff --git a/config/config_test.go b/config/config_test.go index 71cf0df..e6ace0b 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -2,9 +2,11 @@ package config import ( "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" "io/ioutil" "log" "os" + "runtime" "testing" ) @@ -35,20 +37,111 @@ func TestWrongYAMLormat(t *testing.T) { assert.NotNil(t, err) } -// Test config file. -func TestReadConfig(t *testing.T) { - config, err := LoadConfYaml("../config/config.yml") - - assert.Nil(t, err) - assert.Equal(t, "8088", config.Core.Port) - assert.True(t, config.Android.Enabled) +type ConfigTestSuite struct { + suite.Suite + ConfGorushDefault ConfYaml + ConfGorush ConfYaml } -// Test config file. -func TestDefaultConfig(t *testing.T) { - config := BuildDefaultPushConf() - - assert.Equal(t, "8088", config.Core.Port) - assert.Equal(t, "/sys/stats", config.API.SysStatURI) - assert.False(t, config.Android.Enabled) +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") + } +} + +func (suite *ConfigTestSuite) TestValidateConfDefault() { + // Core + assert.Equal(suite.T(), "8088", suite.ConfGorushDefault.Core.Port) + assert.Equal(suite.T(), runtime.NumCPU(), suite.ConfGorushDefault.Core.WorkerNum) + assert.Equal(suite.T(), 8192, suite.ConfGorushDefault.Core.QueueNum) + 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) + assert.Equal(suite.T(), 100, suite.ConfGorushDefault.Core.MaxNotification) + + // 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) + + // Android + assert.Equal(suite.T(), false, suite.ConfGorushDefault.Android.Enabled) + assert.Equal(suite.T(), "", suite.ConfGorushDefault.Android.APIKey) + + // 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) + + // 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) + assert.Equal(suite.T(), int64(0), suite.ConfGorushDefault.Stat.Redis.DB) + + assert.Equal(suite.T(), "gorush.db", suite.ConfGorushDefault.Stat.BoltDB.Path) + assert.Equal(suite.T(), "gorush", suite.ConfGorushDefault.Stat.BoltDB.Bucket) +} + +func (suite *ConfigTestSuite) TestValidateConf() { + // Core + assert.Equal(suite.T(), "8088", suite.ConfGorush.Core.Port) + assert.Equal(suite.T(), 8, suite.ConfGorush.Core.WorkerNum) + assert.Equal(suite.T(), 8192, suite.ConfGorush.Core.QueueNum) + 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) + assert.Equal(suite.T(), 100, suite.ConfGorush.Core.MaxNotification) + + // 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) + + // Android + assert.Equal(suite.T(), true, suite.ConfGorush.Android.Enabled) + assert.Equal(suite.T(), "YOUR_API_KEY", suite.ConfGorush.Android.APIKey) + + // 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) + + // 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) + assert.Equal(suite.T(), int64(0), suite.ConfGorush.Stat.Redis.DB) + + assert.Equal(suite.T(), "gorush.db", suite.ConfGorush.Stat.BoltDB.Path) + assert.Equal(suite.T(), "gorush", suite.ConfGorush.Stat.BoltDB.Bucket) +} + +func TestConfigTestSuite(t *testing.T) { + suite.Run(t, new(ConfigTestSuite)) }