Skip to content

Commit a961158

Browse files
committed
Use default values when not provided
1 parent 41f2a99 commit a961158

File tree

6 files changed

+105
-34
lines changed

6 files changed

+105
-34
lines changed

cmd/sync/aws.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ func (client *AWSClient) GetUpstreams() []Upstream {
6969
ScalingGroup: client.config.Upstreams[i].AutoscalingGroup,
7070
MaxConns: &client.config.Upstreams[i].MaxConns,
7171
MaxFails: &client.config.Upstreams[i].MaxFails,
72-
FailTimeout: client.config.Upstreams[i].FailTimeout,
73-
SlowStart: client.config.Upstreams[i].SlowStart,
72+
FailTimeout: getFailTimeoutOrDefault(client.config.Upstreams[i].FailTimeout),
73+
SlowStart: getSlowStartOrDefault(client.config.Upstreams[i].SlowStart),
7474
}
7575
upstreams = append(upstreams, u)
7676
}
@@ -167,7 +167,7 @@ func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, er
167167

168168
type awsConfig struct {
169169
Region string
170-
Upstreams []*awsUpstream
170+
Upstreams []awsUpstream
171171
}
172172

173173
type awsUpstream struct {

cmd/sync/aws_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type testInputAWS struct {
88
}
99

1010
func getValidAWSConfig() *awsConfig {
11-
upstreams := []*awsUpstream{
11+
upstreams := []awsUpstream{
1212
{
1313
Name: "backend1",
1414
AutoscalingGroup: "backend-group",
@@ -92,21 +92,21 @@ func TestValidateAWSConfigValid(t *testing.T) {
9292

9393
func TestGetUpstreamsAWS(t *testing.T) {
9494
cfg := getValidAWSConfig()
95-
var upstreams = []*awsUpstream{
95+
var upstreams = []awsUpstream{
9696
{
97-
Name: "127.0.0.1",
98-
Port: 80,
99-
MaxFails: 1,
100-
MaxConns: 2,
101-
SlowStart: "5s",
97+
Name: "127.0.0.1",
98+
Port: 80,
99+
MaxFails: 1,
100+
MaxConns: 2,
101+
SlowStart: "5s",
102102
FailTimeout: "10s",
103103
},
104104
{
105-
Name: "127.0.0.2",
106-
Port: 80,
107-
MaxFails: 2,
108-
MaxConns: 3,
109-
SlowStart: "6s",
105+
Name: "127.0.0.2",
106+
Port: 80,
107+
MaxFails: 2,
108+
MaxConns: 3,
109+
SlowStart: "6s",
110110
FailTimeout: "11s",
111111
},
112112
}
@@ -131,7 +131,7 @@ func TestGetUpstreamsAWS(t *testing.T) {
131131
}
132132
}
133133

134-
func areEqualUpstreamsAWS(u1 *awsUpstream, u2 Upstream) bool {
134+
func areEqualUpstreamsAWS(u1 awsUpstream, u2 Upstream) bool {
135135
if u1.Port != u2.Port {
136136
return false
137137
}
@@ -153,4 +153,4 @@ func areEqualUpstreamsAWS(u1 *awsUpstream, u2 Upstream) bool {
153153
}
154154

155155
return true
156-
}
156+
}

cmd/sync/azure.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ func (client *AzureClient) GetUpstreams() []Upstream {
138138
ScalingGroup: client.config.Upstreams[i].VMScaleSet,
139139
MaxConns: &client.config.Upstreams[i].MaxConns,
140140
MaxFails: &client.config.Upstreams[i].MaxFails,
141-
FailTimeout: client.config.Upstreams[i].FailTimeout,
142-
SlowStart: client.config.Upstreams[i].SlowStart,
141+
FailTimeout: getFailTimeoutOrDefault(client.config.Upstreams[i].FailTimeout),
142+
SlowStart: getSlowStartOrDefault(client.config.Upstreams[i].SlowStart),
143143
}
144144
upstreams = append(upstreams, u)
145145
}
@@ -149,7 +149,7 @@ func (client *AzureClient) GetUpstreams() []Upstream {
149149
type azureConfig struct {
150150
SubscriptionID string `yaml:"subscription_id"`
151151
ResourceGroupName string `yaml:"resource_group_name"`
152-
Upstreams []*azureUpstream
152+
Upstreams []azureUpstream
153153
}
154154

155155
type azureUpstream struct {

cmd/sync/azure_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type testInputAzure struct {
1212
}
1313

1414
func getValidAzureConfig() *azureConfig {
15-
upstreams := []*azureUpstream{
15+
upstreams := []azureUpstream{
1616
{
1717
Name: "backend1",
1818
VMScaleSet: "backend-group",
@@ -159,21 +159,21 @@ func TestGetPrimaryIPFromInterfaceIPConfigurationFail(t *testing.T) {
159159

160160
func TestGetUpstreamsAzure(t *testing.T) {
161161
cfg := getValidAzureConfig()
162-
var upstreams = []*azureUpstream{
162+
var upstreams = []azureUpstream{
163163
{
164-
Name: "127.0.0.1",
165-
Port: 80,
166-
MaxFails: 1,
167-
MaxConns: 2,
168-
SlowStart: "5s",
164+
Name: "127.0.0.1",
165+
Port: 80,
166+
MaxFails: 1,
167+
MaxConns: 2,
168+
SlowStart: "5s",
169169
FailTimeout: "10s",
170170
},
171171
{
172-
Name: "127.0.0.2",
173-
Port: 80,
174-
MaxFails: 2,
175-
MaxConns: 3,
176-
SlowStart: "6s",
172+
Name: "127.0.0.2",
173+
Port: 80,
174+
MaxFails: 2,
175+
MaxConns: 3,
176+
SlowStart: "6s",
177177
FailTimeout: "11s",
178178
},
179179
}
@@ -198,7 +198,7 @@ func TestGetUpstreamsAzure(t *testing.T) {
198198
}
199199
}
200200

201-
func areEqualUpstreamsAzure(u1 *azureUpstream, u2 Upstream) bool {
201+
func areEqualUpstreamsAzure(u1 azureUpstream, u2 Upstream) bool {
202202
if u1.Port != u2.Port {
203203
return false
204204
}
@@ -220,4 +220,4 @@ func areEqualUpstreamsAzure(u1 *azureUpstream, u2 Upstream) bool {
220220
}
221221

222222
return true
223-
}
223+
}

cmd/sync/parameters.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
const defaultFailTimeout = "10s"
4+
const defaultSlowStart = "0s"
5+
6+
func getFailTimeoutOrDefault(failTimeout string) string {
7+
if failTimeout == "" {
8+
return defaultFailTimeout
9+
}
10+
11+
return failTimeout
12+
}
13+
14+
func getSlowStartOrDefault(slowStart string) string {
15+
if slowStart == "" {
16+
return defaultSlowStart
17+
}
18+
19+
return slowStart
20+
}

cmd/sync/parameters_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetFailTimeoutOrDefault(t *testing.T) {
8+
tests := []struct{
9+
input string
10+
expected string
11+
}{
12+
{
13+
input: "",
14+
expected: defaultFailTimeout,
15+
},
16+
{
17+
input: "10s",
18+
expected: "10s",
19+
},
20+
}
21+
22+
for _, test := range tests {
23+
result := getFailTimeoutOrDefault(test.input)
24+
if result != test.expected {
25+
t.Errorf("getFailTimeoutOrDefault(%v) returned %v but expected %v", test.input, result, test.expected)
26+
}
27+
}
28+
}
29+
30+
func TestGetSlowStartOrDefault(t *testing.T) {
31+
tests := []struct{
32+
input string
33+
expected string
34+
}{
35+
{
36+
input: "",
37+
expected: defaultSlowStart,
38+
},
39+
{
40+
input: "10s",
41+
expected: "10s",
42+
},
43+
}
44+
45+
for _, test := range tests {
46+
result := getSlowStartOrDefault(test.input)
47+
if result != test.expected {
48+
t.Errorf("getSlowStartOrDefault(%v) returned %v but expected %v", test.input, result, test.expected)
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)