|
31 | 31 | import com.cloud.agent.api.routing.LoadBalancerConfigCommand; |
32 | 32 | import com.cloud.agent.api.to.LoadBalancerTO; |
33 | 33 | import com.cloud.network.lb.LoadBalancingRule.LbDestination; |
34 | | -import com.cloud.utils.Pair; |
35 | | -import com.cloud.network.rules.LbStickinessMethod.StickinessMethodType; |
36 | 34 | import com.cloud.network.lb.LoadBalancingRule.LbStickinessPolicy; |
| 35 | +import com.cloud.network.rules.LbStickinessMethod.StickinessMethodType; |
| 36 | +import com.cloud.utils.Pair; |
37 | 37 | import com.cloud.network.lb.LoadBalancingRule.LbSslCert; |
38 | 38 |
|
39 | 39 | import java.util.List; |
@@ -202,6 +202,114 @@ public void generateConfigurationTestIdleTimeoutDoesNotLeakToNextConfig() { |
202 | 202 | assertTrue("an unset idle timeout should fall back to the default", result.contains("\ttimeout server 50000")); |
203 | 203 | } |
204 | 204 |
|
| 205 | + private LoadBalancerConfigCommand cmdFor(LoadBalancerTO lb, boolean offeringKeepAlive) { |
| 206 | + LoadBalancerTO[] lba = new LoadBalancerTO[1]; |
| 207 | + lba[0] = lb; |
| 208 | + return new LoadBalancerConfigCommand(lba, "10.0.0.1", "10.1.0.1", "10.1.1.1", null, 1L, "12", offeringKeepAlive, 50000L); |
| 209 | + } |
| 210 | + |
| 211 | + private LoadBalancerTO httpRule() { |
| 212 | + return new LoadBalancerTO("1", "10.2.0.1", 80, "http", "roundrobin", false, false, false, null); |
| 213 | + } |
| 214 | + |
| 215 | + /** Just the "listen" block for this rule, so assertions cannot match the defaults or stats sections. */ |
| 216 | + private String poolSection(String config, String poolName) { |
| 217 | + int start = config.indexOf("listen " + poolName + "\n"); |
| 218 | + Assert.assertTrue("no listen block for " + poolName, start >= 0); |
| 219 | + int next = config.indexOf("\nlisten ", start + 1); |
| 220 | + return next < 0 ? config.substring(start) : config.substring(start, next); |
| 221 | + } |
| 222 | + |
| 223 | + @Test |
| 224 | + public void generateConfigurationTestPerRuleKeepAliveKeepsHttpMode() { |
| 225 | + LoadBalancerTO lb = httpRule(); |
| 226 | + lb.setKeepAlive(true); |
| 227 | + String pool = poolSection(genConfig(new HAProxyConfigurator(), cmdFor(lb, false)), "10_2_0_1-80"); |
| 228 | + assertTrue("the rule should stay in http mode so forwardfor still works", pool.contains("\tmode http")); |
| 229 | + assertTrue(pool.contains("\toption http-keep-alive")); |
| 230 | + Assert.assertFalse(pool.contains("\toption httpclose")); |
| 231 | + } |
| 232 | + |
| 233 | + @Test |
| 234 | + public void generateConfigurationTestPerRuleKeepAliveOverridesTheOffering() { |
| 235 | + LoadBalancerTO lb = httpRule(); |
| 236 | + lb.setKeepAlive(false); |
| 237 | + String pool = poolSection(genConfig(new HAProxyConfigurator(), cmdFor(lb, true)), "10_2_0_1-80"); |
| 238 | + assertTrue("the rule should win over the offering", pool.contains("\toption httpclose")); |
| 239 | + Assert.assertFalse(pool.contains("\toption http-keep-alive")); |
| 240 | + } |
| 241 | + |
| 242 | + @Test |
| 243 | + public void generateConfigurationTestUnsetKeepAliveLeavesOldBehaviourAlone() { |
| 244 | + String pool = poolSection(genConfig(new HAProxyConfigurator(), cmdFor(httpRule(), true)), "10_2_0_1-80"); |
| 245 | + Assert.assertFalse("keepalive from the offering still drops to tcp mode", pool.contains("\tmode http")); |
| 246 | + |
| 247 | + pool = poolSection(genConfig(new HAProxyConfigurator(), cmdFor(httpRule(), false)), "10_2_0_1-80"); |
| 248 | + assertTrue(pool.contains("\tmode http")); |
| 249 | + assertTrue(pool.contains("\toption httpclose")); |
| 250 | + } |
| 251 | + |
| 252 | + @Test |
| 253 | + public void generateConfigurationTestKeepAliveTimeout() { |
| 254 | + LoadBalancerTO lb = httpRule(); |
| 255 | + lb.setKeepAlive(true); |
| 256 | + lb.setKeepAliveTimeout(15000L); |
| 257 | + String pool = poolSection(genConfig(new HAProxyConfigurator(), cmdFor(lb, false)), "10_2_0_1-80"); |
| 258 | + assertTrue(pool.contains("\ttimeout http-keep-alive 15000")); |
| 259 | + } |
| 260 | + |
| 261 | + @Test |
| 262 | + public void generateConfigurationTestKeepAliveTimeoutNeedsKeepAlive() { |
| 263 | + LoadBalancerTO lb = httpRule(); |
| 264 | + lb.setKeepAliveTimeout(15000L); |
| 265 | + String pool = poolSection(genConfig(new HAProxyConfigurator(), cmdFor(lb, false)), "10_2_0_1-80"); |
| 266 | + Assert.assertFalse("without keepalive there is no idle connection to time out", pool.contains("timeout http-keep-alive")); |
| 267 | + } |
| 268 | + |
| 269 | + @Test |
| 270 | + public void generateConfigurationTestIdleTimeoutOverridesTheGlobalPerRule() { |
| 271 | + LoadBalancerTO lb = new LoadBalancerTO("1", "10.2.0.1", 3306, "tcp", "roundrobin", false, false, false, null); |
| 272 | + lb.setIdleTimeout(600000L); |
| 273 | + String config = genConfig(new HAProxyConfigurator(), cmdFor(lb, false)); |
| 274 | + String pool = poolSection(config, "10_2_0_1-3306"); |
| 275 | + assertTrue("a tcp rule gets the timeouts too", pool.contains("\ttimeout client 600000")); |
| 276 | + assertTrue(pool.contains("\ttimeout server 600000")); |
| 277 | + assertTrue("the global still sets the defaults", config.contains("\ttimeout client 50000")); |
| 278 | + } |
| 279 | + |
| 280 | + @Test |
| 281 | + public void generateConfigurationTestNegativeTimeoutsAreDropped() { |
| 282 | + LoadBalancerTO lb = httpRule(); |
| 283 | + lb.setKeepAlive(true); |
| 284 | + lb.setIdleTimeout(-1L); |
| 285 | + lb.setKeepAliveTimeout(-5L); |
| 286 | + String pool = poolSection(genConfig(new HAProxyConfigurator(), cmdFor(lb, false)), "10_2_0_1-80"); |
| 287 | + Assert.assertFalse("a negative timeout is a fatal haproxy parse error", pool.contains("-1")); |
| 288 | + Assert.assertFalse("a negative timeout is a fatal haproxy parse error", pool.contains("-5")); |
| 289 | + Assert.assertFalse(pool.contains("timeout client")); |
| 290 | + Assert.assertFalse(pool.contains("timeout http-keep-alive")); |
| 291 | + } |
| 292 | + |
| 293 | + @Test |
| 294 | + public void generateConfigurationTestPerRuleSettingsDoNotCrossContaminate() { |
| 295 | + LoadBalancerTO tuned = httpRule(); |
| 296 | + tuned.setKeepAlive(true); |
| 297 | + tuned.setIdleTimeout(600000L); |
| 298 | + tuned.setKeepAliveTimeout(15000L); |
| 299 | + LoadBalancerTO plain = new LoadBalancerTO("2", "10.2.0.1", 8080, "tcp", "roundrobin", false, false, false, null); |
| 300 | + LoadBalancerTO[] lba = new LoadBalancerTO[] {tuned, plain}; |
| 301 | + LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(lba, "10.0.0.1", "10.1.0.1", "10.1.1.1", null, 1L, "12", false, 50000L); |
| 302 | + String config = genConfig(new HAProxyConfigurator(), cmd); |
| 303 | + |
| 304 | + String tunedPool = poolSection(config, "10_2_0_1-80"); |
| 305 | + assertTrue(tunedPool.contains("\toption http-keep-alive")); |
| 306 | + assertTrue(tunedPool.contains("\ttimeout client 600000")); |
| 307 | + |
| 308 | + String plainPool = poolSection(config, "10_2_0_1-8080"); |
| 309 | + Assert.assertFalse("the other rule's settings must not leak here", plainPool.contains("http-keep-alive")); |
| 310 | + Assert.assertFalse("the other rule's settings must not leak here", plainPool.contains("timeout client")); |
| 311 | + } |
| 312 | + |
205 | 313 | @Test |
206 | 314 | public void generateConfigurationTestAppCookieStickinessUsesAStickTable() { |
207 | 315 | List<Pair<String, String>> params = new ArrayList<>(); |
|
0 commit comments