mirror of
https://github.com/Icinga/icingadb-web.git
synced 2026-05-28 04:36:06 -04:00
This was easy because only README.md and doc/01-About.md were redacted manually, everything else via: git ls-files -z |xargs -0 perl -pi -e 's/Icinga GmbH \| GPLv2/Icinga GmbH | GPLv2+/' This is legal because we have only merged PRs with label:cla/signed or made by Icinga staff: https://github.com/Icinga/icingadb-web/pulls?page=1&q=is%3Apr+is%3Aclosed+-label%3Acla%2Fsigned+-author%3Anilmerg This has no risk for us in people distributing their own version under GPLv3 only. After all, we won't take their patches anyway, unless they sign our CLA. This is the cleanest solution for having e.g. these in one address space: * Icinga Web, GPLv2+ * K8s Web, AGPLv3 * Thirdparty, some LGPLv3 and Apache-2.0 Apropos, K8s Web is even v3-licensed on purpose, to have a stronger protection against cloud ops.
163 lines
7.3 KiB
PHP
163 lines
7.3 KiB
PHP
<?php
|
|
|
|
/* Icinga DB Web | (c) 2024 Icinga GmbH | GPLv2+ */
|
|
|
|
namespace Tests\Icinga\Modules\Icingadb\Model\Behavior;
|
|
|
|
use Icinga\Module\Icingadb\Common\Backend;
|
|
use Icinga\Module\Icingadb\Model\Host;
|
|
use ipl\Sql\Connection;
|
|
use ipl\Sql\Test\SqlAssertions;
|
|
use ipl\Sql\Test\TestConnection;
|
|
use ipl\Stdlib\Filter;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class FlattenedObjectVarsTest extends TestCase
|
|
{
|
|
use SqlAssertions;
|
|
|
|
private const SINGLE_UNEQUAL_RESULT = <<<'SQL'
|
|
SELECT host.id
|
|
FROM host
|
|
WHERE (host.id NOT IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id
|
|
FROM customvar_flat sub_customvar_flat
|
|
INNER JOIN host_customvar sub_customvar_flat_t_host_customvar
|
|
ON sub_customvar_flat_t_host_customvar.customvar_id =
|
|
sub_customvar_flat.customvar_id
|
|
INNER JOIN host sub_customvar_flat_host
|
|
ON sub_customvar_flat_host.id = sub_customvar_flat_t_host_customvar.host_id
|
|
WHERE ((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?))
|
|
AND (sub_customvar_flat_host.id IS NOT NULL)
|
|
GROUP BY sub_customvar_flat_host.id
|
|
HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?)) OR host.id IS NULL)
|
|
ORDER BY host.id
|
|
SQL;
|
|
|
|
private const DOUBLE_UNEQUAL_RESULT = <<<'SQL'
|
|
SELECT host.id
|
|
FROM host
|
|
WHERE (host.id NOT IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id
|
|
FROM customvar_flat sub_customvar_flat
|
|
INNER JOIN host_customvar sub_customvar_flat_t_host_customvar
|
|
ON sub_customvar_flat_t_host_customvar.customvar_id =
|
|
sub_customvar_flat.customvar_id
|
|
INNER JOIN host sub_customvar_flat_host
|
|
ON sub_customvar_flat_host.id = sub_customvar_flat_t_host_customvar.host_id
|
|
WHERE (((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?)) OR
|
|
((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?)))
|
|
AND (sub_customvar_flat_host.id IS NOT NULL)
|
|
GROUP BY sub_customvar_flat_host.id
|
|
HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?)) OR host.id IS NULL)
|
|
ORDER BY host.id
|
|
SQL;
|
|
|
|
private const EQUAL_UNEQUAL_RESULT = <<<'SQL'
|
|
SELECT host.id
|
|
FROM host
|
|
WHERE ((host.id NOT IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id
|
|
FROM customvar_flat sub_customvar_flat
|
|
INNER JOIN host_customvar sub_customvar_flat_t_host_customvar
|
|
ON sub_customvar_flat_t_host_customvar.customvar_id =
|
|
sub_customvar_flat.customvar_id
|
|
INNER JOIN host sub_customvar_flat_host
|
|
ON sub_customvar_flat_host.id = sub_customvar_flat_t_host_customvar.host_id
|
|
WHERE ((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?))
|
|
AND (sub_customvar_flat_host.id IS NOT NULL)
|
|
GROUP BY sub_customvar_flat_host.id
|
|
HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?)) OR host.id IS NULL))
|
|
AND (host.id IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id
|
|
FROM customvar_flat sub_customvar_flat
|
|
INNER JOIN host_customvar sub_customvar_flat_t_host_customvar
|
|
ON sub_customvar_flat_t_host_customvar.customvar_id =
|
|
sub_customvar_flat.customvar_id
|
|
INNER JOIN host sub_customvar_flat_host
|
|
ON sub_customvar_flat_host.id = sub_customvar_flat_t_host_customvar.host_id
|
|
WHERE (sub_customvar_flat.flatname = ?)
|
|
AND (sub_customvar_flat.flatvalue = ?)
|
|
GROUP BY sub_customvar_flat_host.id
|
|
HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?)))
|
|
ORDER BY host.id
|
|
SQL;
|
|
|
|
private const DOUBLE_EQUAL_RESULT = <<<'SQL'
|
|
SELECT host.id
|
|
FROM host
|
|
WHERE host.id IN ((SELECT sub_customvar_flat_host.id AS sub_customvar_flat_host_id
|
|
FROM customvar_flat sub_customvar_flat
|
|
INNER JOIN host_customvar sub_customvar_flat_t_host_customvar
|
|
ON sub_customvar_flat_t_host_customvar.customvar_id =
|
|
sub_customvar_flat.customvar_id
|
|
INNER JOIN host sub_customvar_flat_host
|
|
ON sub_customvar_flat_host.id = sub_customvar_flat_t_host_customvar.host_id
|
|
WHERE ((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?))
|
|
OR ((sub_customvar_flat.flatname = ?) AND (sub_customvar_flat.flatvalue = ?))
|
|
GROUP BY sub_customvar_flat_host.id
|
|
HAVING COUNT(DISTINCT sub_customvar_flat.id) >= ?))
|
|
ORDER BY host.id
|
|
SQL;
|
|
|
|
/** @var Connection */
|
|
private $connection;
|
|
|
|
public function setUp(): void
|
|
{
|
|
$this->connection = new TestConnection();
|
|
Backend::setDb($this->connection);
|
|
$this->setUpSqlAssertions();
|
|
}
|
|
|
|
public function testSingleUnequalCondition()
|
|
{
|
|
$query = Host::on($this->connection)
|
|
->columns('host.id')
|
|
->orderBy('host.id')
|
|
->filter(Filter::unequal('host.vars.in.valid', 'foo'));
|
|
|
|
$this->assertSql(self::SINGLE_UNEQUAL_RESULT, $query->assembleSelect(), ['in.valid', 'foo', 1]);
|
|
}
|
|
|
|
public function testDoubleUnequalCondition()
|
|
{
|
|
$query = Host::on($this->connection)
|
|
->columns('host.id')
|
|
->orderBy('host.id')
|
|
->filter(Filter::unequal('host.vars.in.valid', 'foo'))
|
|
->filter(Filter::unequal('host.vars.missing', 'bar'));
|
|
|
|
$this->assertSql(
|
|
self::DOUBLE_UNEQUAL_RESULT,
|
|
$query->assembleSelect(),
|
|
['in.valid', 'foo', 'missing', 'bar', 1]
|
|
);
|
|
}
|
|
|
|
public function testEqualAndUnequalCondition()
|
|
{
|
|
$query = Host::on($this->connection)
|
|
->columns('host.id')
|
|
->orderBy('host.id')
|
|
->filter(Filter::unequal('host.vars.in.valid', 'bar'))
|
|
->filter(Filter::equal('host.vars.env', 'foo'));
|
|
|
|
$this->assertSql(
|
|
self::EQUAL_UNEQUAL_RESULT,
|
|
$query->assembleSelect(),
|
|
['in.valid', 'bar', 1, 'env', 'foo', 1]
|
|
);
|
|
}
|
|
|
|
public function testDoubleEqualCondition()
|
|
{
|
|
$query = Host::on($this->connection)
|
|
->columns('host.id')
|
|
->orderBy('host.id')
|
|
->filter(Filter::equal('host.vars.env', 'foo'))
|
|
->filter(Filter::equal('host.vars.os', 'bar'));
|
|
|
|
$this->assertSql(
|
|
self::DOUBLE_EQUAL_RESULT,
|
|
$query->assembleSelect(),
|
|
['env', 'foo', 'os', 'bar', 2]
|
|
);
|
|
}
|
|
}
|