import os import testinfra.utils.ansible_runner testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner( os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('parameters-*') def test_database_created(host): """ Tests if database has been created. """ with host.sudo(): show_databases = host.run("mysql -BNe \"show databases like 'testdb'\"") assert show_databases.rc == 0 assert show_databases.stdout == "testdb\n" def test_database_user_login(host): """ Tests database user login. """ login = host.run("mysql -utestdb -ptestdbpassword -BNe 'show databases'") assert login.rc == 0 def test_database_user_permissions(host): """ Tests if database user has been granted correct permissions on the database. """ expected_usage = "GRANT USAGE ON *.* TO `testdb`@`localhost` IDENTIFIED BY PASSWORD '*676852B7FAE972722AD20D6E74781D6B1A100544'" expected_privileges = "GRANT ALL PRIVILEGES ON `testdb`.* TO `testdb`@`localhost`" visible_databases = host.run("mysql -utestdb -ptestdbpassword -BNe 'show databases'") assert visible_databases.rc == 0 assert visible_databases.stdout == "information_schema\ntestdb\n" with host.sudo(): permissions_command = host.run("mysql -BNe 'show grants for testdb@localhost'") permissions = permissions_command.stdout.rstrip().split("\n") assert len(permissions) == 2 assert expected_usage in permissions assert expected_privileges in permissions