def setUp(self): super(EnableRootTest, self).setUp() self.origin_is_valid_user_name = models.MySQLUser._is_valid_user_name self.mySqlAdmin = MySqlAdmin()
def setUp(self): super(MySqlAdminTest, self).setUp() self.orig_get_engine = dbaas.get_engine self.orig_LocalSqlClient = dbaas.LocalSqlClient self.orig_LocalSqlClient_enter = dbaas.LocalSqlClient.__enter__ self.orig_LocalSqlClient_exit = dbaas.LocalSqlClient.__exit__ self.orig_LocalSqlClient_execute = dbaas.LocalSqlClient.execute self.orig_MySQLUser_is_valid_user_name = \ models.MySQLUser._is_valid_user_name dbaas.get_engine = MagicMock(name='get_engine') dbaas.LocalSqlClient = Mock dbaas.LocalSqlClient.__enter__ = Mock() dbaas.LocalSqlClient.__exit__ = Mock() dbaas.LocalSqlClient.execute = Mock() self.mySqlAdmin = MySqlAdmin()
class EnableRootTest(MySqlAdminTest): def setUp(self): super(EnableRootTest, self).setUp() self.origin_is_valid_user_name = models.MySQLUser._is_valid_user_name self.mySqlAdmin = MySqlAdmin() def tearDown(self): super(EnableRootTest, self).tearDown() models.MySQLUser._is_valid_user_name = self.origin_is_valid_user_name def test_enable_root(self): models.MySQLUser._is_valid_user_name =\ MagicMock(return_value=True) self.mySqlAdmin.enable_root() args_list = dbaas.LocalSqlClient.execute.call_args_list args, keyArgs = args_list[0] self.assertEquals(args[0].text.strip(), "CREATE USER :user@:host;", "Create user queries are not the same") self.assertEquals(keyArgs['user'], 'root') self.assertEquals(keyArgs['host'], '%') args, keyArgs = args_list[1] self.assertTrue("UPDATE mysql.user" in args[0].text) args, keyArgs = args_list[2] self.assertTrue("GRANT ALL PRIVILEGES ON *.*" in args[0].text) self.assertEqual(3, dbaas.LocalSqlClient.execute.call_count) def test_enable_root_failed(self): models.MySQLUser._is_valid_user_name =\ MagicMock(return_value=False) self.assertRaises(ValueError, self.mySqlAdmin.enable_root) def test_is_root_enable(self): self.mySqlAdmin.is_root_enabled() args, _ = dbaas.LocalSqlClient.execute.call_args expected = ("""SELECT User FROM mysql.user WHERE User = '******' """ """AND Host != 'localhost';""") self.assertTrue(expected in args[0].text, "%s not in query." % expected) def test_list_databases(self): self.mySqlAdmin.list_databases() args, _ = dbaas.LocalSqlClient.execute.call_args expected = ["SELECT schema_name as name,", "default_character_set_name as charset,", "default_collation_name as collation", "FROM information_schema.schemata", ("schema_name NOT IN (" "'mysql', 'information_schema', " "'lost+found', '#mysql50#lost+found'" ")"), "ORDER BY schema_name ASC", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertFalse("LIMIT " in args[0].text) def test_list_databases_with_limit(self): limit = 2 self.mySqlAdmin.list_databases(limit) args, _ = dbaas.LocalSqlClient.execute.call_args expected = ["SELECT schema_name as name,", "default_character_set_name as charset,", "default_collation_name as collation", "FROM information_schema.schemata", ("schema_name NOT IN (" "'mysql', 'information_schema', " "'lost+found', '#mysql50#lost+found'" ")"), "ORDER BY schema_name ASC", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertTrue("LIMIT " + str(limit + 1) in args[0].text) def test_list_databases_with_marker(self): marker = "aMarker" self.mySqlAdmin.list_databases(marker=marker) args, _ = dbaas.LocalSqlClient.execute.call_args expected = ["SELECT schema_name as name,", "default_character_set_name as charset,", "default_collation_name as collation", "FROM information_schema.schemata", ("schema_name NOT IN (" "'mysql', 'information_schema', " "'lost+found', '#mysql50#lost+found'" ")"), "ORDER BY schema_name ASC", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertFalse("LIMIT " in args[0].text) self.assertTrue("AND schema_name > '" + marker + "'" in args[0].text) def test_list_databases_with_include_marker(self): marker = "aMarker" self.mySqlAdmin.list_databases(marker=marker, include_marker=True) args, _ = dbaas.LocalSqlClient.execute.call_args expected = ["SELECT schema_name as name,", "default_character_set_name as charset,", "default_collation_name as collation", "FROM information_schema.schemata", ("schema_name NOT IN (" "'mysql', 'information_schema', " "'lost+found', '#mysql50#lost+found'" ")"), "ORDER BY schema_name ASC", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertFalse("LIMIT " in args[0].text) self.assertTrue(("AND schema_name >= '%s'" % marker) in args[0].text) def test_list_users(self): self.mySqlAdmin.list_users() args, _ = dbaas.LocalSqlClient.execute.call_args expected = ["SELECT User, Host", "FROM mysql.user", "WHERE Host != 'localhost'", "ORDER BY User", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertFalse("LIMIT " in args[0].text) self.assertFalse("AND Marker > '" in args[0].text) def test_list_users_with_limit(self): limit = 2 self.mySqlAdmin.list_users(limit) args, _ = dbaas.LocalSqlClient.execute.call_args expected = ["SELECT User, Host", "FROM mysql.user", "WHERE Host != 'localhost'", "ORDER BY User", ("LIMIT " + str(limit + 1)), ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) def test_list_users_with_marker(self): marker = "aMarker" self.mySqlAdmin.list_users(marker=marker) args, _ = dbaas.LocalSqlClient.execute.call_args expected = ["SELECT User, Host, Marker", "FROM mysql.user", "WHERE Host != 'localhost'", "ORDER BY User", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertFalse("LIMIT " in args[0].text) self.assertTrue("AND Marker > '" + marker + "'" in args[0].text) def test_list_users_with_include_marker(self): marker = "aMarker" self.mySqlAdmin.list_users(marker=marker, include_marker=True) args, _ = dbaas.LocalSqlClient.execute.call_args expected = ["SELECT User, Host", "FROM mysql.user", "WHERE Host != 'localhost'", "ORDER BY User", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertFalse("LIMIT " in args[0].text) self.assertTrue("AND Marker >= '" + marker + "'" in args[0].text)
class MySqlAdminTest(testtools.TestCase): def setUp(self): super(MySqlAdminTest, self).setUp() self.orig_get_engine = dbaas.get_engine self.orig_LocalSqlClient = dbaas.LocalSqlClient self.orig_LocalSqlClient_enter = dbaas.LocalSqlClient.__enter__ self.orig_LocalSqlClient_exit = dbaas.LocalSqlClient.__exit__ self.orig_LocalSqlClient_execute = dbaas.LocalSqlClient.execute self.orig_MySQLUser_is_valid_user_name = \ models.MySQLUser._is_valid_user_name dbaas.get_engine = MagicMock(name='get_engine') dbaas.LocalSqlClient = Mock dbaas.LocalSqlClient.__enter__ = Mock() dbaas.LocalSqlClient.__exit__ = Mock() dbaas.LocalSqlClient.execute = Mock() self.mySqlAdmin = MySqlAdmin() def tearDown(self): super(MySqlAdminTest, self).tearDown() dbaas.get_engine = self.orig_get_engine dbaas.LocalSqlClient = self.orig_LocalSqlClient dbaas.LocalSqlClient.__enter__ = self.orig_LocalSqlClient_enter dbaas.LocalSqlClient.__exit__ = self.orig_LocalSqlClient_exit dbaas.LocalSqlClient.execute = self.orig_LocalSqlClient_execute models.MySQLUser._is_valid_user_name = \ self.orig_MySQLUser_is_valid_user_name def test_create_database(self): databases = [] databases.append(FAKE_DB) self.mySqlAdmin.create_database(databases) args, _ = dbaas.LocalSqlClient.execute.call_args_list[0] expected = ("CREATE DATABASE IF NOT EXISTS " "`testDB` CHARACTER SET = 'latin2' " "COLLATE = 'latin2_general_ci';") self.assertEquals(args[0].text, expected, "Create database queries are not the same") self.assertEqual(1, dbaas.LocalSqlClient.execute.call_count, "The client object was not 2 times") def test_create_database_more_than_1(self): databases = [] databases.append(FAKE_DB) databases.append(FAKE_DB_2) self.mySqlAdmin.create_database(databases) args, _ = dbaas.LocalSqlClient.execute.call_args_list[0] expected = ("CREATE DATABASE IF NOT EXISTS " "`testDB` CHARACTER SET = 'latin2' " "COLLATE = 'latin2_general_ci';") self.assertEquals(args[0].text, expected, "Create database queries are not the same") args, _ = dbaas.LocalSqlClient.execute.call_args_list[1] expected = ("CREATE DATABASE IF NOT EXISTS " "`testDB2` CHARACTER SET = 'latin2' " "COLLATE = 'latin2_general_ci';") self.assertEquals(args[0].text, expected, "Create database queries are not the same") self.assertEqual(2, dbaas.LocalSqlClient.execute.call_count, "The client object was not 2 times") def test_create_database_no_db(self): databases = [] self.mySqlAdmin.create_database(databases) self.assertFalse(dbaas.LocalSqlClient.execute.called, "The client object was called when it wasn't " + "supposed to") def test_delete_database(self): database = {"_name": "testDB"} self.mySqlAdmin.delete_database(database) args, _ = dbaas.LocalSqlClient.execute.call_args expected = "DROP DATABASE `testDB`;" self.assertEquals(args[0].text, expected, "Delete database queries are not the same") self.assertTrue(dbaas.LocalSqlClient.execute.called, "The client object was not called") def test_delete_user(self): user = {"_name": "testUser"} self.mySqlAdmin.delete_user(user) # For some reason, call_args is None. call_args = dbaas.LocalSqlClient.execute.call_args if call_args is not None: args, _ = call_args expected = "DROP USER `testUser`;" self.assertEquals(args[0].text, expected, "Delete user queries are not the same") self.assertTrue(dbaas.LocalSqlClient.execute.called, "The client object was not called") def test_create_user(self): self.mySqlAdmin.create_user(FAKE_USER) expected = ("GRANT ALL PRIVILEGES ON `testDB`.* TO `random`@`%` " "IDENTIFIED BY 'guesswhat' " "WITH GRANT OPTION;") # For some reason, call_args is None. call_args = dbaas.LocalSqlClient.execute.call_args if call_args is not None: args, _ = call_args self.assertEquals(args[0].text.strip(), expected, "Create user queries are not the same") self.assertEqual(2, dbaas.LocalSqlClient.execute.call_count)
class EnableRootTest(MySqlAdminTest): def setUp(self): super(EnableRootTest, self).setUp() self.origin_is_valid_user_name = models.MySQLUser._is_valid_user_name self.mySqlAdmin = MySqlAdmin() def tearDown(self): super(EnableRootTest, self).tearDown() models.MySQLUser._is_valid_user_name = self.origin_is_valid_user_name def test_enable_root(self): models.MySQLUser._is_valid_user_name =\ MagicMock(return_value=True) self.mySqlAdmin.enable_root() args_list = dbaas.LocalSqlClient.execute.call_args_list args, keyArgs = args_list[0] self.assertEquals(args[0].text.strip(), "CREATE USER :user@:host;", "Create user queries are not the same") self.assertEquals(keyArgs['user'], 'root') self.assertEquals(keyArgs['host'], '%') args, keyArgs = args_list[1] self.assertTrue("UPDATE mysql.user" in args[0].text) args, keyArgs = args_list[2] self.assertTrue("GRANT ALL PRIVILEGES ON *.*" in args[0].text) self.assertEqual(3, dbaas.LocalSqlClient.execute.call_count) def test_enable_root_failed(self): models.MySQLUser._is_valid_user_name =\ MagicMock(return_value=False) self.assertRaises(ValueError, self.mySqlAdmin.enable_root) def test_is_root_enable(self): self.mySqlAdmin.is_root_enabled() args, _ = dbaas.LocalSqlClient.execute.call_args expected = ("""SELECT User FROM mysql.user WHERE User = '******' """ """AND Host != 'localhost';""") self.assertTrue(expected in args[0].text, "%s not in query." % expected) def test_list_databases(self): self.mySqlAdmin.list_databases() args, _ = dbaas.LocalSqlClient.execute.call_args expected = [ "SELECT schema_name as name,", "default_character_set_name as charset,", "default_collation_name as collation", "FROM information_schema.schemata", ("schema_name NOT IN (" "'mysql', 'information_schema', " "'lost+found', '#mysql50#lost+found'" ")"), "ORDER BY schema_name ASC", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertFalse("LIMIT " in args[0].text) def test_list_databases_with_limit(self): limit = 2 self.mySqlAdmin.list_databases(limit) args, _ = dbaas.LocalSqlClient.execute.call_args expected = [ "SELECT schema_name as name,", "default_character_set_name as charset,", "default_collation_name as collation", "FROM information_schema.schemata", ("schema_name NOT IN (" "'mysql', 'information_schema', " "'lost+found', '#mysql50#lost+found'" ")"), "ORDER BY schema_name ASC", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertTrue("LIMIT " + str(limit + 1) in args[0].text) def test_list_databases_with_marker(self): marker = "aMarker" self.mySqlAdmin.list_databases(marker=marker) args, _ = dbaas.LocalSqlClient.execute.call_args expected = [ "SELECT schema_name as name,", "default_character_set_name as charset,", "default_collation_name as collation", "FROM information_schema.schemata", ("schema_name NOT IN (" "'mysql', 'information_schema', " "'lost+found', '#mysql50#lost+found'" ")"), "ORDER BY schema_name ASC", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertFalse("LIMIT " in args[0].text) self.assertTrue("AND schema_name > '" + marker + "'" in args[0].text) def test_list_databases_with_include_marker(self): marker = "aMarker" self.mySqlAdmin.list_databases(marker=marker, include_marker=True) args, _ = dbaas.LocalSqlClient.execute.call_args expected = [ "SELECT schema_name as name,", "default_character_set_name as charset,", "default_collation_name as collation", "FROM information_schema.schemata", ("schema_name NOT IN (" "'mysql', 'information_schema', " "'lost+found', '#mysql50#lost+found'" ")"), "ORDER BY schema_name ASC", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertFalse("LIMIT " in args[0].text) self.assertTrue(("AND schema_name >= '%s'" % marker) in args[0].text) def test_list_users(self): self.mySqlAdmin.list_users() args, _ = dbaas.LocalSqlClient.execute.call_args expected = [ "SELECT User, Host", "FROM mysql.user", "WHERE Host != 'localhost'", "ORDER BY User", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertFalse("LIMIT " in args[0].text) self.assertFalse("AND Marker > '" in args[0].text) def test_list_users_with_limit(self): limit = 2 self.mySqlAdmin.list_users(limit) args, _ = dbaas.LocalSqlClient.execute.call_args expected = [ "SELECT User, Host", "FROM mysql.user", "WHERE Host != 'localhost'", "ORDER BY User", ("LIMIT " + str(limit + 1)), ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) def test_list_users_with_marker(self): marker = "aMarker" self.mySqlAdmin.list_users(marker=marker) args, _ = dbaas.LocalSqlClient.execute.call_args expected = [ "SELECT User, Host, Marker", "FROM mysql.user", "WHERE Host != 'localhost'", "ORDER BY User", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertFalse("LIMIT " in args[0].text) self.assertTrue("AND Marker > '" + marker + "'" in args[0].text) def test_list_users_with_include_marker(self): marker = "aMarker" self.mySqlAdmin.list_users(marker=marker, include_marker=True) args, _ = dbaas.LocalSqlClient.execute.call_args expected = [ "SELECT User, Host", "FROM mysql.user", "WHERE Host != 'localhost'", "ORDER BY User", ] for text in expected: self.assertTrue(text in args[0].text, "%s not in query." % text) self.assertFalse("LIMIT " in args[0].text) self.assertTrue("AND Marker >= '" + marker + "'" in args[0].text)
class MySqlAdminTest(testtools.TestCase): def setUp(self): super(MySqlAdminTest, self).setUp() self.orig_get_engine = dbaas.get_engine self.orig_LocalSqlClient = dbaas.LocalSqlClient self.orig_LocalSqlClient_enter = dbaas.LocalSqlClient.__enter__ self.orig_LocalSqlClient_exit = dbaas.LocalSqlClient.__exit__ self.orig_LocalSqlClient_execute = dbaas.LocalSqlClient.execute self.orig_MySQLUser_is_valid_user_name = \ models.MySQLUser._is_valid_user_name dbaas.get_engine = MagicMock(name='get_engine') dbaas.LocalSqlClient = Mock dbaas.LocalSqlClient.__enter__ = Mock() dbaas.LocalSqlClient.__exit__ = Mock() dbaas.LocalSqlClient.execute = Mock() self.mySqlAdmin = MySqlAdmin() def tearDown(self): super(MySqlAdminTest, self).tearDown() dbaas.get_engine = self.orig_get_engine dbaas.LocalSqlClient = self.orig_LocalSqlClient dbaas.LocalSqlClient.__enter__ = self.orig_LocalSqlClient_enter dbaas.LocalSqlClient.__exit__ = self.orig_LocalSqlClient_exit dbaas.LocalSqlClient.execute = self.orig_LocalSqlClient_execute models.MySQLUser._is_valid_user_name = \ self.orig_MySQLUser_is_valid_user_name def test_create_database(self): databases = [] databases.append(FAKE_DB) self.mySqlAdmin.create_database(databases) args, _ = dbaas.LocalSqlClient.execute.call_args_list[0] expected = ("CREATE DATABASE IF NOT EXISTS " "`testDB` CHARACTER SET = 'latin2' " "COLLATE = 'latin2_general_ci';") self.assertEquals(args[0].text, expected, "Create database queries are not the same") self.assertEqual(1, dbaas.LocalSqlClient.execute.call_count, "The client object was not 2 times") def test_create_database_more_than_1(self): databases = [] databases.append(FAKE_DB) databases.append(FAKE_DB_2) self.mySqlAdmin.create_database(databases) args, _ = dbaas.LocalSqlClient.execute.call_args_list[0] expected = ("CREATE DATABASE IF NOT EXISTS " "`testDB` CHARACTER SET = 'latin2' " "COLLATE = 'latin2_general_ci';") self.assertEquals(args[0].text, expected, "Create database queries are not the same") args, _ = dbaas.LocalSqlClient.execute.call_args_list[1] expected = ("CREATE DATABASE IF NOT EXISTS " "`testDB2` CHARACTER SET = 'latin2' " "COLLATE = 'latin2_general_ci';") self.assertEquals(args[0].text, expected, "Create database queries are not the same") self.assertEqual(2, dbaas.LocalSqlClient.execute.call_count, "The client object was not 2 times") def test_create_database_no_db(self): databases = [] self.mySqlAdmin.create_database(databases) self.assertFalse( dbaas.LocalSqlClient.execute.called, "The client object was called when it wasn't " + "supposed to") def test_delete_database(self): database = {"_name": "testDB"} self.mySqlAdmin.delete_database(database) args, _ = dbaas.LocalSqlClient.execute.call_args expected = "DROP DATABASE `testDB`;" self.assertEquals(args[0].text, expected, "Delete database queries are not the same") self.assertTrue(dbaas.LocalSqlClient.execute.called, "The client object was not called") def test_delete_user(self): user = {"_name": "testUser"} self.mySqlAdmin.delete_user(user) # For some reason, call_args is None. call_args = dbaas.LocalSqlClient.execute.call_args if call_args is not None: args, _ = call_args expected = "DROP USER `testUser`;" self.assertEquals(args[0].text, expected, "Delete user queries are not the same") self.assertTrue(dbaas.LocalSqlClient.execute.called, "The client object was not called") def test_create_user(self): self.mySqlAdmin.create_user(FAKE_USER) expected = ("GRANT ALL PRIVILEGES ON `testDB`.* TO `random`@`%` " "IDENTIFIED BY 'guesswhat' " "WITH GRANT OPTION;") # For some reason, call_args is None. call_args = dbaas.LocalSqlClient.execute.call_args if call_args is not None: args, _ = call_args self.assertEquals(args[0].text.strip(), expected, "Create user queries are not the same") self.assertEqual(2, dbaas.LocalSqlClient.execute.call_count)