-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test demo for database write prohibition plugin
Signed-off-by: hanbingleixue <[email protected]>
- Loading branch information
1 parent
de55650
commit a2f7818
Showing
19 changed files
with
1,631 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
...huaweicloud/sermant/database/prohibition/integration/AbstractDatabaseProhibitionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.huaweicloud.sermant.database.prohibition.integration; | ||
|
||
import com.huaweicloud.sermant.database.prohibition.integration.entity.Result; | ||
import com.huaweicloud.sermant.database.prohibition.integration.utils.HttpRequestUtils; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
|
||
/** | ||
* Database write prohibition test | ||
* | ||
* @author zhp | ||
* @since 2024-03-13 | ||
*/ | ||
public abstract class AbstractDatabaseProhibitionTest { | ||
/** | ||
* enable database write prohibition configuration | ||
*/ | ||
public static final String ENABLE_DATABASE_WRITE_PROHIBITION_CONFIG = "enablePostgreSqlWriteProhibition: true\n" | ||
+ "enableOpenGaussWriteProhibition: true\n" | ||
+ "postgreSqlDatabases:\n" | ||
+ " - test\n" | ||
+ "openGaussDatabases:\n" | ||
+ " - postgres"; | ||
|
||
/** | ||
* disable database write prohibition configuration | ||
*/ | ||
public static final String DISABLE_DATABASE_WRITE_PROHIBITION_CONFIG = ""; | ||
|
||
/** | ||
* test interface results | ||
* | ||
* @param httpRequestUrl Request Address | ||
* @param statusCode 预期结果编码 | ||
* @return 结果 | ||
*/ | ||
public static Result testHttpRequest(String httpRequestUrl, String statusCode) { | ||
String response = HttpRequestUtils.doGet(httpRequestUrl); | ||
Result result = JSONObject.parseObject(response, Result.class); | ||
Assertions.assertEquals(result.getCode(), statusCode); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...src/test/java/com/huaweicloud/sermant/database/prohibition/integration/entity/Result.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.huaweicloud.sermant.database.prohibition.integration.entity; | ||
|
||
/** | ||
* Result Information | ||
* | ||
* @author zhp | ||
* @since 2024-03-13 | ||
*/ | ||
public class Result { | ||
/** | ||
* result code | ||
*/ | ||
private String code; | ||
|
||
/** | ||
* result message | ||
*/ | ||
private String message; | ||
|
||
/** | ||
* result data | ||
*/ | ||
private Object data; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param code result code | ||
* @param message result message | ||
* @param data result data | ||
*/ | ||
public Result(String code, String message, Object data) { | ||
this.code = code; | ||
this.message = message; | ||
this.data = data; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public void setMessage(String message) { | ||
this.message = message; | ||
} | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
|
||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
.../huaweicloud/sermant/database/prohibition/integration/postgresql/BatchSqlExecuteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright (C) 2024-2024 Huawei Technologies Co., Ltd. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.huaweicloud.sermant.database.prohibition.integration.postgresql; | ||
|
||
import com.huaweicloud.sermant.database.prohibition.integration.constant.DatabaseConstant; | ||
import com.huaweicloud.sermant.database.prohibition.integration.entity.Result; | ||
import com.huaweicloud.sermant.database.prohibition.integration.AbstractDatabaseProhibitionTest; | ||
import com.huaweicloud.sermant.database.prohibition.integration.utils.DynamicConfigUtils; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.EnabledIfSystemProperty; | ||
|
||
/** | ||
* Database write prohibition test | ||
* | ||
* @author zhp | ||
* @since 2024-03-13 | ||
*/ | ||
public class BatchSqlExecuteTest extends AbstractDatabaseProhibitionTest { | ||
private static final String CREATE_SEQUENCE_URL = "http://127.0.0.1:8081/postgresql/batch/createSequenceByBatch?"; | ||
|
||
private static final String DELETE_SEQUENCE_URL = "http://127.0.0.1:8081/postgresql/batch/deleteSequenceByBatch?"; | ||
|
||
private static final String CREATE_TABLE_URL = "http://127.0.0.1:8081/postgresql/batch/createTableByBatch?"; | ||
|
||
private static final String DELETE_TABLE_URL = "http://127.0.0.1:8081/postgresql/batch/deleteTableByBatch?"; | ||
|
||
private static final String INSERT_URL = "http://127.0.0.1:8081/postgresql/batch/saveDataByBatch?"; | ||
|
||
private static final String SELECT_URL = "http://127.0.0.1:8081/postgresql/batch/getDataByBatch?"; | ||
|
||
private static final String UPDATE_URL = "http://127.0.0.1:8081/postgresql/batch/updateDataByBatch?"; | ||
|
||
private static final String DELETE_URL = "http://127.0.0.1:8081/postgresql/batch/deleteDataByBatch?"; | ||
|
||
private static final String CREATE_INDEX_URL = "http://127.0.0.1:8081/postgresql/batch/createIndexByBatch?"; | ||
|
||
private static final String DELETE_INDEX_URL = "http://127.0.0.1:8081/postgresql/batch/deleteIndexByBatch?"; | ||
|
||
private static final String ALTER_TABLE_URL = "http://127.0.0.1:8081/postgresql/batch/alterTable?"; | ||
|
||
/** | ||
* test postgresql database write prohibition function | ||
*/ | ||
@Test | ||
@EnabledIfSystemProperty(named = "database.prohibition.integration.test.type", matches = "postgresql") | ||
public void testPostgresql() throws Exception { | ||
DynamicConfigUtils.updateConfig(DISABLE_DATABASE_WRITE_PROHIBITION_CONFIG); | ||
Thread.sleep(2000); | ||
// test create sequence | ||
testHttpRequest(CREATE_SEQUENCE_URL + "sequenceName=students_seq", DatabaseConstant.SUCCESS_CODE); | ||
// test create table | ||
testHttpRequest(CREATE_TABLE_URL + "sequenceName=students_seq&tableName=students", | ||
DatabaseConstant.SUCCESS_CODE); | ||
// test insert data | ||
Result result = testHttpRequest(INSERT_URL + "tableName=students&columnNames=name," | ||
+ "age&columnValues='lili',11", DatabaseConstant.SUCCESS_CODE); | ||
int id = (int) result.getData(); | ||
// test select data | ||
testHttpRequest(SELECT_URL + "tableName=students&columnName=id&columnValue=100", | ||
DatabaseConstant.SUCCESS_CODE); | ||
// test update data | ||
testHttpRequest(UPDATE_URL + "tableName=students&columnNames=name,id&columnValues=" | ||
+ "'lili'," + id, DatabaseConstant.SUCCESS_CODE); | ||
// test create index | ||
testHttpRequest(CREATE_INDEX_URL + "tableName=students&columnName=age&indexName=idx_age", | ||
DatabaseConstant.SUCCESS_CODE); | ||
// test alter table | ||
testHttpRequest(ALTER_TABLE_URL + "tableName=students&columnName=class&columnType=varchar(255)", | ||
DatabaseConstant.SUCCESS_CODE); | ||
// enable database write prohibition | ||
DynamicConfigUtils.updateConfig(ENABLE_DATABASE_WRITE_PROHIBITION_CONFIG); | ||
Thread.sleep(2000); | ||
// test create sequence | ||
testHttpRequest(CREATE_SEQUENCE_URL + "sequenceName=students_new_seq", | ||
DatabaseConstant.OPERATION_SUCCEED_CODE); | ||
// test create table | ||
testHttpRequest(CREATE_TABLE_URL + "sequenceName=students_new_seq&tableName=students_new", | ||
DatabaseConstant.OPERATION_SUCCEED_CODE); | ||
// test insert data | ||
testHttpRequest(INSERT_URL + "columnNames=name,age&columnValues='line',11", | ||
DatabaseConstant.OPERATION_SUCCEED_CODE); | ||
// test select data | ||
testHttpRequest(SELECT_URL + "tableName=students&columnName=id&columnValue=" + id, | ||
DatabaseConstant.SUCCESS_CODE); | ||
// test update data | ||
testHttpRequest(UPDATE_URL + "columnNames=name,id&columnValues='lili'," + id, | ||
DatabaseConstant.OPERATION_SUCCEED_CODE); | ||
// test create index | ||
testHttpRequest(CREATE_INDEX_URL + "tableName=students&columnName=age&indexName=idx_age", | ||
DatabaseConstant.OPERATION_SUCCEED_CODE); | ||
// test delete data | ||
testHttpRequest(DELETE_URL + "tableName=students&columnNames=id&columnValues=" + id, | ||
DatabaseConstant.OPERATION_SUCCEED_CODE); | ||
// test drop index | ||
testHttpRequest(DELETE_INDEX_URL + "indexName=idx_age", | ||
DatabaseConstant.OPERATION_SUCCEED_CODE); | ||
// test drop table | ||
testHttpRequest(CREATE_TABLE_URL + "tableName=students", | ||
DatabaseConstant.OPERATION_SUCCEED_CODE); | ||
// test drop sequence | ||
testHttpRequest(DELETE_SEQUENCE_URL + "sequenceName=students_seq", | ||
DatabaseConstant.OPERATION_SUCCEED_CODE); | ||
// test alter table | ||
testHttpRequest(ALTER_TABLE_URL + "tableName=students&columnName=grade&columnType=varchar(255)", | ||
DatabaseConstant.OPERATION_SUCCEED_CODE); | ||
// disable database write prohibition | ||
DynamicConfigUtils.updateConfig(DISABLE_DATABASE_WRITE_PROHIBITION_CONFIG); | ||
Thread.sleep(2000); | ||
// test delete data | ||
testHttpRequest(DELETE_URL + "tableName=students&columnNames=id&columnValues=" + id, | ||
DatabaseConstant.SUCCESS_CODE); | ||
// test drop index | ||
testHttpRequest(DELETE_INDEX_URL + "indexName=idx_age", DatabaseConstant.SUCCESS_CODE); | ||
// test drop table | ||
testHttpRequest(DELETE_TABLE_URL + "tableName=students", DatabaseConstant.SUCCESS_CODE); | ||
// test drop sequence | ||
testHttpRequest(DELETE_SEQUENCE_URL + "sequenceName=students_seq", DatabaseConstant.SUCCESS_CODE); | ||
} | ||
} |
Oops, something went wrong.