Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ServiceManager: add subservice selection support #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/MilkCheck/Engine/ServiceGroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def has_subservice(self, name):
"""
Check if the service is referenced within the group
"""
return name in self._subservices
return self.search(name) is not None

def has_action(self, action_name):
"""
Expand Down Expand Up @@ -202,6 +202,27 @@ def __update_edges(self, create_links=False):
elif abs_ser is self._sink and len(dep.target.parents) > 1:
dep.target.remove_dep('sink')

def cleanup_unused(self, services):
"""
Cleanup unused dependencies recursively
"""
parent = not self._algo_reversed
if parent:
spot = self._source
else:
spot = self._sink

# Remove unused services
for service in list(spot.deps().keys()):
for target_service in services:
if '.' in target_service:
subservice, subservice_tree = target_service.split('.', 1)
self._subservices[subservice].cleanup_unused([subservice_tree])
if subservice != service:
spot.remove_dep(service, parent=parent)
else:
if target_service != service:
spot.remove_dep(service, parent=parent)

def remove_inter_dep(self, dep_name):
"""
Expand Down
6 changes: 2 additions & 4 deletions lib/MilkCheck/ServiceManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,11 @@ def select_services(self, services):
spot = self._sink

# Remove unused services
for service in list(spot.deps().keys()):
if service not in services:
spot.remove_dep(service, parent=parent)
self.cleanup_unused(services)
# Add direct link to important services
for service in services:
if service not in spot.deps():
svc = self._subservices[service]
svc = self.search(service)
spot.add_dep(svc, parent=parent)

def _disable_deps(self):
Expand Down
25 changes: 25 additions & 0 deletions tests/MilkCheckTests/ServiceManagerTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,28 @@ def test_tagged_run(self):
self.assertFalse(srv.to_skip('start'))
manager._apply_config({'tags': set(['bar'])})
self.assertTrue(srv.to_skip('start'))

def test_subservice_lauch(self):
""" Test that we can launch an action on a subservice """
manager = ServiceManager()
sergrp = ServiceGroup('S1')
sergrp.fromdict(
{'services':
{'srv1':
{'actions':
{'start': {'cmd':'/bin/True'}},
'desc': "I'm the service srv1"
},
'subgroup':
{'services':
{'subservice':
{,
'actions':
{'start': {'cmd': '/bin/True'}},
'desc': 'I am the subservice $NAME'},
'desc': "I'm the service $NAME"}},
}})
manager.add_service(sergrp)
manager.call_services(['subgroup.subservice'], 'start')
self.assertEqual(sergrp['subgroup']['subservice'].status, DONE)
self.assertEqual(sergrp['srv1'].status, DONE)