Skip to content

Commit

Permalink
add more unit tests and add opion max_vm_total_duration to set a tota…
Browse files Browse the repository at this point in the history
…l maximum life of VM above which user cannot extend VM anymore
  • Loading branch information
osallou committed Dec 15, 2017
1 parent b2a7f2d commit 9823726
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 1 deletion.
6 changes: 6 additions & 0 deletions doc/source/development/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ Release notes are managed via reno
.. code-block:: bash
tox -e releasenotes
To add a release note:

.. code-block:: bash
reno -new XXXX
3 changes: 3 additions & 0 deletions doc/source/install/common_configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
# Maximum life extend of VM in days (integer value)
#max_vm_extend = 30
# Maximum life of VM in days, whatever the extends
#max_vm_total_duration = 365
[database]
# SQLAlchemy connection string for the reference implementation
# registry server. Any valid SQLAlchemy connection string is fine.
Expand Down
7 changes: 6 additions & 1 deletion os_vm_expire/api/controllers/vmexpire.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ def on_get(self, meta, instance_id=None):
@controllers.enforce_rbac('vmexpire:extend')
@controllers.enforce_content_types(['application/json'])
def on_put(self, meta, instance_id):
instance = self.vmexpire_repo.extend_vm(entity_id=instance_id)
instance = None
try:
instance = self.vmexpire_repo.extend_vm(entity_id=instance_id)
except Exception as e:
pecan.response.status = 403
return str(e)
# url = hrefs.convert_vmexpire_to_href(instance.id)
repo.commit()
pecan.response.status = 202
Expand Down
4 changes: 4 additions & 0 deletions os_vm_expire/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

MAX_VM_DURATION_DAYS = 60
MAX_VM_EXTEND_DAYS = 30
MAX_VM_TOTAL_DURATION_DAYS = 365

KS_NOTIFICATIONS_GRP_NAME = "nova_notifications"

Expand Down Expand Up @@ -172,6 +173,9 @@
cfg.IntOpt('max_vm_extend',
default=MAX_VM_EXTEND_DAYS,
help=u._("Maximum life extend of VM in days")),
cfg.IntOpt('max_vm_total_duration',
default=MAX_VM_TOTAL_DURATION_DAYS,
help=u._("Maximum life of VM in days, whatever the extends")),
]

host_opts = [
Expand Down
10 changes: 10 additions & 0 deletions os_vm_expire/model/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,15 @@ def extend_vm(self, entity_id, session=None):
session)

entity = query.one()

entity.expire = int(
time.mktime(datetime.datetime.now().timetuple()) +
CONF.max_vm_extend * 3600 * 24
)

if entity.created_at + datetime.timedelta(days=CONF.max_vm_total_duration) < datetime.datetime.fromtimestamp(entity.expire):
_raise_entity_max_extend_reached(entity.id)
return None
entity.notified = False
entity.notified_last = False
entity.save(session=session)
Expand Down Expand Up @@ -553,6 +558,11 @@ def _get_repository(global_ref, repo_class):
return global_ref


def _raise_entity_max_extend_reached(entity_id):
raise Exception(u._("VM reached its maximum life, {id}, cannot extend it").format(
id=entity_id))


def _raise_entity_not_found(entity_name, entity_id):
raise Exception(u._("No {entity} found with ID {id}").format(
entity=entity_name,
Expand Down
15 changes: 15 additions & 0 deletions os_vm_expire/tests/api/controllers/test_vmexpires.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ class WhenTestingVmExpiresResource(utils.OsVMExpireAPIBaseTestCase):

def setUp(self):
super(WhenTestingVmExpiresResource, self).setUp()
self.max_vm_total_duration = repositories.CONF.max_vm_total_duration

def tearDown(self):
super(WhenTestingVmExpiresResource, self).tearDown()
repo = repositories.get_vmexpire_repository()
repo.delete_all_entities()
repositories.commit()
repositories.CONF.max_vm_total_duration = self.max_vm_total_duration

def test_can_get_vmexpires(self):
entity = create_vmexpire_model()
Expand Down Expand Up @@ -75,6 +77,19 @@ def test_can_extend_vmexpire(self):
new_expire > prev_expire
)

def test_cannot_extend_max_reached(self):
repositories.CONF.max_vm_total_duration = 3
entity = create_vmexpire_model()
instance = create_vmexpire(entity)
_get_existing_resp = self.app.get(
'/'+ entity.project_id +'/vmexpires/' + instance.id
)
_get_resp = self.app.put(
'/'+ entity.project_id +'/vmexpires/' + instance.id,
headers={'Content-Type': 'application/json'},
status=403
)

def test_extend_reset_notified_vmexpire(self):
entity = create_vmexpire_model()
instance = create_vmexpire(entity)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features:
- |
Add max_vm_total_duration in configuration file.
This parameter defines a maximum VM duration above which user cannot extend
the VM anymore, defaults to 365 days.

0 comments on commit 9823726

Please sign in to comment.