您的位置:首页 > 运维架构

keystone中test-setup.py

2018-02-12 16:40 309 查看
test-setup.py是keystone/tools目录下的一个脚本文件,openstack其它项目可能也有此文件。

利用tox对keystone进行单元测试之前,需要执行test-setup.py(正如脚本一开始的注释所示)

openstack社区CI也是这么做的。我们提交代码到gerrit后,会有一项py27检查,py27就是在python2.7环境下进行单元测试。通过查看其执行日志,确实发现会执行这么一个文件。

主要作用:

①修改root用户密码(如果不明确指定,则修改为insecure_slave)

②授权openstack_citest用户对任何数据表有所有权限(创建、删除…)

③创建数据库openstack_citest

搞这些干嘛呢?

执行单元测试时,有些测试用例是针对数据库的测试,这是真的测试,不是mock

所以但凡进行这种类型的测试会首先创建相应的数据表,而创建数据表是需要通过一个用户进行的。这个用户在keysone测试时是写死的,就是openstack_citest。

具体参考:oslo.db中的测试用户

另外,创建的数据库openstack_citest,通过我自己的测试发现并没有用到,每次执行测试时,会创建一个名称随机的数据库,然后测试创建的数据表都会创建在这个数据库中。

openstack_citest数据库基本废弃

最底下的部分,是针对PostgreSQL的,功能上跟上面应该是一样的。

#!/bin/bash -xe

# This script will be run by OpenStack CI before unit tests are run,
# it sets up the test system as needed.
# Developers should setup their test systems in a similar way.

# This setup needs to be run as a user that can run sudo.

# The root password for the MySQL database; pass it in via
# MYSQL_ROOT_PW.
DB_ROOT_PW=${MYSQL_ROOT_PW:-insecure_slave}

# This user and its password are used by the tests, if you change it,
# your tests might fail.
DB_USER=openstack_citest
DB_PW=openstack_citest

sudo -H mysqladmin -u root password $DB_ROOT_PW

# It's best practice to remove anonymous users from the database.  If
# a anonymous user exists, then it matches first for connections and
# other connections from that host will not work.
# 删除匿名用户
# 刷新权限(用户做了权限变更之后,一定记得重新加载一下权限,将权限信息从内存中写入数据库)
# 授权openstack_citest用户能够不受任何限制访问任何数据表,且密码指定为openstack_citest
关于数据权限,参考 http://blog.csdn.net/anzhen0429/article/details/78296814 sudo -H mysql -u root -p$DB_ROOT_PW -h localhost -e "
DELETE FROM mysql.user WHERE User='';
FLUSH PRIVILEGES;
GRANT ALL PRIVILEGES ON *.*
TO '$DB_USER'@'%' identified by '$DB_PW' WITH GRANT OPTION;"

# Bump the max_connections limit
sudo -H mysql -u root -p$DB_ROOT_PW -h localhost -e "
SET GLOBAL max_connections = 1024;"

# Now create our database.
# 通过刚授权用户openstack_citest,创建数据库openstack_citest
mysql -u $DB_USER -p$DB_PW -h 127.0.0.1 -e "
SET default_storage_engine=MYISAM;
DROP DATABASE IF EXISTS openstack_citest;
CREATE DATABASE openstack_citest CHARACTER SET utf8;"

# Same for PostgreSQL
# The root password for the PostgreSQL database; pass it in via
# POSTGRES_ROOT_PW.
DB_ROOT_PW=${POSTGRES_ROOT_PW:-insecure_slave}

# Setup user
root_roles=$(sudo -H -u postgres psql -t -c "
SELECT 'HERE' from pg_roles where rolname='$DB_USER'")
if [[ ${root_roles} == *HERE ]];then
sudo -H -u postgres psql -c "ALTER ROLE $DB_USER WITH SUPERUSER LOGIN PASSWORD '$DB_PW'"
else
sudo -H -u postgres psql -c "CREATE ROLE $DB_USER WITH SUPERUSER LOGIN PASSWORD '$DB_PW'"
fi

# Store password for tests
cat << EOF > $HOME/.pgpass
*:*:*:$DB_USER:$DB_PW
EOF
chmod 0600 $HOME/.pgpass

# Now create our database
psql -h 127.0.0.1 -U $DB_USER -d template1 -c "DROP DATABASE IF EXISTS openstack_citest"
createdb -h 127.0.0.1 -U $DB_USER -l C -T template0 -E utf8 openstack_citest


注意:sudo -H mysqladmin -u root password $DB_ROOT_PW

在我本地执行这个脚本的时候,会报错。

error:’Acess denied for user ‘root’@’localhost (using password:No)

MYSQL 5.7版本不支持,上述修改密码的方式

新版的mysql数据库下的user表中已经没有Password字段了

mysql -u root -p

update mysql.user set authentication_string=password(“新密码”) where User=”root” and Host=”localhost”;

flush privileges;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息