Files
trove/trove/tests/unittests/guestagent/test_dbmodels.py
T
Jeremy Stanley 42de1e7f7e Switch from MySQL-python to PyMySQL
As discussed in the Liberty Design Summit "Moving apps to Python 3"
cross-project workshop, the way forward in the near future is to
switch to the pure-python PyMySQL library as a default.

https://etherpad.openstack.org/p/liberty-cross-project-python3

BaseMySqlRootAccess.enable_root(): catch also InternalError because
the PyMySQL error is not wrapped into a SQLAlchemy OperationalError,
but a generic SQLAlchemy InternalError. Similar change is made in
026_datastore_versions_unique_fix.py.

This change requires a trove integration change to add the PyMySQL to
the guest image: Id4d013d174ba40a453819f900aaa316a93e59b48.

Partially implements: blueprint trove-python3
Co-Authored-By: Victor Stinner <vstinner@redhat.com>
Depends-On: Id4d013d174ba40a453819f900aaa316a93e59b48
Change-Id: I65e8a8d5dc251a8b00529cdfb1a6ada3d5720f68
2016-06-30 08:47:01 +02:00

113 lines
3.8 KiB
Python

# Copyright 2012 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from mock import MagicMock
from trove.guestagent.db import models as dbmodels
from trove.tests.unittests import trove_testtools
class MySQLDatabaseTest(trove_testtools.TestCase):
def setUp(self):
super(MySQLDatabaseTest, self).setUp()
self.mysqlDb = dbmodels.ValidatedMySQLDatabase()
self.origin_ignore_db = self.mysqlDb._ignore_dbs
self.mysqlDb._ignore_dbs = ['mysql']
def tearDown(self):
super(MySQLDatabaseTest, self).tearDown()
self.mysqlDb._ignore_dbs = self.origin_ignore_db
def test_name(self):
self.assertIsNone(self.mysqlDb.name)
def test_name_setter(self):
test_name = "Anna"
self.mysqlDb.name = test_name
self.assertEqual(test_name, self.mysqlDb.name)
def test_is_valid_positive(self):
self.assertTrue(self.mysqlDb._is_valid('pymysql'))
def test_is_valid_negative(self):
self.assertFalse(self.mysqlDb._is_valid('mysql'))
class MySQLUserTest(trove_testtools.TestCase):
def setUp(self):
super(MySQLUserTest, self).setUp()
self.mysqlUser = dbmodels.MySQLUser()
def tearDown(self):
super(MySQLUserTest, self).tearDown()
def test_is_valid_negative(self):
self.assertFalse(self.mysqlUser._is_valid(None))
self.assertFalse(self.mysqlUser._is_valid("|;"))
self.assertFalse(self.mysqlUser._is_valid("\\"))
def test_is_valid_positive(self):
self.assertTrue(self.mysqlUser._is_valid("real_name"))
class IsValidUsernameTest(trove_testtools.TestCase):
def setUp(self):
super(IsValidUsernameTest, self).setUp()
self.mysqlUser = dbmodels.MySQLUser()
self.origin_is_valid = self.mysqlUser._is_valid
self.origin_ignore_users = self.mysqlUser._ignore_users
self.mysqlUser._ignore_users = ["king"]
def tearDown(self):
super(IsValidUsernameTest, self).tearDown()
self.mysqlUser._is_valid = self.origin_is_valid
self.mysqlUser._ignore_users = self.origin_ignore_users
def test_is_valid_user_name(self):
value = "trove"
self.assertTrue(self.mysqlUser._is_valid_user_name(value))
def test_is_valid_user_name_negative(self):
self.mysqlUser._is_valid = MagicMock(return_value=False)
self.assertFalse(self.mysqlUser._is_valid_user_name("trove"))
self.mysqlUser._is_valid = MagicMock(return_value=True)
self.assertFalse(self.mysqlUser._is_valid_user_name("king"))
class IsValidHostnameTest(trove_testtools.TestCase):
def setUp(self):
super(IsValidHostnameTest, self).setUp()
self.mysqlUser = dbmodels.MySQLUser()
def tearDown(self):
super(IsValidHostnameTest, self).tearDown()
def test_is_valid_octet(self):
self.assertTrue(self.mysqlUser._is_valid_host_name('192.168.1.1'))
def test_is_valid_bad_octet(self):
self.assertFalse(self.mysqlUser._is_valid_host_name('999.168.1.1'))
def test_is_valid_global_wildcard(self):
self.assertTrue(self.mysqlUser._is_valid_host_name('%'))
def test_is_valid_prefix_wildcard(self):
self.assertTrue(self.mysqlUser._is_valid_host_name('%.168.1.1'))
def test_is_valid_suffix_wildcard(self):
self.assertTrue(self.mysqlUser._is_valid_host_name('192.168.1.%'))