summaryrefslogtreecommitdiffstats
path: root/tests/fio_tests/fio_invoker_test.py
blob: 4672651f92e95726aeafa02829816aa502739ba7 (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
##############################################################################
# Copyright (c) 2017 Dell EMC and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

from StringIO import StringIO
import json
import unittest

from storperf.fio.fio_invoker import FIOInvoker


class Test(unittest.TestCase):

    simple_dictionary = {'Key': 'Value'}

    def exceptional_event(self, callback_id, metric):
        self.exception_called = True
        raise Exception

    def event(self, callback_id, metric):
        self.metric = metric

    def setUp(self):
        self.exception_called = False
        self.metric = None
        self.fio_invoker = FIOInvoker()

    def testStdoutValidJSON(self):
        self.fio_invoker.register(self.event)
        string = json.dumps(self.simple_dictionary, indent=4, sort_keys=True)

        output = StringIO(string + "\n")
        self.fio_invoker.stdout_handler(output)

        self.assertEqual(self.simple_dictionary, self.metric)

    def testStdoutValidJSONWithFIOOutput(self):
        self.fio_invoker.register(self.event)
        string = json.dumps(self.simple_dictionary, indent=4, sort_keys=True)
        terminating = "fio: terminating on signal 2\n"
        output = StringIO(terminating + string + "\n")
        self.fio_invoker.stdout_handler(output)

        self.assertEqual(self.simple_dictionary, self.metric)

    def testStdoutNoJSON(self):
        self.fio_invoker.register(self.event)
        string = "{'key': 'value'}"

        output = StringIO(string + "\n")
        self.fio_invoker.stdout_handler(output)

        self.assertEqual(None, self.metric)

    def testStdoutInvalidJSON(self):
        self.fio_invoker.register(self.event)
        string = "{'key':\n}"

        output = StringIO(string + "\n")
        self.fio_invoker.stdout_handler(output)

        self.assertEqual(None, self.metric)

    def testStdoutAfterTerminated(self):
        self.fio_invoker.register(self.event)
        string = json.dumps(self.simple_dictionary, indent=4, sort_keys=True)

        self.fio_invoker.terminated = True
        output = StringIO(string + "\n")
        self.fio_invoker.stdout_handler(output)

        self.assertEqual(None, self.metric)

    def testStdoutCallbackException(self):
        self.fio_invoker.register(self.exceptional_event)
        self.fio_invoker.register(self.event)
        string = json.dumps(self.simple_dictionary, indent=4, sort_keys=True)

        output = StringIO(string + "\n")
        self.fio_invoker.stdout_handler(output)

        self.assertEqual(self.simple_dictionary, self.metric)
        self.assertEqual(self.exception_called, True)