Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 24 additions & 42 deletions .github/scripts/process-plugins.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,42 @@
#!/usr/bin/env python3

from sys import argv
import re
import os
import json
from sys import argv

common = argv[1] == 'true'

with open('package_directories.txt') as f:
line_packages = f.readline().strip('\n')
n = len(line_packages)
a = line_packages[1:n-1]
input_packages = a.split(',')
packages = set()
for package in input_packages:
packages.add(package.strip('"/').removeprefix('src/'))

with open('plugins.txt') as f:
line_plugins = f.readline().strip('\n')
n = len(line_plugins)
a = line_plugins[1:n-1]
input_plugins = a.split(',')
plugins = set()
for plugin in input_plugins:
plugins.add(plugin.strip('"/').removeprefix('src/'))
def read_file(file_path):
with open(file_path) as f:
return f.readline().strip('\n')[1:-1].split(',')

list_plugins = set()
list_packages = set()
def process_items(items):
return {item.strip('"/').removeprefix('src/') for item in items}

packages = process_items(read_file('package_directories.txt'))
plugins = process_items(read_file('plugins.txt'))

list_plugins = set()
for plugin in plugins:
list_plugins.add(plugin)
try:
found = re.search('(.*)\/(?:plugin\.pm|(?:lib|mode|custom)\/.+)', plugin).group(1)
list_plugins.add(found)
except AttributeError:
pass
match = re.search(r'(.*)/(?:plugin\.pm|(?:lib|mode|custom)/.+)', plugin)
if match:
list_plugins.add(match.group(1))

for filepath in os.popen('find packaging -type f -name pkg.json').read().split('\n')[0:-1]:
packaging_file = open(filepath)
packaging = json.load(packaging_file)
packaging_file.close()
packaging_path = re.search('.*\/(centreon-plugin-.*)\/pkg.json', filepath).group(1)

if not packaging_path == packaging["pkg_name"]:
packaging_path = packaging["pkg_name"]

directory_path = re.search('^(.+)\/pkg.json', filepath).group(1)

if common:
list_packages.add(packaging_path)
elif directory_path in packages:
list_packages = set()
for filepath in os.popen('find packaging -type f -name pkg.json').read().splitlines():
with open(filepath) as packaging_file:
packaging = json.load(packaging_file)
packaging_path = re.search(r'.*/(centreon-plugin-.*)/pkg.json', filepath).group(1)
packaging_path = packaging_path if packaging_path == packaging["pkg_name"] else packaging["pkg_name"]
directory_path = re.search(r'^(.+)/pkg.json', filepath).group(1)

if common or directory_path in packages:
list_packages.add(packaging_path)
else:
for pkg_file in packaging["files"]:
pkg_file_dir = pkg_file.strip('/').removeprefix('src/')
if pkg_file_dir in list_plugins:
if pkg_file.strip('/').removeprefix('src/') in list_plugins:
list_packages.add(packaging_path)

print(*list_packages)
print(*list_packages)
144 changes: 144 additions & 0 deletions .github/workflows/plugins-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
name: plugins-tests

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

on:
workflow_dispatch:
pull_request:
paths:
- '.github/workflows/plugins.yml'
- '.github/scripts/plugins-source.container.pl'
- '.github/packaging/centreon-plugin.yaml.template'
- 'src/**'
- 'packaging/**'
- 'tests/**'
push:
branches:
- develop
- master
paths:
- '.github/workflows/plugins.yml'
- '.github/scripts/plugins-source.container.pl'
- '.github/packaging/centreon-plugin.yaml.template'
- 'src/**'
- 'packaging/**'
- 'tests/**'

jobs:
get-environment-tests:
uses: ./.github/workflows/get-environment.yml

get-plugins-tests:
runs-on: ubuntu-22.04
outputs:
plugins: ${{ steps.get_plugins.outputs.plugins }}
robot_tests: ${{ steps.list_tests.outputs.robot_tests }}
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
fetch-depth: 0

- uses: actions/setup-python@39cd14951b08e74b54015e9e001cdefcf80e669f # v5.1.1
with:
python-version: '3.9'

- uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
id: filter
with:
base: ${{ github.ref }}
list-files: shell
filters: |
common:
- added|deleted|modified: src/centreon/**
- modified: .github/packaging/centreon-plugin.yaml.template
packages:
- added|modified: packaging/**
plugins:
- added|modified: src/**
robot_tests:
- added|modified: tests/**/*.robot
- added|modified: tests/**/*.json
- added|modified: tests/**/*.snmpwalk

- name: Transform plugins to directories
run: |
folders=()
for package_folder in ${{ steps.filter.outputs.packages_files }}; do
echo "Adding $(dirname $package_folder) to folders"
folders+=($(dirname $package_folder))
done
unique_folders=($(printf "%s\n" "${folders[@]}" | sort -u | tr '\n' ' '))
jq --compact-output --null-input '$ARGS.positional' --args -- ${unique_folders[@]} > package_directories.txt

echo "PACKAGES: $(<package_directories.txt)"

files=()
for plugin_file in ${{ steps.filter.outputs.plugins_files }}; do
echo "Adding $plugin_file to files"
files+=($plugin_file)
done
unique_files=($(printf "%s\n" "${files[@]}" | sort -u | tr '\n' ' '))
jq --compact-output --null-input '$ARGS.positional' --args -- ${unique_files[@]} > plugins.txt

echo "PLUGINS: $(<plugins.txt)"

tests_folders=()
echo "robot_tests_files: ${{ steps.filter.outputs.robot_tests_files }}"
for test_file in ${{ steps.filter.outputs.robot_tests_files }}; do
test_folder=$(dirname $test_file)
if [[ ! $unique_files =~ ${test_folder/tests/src} ]]; then
echo "Adding $test_folder to tests_folders"
tests_folders+=($test_folder)
fi
done
unique_tests_folders=($(printf "%s\n" "${tests_folders[@]}" | sort -u | tr '\n' ' '))
jq --compact-output --null-input '$ARGS.positional' --args -- ${unique_tests_folders[@]} > tests_directories.txt

echo "ROBOT_TESTS: $(<tests_directories.txt)"
shell: bash

- name: Get plugins for build
id: get_plugins
if: ${{ steps.filter.outputs.common == 'true' || steps.filter.outputs.packages == 'true' || steps.filter.outputs.plugins == 'true' }}
run: |
PLUGINS="$(python3 .github/scripts/process-plugins.py '${{ steps.filter.outputs.common == 'true' }}')"
echo "plugins=$(echo $PLUGINS)" >> $GITHUB_OUTPUT
if [ "$PLUGINS" == '' ]; then
echo "::notice::There are no modifications to the plugins packages"
fi
shell: bash

- name: List tests not related to a new or updated plugin
id: list_tests
run: |
# Get the unique directories of the robot tests
robot_tests_folders=()
for robot_test_file in ${{ steps.filter.outputs.robot_tests_files }}; do
robot_tests_folders+=($(dirname $robot_test_file))
done
unique_robot_tests_folders=($(printf "%s\n" "${robot_tests_folders[@]}" | sort -u))
robot_tests_folders=("${unique_robot_tests_folders[@]}")

packages=$(<package_directories.txt)
plugins=$(<plugins.txt)

# Get the robot tests that are not related to the added/modified plugins and packages
tests_dirs=()
for test_dir in "${robot_tests_folders[@]}"; do
if [[ ! $plugins =~ ${test_dir/tests/src} ]]; then
found=false
for package_file in $(echo $packages | jq -r '.[]'); do
if grep -q $(echo $test_dir | sed 's|tests/||g') $package_file/pkg.json; then
found=true
break
fi
done
if ! $found; then
tests_dirs+=($test_dir)
fi
fi
done
echo "robot_tests=$(echo ${tests_dirs[@]})" >> $GITHUB_OUTPUT
shell: bash
65 changes: 58 additions & 7 deletions .github/workflows/plugins.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ on:
- '.github/packaging/centreon-plugin.yaml.template'
- 'src/**'
- 'packaging/**'
- 'tests/**'
push:
branches:
- develop
Expand All @@ -23,6 +24,7 @@ on:
- '.github/packaging/centreon-plugin.yaml.template'
- 'src/**'
- 'packaging/**'
- 'tests/**'

jobs:
get-environment:
Expand All @@ -32,6 +34,7 @@ jobs:
runs-on: ubuntu-22.04
outputs:
plugins: ${{ steps.get_plugins.outputs.plugins }}
robot_tests: ${{ steps.list_tests.outputs.robot_tests }}
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
with:
Expand All @@ -54,24 +57,39 @@ jobs:
- added|modified: packaging/**
plugins:
- added|modified: src/**
robot_tests:
- added|modified: tests/**/*.robot
- added|modified: tests/**/*.json
- added|modified: tests/**/*.snmpwalk

- name: transform to directories
- name: Transform to directories
run: |
folders=()
for f in ${{ steps.filter.outputs.packages_files }}; do
echo "Adding $(dirname $f) to folders"
folders+=($(dirname $f))
for package_folder in ${{ steps.filter.outputs.packages_files }}; do
echo "Adding $(dirname $package_folder) to folders"
folders+=($(dirname $package_folder))
done
unique_folders=($(printf "%s\n" "${folders[@]}" | sort -u | tr '\n' ' '))
jq --compact-output --null-input '$ARGS.positional' --args -- ${unique_folders[@]} > package_directories.txt

files=()
for f in ${{ steps.filter.outputs.plugins_files }}; do
echo "Adding $f to files"
files+=($f)
for plugin_file in ${{ steps.filter.outputs.plugins_files }}; do
echo "Adding $plugin_file to files"
files+=($plugin_file)
done
unique_files=($(printf "%s\n" "${files[@]}" | sort -u | tr '\n' ' '))
jq --compact-output --null-input '$ARGS.positional' --args -- ${unique_files[@]} > plugins.txt

tests_folders=()
for test_file in ${{ steps.filter.outputs.robot_tests_files }}; do
test_folder=$(dirname $test_file)
if [[ ! $unique_files =~ ${test_folder/tests/src} ]]; then
echo "Adding $test_folder to tests_folders"
tests_folders+=($test_folder)
fi
done
unique_tests_folders=($(printf "%s\n" "${tests_folders[@]}" | sort -u | tr '\n' ' '))
jq --compact-output --null-input '$ARGS.positional' --args -- ${unique_tests_folders[@]} > tests_directories.txt
shell: bash

- name: Get plugins for build
Expand All @@ -84,7 +102,39 @@ jobs:
if [ "$PLUGINS" == '' ]; then
echo "::notice::There are no modifications to the plugins packages"
fi
shell: bash

- name: List tests not related to a new or updated plugin
id: list_tests
run: |
# Get the unique directories of the robot tests
robot_tests_folders=()
for robot_test_file in ${{ steps.filter.outputs.robot_tests_files }}; do
robot_tests_folders+=($(dirname $robot_test_file))
done
unique_robot_tests_folders=($(printf "%s\n" "${robot_tests_folders[@]}" | sort -u))
robot_tests_folders=("${unique_robot_tests_folders[@]}")

packages=$(<package_directories.txt)
plugins=$(<plugins.txt)

# Get the robot tests that are not related to the added/modified plugins and packages
tests_dirs=()
for test_dir in "${robot_tests_folders[@]}"; do
if [[ ! $plugins =~ ${test_dir/tests/src} ]]; then
found=false
for package_file in $(echo $packages | jq -r '.[]'); do
if grep -q $(echo $test_dir | sed 's|tests/||g') $package_file/pkg.json; then
found=true
break
fi
done
if ! $found; then
tests_dirs+=($test_dir)
fi
fi
done
echo "robot_tests=$(echo ${tests_dirs[@]})" >> $GITHUB_OUTPUT
shell: bash

unit-tests:
Expand Down Expand Up @@ -334,6 +384,7 @@ jobs:
cache-key: ${{ github.sha }}-${{ github.run_id }}-${{ matrix.package_extension }}-${{ matrix.distrib }}
package-extension: ${{ matrix.package_extension }}
plugin-list: ${{ needs.get-plugins.outputs.plugins }}
tests-list: ${{ needs.get-plugins.outputs.robot_tests }}

- name: Upload apt/dnf logs as artifacts if tests failed
if: failure()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"centreon/plugins/script_custom.pm",
"apps/automation/ansible/tower/"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"centreon/plugins/script_custom.pm",
"apps/ipfabric/"
]
}
}
Loading