From 7fdbfcbeb24c1b21fd51838c92d318ba066d36ac Mon Sep 17 00:00:00 2001
From: Ivo De Decker <ivodd@debian.org>
Date: Mon, 2 Sep 2019 19:37:13 +0000
Subject: [PATCH] Check version of cruft removal item

If the cruft removal item has a different version than the binary currently in
testing, then the cruft item was replaced since it was added, or it was added
in error. In this case, the item should not be processed.

Signed-off-by: Ivo De Decker <ivodd@debian.org>
---
 britney.py            | 13 +++++++++----
 britney2/migration.py |  6 ++++++
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/britney.py b/britney.py
index 2cf39b7..4dcf1d2 100755
--- a/britney.py
+++ b/britney.py
@@ -878,10 +878,15 @@ class Britney(object):
                                 target_suite.check_suite_source_pkg_consistency('iter_packages after commit')
                             nuninst_last_accepted = nuninst_after
                             for cruft_item in new_cruft:
-                                _, updates, rms, _ = mm.compute_groups(cruft_item)
-                                result = (cruft_item, frozenset(updates), frozenset(rms))
-                                group_info[cruft_item] = result
-                            worklist.extend([x] for x in new_cruft)
+                                try:
+                                    _, updates, rms, _ = mm.compute_groups(cruft_item)
+                                    result = (cruft_item, frozenset(updates), frozenset(rms))
+                                    group_info[cruft_item] = result
+                                    worklist.append([cruft_item])
+                                except MigrationConstraintException as e:
+                                    output_logger.info(
+                                        "    got exception adding cruft item %s to list: %s" %
+                                        (cruft_item.uvname, repr(e)))
                             rescheduled_packages.extend(maybe_rescheduled_packages)
                             maybe_rescheduled_packages.clear()
                         else:
diff --git a/britney2/migration.py b/britney2/migration.py
index cb33b77..becbfcc 100644
--- a/britney2/migration.py
+++ b/britney2/migration.py
@@ -112,6 +112,12 @@ class MigrationManager(object):
         else:
             assert item_package in binaries_t[item.architecture]
             pkg_id = binaries_t[item.architecture][item_package].pkg_id
+            binary, ver, parch = pkg_id
+            if ver != item.version:
+                raise MigrationConstraintException(
+                    "trying cruft removal item %s, while %s has %s/%s on %s" % (
+                        item, target_suite.name,
+                        binary, ver, parch))
             source_name = binaries_t[item.architecture][item_package].source
             rms = {pkg_id}
             smoothbins = set()