aa384d
#!/usr/bin/python3
aa384d
Theo Chatzimichos e8dbf6
# Various roles related tests:
Theo Chatzimichos e8dbf6
# - Checks if the declared {salt,pillar}/role/*.sls files actually match an
Theo Chatzimichos e8dbf6
#   assigned role to a minion
Theo Chatzimichos e8dbf6
# - Checks if the assigned roles to minions have equivalent
Theo Chatzimichos e8dbf6
#   {salt,pillar}/role/*.sls files
Theo Chatzimichos 2883e0
# - Checks that the special roles are not included in any pillar/id/*.sls
aa384d
aa384d
import os
aa384d
import sys
ac656a
Theo Chatzimichos 4f2316
from get_roles import get_roles, get_roles_of_one_minion
aa384d
b49512
status = 0
Theo Chatzimichos eb3a47
special_roles = ['base']
84e92b
roles = get_roles() + special_roles
aa384d
aa384d
for directory in ['salt', 'pillar']:
b9fd97
    for sls in os.listdir(f'{directory}/role'):
aa384d
        if sls.endswith('.sls'):
Theo Chatzimichos f1d227
            if sls.split('.sls')[0] not in roles:
2cc4af
                print(f'Unused file {directory}/role/{sls} - not in roles')
Theo Chatzimichos f1d227
                status = 1
aa384d
2cc4af
for role in roles:
2cc4af
    role = role.replace('.', '/')
2cc4af
    if not os.path.isfile(f'salt/role/{role}.sls') and not os.path.isfile(f'salt/role/{role}/init.sls'):
2cc4af
        print(f'{role} role is missing the salt/role/{role}.sls or salt/role/{role}/init.sls file')
2cc4af
        status = 1
Theo Chatzimichos 043122
Theo Chatzimichos 2883e0
roles = get_roles()
Theo Chatzimichos 2883e0
Theo Chatzimichos 2883e0
for special_role in special_roles:
Theo Chatzimichos 2883e0
    if special_role in roles:
Theo Chatzimichos 2883e0
        for sls in os.listdir('pillar/id/'):
Theo Chatzimichos 4f2316
            _roles = get_roles_of_one_minion(sls)
Theo Chatzimichos 2883e0
            if special_role in _roles:
b9fd97
                print(f'{special_role} role should not be included in pillar/id/{sls} file')
Theo Chatzimichos 2883e0
                status = 1
Theo Chatzimichos 2883e0
aa384d
sys.exit(status)