Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Fix HyperV warning events (#4353)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfranciszkiewicz committed Jun 17, 2019
1 parent 7e526a5 commit fe81243
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
28 changes: 14 additions & 14 deletions golem/docker/hypervisor/hyperv.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,33 @@
logger = logging.getLogger(__name__)


class events(Enum):
class Events(Enum):
SMB = 'smb_blocked'
MEM = 'lowered_memory'
DISK = 'low_diskspace'


MESSAGES = {
events.SMB: 'Port {SMB_PORT} unreachable. Please check firewall settings.',
events.MEM: 'Not enough free RAM to start the VM, '
Events.SMB: 'Port {SMB_PORT} unreachable. Please check firewall settings.',
Events.MEM: 'Not enough free RAM to start the VM, '
'lowering memory to {mem_mb} MB',
events.DISK: 'Not enough disk space. Creating VM with min memory',
Events.DISK: 'Not enough disk space. Creating VM with min memory',
}

EVENTS: Dict[events, Dict[str, Union[str, Optional[Dict]]]] = {
events.SMB: {
EVENTS: Dict[Events, Dict[str, Union[str, Optional[Dict]]]] = {
Events.SMB: {
'component': Component.hypervisor,
'method': 'setup',
'stage': Stage.exception,
'data': None,
},
events.MEM: {
Events.MEM: {
'component': Component.hypervisor,
'method': 'start_vm',
'stage': Stage.warning,
'data': None,
},
events.DISK: {
Events.DISK: {
'component': Component.hypervisor,
'method': 'start_vm',
'stage': Stage.warning,
Expand Down Expand Up @@ -119,7 +119,7 @@ def _check_smb_port(self) -> None:
# We use splitlines() because output may contain multiple lines with
# debug information
if output is None or ok_str not in output.splitlines():
self._log_and_publish_event(events.SMB, SMB_PORT=self.SMB_PORT)
self._log_and_publish_event(Events.SMB, SMB_PORT=self.SMB_PORT)

@report_calls(Component.hypervisor, 'vm.save')
def save_vm(self, vm_name: Optional[str] = None) -> None:
Expand Down Expand Up @@ -173,7 +173,7 @@ def start_vm(self, name: Optional[str] = None) -> None:
max_memory = self._memory_cap(constr[mem_key])
constr[mem_key] = hardware.cap_memory(constr[mem_key], max_memory,
unit=hardware.MemSize.mebi)
self._log_and_publish_event(events.MEM, mem_mb=constr[mem_key])
self._log_and_publish_event(Events.MEM, mem_mb=constr[mem_key])

# Always constrain to set the appropriate shutdown action
self.constrain(name, **constr)
Expand Down Expand Up @@ -399,13 +399,13 @@ def _get_max_memory(self, constr: Optional[dict] = None) -> int:
return hardware.pad_memory(int(0.9 * max_mem_in_mb))

@staticmethod
def _log_and_publish_event(name, **kwargs) -> None:
event = EVENTS[name].copy()
def _log_and_publish_event(event_type: Events, **kwargs) -> None:
event = EVENTS[event_type].copy()
data = next(iter(kwargs.values()))
message = MESSAGES[name].format(**kwargs)
message = MESSAGES[event_type].format(**kwargs)

if event['stage'] == Stage.warning:
event['data'] = {"status": name, "value": data}
event['data'] = {"status": event_type.value, "value": data}
logger.warning(message)
else:
event['data'] = message
Expand Down
24 changes: 23 additions & 1 deletion tests/golem/docker/test_hyperv.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from os_win.exceptions import OSWinException

from golem.docker.config import DOCKER_VM_NAME, MIN_CONSTRAINTS, CONSTRAINT_KEYS
from golem.docker.hypervisor.hyperv import HyperVHypervisor
from golem.docker.hypervisor.hyperv import HyperVHypervisor, Events, MESSAGES, \
EVENTS
from golem.docker.task_thread import DockerBind


Expand Down Expand Up @@ -339,3 +340,24 @@ def _fail_to_start(_vm_name, state):
))
start_vm.assert_called_once_with('test')
logger.exception.assert_called_once()

def test_log_and_publish_error(self):
params = {'SMB_PORT': 443}
expected = dict(EVENTS[Events.SMB])
expected['data'] = MESSAGES[Events.SMB].format(**params)

with patch(self.PATCH_BASE + '.publish_event') as publish_event:
self.hyperv._log_and_publish_event(Events.SMB, **params)
publish_event.assert_called_with(expected)

def test_log_and_publish_warning(self):
params = {'mem_mb': 2048}
expected = dict(EVENTS[Events.MEM])
expected['data'] = {
'status': Events.MEM.value,
'value': 2048,
}

with patch(self.PATCH_BASE + '.publish_event') as publish_event:
self.hyperv._log_and_publish_event(Events.MEM, **params)
publish_event.assert_called_with(expected)

0 comments on commit fe81243

Please sign in to comment.