Skip to content

Commit

Permalink
database write prohibition integration: mongodb, mysql
Browse files Browse the repository at this point in the history
Signed-off-by: daizhenyu <[email protected]>
  • Loading branch information
daizhenyu committed Mar 14, 2024
1 parent 69bf48f commit 1196942
Show file tree
Hide file tree
Showing 18 changed files with 1,749 additions and 16 deletions.
38 changes: 38 additions & 0 deletions .github/actions/common/database-write-prohibition/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: "Common operations"
description: "do something common for database-write-prohibition plugin test"
runs:
using: "composite"
steps:
- name: Set up JDK ${{ env.javaVersion }}
uses: actions/setup-java@v3
with:
java-version: ${{ env.javaVersion }}
distribution: 'adopt'
cache: maven
- name: get zookeeper from cache
uses: actions/cache@v3
with:
path: apache-zookeeper-3.6.3-bin.tar.gz
key: ${{ runner.os }}-apache-zookeeper-3.6.3
- name: run zookeeper
shell: bash
run: |
tar -zxf apache-zookeeper-3.6.3-bin.tar.gz
bash apache-zookeeper-3.6.3-bin/bin/zkServer.sh start apache-zookeeper-3.6.3-bin/conf/zoo_sample.cfg
- name: cache dependencies
uses: actions/cache@v3
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: cache agent
uses: actions/cache@v3
with:
path: sermant-agent-*/
key: ${{ runner.os }}-agent-${{ github.run_id }}
- name: entry
uses: ./.github/actions/common/entry
with:
log-dir: ./logs/database-write-prohibition/common
key: ${{ runner.os }}-agent-${{ github.run_id }}
117 changes: 117 additions & 0 deletions .github/workflows/database_write_prohibition_integration_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
name: database-write-prohibition integration test
env:
sermantVersion: 1.0.0
on:
push:
pull_request:
branches:
- '*'
paths:
- 'sermant-agentcore/**'
- 'sermant-integration-tests/database-write-prohibition-test/**'
- 'sermant-plugins/sermant-database-write-prohibition/**'
- '.github/workflows/database_write_prohibition_integration_test.yml'
- '.github/actions/common/database-write-prohibition/**'
- '.github/actions/scenarios/database-write-prohibition/**'
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}-${{ github.head_ref }}
cancel-in-progress: true
jobs:
download-midwares-and-cache:
name: download midwares and cache
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: cache zookeeper
uses: actions/cache@v3
with:
path: apache-zookeeper-3.6.3-bin.tar.gz
key: ${{ runner.os }}-apache-zookeeper-3.6.3
restore-keys: |
${{ runner.os }}-apache-zookeeper-3.6.3
- name: download zookeeper
run: |
export ROOT_PATH=$(pwd)
bash ./sermant-integration-tests/scripts/tryDownloadMidware.sh zk
- name: cache mongodb
uses: actions/cache@v3
with:
path: mongodb-linux-x86_64-ubuntu2004-7.0.6.tgz
key: ${{ runner.os }}-mongodb-linux-x86_64-ubuntu2004-7.0.6
restore-keys: |
${{ runner.os }}-mongodb-linux-x86_64-ubuntu2004-7.0.6
- name: download mongodb
run: |
export ROOT_PATH=$(pwd)
bash ./sermant-integration-tests/scripts/tryDownloadMidware.sh mongodb
- name: cache mysql
uses: actions/cache@v3
with:
path: mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz
key: ${{ runner.os }}-mysql-8.0.28-linux-glibc2.12-x86_64
restore-keys: |
${{ runner.os }}-mysql-8.0.28-linux-glibc2.12-x86_64
- name: download mysql
run: |
export ROOT_PATH=$(pwd)
bash ./sermant-integration-tests/scripts/tryDownloadMidware.sh mysql
build-agent-and-cache:
name: build agent and cache
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 8
uses: actions/setup-java@v3
with:
java-version: '8'
distribution: 'adopt'
cache: maven
- name: cache agent
uses: actions/cache@v3
with:
path: sermant-agent-*/
key: ${{ runner.os }}-agent-${{ github.run_id }}
- name: package agent
run: |
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
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:
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 mongodbVersion=${{ matrix.mongodbVersion }}
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:
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 mysqlVersion=${{ matrix.mysqlVersion }}
uses: ./.github/actions/scenarios/database-write-prohibition/mysql
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public class DatabaseConstant {
public static final String SQL_EXCEPTION_MESSAGE_PREFIX = "Database prohibit to write";

/**
* succeed to prohibit database status code
* fail to execute database write operation status code
*/
public static final int SUCCEED_PROHIBITION_CODE = 100;
public static final String OPERATION_FAIL_CODE = "100";

/**
* failed to prohibit database status code
* succeed to execute database write operation status code
*/
public static final int FAILED_PROHIBITION_CODE = 101;
public static final String OPERATION_SUCCEED_CODE = "101";

private DatabaseConstant() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,4 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
/*
* 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.mariadb;

import com.huaweicloud.sermant.database.prohibition.common.constant.DatabaseConstant;
import com.huaweicloud.sermant.database.prohibition.integration.utils.DynamicConfigUtils;
import com.huaweicloud.sermant.database.prohibition.integration.utils.HttpRequestUtils;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;

/**
* mariadb write prohibition integration test
*
* @author daizhenyu
* @since 2024-03-12
**/
@EnabledIfSystemProperty(named = "database.write.prohibition.integration.test.type", matches = "MARIADB")
public class MariaDbProhibitionTest {
private static final String DATA_COUNT = "1";

private static final String LINE_SEPARATOR = System.getProperty("line.separator");

private static final String MARIADB_CONFIG_ON = "enableMySqlWriteProhibition: true" + LINE_SEPARATOR +
"mySqlDatabases:" + LINE_SEPARATOR +
" - test";

private static final String MARIADB_CONFIG_OFF = "enableMySqlWriteProhibition: false" + LINE_SEPARATOR +
"mySqlDatabases:" + LINE_SEPARATOR +
" - test";

@BeforeAll
public static void before() throws Exception {
DynamicConfigUtils.updateConfig(MARIADB_CONFIG_OFF);
Thread.sleep(3000);

// prepare test data
HttpRequestUtils.doGet("http://127.0.0.1:9098/static/createTable?table=tableSelect");
HttpRequestUtils.doGet("http://127.0.0.1:9098/static/insert?table=tableSelect");
HttpRequestUtils.doGet("http://127.0.0.1:9098/static/createTable?table=tableDrop");
HttpRequestUtils.doGet("http://127.0.0.1:9098/static/createTable?table=tableData");
HttpRequestUtils.doGet("http://127.0.0.1:9098/static/insert?table=tableData");

DynamicConfigUtils.updateConfig(MARIADB_CONFIG_ON);
Thread.sleep(3000);
}

/**
* select
*/
@Test
public void testSelect() {
Assertions.assertEquals(DATA_COUNT, HttpRequestUtils
.doGet("http://127.0.0.1:9098/static/select?table=tableSelect"));
Assertions.assertEquals(DatabaseConstant.OPERATION_SUCCEED_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/batch/select?table=tableSelect"));
Assertions.assertEquals(DATA_COUNT, HttpRequestUtils
.doGet("http://127.0.0.1:9098/prepared/select?table=tableSelect"));
}

/**
* create table
*/
@Test
public void testTable() {
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/static/createTable?table=tableCreate"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/batch/createTable?table=tableCreate"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/prepared/createTable?table=tableCreate"));
}

/**
* drop table
*/
@Test
public void testDropCollection() {
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/static/dropTable?table=tableDrop"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/batch/dropTable?table=tableDrop"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/prepared/dropTable?table=tableDrop"));
}

/**
* insert
*/
@Test
public void testInsert() {
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/static/insert?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/batch/insert?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/prepared/insert?table=tableData"));
}

/**
* update
*/
@Test
public void testUpdate() {
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/static/update?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/batch/update?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/prepared/update?table=tableData"));
}

/**
* delete
*/
@Test
public void testDelete() {
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/static/delete?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/batch/delete?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/prepared/delete?table=tableData"));
}

/**
* create index
*/
@Test
public void testCreateIndex() {
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/static/creatIndex?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/batch/creatIndex?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/prepared/creatIndex?table=tableData"));
}

/**
* delete index
*/
@Test
public void testDeleteIndex() throws Exception {
DynamicConfigUtils.updateConfig(MARIADB_CONFIG_OFF);
Thread.sleep(3000);

Assertions.assertEquals(DatabaseConstant.OPERATION_SUCCEED_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/static/creatIndex?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_SUCCEED_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/batch/creatIndex?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_SUCCEED_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/prepared/creatIndex?table=tableData"));

DynamicConfigUtils.updateConfig(MARIADB_CONFIG_ON);
Thread.sleep(3000);

Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/static/dropIndex?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/batch/dropIndex?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/prepared/dropIndex?table=tableData"));
}

/**
* alter table
*/
@Test
public void testAlterTable() {
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/static/alterTable?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/batch/alterTable?table=tableData"));
Assertions.assertEquals(DatabaseConstant.OPERATION_FAIL_CODE, HttpRequestUtils
.doGet("http://127.0.0.1:9098/prepared/alterTable?table=tableData"));
}
}
Loading

0 comments on commit 1196942

Please sign in to comment.