-
Notifications
You must be signed in to change notification settings - Fork 7
/
map_test.go
47 lines (40 loc) · 1.5 KB
/
map_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
package shoveler
import (
"bytes"
"net"
"os"
"testing"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
func TestSingleIp(t *testing.T) {
ip := net.UDPAddr{IP: net.ParseIP("192.168.0.5"), Port: 514}
// If the map is not set
config := Config{}
config.ReadConfig()
ipStr := mapIp(&ip, &config)
assert.Equal(t, "192.168.0.5", ipStr, "Test when map is not set")
// If the map is set by environment variable
err := os.Setenv("SHOVELER_MAP_ALL", "172.168.0.5")
assert.NoError(t, err, "Failed to set environment variable SHOVELER_MAP_ALL")
config.ReadConfig()
ipStr = mapIp(&ip, &config)
assert.Equal(t, "172.168.0.5", ipStr, "Test when map is set by environment variable")
// If the map is set by config file
err = os.Unsetenv("SHOVELER_MAP_ALL")
assert.NoError(t, err, "Failed to unset SHOVELER_MAP_ALL")
// any approach to require this configuration into your program.
var yamlExample = []byte(`
map:
192.168.1.5: 172.168.1.6
172.168.2.7: 129.93.10.5
`)
err = viper.ReadConfig(bytes.NewBuffer(yamlExample))
defer viper.Reset()
assert.NoError(t, err, "Failed to read config file")
config = Config{IpMapAll: viper.GetString("map.all"), IpMap: viper.GetStringMapString("map")}
ip = net.UDPAddr{IP: net.ParseIP("192.168.1.5"), Port: 514}
assert.Equal(t, "172.168.1.6", mapIp(&ip, &config), "Test when map is set by config file")
ip = net.UDPAddr{IP: net.ParseIP("172.168.2.7"), Port: 514}
assert.Equal(t, "129.93.10.5", mapIp(&ip, &config), "Test when map is set by config file")
}