This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
182 lines (162 loc) · 5.58 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or impliem.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"fmt"
"os"
"reflect"
"testing"
"time"
"github.com/airfocusio/default-gateway-agent/internal"
iptest "k8s.io/kubernetes/pkg/util/iptables/testing"
)
// turn off glog logging during tests to avoid clutter in output
func TestMain(m *testing.M) {
flag.Set("logtostderr", "false")
ec := m.Run()
os.Exit(ec)
}
// returns a GatewayDaemon with empty config values and a fake iptables interface
func NewFakeGatewayDaemon() *GatewayDaemon {
return &GatewayDaemon{
config: &AgentConfig{},
iptables: iptest.NewFake(),
}
}
// Returns a AgentConfig with config values that are the same as the default values when the
// ignoreAllReservedRangesFlag is false.
func NewAgentConfigEmpty() *AgentConfig {
return &AgentConfig{
IgnoreCIDRs: []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"},
IgnoreLinkLocal: false,
Gateways: []AgentConfigGateway{},
ResyncInterval: Duration(1 * time.Second),
}
}
// specs for testing config validation
var validateConfigTests = []struct {
cfg *AgentConfig
err error
}{
// Empty Gateway List
{&AgentConfig{}, nil},
// Default Config
{NewAgentConfigEmpty(), nil},
// IP that doesn't match regex
{&AgentConfig{Gateways: []AgentConfigGateway{{IP: "abcdefg"}}}, fmt.Errorf(ipParseErrFmt, "abcdefg")},
}
// tests the AgentConfig.validate method
func TestConfigValidate(t *testing.T) {
for _, tt := range validateConfigTests {
err := tt.cfg.validate()
if errorToString(err) != errorToString(tt.err) {
t.Errorf("%+v.validate() => %s, want %s", tt.cfg, errorToString(err), errorToString(tt.err))
}
}
}
// specs for testing loading config from fs
var syncConfigTests = []struct {
desc string // human readable description of the fs used for the test e.g. "no config file"
fs internal.FileSystem // filesystem interface
err error // expected error from GatewayDaemon.syncConfig(fs)
cfg *AgentConfig // expected values of the configuration after loading from fs
}{
// valid yaml
{"valid yaml file, all keys", internal.StringFS{File: `
ipTables:
mark: 1
ipRule:
table: 1
priority: 30000
podNetwork:
cidr: 10.244.0.0/16
gateways:
- ip: 1.1.1.1
- ip: 2.2.2.2
resyncInterval: 5s
`}, nil, &AgentConfig{
IgnoreCIDRs: []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"},
IgnoreLinkLocal: false,
IPTables: AgentConfigIPTables{Mark: 1},
IPRule: AgentConfigIPRule{Table: 1, Priority: 30000},
PodNetwork: AgentConfigPodNetwork{CIDR: "10.244.0.0/16"},
Gateways: []AgentConfigGateway{{IP: "1.1.1.1"}, {IP: "2.2.2.2"}},
ResyncInterval: Duration(5 * time.Second)}},
// invalid yaml
{"invalid yaml file", internal.StringFS{File: `*`}, fmt.Errorf("yaml: did not find expected alphabetic or numeric character"), NewAgentConfigEmpty()},
// valid json
{"valid json file, all keys", internal.StringFS{File: `
{
"ipTables": {"mark":1},
"ipRule": {"table":1, "priority":30000},
"podNetwork": {"cidr":"10.244.0.0/16"},
"gateways": [{"ip":"1.1.1.1"}, {"ip":"2.2.2.2"}],
"resyncInterval": "5s"
}
`},
nil, &AgentConfig{
IgnoreCIDRs: []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"},
IgnoreLinkLocal: false,
IPTables: AgentConfigIPTables{Mark: 1},
IPRule: AgentConfigIPRule{Table: 1, Priority: 30000},
PodNetwork: AgentConfigPodNetwork{CIDR: "10.244.0.0/16"},
Gateways: []AgentConfigGateway{{IP: "1.1.1.1"}, {IP: "2.2.2.2"}},
ResyncInterval: Duration(5 * time.Second)}},
// invalid json
{"invalid json file", internal.StringFS{File: `{*`}, fmt.Errorf("invalid character '*' looking for beginning of object key string"), NewAgentConfigEmpty()},
// file does not exist
{"no config file", internal.NotExistFS{}, nil, NewAgentConfigEmpty()}, // If the file does not exist, defaults should be used
}
// tests GatewayDaemon.syncConfig
func TestSyncConfig(t *testing.T) {
for _, tt := range syncConfigTests {
m := NewFakeGatewayDaemon()
m.config = NewAgentConfigEmpty()
err := m.syncConfig(tt.fs)
if errorToString(err) != errorToString(tt.err) {
t.Errorf("GatewayDaemon.syncConfig(fs: %s) => %s, want %s", tt.desc, errorToString(err), errorToString(tt.err))
} else if !reflect.DeepEqual(m.config, tt.cfg) {
t.Errorf("GatewayDaemon.syncConfig(fs: %s) loaded as %+v, want %+v", tt.desc, m.config, tt.cfg)
}
}
}
// tests GatewayDaemon.sync
func TestSync(t *testing.T) {
var syncTests = []struct {
desc string // human readable description of the test
cfg *AgentConfig // configuration to use
err error // expected error, if any. If nil, no error expected
want string // String expected to be sent to iptables-restore
}{
{
desc: "empty config",
cfg: &AgentConfig{},
want: ``,
},
}
for _, tt := range syncTests {
t.Run(tt.desc, func(t *testing.T) {
m := NewFakeGatewayDaemon()
m.config = tt.cfg
m.sync()
})
}
}
// convert error to string, while also handling nil errors
func errorToString(err error) string {
if err == nil {
return "nil error"
}
return fmt.Sprintf("error %q", err.Error())
}