def test_to_json(self): """ Test the to_json() helper function. """ class Serializable: """ A test class with a toJSON() method. """ def toJSON(self): # nopep8 pylint: disable=invalid-name """ Fake JSON serialization. """ return vars(self) class NonSerializable: """ A test class without a toJSON() method. """ serializable = Serializable() self.assertEqual(to_json(serializable), '{}') non_serializable = NonSerializable() non_serializable.attr = 42 # pylint: disable=attribute-defined-outside-init self.assertEqual(to_json(non_serializable), '{\n "attr": 42\n}') serializable.attr = non_serializable # pylint: disable=attribute-defined-outside-init self.assertEqual(to_json(serializable), '{\n "attr": "%s"\n}' % (non_serializable, ))
def test_to_json(self): """ Test the to_json() helper function. """ class Serializable: """ A test class with a toJSON() method. """ def toJSON(self): # nopep8 pylint: disable=invalid-name """ Fake JSON serialization. """ return vars(self) class NonSerializable: """ A test class without a toJSON() method. """ serializable = Serializable() self.assertEqual(to_json(serializable), '{}') non_serializable = NonSerializable() non_serializable.attr = 42 # pylint: disable=attribute-defined-outside-init self.assertEqual(to_json(non_serializable), '{\n "attr": 42\n}') serializable.attr = non_serializable # pylint: disable=attribute-defined-outside-init self.assertEqual(to_json(serializable), '{\n "attr": "%s"\n}' % (non_serializable,))
def _update_status_from_nova(self, os_server): """ Update the status from the Nova Server object given in os_server. """ self.logger.debug('Updating status from nova (currently %s):\n%s', self.status, to_json(os_server)) if self.status == Status.Unknown: if os_server.status in ('INITIALIZED', 'BUILDING'): # OpenStack has multiple API versions; INITIALIZED is current; BUILDING was used in the past self._status_to_building() if self.status in (Status.Building, Status.Unknown): self.logger.debug('OpenStack: loaded="%s" status="%s"', os_server._loaded, os_server.status) if os_server._loaded and os_server.status == 'ACTIVE': self._status_to_booting() if self.status in (Status.Booting, Status.Unknown) and self.public_ip and is_port_open( self.public_ip, 22): self._status_to_ready()
def update_status(self): """ Refresh the status by querying the openstack server via nova """ # TODO: Check when server is stopped or terminated # First check if it makes sense to update the current status. # This is not the case if we can not interact with the server: if self.status == Status.BuildFailed: return self.status os_server = self.os_server self.logger.debug('Updating status from nova (currently %s):\n%s', self.status, to_json(os_server)) if self.status == Status.Building: self.logger.debug('OpenStack: loaded="%s" status="%s"', os_server._loaded, os_server.status) if os_server._loaded and os_server.status == 'ACTIVE': self._status_to_booting() elif self.status == Status.Booting and self.public_ip and is_port_open(self.public_ip, 22): self._status_to_ready() return self.status
def update_status(self, provisioning=False, rebooting=False, failed=None): """ Refresh the status by querying the openstack server via nova """ # TODO: Check when server is stopped or terminated os_server = self.os_server self.logger.debug('Updating status from nova (currently %s):\n%s', self.status, to_json(os_server)) if self.status == self.STARTED: self.logger.debug('OpenStack: loaded="%s" status="%s"', os_server._loaded, os_server.status) if os_server._loaded and os_server.status == 'ACTIVE': self._set_status(self.ACTIVE, self.PROGRESS_SUCCESS) else: self._set_status(self.ACTIVE, self.PROGRESS_RUNNING) elif self.status == self.ACTIVE and self.public_ip and is_port_open(self.public_ip, 22): self._set_status(self.BOOTED, self.PROGRESS_RUNNING) elif self.status == self.BOOTED: if provisioning: self._set_status(self.PROVISIONING, self.PROGRESS_RUNNING) else: self._set_status(self.BOOTED, self.PROGRESS_SUCCESS) elif self.status == self.PROVISIONING and failed is not None: if failed: self._set_status(self.PROVISIONING, self.PROGRESS_FAILED) else: self._set_status(self.PROVISIONING, self.PROGRESS_SUCCESS) elif self.status in (self.PROVISIONING, self.READY) and rebooting: self._set_status(self.REBOOTING, self.PROGRESS_RUNNING) elif self.status == self.REBOOTING and not rebooting and is_port_open(self.public_ip, 22): self._set_status(self.READY, self.PROGRESS_SUCCESS) return self.status
def update_status(self): """ Refresh the status by querying the openstack server via nova """ # TODO: Check when server is stopped or terminated # First check if it makes sense to update the current status. # This is not the case if we can not interact with the server: if self.status in (Status.BuildFailed, Status.Terminated): return self.status try: os_server = self.os_server except (requests.RequestException, novaclient.exceptions.ClientException): self.logger.debug("Could not reach the OpenStack API") if self.status not in (Status.BuildFailed, Status.Terminated, Status.Pending, Status.Unknown): self._status_to_unknown() return self.status self.logger.debug("Updating status from nova (currently %s):\n%s", self.status, to_json(os_server)) if self.status == Status.Unknown: if os_server.status in ("INITIALIZED", "BUILDING"): # OpenStack has multiple API versions; INITIALIZED is current; BUILDING was used in the past self._status_to_building() if self.status in (Status.Building, Status.Unknown): self.logger.debug('OpenStack: loaded="%s" status="%s"', os_server._loaded, os_server.status) if os_server._loaded and os_server.status == "ACTIVE": self._status_to_booting() if self.status in (Status.Booting, Status.Unknown) and self.public_ip and is_port_open(self.public_ip, 22): self._status_to_ready() return self.status