Skip to content

Commit

Permalink
Unit test auth (#718)
Browse files Browse the repository at this point in the history
* fix issue #629 (#693)

* docs:优化错误信息描述

* Update zh.toml

* fix:修复eureka心跳协议错误码不兼容问题

* fix:修复eureka心跳协议错误码不兼容问题

* unit:添加单元测试

* test:调整测试配置文件位置

* fix:issue #692

* fix:issue #692

* fix:issue #692

* fix:issue #692

* docs:add error code desc

* unittest:auth模块添加sqldb的测试覆盖

* fix:修复auth下结合数据源的集成测试

* fix:修复auth下结合数据源的集成测试
  • Loading branch information
chuntaojun committed Oct 9, 2022
1 parent bc5d8ec commit 905e01b
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 14 deletions.
33 changes: 29 additions & 4 deletions .github/workflows/codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ jobs:
# Run unit tests
- name: Go Test With DB
run: |
# 测试配置模块
export STORE_MODE=sqldb
echo "cur STORE MODE=${STORE_MODE}"
Expand All @@ -85,12 +86,16 @@ jobs:
mysql -h127.0.0.1 -P3306 -uroot -p"polaris" -e "set GLOBAL max_connections = 3000;"
pushd ./config
go mod vendor && go test -v -timeout 40m -v -coverprofile=coverage_sqldb_1.cover -coverpkg=github.com/polarismesh/polaris-server/auth/defaultauth,github.com/polarismesh/polaris-server/service,github.com/polarismesh/polaris-server/service/batch,github.com/polarismesh/polaris-server/cache,github.com/polarismesh/polaris-server/store/boltdb,github.com/polarismesh/polaris-server/store/sqldb,github.com/polarismesh/polaris-server/plugin,github.com/polarismesh/polaris-server/config,github.com/polarismesh/polaris-server/common/model
mv coverage_sqldb_1.cover ../
go mod vendor && go test -v -timeout 40m -v -coverprofile=coverage_sqldb_2.cover -coverpkg=github.com/polarismesh/polaris-server/auth/defaultauth,github.com/polarismesh/polaris-server/service,github.com/polarismesh/polaris-server/service/batch,github.com/polarismesh/polaris-server/cache,github.com/polarismesh/polaris-server/store/boltdb,github.com/polarismesh/polaris-server/store/sqldb,github.com/polarismesh/polaris-server/plugin,github.com/polarismesh/polaris-server/config,github.com/polarismesh/polaris-server/common/model
mv coverage_sqldb_2.cover ../
popd
sleep 10s
# 测试服务、治理
export STORE_MODE=sqldb
echo "cur STORE MODE=${STORE_MODE}"
# 设置严格模式
mysql -h127.0.0.1 -P3306 -uroot -p"polaris" -e "set sql_mode='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'";
# 清空数据
Expand All @@ -101,8 +106,28 @@ jobs:
mysql -h127.0.0.1 -P3306 -uroot -p"polaris" -e "set GLOBAL max_connections = 3000;"
pushd ./service
go mod vendor && go test -v -timeout 40m -v -coverprofile=coverage_sqldb_2.cover -coverpkg=github.com/polarismesh/polaris-server/auth/defaultauth,github.com/polarismesh/polaris-server/service,github.com/polarismesh/polaris-server/service/batch,github.com/polarismesh/polaris-server/cache,github.com/polarismesh/polaris-server/store/boltdb,github.com/polarismesh/polaris-server/store/sqldb,github.com/polarismesh/polaris-server/plugin,github.com/polarismesh/polaris-server/config,github.com/polarismesh/polaris-server/common/model
mv coverage_sqldb_2.cover ../
go mod vendor && go test -v -timeout 40m -v -coverprofile=coverage_sqldb_3.cover -coverpkg=github.com/polarismesh/polaris-server/auth/defaultauth,github.com/polarismesh/polaris-server/service,github.com/polarismesh/polaris-server/service/batch,github.com/polarismesh/polaris-server/cache,github.com/polarismesh/polaris-server/store/boltdb,github.com/polarismesh/polaris-server/store/sqldb,github.com/polarismesh/polaris-server/plugin,github.com/polarismesh/polaris-server/config,github.com/polarismesh/polaris-server/common/model
mv coverage_sqldb_3.cover ../
popd
sleep 10s
# 测试鉴权
export STORE_MODE=sqldb
echo "cur STORE MODE=${STORE_MODE}"
# 设置严格模式
mysql -h127.0.0.1 -P3306 -uroot -p"polaris" -e "set sql_mode='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'";
# 清空数据
mysql -h127.0.0.1 -P3306 -uroot -p"polaris" -e "DROP DATABASE IF EXISTS polaris_server";
# 初始化 polaris 数据库
mysql -h127.0.0.1 -P3306 -uroot -p"polaris" < store/sqldb/scripts/polaris_server.sql
# 临时放开 DB 的最大连接数
mysql -h127.0.0.1 -P3306 -uroot -p"polaris" -e "set GLOBAL max_connections = 3000;"
pushd ./auth/defaultauth
go mod vendor && go test -v -timeout 40m -v -coverprofile=coverage_sqldb_1.cover -coverpkg=github.com/polarismesh/polaris-server/auth/defaultauth,github.com/polarismesh/polaris-server/service,github.com/polarismesh/polaris-server/service/batch,github.com/polarismesh/polaris-server/cache,github.com/polarismesh/polaris-server/store/boltdb,github.com/polarismesh/polaris-server/store/sqldb,github.com/polarismesh/polaris-server/plugin,github.com/polarismesh/polaris-server/config,github.com/polarismesh/polaris-server/common/model
mv coverage_sqldb_1.cover ../../
popd
- name: Merge All Coverage
Expand Down
3 changes: 3 additions & 0 deletions apiserver/httpserver/i18n/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```bash
go run cmd/gen.go
```
2 changes: 1 addition & 1 deletion apiserver/httpserver/i18n/cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func generateI18nMessageSrc(fileName string) {

func getSrcCodeInfo() []errCodeItem {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "../../../../common/api/v1/codeinfo.go", nil, parser.ParseComments)
f, err := parser.ParseFile(fset, "../../../common/api/v1/codeinfo.go", nil, parser.ParseComments)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion apiserver/httpserver/i18n/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,4 @@
500007 = "heartbeat execute exception" #HeartbeatException
500008 = "instance async regist timeout" #InstanceRegisTimeout
500100 = "token verify exception" #AuthTokenVerifyException
500101 = "operation role exception" #OperationRoleException
500101 = "operation role exception" #OperationRoleException
3 changes: 2 additions & 1 deletion apiserver/httpserver/i18n/messages.genearate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apiserver/httpserver/i18n/zh.toml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
400506 = "服务别名不允许绑定规则" #NotAllowAliasBindRule
400507 = "不允许不同的命名空间绑定同一规则" #NotAllowDifferentNamespaceBindRule
400508 = "不允许修改默认策略" #NotAllowModifyDefaultStrategyPrincipal
400509 = "not allow modify main account default strategy" #NotAllowModifyOwnerDefaultStrategy
400700 = "路由规则ID非法" #InvalidRoutingID
400701 = "路由规则类型非法,只支持 (RulePolicy,MetadataPolicy)" #InvalidRoutingPolicy
400702 = "路由名称非法" #InvalidRoutingName
Expand Down
4 changes: 2 additions & 2 deletions auth/defaultauth/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func createApiMockUser(total int, prefix ...string) []*api.User {

for i := range models {
users = append(users, &api.User{
Name: utils.NewStringValue(models[i].Name),
Name: utils.NewStringValue("test-" + models[i].Name),
Password: utils.NewStringValue("123456"),
Source: utils.NewStringValue("Polaris"),
Comment: utils.NewStringValue(models[i].Comment),
Expand All @@ -205,7 +205,7 @@ func createMockUserGroup(users []*model.User) []*model.UserGroupDetail {
groups = append(groups, &model.UserGroupDetail{
UserGroup: &model.UserGroup{
ID: id,
Name: fmt.Sprintf("group-%d", i),
Name: fmt.Sprintf("test-group-%d", i),
Owner: users[0].ID,
Token: token,
TokenEnable: true,
Expand Down
10 changes: 5 additions & 5 deletions auth/defaultauth/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (d *AuthTestSuit) cleanAllUser() {

defer dbTx.Rollback()

if _, err := dbTx.Exec("delete from user"); err != nil {
if _, err := dbTx.Exec("delete from user where name like 'test%'"); err != nil {
dbTx.Rollback()
panic(err)
}
Expand Down Expand Up @@ -259,7 +259,7 @@ func (d *AuthTestSuit) cleanAllUserGroup() {

defer dbTx.Rollback()

if _, err := dbTx.Exec("delete from user_group"); err != nil {
if _, err := dbTx.Exec("delete from user_group where name like 'test%'"); err != nil {
dbTx.Rollback()
panic(err)
}
Expand Down Expand Up @@ -304,15 +304,15 @@ func (d *AuthTestSuit) cleanAllAuthStrategy() {

defer dbTx.Rollback()

if _, err := dbTx.Exec("delete from auth_strategy"); err != nil {
if _, err := dbTx.Exec("delete from auth_strategy where id != 'fbca9bfa04ae4ead86e1ecf5811e32a9'"); err != nil {
dbTx.Rollback()
panic(err)
}
if _, err := dbTx.Exec("delete from auth_principal"); err != nil {
if _, err := dbTx.Exec("delete from auth_principal where strategy_id != 'fbca9bfa04ae4ead86e1ecf5811e32a9'"); err != nil {
dbTx.Rollback()
panic(err)
}
if _, err := dbTx.Exec("delete from auth_strategy_resource"); err != nil {
if _, err := dbTx.Exec("delete from auth_strategy_resource where strategy_id != 'fbca9bfa04ae4ead86e1ecf5811e32a9'"); err != nil {
dbTx.Rollback()
panic(err)
}
Expand Down

0 comments on commit 905e01b

Please sign in to comment.