def test_get_details(self): """ GET - Detailed attributes """ self.api_client.login(username='******', password='******') instance = OpenEdXInstanceFactory(sub_domain='domain.api') app_server = make_test_appserver(instance) instance.active_appserver = app_server # Outside of tests, use set_appserver_active() instead instance.save() response = self.api_client.get('/api/v1/instance/{pk}/'.format(pk=instance.ref.pk)) self.assertEqual(response.status_code, status.HTTP_200_OK) instance_data = response.data.items() self.assertIn(('domain', 'domain.api.example.com'), instance_data) self.assertIn(('is_shut_down', False), instance_data) self.assertIn(('name', instance.name), instance_data) self.assertIn(('url', 'http://domain.api.example.com/'), instance_data) self.assertIn(('studio_url', 'http://studio-domain.api.example.com/'), instance_data) self.assertIn( ('edx_platform_repository_url', 'https://github.com/{}.git'.format(settings.DEFAULT_FORK)), instance_data ) self.assertIn(('edx_platform_commit', 'master'), instance_data) # AppServer info: self.assertIn(('appserver_count', 1), instance_data) self.assertIn('active_appserver', response.data) self.assertIn('newest_appserver', response.data) for key in ('active_appserver', 'newest_appserver'): app_server_data = response.data[key] self.assertEqual(app_server_data['id'], app_server.pk) self.assertEqual( app_server_data['api_url'], 'http://testserver/api/v1/openedx_appserver/{pk}/'.format(pk=app_server.pk) ) self.assertEqual(app_server_data['status'], 'new')
def test_shut_down(self, mock_reconfigure, mock_disable_monitoring, mock_remove_dns_records): """ Test that `shut_down` method terminates all app servers belonging to an instance and disables monitoring. """ instance = OpenEdXInstanceFactory() instance.load_balancing_server = LoadBalancingServer.objects.select_random() instance.save() reference_date = timezone.now() # Create app servers obsolete_appserver = self._create_running_appserver(instance, reference_date - timedelta(days=5)) obsolete_appserver_failed = self._create_failed_appserver(instance, reference_date - timedelta(days=5)) recent_appserver = self._create_running_appserver(instance, reference_date - timedelta(days=1)) recent_appserver_failed = self._create_failed_appserver(instance, reference_date - timedelta(days=1)) active_appserver = self._create_running_appserver(instance, reference_date) newer_appserver = self._create_running_appserver(instance, reference_date + timedelta(days=3)) newer_appserver_failed = self._create_failed_appserver(instance, reference_date + timedelta(days=3)) # Set single app server active instance.active_appserver = active_appserver instance.save() active_appserver.instance.refresh_from_db() self.assertEqual(mock_reconfigure.call_count, 0) self.assertEqual(mock_disable_monitoring.call_count, 0) self.assertEqual(mock_remove_dns_records.call_count, 0) # Shut down instance instance.shut_down() self.assertEqual(mock_reconfigure.call_count, 1) self.assertEqual(mock_disable_monitoring.call_count, 1) self.assertEqual(mock_remove_dns_records.call_count, 1) # Check status of running app servers self._assert_status([ (obsolete_appserver, AppServerStatus.Terminated, ServerStatus.Terminated), (recent_appserver, AppServerStatus.Terminated, ServerStatus.Terminated), (active_appserver, AppServerStatus.Terminated, ServerStatus.Terminated), (newer_appserver, AppServerStatus.Terminated, ServerStatus.Terminated), ]) # Check status of failed app servers: # AppServerStatus.Terminated is reserved for instances that were running successfully at some point, # so app servers with AppServerStatus.ConfigurationFailed will still have that status # after `shut_down` calls `terminate_vm` on them. # However, the VM (OpenStackServer) that an app server is associated with # *should* have ServerStatus.Terminated if the app server was old enough to be terminated. self._assert_status([ (obsolete_appserver_failed, AppServerStatus.ConfigurationFailed, ServerStatus.Terminated), (recent_appserver_failed, AppServerStatus.ConfigurationFailed, ServerStatus.Terminated), (newer_appserver_failed, AppServerStatus.ConfigurationFailed, ServerStatus.Terminated), ])
def test_terminate_obsolete_appservers(self, days): """ Test that `terminate_obsolete_appservers` correctly identifies and terminates app servers that were created (more than) `days` before the currently-active app server of the parent instance. """ instance = OpenEdXInstanceFactory() reference_date = timezone.now() # Create app servers obsolete_appserver = self._create_running_appserver(instance, reference_date - timedelta(days=days + 1)) obsolete_appserver_failed = self._create_failed_appserver(instance, reference_date - timedelta(days=days + 1)) recent_appserver = self._create_running_appserver(instance, reference_date - timedelta(days=days - 1)) recent_appserver_failed = self._create_failed_appserver(instance, reference_date - timedelta(days=days - 1)) active_appserver = self._create_running_appserver(instance, reference_date) newer_appserver = self._create_running_appserver(instance, reference_date + timedelta(days=days)) newer_appserver_failed = self._create_failed_appserver(instance, reference_date + timedelta(days=days)) # Set single app server active instance.active_appserver = active_appserver instance.save() # Terminate app servers instance.terminate_obsolete_appservers(days=days) # Check status of running app servers self._assert_status([ (obsolete_appserver, AppServerStatus.Terminated, ServerStatus.Terminated), (recent_appserver, AppServerStatus.Running, ServerStatus.Pending), (active_appserver, AppServerStatus.Running, ServerStatus.Pending), (newer_appserver, AppServerStatus.Running, ServerStatus.Pending), ]) # Check status of failed app servers: # AppServerStatus.Terminated is reserved for instances that were running successfully at some point, # so app servers with AppServerStatus.ConfigurationFailed will still have that status # after `terminate_obsolete_appservers` calls `terminate_vm` on them. # However, the VM (OpenStackServer) that an app server is associated with # *should* have ServerStatus.Terminated if the app server was old enough to be terminated. self._assert_status([ (obsolete_appserver_failed, AppServerStatus.ConfigurationFailed, ServerStatus.Terminated), (recent_appserver_failed, AppServerStatus.ConfigurationFailed, ServerStatus.Pending), (newer_appserver_failed, AppServerStatus.ConfigurationFailed, ServerStatus.Pending), ])
def test_shut_down_monitors_not_found(self, *mock_methods): """ Test that instance `is_shut_down` after calling `shut_down` on it, even if monitors associated with it no longer exist. """ monitor_ids = [str(uuid4()) for i in range(3)] instance = OpenEdXInstanceFactory() appserver = self._create_running_appserver(instance) instance.active_appserver = appserver instance.save() appserver.instance.refresh_from_db() for monitor_id in monitor_ids: instance.new_relic_availability_monitors.create(pk=monitor_id) responses.add( responses.DELETE, '{0}/monitors/{1}'.format(newrelic.SYNTHETICS_API_URL, monitor_id), status=requests.codes.not_found # pylint: disable=no-member ) # Preconditions self.assertEqual(instance.new_relic_availability_monitors.count(), 3) self.assertFalse(instance.is_shut_down) # Shut down instance instance.shut_down() # Instance should # - no longer have any monitors associated with it # - no longer have any running app servers # - be considered "shut down" self.assertEqual(instance.new_relic_availability_monitors.count(), 0) self._assert_status([ (appserver, AppServerStatus.Terminated, ServerStatus.Terminated), ]) self.assertTrue(instance.is_shut_down)