mirror of
https://github.com/Vateron-Media/XC_VM.git
synced 2026-10-31 19:03:43 +01:00
The suite ran on in-memory SQLite, an engine production never uses: it hid column lengths, MySQL-only syntax and type coercion (the identity bug fixed in the previous commit passed CI that way). SQLite is gone: - TestDb connects only to MariaDB/MySQL (XCVM_TEST_DB_DSN/_USER/_PASS; root over the local socket, else 127.0.0.1:3306). Each instance gets a throwaway schema, dropped with it, in the installer's sql_mode, with an explicit connect timeout (a boot under test can zero default_socket_timeout). - InstallSchema and the tests take the production DDL as is: the SQLite reducers, the keys they re-added, the faked information_schema and the sqliteCreateFunction() stand-ins are removed; test DDL is MySQL DDL. - ReplicaBootTest, DbConnectRefusalTest and LazyDatabaseHandlerTest reach the test server instead of SQLite files; RecordingFinalizer no longer swallows an information_schema failure "for tests on SQLite". - `make test-db` starts a throwaway mariadb:11.4 (tmpfs) for a machine without a server; the CI PHPUnit job gets the same as a service. The suite also runs on a panel host with the bundled PHP and its own php.ini: tests reach the code through MAIN_HOME, not `<repo>/src`; repository checks and process-isolated tests (ionCube Loader + OPcache CLI segfault on a script read from stdin) are in the skip-on-panel group; xcvm_test_child_php() starts a child that fakes \XC_VM without the real extension; Redis-down tests use RedisManager::useConnector() instead of an eval'd \XC_VM. Stale opt-in tests fixed on the way: the real-agent provisioning test answers the agent-started check, the extension integration test sends the typed starts and reads the binary signature raw, a fake producer starts as PHP_BINARY, and the certbot test waits for the line its background command writes.
61 lines
3.7 KiB
PHP
61 lines
3.7 KiB
PHP
<?php
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use XcVm\Core\Cache\FileCache;
|
|
use XcVm\Domain\Server\ServerRepository;
|
|
use XcVm\Infrastructure\Database\DatabaseFactory;
|
|
|
|
/**
|
|
* verify_host's list (`allowed_domains`, read by HostVerificationStage and
|
|
* the streaming bootstrap): the enabled servers' names and addresses and the
|
|
* active resellers' DNS. Nothing had written it since the move off
|
|
* CoreUtilities, so every host passed; cron:cache writes it again.
|
|
*/
|
|
final class AllowedDomainsTest extends TestCase {
|
|
private string $rDir;
|
|
|
|
protected function setUp(): void {
|
|
$this->rDir = sys_get_temp_dir() . '/xcvm-domains-' . bin2hex(random_bytes(4)) . '/';
|
|
mkdir($this->rDir, 0777, true);
|
|
(new \ReflectionProperty(FileCache::class, 'defaultInstance'))->setValue(null, new FileCache($this->rDir));
|
|
$rDb = new TestDb();
|
|
$rDb->exec('CREATE TABLE `servers` (`id` INTEGER PRIMARY KEY AUTO_INCREMENT, `server_ip` text, `private_ip` text, `domain_name` text, `enabled` int)');
|
|
$rDb->exec("INSERT INTO `servers` VALUES (1, '203.0.113.1', '10.0.0.1', 'panel.example.com,tv.example.com', 1), (5, '203.0.113.5', '', '', 1), (6, '203.0.113.6', '', 'off.example.com', 0)");
|
|
$rDb->exec('CREATE TABLE `users` (`id` INTEGER PRIMARY KEY AUTO_INCREMENT, `reseller_dns` text, `status` int)');
|
|
$rDb->exec("INSERT INTO `users` VALUES (1, 'reseller.example.net', 1), (2, 'gone.example.net', 0), (3, '', 1)");
|
|
DatabaseFactory::set($rDb);
|
|
(new \ReflectionProperty(ServerRepository::class, 'db'))->setValue(null, null);
|
|
}
|
|
|
|
protected function tearDown(): void {
|
|
(new \ReflectionProperty(FileCache::class, 'defaultInstance'))->setValue(null, null);
|
|
(new \ReflectionProperty(ServerRepository::class, 'db'))->setValue(null, null);
|
|
DatabaseFactory::reset();
|
|
exec('rm -rf ' . escapeshellarg($this->rDir));
|
|
}
|
|
|
|
public function testTheListIsBuiltAndCachedForVerifyHost(): void {
|
|
$rWant = ['127.0.0.1', 'localhost', 'panel.example.com', 'tv.example.com', '203.0.113.1', '10.0.0.1', '203.0.113.5', 'reseller.example.net'];
|
|
$this->assertEqualsCanonicalizing($rWant, array_values(ServerRepository::getAllowedDomains(true)), 'enabled servers and active resellers only');
|
|
$this->assertEqualsCanonicalizing($rWant, array_values((array) FileCache::getCache('allowed_domains')), 'the cache HostVerificationStage reads');
|
|
|
|
$rCron = (string) file_get_contents(MAIN_HOME . 'Cli/CronJobs/CacheCronJob.php');
|
|
$this->assertStringContainsString('ServerRepository::getAllowedDomains(true);', $rCron, 'cron:cache writes it every minute');
|
|
$this->assertStringContainsString("FileCache::delCache('allowed_domains');", $rCron, 'a node in mode 2 keeps none');
|
|
$this->assertStringContainsString('if (!ReplicaApply::owns(ReplicaSections::SERVERS)) {', $rCron, 'unless its replica owns the servers: the replica writes the list');
|
|
}
|
|
|
|
public function testTheReplicasServersAndResellersMakeTheDatabasesList(): void {
|
|
// What ReplicaApply builds on a node: the servers cache rows (every
|
|
// server, a disabled one too) and the section's `reseller_dns`.
|
|
$rRows = [
|
|
1 => ['enabled' => 1, 'server_ip' => '203.0.113.1', 'private_ip' => '10.0.0.1', 'domain_name' => 'panel.example.com,tv.example.com'],
|
|
5 => ['enabled' => 1, 'server_ip' => '203.0.113.5', 'private_ip' => null, 'domain_name' => null],
|
|
6 => ['enabled' => 0, 'server_ip' => '203.0.113.6', 'private_ip' => '', 'domain_name' => 'off.example.com'],
|
|
];
|
|
$this->assertEqualsCanonicalizing(ServerRepository::getAllowedDomains(true), ServerRepository::allowedDomains($rRows, ['reseller.example.net']));
|
|
$this->assertSame(['127.0.0.1', 'localhost'], ServerRepository::allowedDomains([], []));
|
|
$this->assertSame(['127.0.0.1', 'localhost', 'a.example.net'], ServerRepository::allowedDomains([], ['a.example.net', 'localhost', '']), 'once each, never empty');
|
|
}
|
|
}
|