Support DISTRO in alternates (#72)

This commit is contained in:
Tim Byrne 2019-10-06 11:04:21 -05:00
parent 4f6b0f09cd
commit 6a3199ceea
No known key found for this signature in database
GPG Key ID: 14DB4FC2465A4B12
4 changed files with 60 additions and 13 deletions

View File

@ -49,13 +49,14 @@ def test_alt_source(
@pytest.mark.parametrize('suffix', [ @pytest.mark.parametrize('suffix', [
'##default', '##default',
'##o.$tst_sys', '##os.$tst_sys', '##o.$tst_sys', '##os.$tst_sys',
'##d.$tst_distro', '##distro.$tst_distro',
'##c.$tst_class', '##class.$tst_class', '##c.$tst_class', '##class.$tst_class',
'##h.$tst_host', '##hostname.$tst_host', '##h.$tst_host', '##hostname.$tst_host',
'##u.$tst_user', '##user.$tst_user', '##u.$tst_user', '##user.$tst_user',
]) ])
def test_alt_conditions( def test_alt_conditions(
runner, yadm_y, paths, runner, yadm_y, paths,
tst_sys, tst_host, tst_user, suffix): tst_sys, tst_distro, tst_host, tst_user, suffix):
"""Test conditions supported by yadm alt""" """Test conditions supported by yadm alt"""
# set the class # set the class
@ -64,6 +65,7 @@ def test_alt_conditions(
suffix = string.Template(suffix).substitute( suffix = string.Template(suffix).substitute(
tst_sys=tst_sys, tst_sys=tst_sys,
tst_distro=tst_distro,
tst_class=tst_class, tst_class=tst_class,
tst_host=tst_host, tst_host=tst_host,
tst_user=tst_user, tst_user=tst_user,

View File

@ -10,17 +10,21 @@ CONDITION = {
'labels': ['o', 'os'], 'labels': ['o', 'os'],
'modifier': 1, 'modifier': 1,
}, },
'distro': {
'labels': ['d', 'distro'],
'modifier': 2,
},
'class': { 'class': {
'labels': ['c', 'class'], 'labels': ['c', 'class'],
'modifier': 2, 'modifier': 4,
}, },
'hostname': { 'hostname': {
'labels': ['h', 'hostname'], 'labels': ['h', 'hostname'],
'modifier': 4, 'modifier': 8,
}, },
'user': { 'user': {
'labels': ['u', 'user'], 'labels': ['u', 'user'],
'modifier': 8, 'modifier': 16,
}, },
} }
TEMPLATE_LABELS = ['t', 'template', 'yadm'] TEMPLATE_LABELS = ['t', 'template', 'yadm']
@ -44,24 +48,35 @@ def calculate_score(filename):
if value == 'testsystem': if value == 'testsystem':
score += 1000 + CONDITION['system']['modifier'] score += 1000 + CONDITION['system']['modifier']
else: else:
return 0 score = 0
break
elif label in CONDITION['distro']['labels']:
if value == 'testdistro':
score += 1000 + CONDITION['distro']['modifier']
else:
score = 0
break
elif label in CONDITION['class']['labels']: elif label in CONDITION['class']['labels']:
if value == 'testclass': if value == 'testclass':
score += 1000 + CONDITION['class']['modifier'] score += 1000 + CONDITION['class']['modifier']
else: else:
return 0 score = 0
break
elif label in CONDITION['hostname']['labels']: elif label in CONDITION['hostname']['labels']:
if value == 'testhost': if value == 'testhost':
score += 1000 + CONDITION['hostname']['modifier'] score += 1000 + CONDITION['hostname']['modifier']
else: else:
return 0 score = 0
break
elif label in CONDITION['user']['labels']: elif label in CONDITION['user']['labels']:
if value == 'testuser': if value == 'testuser':
score += 1000 + CONDITION['user']['modifier'] score += 1000 + CONDITION['user']['modifier']
else: else:
return 0 score = 0
break
elif label in TEMPLATE_LABELS: elif label in TEMPLATE_LABELS:
return 0 score = 0
break
return score return score
@ -69,6 +84,8 @@ def calculate_score(filename):
'default', ['default', None], ids=['default', 'no-default']) 'default', ['default', None], ids=['default', 'no-default'])
@pytest.mark.parametrize( @pytest.mark.parametrize(
'system', ['system', None], ids=['system', 'no-system']) 'system', ['system', None], ids=['system', 'no-system'])
@pytest.mark.parametrize(
'distro', ['distro', None], ids=['distro', 'no-distro'])
@pytest.mark.parametrize( @pytest.mark.parametrize(
'cla', ['class', None], ids=['class', 'no-class']) 'cla', ['class', None], ids=['class', 'no-class'])
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -76,11 +93,12 @@ def calculate_score(filename):
@pytest.mark.parametrize( @pytest.mark.parametrize(
'user', ['user', None], ids=['user', 'no-user']) 'user', ['user', None], ids=['user', 'no-user'])
def test_score_values( def test_score_values(
runner, yadm, default, system, cla, host, user): runner, yadm, default, system, distro, cla, host, user):
"""Test score results""" """Test score results"""
# pylint: disable=too-many-branches # pylint: disable=too-many-branches
local_class = 'testclass' local_class = 'testclass'
local_system = 'testsystem' local_system = 'testsystem'
local_distro = 'testdistro'
local_host = 'testhost' local_host = 'testhost'
local_user = 'testuser' local_user = 'testuser'
filenames = {'filename##': 0} filenames = {'filename##': 0}
@ -105,6 +123,18 @@ def test_score_values(
local_system if match else 'badsys' local_system if match else 'badsys'
]) ])
filenames[newfile] = calculate_score(newfile) filenames[newfile] = calculate_score(newfile)
if distro:
for filename in list(filenames):
for match in [True, False]:
for label in CONDITION[distro]['labels']:
newfile = filename
if not newfile.endswith('##'):
newfile += ','
newfile += '.'.join([
label,
local_distro if match else 'baddistro'
])
filenames[newfile] = calculate_score(newfile)
if cla: if cla:
for filename in list(filenames): for filename in list(filenames):
for match in [True, False]: for match in [True, False]:
@ -147,6 +177,7 @@ def test_score_values(
score=0 score=0
local_class={local_class} local_class={local_class}
local_system={local_system} local_system={local_system}
local_distro={local_distro}
local_host={local_host} local_host={local_host}
local_user={local_user} local_user={local_user}
""" """
@ -169,6 +200,7 @@ def test_score_values_templates(runner, yadm):
"""Test score results""" """Test score results"""
local_class = 'testclass' local_class = 'testclass'
local_system = 'testsystem' local_system = 'testsystem'
local_distro = 'testdistro'
local_host = 'testhost' local_host = 'testhost'
local_user = 'testuser' local_user = 'testuser'
filenames = {'filename##': 0} filenames = {'filename##': 0}
@ -186,6 +218,7 @@ def test_score_values_templates(runner, yadm):
score=0 score=0
local_class={local_class} local_class={local_class}
local_system={local_system} local_system={local_system}
local_distro={local_distro}
local_host={local_host} local_host={local_host}
local_user={local_user} local_user={local_user}
""" """

13
yadm
View File

@ -154,23 +154,30 @@ function score_file() {
score=0 score=0
return return
fi fi
elif [[ "$label" =~ ^(d|distro)$ ]]; then
if [ "$value" = "$local_distro" ]; then
score=$((score + 2))
else
score=0
return
fi
elif [[ "$label" =~ ^(c|class)$ ]]; then elif [[ "$label" =~ ^(c|class)$ ]]; then
if [ "$value" = "$local_class" ]; then if [ "$value" = "$local_class" ]; then
score=$((score + 2)) score=$((score + 4))
else else
score=0 score=0
return return
fi fi
elif [[ "$label" =~ ^(h|hostname)$ ]]; then elif [[ "$label" =~ ^(h|hostname)$ ]]; then
if [ "$value" = "$local_host" ]; then if [ "$value" = "$local_host" ]; then
score=$((score + 4)) score=$((score + 8))
else else
score=0 score=0
return return
fi fi
elif [[ "$label" =~ ^(u|user)$ ]]; then elif [[ "$label" =~ ^(u|user)$ ]]; then
if [ "$value" = "$local_user" ]; then if [ "$value" = "$local_user" ]; then
score=$((score + 8)) score=$((score + 16))
else else
score=0 score=0
return return

5
yadm.1
View File

@ -424,6 +424,11 @@ Valid if the value matches the current user.
Current user is calculated by running Current user is calculated by running
.BR "id -u -n" . .BR "id -u -n" .
.TP .TP
.BR distro , " d
Valid if the value matches the distro.
Distro is calculated by running
.BR "lsb_release -si" .
.TP
.BR os , " o .BR os , " o
Valid if the value matches the OS. Valid if the value matches the OS.
OS is calculated by running OS is calculated by running