. * * @link https://www.librenms.org * * @copyright 2016 Tony Murray * @author Tony Murray */ namespace LibreNMS\Tests; use LibreNMS\Config; use LibreNMS\Data\Store\Rrd; class RrdtoolTest extends TestCase { public function testBuildCommandLocal(): void { Config::set('rrdcached', ''); Config::set('rrdtool_version', '1.4'); Config::set('rrd_dir', '/opt/librenms/rrd'); $cmd = $this->buildCommandProxy('create', '/opt/librenms/rrd/f', 'o'); $this->assertEquals('create /opt/librenms/rrd/f o', $cmd); $cmd = $this->buildCommandProxy('tune', '/opt/librenms/rrd/f', 'o'); $this->assertEquals('tune /opt/librenms/rrd/f o', $cmd); $cmd = $this->buildCommandProxy('update', '/opt/librenms/rrd/f', 'o'); $this->assertEquals('update /opt/librenms/rrd/f o', $cmd); $this->app->forgetInstance(Rrd::class); Config::set('rrdtool_version', '1.6'); $cmd = $this->buildCommandProxy('create', '/opt/librenms/rrd/f', 'o'); $this->assertEquals('create /opt/librenms/rrd/f o -O', $cmd); $cmd = $this->buildCommandProxy('tune', '/opt/librenms/rrd/f', 'o'); $this->assertEquals('tune /opt/librenms/rrd/f o', $cmd); $cmd = $this->buildCommandProxy('update', '/opt/librenms/rrd/f', 'o'); $this->assertEquals('update /opt/librenms/rrd/f o', $cmd); } public function testBuildCommandRemote(): void { Config::set('rrdcached', 'server:42217'); Config::set('rrdtool_version', '1.4'); Config::set('rrd_dir', '/opt/librenms/rrd'); $cmd = $this->buildCommandProxy('create', '/opt/librenms/rrd/f', 'o'); $this->assertEquals('create /opt/librenms/rrd/f o', $cmd); $cmd = $this->buildCommandProxy('tune', '/opt/librenms/rrd/f', 'o'); $this->assertEquals('tune /opt/librenms/rrd/f o', $cmd); $cmd = $this->buildCommandProxy('update', '/opt/librenms/rrd/f', 'o'); $this->assertEquals('update f o --daemon server:42217', $cmd); $this->app->forgetInstance(Rrd::class); Config::set('rrdtool_version', '1.6'); $cmd = $this->buildCommandProxy('create', '/opt/librenms/rrd/f', 'o'); $this->assertEquals('create f o -O --daemon server:42217', $cmd); $cmd = $this->buildCommandProxy('tune', '/opt/librenms/rrd/f', 'o'); $this->assertEquals('tune f o --daemon server:42217', $cmd); $cmd = $this->buildCommandProxy('update', '/opt/librenms/rrd/f', 'o'); $this->assertEquals('update f o --daemon server:42217', $cmd); } public function testBuildCommandException(): void { Config::set('rrdcached', ''); Config::set('rrdtool_version', '1.4'); $this->expectException('LibreNMS\Exceptions\FileExistsException'); // use this file, since it is guaranteed to exist $this->buildCommandProxy('create', __FILE__, 'o'); } private function buildCommandProxy($command, $filename, $options) { // todo better tests $reflection = new \ReflectionClass(Rrd::class); $method = $reflection->getMethod('buildCommand'); $method->setAccessible(true); return $method->invokeArgs($this->app->make(Rrd::class), func_get_args()); } }