[libcalamares] Add a test for command-timeout

This commit is contained in:
Adriaan de Groot 2024-06-30 15:49:27 +02:00
parent 8bb6c63931
commit e6de798228

View File

@ -31,6 +31,8 @@
#include <QtTest/QtTest> #include <QtTest/QtTest>
#include <utility>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
@ -56,6 +58,7 @@ private Q_SLOTS:
void testCommandConstructors(); void testCommandConstructors();
void testCommandConstructorsYAML(); void testCommandConstructorsYAML();
void testCommandRunning(); void testCommandRunning();
void testCommandTimeout();
void testCommandVerbose(); void testCommandVerbose();
/** @section Test that all the UMask objects work correctly. */ /** @section Test that all the UMask objects work correctly. */
@ -455,6 +458,38 @@ LibCalamaresTests::testCommandRunning()
tempRoot.setAutoRemove( true ); tempRoot.setAutoRemove( true );
} }
void
LibCalamaresTests::testCommandTimeout()
{
QTemporaryDir tempRoot( QDir::tempPath() + QStringLiteral( "/test-job-XXXXXX" ) );
tempRoot.setAutoRemove( false );
const QString testExecutable = tempRoot.filePath( "example.sh" );
cDebug() << "Creating example executable" << testExecutable;
{
QFile f( testExecutable );
QVERIFY( f.open( QIODevice::WriteOnly ) );
f.write( "#! /bin/sh\necho early\nsleep 3\necho late" );
f.close();
Calamares::Permissions::apply( testExecutable, 0755 );
}
{
Calamares::CommandList l( false ); // no chroot
Calamares::CommandLine c( testExecutable, {}, std::chrono::seconds( 2 ) );
l.push_back( c );
const auto r = l.run();
QVERIFY( !bool( r ) ); // Because it times out after 2 seconds
// The **command** timed out, but the job result is a generic "error"
// QCOMPARE( r.errorCode(), static_cast<std::underlying_type_t<Calamares::ProcessResult::Code>>(Calamares::ProcessResult::Code::TimedOut));
QCOMPARE( r.errorCode(), -1 );
}
}
void void
LibCalamaresTests::testCommandVerbose() LibCalamaresTests::testCommandVerbose()
{ {