From 7360d0ff73d982f28439057b98741bea538b31c7 Mon Sep 17 00:00:00 2001 From: Theo Chatzimichos Date: Nov 16 2017 20:23:18 +0000 Subject: Merge branch 'tampakrap_get_formulas' into 'production' Add support to run the tests locally See merge request infra/salt!100 --- diff --git a/bin/get_formulas.py b/bin/get_formulas.py index a7ba387..3b795e0 100755 --- a/bin/get_formulas.py +++ b/bin/get_formulas.py @@ -71,6 +71,13 @@ def create_symlinks(DEST): os.symlink('%s/%s-formula/%s' % (DEST, formula, formula), FULL_PATH) +def remove_symlinks(): + for formula in FORMULAS.keys(): + FULL_PATH = '/srv/salt/%s' % formula + if os.path.islink(FULL_PATH): + os.unlink(FULL_PATH) + + def fetch_remote(remote, formula): from pygit2.errors import GitError import pygit2 @@ -148,6 +155,13 @@ def push(REMOTES, DEST): fetch_remote(repo.remotes[remote], formula) +def checkout_remote_and_branch(REMOTE_BRANCH, DEST): + for formula in FORMULAS.keys(): + FULL_PATH = '%s/%s-formula' % (DEST, formula) + branch = REMOTE_BRANCH.split('/')[1] + git(['checkout', '-qB', branch, REMOTE_BRANCH], cwd=FULL_PATH) + + with open('FORMULAS.yaml', 'r') as f: FORMULAS = yaml.load(f) @@ -158,6 +172,7 @@ parser.add_argument('-c', '--clone', action='store_true', help='Clone the formul parser.add_argument('--clone-from', nargs=1, help='Specify the git provider to clone from together with the namespace.') parser.add_argument('--clone-branch', nargs=1, help='Specify the branch to clone.') parser.add_argument('-s', '--symlink', action='store_true', help='Creates symlink from the specified destination to /srv/salt.') +parser.add_argument('--remove-symlinks', action='store_true', help='Removes all symlinks that were created in /srv/salt.') parser.add_argument('-r', '--add-remote', action='append', nargs='+', help='''Add the specified remotes on the local repositories. It can be passed multiple times. Usage: REMOTE_NAME USE_PREFIXES [GIT_PROVIDER_URL] [NAMESPACE]. - REMOTE is string @@ -171,6 +186,7 @@ Examples: -r mycompany_forks no_prefixes git@gitlab.mycompany.com: saltstack-formulas''') parser.add_argument('-u', '--update', nargs='*', help='Switch to origin/master and git pull. Optionally it can accept a list of remotes as arguments, that will be fetched.') parser.add_argument('-p', '--push', nargs='+', help='Pushes (with --force) to the given list of remotes from origin/master to their master and production branch, and then fetches them.') +parser.add_argument('--checkout', nargs=1, help='Checkout to the specified remote/branch.') parser.add_argument('--use-pygit2', action='store_true', help='Use pygit2 instead of invoking git whenever possible.') args = parser.parse_args() @@ -180,8 +196,12 @@ if args.pull_requests: will_run = True check_open_pull_requests() +if args.remove_symlinks: + will_run = True + remove_symlinks() + # Every option below requires the --destination argument to be set -if args.clone or args.symlink or args.clone_from or args.clone_branch or args.add_remote or args.update or args.push: +if args.clone or args.symlink or args.clone_from or args.clone_branch or args.add_remote or args.update or args.push or args.checkout: will_run = True if (args.clone_from or args.clone_branch) and not args.clone: @@ -219,6 +239,9 @@ if args.clone or args.symlink or args.clone_from or args.clone_branch or args.ad if args.push: push(args.push, args.destination[0]) + if args.checkout: + checkout_remote_and_branch(args.checkout[0], args.destination[0]) + if not will_run: parser.print_help() sys.exit(1) diff --git a/bin/prepare_test_show_highstate_env.sh b/bin/prepare_test_show_highstate_env.sh index b3ba14b..fe68558 100755 --- a/bin/prepare_test_show_highstate_env.sh +++ b/bin/prepare_test_show_highstate_env.sh @@ -38,7 +38,7 @@ $SUDO ln -s $PWD/salt /srv/salt $SUDO ln -s $PWD/pillar /srv/pillar ID=$(hostname -f) ROLES=$(bin/get_roles.py --yaml) -printf "city:\ncountry:\ndomain: infra.opensuse.org\nosfullname:\nosmajorrelease:\nosrelease_info:\n$ROLES\nsalt_cluster: opensuse\nvirt_cluster:" | $SUDO tee /etc/salt/grains > /dev/null +printf "city:\ncountry:\ndomain: infra.opensuse.org\nosfullname:\nosmajorrelease:\nosrelease_info:\n$ROLES\nsalt_cluster: opensuse\nvirt_cluster:\n" | $SUDO tee /etc/salt/grains > /dev/null sed -i -e 's/^production:$/base:/' /srv/{salt,pillar}/top.sls touch pillar/id/${ID//./_}.sls [[ -n $STRIP_SECRETS ]] && sed -i -e "s#\- secrets\..*#- id.${ID//./_}#g" $(grep -lr "\- secrets\." pillar) diff --git a/bin/run_tests_locally.sh b/bin/run_tests_locally.sh new file mode 100755 index 0000000..ba986f1 --- /dev/null +++ b/bin/run_tests_locally.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +help() { + echo "Runs all the tests on the user's workstation." + echo "Needs to be run as normal user." + echo + echo "Arguments:" + echo "-d Absolute path of the destination directory where the formulas are / should be cloned" + echo +} + +[[ $(whoami) == 'root' ]] && help && exit 1 + +[[ $1 == '--help' ]] && help && exit + +while getopts d:h arg; do + case ${arg} in + d) DESTINATION=${OPTARG} ;; + h) help && exit ;; + *) help && exit 1 ;; + esac +done + +[[ -z $DESTINATION ]] && help && exit 1 + +SALT_DIRS=( + /etc/salt + /var/log/salt + /var/cache/salt +) + +bin/test_roles.py +bin/test_custom_grains.py +bin/prepare_test_show_highstate_env.sh +bin/get_formulas.py --destination $DESTINATION --clone --symlink --use-pygit2 --update opensuse \ + --add-remote opensuse no_prefix gitlab@gitlab.infra.opensuse.org: saltstack-formulas +for dir in ${SALT_DIRS[@]}; do + sudo chown -R ${USER}: $dir +done +echo "virtual: kvm" >> /etc/salt/grains +ln -s ~/.gnupg /etc/salt/gpgkeys +echo "Running against upstream formulas" +LC_ALL=C bin/test_show_highstate.sh +bin/get_formulas.py --destination $DESTINATION --checkout opensuse/production +echo "Running against forked formulas" +LC_ALL=C bin/test_show_highstate.sh + +# Cleanup +bin/get_formulas.py --destination $DESTINATION --remove-symlinks --checkout origin/master +sed -i -e 's/^base:$/production:/' /srv/{salt,pillar}/top.sls +for dir in ${SALT_DIRS[@]}; do + sudo chown -R root: $dir +done +sudo rm /srv/{salt,pillar} /etc/salt/{grains,gpgkeys} +sudo mkdir /srv/{salt,pillar} +ID=$(hostname -f) +rm pillar/id/${ID//./_}.sls diff --git a/bin/test_show_highstate.sh b/bin/test_show_highstate.sh index b787c74..f43a36e 100755 --- a/bin/test_show_highstate.sh +++ b/bin/test_show_highstate.sh @@ -31,7 +31,7 @@ write_grains() { for os in ${ALL_OS[@]}; do for location in ${ALL_LOCATIONS[@]}; do write_grains ${location//,/ } ${os//,/ } - if $($SUDO $RUN_TEST > /dev/null); then + if $($RUN_TEST > /dev/null); then echo 'PASSED' else STATUS=1