Skip to content

Commit

Permalink
[vault] consider backup as failed if any unit is failed. Fixes MER#1026
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Zalevskiy <denis.zalevskiy@jolla.com>
  • Loading branch information
Denis Zalevskiy committed May 26, 2015
1 parent 76a398e commit ae46fe4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 9 deletions.
1 change: 0 additions & 1 deletion .gitignore
Expand Up @@ -23,5 +23,4 @@ units/vault-*
*.moc
tests/tests.xml
tests/*_unit.cpp
tests/*_vault_test
src/config.hpp
12 changes: 10 additions & 2 deletions qml/Vault/vault.cpp
Expand Up @@ -64,10 +64,18 @@ class Worker : public QObject
Q_INVOKABLE void backup(const QString &home, const QStringList &units, const QString &message)
{
debug::debug("Backup: home", home);
m_vault->backup(home, units, message, [this](const QString unit, const QString &status) {
auto res = m_vault->backup(home, units, message, [this](const QString unit, const QString &status) {
emit progress(Vault::Backup, {{"unit", unit}, {"status", status}});
});
emit done(Vault::Backup, QVariantMap());
if (res.failedUnits.size() == 0) {
emit done(Vault::Backup, QVariantMap());
} else {
QVariantMap info{{"msg", "Backup of some units is failed"}
, {"reason", "Backup"}
, {"succeeded", res.succededUnits}
, {"failed", res.failedUnits}};
emit error(Vault::Backup, info);
}
}

QStringList snapshots() const
Expand Down
11 changes: 7 additions & 4 deletions src/vault.cpp
Expand Up @@ -405,7 +405,6 @@ Vault::Result Vault::backup(const QString &home, const QStringList &units, const
debug::info("Backup units", units, ", home", home);
auto l = lock();
Result res;
res.failedUnits << units;

if (!os::path::isDir(home)) {
qWarning("Home is not a dir: %s", qPrintable(home));
Expand All @@ -417,6 +416,7 @@ Vault::Result Vault::backup(const QString &home, const QStringList &units, const
};

resetMaster();
Gittin::Commit head = Gittin::Branch(&m_vcs, "master").head();

QStringList usedUnits = units;
if (units.isEmpty()) {
Expand All @@ -428,12 +428,14 @@ Vault::Result Vault::backup(const QString &home, const QStringList &units, const
}
for (const QString &unit: usedUnits) {
if (backupUnit(home, unit, progress)) {
res.failedUnits.removeOne(unit);
res.succededUnits << unit;
} else {
res.failedUnits << unit;
break;
}
}

if (res.succededUnits.size()) {
if (res.succededUnits.size() == usedUnits.size()) {
QString timeTag = QDateTime::currentDateTimeUtc().toString("yyyy-MM-ddTHH-mm-ss.zzzZ");
qDebug()<<timeTag<<message;
QString msg = message.isEmpty() ? timeTag : message + '\n' + timeTag;
Expand All @@ -443,7 +445,8 @@ Vault::Result Vault::backup(const QString &home, const QStringList &units, const
commit.addNote(message);
tagSnapshot(timeTag);
} else {
debug::warning("There is no succeeded units, no tag");
debug::warning("Some unit backup is failed, no tag");
reset(head.sha());
}
return res;
}
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Expand Up @@ -42,6 +42,7 @@ ENDMACRO(UNIT_IMPL)

UNIT_IMPL(unit1)
UNIT_IMPL(unit2)
install(PROGRAMS unit_fail_vault_test DESTINATION ${TESTS_DIR})

configure_file(tests.xml.in tests.xml @ONLY)
install(FILES tests.xml DESTINATION ${TESTS_DIR})
Expand Down
4 changes: 4 additions & 0 deletions tests/unit_fail_vault_test
@@ -0,0 +1,4 @@
#!/bin/sh
echo "SHOULD FAIL"
echo "FAIL UNIT is FAILED" >&2
exit 1
48 changes: 46 additions & 2 deletions tests/vault.cpp
Expand Up @@ -18,6 +18,7 @@
#include <unistd.h>

namespace os = qtaround::os;
namespace subprocess = qtaround::subprocess;
namespace error = qtaround::error;

namespace tut
Expand All @@ -41,7 +42,8 @@ enum test_ids {
tid_config_update,
tid_simple_blobs,
tid_clear,
tid_cli_backup_restore_several_units
tid_cli_backup_restore_several_units,
tid_backup_fail
};

namespace {
Expand Down Expand Up @@ -298,10 +300,52 @@ void object::test<tid_cli_backup_restore_several_units>()
vault_init();
register_unit(vault_dir, "unit1", false);
register_unit(vault_dir, "unit2", false);
vault::Vault::execute({{"action", "export"}
auto rc = vault::Vault::execute({{"action", "export"}
, {"vault", vault_dir}
, {"home", home}
, {"unit", "unit1,unit2"}});
ensure_eq("Should succeed", rc, 0);
on_exit();
}

template<> template<>
void object::test<tid_backup_fail>()
{
using vault::Vault;
auto on_exit = setup(tid_backup_fail);
auto describe = []() {
subprocess::Process ps;
ps.setWorkingDirectory(vault_dir);
ps.start("git", {"describe", "--tags", "--exact-match"});
return std::move(ps);
};
auto last_snapshot = []() {
return vlt->snapshots().last().name();
};
os::rmtree(home);
os::mkdir(home);
vault_init();
register_unit(vault_dir, "unit1", false);
register_unit(vault_dir, "unit2", false);
auto rc = Vault::execute({{"action", "export"}
, {"vault", vault_dir}
, {"home", home}
, {"unit", "unit1,unit2"}});
ensure_eq("These units should proceed", rc, 0);

auto snap1 = last_snapshot();

register_unit(vault_dir, "unit_fail", false);
rc = Vault::execute({{"action", "export"}
, {"vault", vault_dir}
, {"home", home}
, {"unit", "unit1,unit2,unit_fail"}});
ensure_eq("With failed unit it should fail", rc, 1);

auto ps = describe();
ensure("Git is not found", ps.wait(10));
ensure_eq("There should be a previous tag on top", ps.rc(), 0);
ensure_eq("Should be the same snapshot as before", snap1, last_snapshot());
on_exit();
}

Expand Down

0 comments on commit ae46fe4

Please sign in to comment.