summaryrefslogtreecommitdiff
path: root/oslo_vmware/tests/test_exceptions.py
blob: d70756bd91c3245d9e0e7215e536c37ef6009d19 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Copyright (c) 2015 VMware, Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
Unit tests for exceptions module.
"""
import mock

from oslo_vmware._i18n import _
from oslo_vmware import exceptions
from oslo_vmware.tests import base


class ExceptionsTest(base.TestCase):

    def test_exception_summary_exception_as_list(self):
        # assert that if a list is fed to the VimException object
        # that it will error.
        self.assertRaises(ValueError,
                          exceptions.VimException,
                          [], ValueError('foo'))

    def test_exception_summary_string(self):
        e = exceptions.VimException(_("string"), ValueError("foo"))
        string = str(e)
        self.assertEqual("string\nCause: foo", string)

    def test_vim_fault_exception_string(self):
        self.assertRaises(ValueError,
                          exceptions.VimFaultException,
                          "bad", ValueError("argument"))

    def test_vim_fault_exception(self):
        vfe = exceptions.VimFaultException([ValueError("example")], _("cause"))
        string = str(vfe)
        self.assertIn(string, ["cause\nFaults: [ValueError('example',)]",
                               "cause\nFaults: [ValueError('example')]"])

    def test_vim_fault_exception_with_cause_and_details(self):
        vfe = exceptions.VimFaultException([ValueError("example")],
                                           "MyMessage",
                                           "FooBar",
                                           {'foo': 'bar'})
        string = str(vfe)
        self.assertIn(string, ["MyMessage\n"
                               "Cause: FooBar\n"
                               "Faults: [ValueError('example',)]\n"
                               "Details: {'foo': 'bar'}",
                               "MyMessage\n"
                               "Cause: FooBar\n"
                               "Faults: [ValueError('example')]\n"
                               "Details: {'foo': 'bar'}"])

    def _create_subclass_exception(self):
        class VimSubClass(exceptions.VimException):
            pass
        return VimSubClass

    def test_register_fault_class(self):
        exc = self._create_subclass_exception()
        exceptions.register_fault_class('ValueError', exc)
        self.assertEqual(exc, exceptions.get_fault_class('ValueError'))

    def test_register_fault_class_override(self):
        exc = self._create_subclass_exception()
        exceptions.register_fault_class(exceptions.ALREADY_EXISTS, exc)
        self.assertEqual(exc,
                         exceptions.get_fault_class(exceptions.ALREADY_EXISTS))

    def test_register_fault_class_invalid(self):
        self.assertRaises(TypeError,
                          exceptions.register_fault_class,
                          'ValueError', ValueError)

    def test_log_exception_to_string(self):
        self.assertEqual('Insufficient disk space.',
                         str(exceptions.NoDiskSpaceException()))

    def test_get_fault_class(self):
        self.assertEqual(exceptions.AlreadyExistsException,
                         exceptions.get_fault_class("AlreadyExists"))
        self.assertEqual(exceptions.CannotDeleteFileException,
                         exceptions.get_fault_class("CannotDeleteFile"))
        self.assertEqual(exceptions.FileAlreadyExistsException,
                         exceptions.get_fault_class("FileAlreadyExists"))
        self.assertEqual(exceptions.FileFaultException,
                         exceptions.get_fault_class("FileFault"))
        self.assertEqual(exceptions.FileLockedException,
                         exceptions.get_fault_class("FileLocked"))
        self.assertEqual(exceptions.FileNotFoundException,
                         exceptions.get_fault_class("FileNotFound"))
        self.assertEqual(exceptions.InvalidPowerStateException,
                         exceptions.get_fault_class("InvalidPowerState"))
        self.assertEqual(exceptions.InvalidPropertyException,
                         exceptions.get_fault_class("InvalidProperty"))
        self.assertEqual(exceptions.NoPermissionException,
                         exceptions.get_fault_class("NoPermission"))
        self.assertEqual(exceptions.NotAuthenticatedException,
                         exceptions.get_fault_class("NotAuthenticated"))
        self.assertEqual(exceptions.TaskInProgress,
                         exceptions.get_fault_class("TaskInProgress"))
        self.assertEqual(exceptions.DuplicateName,
                         exceptions.get_fault_class("DuplicateName"))
        self.assertEqual(exceptions.NoDiskSpaceException,
                         exceptions.get_fault_class("NoDiskSpace"))
        self.assertEqual(exceptions.ToolsUnavailableException,
                         exceptions.get_fault_class("ToolsUnavailable"))
        self.assertEqual(exceptions.ManagedObjectNotFoundException,
                         exceptions.get_fault_class("ManagedObjectNotFound"))
        # Test unknown fault.
        self.assertIsNone(exceptions.get_fault_class("NotAFile"))

    def test_translate_fault(self):

        def fake_task(fault_class_name, error_msg=None):
            task_info = mock.Mock()
            task_info.localizedMessage = error_msg
            if fault_class_name:
                error_fault = mock.Mock()
                error_fault.__class__.__name__ = fault_class_name
                task_info.fault = error_fault
            return task_info

        error_msg = "OUCH"
        task = fake_task(exceptions.FILE_LOCKED, error_msg)
        actual = exceptions.translate_fault(task)

        expected = exceptions.FileLockedException(error_msg)
        self.assertEqual(expected.__class__, actual.__class__)
        self.assertEqual(expected.message, actual.message)

        error_msg = "Oopsie"
        task = fake_task(None, error_msg)
        actual = exceptions.translate_fault(task)
        expected = exceptions.VimFaultException(['Mock'], message=error_msg)
        self.assertEqual(expected.__class__, actual.__class__)
        self.assertEqual(expected.message, actual.message)
        self.assertEqual(expected.fault_list, actual.fault_list)