From 12a53c69da98ca4912aaa350e9efd13528b9c257 Mon Sep 17 00:00:00 2001 From: shown Date: Tue, 27 Aug 2024 10:44:04 +0800 Subject: [PATCH 1/2] [optimize]: update github action (#2608) Signed-off-by: yuluo-yx Signed-off-by: shown --- .github/workflows/backend-build-test.yml | 9 +- .github/workflows/doc-build-test.yml | 94 +++---------------- .github/workflows/nightly-build.yml | 5 +- .../version-v1.5.x/download.md | 2 +- package.json | 5 - script/ci/docs/check_file_name.json | 8 ++ script/ci/docs/check_file_name.py | 65 +++++++++++++ {.github => script/ci}/exclude_files.txt | 2 +- .../ci/github-actions/setup-deps/action.yml | 28 ++++++ {.github => script/ci}/link_check.json | 0 10 files changed, 122 insertions(+), 96 deletions(-) delete mode 100644 package.json create mode 100644 script/ci/docs/check_file_name.json create mode 100644 script/ci/docs/check_file_name.py rename {.github => script/ci}/exclude_files.txt (99%) create mode 100644 script/ci/github-actions/setup-deps/action.yml rename {.github => script/ci}/link_check.json (100%) diff --git a/.github/workflows/backend-build-test.yml b/.github/workflows/backend-build-test.yml index 38aaaaf9bb3..7202646ebad 100644 --- a/.github/workflows/backend-build-test.yml +++ b/.github/workflows/backend-build-test.yml @@ -39,16 +39,15 @@ on: - 'material/**' jobs: - build: + backend-build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: Set up JDK 17 - uses: actions/setup-java@v1 - with: - java-version: 17 + - uses: ./script/ci/github-actions/setup-deps + - name: Build with Maven run: mvn clean -B package -Prelease --file pom.xml + - name: Upload coverage reports to Codecov uses: codecov/codecov-action@v4.0.1 with: diff --git a/.github/workflows/doc-build-test.yml b/.github/workflows/doc-build-test.yml index 670da80af79..18c14eda37c 100644 --- a/.github/workflows/doc-build-test.yml +++ b/.github/workflows/doc-build-test.yml @@ -28,103 +28,37 @@ on: - 'home/**' jobs: - build: + docs-build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: fetch-depth: 0 + - uses: actions/setup-python@v4 + with: + python-version: '3.8' + - name: Check Markdown uses: DavidAnson/markdownlint-cli2-action@v16 with: globs: './home/**/*.md' - - name: Check filename in home/blog - run: | - TARGET_DIR="./home/blog" - invalid_files=() - while IFS= read -r -d '' file; do - filename=$(basename "$file") - if [[ ! "$filename" =~ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*$ ]]; then - invalid_files+=("$file") - fi - done < <(find "$TARGET_DIR" -type f -print0) - if [ ${#invalid_files[@]} -ne 0 ]; then - echo "Error: The following files have invalid names:(File name should only contain lowercase letters, numbers, and hyphens.)" - for invalid_file in "${invalid_files[@]}"; do - echo "$invalid_file" - done - exit 1 - else - echo "All file names are valid." - fi - - name: Check filename in home/docs - run: | - TARGET_DIR="./home/docs" - invalid_files=() - while IFS= read -r -d '' file; do - filename=$(basename "$file") - if [[ ! "$filename" =~ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*$ ]]; then - invalid_files+=("$file") - fi - done < <(find "$TARGET_DIR" -type f -print0) - if [ ${#invalid_files[@]} -ne 0 ]; then - echo "Error: The following files have invalid names:(File name should only contain lowercase letters, numbers, and hyphens.)" - for invalid_file in "${invalid_files[@]}"; do - echo "$invalid_file" - done - exit 1 - else - echo "All file names are valid." - fi - - name: Check filename in /zh-cn/docusaurus-plugin-content-blog - run: | - TARGET_DIR="./home/i18n/zh-cn/docusaurus-plugin-content-blog" - invalid_files=() - while IFS= read -r -d '' file; do - filename=$(basename "$file") - if [[ ! "$filename" =~ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*$ ]]; then - invalid_files+=("$file") - fi - done < <(find "$TARGET_DIR" -type f -print0) - if [ ${#invalid_files[@]} -ne 0 ]; then - echo "Error: The following files have invalid names:(File name should only contain lowercase letters, numbers, and hyphens.)" - for invalid_file in "${invalid_files[@]}"; do - echo "$invalid_file" - done - exit 1 - else - echo "All file names are valid." - fi - - name: Check filename in /home/i18n/zh-cn/docusaurus-plugin-content-docs/current - run: | - TARGET_DIR="./home/i18n/zh-cn/docusaurus-plugin-content-docs/current" - invalid_files=() - while IFS= read -r -d '' file; do - filename=$(basename "$file") - if [[ ! "$filename" =~ ^[_a-z0-9-]+(\.[_a-z0-9-]+)*$ ]]; then - invalid_files+=("$file") - fi - done < <(find "$TARGET_DIR" -type f -print0) - if [ ${#invalid_files[@]} -ne 0 ]; then - echo "Error: The following files have invalid names:(File name should only contain lowercase letters, numbers, and hyphens.)" - for invalid_file in "${invalid_files[@]}"; do - echo "$invalid_file" - done - exit 1 - else - echo "All file names are valid." - fi - - name: Dead Link + + - name: Check filenames + run: python ./script/ci/docs/check_file_name.py ./script/ci/docs/check_file_name.json + + - name: Dead Link Check run: | sudo npm install -g markdown-link-check@3.8.7 for file in $(find ./home -name "*.md"); do - if ! grep -Fxq "$file" .github/exclude_files.txt; then - markdown-link-check -c .github/link_check.json -q "$file" + if ! grep -Fxq "$file" ./script/ci/exclude_files.txt; then + markdown-link-check -c ./script/ci/link_check.json -q "$file" fi done + - name: NPM INSTALL working-directory: home run: npm install + - name: NPM BUILD working-directory: home run: npm run build diff --git a/.github/workflows/nightly-build.yml b/.github/workflows/nightly-build.yml index edbecde000d..d41d6528839 100644 --- a/.github/workflows/nightly-build.yml +++ b/.github/workflows/nightly-build.yml @@ -30,10 +30,7 @@ jobs: if: ${{ github.repository == 'apache/hertzbeat' }} steps: - uses: actions/checkout@v4 - - name: Set up JDK 17 - uses: actions/setup-java@v1 - with: - java-version: 17 + - uses: ./script/ci/github-actions/setup-deps - name: Build the Frontend run: | diff --git a/home/i18n/zh-cn/docusaurus-plugin-content-docs/version-v1.5.x/download.md b/home/i18n/zh-cn/docusaurus-plugin-content-docs/version-v1.5.x/download.md index 51a2fadb805..7de1466e664 100644 --- a/home/i18n/zh-cn/docusaurus-plugin-content-docs/version-v1.5.x/download.md +++ b/home/i18n/zh-cn/docusaurus-plugin-content-docs/version-v1.5.x/download.md @@ -30,7 +30,7 @@ sidebar_label: Download ## Docker 镜像版本 -> Apache HertzBeat 为每个版本制作了 Docker 镜像. 你可以从 [Docker Hub](https://hub.docker.com/r/apache/hertzbeat) 拉取使用. +> Apache HertzBeat 为每个版本制作了 Docker 镜像. 您可以从 [Docker Hub](https://hub.docker.com/r/apache/hertzbeat) 拉取使用. - HertzBeat - HertzBeat Collector diff --git a/package.json b/package.json deleted file mode 100644 index 5d3eff22d75..00000000000 --- a/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "devDependencies": { - "markdownlint": "^0.34.0" - } -} diff --git a/script/ci/docs/check_file_name.json b/script/ci/docs/check_file_name.json new file mode 100644 index 00000000000..16ca62b61c8 --- /dev/null +++ b/script/ci/docs/check_file_name.json @@ -0,0 +1,8 @@ +{ + "directories": [ + "./home/blog", + "./home/docs", + "./home/i18n/zh-cn/docusaurus-plugin-content-blog", + "./home/i18n/zh-cn/docusaurus-plugin-content-docs/current" + ] +} diff --git a/script/ci/docs/check_file_name.py b/script/ci/docs/check_file_name.py new file mode 100644 index 00000000000..f9c5207d2b1 --- /dev/null +++ b/script/ci/docs/check_file_name.py @@ -0,0 +1,65 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +import os +import re +import sys +import json + +def read_config(file_path): + try: + with open(file_path, 'r') as file: + config = json.load(file) + return config.get("directories", []) + except Exception as e: + print(f"Error reading configuration file: {e}") + sys.exit(1) + + +def find_invalid_files(directories): + invalid_files = [] + pattern = re.compile(r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*$') + + for target_dir in directories: + print(f"Checking directory: {target_dir}") + for root, _, files in os.walk(target_dir): + for filename in files: + if not pattern.match(filename): + invalid_files.append(os.path.join(root, filename)) + + if invalid_files: + print("\nError: The following files have invalid names (file names should only contain lowercase letters, numbers, and hyphens):") + for invalid_file in invalid_files: + print(invalid_file) + sys.exit(1) + else: + print("All file names are valid.") + + +if __name__ == "__main__": + # usage: python check_filenames.py config.json + if len(sys.argv) != 2: + print("Usage: python xxx_script.py config.json") + sys.exit(1) + + config_file = sys.argv[1] + directories = read_config(config_file) + if not directories: + print("\nNo directories found in configuration file.") + sys.exit(1) + + find_invalid_files(directories) diff --git a/.github/exclude_files.txt b/script/ci/exclude_files.txt similarity index 99% rename from .github/exclude_files.txt rename to script/ci/exclude_files.txt index ce60d627e71..174f52f838f 100644 --- a/.github/exclude_files.txt +++ b/script/ci/exclude_files.txt @@ -41,4 +41,4 @@ ./home/i18n/zh-cn/docusaurus-plugin-content-docs/version-v1.5.x/others/resource.md ./home/i18n/zh-cn/docusaurus-plugin-content-docs/version-v1.5.x/start/tdengine-init.md ./home/i18n/zh-cn/docusaurus-plugin-content-docs/version-v1.4.x/start/tdengine-init.md -./home/i18n/zh-cn/docusaurus-plugin-content-docs/current/start/tdengine-init.md \ No newline at end of file +./home/i18n/zh-cn/docusaurus-plugin-content-docs/current/start/tdengine-init.md diff --git a/script/ci/github-actions/setup-deps/action.yml b/script/ci/github-actions/setup-deps/action.yml new file mode 100644 index 00000000000..e0d39fc280c --- /dev/null +++ b/script/ci/github-actions/setup-deps/action.yml @@ -0,0 +1,28 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +name: setup-deps +description: Install host system dependencies + +runs: + using: composite + steps: + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + distribution: "zulu" + java-version: 17 diff --git a/.github/link_check.json b/script/ci/link_check.json similarity index 100% rename from .github/link_check.json rename to script/ci/link_check.json From 6e430bcab6fbd58a00fb5229522bdfabd53f67f5 Mon Sep 17 00:00:00 2001 From: TemirlanBasitov <57500808+TemirlanBasitov@users.noreply.github.com> Date: Tue, 27 Aug 2024 05:01:16 +0200 Subject: [PATCH 2/2] Add push dao unit tests (#2614) Co-authored-by: I744506 Co-authored-by: shown --- .../push/dao/PushMetricsDaoTest.java | 77 +++++++++++++++ .../push/dao/PushMonitorDaoTest.java | 94 +++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 push/src/test/java/org/apache/hertzbeat/push/dao/PushMetricsDaoTest.java create mode 100644 push/src/test/java/org/apache/hertzbeat/push/dao/PushMonitorDaoTest.java diff --git a/push/src/test/java/org/apache/hertzbeat/push/dao/PushMetricsDaoTest.java b/push/src/test/java/org/apache/hertzbeat/push/dao/PushMetricsDaoTest.java new file mode 100644 index 00000000000..c90a9c7e4da --- /dev/null +++ b/push/src/test/java/org/apache/hertzbeat/push/dao/PushMetricsDaoTest.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.hertzbeat.push.dao; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.hertzbeat.common.entity.push.PushMetrics; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoExtension; + +/** + * test case for {@link PushMetricsDao} + */ + +@ExtendWith(MockitoExtension.class) +public class PushMetricsDaoTest { + @Mock + private PushMetricsDao pushMetricsDao; + + @InjectMocks + private PushMetricsDaoTest pushMetricsDaoTest; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void shallFindFirstByMonitorIdOrderByTimeDesc() { + + PushMetrics expectedMetrics = new PushMetrics(); + expectedMetrics.setMonitorId(1L); + expectedMetrics.setTime(System.currentTimeMillis()); + + when(pushMetricsDao.findFirstByMonitorIdOrderByTimeDesc(1L)).thenReturn(expectedMetrics); + + PushMetrics actualMetrics = pushMetricsDao.findFirstByMonitorIdOrderByTimeDesc(1L); + + assertEquals(expectedMetrics, actualMetrics); + verify(pushMetricsDao, times(1)).findFirstByMonitorIdOrderByTimeDesc(1L); + } + + @Test + void shallDeleteAllByTimeBefore() { + + doNothing().when(pushMetricsDao).deleteAllByTimeBefore(anyLong()); + + pushMetricsDao.deleteAllByTimeBefore(1000L); + + verify(pushMetricsDao, times(1)).deleteAllByTimeBefore(1000L); + } +} diff --git a/push/src/test/java/org/apache/hertzbeat/push/dao/PushMonitorDaoTest.java b/push/src/test/java/org/apache/hertzbeat/push/dao/PushMonitorDaoTest.java new file mode 100644 index 00000000000..3681154b161 --- /dev/null +++ b/push/src/test/java/org/apache/hertzbeat/push/dao/PushMonitorDaoTest.java @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.hertzbeat.push.dao; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Optional; + +import org.apache.hertzbeat.common.entity.manager.Monitor; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.jupiter.MockitoExtension; + +/** + * test case for {@link PushMonitorDao} + */ + +@ExtendWith(MockitoExtension.class) +public class PushMonitorDaoTest { + @Mock + private PushMonitorDao pushMonitorDao; + + @InjectMocks + private PushMonitorDaoTest pushMonitorDaoTest; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void shallSaveMonitor() { + + Monitor monitor = new Monitor(); + monitor.setId(1L); + monitor.setName("Test Monitor"); + + when(pushMonitorDao.save(any(Monitor.class))).thenReturn(monitor); + + Monitor savedMonitor = pushMonitorDao.save(monitor); + + assertEquals(monitor, savedMonitor); + verify(pushMonitorDao, times(1)).save(monitor); + } + + @Test + void shallFindById() { + Monitor monitor = new Monitor(); + monitor.setId(1L); + monitor.setName("Test Monitor"); + + when(pushMonitorDao.findById(1L)).thenReturn(Optional.of(monitor)); + + Optional foundMonitor = pushMonitorDao.findById(1L); + + assertTrue(foundMonitor.isPresent()); + assertEquals(monitor, foundMonitor.get()); + verify(pushMonitorDao, times(1)).findById(1L); + } + + @Test + void shallDeleteById() { + doNothing().when(pushMonitorDao).deleteById(1L); + + pushMonitorDao.deleteById(1L); + + verify(pushMonitorDao, times(1)).deleteById(1L); + } +}