From f1e803dff936ca8601c951da41e8a1bce68e94f9 Mon Sep 17 00:00:00 2001 From: Boris Pavlovic Date: Fri, 30 Nov 2012 15:15:30 +0400 Subject: [PATCH] Fix test_migrations for postgres Fix creating .pgpass file that is used to connect to postgress database without password prompt. Fix command that execute SQL in postgres DB: a) there were no spaces between command options and values b) we can't drop database while we are connected to it. There is a special database `template1` in postgres for dropping databases. c) using `'` instead of `"` in bash to quote SQL expressions because bash interprets only text in single quotes as plain string. Remove SQL requests which lock database Fix stripping password, so it works not only when password contains only spaces. Change-Id: I753a044aba8579b0f4225b4e67163d046ec1a32e --- nova/tests/test_migrations.py | 36 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/nova/tests/test_migrations.py b/nova/tests/test_migrations.py index d82ae7585..138bb3477 100644 --- a/nova/tests/test_migrations.py +++ b/nova/tests/test_migrations.py @@ -177,29 +177,23 @@ class TestMigrations(test.TestCase): user = auth_pieces[0] password = "" if len(auth_pieces) > 1: - if auth_pieces[1].strip(): - password = auth_pieces[1] - cmd = ("touch ~/.pgpass;" - "chmod 0600 ~/.pgpass;" - "sed -i -e" - "'1{s/^.*$/\*:\*:\*:%(user)s:%(password)s/};" - "1!d' ~/.pgpass") % locals() - execute_cmd(cmd) - sql = ("UPDATE pg_catalog.pg_database SET datallowconn=false " - "WHERE datname='%(database)s';") % locals() - cmd = ("psql -U%(user)s -h%(host)s -c\"%(sql)s\"") % locals() - execute_cmd(cmd) - sql = ("SELECT pg_catalog.pg_terminate_backend(procpid) " - "FROM pg_catalog.pg_stat_activity " - "WHERE datname='%(database)s';") % locals() - cmd = ("psql -U%(user)s -h%(host)s -c\"%(sql)s\"") % locals() - execute_cmd(cmd) + password = auth_pieces[1].strip() + # note(boris-42): This file is used for authentication + # without password prompt. + createpgpass = ("echo '*:*:*:%(user)s:%(password)s' > " + "~/.pgpass && chmod 0600 ~/.pgpass" % locals()) + execute_cmd(createpgpass) + # note(boris-42): We must create and drop database, we can't + # drop database wich we have connected to, so for such + # operations there is a special database template1. + sqlcmd = ("psql -w -U %(user)s -h %(host)s -c" + " '%(sql)s' -d template1") sql = ("drop database if exists %(database)s;") % locals() - cmd = ("psql -U%(user)s -h%(host)s -c\"%(sql)s\"") % locals() - execute_cmd(cmd) + droptable = sqlcmd % locals() + execute_cmd(droptable) sql = ("create database %(database)s;") % locals() - cmd = ("psql -U%(user)s -h%(host)s -c\"%(sql)s\"") % locals() - execute_cmd(cmd) + createtable = sqlcmd % locals() + execute_cmd(createtable) def test_walk_versions(self): """