ssuclitest.cpp 5.32 KB
Newer Older
1
/**
2
 * @file ssuclitest.cpp
3 4 5 6 7
 * @copyright 2013 Jolla Ltd.
 * @author Martin Kampas <martin.kampas@tieto.com>
 * @date 2013
 */

8
#include "ssuclitest.h"
9 10 11 12 13 14

#include <stdlib.h>
#include <zypp/media/UrlResolverPlugin.h>

#include <QtTest/QtTest>

15
#include "libssu/sandbox_p.h"
16
#include "testutils/process.h"
17

18
typedef QStringList Args; // improve readability
19

20 21 22 23 24 25 26 27 28 29 30 31 32
void SsuCliTest::init()
{
    Q_ASSERT(m_sandbox == 0);

    m_sandbox = new Sandbox(QString("%1/configroot").arg(LOCATE_DATA_PATH),
                            Sandbox::UseAsSkeleton, Sandbox::ChildProcesses);
    if (!m_sandbox->activate()) {
        QFAIL("Failed to activate sandbox");
    }
    if (getenv("SSU_SANDBOX_PATH")) {
        qDebug() << "Using in-tree sandbox";
        setenv("LD_PRELOAD", getenv("SSU_SANDBOX_PATH"), 1);
    } else
33
        setenv("LD_PRELOAD", SSU_SANDBOX_PATH, 1);
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

    m_bus = new QProcess(this);
    m_bus->start("dbus-daemon",
                 Args() << "--session" << "--nofork" << "--nopidfile" << "--print-address");
    if (!m_bus->waitForReadyRead()) {
        QFAIL("Failed to run sandboxed D-Bus instance");
    }
    const QByteArray busAddress = m_bus->readAllStandardOutput();
    setenv("DBUS_SESSION_BUS_ADDRESS", busAddress.constData(), 1);
    setenv("DBUS_SYSTEM_BUS_ADDRESS", busAddress.constData(), 1);

    m_ssud = new QProcess(this);
    m_ssud->start("ssud");
    if (!m_ssud->waitForStarted()) {
        QFAIL("Failed to run sandboxed ssud instance");
    }
50 51
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
void SsuCliTest::cleanup()
{
    if (m_ssud->state() != QProcess::Running) {
        QFAIL("Sandboxed ssud instance exited unexpectedly");
    }
    delete m_ssud;
    m_ssud = 0;

    if (m_bus->state() != QProcess::Running) {
        QFAIL("Sandboxed D-Bus instance exited unexpectedly");
    }
    unsetenv("DBUS_SESSION_BUS_ADDRESS");
    unsetenv("DBUS_SYSTEM_BUS_ADDRESS");
    delete m_bus;
    m_bus = 0;

    delete m_sandbox;
    m_sandbox = 0;
70 71
}

72 73 74 75
void SsuCliTest::testSubcommandFlavour()
{
    Process ssu;
    QString output;
76

77 78 79
    // set flavour to 'release'
    ssu.execute("ssu", Args() << "flavour" << "release");
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
80

81 82
    output = ssu.execute("ssu", Args() << "flavour").trimmed();
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
83

84
    QCOMPARE(output, QString("Device flavour is currently: release"));
85

86 87 88
    // set flavour to 'testing'
    ssu.execute("ssu", Args() << "flavour" << "testing");
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
89

90 91
    output = ssu.execute("ssu", Args() << "flavour").trimmed();
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
92

93
    QCOMPARE(output, QString("Device flavour is currently: testing"));
94 95
}

96 97 98 99
void SsuCliTest::testSubcommandRelease()
{
    Process ssu;
    QString output;
100

101 102 103
    // set release to latest
    ssu.execute("ssu", Args() << "release" << "latest");
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
104

105 106
    output = ssu.execute("ssu", Args() << "release").trimmed();
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
107

108
    QCOMPARE(output, QString("Device release is currently: latest"));
109

110 111 112
    // set release to next
    ssu.execute("ssu", Args() << "release" << "next");
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
113

114 115
    output = ssu.execute("ssu", Args() << "release").trimmed();
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
116

117
    QCOMPARE(output, QString("Device release is currently: next"));
118

119 120 121
    // verify the mode is release
    output = ssu.execute("ssu", Args() << "mode").trimmed();
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
122

123
    QCOMPARE(output, QString("Device mode is: 0 ()"));
124

125 126 127
    // set RnD release to latest
    ssu.execute("ssu", Args() << "release" << "-r" << "latest");
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
128

129 130
    output = ssu.execute("ssu", Args() << "release" << "-r").trimmed();
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
131

132
    QCOMPARE(output, QString("Device release (RnD) is currently: latest"));
133

134 135 136
    // set RnD release to next
    ssu.execute("ssu", Args() << "release" << "-r" << "next");
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
137

138 139
    output = ssu.execute("ssu", Args() << "release" << "-r").trimmed();
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
140

141
    QCOMPARE(output, QString("Device release (RnD) is currently: next"));
142

143 144 145
    // verify the mode is RnD
    output = ssu.execute("ssu", Args() << "mode").trimmed();
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
146

147
    QCOMPARE(output, QString("Device mode is: 2 (RndMode)"));
148 149
}

150 151 152 153
void SsuCliTest::testSubcommandMode()
{
    Process ssu;
    QString output;
154

155 156 157
    // set release mode
    ssu.execute("ssu", Args() << "mode" << "0");
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
158

159 160
    output = ssu.execute("ssu", Args() << "mode").trimmed();
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
161

162
    QCOMPARE(output, QString("Device mode is: 0 ()"));
163

164 165 166
    // set RnD mode
    ssu.execute("ssu", Args() << "mode" << "2");
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
167

168 169
    output = ssu.execute("ssu", Args() << "mode").trimmed();
    QVERIFY2(!ssu.hasError(), qPrintable(ssu.fmtErrorMessage()));
170

171
    QCOMPARE(output, QString("Device mode is: 2 (RndMode)"));
172
}