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

Fix lease get detail #33

Merged
merged 3 commits into from
Apr 16, 2024
Merged
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
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Unit tests

env:
# This should match the default python_version build arg
PYTHON_VERSION: 3.8
TOX_ENV: py38

on:
push:
branches:
- "*"
pull_request:
types: [opened, reopened]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Set up Python 3.x
uses: actions/setup-python@v1
with:
python-version: ${{ env.PYTHON_VERSION }}

- name: Install tox
run: pip install tox

- name: Run tests
run: tox -e ${{ env.TOX_ENV }}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

4 changes: 3 additions & 1 deletion blazarclient/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,9 @@ def get_data(self, parsed_args):
res_id = parsed_args.id

resource_manager = getattr(blazar_client, self.resource)
data = resource_manager.get(res_id, detail=body['detail'])
data = resource_manager.get(res_id)
if body.get('detail'):
data.update(resource_manager.additional_details(res_id))
self.format_output_data(data)
return list(zip(*sorted(data.items())))

Expand Down
52 changes: 50 additions & 2 deletions blazarclient/tests/v1/shell_commands/test_leases.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,33 @@ def test_show_lease(self):
]
mock.seal(lease_manager)

args = argparse.Namespace(id=FIRST_LEASE)
args = argparse.Namespace(id=FIRST_LEASE, detail=False)
expected = [('id',), (FIRST_LEASE,)]

self.assertEqual(show_lease.get_data(args), expected)
lease_manager.get.assert_called_once_with(FIRST_LEASE)

def test_show_lease_with_allocations(self):
show_lease, lease_manager = self.create_show_command()
lease_manager.get.return_value = {'id': FIRST_LEASE}
lease_manager.additional_details.return_value = {
'hosts': [],
'networks': [],
'devices': []
}
lease_manager.list.return_value = [
{'id': FIRST_LEASE, 'name': 'first-lease'},
{'id': SECOND_LEASE, 'name': 'second-lease'},
]
mock.seal(lease_manager)

args = argparse.Namespace(id=FIRST_LEASE, detail=True)
expected = [('devices', 'hosts', 'id', 'networks',), ('', '', FIRST_LEASE, '',)]

self.assertEqual(show_lease.get_data(args), expected)
lease_manager.get.assert_called_once_with(FIRST_LEASE)
lease_manager.additional_details.assert_called_once_with(FIRST_LEASE)

def test_show_lease_by_name(self):
show_lease, lease_manager = self.create_show_command()
lease_manager.list.return_value = [
Expand All @@ -349,13 +370,40 @@ def test_show_lease_by_name(self):
lease_manager.get.return_value = {'id': SECOND_LEASE}
mock.seal(lease_manager)

args = argparse.Namespace(id='second-lease')
args = argparse.Namespace(id='second-lease', detail=False)
expected = [('id',), (SECOND_LEASE,)]

self.assertEqual(show_lease.get_data(args), expected)
lease_manager.list.assert_called_once_with()
lease_manager.get.assert_called_once_with(SECOND_LEASE)

def test_show_lease_by_name_with_allocations(self):
show_lease, lease_manager = self.create_show_command()
lease_manager.list.return_value = [
{'id': FIRST_LEASE, 'name': 'first-lease'},
{'id': SECOND_LEASE, 'name': 'second-lease'},
]
lease_manager.get.return_value = {'id': SECOND_LEASE}
host1 = {'id': '101', 'hypervisor_hostname': 'host-1'}
lease_manager.additional_details.return_value = {
'hosts': [host1],
'networks': [],
'devices': []
}
import json
d = json.dumps(host1, indent=4)
mock.seal(lease_manager)
args = argparse.Namespace(id='second-lease', detail=True)
expected = [
('devices', 'hosts', 'id', 'networks',),
('', d, SECOND_LEASE, '',)
]

self.assertEqual(show_lease.get_data(args), expected)
lease_manager.list.assert_called_once_with()
lease_manager.get.assert_called_once_with(SECOND_LEASE)
lease_manager.additional_details.assert_called_once_with(SECOND_LEASE)


class DeleteLeaseTestCase(tests.TestCase):

Expand Down
25 changes: 14 additions & 11 deletions blazarclient/v1/leases.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ def get(self, lease_id, detail=False):
condition.
"""
resp, body = self.request_manager.get('/leases/%s' % lease_id)
if detail and body['lease']:
with ThreadPoolExecutor() as executor:
# Submit the calls
h_future = executor.submit(self.hosts_in_lease, lease_id)
n_future = executor.submit(self.networks_in_lease, lease_id)
d_future = executor.submit(self.devices_in_lease, lease_id)

# Retrieve the results
body['lease']['hosts'] = h_future.result()
body['lease']['networks'] = n_future.result()
body['lease']['devices'] = d_future.result()
return body['lease']

def update(self, lease_id, name=None, prolong_for=None, reduce_by=None,
Expand Down Expand Up @@ -107,6 +96,20 @@ def list(self, sort_by=None):
leases = sorted(leases, key=lambda l: l[sort_by])
return leases

def additional_details(self, lease_id):
allocations = {}
with ThreadPoolExecutor() as executor:
# Submit the calls
h_future = executor.submit(self.hosts_in_lease, lease_id)
n_future = executor.submit(self.networks_in_lease, lease_id)
d_future = executor.submit(self.devices_in_lease, lease_id)

# Retrieve the results
allocations['hosts'] = h_future.result()
allocations['networks'] = n_future.result()
allocations['devices'] = d_future.result()
return allocations

def hosts_in_lease(self, lease_id):
"""List all hosts in lease"""
resp, body = self.request_manager.get(f'/leases/{lease_id}/hosts')
Expand Down
Loading