1 package entrypoint_test
2
3 import (
4 "strings"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9
10 "github.com/emissary-ingress/emissary/v3/cmd/entrypoint"
11 v3bootstrap "github.com/emissary-ingress/emissary/v3/pkg/api/envoy/config/bootstrap/v3"
12 v3cluster "github.com/emissary-ingress/emissary/v3/pkg/api/envoy/config/cluster/v3"
13 )
14
15 func ClusterHasAltName(altName string) func(*v3cluster.Cluster) bool {
16 return func(c *v3cluster.Cluster) bool {
17 return c.AltStatName == altName
18 }
19 }
20
21 func TestFakeCollision(t *testing.T) {
22 f := entrypoint.RunFake(t, entrypoint.FakeConfig{EnvoyConfig: true, DiagdDebug: true}, nil)
23
24 assert.NoError(t, f.UpsertFile("testdata/Collision1.yaml"))
25 f.Flush()
26
27 snap, err := f.GetSnapshot(HasMapping("staging", "subway-staging-socket-stable-mapping"))
28 require.NoError(t, err)
29
30
31 assert.NotNil(t, snap)
32
33
34
35
36
37
38 envoyConfig, err := f.GetEnvoyConfig(func(config *v3bootstrap.Bootstrap) bool {
39
40 if len(config.StaticResources.Clusters) != 3 {
41 return false
42 }
43
44
45 foundClusters := map[string]bool{}
46
47 for _, cluster := range config.StaticResources.Clusters {
48 foundClusters[cluster.AltStatName] = true
49 }
50
51
52 if !foundClusters["127_0_0_1_8877"] ||
53 !foundClusters["subway_staging_stable_staging_3000"] ||
54 !foundClusters["subway_staging_stable_staging_3001"] {
55 return false
56 }
57
58 return true
59 })
60
61 require.NoError(t, err)
62 assert.NotNil(t, envoyConfig)
63
64 LogJSON(t, envoyConfig)
65
66
67
68
69 for _, cluster := range envoyConfig.StaticResources.Clusters {
70 LogJSON(t, cluster)
71 }
72
73 c0 := FindCluster(envoyConfig, ClusterHasAltName("subway_staging_stable_staging_3000"))
74 assert.NotNil(t, c0)
75
76 c1 := FindCluster(envoyConfig, ClusterHasAltName("subway_staging_stable_staging_3001"))
77 assert.NotNil(t, c1)
78
79
80
81
82
83 assert.Equal(t, "k8s/staging/subway-staging-stable/3000", c0.EdsClusterConfig.ServiceName)
84
85 assert.True(t, strings.HasPrefix(c0.Name, "cluster_subway_staging_stable_staging_30-"))
86 assert.True(t, strings.HasSuffix(c0.Name, "-0"))
87
88
89
90
91
92 assert.Equal(t, "k8s/staging/subway-staging-stable/3001", c1.EdsClusterConfig.ServiceName)
93
94 assert.True(t, strings.HasPrefix(c1.Name, "cluster_subway_staging_stable_staging_30-"))
95 assert.True(t, strings.HasSuffix(c1.Name, "-0"))
96
97
98
99
100 nameMap := map[string]int{}
101
102 for _, cluster := range envoyConfig.StaticResources.Clusters {
103 nameMap[cluster.Name]++
104 assert.Equal(t, 1, nameMap[cluster.Name])
105 }
106
107
108 assert.NoError(t, f.UpsertFile("testdata/Collision2.yaml"))
109 f.Flush()
110
111 snap, err = f.GetSnapshot(HasMapping("staging", "subway-staging-socket-stable-mapping"))
112
113
114 assert.NotNil(t, snap)
115
116
117
118
119
120
121
122
123
124 envoyConfig, err = f.GetEnvoyConfig(func(config *v3bootstrap.Bootstrap) bool {
125
126 if len(config.StaticResources.Clusters) != 4 {
127 return false
128 }
129
130
131 foundClusters := map[string]bool{}
132
133 for _, cluster := range config.StaticResources.Clusters {
134 foundClusters[cluster.AltStatName] = true
135 }
136
137
138 if !foundClusters["127_0_0_1_8877"] ||
139 !foundClusters["subway_staging_stable_staging_3000"] ||
140 !foundClusters["subway_staging_stable_staging_3001"] {
141 return false
142 }
143
144 return true
145 })
146
147 require.NoError(t, err)
148 assert.NotNil(t, envoyConfig)
149
150 LogJSON(t, envoyConfig)
151
152
153
154
155
156
157
158
159 nameMap = map[string]int{}
160 altNameMap := map[string]int{}
161
162 for _, cluster := range envoyConfig.StaticResources.Clusters {
163
164 nameMap[cluster.Name]++
165 assert.Equal(t, 1, nameMap[cluster.Name])
166
167
168
169 altNameMap[cluster.AltStatName]++
170
171
172
173
174
175 if cluster.AltStatName != "127_0_0_1_8877" {
176 assert.True(t, strings.HasPrefix(cluster.Name, "cluster_subway_staging_stable_staging_30-"))
177 assert.True(t, strings.HasSuffix(cluster.Name, "-0"))
178 }
179 }
180
181
182
183
184
185 assert.Equal(t, 1, altNameMap["127_0_0_1_8877"])
186 assert.Equal(t, 2, altNameMap["subway_staging_stable_staging_3000"])
187 assert.Equal(t, 1, altNameMap["subway_staging_stable_staging_3001"])
188 }
189
View as plain text