From 28bf4082b3bf4ce62f83ffe1f2d19fb4f852d7e9 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Thu, 27 Feb 2020 22:34:46 +0100 Subject: [PATCH] [contextualprocess] Tests for new lookup behavior --- src/modules/contextualprocess/Binding.h | 2 + src/modules/contextualprocess/Tests.cpp | 125 ++++++++++++++++++++++++ src/modules/contextualprocess/Tests.h | 3 + 3 files changed, 130 insertions(+) diff --git a/src/modules/contextualprocess/Binding.h b/src/modules/contextualprocess/Binding.h index fce43a65c..25b5c7547 100644 --- a/src/modules/contextualprocess/Binding.h +++ b/src/modules/contextualprocess/Binding.h @@ -22,6 +22,8 @@ #ifndef CONTEXTUALPROCESSJOB_BINDING_H #define CONTEXTUALPROCESSJOB_BINDING_H +#include "Job.h" + #include #include #include diff --git a/src/modules/contextualprocess/Tests.cpp b/src/modules/contextualprocess/Tests.cpp index 8e7000f17..8f3322455 100644 --- a/src/modules/contextualprocess/Tests.cpp +++ b/src/modules/contextualprocess/Tests.cpp @@ -17,9 +17,15 @@ */ #include "Tests.h" + +#include "Binding.h" #include "ContextualProcessJob.h" +#include "GlobalStorage.h" +#include "JobQueue.h" +#include "utils/CalamaresUtilsSystem.h" #include "utils/CommandList.h" +#include "utils/Logger.h" #include "utils/Yaml.h" #include @@ -38,6 +44,22 @@ ContextualProcessTests::~ContextualProcessTests() {} void ContextualProcessTests::initTestCase() { + Logger::setupLogLevel( Logger::LOGDEBUG ); + + // Ensure we have a system object, expect it to be a "bogus" one + CalamaresUtils::System* system = CalamaresUtils::System::instance(); + QVERIFY( system ); + QVERIFY( system->doChroot() ); + + // Ensure we have a system-wide GlobalStorage with /tmp as root + if ( !Calamares::JobQueue::instance() ) + { + cDebug() << "Creating new JobQueue"; + (void)new Calamares::JobQueue(); + } + Calamares::GlobalStorage* gs + = Calamares::JobQueue::instance() ? Calamares::JobQueue::instance()->globalStorage() : nullptr; + QVERIFY( gs ); } void @@ -61,4 +83,107 @@ ContextualProcessTests::testProcessListSampleConfig() QCOMPARE( job.count(), 2 ); // Only "firmwareType" and "branding.shortVersion" QCOMPARE( job.count( "firmwareType" ), 4 ); + QCOMPARE( job.count( "branding.shortVersion" ), 2 ); // in the example config +} + +void ContextualProcessTests::testFetch() +{ + QVariantMap m; + // Some keys without sub-map + m.insert( QStringLiteral( "carrot" ), true ); + m.insert( QStringLiteral( "tomato" ), QStringLiteral( "fruit" ) ); + + // A key with sub-map + { + QVariantMap names; + names.insert( QStringLiteral( "blackcurrant" ), QStringLiteral( "black" ) ); + names.insert( QStringLiteral( "redcurrant" ), QStringLiteral( "red" ) ); + names.insert( QStringLiteral( "knoebels" ), QStringLiteral( "green" ) ); + names.insert( QStringLiteral( "strawberry" ), QStringLiteral( "red" ) ); + m.insert( QStringLiteral( "berries" ), names ); + } + + // Another key with sub-map + { + QVariantMap names; + names.insert( QStringLiteral( "ext4" ), 1 ); + names.insert( QStringLiteral( "zfs" ), 2 ); + names.insert( QStringLiteral( "swap" ), 2 ); + m.insert( QStringLiteral( "filesystem_use" ), names ); + } + + QCOMPARE( m.count(), 4 ); + + Calamares::GlobalStorage* gs + = Calamares::JobQueue::instance() ? Calamares::JobQueue::instance()->globalStorage() : nullptr; + QVERIFY( gs ); + + // Copy the built-up-map into GS + for ( auto it = m.cbegin(); it != m.cend(); ++it ) + { + gs->insert( it.key(), it.value() ); + } + + // Testing of fetch() + { + ContextualProcessBinding b( QStringLiteral( "carrot" ) ); + QString s; + QVERIFY( b.fetch( gs, s ) ); + QCOMPARE( s, QStringLiteral( "true" ) ); // String representation of boolean true + } + { + ContextualProcessBinding b( QStringLiteral( "tomato" ) ); + QString s; + QVERIFY( b.fetch( gs, s ) ); + QCOMPARE( s, QStringLiteral( "fruit" ) ); + } + { + // Key not found + ContextualProcessBinding b( QStringLiteral( "parsnip" ) ); + QString s = QStringLiteral( "white" ); + QVERIFY( !b.fetch( gs, s ) ); + QCOMPARE( s, QString() ); + QVERIFY( s.isEmpty() ); + } + { + // Submap gets smashed + ContextualProcessBinding b( QStringLiteral( "berries" ) ); + QString s; + QVERIFY( b.fetch( gs, s ) ); + QVERIFY( s.contains( QStringLiteral( "QVariant" ) ) ); + } + { + // Compound lookup + ContextualProcessBinding b( QStringLiteral( "berries.strawberry" ) ); + QString s; + QVERIFY( b.fetch( gs, s ) ); + QCOMPARE( s, QStringLiteral( "red" ) ); + } + { + ContextualProcessBinding b( QStringLiteral( "berries.knoebels" ) ); + QString s; + QVERIFY( b.fetch( gs, s ) ); + QCOMPARE( s, QStringLiteral( "green" ) ); + } + { + ContextualProcessBinding b( QStringLiteral( "filesystem_use.ext4" ) ); + QString s; + QVERIFY( b.fetch( gs, s ) ); + QCOMPARE( s, QStringLiteral( "1" ) ); + } + { + ContextualProcessBinding b( QStringLiteral( "filesystem_use.zfs" ) ); + QString s; + QVERIFY( b.fetch( gs, s ) ); + QCOMPARE( s, QStringLiteral( "2" ) ); + } + { + // Key not found, compound + ContextualProcessBinding b( QStringLiteral( "filesystem_use.ufs" ) ); + QString s = QStringLiteral( "ufs" ); + QVERIFY( !b.fetch( gs, s ) ); + QCOMPARE( s, QString() ); + QVERIFY( s.isEmpty() ); + } + } diff --git a/src/modules/contextualprocess/Tests.h b/src/modules/contextualprocess/Tests.h index 1708e53f0..fd705a103 100644 --- a/src/modules/contextualprocess/Tests.h +++ b/src/modules/contextualprocess/Tests.h @@ -32,6 +32,9 @@ private Q_SLOTS: void initTestCase(); // Check the sample config file is processed correctly void testProcessListSampleConfig(); + + // Variable binding lookup + void testFetch(); }; #endif