summaryrefslogtreecommitdiffstats
path: root/tests/test_sadf.py
blob: 73c0d9b141daade9c4d2dd3258816d085a19c1d3 (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
#
# Copyright 2014 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
#
# Refer to the README and COPYING files for full details of the license
#

import mock
from datetime import datetime
import xml.etree.cElementTree as etree
from testrunner import PluginsTestCase as TestCaseBase

import plugins


class sadfTests(TestCaseBase):
    @mock.patch('plugins.sadf.utcnow')
    def test_getLatestStat_success(self, utcnow_mock):
        expectedDict = {'cpu-load': {'cpu': {'idle': '96.62',
                                             'iowait': '0.17',
                                             'nice': '0.00',
                                             'number': 'all',
                                             'steal': '0.00',
                                             'system': '1.20',
                                             'user': '2.01'}},
                        'date': '2014-03-19',
                        'interval': '60',
                        'time': '10:19:01',
                        'utc': '1'}
        utcnow_mock.return_value = datetime(2014, 3, 19, 10, 19, 22,
                                            164227)
        with open("getLatestStat_success.xml") as f:
            out = f.read()
            tree = etree.fromstring(out)
        outDict = plugins.sadf.getLatestStat(tree)
        self.assertEquals(expectedDict, outDict)

    def test_getLatestStat_failure(self):
        expectedValue = None
        with open("getLatestStat_success.xml") as f:
            out = f.read()
            tree = etree.fromstring(out)
        outValue = plugins.sadf.getLatestStat(tree)
        self.assertEquals(expectedValue, outValue)

    def test_getLatestStat_exception(self):
        def _getLatestStat():
            with open("getLatestStat_exception.xml") as f:
                out = f.read()
            tree = etree.fromstring(out)
            plugins.sadf.getLatestStat(tree)

        self.assertRaises(plugins.sadf.SadfXmlErrorException, _getLatestStat)