Pārlūkot izejas kodu

Add tests for configuration parsing

tags/v2.0.0-rc1
9seconds 5 gadus atpakaļ
vecāks
revīzija
c0b6124d94

+ 37
- 34
config/config.go Parādīt failu

@@ -16,7 +16,7 @@ type Config struct {
16 16
 	TCPBuffer TypeBytes     `json:"tcp-buffer"`
17 17
 	PreferIP  TypePreferIP  `json:"prefer-ip"`
18 18
 	CloakPort TypePort      `json:"cloak-port"`
19
-	Probes    struct {
19
+	Defense   struct {
20 20
 		Time struct {
21 21
 			Enabled       bool         `json:"enabled"`
22 22
 			AllowSkewness TypeDuration `json:"allow-skewness"`
@@ -26,7 +26,7 @@ type Config struct {
26 26
 			MaxSize   TypeBytes     `json:"max-size"`
27 27
 			ErrorRate TypeErrorRate `json:"error-rate"`
28 28
 		} `json:"anti-replay"`
29
-	} `json:"probes"`
29
+	} `json:"defense"`
30 30
 	Network struct {
31 31
 		PublicIP struct {
32 32
 			IPv4 TypeIP `json:"ipv4"`
@@ -58,6 +58,9 @@ func (c *Config) Validate() error {
58 58
 	if !c.Secret.Valid() {
59 59
 		return fmt.Errorf("invalid secret %s", c.Secret.String())
60 60
 	}
61
+	if len(c.BindTo.HostValue(nil)) == 0 || c.BindTo.PortValue(0) == 0 {
62
+		return fmt.Errorf("incorrect bind-to parameter %s", c.BindTo.String())
63
+	}
61 64
 
62 65
 	return nil
63 66
 }
@@ -76,48 +79,48 @@ func (c *Config) String() string {
76 79
 }
77 80
 
78 81
 type configRaw struct {
79
-	Debug     bool   `toml:"debug" json:"debug"`
82
+	Debug     bool   `toml:"debug" json:"debug,omitempty"`
80 83
 	Secret    string `toml:"secret" json:"secret"`
81 84
 	BindTo    string `toml:"bind-to" json:"bind-to"`
82
-	TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer"`
83
-	PreferIP  string `toml:"prefer-ip" json:"prefer-ip"`
84
-	CloakPort uint   `toml:"cloak-port" json:"cloak-port"`
85
-	Probes    struct {
85
+	TCPBuffer string `toml:"tcp-buffer" json:"tcp-buffer,omitempty"`
86
+	PreferIP  string `toml:"prefer-ip" json:"prefer-ip,omitempty"`
87
+	CloakPort uint   `toml:"cloak-port" json:"cloak-port,omitempty"`
88
+	Defense   struct {
86 89
 		Time struct {
87
-			Enabled       bool   `toml:"enabled" json:"enabled"`
88
-			AllowSkewness string `toml:"allow-skewness" json:"allow-skewness"`
89
-		} `toml:"time" json:"time"`
90
+			Enabled       bool   `toml:"enabled" json:"enabled,omitempty"`
91
+			AllowSkewness string `toml:"allow-skewness" json:"allow-skewness,omitempty"`
92
+		} `toml:"time" json:"time,omitempty"`
90 93
 		AntiReplay struct {
91
-			Enabled   bool    `toml:"enabled" json:"enabled"`
92
-			MaxSize   string  `toml:"max-size" json:"max-size"`
93
-			ErrorRate float64 `toml:"error-rate" json:"error-rate"`
94
-		} `toml:"anti-replay" json:"anti-replay"`
95
-	} `toml:"probes" json:"probes"`
94
+			Enabled   bool    `toml:"enabled" json:"enabled,omitempty"`
95
+			MaxSize   string  `toml:"max-size" json:"max-size,omitempty"`
96
+			ErrorRate float64 `toml:"error-rate" json:"error-rate,omitempty"`
97
+		} `toml:"anti-replay" json:"anti-replay,omitempty"`
98
+	} `toml:"defense" json:"defense,omitempty"`
96 99
 	Network struct {
97 100
 		PublicIP struct {
98
-			IPv4 string `toml:"ipv4" json:"ipv4"`
99
-			IPv6 string `toml:"ipv6" json:"ipv6"`
100
-		} `toml:"public-ip" json:"public-ip"`
101
+			IPv4 string `toml:"ipv4" json:"ipv4,omitempty"`
102
+			IPv6 string `toml:"ipv6" json:"ipv6,omitempty"`
103
+		} `toml:"public-ip" json:"public-ip,omitempty"`
101 104
 		Timeout struct {
102
-			TCP  string `toml:"tcp" json:"tcp"`
103
-			Idle string `toml:"idle" json:"idle"`
104
-		} `toml:"timeout" json:"timeout"`
105
-		DOHIP   string   `toml:"doh-ip" json:"doh-ip"`
106
-		Proxies []string `toml:"proxies" json:"proxies"`
107
-	} `toml:"network" json:"network"`
105
+			TCP  string `toml:"tcp" json:"tcp,omitempty"`
106
+			Idle string `toml:"idle" json:"idle,omitempty"`
107
+		} `toml:"timeout" json:"timeout,omitempty"`
108
+		DOHIP   string   `toml:"doh-ip" json:"doh-ip,omitempty"`
109
+		Proxies []string `toml:"proxies" json:"proxies,omitempty"`
110
+	} `toml:"network" json:"network,omitempty"`
108 111
 	Stats struct {
109 112
 		StatsD struct {
110
-			Enabled      bool   `toml:"enabled" json:"enabled"`
111
-			Address      string `toml:"address" json:"address"`
112
-			MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
113
-		} `toml:"statsd" json:"statsd"`
113
+			Enabled      bool   `toml:"enabled" json:"enabled,omitempty"`
114
+			Address      string `toml:"address" json:"address,omitempty"`
115
+			MetricPrefix string `toml:"metric-prefix" json:"metric-prefix,omitempty"`
116
+		} `toml:"statsd" json:"statsd,omitempty"`
114 117
 		Prometheus struct {
115
-			Enabled      bool   `toml:"enabled" json:"enabled"`
116
-			BindTo       string `toml:"bind-to" json:"bind-to"`
117
-			HTTPPath     string `toml:"http-path" json:"http-path"`
118
-			MetricPrefix string `toml:"metric-prefix" json:"metric-prefix"`
119
-		} `toml:"prometheus" json:"prometheus"`
120
-	} `toml:"stats" json:"stats"`
118
+			Enabled      bool   `toml:"enabled" json:"enabled,omitempty"`
119
+			BindTo       string `toml:"bind-to" json:"bind-to,omitempty"`
120
+			HTTPPath     string `toml:"http-path" json:"http-path,omitempty"`
121
+			MetricPrefix string `toml:"metric-prefix" json:"metric-prefix,omitempty"`
122
+		} `toml:"prometheus" json:"prometheus,omitempty"`
123
+	} `toml:"stats" json:"stats,omitempty"`
121 124
 }
