def test_update_status_started(self): """ Update status while the server is started, without change on the OpenStack VM """ server = StartedOpenStackServerFactory(os_server_fixture='openstack/api_server_1_building.json') self.assertEqual(server.update_status(), server.STARTED) self.assertEqual(server.status, server.STARTED)
def test_update_status_started(self): """ Update status while the server is started, without change on the OpenStack VM """ server = StartedOpenStackServerFactory(os_server_fixture='openstack/api_server_1_building.json') self.assertIsInstance(server.update_status(), ServerStatus.Active) self.assertEqual(server.progress, ServerProgress.Running)
def test_reboot_server_wrong_status(self): """ Attempt to reboot a server while in a status that doesn't allow it """ server = StartedOpenStackServerFactory() with self.assertRaises(ServerNotReady): server.reboot()
def test_update_status_started(self): """ Update status while the server is started, without change on the OpenStack VM """ server = StartedOpenStackServerFactory(os_server_fixture='openstack/api_server_1_building.json') self.assertEqual(server.update_status(), server.ACTIVE) self.assertEqual(server.progress, server.PROGRESS_RUNNING)
def test_reboot_server_wrong_status(self): """ Attempt to reboot a server while in a status that doesn't allow it """ server = StartedOpenStackServerFactory() with self.assertRaises(WrongStateException): server.reboot()
def test_terminate_started_server(self): """ Terminate a server with a 'started' status """ server = StartedOpenStackServerFactory() server.terminate() self.assertEqual(server.status, server.TERMINATED) server.os_server.delete.assert_called_once_with()
def test_update_status_started_to_active(self, mock_is_port_open): """ Update status while the server is started, when the VM becomes active """ mock_is_port_open.return_value = False server = StartedOpenStackServerFactory(os_server_fixture='openstack/api_server_2_active.json') self.assertEqual(server.update_status(), server.ACTIVE) self.assertEqual(server.status, server.ACTIVE)
def test_update_status_started_to_active(self, mock_is_port_open): """ Update status while the server is started, when the VM becomes active """ mock_is_port_open.return_value = False server = StartedOpenStackServerFactory(os_server_fixture='openstack/api_server_2_active.json') self.assertIsInstance(server.update_status(), ServerStatus.Active) self.assertEqual(server.status, ServerStatus.Active) self.assertEqual(server.progress, ServerProgress.Success)
def test_terminate_started_server(self): """ Terminate a server with a 'started' status """ server = StartedOpenStackServerFactory() server.terminate() self.assertEqual(server.status, ServerStatus.Terminated) self.assertEqual(server.progress, ServerProgress.Success) server.os_server.delete.assert_called_once_with()
def test_reboot_provisioned_server(self, mock_sleep): """ Reboot a provisioned server """ server = StartedOpenStackServerFactory(_status=ServerStatus.Provisioning.state_id) server.reboot() self.assertEqual(server.status, ServerStatus.Rebooting) server.os_server.reboot.assert_called_once_with(reboot_type='SOFT') mock_sleep.assert_called_once_with(30)
def test_status_terminated(self): """ Instance status should revert to 'empty' when all its servers are terminated """ instance = OpenEdXInstanceFactory() server = StartedOpenStackServerFactory(instance=instance) self.assertEqual(instance.status, server.Status.Started) server._transition(server._status_to_terminated) self.assertIsNone(instance.status)
def test_reboot_provisioned_server(self, mock_sleep): """ Reboot a provisioned server """ server = StartedOpenStackServerFactory(status=OpenStackServer.PROVISIONING) server.reboot() self.assertEqual(server.status, server.REBOOTING) server.os_server.reboot.assert_called_once_with(reboot_type='SOFT') mock_sleep.assert_called_once_with(30)
def test_update_status_provisioned_to_rebooting(self): """ Update status when the server is rebooted, after being provisioned """ server = StartedOpenStackServerFactory( os_server_fixture='openstack/api_server_2_active.json', status=OpenStackServer.PROVISIONED) self.assertEqual(server.update_status(), server.PROVISIONED) self.assertEqual(server.update_status(rebooting=True), server.REBOOTING) self.assertEqual(server.status, server.REBOOTING)
def test_update_status_booted_to_provisioned(self): """ Update status while the server is booted, when the VM is provisioned """ server = StartedOpenStackServerFactory( os_server_fixture='openstack/api_server_2_active.json', status=OpenStackServer.BOOTED) self.assertEqual(server.update_status(), server.BOOTED) self.assertEqual(server.update_status(provisioned=True), server.PROVISIONED) self.assertEqual(server.status, server.PROVISIONED)
def test_status_terminated(self): """ Instance status should revert to 'empty' when all its servers are terminated """ instance = OpenEdXInstanceFactory() server = StartedOpenStackServerFactory(instance=instance) self.assertEqual(instance.status, instance.STARTED) server.status = 'terminated' server.save() self.assertEqual(instance.status, instance.EMPTY)
def test_update_status_booted_to_error(self): """ Update status while the server is booted, when the VM failed to be provisioned """ server = StartedOpenStackServerFactory( os_server_fixture='openstack/api_server_2_active.json', status=OpenStackServer.BOOTED) self.assertEqual(server.update_status(), server.BOOTED) self.assertEqual(server.update_status(provisioning=True), server.PROVISIONING) self.assertEqual(server.update_status(provisioning=True, failed=True), server.PROVISIONING) self.assertEqual(server.progress, server.PROGRESS_FAILED)
def test_status_multiple_servers(self): """ Instance status should not allow multiple active servers """ instance = OpenEdXInstanceFactory() StartedOpenStackServerFactory(instance=instance) self.assertEqual(instance.status, Server.Status.Started) self.assertEqual(instance.progress, Server.Progress.Running) StartedOpenStackServerFactory(instance=instance) with self.assertRaises(InconsistentInstanceState): instance.status #pylint: disable=pointless-statement
def test_status(self): """ Instance status with one active server """ instance = OpenEdXInstanceFactory() self.assertEqual(instance.status, instance.EMPTY) server = StartedOpenStackServerFactory(instance=instance) self.assertEqual(instance.status, instance.STARTED) server.status = server.BOOTED server.save() self.assertEqual(instance.status, instance.BOOTED)
def test_update_status_rebooting_to_ready(self, mock_is_port_open): """ Update status while the server is rebooting, when the server becomes ready (ssh accessible again) """ server = StartedOpenStackServerFactory( os_server_fixture='openstack/api_server_2_active.json', status=OpenStackServer.REBOOTING) mock_is_port_open.return_value = False self.assertEqual(server.update_status(), server.REBOOTING) mock_is_port_open.return_value = True self.assertEqual(server.update_status(), server.READY) self.assertEqual(server.status, server.READY)
def test_update_status_active_to_booted(self, mock_is_port_open): """ Update status while the server is active, when the VM becomes booted """ server = StartedOpenStackServerFactory( os_server_fixture='openstack/api_server_2_active.json', status=OpenStackServer.ACTIVE) mock_is_port_open.return_value = False self.assertEqual(server.update_status(), server.ACTIVE) mock_is_port_open.return_value = True self.assertEqual(server.update_status(), server.BOOTED) self.assertEqual(server.status, server.BOOTED)
def test_update_status_rebooting_to_ready(self, mock_is_port_open): """ Update status while the server is rebooting, when the server becomes ready (ssh accessible again) """ server = StartedOpenStackServerFactory( os_server_fixture='openstack/api_server_2_active.json', _status=ServerStatus.Rebooting.state_id) mock_is_port_open.return_value = False self.assertIsInstance(server.update_status(), ServerStatus.Rebooting) mock_is_port_open.return_value = True self.assertIsInstance(server.update_status(), ServerStatus.Ready) self.assertEqual(server.status, ServerStatus.Ready) self.assertEqual(server.progress, ServerProgress.Success)
def test_update_status_active_to_booted(self, mock_is_port_open): """ Update status while the server is active, when the VM becomes booted """ server = StartedOpenStackServerFactory( os_server_fixture='openstack/api_server_2_active.json', _status=ServerStatus.Active.state_id) mock_is_port_open.return_value = False self.assertIsInstance(server.update_status(), ServerStatus.Active) mock_is_port_open.return_value = True self.assertIsInstance(server.update_status(), ServerStatus.Booted) self.assertEqual(server.status, ServerStatus.Booted) self.assertEqual(server.progress, ServerProgress.Success)
def test_terminate_server_not_found(self): """ Terminate a server for which the corresponding VM doesn't exist anymore """ server = StartedOpenStackServerFactory() def raise_not_found(): #pylint: disable=missing-docstring raise novaclient.exceptions.NotFound('not-found') server.os_server.delete.side_effect = raise_not_found server.log = Mock() server.terminate() server.log.assert_any_call('exception', AnyStringMatching('Error while attempting to terminate server')) self.assertEqual(server.status, server.TERMINATED)
def test_status(self): """ Instance status with one active server """ instance = OpenEdXInstanceFactory() self.assertIsNone(instance.status) self.assertIsNone(instance.progress) server = StartedOpenStackServerFactory(instance=instance) self.assertEqual(instance.status, Server.Status.Started) self.assertEqual(instance.progress, Server.Progress.Running) server._transition(server._status_to_active) self.assertEqual(instance.status, Server.Status.Active) server._transition(server._status_to_booted) self.assertEqual(instance.status, Server.Status.Booted)
def test_terminate_server_not_found(self): """ Terminate a server for which the corresponding VM doesn't exist anymore """ server = StartedOpenStackServerFactory() def raise_not_found(): #pylint: disable=missing-docstring raise novaclient.exceptions.NotFound('not-found') server.os_server.delete.side_effect = raise_not_found server.logger = Mock() mock_logger = server.logger server.terminate() mock_logger.error.assert_called_once_with(AnyStringMatching('Error while attempting to terminate server')) self.assertEqual(server.status, ServerStatus.Terminated) self.assertEqual(server.progress, ServerProgress.Success)
def test_inventory_str(self, os_server_manager): """ Ansible inventory - showing servers once they are in booted status """ instance = OpenEdXInstanceFactory() self.assertEqual(instance.inventory_str, '[app]') # Server 1: 'started' StartedOpenStackServerFactory(instance=instance) self.assertEqual(instance.inventory_str, '[app]') # Server 2: 'booted' server2 = BootedOpenStackServerFactory(instance=instance) os_server_manager.add_fixture(server2.openstack_id, 'openstack/api_server_2_active.json') self.assertEqual(instance.inventory_str, '[app]') # Server 3: 'provisioning' server3 = ProvisioningOpenStackServerFactory(instance=instance) os_server_manager.add_fixture(server3.openstack_id, 'openstack/api_server_2_active.json') self.assertEqual(instance.inventory_str, '[app]\n192.168.100.200') # Server 4: 'provisioning' server4 = ProvisioningOpenStackServerFactory(instance=instance) os_server_manager.add_fixture(server4.openstack_id, 'openstack/api_server_3_active.json') self.assertEqual(instance.inventory_str, '[app]\n192.168.100.200\n192.168.99.66')
def test_update_status_booted_to_error(self): """ Update status while the server is booted, when the VM failed to be provisioned """ server = StartedOpenStackServerFactory( os_server_fixture='openstack/api_server_2_active.json', _status=ServerStatus.Booted.state_id) self.assertIsInstance(server.update_status(), ServerStatus.Booted) server.mark_as_provisioning() self.assertEqual(server.status, ServerStatus.Provisioning) self.assertEqual(server.progress, ServerProgress.Running) server.mark_provisioning_finished(success=False) self.assertEqual(server.status, ServerStatus.Provisioning) self.assertEqual(server.progress, ServerProgress.Failed)
def test_update_status_provisioned_to_rebooting(self, _mock_sleep, mock_is_port_open): """ Update status when the server is rebooted, after being provisioned """ server = StartedOpenStackServerFactory( os_server_fixture='openstack/api_server_2_active.json', _status=ServerStatus.Provisioning.state_id, _progress=ServerProgress.Success.state_id) # If server is in Status.Rebooting, update_status calls is_port_open # to determine if server should transition to Status.Ready. # When using a fixture for server.os_server, is_port_open will eventually return False, # but only after a delay of about two minutes. # So we mock out is_port_open here to speed up testing: mock_is_port_open.return_value = False self.assertIsInstance(server.update_status(), ServerStatus.Provisioning) self.assertEqual(server.progress, ServerProgress.Success) server.reboot() self.assertEqual(server.status, ServerStatus.Rebooting) self.assertIsInstance(server.update_status(), ServerStatus.Rebooting) self.assertEqual(server.status, ServerStatus.Rebooting) self.assertEqual(server.progress, ServerProgress.Running)
def test_public_ip_active_server(self): """ Get the public IP of an active server """ server = StartedOpenStackServerFactory(os_server_fixture='openstack/api_server_2_active.json') self.assertEqual(server.public_ip, '192.168.100.200')
def test_public_ip_started_server(self): """ A server in `started` status doesn't have a public IP """ server = StartedOpenStackServerFactory(os_server_fixture='openstack/api_server_1_building.json') self.assertEqual(server.public_ip, None)