dcavalca / rpms / libdnf

Forked from rpms/libdnf 2 years ago
Clone

Blame SOURCES/0001-history-Fix-dnf-history-rollback-when-a-package-was-removed-RhBug1683134.patch

c06cc3
From a96d701f7f55ff475e11ac9cda63b81c31c54e7a Mon Sep 17 00:00:00 2001
c06cc3
From: Daniel Mach <dmach@redhat.com>
c06cc3
Date: Wed, 6 May 2020 08:34:46 +0200
c06cc3
Subject: [PATCH] history: Fix dnf history rollback when a package was removed
c06cc3
 (RhBug:1683134)
c06cc3
c06cc3
---
c06cc3
 libdnf/transaction/MergedTransaction.cpp      |  25 +++-
c06cc3
 .../transaction/MergedTransactionTest.cpp     | 120 ++++++++++++++++--
c06cc3
 .../transaction/MergedTransactionTest.hpp     |   6 +-
c06cc3
 3 files changed, 137 insertions(+), 14 deletions(-)
c06cc3
c06cc3
diff --git a/libdnf/transaction/MergedTransaction.cpp b/libdnf/transaction/MergedTransaction.cpp
c06cc3
index a7c06ffa9..a8d878cb5 100644
c06cc3
--- a/libdnf/transaction/MergedTransaction.cpp
c06cc3
+++ b/libdnf/transaction/MergedTransaction.cpp
c06cc3
@@ -19,6 +19,7 @@
c06cc3
  */
c06cc3
 
c06cc3
 #include "MergedTransaction.hpp"
c06cc3
+#include <algorithm>
c06cc3
 #include <vector>
c06cc3
 
c06cc3
 namespace libdnf {
c06cc3
@@ -171,6 +172,21 @@ MergedTransaction::getConsoleOutput()
c06cc3
     return output;
c06cc3
 }
c06cc3
 
