-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_test.go
124 lines (116 loc) · 4.21 KB
/
config_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
package main
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
testFlagGlobalSvcLabelSelector = "flag-global-label"
testFlagGlobalSvcTopologyLabel = "flag-global-topology-label"
testFlagMirrorSvcLabelSelector = "flag-mirror-label"
testFlagMirrorNamespace = "flag-namespace"
)
func TestConfig(t *testing.T) {
emptyConfig := []byte(`
{
"global": {}
}
`)
_, err := parseConfig(emptyConfig, testFlagGlobalSvcLabelSelector, testFlagGlobalSvcTopologyLabel, testFlagMirrorSvcLabelSelector, testFlagMirrorNamespace)
assert.Equal(t, fmt.Errorf("Configuration is missing local cluster name"), err)
globalConfigOnly := []byte(`
{
"global": {},
"localCluster":{
"name": "local_cluster"
}
}
`)
_, err = parseConfig(globalConfigOnly, testFlagGlobalSvcLabelSelector, testFlagGlobalSvcTopologyLabel, testFlagMirrorSvcLabelSelector, testFlagMirrorNamespace)
assert.Equal(t, fmt.Errorf("No remote cluster configuration defined"), err)
emptyRemoteConfigName := []byte(`
{
"localCluster":{
"name": "local_cluster"
},
"remoteClusters": [
{
"name": ""
}
]
}
`)
_, err = parseConfig(emptyRemoteConfigName, testFlagGlobalSvcLabelSelector, testFlagGlobalSvcTopologyLabel, testFlagMirrorSvcLabelSelector, testFlagMirrorNamespace)
assert.Equal(t, fmt.Errorf("Configuration is missing remote cluster name"), err)
insufficientRemoteKubeConfigPath := []byte(`
{
"localCluster": {
"name": "local_cluster",
"kubeConfigPath": "/path/to/kube/config"
},
"remoteClusters": [
{
"name": "remote_cluster_1",
"remoteCAURL": "remote_ca_url",
"remoteAPIURL": "remote_api_url"
}
]
}
`)
_, err = parseConfig(insufficientRemoteKubeConfigPath, testFlagGlobalSvcLabelSelector, testFlagGlobalSvcTopologyLabel, testFlagMirrorSvcLabelSelector, testFlagMirrorNamespace)
assert.Equal(t, fmt.Errorf("Insufficient configuration to create remote cluster client. Set kubeConfigPath or remoteAPIURL and remoteCAURL and remoteSATokenPath"), err)
rawFullConfig := []byte(`
{
"global": {
"globalSvcLabelSelector": "globalLabel",
"globalSvcRoutingStrategyLabel": "globalTopologyLabel",
"mirrorSvcLabelSelector": "mirrorLabel",
"mirrorNamespace": "sys-semaphore",
"serviceSync": true
},
"localCluster": {
"name": "local_cluster",
"kubeConfigPath": "/path/to/kube/config"
},
"remoteClusters": [
{
"name": "remote_cluster_1",
"remoteCAURL": "remote_ca_url",
"remoteAPIURL": "remote_api_url",
"remoteSATokenPath": "/path/to/token",
"resyncPeriod": "10s",
"servicePrefix": "cluster-1"
},
{
"name": "remote_cluster_2",
"kubeConfigPath": "/path/to/kube/config",
"servicePrefix": "cluster-2"
}
]
}
`)
config, err := parseConfig(rawFullConfig, "", "", "", "")
assert.Equal(t, nil, err)
assert.Equal(t, "globalLabel", config.Global.GlobalSvcLabelSelector)
assert.Equal(t, "globalTopologyLabel", config.Global.GlobalSvcRoutingStrategyLabel)
assert.Equal(t, "mirrorLabel", config.Global.MirrorSvcLabelSelector)
assert.Equal(t, "sys-semaphore", config.Global.MirrorNamespace)
assert.Equal(t, true, config.Global.ServiceSync)
assert.Equal(t, "local_cluster", config.LocalCluster.Name)
assert.Equal(t, "/path/to/kube/config", config.LocalCluster.KubeConfigPath)
assert.Equal(t, 2, len(config.RemoteClusters))
assert.Equal(t, "remote_ca_url", config.RemoteClusters[0].RemoteCAURL)
assert.Equal(t, "remote_api_url", config.RemoteClusters[0].RemoteAPIURL)
assert.Equal(t, "/path/to/token", config.RemoteClusters[0].RemoteSATokenPath)
assert.Equal(t, "", config.RemoteClusters[0].KubeConfigPath)
assert.Equal(t, Duration{10 * time.Second}, config.RemoteClusters[0].ResyncPeriod)
assert.Equal(t, "cluster-1", config.RemoteClusters[0].ServicePrefix)
assert.Equal(t, "remote_cluster_2", config.RemoteClusters[1].Name)
assert.Equal(t, "", config.RemoteClusters[1].RemoteCAURL)
assert.Equal(t, "", config.RemoteClusters[1].RemoteAPIURL)
assert.Equal(t, "", config.RemoteClusters[1].RemoteSATokenPath)
assert.Equal(t, "/path/to/kube/config", config.RemoteClusters[1].KubeConfigPath)
assert.Equal(t, Duration{0}, config.RemoteClusters[1].ResyncPeriod)
assert.Equal(t, "cluster-2", config.RemoteClusters[1].ServicePrefix)
}