122 125
 
123 126
 func Parse(rawData []byte) (*Config, error) {

+ 48
- 0
config/config_test.go Parādīt failu

@@ -0,0 +1,48 @@
1
+package config_test
2
+
3
+import (
4
+	"io/ioutil"
5
+	"path/filepath"
6
+	"testing"
7
+
8
+	"github.com/9seconds/mtg/v2/config"
9
+	"github.com/stretchr/testify/suite"
10
+)
11
+
12
+type ConfigTestSuite struct {
13
+	suite.Suite
14
+}
15
+
16
+func (suite *ConfigTestSuite) ReadConfig(filename string) []byte {
17
+	data, err := ioutil.ReadFile(filepath.Join("testdata", filename))
18
+	suite.NoError(err)
19
+
20
+	return data
21
+}
22
+
23
+func (suite *ConfigTestSuite) TestParseEmpty() {
24
+	_, err := config.Parse([]byte{})
25
+	suite.Error(err)
26
+}
27
+
28
+func (suite *ConfigTestSuite) TestParseBrokenToml() {
29
+	_, err := config.Parse(suite.ReadConfig("broken.toml"))
30
+	suite.Error(err)
31
+}
32
+
33
+func (suite *ConfigTestSuite) TestParseOnlySecret() {
34
+	_, err := config.Parse(suite.ReadConfig("only_secret.toml"))
35
+	suite.Error(err)
36
+}
37
+
38
+func (suite *ConfigTestSuite) TestParseMinimalConfig() {
39
+	conf, err := config.Parse(suite.ReadConfig("minimal.toml"))
40
+	suite.NoError(err)
41
+	suite.Equal("7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t", conf.Secret.Base64())
42
+    suite.Equal("0.0.0.0:3128", conf.BindTo.String())
43
+}
44
+
45
+func TestConfig(t *testing.T) {
46
+	t.Parallel()
47
+	suite.Run(t, &ConfigTestSuite{})
48
+}

+ 1
- 0
config/testdata/broken.toml Parādīt failu

@@ -0,0 +1 @@
1
+s = sdfsdfds

+ 2
- 0
config/testdata/minimal.toml Parādīt failu

@@ -0,0 +1,2 @@
1
+secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t"
2
+bind-to = "0.0.0.0:3128"

+ 1
- 0
config/testdata/only_secret.toml Parādīt failu

@@ -0,0 +1 @@
1
+secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t"

+ 2
- 2
example.config.toml Parādīt failu

@@ -119,7 +119,7 @@ idle = "1m"
119 119
 #
120 120
 # Please ensure that you have some ntp active on this host. Otherwise,
121 121
 # you can endup with badly performing proxy.
122
-[probes.time]
122
+[defense.time]
123 123
 # You can enable/disable that. A good idea is always enable.
124 124
 enabled = true
125 125
 # Time can be skewed by many reasons. So, this is a time interval
@@ -132,7 +132,7 @@ allow-skewness = "5s"
132 132
 # mtg has a cache of some connection fingerprints. Actually, first bytes
133 133
 # of each connection. So, it stores them in some in-memory LRU+TTL cache.
134 134
 # You can configure this cache here.
135
-[probes.anti-replay]
135
+[defense.anti-replay]
136 136
 # You can enable/disable this feature.
137 137
 enabled = true
138 138
 # max size of such a cache. Please be aware that this number is

Notiek ielāde…
Atcelt
Saglabāt