summaryrefslogtreecommitdiffstats
path: root/tests/format.py
blob: 7024ece95a140690e3ccec957a7016b66ed599a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: UTF-8 -*-
# Copyright 2014 Red Hat, Inc.
# Part of clufter project
# Licensed under GPLv2+ (a copy included | http://gnu.org/licenses/gpl-2.0.txt)
"""Testing format"""
__author__ = "Jan Pokorný <jpokorny @at@ Red Hat .dot. com>"

from os.path import join, dirname as d; execfile(join(d(d((__file__))), '_go'))


from os.path import dirname, join
from unittest import TestCase
#from pprint import pprint

from lxml import etree

from .format import FormatError
from .formats.ccs import ccs
from .formats.coroxml import coroxml_needle
from .utils import head_tail


class XMLFormatWalkTestCase(TestCase):
    walk_dir = join(dirname(__file__), 'XMLFormat-walk')
    result_walk_full = {
        'cluster': ('cluster-full', {
            'clusternodes': ('clusternodes-full', {
                'clusternode': ('clusternode-full', {
                })
            }),
            'cman': ('cman-full', {
            }),
            'dlm': ('dlm-full', {
            }),
            'rm': ('rm-full', {
                'failoverdomains': ('failoverdomains-full', {
                    'failoverdomain': ('failoverdomain-full', {
                    })
                }),
                'service': ('service-full', {
                })
            })
        })
    }
    result_walk_sparse = {
        'failoverdomain': ('failoverdomain-sparse', {
        }),
        'heuristic': ('heuristic-sparse', {
        })
    }

    def testWalkFull(self):
        r = ccs.walk_schema(self.walk_dir, 'full')
        #pprint(r, width=8)  # --> expected
        self.assertTrue(r == self.result_walk_full)

    def testWalkSparse(self):
        r = ccs.walk_schema(self.walk_dir, 'sparse')
        #pprint(r, width=8)  # --> expected
        self.assertTrue(r == self.result_walk_sparse)


class XMLValidationTestCase(TestCase):
    coro_input_ok = join(dirname(__file__), 'coro_ok.xml')
    coro_input_fail = join(dirname(__file__), 'coro_fail.xml')

    def testRngImplicitValidationOk(self):
        try:
            et = coroxml_needle('file', self.coro_input_ok)('etree')
        except Exception:
            raise
            self.assertTrue(False)

    def testRngImplicitValidationFail(self):
        try:
            et = coroxml_needle('file', self.coro_input_fail)('etree')
        except FormatError as e:
            self.assertTrue('Validation' in str(e))
            pass
        else:
            self.assertTrue(False)

    def testRngExplicitValidationOk(self):
        entries, _ = head_tail(coroxml_needle.etree_validator(
            etree.parse(self.coro_input_ok)
        ))
        self.assertFalse(entries)

    def testRngExplicitValidationFail(self):
        entries, _ = head_tail(coroxml_needle.etree_validator(
            etree.parse(self.coro_input_fail)
        ))
        self.assertTrue(entries)


from os.path import join, dirname as d; execfile(join(d(d(__file__)), '_gone'))