本文整理汇总了Python中instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory.save方法的典型用法代码示例。如果您正苦于以下问题:Python OpenEdXInstanceFactory.save方法的具体用法?Python OpenEdXInstanceFactory.save怎么用?Python OpenEdXInstanceFactory.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory
的用法示例。
在下文中一共展示了OpenEdXInstanceFactory.save方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_details
# 需要导入模块: from instance.tests.models.factories.openedx_instance import OpenEdXInstanceFactory [as 别名]
# 或者: from instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory import save [as 别名]
def test_get_details(self):
"""
GET - Detailed attributes
"""
self.api_client.login(username='user3', password='pass')
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')
示例2: test_shut_down
# 需要导入模块: from instance.tests.models.factories.openedx_instance import OpenEdXInstanceFactory [as 别名]
# 或者: from instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory import save [as 别名]
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),
])
示例3: make_test_appserver
# 需要导入模块: from instance.tests.models.factories.openedx_instance import OpenEdXInstanceFactory [as 别名]
# 或者: from instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory import save [as 别名]
def make_test_appserver(instance=None):
"""
Factory method to create an OpenEdXAppServer (and OpenStackServer).
"""
if not instance:
instance = OpenEdXInstanceFactory()
if not instance.load_balancing_server:
instance.load_balancing_server = LoadBalancingServer.objects.select_random()
instance.save()
return instance._create_owned_appserver()
示例4: test_set_dns_records
# 需要导入模块: from instance.tests.models.factories.openedx_instance import OpenEdXInstanceFactory [as 别名]
# 或者: from instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory import save [as 别名]
def test_set_dns_records(self):
"""
Test set_dns_records() without external domains.
"""
instance = OpenEdXInstanceFactory(internal_lms_domain='test.dns.example.com',
use_ephemeral_databases=True)
instance.load_balancing_server = LoadBalancingServer.objects.select_random()
instance.save()
instance.set_dns_records()
self._verify_dns_records(instance, 'example.com')
示例5: test_remove_dns_records
# 需要导入模块: from instance.tests.models.factories.openedx_instance import OpenEdXInstanceFactory [as 别名]
# 或者: from instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory import save [as 别名]
def test_remove_dns_records(self):
"""
Test remove_dns_records().
"""
instance = OpenEdXInstanceFactory(internal_lms_domain='test.dns.opencraft.co.uk')
instance.load_balancing_server = LoadBalancingServer.objects.select_random()
instance.save()
instance.set_dns_records()
instance.remove_dns_records()
dns_records = gandi.api.client.list_records('opencraft.co.uk')
self.assertEqual(dns_records, [])
示例6: test_set_dns_records_external_domain
# 需要导入模块: from instance.tests.models.factories.openedx_instance import OpenEdXInstanceFactory [as 别名]
# 或者: from instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory import save [as 别名]
def test_set_dns_records_external_domain(self):
"""
Test set_dns_records() with custom external domains.
Ensure that the DNS records are only created for the internal domains.
"""
instance = OpenEdXInstanceFactory(internal_lms_domain='test.dns.opencraft.co.uk',
external_lms_domain='courses.myexternal.org',
external_lms_preview_domain='preview.myexternal.org',
external_studio_domain='studio.myexternal.org',
use_ephemeral_databases=True)
instance.load_balancing_server = LoadBalancingServer.objects.select_random()
instance.save()
instance.set_dns_records()
self._verify_dns_records(instance, 'opencraft.co.uk')
示例7: test_shut_down_no_active_appserver
# 需要导入模块: from instance.tests.models.factories.openedx_instance import OpenEdXInstanceFactory [as 别名]
# 或者: from instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory import save [as 别名]
def test_shut_down_no_active_appserver(self, mock_reconfigure, mock_disable_monitoring, mock_remove_dns_records):
"""
Test that the shut_down method works correctly if no appserver is active.
"""
instance = OpenEdXInstanceFactory()
instance.load_balancing_server = LoadBalancingServer.objects.select_random()
instance.save()
appserver = self._create_running_appserver(instance)
instance.shut_down()
self.assertEqual(mock_reconfigure.call_count, 1)
self.assertEqual(mock_disable_monitoring.call_count, 0)
self.assertEqual(mock_remove_dns_records.call_count, 1)
self._assert_status([
(appserver, AppServerStatus.Terminated, ServerStatus.Terminated)
])
示例8: test_do_not_create_insecure_secret_keys
# 需要导入模块: from instance.tests.models.factories.openedx_instance import OpenEdXInstanceFactory [as 别名]
# 或者: from instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory import save [as 别名]
def test_do_not_create_insecure_secret_keys(self, mocks):
"""
Test that if we have a brand-new instance with no appservers, we refuse to create insecure
keys for those appservers if we don't have a secure secret key for the instance.
"""
instance = OpenEdXInstanceFactory()
instance.secret_key_b64encoded = ''
instance.save()
expected_error_string = re.escape(
'Attempted to create secret key for instance {}, but no master key present.'.format(instance)
)
# Six provides a compatibility method for assertRaisesRegex, since the method
# is named differently between Py2k and Py3k.
with six.assertRaisesRegex(self, ValueError, expected_error_string):
instance.spawn_appserver()
示例9: test_terminate_obsolete_appservers
# 需要导入模块: from instance.tests.models.factories.openedx_instance import OpenEdXInstanceFactory [as 别名]
# 或者: from instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory import save [as 别名]
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),
])
示例10: test_secret_key_creation
# 需要导入模块: from instance.tests.models.factories.openedx_instance import OpenEdXInstanceFactory [as 别名]
# 或者: from instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory import save [as 别名]
def test_secret_key_creation(self):
"""
Test that we can reliably produce derived secret keys for an instance with a particular
existing secret key.
"""
instance = OpenEdXInstanceFactory()
instance.secret_key_b64encoded = 'esFyh7kbvbMQiYhRx9fISJw9gkcSCStGAfOWaPu9cfc6/tMu'
instance.save()
self.assertEqual(
instance.get_secret_key_for_var('THIS_IS_A_TEST'),
'95652a974218e2efc44f99feb6f2ab89a263746688ff428ca2c898ae44111f58',
)
self.assertEqual(
instance.get_secret_key_for_var('OTHER_TEST'),
'820b455b1f0e30b75ec0514ab172c588223b010de3beacce3cd27217adc7fe60',
)
self.assertEqual(
instance.get_secret_key_for_var('SUPER_SECRET'),
'21b5271f21ee6dacfde05cd97e20739f0e73dc8a43408ef14b657bfbf718e2b4',
)
示例11: test_shut_down_monitors_not_found
# 需要导入模块: from instance.tests.models.factories.openedx_instance import OpenEdXInstanceFactory [as 别名]
# 或者: from instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory import save [as 别名]
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)
示例12: MySQLInstanceTestCase
# 需要导入模块: from instance.tests.models.factories.openedx_instance import OpenEdXInstanceFactory [as 别名]
# 或者: from instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory import save [as 别名]
#.........这里部分代码省略.........
def test_provision_mysql_weird_domain(self):
"""
Make sure that database names are escaped correctly
"""
sub_domain = 'really.really.really.really.long.subdomain'
base_domain = 'this-is-a-really-long-unusual-domain-แปลกมาก.com'
internal_lms_domain = '{}.{}'.format(sub_domain, base_domain)
self.instance = OpenEdXInstanceFactory(use_ephemeral_databases=False,
internal_lms_domain=internal_lms_domain)
self.instance.provision_mysql()
self.check_mysql()
def test_provision_mysql_again(self):
"""
Only create the database once
"""
self.instance = OpenEdXInstanceFactory(use_ephemeral_databases=False)
self.instance.provision_mysql()
self.assertIs(self.instance.mysql_provisioned, True)
mysql_user = self.instance.mysql_user
mysql_pass = self.instance.mysql_pass
self.instance.provision_mysql()
self.assertEqual(self.instance.mysql_user, mysql_user)
self.assertEqual(self.instance.mysql_pass, mysql_pass)
self.check_mysql()
def test_provision_mysql_no_mysql_server(self):
"""
Don't provision a mysql database if instance has no MySQL server
"""
self.instance = OpenEdXInstanceFactory(use_ephemeral_databases=False)
self.instance.mysql_server = None
self.instance.save()
self.instance.provision_mysql()
databases = subprocess.check_output("mysql -u root -e 'SHOW DATABASES'", shell=True).decode()
for database in self.instance.mysql_databases:
self.assertNotIn(database["name"], databases)
@patch_services
@override_settings(DEFAULT_INSTANCE_MYSQL_URL='mysql://user:[email protected]')
def test_ansible_settings_mysql(self, mocks):
"""
Test that get_database_settings produces correct settings for MySQL databases
"""
self.instance = OpenEdXInstanceFactory(use_ephemeral_databases=False)
expected_host = "mysql.opencraft.com"
expected_port = MYSQL_SERVER_DEFAULT_PORT
def make_flat_group_info(var_names=None, database=None, include_port=True):
""" Return dict containing info for a flat group of variables """
group_info = {}
if var_names:
group_info["vars"] = var_names
# Compute and insert values
name = self.instance._get_mysql_database_name(database["name"])
user = self.instance._get_mysql_user_name(database["user"])
password = self.instance._get_mysql_pass(user)
values = [name, user, password, expected_host]
if include_port:
values.append(expected_port)
group_info["values"] = values
return group_info
def make_nested_group_info(var_names, databases):
""" Return dict containing info for a nested group of variables """
示例13: MongoDBInstanceTestCase
# 需要导入模块: from instance.tests.models.factories.openedx_instance import OpenEdXInstanceFactory [as 别名]
# 或者: from instance.tests.models.factories.openedx_instance.OpenEdXInstanceFactory import save [as 别名]
class MongoDBInstanceTestCase(TestCase):
"""
Test cases for MongoDBInstanceMixin and OpenEdXDatabaseMixin
"""
def setUp(self):
super().setUp()
self.instance = None
def tearDown(self):
if self.instance:
self.instance.deprovision_mongo()
super().tearDown()
def check_mongo(self):
"""
Check that the instance mongo user has access to the external mongo database
"""
mongo = pymongo.MongoClient(settings.DEFAULT_INSTANCE_MONGO_URL)
for database in self.instance.mongo_database_names:
self.assertTrue(mongo[database].authenticate(self.instance.mongo_user, self.instance.mongo_pass))
def check_mongo_vars_not_set(self, appserver):
"""
Check that the given OpenEdXAppServer does not point to a mongo database
"""
for var in ('EDXAPP_MONGO_USER',
'EDXAPP_MONGO_PASSWORD'
'EDXAPP_MONGO_HOSTS',
'EDXAPP_MONGO_PORT',
'EDXAPP_MONGO_DB_NAME',
'FORUM_MONGO_USER',
'FORUM_MONGO_PASSWORD',
'FORUM_MONGO_HOSTS',
'FORUM_MONGO_PORT',
'FORUM_MONGO_DATABASE'):
self.assertNotIn(var, appserver.configuration_settings)
@override_settings(DEFAULT_INSTANCE_MONGO_URL=None)
def test_create_instance_no_default_mongo_url(self):
"""
Test that creating an instance with persistent databases raises an exception
if DEFAULT_INSTANCE_MONGO_URL not set.
"""
with self.assertRaises(MongoDBServer.DoesNotExist):
self.instance = OpenEdXInstanceFactory(use_ephemeral_databases=False)
def test_provision_mongo(self):
"""
Provision mongo databases
"""
self.instance = OpenEdXInstanceFactory(use_ephemeral_databases=False)
self.instance.provision_mongo()
self.check_mongo()
def test_provision_mongo_again(self):
"""
Only create the databases once
"""
self.instance = OpenEdXInstanceFactory(use_ephemeral_databases=False)
self.instance.provision_mongo()
self.assertIs(self.instance.mongo_provisioned, True)
mongo_user = self.instance.mongo_user
mongo_pass = self.instance.mongo_pass
self.instance.provision_mongo()
self.assertEqual(self.instance.mongo_user, mongo_user)
self.assertEqual(self.instance.mongo_pass, mongo_pass)
self.check_mongo()
def test_provision_mongo_no_mongodb_server(self):
"""
Don't provision a mongo database if instance has no MongoDB server
"""
mongo = pymongo.MongoClient(settings.DEFAULT_INSTANCE_MONGO_URL)
self.instance = OpenEdXInstanceFactory(use_ephemeral_databases=False)
self.instance.mongodb_server = None
self.instance.save()
self.instance.provision_mongo()
databases = mongo.database_names()
for database in self.instance.mongo_database_names:
self.assertNotIn(database, databases)
@override_settings(DEFAULT_INSTANCE_MONGO_URL='mongodb://user:[email protected]')
def test_ansible_settings_mongo(self):
"""
Add mongo ansible vars if instance has a MongoDB server
"""
self.instance = OpenEdXInstanceFactory(use_ephemeral_databases=False)
appserver = make_test_appserver(self.instance)
ansible_vars = appserver.configuration_settings
self.assertIn('EDXAPP_MONGO_USER: {0}'.format(self.instance.mongo_user), ansible_vars)
self.assertIn('EDXAPP_MONGO_PASSWORD: {0}'.format(self.instance.mongo_pass), ansible_vars)
self.assertIn('EDXAPP_MONGO_HOSTS: [mongo.opencraft.com]', ansible_vars)
self.assertIn('EDXAPP_MONGO_PORT: {0}'.format(MONGODB_SERVER_DEFAULT_PORT), ansible_vars)
self.assertIn('EDXAPP_MONGO_DB_NAME: {0}'.format(self.instance.mongo_database_name), ansible_vars)
self.assertIn('FORUM_MONGO_USER: {0}'.format(self.instance.mongo_user), ansible_vars)
self.assertIn('FORUM_MONGO_PASSWORD: {0}'.format(self.instance.mongo_pass), ansible_vars)
self.assertIn('FORUM_MONGO_HOSTS: [mongo.opencraft.com]', ansible_vars)
self.assertIn('FORUM_MONGO_PORT: {0}'.format(MONGODB_SERVER_DEFAULT_PORT), ansible_vars)
self.assertIn('FORUM_MONGO_DATABASE: {0}'.format(self.instance.forum_database_name), ansible_vars)
#.........这里部分代码省略.........