Example #1
0
    def _create_database(self, key):
        """Create database if it doesn't exist."""
        conn_string = self.test_databases[key]
        conn_pieces = urlparse.urlparse(conn_string)

        if conn_string.startswith('mysql'):
            (user, password, database, host) = \
                get_mysql_connection_info(conn_pieces)
            sql = "create database if not exists %s;" % database
            cmd = ("mysql -u \"%(user)s\" %(password)s -h %(host)s "
                   "-e \"%(sql)s\"" % {
                       'user': user,
                       'password': password,
                       'host': host,
                       'sql': sql
                   })
            self.execute_cmd(cmd)
        elif conn_string.startswith('postgresql'):
            (user, password, database, host) = \
                get_pgsql_connection_info(conn_pieces)
            os.environ['PGPASSWORD'] = password
            os.environ['PGUSER'] = user

            sqlcmd = ("psql -w -U %(user)s -h %(host)s -c"
                      " '%(sql)s' -d template1")

            sql = ("create database if not exists %s;") % database
            createtable = sqlcmd % {'user': user, 'host': host, 'sql': sql}
            # 0 means databases is created
            # 256 means it already exists (which is fine)
            # otherwise raise an error
            out, err = processutils.trycmd(createtable,
                                           shell=True,
                                           check_exit_code=[0, 256],
                                           discard_warnings=True)
            output = out or err
            if err != '':
                self.fail("Failed to run: %s\n%s" % (createtable, output))

            os.unsetenv('PGPASSWORD')
            os.unsetenv('PGUSER')
    def _reset_pg(self, conn_pieces):
        (user, password, database,
         host) = (get_pgsql_connection_info(conn_pieces))
        os.environ['PGPASSWORD'] = password
        os.environ['PGUSER'] = user
        # note(boris-42): We must create and drop database, we can't
        # drop database which 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')
        sqldict = {'user': user, 'host': host}
        # note(apavlov): We need to kill all connections before dropping
        # database. Otherwise it does not happen.
        sqldict['sql'] = "select version();"
        getversion = sqlcmd % sqldict
        out, err = processutils.trycmd(getversion, shell=True)
        version = out.split()

        sqldict['sql'] = ("SELECT pg_terminate_backend(%s) FROM "
                          "pg_stat_activity WHERE datname = "
                          "'%s';")
        if float(version[version.index('PostgreSQL') + 1][:3]) < 9.2:
            sqldict['sql'] = sqldict['sql'] % ('procpid', database)
        else:
            sqldict['sql'] = sqldict['sql'] % ('pid', database)
        killconnections = sqlcmd % sqldict
        self.execute_cmd(killconnections)

        sqldict['sql'] = "drop database if exists %s;" % database
        droptable = sqlcmd % sqldict
        self.execute_cmd(droptable)

        sqldict['sql'] = ("create database %s;") % database
        createtable = sqlcmd % sqldict
        self.execute_cmd(createtable)

        os.unsetenv('PGPASSWORD')
        os.unsetenv('PGUSER')
Example #3
0
    def _create_database(self, key):
        """Create database if it doesn't exist."""
        conn_string = self.test_databases[key]
        conn_pieces = urlparse.urlparse(conn_string)

        if conn_string.startswith('mysql'):
            (user, password, database, host) = (
                get_mysql_connection_info(conn_pieces))
            sql = "create database if not exists %s;" % database
            cmd = ("mysql -u \"%(user)s\" %(password)s -h %(host)s "
                   "-e \"%(sql)s\"" % {'user': user, 'password': password,
                                       'host': host, 'sql': sql})
            self.execute_cmd(cmd)
        elif conn_string.startswith('postgresql'):
            (user, password, database, host) = (
                get_pgsql_connection_info(conn_pieces))
            os.environ['PGPASSWORD'] = password
            os.environ['PGUSER'] = user

            sqlcmd = ("psql -w -U %(user)s -h %(host)s -c"
                      " '%(sql)s' -d template1")

            sql = ("create database if not exists %s;") % database
            createtable = sqlcmd % {'user': user, 'host': host, 'sql': sql}
            # 0 means databases is created
            # 256 means it already exists (which is fine)
            # otherwise raise an error
            out, err = processutils.trycmd(createtable, shell=True,
                                           check_exit_code=[0, 256],
                                           discard_warnings=True)
            output = out or err
            if err != '':
                self.fail("Failed to run: %s\n%s" % (createtable, output))

            os.unsetenv('PGPASSWORD')
            os.unsetenv('PGUSER')
Example #4
0
 def execute_cmd(self, cmd=None):
     out, err = processutils.trycmd(cmd, shell=True, discard_warnings=True)
     output = out or err
     LOG.debug(output)
     self.assertEqual('', err, "Failed to run: %s\n%s" % (cmd, output))
Example #5
0
 def execute_cmd(self, cmd=None):
     out, err = processutils.trycmd(cmd, shell=True, discard_warnings=True)
     output = out or err
     LOG.debug(output)
     self.assertEqual('', err,
                      "Failed to run: %s\n%s" % (cmd, output))