wiki.techinc.nl/tests/phpunit/unit/includes/libs/DnsSrvDiscovererTest.php

113 lines
2.8 KiB
PHP
Raw Normal View History

<?php
/**
* @covers DnsSrvDiscoverer
*/
class DnsSrvDiscovererTest extends PHPUnit\Framework\TestCase {
use MediaWikiCoversValidator;
Simplify DnsSrvDiscoverer interface Refactor DnsSrvDiscoverer constructor to accept service, protocol, and domain as discrete arguments to better match the specifications outlined in RFC 2782 and make clearer the concerns and contract between the discoverer and the caller. The latter should not need to know about discovery details but only the service to be discovered and the scope of that discovery (the domain). Domain is now optional where an omitted value results in discovery relative to the host's configured search domain, supporting installations where full DNS based discovery is desired over explicit configuration. This makes the simplest use case: $servers = ( new DnsSrvDiscoverer( 'service' ) )->getServers(); Overall use of the interface is simplified by changing `getServers()` to unconditionally sort results by priority according to RFC 2872 and return a simplified result of host/port tuples (the only data relevant in primary discovery use cases). This makes the handling of discovery fallback simpler since the caller can construct a default without having to populate all response record details. Example $dsd = new DnsSrvDiscoverer( $service, $proto, $host ); $servers = $dsd->getServers() ?: [ [ $host, $defaultPort ] ]; A `getRecords()` method is provided as public to allow a caller to obtain details on the response (and do its own sorting) should that be needed. Added an optional `$resolver` argument to the constructor (which defaults to `dns_get_record`) to make the class implementation more testable, and wrote functional tests. EtcdConfig has been refactored to reflect the new usage of DnsSrvDiscoverer and now sets the discoverer as a property in the constructor to allow for mocking in tests. Implemented tests for etcd DNS SRV discovery within EtcdConfig. Bug: T296771 Change-Id: Idbd60049853439f96ff6045e01aa03014b4e587f
2021-12-08 00:04:56 +00:00
public function testGetSrvName() {
$dsd = new DnsSrvDiscoverer( 'etcd', 'tcp', 'an.example' );
$this->assertSame( '_etcd._tcp.an.example', $dsd->getSrvName() );
}
Simplify DnsSrvDiscoverer interface Refactor DnsSrvDiscoverer constructor to accept service, protocol, and domain as discrete arguments to better match the specifications outlined in RFC 2782 and make clearer the concerns and contract between the discoverer and the caller. The latter should not need to know about discovery details but only the service to be discovered and the scope of that discovery (the domain). Domain is now optional where an omitted value results in discovery relative to the host's configured search domain, supporting installations where full DNS based discovery is desired over explicit configuration. This makes the simplest use case: $servers = ( new DnsSrvDiscoverer( 'service' ) )->getServers(); Overall use of the interface is simplified by changing `getServers()` to unconditionally sort results by priority according to RFC 2872 and return a simplified result of host/port tuples (the only data relevant in primary discovery use cases). This makes the handling of discovery fallback simpler since the caller can construct a default without having to populate all response record details. Example $dsd = new DnsSrvDiscoverer( $service, $proto, $host ); $servers = $dsd->getServers() ?: [ [ $host, $defaultPort ] ]; A `getRecords()` method is provided as public to allow a caller to obtain details on the response (and do its own sorting) should that be needed. Added an optional `$resolver` argument to the constructor (which defaults to `dns_get_record`) to make the class implementation more testable, and wrote functional tests. EtcdConfig has been refactored to reflect the new usage of DnsSrvDiscoverer and now sets the discoverer as a property in the constructor to allow for mocking in tests. Implemented tests for etcd DNS SRV discovery within EtcdConfig. Bug: T296771 Change-Id: Idbd60049853439f96ff6045e01aa03014b4e587f
2021-12-08 00:04:56 +00:00
public function testGetSrvNameWithoutDomain() {
$dsd = new DnsSrvDiscoverer( 'etcd', 'tcp' );
$this->assertSame( '_etcd._tcp', $dsd->getSrvName() );
}
Simplify DnsSrvDiscoverer interface Refactor DnsSrvDiscoverer constructor to accept service, protocol, and domain as discrete arguments to better match the specifications outlined in RFC 2782 and make clearer the concerns and contract between the discoverer and the caller. The latter should not need to know about discovery details but only the service to be discovered and the scope of that discovery (the domain). Domain is now optional where an omitted value results in discovery relative to the host's configured search domain, supporting installations where full DNS based discovery is desired over explicit configuration. This makes the simplest use case: $servers = ( new DnsSrvDiscoverer( 'service' ) )->getServers(); Overall use of the interface is simplified by changing `getServers()` to unconditionally sort results by priority according to RFC 2872 and return a simplified result of host/port tuples (the only data relevant in primary discovery use cases). This makes the handling of discovery fallback simpler since the caller can construct a default without having to populate all response record details. Example $dsd = new DnsSrvDiscoverer( $service, $proto, $host ); $servers = $dsd->getServers() ?: [ [ $host, $defaultPort ] ]; A `getRecords()` method is provided as public to allow a caller to obtain details on the response (and do its own sorting) should that be needed. Added an optional `$resolver` argument to the constructor (which defaults to `dns_get_record`) to make the class implementation more testable, and wrote functional tests. EtcdConfig has been refactored to reflect the new usage of DnsSrvDiscoverer and now sets the discoverer as a property in the constructor to allow for mocking in tests. Implemented tests for etcd DNS SRV discovery within EtcdConfig. Bug: T296771 Change-Id: Idbd60049853439f96ff6045e01aa03014b4e587f
2021-12-08 00:04:56 +00:00
public function testGetRecords() {
$resolver = $this->mockResolver();
Simplify DnsSrvDiscoverer interface Refactor DnsSrvDiscoverer constructor to accept service, protocol, and domain as discrete arguments to better match the specifications outlined in RFC 2782 and make clearer the concerns and contract between the discoverer and the caller. The latter should not need to know about discovery details but only the service to be discovered and the scope of that discovery (the domain). Domain is now optional where an omitted value results in discovery relative to the host's configured search domain, supporting installations where full DNS based discovery is desired over explicit configuration. This makes the simplest use case: $servers = ( new DnsSrvDiscoverer( 'service' ) )->getServers(); Overall use of the interface is simplified by changing `getServers()` to unconditionally sort results by priority according to RFC 2872 and return a simplified result of host/port tuples (the only data relevant in primary discovery use cases). This makes the handling of discovery fallback simpler since the caller can construct a default without having to populate all response record details. Example $dsd = new DnsSrvDiscoverer( $service, $proto, $host ); $servers = $dsd->getServers() ?: [ [ $host, $defaultPort ] ]; A `getRecords()` method is provided as public to allow a caller to obtain details on the response (and do its own sorting) should that be needed. Added an optional `$resolver` argument to the constructor (which defaults to `dns_get_record`) to make the class implementation more testable, and wrote functional tests. EtcdConfig has been refactored to reflect the new usage of DnsSrvDiscoverer and now sets the discoverer as a property in the constructor to allow for mocking in tests. Implemented tests for etcd DNS SRV discovery within EtcdConfig. Bug: T296771 Change-Id: Idbd60049853439f96ff6045e01aa03014b4e587f
2021-12-08 00:04:56 +00:00
$dsd = new DnsSrvDiscoverer( 'etcd', 'tcp', 'an.example', $resolver );
Simplify DnsSrvDiscoverer interface Refactor DnsSrvDiscoverer constructor to accept service, protocol, and domain as discrete arguments to better match the specifications outlined in RFC 2782 and make clearer the concerns and contract between the discoverer and the caller. The latter should not need to know about discovery details but only the service to be discovered and the scope of that discovery (the domain). Domain is now optional where an omitted value results in discovery relative to the host's configured search domain, supporting installations where full DNS based discovery is desired over explicit configuration. This makes the simplest use case: $servers = ( new DnsSrvDiscoverer( 'service' ) )->getServers(); Overall use of the interface is simplified by changing `getServers()` to unconditionally sort results by priority according to RFC 2872 and return a simplified result of host/port tuples (the only data relevant in primary discovery use cases). This makes the handling of discovery fallback simpler since the caller can construct a default without having to populate all response record details. Example $dsd = new DnsSrvDiscoverer( $service, $proto, $host ); $servers = $dsd->getServers() ?: [ [ $host, $defaultPort ] ]; A `getRecords()` method is provided as public to allow a caller to obtain details on the response (and do its own sorting) should that be needed. Added an optional `$resolver` argument to the constructor (which defaults to `dns_get_record`) to make the class implementation more testable, and wrote functional tests. EtcdConfig has been refactored to reflect the new usage of DnsSrvDiscoverer and now sets the discoverer as a property in the constructor to allow for mocking in tests. Implemented tests for etcd DNS SRV discovery within EtcdConfig. Bug: T296771 Change-Id: Idbd60049853439f96ff6045e01aa03014b4e587f
2021-12-08 00:04:56 +00:00
$resolver
->expects( $this->once() )
->method( '__invoke' )
->with( '_etcd._tcp.an.example' )
->willReturn( [
[ 'target' => 'foo', 'port' => '123', 'pri' => '1', 'weight' => '1' ],
[ 'target' => 'qux', 'port' => '322', 'pri' => '2', 'weight' => '2' ],
[ 'target' => 'bar', 'port' => '124', 'pri' => '1', 'weight' => '2' ],
[ 'target' => 'baz', 'port' => '321', 'pri' => '2', 'weight' => '1' ],
] );
$this->assertSame(
[
Simplify DnsSrvDiscoverer interface Refactor DnsSrvDiscoverer constructor to accept service, protocol, and domain as discrete arguments to better match the specifications outlined in RFC 2782 and make clearer the concerns and contract between the discoverer and the caller. The latter should not need to know about discovery details but only the service to be discovered and the scope of that discovery (the domain). Domain is now optional where an omitted value results in discovery relative to the host's configured search domain, supporting installations where full DNS based discovery is desired over explicit configuration. This makes the simplest use case: $servers = ( new DnsSrvDiscoverer( 'service' ) )->getServers(); Overall use of the interface is simplified by changing `getServers()` to unconditionally sort results by priority according to RFC 2872 and return a simplified result of host/port tuples (the only data relevant in primary discovery use cases). This makes the handling of discovery fallback simpler since the caller can construct a default without having to populate all response record details. Example $dsd = new DnsSrvDiscoverer( $service, $proto, $host ); $servers = $dsd->getServers() ?: [ [ $host, $defaultPort ] ]; A `getRecords()` method is provided as public to allow a caller to obtain details on the response (and do its own sorting) should that be needed. Added an optional `$resolver` argument to the constructor (which defaults to `dns_get_record`) to make the class implementation more testable, and wrote functional tests. EtcdConfig has been refactored to reflect the new usage of DnsSrvDiscoverer and now sets the discoverer as a property in the constructor to allow for mocking in tests. Implemented tests for etcd DNS SRV discovery within EtcdConfig. Bug: T296771 Change-Id: Idbd60049853439f96ff6045e01aa03014b4e587f
2021-12-08 00:04:56 +00:00
[ 'target' => 'foo', 'port' => 123, 'pri' => 1, 'weight' => 1 ],
[ 'target' => 'qux', 'port' => 322, 'pri' => 2, 'weight' => 2 ],
[ 'target' => 'bar', 'port' => 124, 'pri' => 1, 'weight' => 2 ],
[ 'target' => 'baz', 'port' => 321, 'pri' => 2, 'weight' => 1 ],
],
Simplify DnsSrvDiscoverer interface Refactor DnsSrvDiscoverer constructor to accept service, protocol, and domain as discrete arguments to better match the specifications outlined in RFC 2782 and make clearer the concerns and contract between the discoverer and the caller. The latter should not need to know about discovery details but only the service to be discovered and the scope of that discovery (the domain). Domain is now optional where an omitted value results in discovery relative to the host's configured search domain, supporting installations where full DNS based discovery is desired over explicit configuration. This makes the simplest use case: $servers = ( new DnsSrvDiscoverer( 'service' ) )->getServers(); Overall use of the interface is simplified by changing `getServers()` to unconditionally sort results by priority according to RFC 2872 and return a simplified result of host/port tuples (the only data relevant in primary discovery use cases). This makes the handling of discovery fallback simpler since the caller can construct a default without having to populate all response record details. Example $dsd = new DnsSrvDiscoverer( $service, $proto, $host ); $servers = $dsd->getServers() ?: [ [ $host, $defaultPort ] ]; A `getRecords()` method is provided as public to allow a caller to obtain details on the response (and do its own sorting) should that be needed. Added an optional `$resolver` argument to the constructor (which defaults to `dns_get_record`) to make the class implementation more testable, and wrote functional tests. EtcdConfig has been refactored to reflect the new usage of DnsSrvDiscoverer and now sets the discoverer as a property in the constructor to allow for mocking in tests. Implemented tests for etcd DNS SRV discovery within EtcdConfig. Bug: T296771 Change-Id: Idbd60049853439f96ff6045e01aa03014b4e587f
2021-12-08 00:04:56 +00:00
$dsd->getRecords()
);
}
Simplify DnsSrvDiscoverer interface Refactor DnsSrvDiscoverer constructor to accept service, protocol, and domain as discrete arguments to better match the specifications outlined in RFC 2782 and make clearer the concerns and contract between the discoverer and the caller. The latter should not need to know about discovery details but only the service to be discovered and the scope of that discovery (the domain). Domain is now optional where an omitted value results in discovery relative to the host's configured search domain, supporting installations where full DNS based discovery is desired over explicit configuration. This makes the simplest use case: $servers = ( new DnsSrvDiscoverer( 'service' ) )->getServers(); Overall use of the interface is simplified by changing `getServers()` to unconditionally sort results by priority according to RFC 2872 and return a simplified result of host/port tuples (the only data relevant in primary discovery use cases). This makes the handling of discovery fallback simpler since the caller can construct a default without having to populate all response record details. Example $dsd = new DnsSrvDiscoverer( $service, $proto, $host ); $servers = $dsd->getServers() ?: [ [ $host, $defaultPort ] ]; A `getRecords()` method is provided as public to allow a caller to obtain details on the response (and do its own sorting) should that be needed. Added an optional `$resolver` argument to the constructor (which defaults to `dns_get_record`) to make the class implementation more testable, and wrote functional tests. EtcdConfig has been refactored to reflect the new usage of DnsSrvDiscoverer and now sets the discoverer as a property in the constructor to allow for mocking in tests. Implemented tests for etcd DNS SRV discovery within EtcdConfig. Bug: T296771 Change-Id: Idbd60049853439f96ff6045e01aa03014b4e587f
2021-12-08 00:04:56 +00:00
public function testGetServers() {
$resolver = $this->mockResolver();
$dsd = new DnsSrvDiscoverer( 'etcd', 'tcp', 'an.example', $resolver );
$resolver
->expects( $this->once() )
->method( '__invoke' )
->with( '_etcd._tcp.an.example' )
->willReturn( [
[ 'target' => 'foo', 'port' => '123', 'pri' => '1', 'weight' => '1' ],
[ 'target' => 'qux', 'port' => '322', 'pri' => '2', 'weight' => '2' ],
[ 'target' => 'bar', 'port' => '124', 'pri' => '1', 'weight' => '2' ],
[ 'target' => 'baz', 'port' => '321', 'pri' => '2', 'weight' => '1' ],
] );
$servers = $dsd->getServers();
$prio1 = array_slice( $servers, 0, 2 );
$prio2 = array_slice( $servers, 2, 2 );
$this->assertContains(
[ 'foo', 123 ],
$prio1
);
$this->assertContains(
[ 'bar', 124 ],
$prio1
);
$this->assertContains(
[ 'baz', 321 ],
$prio2
);
Simplify DnsSrvDiscoverer interface Refactor DnsSrvDiscoverer constructor to accept service, protocol, and domain as discrete arguments to better match the specifications outlined in RFC 2782 and make clearer the concerns and contract between the discoverer and the caller. The latter should not need to know about discovery details but only the service to be discovered and the scope of that discovery (the domain). Domain is now optional where an omitted value results in discovery relative to the host's configured search domain, supporting installations where full DNS based discovery is desired over explicit configuration. This makes the simplest use case: $servers = ( new DnsSrvDiscoverer( 'service' ) )->getServers(); Overall use of the interface is simplified by changing `getServers()` to unconditionally sort results by priority according to RFC 2872 and return a simplified result of host/port tuples (the only data relevant in primary discovery use cases). This makes the handling of discovery fallback simpler since the caller can construct a default without having to populate all response record details. Example $dsd = new DnsSrvDiscoverer( $service, $proto, $host ); $servers = $dsd->getServers() ?: [ [ $host, $defaultPort ] ]; A `getRecords()` method is provided as public to allow a caller to obtain details on the response (and do its own sorting) should that be needed. Added an optional `$resolver` argument to the constructor (which defaults to `dns_get_record`) to make the class implementation more testable, and wrote functional tests. EtcdConfig has been refactored to reflect the new usage of DnsSrvDiscoverer and now sets the discoverer as a property in the constructor to allow for mocking in tests. Implemented tests for etcd DNS SRV discovery within EtcdConfig. Bug: T296771 Change-Id: Idbd60049853439f96ff6045e01aa03014b4e587f
2021-12-08 00:04:56 +00:00
$this->assertContains(
[ 'qux', 322 ],
$prio2
);
}
Simplify DnsSrvDiscoverer interface Refactor DnsSrvDiscoverer constructor to accept service, protocol, and domain as discrete arguments to better match the specifications outlined in RFC 2782 and make clearer the concerns and contract between the discoverer and the caller. The latter should not need to know about discovery details but only the service to be discovered and the scope of that discovery (the domain). Domain is now optional where an omitted value results in discovery relative to the host's configured search domain, supporting installations where full DNS based discovery is desired over explicit configuration. This makes the simplest use case: $servers = ( new DnsSrvDiscoverer( 'service' ) )->getServers(); Overall use of the interface is simplified by changing `getServers()` to unconditionally sort results by priority according to RFC 2872 and return a simplified result of host/port tuples (the only data relevant in primary discovery use cases). This makes the handling of discovery fallback simpler since the caller can construct a default without having to populate all response record details. Example $dsd = new DnsSrvDiscoverer( $service, $proto, $host ); $servers = $dsd->getServers() ?: [ [ $host, $defaultPort ] ]; A `getRecords()` method is provided as public to allow a caller to obtain details on the response (and do its own sorting) should that be needed. Added an optional `$resolver` argument to the constructor (which defaults to `dns_get_record`) to make the class implementation more testable, and wrote functional tests. EtcdConfig has been refactored to reflect the new usage of DnsSrvDiscoverer and now sets the discoverer as a property in the constructor to allow for mocking in tests. Implemented tests for etcd DNS SRV discovery within EtcdConfig. Bug: T296771 Change-Id: Idbd60049853439f96ff6045e01aa03014b4e587f
2021-12-08 00:04:56 +00:00
public function testGetServersNoDiscoveryResultsRfc2782() {
$resolver = $this->mockResolver();
$dsd = new DnsSrvDiscoverer( 'etcd', 'tcp', 'an.example', $resolver );
$resolver
->expects( $this->once() )
->method( '__invoke' )
->with( '_etcd._tcp.an.example' )
->willReturn( [
[ 'target' => '.', 'port' => '1', 'pri' => '1', 'weight' => '1' ],
] );
$this->assertSame( [], $dsd->getServers() );
Simplify DnsSrvDiscoverer interface Refactor DnsSrvDiscoverer constructor to accept service, protocol, and domain as discrete arguments to better match the specifications outlined in RFC 2782 and make clearer the concerns and contract between the discoverer and the caller. The latter should not need to know about discovery details but only the service to be discovered and the scope of that discovery (the domain). Domain is now optional where an omitted value results in discovery relative to the host's configured search domain, supporting installations where full DNS based discovery is desired over explicit configuration. This makes the simplest use case: $servers = ( new DnsSrvDiscoverer( 'service' ) )->getServers(); Overall use of the interface is simplified by changing `getServers()` to unconditionally sort results by priority according to RFC 2872 and return a simplified result of host/port tuples (the only data relevant in primary discovery use cases). This makes the handling of discovery fallback simpler since the caller can construct a default without having to populate all response record details. Example $dsd = new DnsSrvDiscoverer( $service, $proto, $host ); $servers = $dsd->getServers() ?: [ [ $host, $defaultPort ] ]; A `getRecords()` method is provided as public to allow a caller to obtain details on the response (and do its own sorting) should that be needed. Added an optional `$resolver` argument to the constructor (which defaults to `dns_get_record`) to make the class implementation more testable, and wrote functional tests. EtcdConfig has been refactored to reflect the new usage of DnsSrvDiscoverer and now sets the discoverer as a property in the constructor to allow for mocking in tests. Implemented tests for etcd DNS SRV discovery within EtcdConfig. Bug: T296771 Change-Id: Idbd60049853439f96ff6045e01aa03014b4e587f
2021-12-08 00:04:56 +00:00
}
private function mockResolver() {
return $this
->getMockBuilder( \stdClass::class )
->addMethods( [ '__invoke' ] )
->getMock();
}
}