Add tests for configuration parsing

This commit is contained in:
9seconds
2021-03-12 15:34:32 +03:00
parent 113b5ecbe0
commit c0b6124d94
6 changed files with 91 additions and 36 deletions
+37 -34
View File
@@ -16,7 +16,7 @@ type Config struct {
TCPBuffer TypeBytes `json:"tcp-buffer"`
PreferIP TypePreferIP `json:"prefer-ip"`
CloakPort TypePort `json:"cloak-port"`
Probes struct {
Defense struct {
Time struct {
Enabled bool `json:"enabled"`
AllowSkewness TypeDuration `json:"allow-skewness"`
@@ -26,7 +26,7 @@ type Config struct {
MaxSize TypeBytes `json:"max-size"`
ErrorRate TypeErrorRate `json:"error-rate"`
} `json:"anti-replay"`
} `json:"probes"`
} `json:"defense"`
Network struct {
PublicIP struct {
IPv4 TypeIP `json:"ipv4"`
@@ -58,6 +58,9 @@ func (c *Config) Validate() error {
if !c.Secret.Valid() {
return fmt.Errorf("invalid secret %s", c.Secret.String())
}
if len(c.BindTo.HostValue(nil)) == 0 || c.BindTo.PortValue(0) == 0 {
return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String())
}
return nil
}
@@ -76,48 +79,48 @@ func (c *Config) String() string {
}
type configRaw struct {
Debug bool `toml:"debug" json:"debug"`
Debug bool `toml:"debug" json:"debug,omitempty"`
Secret string `toml:"secret" json:"secret"`
BindTo string `toml:"bind-to" json:"bind-to"`
TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer"`
PreferIP string `toml:"prefer-ip" json:"prefer-ip"`
CloakPort uint `toml:"cloak-port" json:"cloak-port"`
Probes struct {
TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer,omitempty"`
PreferIP string `toml:"prefer-ip" json:"prefer-ip,omitempty"`
CloakPort uint `toml:"cloak-port" json:"cloak-port,omitempty"`
Defense struct {
Time struct {
Enabled bool `toml:"enabled" json:"enabled"`
AllowSkewness string `toml:"allow-skewness" json:"allow-skewness"`
} `toml:"time" json:"time"`
Enabled bool `toml:"enabled" json:"enabled,omitempty"`
AllowSkewness string `toml:"allow-skewness" json:"allow-skewness,omitempty"`
} `toml:"time" json:"time,omitempty"`
AntiReplay struct {
Enabled bool `toml:"enabled" json:"enabled"`
MaxSize string `toml:"max-size" json:"max-size"`
ErrorRate float64 `toml:"error-rate" json:"error-rate"`
} `toml:"anti-replay" json:"anti-replay"`
} `toml:"probes" json:"probes"`
Enabled bool `toml:"enabled" json:"enabled,omitempty"`
MaxSize string `toml:"max-size" json:"max-size,omitempty"`
ErrorRate float64 `toml:"error-rate" json:"error-rate,omitempty"`
} `toml:"anti-replay" json:"anti-replay,omitempty"`
} `toml:"defense" json:"defense,omitempty"`
Network struct {
PublicIP struct {
IPv4 string `toml:"ipv4" json:"ipv4"`
IPv6 string `toml:"ipv6" json:"ipv6"`
} `toml:"public-ip" json:"public-ip"`
IPv4 string `toml:"ipv4" json:"ipv4,omitempty"`
IPv6 string `toml:"ipv6" json:"ipv6,omitempty"`
} `toml:"public-ip" json:"public-ip,omitempty"`
Timeout struct {
TCP string `toml:"tcp" json:"tcp"`
Idle string `toml:"idle" json:"idle"`
} `toml:"timeout" json:"timeout"`
DOHIP string `toml:"doh-ip" json:"doh-ip"`
Proxies []string `toml:"proxies" json:"proxies"`
} `toml:"network" json:"network"`
TCP string `toml:"tcp" json:"tcp,omitempty"`
Idle string `toml:"idle" json:"idle,omitempty"`
} `toml:"timeout" json:"timeout,omitempty"`
DOHIP string `toml:"doh-ip" json:"doh-ip,omitempty"`
Proxies []string `toml:"proxies" json:"proxies,omitempty"`
} `toml:"network" json:"network,omitempty"`
Stats struct {
StatsD struct {
Enabled bool `toml:"enabled" json:"enabled"`
Address string `toml:"address" json:"address"`
MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
} `toml:"statsd" json:"statsd"`
Enabled bool `toml:"enabled" json:"enabled,omitempty"`
Address string `toml:"address" json:"address,omitempty"`
MetricPrefix string `toml:"metric-prefix" json:"metric-prefix,omitempty"`
} `toml:"statsd" json:"statsd,omitempty"`
Prometheus struct {
Enabled bool `toml:"enabled" json:"enabled"`
BindTo string `toml:"bind-to" json:"bind-to"`
HTTPPath string `toml:"http-path" json:"http-path"`
MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
} `toml:"prometheus" json:"prometheus"`
} `toml:"stats" json:"stats"`
Enabled bool `toml:"enabled" json:"enabled,omitempty"`
BindTo string `toml:"bind-to" json:"bind-to,omitempty"`
HTTPPath string `toml:"http-path" json:"http-path,omitempty"`
MetricPrefix string `toml:"metric-prefix" json:"metric-prefix,omitempty"`
} `toml:"prometheus" json:"prometheus,omitempty"`
} `toml:"stats" json:"stats,omitempty"`
}
func Parse(rawData []byte) (*Config, error) {
+48
View File
@@ -0,0 +1,48 @@
package config_test
import (
"io/ioutil"
"path/filepath"
"testing"
"github.com/9seconds/mtg/v2/config"
"github.com/stretchr/testify/suite"
)
type ConfigTestSuite struct {
suite.Suite
}
func (suite *ConfigTestSuite) ReadConfig(filename string) []byte {
data, err := ioutil.ReadFile(filepath.Join("testdata", filename))
suite.NoError(err)
return data
}
func (suite *ConfigTestSuite) TestParseEmpty() {
_, err := config.Parse([]byte{})
suite.Error(err)
}
func (suite *ConfigTestSuite) TestParseBrokenToml() {
_, err := config.Parse(suite.ReadConfig("broken.toml"))
suite.Error(err)
}
func (suite *ConfigTestSuite) TestParseOnlySecret() {
_, err := config.Parse(suite.ReadConfig("only_secret.toml"))
suite.Error(err)
}
func (suite *ConfigTestSuite) TestParseMinimalConfig() {
conf, err := config.Parse(suite.ReadConfig("minimal.toml"))
suite.NoError(err)
suite.Equal("7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t", conf.Secret.Base64())
suite.Equal("0.0.0.0:3128", conf.BindTo.String())
}
func TestConfig(t *testing.T) {
t.Parallel()
suite.Run(t, &ConfigTestSuite{})
}
+1
View File
@@ -0,0 +1 @@
s = sdfsdfds
+2
View File
@@ -0,0 +1,2 @@
secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t"
bind-to = "0.0.0.0:3128"
+1
View File
@@ -0,0 +1 @@
secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t"
+2 -2
View File
@@ -119,7 +119,7 @@ idle = "1m"
#
# Please ensure that you have some ntp active on this host. Otherwise,
# you can endup with badly performing proxy.
[probes.time]
[defense.time]
# You can enable/disable that. A good idea is always enable.
enabled = true
# Time can be skewed by many reasons. So, this is a time interval
@@ -132,7 +132,7 @@ allow-skewness = "5s"
# mtg has a cache of some connection fingerprints. Actually, first bytes
# of each connection. So, it stores them in some in-memory LRU+TTL cache.
# You can configure this cache here.
[probes.anti-replay]
[defense.anti-replay]
# You can enable/disable this feature.
enabled = true
# max size of such a cache. Please be aware that this number is