Skip to content

Commit

Permalink
Merge pull request #1473 from daizhenyu/develop-database-write-prohib…
Browse files Browse the repository at this point in the history
…ition-integration

database write prohibition integration: mongodb and mysql action
  • Loading branch information
Sherlockhan authored Mar 21, 2024
2 parents 1dc4544 + dfda9cc commit 3c1ae15
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: "Database Write Prohibition Plugin MongoDB Test"
description: "Auto test for mongodb write prohibition"
runs:
using: composite
steps:
- name: entry
uses: ./.github/actions/common/entry
with:
log-dir: ./logs/databse-write-prohibition/mongodb
- name: start mongodb
shell: bash
run: |
sudo apt-get install gnupg curl
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo systemctl start mongod
sudo systemctl status mongod
nohup mongo test &
- name: package demos
shell: bash
run: |
mvn package -Dmongodb.version=${{ matrix.mongodbClientVersion }} -DskipTests -Pmongodb-test --file \
sermant-integration-tests/database-write-prohibition-test/pom.xml
- name: start mongodb demo
shell: bash
run: |
nohup java -javaagent:sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar -jar \
-Dsermant_log_dir=${{ env.logDir }}/mongodb \
sermant-integration-tests/database-write-prohibition-test/mongodb-demo/target/mongodb-demo.jar > ${{ env.logDir }}/mongodb.log 2>&1 &
- name: waiting for services start
shell: bash
run: |
ps -ef | grep java
bash ./sermant-integration-tests/scripts/checkService.sh http://127.0.0.1:9098/checkStatus 120
- name: test mongodb
shell: bash
run: |
mvn test -Ddatabase.write.prohibition.integration.test.type=MONGODB --file \
sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/pom.xml
- name: exit
if: always()
uses: ./.github/actions/common/exit
with:
processor-keyword: mongo
- name: if failure then upload error log
uses: actions/upload-artifact@v3
if: ${{ failure() || cancelled() }}
with:
name: (${{ github.job }})-database-write-prohibition-mongodb-(${{ matrix.mongodbClientVersion }})-logs
path: |
./*.log
./logs/**
if-no-files-found: warn
retention-days: 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: "Database Write Prohibition Plugin MySQL Test"
description: "Auto test for mysql write prohibition"
runs:
using: composite
steps:
- name: entry
uses: ./.github/actions/common/entry
with:
log-dir: ./logs/databse-write-prohibition/mysql
- name: start mysql
shell: bash
run: |
sudo apt-get update
sudo apt-get install -y mysql-server
sudo systemctl start mysql.service
user=$(sudo awk -F '=' '/user/ {print $2; exit}' "/etc/mysql/debian.cnf" | tr -d '[:space:]')
password=$(sudo awk -F '=' '/password/ {print $2; exit}' "/etc/mysql/debian.cnf" | tr -d '[:space:]')
sudo mysql -u $user -p$password -e "CREATE DATABASE IF NOT EXISTS test;"
echo "MYSQL_USER=$user" >> $GITHUB_ENV
echo "MYSQL_PASSWORD=$password" >> $GITHUB_ENV
- name: package demos
shell: bash
run: |
mvn package -Dmariadb.version=${{ matrix.mysqlClientVersion }} -DskipTests -Pmysql-test --file \
sermant-integration-tests/database-write-prohibition-test/pom.xml
- name: start mysql demo
shell: bash
run: |
nohup java -javaagent:sermant-agent-${{ env.sermantVersion }}/agent/sermant-agent.jar -jar \
-Dsermant_log_dir=${{ env.logDir }}/mysql -Dmysql.user=${{ env.MYSQL_USER }} -Dmysql.password=${{ env.MYSQL_PASSWORD }} \
sermant-integration-tests/database-write-prohibition-test/mariadb-demo/target/mariadb-demo.jar > ${{ env.logDir }}/mariadb.log 2>&1 &
- name: waiting for services start
shell: bash
run: |
ps -ef | grep java
bash ./sermant-integration-tests/scripts/checkService.sh http://127.0.0.1:9098/static/checkStatus 120
- name: test mysql
shell: bash
run: |
mvn test -Ddatabase.write.prohibition.integration.test.type=MARIADB --file \
sermant-integration-tests/database-write-prohibition-test/database-write-prohibition-integration-test/pom.xml
- name: exit
if: always()
uses: ./.github/actions/common/exit
with:
processor-keyword: mariadb
- name: if failure then upload error log
uses: actions/upload-artifact@v3
if: ${{ failure() || cancelled() }}
with:
name: (${{ github.job }})-database-write-prohibition-mariadb-(${{ matrix.mysqlClientVersion }})-logs
path: |
./*.log
./logs/**
if-no-files-found: warn
retention-days: 2
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,68 @@ jobs:
sed -i '/sermant-backend/d' pom.xml
sed -i '/sermant-integration-tests/d' pom.xml
sed -i '/sermant-injector/d' pom.xml
mvn package -DskipTests -Ptest --file pom.xml
mvn package -DskipTests -Ptest --file pom.xml
test-for-database-write-prohibition-mongodb:
name: Test for database-write-prohibition mongodb
runs-on: ubuntu-latest
needs: [build-agent-and-cache, download-midwares-and-cache]
strategy:
matrix:
include:
- mongodbClientVersion: "3.7.0"
- mongodbClientVersion: "3.8.2"
- mongodbClientVersion: "3.9.1"
- mongodbClientVersion: "3.10.2"
- mongodbClientVersion: "3.11.3"
- mongodbClientVersion: "3.12.14"
- mongodbClientVersion: "4.0.6"
- mongodbClientVersion: "4.1.2"
- mongodbClientVersion: "4.2.3"
- mongodbClientVersion: "4.3.4"
- mongodbClientVersion: "4.4.2"
- mongodbClientVersion: "4.5.1"
- mongodbClientVersion: "4.6.1"
- mongodbClientVersion: "4.7.2"
- mongodbClientVersion: "4.8.2"
- mongodbClientVersion: "4.9.1"
- mongodbClientVersion: "4.10.2"
- mongodbClientVersion: "4.11.1"
fail-fast: false
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 100
- name: set java version to environment
run: |
echo "javaVersion=8" >> $GITHUB_ENV
- name: common operations
uses: ./.github/actions/common/database-write-prohibition
- name: database-write-prohibition-mongodb test for mongodbClientVersion=${{ matrix.mongodbClientVersion }}
uses: ./.github/actions/scenarios/database-write-prohibition/mongodb
test-for-database-write-prohibition-mysql:
name: Test for database-write-prohibition mysql
runs-on: ubuntu-latest
needs: [build-agent-and-cache, download-midwares-and-cache]
strategy:
matrix:
include:
- mysqlClientVersion: "2.6.2"
- mysqlClientVersion: "2.7.6"
- mysqlClientVersion: "2.7.12"
- mysqlClientVersion: "3.0.6"
- mysqlClientVersion: "3.0.11"
- mysqlClientVersion: "3.1.4"
- mysqlClientVersion: "3.2.0"
- mysqlClientVersion: "3.3.3"
fail-fast: false
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 100
- name: set java version to environment
run: |
echo "javaVersion=8" >> $GITHUB_ENV
- name: common operations
uses: ./.github/actions/common/database-write-prohibition
- name: database-write-prohibition-mysql test for mysqlClientVersion=${{ matrix.mysqlClientVersion }}
uses: ./.github/actions/scenarios/database-write-prohibition/mysql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ plugins:
- service-removal
- service-visibility
- tag-transmission
- database-write-prohibition
# dynamicPlugins用于配置支持动态安装的插件, agentmain启动时生效, 允许卸载:
# 1. active类型插件将会主动启用
# 2. passive类型插件需通过指令或接口调用启用
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public class MariadbBatchSqlController {
@Value("${mysql.address}")
private String mysqlAddress;

@Value("${mysql.user}")
private String user;

@Value("${mysql.password}")
private String password;

/**
* createTable
*
Expand All @@ -47,7 +53,7 @@ public class MariadbBatchSqlController {
*/
@RequestMapping("createTable")
public String createTable(String table) {
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
Statement statement = connection.createStatement();
String createTableQuery =
"CREATE TABLE " + table + " (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255),"
Expand All @@ -70,7 +76,7 @@ public String createTable(String table) {
*/
@RequestMapping("dropTable")
public String dropTable(String table) {
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
Statement statement = connection.createStatement();
String dropTableQuery = "DROP TABLE IF EXISTS " + table;
statement.addBatch(dropTableQuery);
Expand All @@ -91,7 +97,7 @@ public String dropTable(String table) {
*/
@RequestMapping("creatIndex")
public String createIndex(String table) {
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
Statement statement = connection.createStatement();
String createIndexQuery = "CREATE INDEX idx_name ON " + table + " (name)";
statement.addBatch(createIndexQuery);
Expand All @@ -112,7 +118,7 @@ public String createIndex(String table) {
*/
@RequestMapping("dropIndex")
public String dropIndex(String table) {
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
Statement statement = connection.createStatement();
String dropIndexQuery = "DROP INDEX idx_name ON " + table;
statement.addBatch(dropIndexQuery);
Expand All @@ -133,7 +139,7 @@ public String dropIndex(String table) {
*/
@RequestMapping("alterTable")
public String alterTable(String table) {
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
Statement statement = connection.createStatement();
String alterTableQuery = "ALTER TABLE " + table + " ADD COLUMN address VARCHAR(255)";
statement.addBatch(alterTableQuery);
Expand All @@ -154,7 +160,7 @@ public String alterTable(String table) {
*/
@RequestMapping("insert")
public String insert(String table) {
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
Statement statement = connection.createStatement();
String insertQuery = "INSERT INTO " + table + " (name, age) VALUES ('John Doe', 25)";
statement.addBatch(insertQuery);
Expand All @@ -175,7 +181,7 @@ public String insert(String table) {
*/
@RequestMapping("update")
public String update(String table) {
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
Statement statement = connection.createStatement();
String updateQuery = "UPDATE " + table + " SET age = 26 WHERE id = 1;";
statement.addBatch(updateQuery);
Expand All @@ -196,7 +202,7 @@ public String update(String table) {
*/
@RequestMapping("delete")
public String delete(String table) {
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
Statement statement = connection.createStatement();
String deleteQuery = "DELETE FROM " + table + " WHERE id = 1";
statement.addBatch(deleteQuery);
Expand All @@ -217,7 +223,7 @@ public String delete(String table) {
*/
@RequestMapping("select")
public String select(String table) {
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
Statement statement = connection.createStatement();
String selectQuery = "SELECT * FROM " + table;
statement.addBatch(selectQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public class MariadbPreparedSqlController {
@Value("${mysql.address}")
private String mysqlAddress;

@Value("${mysql.user}")
private String user;

@Value("${mysql.password}")
private String password;

/**
* createTable
*
Expand All @@ -56,7 +62,7 @@ public class MariadbPreparedSqlController {
public String createTable(String table) {
String createTableQuery =
"CREATE TABLE " + table + " (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)";
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
PreparedStatement statement = connection.prepareStatement(createTableQuery);
statement.executeUpdate();
} catch (SQLException e) {
Expand All @@ -76,7 +82,7 @@ public String createTable(String table) {
@RequestMapping("dropTable")
public String dropTable(String table) {
String dropTableQuery = "DROP TABLE IF EXISTS " + table;
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
PreparedStatement statement = connection.prepareStatement(dropTableQuery);
statement.executeUpdate();
} catch (SQLException e) {
Expand All @@ -96,7 +102,7 @@ public String dropTable(String table) {
@RequestMapping("creatIndex")
public String createIndex(String table) {
String createIndexQuery = "CREATE INDEX idx_name ON " + table + " (name)";
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
PreparedStatement statement = connection.prepareStatement(createIndexQuery);
statement.executeUpdate();
} catch (SQLException e) {
Expand All @@ -116,7 +122,7 @@ public String createIndex(String table) {
@RequestMapping("dropIndex")
public String dropIndex(String table) {
String dropIndexQuery = "DROP INDEX idx_name ON " + table;
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
PreparedStatement statement = connection.prepareStatement(dropIndexQuery);
statement.executeUpdate();
} catch (SQLException e) {
Expand All @@ -136,7 +142,7 @@ public String dropIndex(String table) {
@RequestMapping("alterTable")
public String alterTable(String table) {
String alterTableQuery = "ALTER TABLE " + table + " ADD COLUMN address VARCHAR(255)";
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
PreparedStatement statement = connection.prepareStatement(alterTableQuery);
statement.executeUpdate();
} catch (SQLException e) {
Expand All @@ -156,7 +162,7 @@ public String alterTable(String table) {
@RequestMapping("insert")
public String insert(String table) {
String insertQuery = "INSERT INTO " + table + " (name, age) VALUES (?, ?)";
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
PreparedStatement statement = connection.prepareStatement(insertQuery);
statement.setString(PARAM_INDEX_FIRST, "John Doe");
statement.setInt(PARAM_INDEX_SECOND, TABLE_FIELD_AGE);
Expand All @@ -178,7 +184,7 @@ public String insert(String table) {
@RequestMapping("update")
public String update(String table) {
String updateQuery = "UPDATE " + table + " SET age = ? WHERE id = ?";
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
PreparedStatement statement = connection.prepareStatement(updateQuery);
statement.setInt(PARAM_INDEX_FIRST, TABLE_FIELD_AGE);
statement.setInt(PARAM_INDEX_SECOND, 1);
Expand All @@ -200,7 +206,7 @@ public String update(String table) {
@RequestMapping("delete")
public String delete(String table) {
String deleteQuery = "DELETE FROM " + table + " WHERE id = ?";
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
PreparedStatement statement = connection.prepareStatement(deleteQuery);
statement.setInt(1, 1);
statement.executeUpdate();
Expand All @@ -222,7 +228,7 @@ public String delete(String table) {
public int select(String table) {
int rowCount = 0;
String selectQuery = "SELECT * FROM " + table;
try (Connection connection = DriverManager.getConnection(mysqlAddress)) {
try (Connection connection = DriverManager.getConnection(mysqlAddress, user, password)) {
PreparedStatement statement = connection.prepareStatement(selectQuery);
ResultSet resultSet = statement.executeQuery();
rowCount = countRows(resultSet);
Expand Down
Loading

0 comments on commit 3c1ae15

Please sign in to comment.