Skip to content

Commit

Permalink
Fix unused component detection
Browse files Browse the repository at this point in the history
There was a bug in unused component detection which was triggered occasionally. It is not entirely clear what triggered it, but it "helped" to have a lot of components.
  • Loading branch information
zagy committed Nov 18, 2024
1 parent 6cc569b commit d1c7a03
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGES.d/20241118_114301_cz_regression.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Fix unused component detection

There was a bug in unused component detection which was triggered occasionally. It is not entirely clear what triggered it, but it "helped" to have a lot of components.
11 changes: 5 additions & 6 deletions src/batou/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,9 @@ class UnsatisfiedResources(ConfigurationError):
def from_context(cls, resources):
self = cls()
self.unsatisfied_resources = []
for key in sorted(resources.keys()):
for (key, host), res_for_key in sorted(resources.items()):
self.unsatisfied_resources.append(
(key, [r.name for r in resources[key]])
(key, host, [r.name for r in res_for_key])
)
return self

Expand All @@ -578,11 +578,10 @@ def __str__(self):

def report(self):
output.error("Unsatisfied resource requirements")
for key, resources in self.unsatisfied_resources:
for key, host, resources in self.unsatisfied_resources:
output.line(
' Resource "{}" required by {}'.format(
key, ",".join(resources)
),
f' Resource "{key}" at host {host} required by '
f'{",".join(resources)}',
red=True,
)

Expand Down
14 changes: 7 additions & 7 deletions src/batou/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def unsatisfied(self):
if not any(s.strict for s in subscribers):
continue
if key not in self.resources:
unsatisfied.add(key)
unsatisfied.add((key, None))
continue
for s in subscribers:
if s.host is None:
Expand All @@ -163,25 +163,25 @@ def unsatisfied(self):
resource_root.host.name == s.host.name
for resource_root in self.resources[key]
):
unsatisfied.add(key)
unsatisfied.add((key, s.host.name))
break
return unsatisfied

@property
def unsatisfied_components(self):
components = set()
for resource in self.unsatisfied:
for resource, host in self.unsatisfied:
components.update(
[s.root for s in self._subscriptions(resource, None)]
[s.root for s in self._subscriptions(resource, host)]
)
return components

@property
def unsatisfied_keys_and_components(self):
keys = {}
for resource in self.unsatisfied:
keys[resource] = set(
[s.root for s in self._subscriptions(resource, None)]
for resource, host in self.unsatisfied:
keys[(resource, host)] = set(
[s.root for s in self._subscriptions(resource, host)]
)
return keys

Expand Down
5 changes: 4 additions & 1 deletion src/batou/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ def test_mentions_missing_requirement_with_host_requirement(sample_service):
assert len(errors) == 2
assert isinstance(errors[0], UnsatisfiedResources)
assert isinstance(errors[1], NonConvergingWorkingSet)
assert "key" in str(errors[0].__dict__)
assert errors[0].unsatisfied_resources == [
("key", "host1", []),
("unrelated", None, ["component1"]),
]

0 comments on commit d1c7a03

Please sign in to comment.