| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Config\ConfigException; |
| 4 |
use MediaWiki\Config\EtcdConfig; |
| 5 |
use Wikimedia\Http\MultiHttpClient; |
| 6 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 7 |
use Wikimedia\TestingAccessWrapper; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class EtcdConfigTest extends MediaWikiUnitTestCase { |
| 13 |
|
| 14 |
private function createConfigMock( array $options = [], ?array $methods = null ) { |
| 15 |
return $this->getMockBuilder( EtcdConfig::class ) |
| 16 |
->setConstructorArgs( [ $options + [ |
| 17 |
'host' => 'etcd-tcp.example.net', |
| 18 |
'directory' => '/', |
| 19 |
'timeout' => 0.1, |
| 20 |
] ] ) |
| 21 |
->onlyMethods( $methods ?? [ 'fetchAllFromEtcd' ] ) |
| 22 |
->getMock(); |
| 23 |
} |
| 24 |
|
| 25 |
private static function createEtcdResponse( array $response ) { |
| 26 |
$baseResponse = [ |
| 27 |
'config' => null, |
| 28 |
'error' => null, |
| 29 |
'retry' => false, |
| 30 |
'modifiedIndex' => 0, |
| 31 |
]; |
| 32 |
return array_merge( $baseResponse, $response ); |
| 33 |
} |
| 34 |
|
| 35 |
private function createSimpleConfigMock( array $config, $index = 0 ) { |
| 36 |
$mock = $this->createConfigMock(); |
| 37 |
$mock->expects( $this->once() )->method( 'fetchAllFromEtcd' ) |
| 38 |
->willReturn( self::createEtcdResponse( [ |
| 39 |
'config' => $config, |
| 40 |
'modifiedIndex' => $index, |
| 41 |
] ) ); |
| 42 |
return $mock; |
| 43 |
} |
| 44 |
|
| 45 |
private function createCallableMock() { |
| 46 |
return $this |
| 47 |
->getMockBuilder( \stdClass::class ) |
| 48 |
->addMethods( [ '__invoke' ] ) |
| 49 |
->getMock(); |
| 50 |
} |
| 51 |
|
| 52 |
public function testHasKnown() { |
| 53 |
$config = $this->createSimpleConfigMock( [ |
| 54 |
'known' => 'value' |
| 55 |
] ); |
| 56 |
$this->assertSame( true, $config->has( 'known' ) ); |
| 57 |
} |
| 58 |
|
| 59 |
public function testGetKnown() { |
| 60 |
$config = $this->createSimpleConfigMock( [ |
| 61 |
'known' => 'value' |
| 62 |
] ); |
| 63 |
$this->assertSame( 'value', $config->get( 'known' ) ); |
| 64 |
} |
| 65 |
|
| 66 |
public function testHasUnknown() { |
| 67 |
$config = $this->createSimpleConfigMock( [ |
| 68 |
'known' => 'value' |
| 69 |
] ); |
| 70 |
$this->assertSame( false, $config->has( 'unknown' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
public function testGetUnknown() { |
| 74 |
$config = $this->createSimpleConfigMock( [ |
| 75 |
'known' => 'value' |
| 76 |
] ); |
| 77 |
$this->expectException( ConfigException::class ); |
| 78 |
$config->get( 'unknown' ); |
| 79 |
} |
| 80 |
|
| 81 |
public function testGetModifiedIndex() { |
| 82 |
$config = $this->createSimpleConfigMock( |
| 83 |
[ 'some' => 'value' ], |
| 84 |
123 |
| 85 |
); |
| 86 |
$this->assertSame( 123, $config->getModifiedIndex() ); |
| 87 |
} |
| 88 |
|
| 89 |
public function testConstructCacheObj() { |
| 90 |
$cache = $this->getMockBuilder( HashBagOStuff::class ) |
| 91 |
->onlyMethods( [ 'get' ] ) |
| 92 |
->getMock(); |
| 93 |
$cache->expects( $this->once() )->method( 'get' ) |
| 94 |
->willReturn( [ |
| 95 |
'config' => [ 'known' => 'from-cache' ], |
| 96 |
'expires' => INF, |
| 97 |
'modifiedIndex' => 123 |
| 98 |
] ); |
| 99 |
$config = $this->createConfigMock( [ 'cache' => $cache ] ); |
| 100 |
|
| 101 |
$this->assertSame( 'from-cache', $config->get( 'known' ) ); |
| 102 |
} |
| 103 |
|
| 104 |
public function testConstructCacheSpec() { |
| 105 |
$config = $this->createConfigMock( [ 'cache' => [ |
| 106 |
'class' => HashBagOStuff::class |
| 107 |
] ] ); |
| 108 |
$config->expects( $this->once() )->method( 'fetchAllFromEtcd' ) |
| 109 |
->willReturn( self::createEtcdResponse( |
| 110 |
[ 'config' => [ 'known' => 'from-fetch' ], ] ) ); |
| 111 |
|
| 112 |
$this->assertSame( 'from-fetch', $config->get( 'known' ) ); |
| 113 |
} |
| 114 |
|
| 115 |
public static function provideScenario() { |
| 116 |
$miss = false; |
| 117 |
$hit = [ |
| 118 |
'config' => [ 'mykey' => 'from-cache' ], |
| 119 |
'expires' => INF, |
| 120 |
'modifiedIndex' => 0, |
| 121 |
]; |
| 122 |
$stale = [ |
| 123 |
'config' => [ 'mykey' => 'from-cache-expired' ], |
| 124 |
'expires' => -INF, |
| 125 |
'modifiedIndex' => 0, |
| 126 |
]; |
| 127 |
|
| 128 |
yield 'Cache miss' => [ 'from-fetch', [ |
| 129 |
'cache' => $miss, |
| 130 |
'lock' => 'acquired', |
| 131 |
'backend' => 'success', |
| 132 |
] ]; |
| 133 |
|
| 134 |
yield 'Cache miss with backend error' => [ 'error', [ |
| 135 |
'cache' => $miss, |
| 136 |
'lock' => 'acquired', |
| 137 |
'backend' => 'error', |
| 138 |
] ]; |
| 139 |
|
| 140 |
yield 'Cache miss with retry after backend error' => [ 'from-cache', [ |
| 141 |
'cache' => [ $miss, $hit ], |
| 142 |
'lock' => 'acquired', |
| 143 |
'backend' => 'error-may-retry', |
| 144 |
] ]; |
| 145 |
|
| 146 |
yield 'Cache hit after lock fail and cache retry' => [ 'from-cache', [ |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
'cache' => [ $miss, $hit ], |
| 151 |
'lock' => 'fail', |
| 152 |
'backend' => 'never', |
| 153 |
] ]; |
| 154 |
|
| 155 |
yield 'Cache hit' => [ 'from-cache', [ |
| 156 |
'cache' => $hit, |
| 157 |
'lock' => 'never', |
| 158 |
'backend' => 'never', |
| 159 |
] ]; |
| 160 |
|
| 161 |
yield 'Cache expired' => [ 'from-fetch', [ |
| 162 |
'cache' => $stale, |
| 163 |
'lock' => 'acquired', |
| 164 |
'backend' => 'success', |
| 165 |
] ]; |
| 166 |
|
| 167 |
yield 'Cache expired with retry after backend failure' => [ 'from-cache-expired', [ |
| 168 |
'cache' => $stale, |
| 169 |
'lock' => 'acquired', |
| 170 |
'backend' => 'error-may-retry', |
| 171 |
] ]; |
| 172 |
|
| 173 |
yield 'Cache expired without retry after backend failure' => [ 'from-cache-expired', [ |
| 174 |
'cache' => $stale, |
| 175 |
'lock' => 'acquired', |
| 176 |
'backend' => 'error', |
| 177 |
] ]; |
| 178 |
|
| 179 |
yield 'Cache expired with lock failure' => [ 'from-cache-expired', [ |
| 180 |
'cache' => $stale, |
| 181 |
'lock' => 'fail', |
| 182 |
'backend' => 'never', |
| 183 |
] ]; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function testScenario( string $expect, array $scenario ) { |
| 190 |
|
| 191 |
$cache = $this->getMockBuilder( HashBagOStuff::class ) |
| 192 |
->onlyMethods( [ 'get', 'lock' ] ) |
| 193 |
->getMock(); |
| 194 |
if ( is_array( $scenario['cache'] ) && array_is_list( $scenario['cache'] ) ) { |
| 195 |
$cache->expects( $this->exactly( count( $scenario['cache'] ) ) )->method( 'get' ) |
| 196 |
->willReturnOnConsecutiveCalls( |
| 197 |
...$scenario['cache'] |
| 198 |
); |
| 199 |
} else { |
| 200 |
$cache->expects( $this->any() )->method( 'get' ) |
| 201 |
->willReturn( $scenario['cache'] ); |
| 202 |
} |
| 203 |
if ( $scenario['lock'] === 'acquired' ) { |
| 204 |
$cache->expects( $this->once() )->method( 'lock' ) |
| 205 |
->willReturn( true ); |
| 206 |
} elseif ( $scenario['lock'] === 'fail' ) { |
| 207 |
$cache->expects( $this->once() )->method( 'lock' ) |
| 208 |
->willReturn( false ); |
| 209 |
} else { |
| 210 |
|
| 211 |
$cache->expects( $this->never() )->method( 'lock' ); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
$mock = $this->createConfigMock( [ |
| 216 |
'cache' => $cache, |
| 217 |
] ); |
| 218 |
if ( $scenario['backend'] === 'success' ) { |
| 219 |
$mock->expects( $this->once() )->method( 'fetchAllFromEtcd' ) |
| 220 |
->willReturn( |
| 221 |
self::createEtcdResponse( [ 'config' => [ 'mykey' => 'from-fetch' ] ] ) |
| 222 |
); |
| 223 |
} elseif ( $scenario['backend'] === 'error' ) { |
| 224 |
$mock->expects( $this->once() )->method( 'fetchAllFromEtcd' ) |
| 225 |
->willReturn( self::createEtcdResponse( [ 'error' => 'Fake error' ] ) ); |
| 226 |
} elseif ( $scenario['backend'] === 'error-may-retry' ) { |
| 227 |
$mock->expects( $this->once() )->method( 'fetchAllFromEtcd' ) |
| 228 |
->willReturn( self::createEtcdResponse( [ 'error' => 'Fake error', 'retry' => true ] ) ); |
| 229 |
} elseif ( $scenario['backend'] === 'never' ) { |
| 230 |
$mock->expects( $this->never() )->method( 'fetchAllFromEtcd' ); |
| 231 |
} |
| 232 |
|
| 233 |
if ( $expect === 'error' ) { |
| 234 |
$this->expectException( ConfigException::class ); |
| 235 |
@$mock->get( 'mykey' ); |
| 236 |
} else { |
| 237 |
$this->assertSame( $expect, @$mock->get( 'mykey' ) ); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
public function testLoadProcessCacheHit() { |
| 242 |
|
| 243 |
$cache = $this->getMockBuilder( HashBagOStuff::class ) |
| 244 |
->onlyMethods( [ 'get', 'lock' ] ) |
| 245 |
->getMock(); |
| 246 |
$cache->expects( $this->once() )->method( 'get' ) |
| 247 |
|
| 248 |
->willReturn( [ |
| 249 |
'config' => [ 'known' => 'from-cache' ], |
| 250 |
'expires' => INF, |
| 251 |
'modifiedIndex' => 0, |
| 252 |
] ); |
| 253 |
$cache->expects( $this->never() )->method( 'lock' ); |
| 254 |
|
| 255 |
|
| 256 |
$mock = $this->createConfigMock( [ |
| 257 |
'cache' => $cache, |
| 258 |
] ); |
| 259 |
$mock->expects( $this->never() )->method( 'fetchAllFromEtcd' ); |
| 260 |
|
| 261 |
$this->assertSame( 'from-cache', $mock->get( 'known' ), 'Cache hit' ); |
| 262 |
$this->assertSame( 'from-cache', $mock->get( 'known' ), 'Process cache hit' ); |
| 263 |
} |
| 264 |
|
| 265 |
public function testLoadCacheExpiredLockWarning() { |
| 266 |
|
| 267 |
$cache = $this->getMockBuilder( HashBagOStuff::class ) |
| 268 |
->onlyMethods( [ 'get', 'lock' ] ) |
| 269 |
->getMock(); |
| 270 |
$cache->expects( $this->once() )->method( 'get' ) |
| 271 |
|
| 272 |
->willReturn( [ |
| 273 |
'config' => [ 'known' => 'from-cache-expired' ], |
| 274 |
'expires' => -INF, |
| 275 |
'modifiedIndex' => 0, |
| 276 |
] ); |
| 277 |
|
| 278 |
$cache->expects( $this->once() )->method( 'lock' ) |
| 279 |
->willReturn( false ); |
| 280 |
|
| 281 |
|
| 282 |
$mock = $this->createConfigMock( [ |
| 283 |
'cache' => $cache, |
| 284 |
] ); |
| 285 |
$mock->expects( $this->never() )->method( 'fetchAllFromEtcd' ); |
| 286 |
|
| 287 |
$this->expectPHPError( |
| 288 |
E_USER_NOTICE, |
| 289 |
static function () use ( $mock ) { |
| 290 |
$mock->get( 'known' ); |
| 291 |
}, |
| 292 |
'using stale data: lost lock' |
| 293 |
); |
| 294 |
} |
| 295 |
|
| 296 |
public static function provideFetchFromServer() { |
| 297 |
return [ |
| 298 |
'200 OK - Success' => [ |
| 299 |
'http' => [ |
| 300 |
'code' => 200, |
| 301 |
'reason' => 'OK', |
| 302 |
'headers' => [], |
| 303 |
'body' => json_encode( [ 'node' => [ 'nodes' => [ |
| 304 |
[ |
| 305 |
'key' => '/example/foo', |
| 306 |
'value' => json_encode( [ 'val' => true ] ), |
| 307 |
'modifiedIndex' => 123 |
| 308 |
], |
| 309 |
] ] ] ), |
| 310 |
'error' => '', |
| 311 |
], |
| 312 |
'expect' => self::createEtcdResponse( [ |
| 313 |
'config' => [ 'foo' => true ], |
| 314 |
'modifiedIndex' => 123 |
| 315 |
] ), |
| 316 |
], |
| 317 |
'200 OK - Empty dir' => [ |
| 318 |
'http' => [ |
| 319 |
'code' => 200, |
| 320 |
'reason' => 'OK', |
| 321 |
'headers' => [], |
| 322 |
'body' => json_encode( [ 'node' => [ 'nodes' => [ |
| 323 |
[ |
| 324 |
'key' => '/example/foo', |
| 325 |
'value' => json_encode( [ 'val' => true ] ), |
| 326 |
'modifiedIndex' => 123 |
| 327 |
], |
| 328 |
[ |
| 329 |
'key' => '/example/sub', |
| 330 |
'dir' => true, |
| 331 |
'modifiedIndex' => 234, |
| 332 |
'nodes' => [], |
| 333 |
], |
| 334 |
[ |
| 335 |
'key' => '/example/bar', |
| 336 |
'value' => json_encode( [ 'val' => false ] ), |
| 337 |
'modifiedIndex' => 125 |
| 338 |
], |
| 339 |
] ] ] ), |
| 340 |
'error' => '', |
| 341 |
], |
| 342 |
'expect' => self::createEtcdResponse( [ |
| 343 |
'config' => [ 'foo' => true, 'bar' => false ], |
| 344 |
'modifiedIndex' => 125 |
| 345 |
] ), |
| 346 |
], |
| 347 |
'200 OK - Recursive' => [ |
| 348 |
'http' => [ |
| 349 |
'code' => 200, |
| 350 |
'reason' => 'OK', |
| 351 |
'headers' => [], |
| 352 |
'body' => json_encode( [ 'node' => [ 'nodes' => [ |
| 353 |
[ |
| 354 |
'key' => '/example/a', |
| 355 |
'dir' => true, |
| 356 |
'modifiedIndex' => 124, |
| 357 |
'nodes' => [ |
| 358 |
[ |
| 359 |
'key' => 'b', |
| 360 |
'value' => json_encode( [ 'val' => true ] ), |
| 361 |
'modifiedIndex' => 123, |
| 362 |
|
| 363 |
], |
| 364 |
[ |
| 365 |
'key' => 'c', |
| 366 |
'value' => json_encode( [ 'val' => false ] ), |
| 367 |
'modifiedIndex' => 123, |
| 368 |
], |
| 369 |
], |
| 370 |
], |
| 371 |
] ] ] ), |
| 372 |
'error' => '', |
| 373 |
], |
| 374 |
'expect' => self::createEtcdResponse( [ |
| 375 |
'config' => [ 'a/b' => true, 'a/c' => false ], |
| 376 |
'modifiedIndex' => 123 |
| 377 |
] ), |
| 378 |
], |
| 379 |
'200 OK - Missing nodes at second level' => [ |
| 380 |
'http' => [ |
| 381 |
'code' => 200, |
| 382 |
'reason' => 'OK', |
| 383 |
'headers' => [], |
| 384 |
'body' => json_encode( [ 'node' => [ 'nodes' => [ |
| 385 |
[ |
| 386 |
'key' => '/example/a', |
| 387 |
'dir' => true, |
| 388 |
'modifiedIndex' => 0, |
| 389 |
], |
| 390 |
] ] ] ), |
| 391 |
'error' => '', |
| 392 |
], |
| 393 |
'expect' => self::createEtcdResponse( [ |
| 394 |
'error' => "Unexpected JSON response in dir 'a'; missing 'nodes' list.", |
| 395 |
] ), |
| 396 |
], |
| 397 |
'200 OK - Directory with non-array "nodes" key' => [ |
| 398 |
'http' => [ |
| 399 |
'code' => 200, |
| 400 |
'reason' => 'OK', |
| 401 |
'headers' => [], |
| 402 |
'body' => json_encode( [ 'node' => [ 'nodes' => [ |
| 403 |
[ |
| 404 |
'key' => '/example/a', |
| 405 |
'dir' => true, |
| 406 |
'nodes' => 'not an array' |
| 407 |
], |
| 408 |
] ] ] ), |
| 409 |
'error' => '', |
| 410 |
], |
| 411 |
'expect' => self::createEtcdResponse( [ |
| 412 |
'error' => "Unexpected JSON response in dir 'a'; 'nodes' is not an array.", |
| 413 |
] ), |
| 414 |
], |
| 415 |
'200 OK - Correctly encoded garbage response' => [ |
| 416 |
'http' => [ |
| 417 |
'code' => 200, |
| 418 |
'reason' => 'OK', |
| 419 |
'headers' => [], |
| 420 |
'body' => json_encode( [ 'foo' => 'bar' ] ), |
| 421 |
'error' => '', |
| 422 |
], |
| 423 |
'expect' => self::createEtcdResponse( [ |
| 424 |
'error' => "Unexpected JSON response: Missing or invalid node at top level.", |
| 425 |
] ), |
| 426 |
], |
| 427 |
'200 OK - Bad value' => [ |
| 428 |
'http' => [ |
| 429 |
'code' => 200, |
| 430 |
'reason' => 'OK', |
| 431 |
'headers' => [], |
| 432 |
'body' => json_encode( [ 'node' => [ 'nodes' => [ |
| 433 |
[ |
| 434 |
'key' => '/example/foo', |
| 435 |
'value' => ';"broken{value', |
| 436 |
'modifiedIndex' => 123, |
| 437 |
] |
| 438 |
] ] ] ), |
| 439 |
'error' => '', |
| 440 |
], |
| 441 |
'expect' => self::createEtcdResponse( [ |
| 442 |
'error' => "Failed to parse value for 'foo'.", |
| 443 |
] ), |
| 444 |
], |
| 445 |
'200 OK - Empty node list' => [ |
| 446 |
'http' => [ |
| 447 |
'code' => 200, |
| 448 |
'reason' => 'OK', |
| 449 |
'headers' => [], |
| 450 |
'body' => '{"node":{"nodes":[], "modifiedIndex": 12 }}', |
| 451 |
'error' => '', |
| 452 |
], |
| 453 |
'expect' => self::createEtcdResponse( [ |
| 454 |
'config' => [], |
| 455 |
] ), |
| 456 |
], |
| 457 |
'200 OK - Invalid JSON' => [ |
| 458 |
'http' => [ |
| 459 |
'code' => 200, |
| 460 |
'reason' => 'OK', |
| 461 |
'headers' => [ 'content-length' => 0 ], |
| 462 |
'body' => '', |
| 463 |
'error' => '(curl error: no status set)', |
| 464 |
], |
| 465 |
'expect' => self::createEtcdResponse( [ |
| 466 |
'error' => "Error unserializing JSON response.", |
| 467 |
] ), |
| 468 |
], |
| 469 |
'404 Not Found' => [ |
| 470 |
'http' => [ |
| 471 |
'code' => 404, |
| 472 |
'reason' => 'Not Found', |
| 473 |
'headers' => [ 'content-length' => 0 ], |
| 474 |
'body' => '', |
| 475 |
'error' => '', |
| 476 |
], |
| 477 |
'expect' => self::createEtcdResponse( [ |
| 478 |
'error' => 'HTTP 404 (Not Found)', |
| 479 |
] ), |
| 480 |
], |
| 481 |
'400 Bad Request - custom error' => [ |
| 482 |
'http' => [ |
| 483 |
'code' => 400, |
| 484 |
'reason' => 'Bad Request', |
| 485 |
'headers' => [ 'content-length' => 0 ], |
| 486 |
'body' => '', |
| 487 |
'error' => 'No good reason', |
| 488 |
], |
| 489 |
'expect' => self::createEtcdResponse( [ |
| 490 |
'error' => 'No good reason', |
| 491 |
'retry' => true, |
| 492 |
] ), |
| 493 |
], |
| 494 |
]; |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
public function testFetchFromServer( array $httpResponse, array $expected ) { |
| 502 |
$http = $this->createMock( MultiHttpClient::class ); |
| 503 |
$http->expects( $this->once() )->method( 'run' ) |
| 504 |
->willReturn( array_values( $httpResponse ) ); |
| 505 |
|
| 506 |
$conf = $this->createMock( EtcdConfig::class ); |
| 507 |
|
| 508 |
$conf = TestingAccessWrapper::newFromObject( $conf ); |
| 509 |
$conf->http = $http; |
| 510 |
|
| 511 |
$this->assertSame( |
| 512 |
$expected, |
| 513 |
$conf->fetchAllFromEtcdServer( 'etcd-tcp.example.net' ) |
| 514 |
); |
| 515 |
} |
| 516 |
|
| 517 |
public function testFetchFromServerWithoutPort() { |
| 518 |
$conf = $this->createMock( EtcdConfig::class ); |
| 519 |
|
| 520 |
$http = $this->createMock( MultiHttpClient::class ); |
| 521 |
|
| 522 |
$conf = TestingAccessWrapper::newFromObject( $conf ); |
| 523 |
$conf->protocol = 'https'; |
| 524 |
$conf->http = $http; |
| 525 |
|
| 526 |
$http |
| 527 |
->expects( $this->once() ) |
| 528 |
->method( 'run' ) |
| 529 |
->with( |
| 530 |
$this->logicalAnd( |
| 531 |
$this->arrayHasKey( 'url' ), |
| 532 |
$this->callback( function ( $request ) { |
| 533 |
$this->assertStringStartsWith( |
| 534 |
'https://etcd.example/', |
| 535 |
$request['url'] |
| 536 |
); |
| 537 |
return true; |
| 538 |
} ) |
| 539 |
) |
| 540 |
)->willReturn( [ 200, '', [], '', '' ] ); |
| 541 |
|
| 542 |
$conf->fetchAllFromEtcdServer( 'etcd.example' ); |
| 543 |
} |
| 544 |
|
| 545 |
public function testFetchFromServerWithPort() { |
| 546 |
$conf = $this->createMock( EtcdConfig::class ); |
| 547 |
|
| 548 |
$http = $this->createMock( MultiHttpClient::class ); |
| 549 |
|
| 550 |
$conf = TestingAccessWrapper::newFromObject( $conf ); |
| 551 |
$conf->protocol = 'https'; |
| 552 |
$conf->http = $http; |
| 553 |
|
| 554 |
$http |
| 555 |
->expects( $this->once() ) |
| 556 |
->method( 'run' ) |
| 557 |
->with( |
| 558 |
$this->logicalAnd( |
| 559 |
$this->arrayHasKey( 'url' ), |
| 560 |
$this->callback( function ( $request ) { |
| 561 |
$this->assertStringStartsWith( |
| 562 |
'https://etcd.example:4001/', |
| 563 |
$request['url'] |
| 564 |
); |
| 565 |
return true; |
| 566 |
} ) |
| 567 |
) |
| 568 |
)->willReturn( [ 200, '', [], '', '' ] ); |
| 569 |
|
| 570 |
$conf->fetchAllFromEtcdServer( 'etcd.example', 4001 ); |
| 571 |
} |
| 572 |
|
| 573 |
public function testServiceDiscovery() { |
| 574 |
$conf = $this->createConfigMock( |
| 575 |
[ 'host' => 'an.example' ], |
| 576 |
[ 'fetchAllFromEtcdServer' ] |
| 577 |
); |
| 578 |
$conf = TestingAccessWrapper::newFromObject( $conf ); |
| 579 |
|
| 580 |
$conf->dsd = TestingAccessWrapper::newFromObject( $conf->dsd ); |
| 581 |
$conf->dsd->resolver = $this->createCallableMock(); |
| 582 |
$conf->dsd->resolver |
| 583 |
->expects( $this->once() ) |
| 584 |
->method( '__invoke' ) |
| 585 |
->with( '_etcd._tcp.an.example' ) |
| 586 |
->willReturn( [ |
| 587 |
[ |
| 588 |
'target' => 'etcd-target.an.example', |
| 589 |
'port' => '2379', |
| 590 |
'pri' => '1', |
| 591 |
'weight' => '1', |
| 592 |
], |
| 593 |
] ); |
| 594 |
|
| 595 |
$conf->expects( $this->once() ) |
| 596 |
->method( 'fetchAllFromEtcdServer' ) |
| 597 |
->with( 'etcd-target.an.example', 2379 ) |
| 598 |
->willReturn( self::createEtcdResponse( [ 'foo' => true ] ) ); |
| 599 |
|
| 600 |
$conf->fetchAllFromEtcd(); |
| 601 |
} |
| 602 |
|
| 603 |
public function testServiceDiscoverySrvRecordAsHost() { |
| 604 |
$conf = $this->createConfigMock( |
| 605 |
[ 'host' => '_etcd-client-ssl._tcp.an.example' ], |
| 606 |
[ 'fetchAllFromEtcdServer' ] |
| 607 |
); |
| 608 |
$conf = TestingAccessWrapper::newFromObject( $conf ); |
| 609 |
|
| 610 |
$conf->dsd = TestingAccessWrapper::newFromObject( $conf->dsd ); |
| 611 |
$conf->dsd->resolver = $this->createCallableMock(); |
| 612 |
$conf->dsd->resolver |
| 613 |
->expects( $this->once() ) |
| 614 |
->method( '__invoke' ) |
| 615 |
->with( '_etcd-client-ssl._tcp.an.example' ) |
| 616 |
->willReturn( [ |
| 617 |
[ |
| 618 |
'target' => 'etcd-target.an.example', |
| 619 |
'port' => '2379', |
| 620 |
'pri' => '1', |
| 621 |
'weight' => '1', |
| 622 |
], |
| 623 |
] ); |
| 624 |
|
| 625 |
$conf->expects( $this->once() ) |
| 626 |
->method( 'fetchAllFromEtcdServer' ) |
| 627 |
->with( 'etcd-target.an.example', 2379 ) |
| 628 |
->willReturn( self::createEtcdResponse( [ 'foo' => true ] ) ); |
| 629 |
|
| 630 |
$conf->fetchAllFromEtcd(); |
| 631 |
} |
| 632 |
} |