Skip to content

Commit

Permalink
Merge pull request #2 from mfalesni/openstack-vm-error
Browse files Browse the repository at this point in the history
Let us know about ERROR state during OS provisioning.
  • Loading branch information
psav committed Nov 3, 2015
2 parents 944652f + d4b2eff commit e786447
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
*.pyc
.cache/
.coverage
8 changes: 8 additions & 0 deletions .travis.yml
@@ -0,0 +1,8 @@
language: python
python:
- "2.7"
install:
- "pip install -Ur requirements-test.txt"
script: "py.test tests/ -v --cov mgmtsystem"
after_success:
- coveralls
7 changes: 6 additions & 1 deletion README.md
@@ -1 +1,6 @@
MGMT_SYSTEM
mgmtsystem - A simple virtualization client
===========================================

[![Coverage Status](https://coveralls.io/repos/RedHatQE/mgmtsystem/badge.svg?branch=master&service=github)](https://coveralls.io/github/RedHatQE/mgmtsystem?branch=master)
[![Build Status](https://travis-ci.org/RedHatQE/mgmtsystem.svg)](https://travis-ci.org/RedHatQE/mgmtsystem)

4 changes: 4 additions & 0 deletions mgmtsystem/exceptions.py
Expand Up @@ -71,3 +71,7 @@ class VMNotFoundViaIP(Exception):

class HostNotRemoved(Exception):
"""Raised when :py:mod:`utils.mgmt_system` fails to remove host from cluster"""


class VMError(Exception):
"""Raised when a VM goes to the ERROR state."""
16 changes: 14 additions & 2 deletions mgmtsystem/openstack.py
Expand Up @@ -20,7 +20,8 @@

from base import MgmtSystemAPIBase, VMInfo
from exceptions import (
NoMoreFloatingIPs, NetworkNameNotFound, VMInstanceNotFound, VMNotFoundViaIP, ActionTimedOutError
NoMoreFloatingIPs, NetworkNameNotFound, VMInstanceNotFound, VMNotFoundViaIP, ActionTimedOutError,
VMError
)


Expand Down Expand Up @@ -248,7 +249,18 @@ def disconnect(self):
pass

def vm_status(self, vm_name):
return self._find_instance_by_name(vm_name).status
"""Retrieve Instance status.
Raises:
:py:class:`mgmtsystem.exceptions.VMError
"""
inst = self._find_instance_by_name(vm_name)
if inst.status != "ERROR":
return inst.status
if not hasattr(inst, "fault"):
raise VMError("Instance {} in error state!".format(vm_name))
raise VMError("Instance {} error {}: {} | {}".format(
vm_name, inst.fault["code"], inst.fault["message"], inst.fault["created"]))

def create_volume(self, size_gb, **kwargs):
volume = self.capi.volumes.create(size_gb, **kwargs).id
Expand Down
5 changes: 5 additions & 0 deletions requirements-test.txt
@@ -0,0 +1,5 @@
-r ./requirements.txt

pytest
pytest-cov
coveralls
1 change: 1 addition & 0 deletions tests/__init__.py
@@ -0,0 +1 @@
# -*- coding: utf-8 -*-
53 changes: 53 additions & 0 deletions tests/test_openstack.py
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
"""Unit tests for Openstack client."""
import pytest

from mgmtsystem import exceptions, openstack


@pytest.fixture(scope="function")
def provider():
os = openstack.OpenstackSystem(tenant=None, username=None, password=None, auth_url=None)
return os


class mkobj(object):
def __init__(self, **d):
self.__dict__ = d


def test_vm_status_error_raises_with_fault(provider, monkeypatch):
"""Check that if the Instance gets to the ERROR state, vm_status will let us know."""
def _find_instance_by_name(vm_name):
return mkobj(
status="ERROR",
fault={"code": 500, "created": "2015-11-02T10:54:18Z", "details": "x", "message": "y"})

monkeypatch.setattr(provider, '_find_instance_by_name', _find_instance_by_name)

with pytest.raises(exceptions.VMError):
provider.vm_status("xyz")


def test_vm_status_error_raises_without_fault(provider, monkeypatch):
"""Check that if the Instance gets to the ERROR state, vm_status will let us know.
With no fault field.
"""
def _find_instance_by_name(vm_name):
return mkobj(status="ERROR")

monkeypatch.setattr(provider, '_find_instance_by_name', _find_instance_by_name)

with pytest.raises(exceptions.VMError):
provider.vm_status("xyz")


def test_vm_status_no_error(provider, monkeypatch):
"""Check that if the Instance is not in error state, it works as usual."""
def _find_instance_by_name(vm_name):
return mkobj(status="UP")

monkeypatch.setattr(provider, '_find_instance_by_name', _find_instance_by_name)

assert provider.vm_status("xyz") == "UP"

0 comments on commit e786447

Please sign in to comment.