Skip to content

Commit 5f830fe

Browse files
committed
interface based tests
1 parent b42bc5a commit 5f830fe

File tree

2 files changed

+103
-75
lines changed

2 files changed

+103
-75
lines changed

envoy-control-tests/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/routing/CanaryLoadBalancingTest.kt

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import pl.allegro.tech.servicemesh.envoycontrol.config.envoy.ResponseWithBody
1212
import pl.allegro.tech.servicemesh.envoycontrol.config.envoycontrol.EnvoyControlExtension
1313
import pl.allegro.tech.servicemesh.envoycontrol.config.service.EchoServiceExtension
1414

15-
open class CanaryLoadBalancingTest {
15+
open class ImplCanaryLoadBalancingTest : CanaryLoadBalancingTest {
1616

1717
companion object {
1818
private val properties = mapOf(
@@ -44,12 +44,23 @@ open class CanaryLoadBalancingTest {
4444
@RegisterExtension
4545
val envoy = EnvoyExtension(envoyControl)
4646
}
47+
override fun envoyControl() = envoyControl
4748

49+
override fun envoy() = envoy
50+
51+
override fun consul() = consul
52+
53+
override fun canaryContainer() = canaryContainer
54+
55+
override fun regularContainer() = regularContainer
56+
}
57+
58+
interface CanaryLoadBalancingTest {
4859
@Test
4960
fun `should balance load according to weights`() {
5061
// given
51-
consul.server.operations.registerService(name = "echo", extension = canaryContainer(), tags = listOf("canary", "weight:1"))
52-
consul.server.operations.registerService(name = "echo", extension = regularContainer(), tags = listOf("weight:20"))
62+
consul().server.operations.registerService(name = "echo", extension = canaryContainer(), tags = listOf("canary", "weight:1"))
63+
consul().server.operations.registerService(name = "echo", extension = regularContainer(), tags = listOf("weight:20"))
5364

5465
untilAsserted {
5566
envoy().egressOperations.callService("echo").also {
@@ -92,8 +103,8 @@ open class CanaryLoadBalancingTest {
92103
@Test
93104
fun `should route request to canary instance only`() {
94105
// given
95-
consul.server.operations.registerService(name = "echo", extension = canaryContainer(), tags = listOf("canary", "weight:1"))
96-
consul.server.operations.registerService(name = "echo", extension = regularContainer(), tags = listOf("weight:20"))
106+
consul().server.operations.registerService(name = "echo", extension = canaryContainer(), tags = listOf("canary", "weight:1"))
107+
consul().server.operations.registerService(name = "echo", extension = regularContainer(), tags = listOf("weight:20"))
97108

98109
untilAsserted {
99110
envoy().egressOperations.callService("echo").also {
@@ -115,9 +126,9 @@ open class CanaryLoadBalancingTest {
115126
}
116127

117128
@Test
118-
open fun `should route to both canary and regular instances when canary weight is 0`() {
119-
consul.server.operations.registerService(name = "echo", extension = canaryContainer(), tags = listOf("canary", "weight:0"))
120-
consul.server.operations.registerService(name = "echo", extension = regularContainer(), tags = listOf("weight:20"))
129+
fun `should route to both canary and regular instances when canary weight is 0`() {
130+
consul().server.operations.registerService(name = "echo", extension = canaryContainer(), tags = listOf("canary", "weight:0"))
131+
consul().server.operations.registerService(name = "echo", extension = regularContainer(), tags = listOf("weight:20"))
121132

122133
untilAsserted {
123134
envoy().egressOperations.callService("echo").also {
@@ -138,7 +149,7 @@ open class CanaryLoadBalancingTest {
138149
assertThat(stats.canaryHits).isGreaterThan(0)
139150
}
140151

141-
protected open fun callStats() = CallStats(listOf(canaryContainer(), regularContainer()))
152+
fun callStats() = CallStats(listOf(canaryContainer(), regularContainer()))
142153

143154
fun callEchoServiceRepeatedly(
144155
minRepeat: Int,
@@ -163,13 +174,13 @@ open class CanaryLoadBalancingTest {
163174
val CallStats.canaryHits: Int
164175
get() = this.hits(canaryContainer())
165176

166-
open fun envoyControl() = envoyControl
177+
fun envoyControl(): EnvoyControlExtension
167178

168-
open fun envoy() = envoy
179+
fun envoy(): EnvoyExtension
169180

170-
open fun consul() = consul
181+
fun consul(): ConsulExtension
171182

172-
open fun canaryContainer() = canaryContainer
183+
fun canaryContainer(): EchoServiceExtension
173184

174-
open fun regularContainer() = regularContainer
185+
fun regularContainer(): EchoServiceExtension
175186
}

envoy-control-tests/src/main/kotlin/pl/allegro/tech/servicemesh/envoycontrol/routing/ServiceTagsTest.kt

Lines changed: 78 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import pl.allegro.tech.servicemesh.envoycontrol.config.envoy.EnvoyExtension
99
import pl.allegro.tech.servicemesh.envoycontrol.config.envoycontrol.EnvoyControlExtension
1010
import pl.allegro.tech.servicemesh.envoycontrol.config.service.EchoServiceExtension
1111

12-
open class ServiceTagsTest {
12+
open class ImplServiceTagsTest : ServiceTagsTest {
1313

1414
companion object {
1515
private val properties = mapOf(
@@ -55,22 +55,35 @@ open class ServiceTagsTest {
5555
val envoy = EnvoyExtension(envoyControl, regularService)
5656
}
5757

58-
protected fun registerServices() {
59-
consul.server.operations.registerService(name = "echo", extension = regularService, tags = emptyList())
60-
consul.server.operations.registerService(
58+
override fun envoy() = envoy
59+
60+
override fun envoyControl() = envoyControl
61+
62+
override fun consul() = consul
63+
override fun regularService() = regularService
64+
override fun loremService() = loremService
65+
override fun loremIpsumService() = loremIpsumService
66+
override fun genericService() = genericService
67+
}
68+
69+
interface ServiceTagsTest {
70+
71+
fun registerServices() {
72+
consul().server.operations.registerService(name = "echo", extension = regularService(), tags = emptyList())
73+
consul().server.operations.registerService(
6174
name = "echo",
62-
extension = loremService,
75+
extension = loremService(),
6376
tags = listOf("lorem", "blacklisted")
6477
)
65-
consul.server.operations.registerService(
78+
consul().server.operations.registerService(
6679
name = "echo",
67-
extension = loremIpsumService,
80+
extension = loremIpsumService(),
6881
tags = listOf("lorem", "ipsum")
6982
)
7083
}
7184

7285
@Test
73-
open fun `should route requests to instance with tag ipsum`() {
86+
fun `should route requests to instance with tag ipsum`() {
7487
// given
7588
registerServices()
7689
envoy().waitForReadyServices("echo")
@@ -80,13 +93,13 @@ open class ServiceTagsTest {
8093

8194
// then
8295
assertThat(stats.totalHits).isEqualTo(10)
83-
assertThat(stats.hits(regularService)).isEqualTo(0)
84-
assertThat(stats.hits(loremService)).isEqualTo(0)
85-
assertThat(stats.hits(loremIpsumService)).isEqualTo(10)
96+
assertThat(stats.hits(regularService())).isEqualTo(0)
97+
assertThat(stats.hits(loremService())).isEqualTo(0)
98+
assertThat(stats.hits(loremIpsumService())).isEqualTo(10)
8699
}
87100

88101
@Test
89-
open fun `should route requests to instances with tag lorem`() {
102+
fun `should route requests to instances with tag lorem`() {
90103
// given
91104
registerServices()
92105
envoy().waitForReadyServices("echo")
@@ -96,14 +109,14 @@ open class ServiceTagsTest {
96109

97110
// then
98111
assertThat(stats.totalHits).isEqualTo(20)
99-
assertThat(stats.hits(regularService)).isEqualTo(0)
100-
assertThat(stats.hits(loremService)).isGreaterThan(2)
101-
assertThat(stats.hits(loremIpsumService)).isGreaterThan(2)
102-
assertThat(stats.hits(loremService) + stats.hits(loremIpsumService)).isEqualTo(20)
112+
assertThat(stats.hits(regularService())).isEqualTo(0)
113+
assertThat(stats.hits(loremService())).isGreaterThan(2)
114+
assertThat(stats.hits(loremIpsumService())).isGreaterThan(2)
115+
assertThat(stats.hits(loremService()) + stats.hits(loremIpsumService())).isEqualTo(20)
103116
}
104117

105118
@Test
106-
open fun `should route requests to all instances`() {
119+
fun `should route requests to all instances`() {
107120
// given
108121
registerServices()
109122
envoy().waitForReadyServices("echo")
@@ -113,20 +126,20 @@ open class ServiceTagsTest {
113126

114127
// then
115128
assertThat(stats.totalHits).isEqualTo(20)
116-
assertThat(stats.hits(regularService)).isGreaterThan(1)
117-
assertThat(stats.hits(loremService)).isGreaterThan(1)
118-
assertThat(stats.hits(loremIpsumService)).isGreaterThan(1)
129+
assertThat(stats.hits(regularService())).isGreaterThan(1)
130+
assertThat(stats.hits(loremService())).isGreaterThan(1)
131+
assertThat(stats.hits(loremIpsumService())).isGreaterThan(1)
119132
assertThat(
120-
stats.hits(regularService) + stats.hits(loremService) + stats.hits(
121-
loremIpsumService
133+
stats.hits(regularService()) + stats.hits(loremService()) + stats.hits(
134+
loremIpsumService()
122135
)
123136
).isEqualTo(
124137
20
125138
)
126139
}
127140

128141
@Test
129-
open fun `should return 503 if instance with requested tag is not found`() {
142+
fun `should return 503 if instance with requested tag is not found`() {
130143
// given
131144
registerServices()
132145
envoy().waitForReadyServices("echo")
@@ -137,13 +150,13 @@ open class ServiceTagsTest {
137150
// then
138151
assertThat(stats.totalHits).isEqualTo(10)
139152
assertThat(stats.failedHits).isEqualTo(10)
140-
assertThat(stats.hits(regularService)).isEqualTo(0)
141-
assertThat(stats.hits(loremService)).isEqualTo(0)
142-
assertThat(stats.hits(loremIpsumService)).isEqualTo(0)
153+
assertThat(stats.hits(regularService())).isEqualTo(0)
154+
assertThat(stats.hits(loremService())).isEqualTo(0)
155+
assertThat(stats.hits(loremIpsumService())).isEqualTo(0)
143156
}
144157

145158
@Test
146-
open fun `should return 503 if requested tag is blacklisted`() {
159+
fun `should return 503 if requested tag is blacklisted`() {
147160
// given
148161
registerServices()
149162
envoy().waitForReadyServices("echo")
@@ -154,22 +167,22 @@ open class ServiceTagsTest {
154167
// then
155168
assertThat(stats.totalHits).isEqualTo(10)
156169
assertThat(stats.failedHits).isEqualTo(10)
157-
assertThat(stats.hits(regularService)).isEqualTo(0)
158-
assertThat(stats.hits(loremService)).isEqualTo(0)
159-
assertThat(stats.hits(loremIpsumService)).isEqualTo(0)
170+
assertThat(stats.hits(regularService())).isEqualTo(0)
171+
assertThat(stats.hits(loremService())).isEqualTo(0)
172+
assertThat(stats.hits(loremIpsumService())).isEqualTo(0)
160173
}
161174

162175
@Test
163-
open fun `should route request with three tags if combination is valid`() {
176+
fun `should route request with three tags if combination is valid`() {
164177
// given
165-
val matching = loremService
166-
val notMatching = loremIpsumService
178+
val matching = loremService()
179+
val notMatching = loremIpsumService()
167180

168-
consul.server.operations.registerService(
181+
consul().server.operations.registerService(
169182
name = "service-1", extension = matching,
170183
tags = listOf("version:v1.5", "hardware:c32", "role:master")
171184
)
172-
consul.server.operations.registerService(
185+
consul().server.operations.registerService(
173186
name = "service-1", extension = notMatching,
174187
tags = listOf("version:v1.5", "hardware:c64", "role:master")
175188
)
@@ -188,11 +201,11 @@ open class ServiceTagsTest {
188201
}
189202

190203
@Test
191-
open fun `should not route request with multiple tags if service is not whitelisted`() {
204+
fun `should not route request with multiple tags if service is not whitelisted`() {
192205
// given
193-
val matching = loremService
206+
val matching = loremService()
194207

195-
consul.server.operations.registerService(
208+
consul().server.operations.registerService(
196209
name = "service-3", extension = matching,
197210
tags = listOf("version:v1.5", "hardware:c32", "role:master")
198211
)
@@ -225,16 +238,16 @@ open class ServiceTagsTest {
225238
}
226239

227240
@Test
228-
open fun `should not route request with three tags if combination is not allowed`() {
241+
fun `should not route request with three tags if combination is not allowed`() {
229242
// given
230-
val service1Matching = loremService
231-
val service2Matching = loremIpsumService
243+
val service1Matching = loremService()
244+
val service2Matching = loremIpsumService()
232245

233-
consul.server.operations.registerService(
246+
consul().server.operations.registerService(
234247
name = "service-1", extension = service1Matching,
235248
tags = listOf("version:v1.5", "hardware:c32", "ram:512")
236249
)
237-
consul.server.operations.registerService(
250+
consul().server.operations.registerService(
238251
name = "service-2", extension = service2Matching,
239252
tags = listOf("version:v1.5", "hardware:c32", "role:master")
240253
)
@@ -260,26 +273,26 @@ open class ServiceTagsTest {
260273
}
261274

262275
@Test
263-
open fun `should route request with two tags if combination is valid`() {
276+
fun `should route request with two tags if combination is valid`() {
264277
// given
265-
val service1Matching = loremService
266-
val service1NotMatching = regularService
267-
val service2Master = loremIpsumService
268-
val service2Secondary = genericService
278+
val service1Matching = loremService()
279+
val service1NotMatching = regularService()
280+
val service2Master = loremIpsumService()
281+
val service2Secondary = genericService()
269282

270-
consul.server.operations.registerService(
283+
consul().server.operations.registerService(
271284
name = "service-1", extension = service1Matching,
272285
tags = listOf("version:v2.0", "hardware:c32", "role:master")
273286
)
274-
consul.server.operations.registerService(
287+
consul().server.operations.registerService(
275288
name = "service-1", extension = service1NotMatching,
276289
tags = listOf("version:v1.5", "hardware:c32", "role:master")
277290
)
278-
consul.server.operations.registerService(
291+
consul().server.operations.registerService(
279292
name = "service-2", extension = service2Master,
280293
tags = listOf("version:v1.5", "hardware:c32", "role:master")
281294
)
282-
consul.server.operations.registerService(
295+
consul().server.operations.registerService(
283296
name = "service-2", extension = service2Secondary,
284297
tags = listOf("version:v2.0", "hardware:c32", "role:secondary")
285298
)
@@ -309,11 +322,11 @@ open class ServiceTagsTest {
309322
}
310323

311324
@Test
312-
open fun `should not route request with two tags if combination is not allowed`() {
325+
fun `should not route request with two tags if combination is not allowed`() {
313326
// given
314-
val matching = loremService
327+
val matching = loremService()
315328

316-
consul.server.operations.registerService(
329+
consul().server.operations.registerService(
317330
name = "service-2", extension = matching,
318331
tags = listOf("version:v1.5", "hardware:c32", "role:master")
319332
)
@@ -331,7 +344,7 @@ open class ServiceTagsTest {
331344
assertThat(stats.hits(matching)).isEqualTo(0)
332345
}
333346

334-
protected fun callEchoServiceRepeatedly(
347+
fun callEchoServiceRepeatedly(
335348
repeat: Int,
336349
tag: String? = null,
337350
assertNoErrors: Boolean = true
@@ -344,9 +357,9 @@ open class ServiceTagsTest {
344357
)
345358
}
346359

347-
protected open fun callStats() = CallStats(listOf(regularService, loremService, loremIpsumService, genericService))
360+
fun callStats() = CallStats(listOf(regularService(), loremService(), loremIpsumService(), genericService()))
348361

349-
protected open fun callServiceRepeatedly(
362+
fun callServiceRepeatedly(
350363
service: String,
351364
repeat: Int,
352365
tag: String? = null,
@@ -364,7 +377,11 @@ open class ServiceTagsTest {
364377
return stats
365378
}
366379

367-
open fun envoy() = envoy
368-
369-
open fun envoyControl() = envoyControl
380+
fun consul(): ConsulExtension
381+
fun envoyControl(): EnvoyControlExtension
382+
fun regularService(): EchoServiceExtension
383+
fun loremService(): EchoServiceExtension
384+
fun loremIpsumService(): EchoServiceExtension
385+
fun genericService(): EchoServiceExtension
386+
fun envoy(): EnvoyExtension
370387
}

0 commit comments

Comments
 (0)