1
0
mirror of https://github.com/kakwa/ldapcherry synced 2024-11-25 18:54:29 +01:00

adding an exception for failures to open role file

This commit is contained in:
kakwa 2015-05-12 11:34:28 +02:00
parent 17b8aa6406
commit 804e2b7202
3 changed files with 25 additions and 3 deletions

View File

@ -9,6 +9,7 @@ class MissingParameter(Exception):
def __init__(self, section, key): def __init__(self, section, key):
self.section = section self.section = section
self.key = key self.key = key
self.log = "missing parameter <%(key)s> in section <%(section)s>" % { 'key' : key, 'section' : section }
class MissingKey(Exception): class MissingKey(Exception):
def __init__(self, key): def __init__(self, key):
@ -17,8 +18,17 @@ class MissingKey(Exception):
class DumplicateRoleKey(Exception): class DumplicateRoleKey(Exception):
def __init__(self, role): def __init__(self, role):
self.role = role self.role = role
self.log = "duplicate role key <%(role)s> in role file" % { 'role' : role}
class DumplicateRoleContent(Exception): class DumplicateRoleContent(Exception):
def __init__(self, role1, role2): def __init__(self, role1, role2):
self.role1 = role1 self.role1 = role1
self.role2 = role2 self.role2 = role2
self.log = "role <%(role1)s> and <%(role2)s> are identical" % { 'role1' : role1, 'role2': role2}
class MissingRolesFile(Exception):
def __init__(self, rolefile):
self.rolefile = rolefile
self.log = "fail to open role file <%(rolefile)s>" % { 'rolefile' : rolefile}

View File

@ -10,13 +10,16 @@ import sys
from ldapcherry.pyyamlwrapper import loadNoDump from ldapcherry.pyyamlwrapper import loadNoDump
from ldapcherry.pyyamlwrapper import DumplicatedKey from ldapcherry.pyyamlwrapper import DumplicatedKey
from ldapcherry.exceptions import DumplicateRoleKey, MissingKey, DumplicateRoleContent from ldapcherry.exceptions import DumplicateRoleKey, MissingKey, DumplicateRoleContent, MissingRolesFile
class Roles: class Roles:
def __init__(self, role_file): def __init__(self, role_file):
try:
stream = open(role_file, 'r') stream = open(role_file, 'r')
except:
raise MissingRolesFile(role_file)
try: try:
self.roles_raw = loadNoDump(stream) self.roles_raw = loadNoDump(stream)
except DumplicatedKey as e: except DumplicatedKey as e:

View File

@ -7,7 +7,7 @@ from __future__ import unicode_literals
import pytest import pytest
import sys import sys
from ldapcherry.roles import Roles from ldapcherry.roles import Roles
from ldapcherry.exceptions import DumplicateRoleKey, MissingKey, DumplicateRoleContent from ldapcherry.exceptions import DumplicateRoleKey, MissingKey, DumplicateRoleContent, MissingRolesFile
from ldapcherry.pyyamlwrapper import DumplicatedKey, RelationError from ldapcherry.pyyamlwrapper import DumplicatedKey, RelationError
@ -34,6 +34,15 @@ class TestError(object):
else: else:
raise AssertionError("expected an exception") raise AssertionError("expected an exception")
def testNoFile(self):
try:
inv = Roles('./tests/cfg/dontexist')
except MissingRolesFile:
return
else:
raise AssertionError("expected an exception")
def testRoleContentDuplication(self): def testRoleContentDuplication(self):
try: try:
inv = Roles('./tests/cfg/roles_content_dump.yml') inv = Roles('./tests/cfg/roles_content_dump.yml')