c06cc3
+
c06cc3
+static bool transaction_item_sort_function(const std::shared_ptr<TransactionItemBase> lhs, const std::shared_ptr<TransactionItemBase> rhs) {
c06cc3
+    if (lhs->isForwardAction() && rhs->isForwardAction()) {
c06cc3
+        return false;
c06cc3
+    }
c06cc3
+    if (lhs->isBackwardAction() && rhs->isBackwardAction()) {
c06cc3
+        return false;
c06cc3
+    }
c06cc3
+    if (lhs->isBackwardAction()) {
c06cc3
+        return true;
c06cc3
+    }
c06cc3
+    return false;
c06cc3
+}
c06cc3
+
c06cc3
+
c06cc3
 /**
c06cc3
  * Get list of transaction items involved in the merged transaction
c06cc3
  * Actions are merged using following rules:
c06cc3
@@ -203,6 +219,9 @@ MergedTransaction::getItems()
c06cc3
     // iterate over transaction
c06cc3
     for (auto t : transactions) {
c06cc3
         auto transItems = t->getItems();
c06cc3
+        // sort transaction items by their action type - forward/backward
c06cc3
+        // this fixes behavior of the merging algorithm in several edge cases
c06cc3
+        std::sort(transItems.begin(), transItems.end(), transaction_item_sort_function);
c06cc3
         // iterate over transaction items
c06cc3
         for (auto transItem : transItems) {
c06cc3
             // get item and its type
c06cc3
@@ -383,10 +402,6 @@ MergedTransaction::mergeItem(ItemPairMap &itemPairMap, TransactionItemBasePtr mT
c06cc3
     auto firstState = previousItemPair.first->getAction();
c06cc3
     auto newState = mTransItem->getAction();
c06cc3
 
c06cc3
-    if (firstState == TransactionItemAction::INSTALL && mTransItem->isBackwardAction()) {
c06cc3
-        return;
c06cc3
-    }
c06cc3
-
c06cc3
     switch (firstState) {
c06cc3
         case TransactionItemAction::REMOVE:
c06cc3
         case TransactionItemAction::OBSOLETED:
c06cc3
@@ -399,6 +414,8 @@ MergedTransaction::mergeItem(ItemPairMap &itemPairMap, TransactionItemBasePtr mT
c06cc3
                 // Install -> Remove = (nothing)
c06cc3
                 itemPairMap.erase(name);
c06cc3
                 break;
c06cc3
+            } else if (mTransItem->isBackwardAction()) {
c06cc3
+                break;
c06cc3
             }
c06cc3
             // altered -> transfer install to the altered package
c06cc3
             mTransItem->setAction(TransactionItemAction::INSTALL);
c06cc3
diff --git a/tests/libdnf/transaction/MergedTransactionTest.cpp b/tests/libdnf/transaction/MergedTransactionTest.cpp
c06cc3
index 90ad182cf..52507700b 100644
c06cc3
--- a/tests/libdnf/transaction/MergedTransactionTest.cpp
c06cc3
+++ b/tests/libdnf/transaction/MergedTransactionTest.cpp
c06cc3
@@ -700,7 +700,7 @@ MergedTransactionTest::test_downgrade()
c06cc3
 }
c06cc3
 
c06cc3
 void
c06cc3
-MergedTransactionTest::test_install_downgrade()
c06cc3
+MergedTransactionTest::test_install_downgrade_upgrade_remove()
c06cc3
 {
c06cc3
     auto trans1 = std::make_shared< libdnf::swdb_private::Transaction >(conn);
c06cc3
     trans1->addItem(
c06cc3
@@ -724,19 +724,123 @@ MergedTransactionTest::test_install_downgrade()
c06cc3
         TransactionItemReason::USER
c06cc3
     );
c06cc3
 
c06cc3
+    auto trans3 = std::make_shared< libdnf::swdb_private::Transaction >(conn);
c06cc3
+    trans3->addItem(
c06cc3
+        nevraToRPMItem(conn, "tour-0:4.6-1.noarch"),
c06cc3
+        "repo2",
c06cc3
+        TransactionItemAction::UPGRADED,
c06cc3
+        TransactionItemReason::USER
c06cc3
+    );
c06cc3
+    trans3->addItem(
c06cc3
+        nevraToRPMItem(conn, "tour-0:4.8-1.noarch"),
c06cc3
+        "repo1",
c06cc3
+        TransactionItemAction::UPGRADE,
c06cc3
+        TransactionItemReason::USER
c06cc3
+    );
c06cc3
+
c06cc3
+    auto trans4 = std::make_shared< libdnf::swdb_private::Transaction >(conn);
c06cc3
+    trans4->addItem(
c06cc3
+        nevraToRPMItem(conn, "tour-0:4.8-1.noarch"),
c06cc3
+        "repo1",
c06cc3
+        TransactionItemAction::REMOVE,
c06cc3
+        TransactionItemReason::USER
c06cc3
+    );
c06cc3
+
c06cc3
     MergedTransaction merged(trans1);
c06cc3
+
c06cc3
+    // test merging trans1, trans2
c06cc3
     merged.merge(trans2);
c06cc3
+    auto items2 = merged.getItems();
c06cc3
+    CPPUNIT_ASSERT_EQUAL(1, (int)items2.size());
c06cc3
+    auto item2 = items2.at(0);
c06cc3
+    CPPUNIT_ASSERT_EQUAL(std::string("tour-4.6-1.noarch"), item2->getItem()->toStr());
c06cc3
+    CPPUNIT_ASSERT_EQUAL(std::string("repo2"), item2->getRepoid());
c06cc3
+    CPPUNIT_ASSERT_EQUAL(TransactionItemAction::INSTALL, item2->getAction());
c06cc3
+    CPPUNIT_ASSERT_EQUAL(TransactionItemReason::USER, item2->getReason());
c06cc3
 
c06cc3
-    auto items = merged.getItems();
c06cc3
-    CPPUNIT_ASSERT_EQUAL(1, (int)items.size());
c06cc3
+    // test merging trans1, trans2, trans3
c06cc3
+    merged.merge(trans3);
c06cc3
+    auto items3 = merged.getItems();
c06cc3
+    CPPUNIT_ASSERT_EQUAL(1, (int)items3.size());
c06cc3
+    auto item3 = items3.at(0);
c06cc3
+    CPPUNIT_ASSERT_EQUAL(std::string("tour-4.8-1.noarch"), item3->getItem()->toStr());
c06cc3
+    CPPUNIT_ASSERT_EQUAL(std::string("repo1"), item3->getRepoid());
c06cc3
+    CPPUNIT_ASSERT_EQUAL(TransactionItemAction::INSTALL, item3->getAction());
c06cc3
+    CPPUNIT_ASSERT_EQUAL(TransactionItemReason::USER, item3->getReason());
c06cc3
 
c06cc3
-    auto item = items.at(0);
c06cc3
-    CPPUNIT_ASSERT_EQUAL(std::string("tour-4.6-1.noarch"), item->getItem()->toStr());
c06cc3
-    CPPUNIT_ASSERT_EQUAL(std::string("repo2"), item->getRepoid());
c06cc3
-    CPPUNIT_ASSERT_EQUAL(TransactionItemAction::INSTALL, item->getAction());
c06cc3
-    CPPUNIT_ASSERT_EQUAL(TransactionItemReason::USER, item->getReason());
c06cc3
+    // test merging trans1, trans2, trans3, trans4
c06cc3
+    merged.merge(trans4);
c06cc3
+    auto items4 = merged.getItems();
c06cc3
+    CPPUNIT_ASSERT_EQUAL(0, (int)items4.size());
c06cc3
+    // trans4 removes the package, empty output is expected
c06cc3
 }
c06cc3
 
c06cc3
+
c06cc3
+void
c06cc3
+MergedTransactionTest::test_downgrade_upgrade_remove()
c06cc3
+{
c06cc3
+    auto trans1 = std::make_shared< libdnf::swdb_private::Transaction >(conn);
c06cc3
+    trans1->addItem(
c06cc3
+        nevraToRPMItem(conn, "tour-0:4.6-1.noarch"),
c06cc3
+        "repo2",
c06cc3
+        TransactionItemAction::DOWNGRADE,
c06cc3
+        TransactionItemReason::USER
c06cc3
+    );
c06cc3
+    trans1->addItem(
c06cc3
+        nevraToRPMItem(conn, "tour-0:4.8-1.noarch"),
c06cc3
+        "repo1",
c06cc3
+        TransactionItemAction::DOWNGRADED,
c06cc3
+        TransactionItemReason::USER
c06cc3
+    );
c06cc3
+
c06cc3
+    // items are in reversed order than in test_install_downgrade_upgrade_remove()
c06cc3
+    // fixing this required ordering transaction items by forward/backward action
c06cc3
+    auto trans2 = std::make_shared< libdnf::swdb_private::Transaction >(conn);
c06cc3
+    trans2->addItem(
c06cc3
+        nevraToRPMItem(conn, "tour-0:4.8-1.noarch"),
c06cc3
+        "repo1",
c06cc3
+        TransactionItemAction::UPGRADE,
c06cc3
+        TransactionItemReason::USER
c06cc3
+    );
c06cc3
+    trans2->addItem(
c06cc3
+        nevraToRPMItem(conn, "tour-0:4.6-1.noarch"),
c06cc3
+        "repo2",
c06cc3
+        TransactionItemAction::UPGRADED,
c06cc3
+        TransactionItemReason::USER
c06cc3
+    );
c06cc3
+
c06cc3
+    auto trans3 = std::make_shared< libdnf::swdb_private::Transaction >(conn);
c06cc3
+    trans3->addItem(
c06cc3
+        nevraToRPMItem(conn, "tour-0:4.8-1.noarch"),
c06cc3
+        "repo1",
c06cc3
+        TransactionItemAction::REMOVE,
c06cc3
+        TransactionItemReason::USER
c06cc3
+    );
c06cc3
+
c06cc3
+    MergedTransaction merged(trans1);
c06cc3
+
c06cc3
+    // test merging trans1, trans2
c06cc3
+    merged.merge(trans2);
c06cc3
+    auto items2 = merged.getItems();
c06cc3
+    CPPUNIT_ASSERT_EQUAL(1, (int)items2.size());
c06cc3
+    auto item2 = items2.at(0);
c06cc3
+    CPPUNIT_ASSERT_EQUAL(std::string("tour-4.8-1.noarch"), item2->getItem()->toStr());
c06cc3
+    CPPUNIT_ASSERT_EQUAL(std::string("repo1"), item2->getRepoid());
c06cc3
+    CPPUNIT_ASSERT_EQUAL(TransactionItemAction::REINSTALL, item2->getAction());
c06cc3
+    CPPUNIT_ASSERT_EQUAL(TransactionItemReason::USER, item2->getReason());
c06cc3
+
c06cc3
+    // test merging trans1, trans2, trans3
c06cc3
+    merged.merge(trans3);
c06cc3
+    auto items3 = merged.getItems();
c06cc3
+    CPPUNIT_ASSERT_EQUAL(1, (int)items3.size());
c06cc3
+    auto item3 = items3.at(0);
c06cc3
+    CPPUNIT_ASSERT_EQUAL(std::string("tour-4.8-1.noarch"), item3->getItem()->toStr());
c06cc3
+    CPPUNIT_ASSERT_EQUAL(std::string("repo1"), item3->getRepoid());
c06cc3
+    CPPUNIT_ASSERT_EQUAL(TransactionItemAction::REMOVE, item3->getAction());
c06cc3
+    CPPUNIT_ASSERT_EQUAL(TransactionItemReason::USER, item3->getReason());
c06cc3
+}
c06cc3
+
c06cc3
+
c06cc3
 void
c06cc3
 MergedTransactionTest::test_multilib_identity()
c06cc3
 {
c06cc3
diff --git a/tests/libdnf/transaction/MergedTransactionTest.hpp b/tests/libdnf/transaction/MergedTransactionTest.hpp
c06cc3
index 9f1ed660a..77585e865 100644
c06cc3
--- a/tests/libdnf/transaction/MergedTransactionTest.hpp
c06cc3
+++ b/tests/libdnf/transaction/MergedTransactionTest.hpp
c06cc3
@@ -26,7 +26,8 @@ class MergedTransactionTest : public CppUnit::TestCase {
c06cc3
     CPPUNIT_TEST(test_add_obsoleted_obsoleted);
c06cc3
 
c06cc3
     CPPUNIT_TEST(test_downgrade);
c06cc3
-    CPPUNIT_TEST(test_install_downgrade);
c06cc3
+    CPPUNIT_TEST(test_install_downgrade_upgrade_remove);
c06cc3
+    CPPUNIT_TEST(test_downgrade_upgrade_remove);
c06cc3
 
c06cc3
     CPPUNIT_TEST(test_multilib_identity);
c06cc3
 
c06cc3
@@ -56,7 +57,8 @@ class MergedTransactionTest : public CppUnit::TestCase {
c06cc3
     // END: tests ported from DNF unit tests
c06cc3
 
c06cc3
     void test_downgrade();
c06cc3
-    void test_install_downgrade();
c06cc3
+    void test_install_downgrade_upgrade_remove();
c06cc3
+    void test_downgrade_upgrade_remove();
c06cc3
 
c06cc3
     void test_multilib_identity();
c06cc3
 private: