From 4552b44c3cef4ca56c348af2d9b63f0141f203b2 Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Dec 29 2023 20:36:05 +0000 Subject: test_highstate: render one job per role Split test_highstate CI job into dynamically generates ones, one per Salt role. Signed-off-by: Georg Pfuetzenreuter --- diff --git a/.gitlab-ci.templates/test_highstate.jinja b/.gitlab-ci.templates/test_highstate.jinja new file mode 100644 index 0000000..b5f0a28 --- /dev/null +++ b/.gitlab-ci.templates/test_highstate.jinja @@ -0,0 +1,22 @@ +.test_highstate: + image: registry.opensuse.org/opensuse/infrastructure/containers/heroes-salt-testing-systemd:latest + stage: test + before_script: + - bin/prepare_test_env.sh -g -o Leap,15,5 -s -n + - bin/get_formulas.py -c -d /srv/formula -s --clone-from 'https://gitlab.infra.opensuse.org/saltstack-formulas' --clone-branch production + tags: + - docker + artifacts: + when: always + paths: + - '*.txt' + - 'system/*.txt' + rules: + - if: $CI_COMMIT_BRANCH + +{% for role in roles %} +test_highstate_{{ role }}: + extends: + - .test_highstate + script: bin/test_highstate.sh {{ role }} +{% endfor %} diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index cdf13ff..831f98a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -51,83 +51,6 @@ test_haproxy: # - salt/profile/proxy/* # - salt/role/proxy* -.test_highstate: - image: registry.opensuse.org/opensuse/infrastructure/containers/heroes-salt-testing-systemd:latest - stage: test - before_script: - - bin/prepare_test_env.sh -g -o Leap,15,5 -s -n - - bin/get_formulas.py -c -d /srv/formula -s --clone-from 'https://gitlab.infra.opensuse.org/saltstack-formulas' --clone-branch production - tags: - - docker - artifacts: - when: always - paths: - - '*.txt' - - 'system/*.txt' - rules: - - if: $CI_COMMIT_BRANCH - -test_highstate_1: - extends: - - .test_highstate - script: bin/test_highstate.sh '1,5' - -test_highstate_2: - extends: - - .test_highstate - script: bin/test_highstate.sh '6,10' - -test_highstate_3: - extends: - - .test_highstate - script: bin/test_highstate.sh '11,15' - -test_highstate_4: - extends: - - .test_highstate - script: bin/test_highstate.sh '16,20' - -test_highstate_5: - extends: - - .test_highstate - script: bin/test_highstate.sh '21,25' - -test_highstate_6: - extends: - - .test_highstate - script: bin/test_highstate.sh '26,30' - -test_highstate_7: - extends: - - .test_highstate - script: bin/test_highstate.sh '31,35' - -test_highstate_8: - extends: - - .test_highstate - script: bin/test_highstate.sh '36,40' - -test_highstate_9: - extends: - - .test_highstate - script: bin/test_highstate.sh '41,45' - -test_highstate_10: - extends: - - .test_highstate - script: bin/test_highstate.sh '46,50' - -test_highstate_11: - extends: - - .test_highstate - script: bin/test_highstate.sh '51,55' - -test_highstate_12: - extends: - - .test_highstate - script: bin/test_highstate.sh '56,$' - - test_nginx: image: registry.opensuse.org/opensuse/infrastructure/containers/heroes-salt-testing-nginx:latest stage: test @@ -155,6 +78,26 @@ test_sudo: rules: - if: $CI_COMMIT_BRANCH + +# Dynamically generated jobs +prepare_includes: + script: + - mkdir .gitlab-ci.includes + - bin/render_gitlab_ci_includes.py + artifacts: + paths: + - .gitlab-ci.includes/test_highstate.yml + +test_highstate: + needs: + - prepare_includes + trigger: + include: + - artifact: .gitlab-ci.includes/test_highstate.yml + job: prepare_includes + strategy: depend + + deploy_job: stage: deploy script: diff --git a/bin/render_gitlab_ci_includes.py b/bin/render_gitlab_ci_includes.py new file mode 100755 index 0000000..280d138 --- /dev/null +++ b/bin/render_gitlab_ci_includes.py @@ -0,0 +1,37 @@ +#!/usr/bin/python3 +""" +Script for generating a .gitlab-ci.yml drop-in for test_highstate +(can be expanded for other jobs if needed) +Copyright (C) 2023 openSUSE contributors +Copyright (C) 2023 Georg Pfuetzenreuter + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +""" + +from get_roles import get_roles +from jinja2 import Template +from pathlib import Path + +outdir = '.gitlab-ci.includes' + +# Render include with role based test_highstate chunks +with open('.gitlab-ci.templates/test_highstate.jinja', 'r') as j2: + template = Template(j2.read()) + +rendered = template.render(roles=get_roles()) +print(rendered) + +if Path.is_dir(Path(outdir)): + with open(f'{outdir}/test_highstate.yml', 'w') as fh: + fh.write(rendered) diff --git a/bin/test_highstate.sh b/bin/test_highstate.sh index 3860552..62ca969 100755 --- a/bin/test_highstate.sh +++ b/bin/test_highstate.sh @@ -1,11 +1,10 @@ #!/bin/bash # Validate that a highstate works for all roles +# Takes the role name as an argument -# Parameters: -# $1 - role numbers (actually line numbers for get_roles.py output) to test. Expects a format for sed -n "$1 p" - for example '1,10' or '50,$' -# if $1 is empty, that means sed -n " p" which will test all roles. - +set -u +role="$1" [[ $(whoami) == 'root' ]] || { echo 'Please run this script as root'; exit 1; } @@ -69,61 +68,38 @@ salt $(hostname) mine.update printf '[mysqld]\nskip-grant-tables\n' > /etc/my.cnf.d/danger.cnf systemctl start mariadb -succeeded_roles="" -failed_roles="" -nr=0 - -for role in $(bin/get_roles.py | sed -n "$1 p"); do - nr=$((( $nr + 1))) - rolestatus=0 - sls_role="salt/role/${role/./\/}.sls" - out="$role.txt" - echo "START OF $role" > "$out" - echo_INFO "Testing role $nr: $role" - - cp "$IDFILE_BASE" "$IDFILE" - printf "roles:\n- $role" >> "$IDFILE" - - if [ -x "test/setup/role/$role" ] - then - echo "Preparing test environment for role $role ..." >> "$out" - test/setup/role/$role - fi +out="$role.txt" +echo "START OF $role" > "$out" - salt --out=raw --out-file=/dev/null "$HOSTNAME" saltutil.refresh_pillar +echo_INFO "Testing role: $role" - echo "Testing role $role ..." >> "$out" +if [ -x "test/setup/role/$role" ] +then + echo "Preparing test environment for role $role ..." >> "$out" + test/setup/role/$role +fi - reset_role +salt --out=raw --out-file=/dev/null "$HOSTNAME" saltutil.refresh_pillar - salt-call --retcode-passthrough --state-output=full --output-diff state.apply test=True >> "$out" - rolestatus=$? - echo >> "$out" +salt-call --retcode-passthrough --state-output=full --output-diff state.apply test=True >> "$out" +rolestatus=$? +echo >> "$out" - if test $rolestatus = 0; then - succeeded_roles="$succeeded_roles $role" - echo_PASSED - else - failed_roles="$failed_roles $role" - echo_FAILED - # tail -n100 "$out" - STATUS=1 - fi - echo +if test $rolestatus = 0; then + echo_PASSED +else + echo_FAILED +fi +echo - echo "END OF $role" >> "$out" -done +echo "END OF $role" >> "$out" mkdir system cp /var/log/salt/minion system/minion_log.txt cp /var/log/salt/master system/master_log.txt journalctl --no-pager > system/journal.txt -echo "succeeded roles ($(echo $succeeded_roles | wc -w)): $succeeded_roles" -echo -echo "failed roles ($(echo $failed_roles | wc -w)): $failed_roles" -echo echo 'Output and logs can be found in the job artifacts!' -exit $STATUS +exit $rolestatus vim:expandtab