[shellprocess] Apply current coding style

This commit is contained in:
Adriaan de Groot 2019-08-01 23:10:20 +02:00
parent d363732302
commit a0430f76b7
3 changed files with 57 additions and 54 deletions

View File

@ -18,13 +18,13 @@
#include "ShellProcessJob.h" #include "ShellProcessJob.h"
#include <QProcess>
#include <QDateTime> #include <QDateTime>
#include <QProcess>
#include <QThread> #include <QThread>
#include "CalamaresVersion.h" #include "CalamaresVersion.h"
#include "JobQueue.h"
#include "GlobalStorage.h" #include "GlobalStorage.h"
#include "JobQueue.h"
#include "utils/CommandList.h" #include "utils/CommandList.h"
#include "utils/Logger.h" #include "utils/Logger.h"
@ -55,7 +55,7 @@ Calamares::JobResult
ShellProcessJob::exec() ShellProcessJob::exec()
{ {
if ( ! m_commands || m_commands->isEmpty() ) if ( !m_commands || m_commands->isEmpty() )
{ {
cWarning() << "No commands to execute" << moduleInstanceKey(); cWarning() << "No commands to execute" << moduleInstanceKey();
return Calamares::JobResult::ok(); return Calamares::JobResult::ok();
@ -71,16 +71,23 @@ ShellProcessJob::setConfigurationMap( const QVariantMap& configurationMap )
bool dontChroot = CalamaresUtils::getBool( configurationMap, "dontChroot", false ); bool dontChroot = CalamaresUtils::getBool( configurationMap, "dontChroot", false );
int timeout = CalamaresUtils::getInteger( configurationMap, "timeout", 10 ); int timeout = CalamaresUtils::getInteger( configurationMap, "timeout", 10 );
if ( timeout < 1 ) if ( timeout < 1 )
{
timeout = 10; timeout = 10;
}
if ( configurationMap.contains( "script" ) ) if ( configurationMap.contains( "script" ) )
{ {
m_commands = new CalamaresUtils::CommandList( configurationMap.value( "script" ), !dontChroot, std::chrono::seconds( timeout ) ); m_commands = new CalamaresUtils::CommandList(
configurationMap.value( "script" ), !dontChroot, std::chrono::seconds( timeout ) );
if ( m_commands->isEmpty() ) if ( m_commands->isEmpty() )
{
cDebug() << "ShellProcessJob: \"script\" contains no commands for" << moduleInstanceKey(); cDebug() << "ShellProcessJob: \"script\" contains no commands for" << moduleInstanceKey();
} }
}
else else
{
cWarning() << "No script given for ShellProcessJob" << moduleInstanceKey(); cWarning() << "No script given for ShellProcessJob" << moduleInstanceKey();
}
} }
CALAMARES_PLUGIN_FACTORY_DEFINITION( ShellProcessJobFactory, registerPlugin<ShellProcessJob>(); ) CALAMARES_PLUGIN_FACTORY_DEFINITION( ShellProcessJobFactory, registerPlugin< ShellProcessJob >(); )

View File

@ -37,13 +37,9 @@ using CommandList = CalamaresUtils::CommandList;
using std::operator""s; using std::operator""s;
ShellProcessTests::ShellProcessTests() ShellProcessTests::ShellProcessTests() {}
{
}
ShellProcessTests::~ShellProcessTests() ShellProcessTests::~ShellProcessTests() {}
{
}
void void
ShellProcessTests::initTestCase() ShellProcessTests::initTestCase()
@ -66,16 +62,16 @@ ShellProcessTests::testProcessListSampleConfig()
} }
} }
CommandList cl( CommandList cl( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( !cl.isEmpty() ); QVERIFY( !cl.isEmpty() );
QCOMPARE( cl.count(), 3 ); QCOMPARE( cl.count(), 3 );
QCOMPARE( cl.at(0).timeout(), CalamaresUtils::CommandLine::TimeoutNotSet() ); QCOMPARE( cl.at( 0 ).timeout(), CalamaresUtils::CommandLine::TimeoutNotSet() );
QCOMPARE( cl.at(2).timeout(), 3600s ); // slowloris QCOMPARE( cl.at( 2 ).timeout(), 3600s ); // slowloris
} }
void ShellProcessTests::testProcessListFromList() void
ShellProcessTests::testProcessListFromList()
{ {
YAML::Node doc = YAML::Load( R"(--- YAML::Node doc = YAML::Load( R"(---
script: script:
@ -83,8 +79,7 @@ script:
- "ls /nonexistent" - "ls /nonexistent"
- "/bin/false" - "/bin/false"
)" ); )" );
CommandList cl( CommandList cl( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( !cl.isEmpty() ); QVERIFY( !cl.isEmpty() );
QCOMPARE( cl.count(), 3 ); QCOMPARE( cl.count(), 3 );
@ -95,52 +90,51 @@ script:
- false - false
- "ls /nonexistent" - "ls /nonexistent"
)" ); )" );
CommandList cl1( CommandList cl1( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( !cl1.isEmpty() ); QVERIFY( !cl1.isEmpty() );
QCOMPARE( cl1.count(), 2 ); // One element ignored QCOMPARE( cl1.count(), 2 ); // One element ignored
} }
void ShellProcessTests::testProcessListFromString() void
ShellProcessTests::testProcessListFromString()
{ {
YAML::Node doc = YAML::Load( R"(--- YAML::Node doc = YAML::Load( R"(---
script: "ls /tmp" script: "ls /tmp"
)" ); )" );
CommandList cl( CommandList cl( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( !cl.isEmpty() ); QVERIFY( !cl.isEmpty() );
QCOMPARE( cl.count(), 1 ); QCOMPARE( cl.count(), 1 );
QCOMPARE( cl.at(0).timeout(), 10s ); QCOMPARE( cl.at( 0 ).timeout(), 10s );
QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) ); QCOMPARE( cl.at( 0 ).command(), QStringLiteral( "ls /tmp" ) );
// Not a string // Not a string
doc = YAML::Load( R"(--- doc = YAML::Load( R"(---
script: false script: false
)" ); )" );
CommandList cl1( CommandList cl1( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( cl1.isEmpty() ); QVERIFY( cl1.isEmpty() );
QCOMPARE( cl1.count(), 0 ); QCOMPARE( cl1.count(), 0 );
} }
void ShellProcessTests::testProcessFromObject() void
ShellProcessTests::testProcessFromObject()
{ {
YAML::Node doc = YAML::Load( R"(--- YAML::Node doc = YAML::Load( R"(---
script: script:
command: "ls /tmp" command: "ls /tmp"
timeout: 20 timeout: 20
)" ); )" );
CommandList cl( CommandList cl( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( !cl.isEmpty() ); QVERIFY( !cl.isEmpty() );
QCOMPARE( cl.count(), 1 ); QCOMPARE( cl.count(), 1 );
QCOMPARE( cl.at(0).timeout(), 20s ); QCOMPARE( cl.at( 0 ).timeout(), 20s );
QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) ); QCOMPARE( cl.at( 0 ).command(), QStringLiteral( "ls /tmp" ) );
} }
void ShellProcessTests::testProcessListFromObject() void
ShellProcessTests::testProcessListFromObject()
{ {
YAML::Node doc = YAML::Load( R"(--- YAML::Node doc = YAML::Load( R"(---
script: script:
@ -148,34 +142,36 @@ script:
timeout: 12 timeout: 12
- "-/bin/false" - "-/bin/false"
)" ); )" );
CommandList cl( CommandList cl( CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
QVERIFY( !cl.isEmpty() ); QVERIFY( !cl.isEmpty() );
QCOMPARE( cl.count(), 2 ); QCOMPARE( cl.count(), 2 );
QCOMPARE( cl.at(0).timeout(), 12s ); QCOMPARE( cl.at( 0 ).timeout(), 12s );
QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) ); QCOMPARE( cl.at( 0 ).command(), QStringLiteral( "ls /tmp" ) );
QCOMPARE( cl.at(1).timeout(), CalamaresUtils::CommandLine::TimeoutNotSet() ); // not set QCOMPARE( cl.at( 1 ).timeout(), CalamaresUtils::CommandLine::TimeoutNotSet() ); // not set
} }
void ShellProcessTests::testRootSubstitution() void
ShellProcessTests::testRootSubstitution()
{ {
YAML::Node doc = YAML::Load( R"(--- YAML::Node doc = YAML::Load( R"(---
script: script:
- "ls /tmp" - "ls /tmp"
)" ); )" );
QVariant plainScript = CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ); QVariant plainScript = CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" );
QVariant rootScript = CalamaresUtils::yamlMapToVariant( QVariant rootScript = CalamaresUtils::yamlMapToVariant( YAML::Load( R"(---
YAML::Load( R"(---
script: script:
- "ls @@ROOT@@" - "ls @@ROOT@@"
)" ) ).toMap().value( "script" ); )" ) )
QVariant userScript = CalamaresUtils::yamlMapToVariant( .toMap()
YAML::Load( R"(--- .value( "script" );
QVariant userScript = CalamaresUtils::yamlMapToVariant( YAML::Load( R"(---
script: script:
- mktemp -d @@ROOT@@/calatestXXXXXXXX - mktemp -d @@ROOT@@/calatestXXXXXXXX
- "chown @@USER@@ @@ROOT@@/calatest*" - "chown @@USER@@ @@ROOT@@/calatest*"
- rm -rf @@ROOT@@/calatest* - rm -rf @@ROOT@@/calatest*
)" ) ).toMap().value( "script" ); )" ) )
.toMap()
.value( "script" );
if ( !Calamares::JobQueue::instance() ) if ( !Calamares::JobQueue::instance() )
(void)new Calamares::JobQueue( nullptr ); (void)new Calamares::JobQueue( nullptr );
@ -188,29 +184,29 @@ script:
qDebug() << "Expect WARNING, ERROR, WARNING"; qDebug() << "Expect WARNING, ERROR, WARNING";
// Doesn't use @@ROOT@@, so no failures // Doesn't use @@ROOT@@, so no failures
QVERIFY( bool(CommandList(plainScript, false, 10s ).run()) ); QVERIFY( bool( CommandList( plainScript, false, 10s ).run() ) );
// Doesn't use @@ROOT@@, but does chroot, so fails // Doesn't use @@ROOT@@, but does chroot, so fails
QVERIFY( !bool(CommandList(plainScript, true, 10s ).run()) ); QVERIFY( !bool( CommandList( plainScript, true, 10s ).run() ) );
// Does use @@ROOT@@, which is not set, so fails // Does use @@ROOT@@, which is not set, so fails
QVERIFY( !bool(CommandList(rootScript, false, 10s ).run()) ); QVERIFY( !bool( CommandList( rootScript, false, 10s ).run() ) );
// .. fails for two reasons // .. fails for two reasons
QVERIFY( !bool(CommandList(rootScript, true, 10s ).run()) ); QVERIFY( !bool( CommandList( rootScript, true, 10s ).run() ) );
gs->insert( "rootMountPoint", "/tmp" ); gs->insert( "rootMountPoint", "/tmp" );
// Now that the root is set, two variants work .. still can't // Now that the root is set, two variants work .. still can't
// chroot, unless the rootMountPoint contains a full system, // chroot, unless the rootMountPoint contains a full system,
// *and* we're allowed to chroot (ie. running tests as root). // *and* we're allowed to chroot (ie. running tests as root).
qDebug() << "Expect no output."; qDebug() << "Expect no output.";
QVERIFY( bool(CommandList(plainScript, false, 10s ).run()) ); QVERIFY( bool( CommandList( plainScript, false, 10s ).run() ) );
QVERIFY( bool(CommandList(rootScript, false, 10s ).run()) ); QVERIFY( bool( CommandList( rootScript, false, 10s ).run() ) );
qDebug() << "Expect ERROR"; qDebug() << "Expect ERROR";
// But no user set yet // But no user set yet
QVERIFY( !bool(CommandList(userScript, false, 10s ).run()) ); QVERIFY( !bool( CommandList( userScript, false, 10s ).run() ) );
// Now play dangerous games with shell expansion // Now play dangerous games with shell expansion
gs->insert( "username", "`id -u`" ); gs->insert( "username", "`id -u`" );
QVERIFY( bool(CommandList(userScript, false, 10s ).run()) ); QVERIFY( bool( CommandList( userScript, false, 10s ).run() ) );
} }