129 lines
3.8 KiB
Go
129 lines
3.8 KiB
Go
![]() |
package app
|
||
|
|
||
|
import (
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"os"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestNewOptions(t *testing.T) {
|
||
|
os.Setenv("DEVELOPMENT", "false")
|
||
|
os.Setenv("APP_NAME", "testValidate")
|
||
|
os.Setenv("APP_ADDR", ":7113")
|
||
|
os.Setenv("CH_CRED", "tcp://127.0.0.1:9000?debug=true")
|
||
|
os.Setenv("DB_PATH", "./recover.bolt")
|
||
|
os.Setenv("TG_TOKEN", "6712573453:AAFqTOsgwe_j48ZQ1GzWKQDT5Nwr-SAWjz8")
|
||
|
os.Setenv("TG_CHAT_ID", "1002217604546")
|
||
|
os.Setenv("DB_CLICKHOUSE_NAME", "test_validator")
|
||
|
os.Setenv("HTTP_HOST", "127.0.0.1")
|
||
|
os.Setenv("HTTP_PORT", "8000")
|
||
|
|
||
|
opts := NewOptions()
|
||
|
|
||
|
assert.Equal(t, false, opts.Development)
|
||
|
assert.Equal(t, "testValidate", opts.AppName)
|
||
|
assert.Equal(t, ":7113", opts.Addr)
|
||
|
assert.Equal(t, "tcp://127.0.0.1:9000?debug=true", opts.ClickhouseCred)
|
||
|
assert.Equal(t, "./recover.bolt", opts.DBPath)
|
||
|
assert.Equal(t, "6712573453:AAFqTOsgwe_j48ZQ1GzWKQDT5Nwr-SAWjz8", opts.TgToken)
|
||
|
assert.Equal(t, uint64(1002217604546), opts.TgChatID)
|
||
|
assert.Equal(t, "test_validator", opts.DBClickHouseName)
|
||
|
assert.Equal(t, "127.0.0.1", opts.HTTPHost)
|
||
|
assert.Equal(t, "8000", opts.HTTPPort)
|
||
|
}
|
||
|
|
||
|
func TestOptions_Validate(t *testing.T) {
|
||
|
optsMain := Options{
|
||
|
Addr: ":7113",
|
||
|
ClickhouseCred: "tcp://127.0.0.1:9000?debug=true",
|
||
|
DBPath: "./recover.bolt",
|
||
|
TgToken: "6712573453:AAFqTOsgwe_j48ZQ1GzWKQDT5Nwr-SAWjz8",
|
||
|
TgChatID: 1002217604546,
|
||
|
DBClickHouseName: "test_validator",
|
||
|
HTTPHost: "127.0.0.1",
|
||
|
HTTPPort: "8000",
|
||
|
}
|
||
|
t.Run("valid configuration", func(t *testing.T) {
|
||
|
opts := optsMain
|
||
|
err := opts.Validate()
|
||
|
assert.NoError(t, err)
|
||
|
})
|
||
|
|
||
|
t.Run("missing DBClickHouseName", func(t *testing.T) {
|
||
|
opts := optsMain
|
||
|
opts.DBClickHouseName = ""
|
||
|
err := opts.Validate()
|
||
|
assert.EqualError(t, err, "invalid value for DBClickHouseName, some value is empty")
|
||
|
})
|
||
|
|
||
|
t.Run("missing Addr prefix ':'", func(t *testing.T) {
|
||
|
opts := optsMain
|
||
|
opts.Addr = "7113"
|
||
|
err := opts.Validate()
|
||
|
assert.EqualError(t, err, "APP_ADDR must start with ':'")
|
||
|
})
|
||
|
|
||
|
t.Run("invalid HTTPHost IP address", func(t *testing.T) {
|
||
|
opts := optsMain
|
||
|
opts.HTTPHost = "invalid_host"
|
||
|
err := opts.Validate()
|
||
|
assert.EqualError(t, err, "HTTP_HOST must be a valid IP address or hostname")
|
||
|
})
|
||
|
|
||
|
t.Run("missing HTTPPort", func(t *testing.T) {
|
||
|
opts := optsMain
|
||
|
opts.HTTPPort = ""
|
||
|
err := opts.Validate()
|
||
|
assert.EqualError(t, err, "HTTP_PORT not be empty")
|
||
|
})
|
||
|
|
||
|
t.Run("invalid HTTPPort - non-numeric", func(t *testing.T) {
|
||
|
opts := optsMain
|
||
|
opts.HTTPPort = "port"
|
||
|
err := opts.Validate()
|
||
|
assert.EqualError(t, err, "HTTP_PORT must be a valid port number between 1 and 65535")
|
||
|
})
|
||
|
|
||
|
t.Run("invalid HTTPPort - out of range", func(t *testing.T) {
|
||
|
opts := optsMain
|
||
|
opts.HTTPPort = "70000"
|
||
|
err := opts.Validate()
|
||
|
assert.EqualError(t, err, "HTTP_PORT must be a valid port number between 1 and 65535")
|
||
|
})
|
||
|
|
||
|
t.Run("missing Clickhouse credentials", func(t *testing.T) {
|
||
|
opts := optsMain
|
||
|
opts.ClickhouseCred = ""
|
||
|
err := opts.Validate()
|
||
|
assert.EqualError(t, err, "APP_CLICKHOUSE_CRED is empty")
|
||
|
})
|
||
|
|
||
|
t.Run("invalid DBPath directory", func(t *testing.T) {
|
||
|
opts := optsMain
|
||
|
opts.DBPath = "/invalid_path/test.db"
|
||
|
err := opts.Validate()
|
||
|
assert.Contains(t, err.Error(), "unable to create temporary file in directory")
|
||
|
})
|
||
|
|
||
|
t.Run("empty Telegram token", func(t *testing.T) {
|
||
|
opts := optsMain
|
||
|
opts.TgToken = ""
|
||
|
err := opts.Validate()
|
||
|
assert.EqualError(t, err, "telegram token or chat ID empty")
|
||
|
})
|
||
|
|
||
|
t.Run("empty Telegram ChatID", func(t *testing.T) {
|
||
|
opts := optsMain
|
||
|
opts.TgChatID = 0
|
||
|
err := opts.Validate()
|
||
|
assert.EqualError(t, err, "telegram token or chat ID empty")
|
||
|
})
|
||
|
|
||
|
t.Run("invalid Clickhouse credentials - connection error", func(t *testing.T) {
|
||
|
opts := optsMain
|
||
|
opts.ClickhouseCred = "tcp://invalid:9000?debug=true"
|
||
|
err := opts.Validate()
|
||
|
assert.Contains(t, err.Error(), "clickhouse ping error")
|
||
|
})
|
||
|
}
|