Diagnostics: add validation for types
When creating diagnostics the calling application is able to pass cpu, nic and disk diagnostics. This patch validates that those are of the correct type. TrivialFix Change-Id: Id76c37e2f3ff50b0029bb1fa26348e43aee432f1
This commit is contained in:
parent
785d7c78ca
commit
9830d97564
|
@ -12,6 +12,7 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from nova import exception
|
||||
from nova import test
|
||||
from nova.virt import diagnostics
|
||||
|
||||
|
@ -208,3 +209,23 @@ class DiagnosticsTests(test.NoDBTestCase):
|
|||
'version': '1.0'}
|
||||
result = diags.serialize()
|
||||
self.assertEqual(expected, result)
|
||||
|
||||
def test_diagnostics_invalid_input(self):
|
||||
self.assertRaises(exception.InvalidInput,
|
||||
diagnostics.Diagnostics,
|
||||
cpu_details='invalid type')
|
||||
self.assertRaises(exception.InvalidInput,
|
||||
diagnostics.Diagnostics,
|
||||
cpu_details=['invalid entry'])
|
||||
self.assertRaises(exception.InvalidInput,
|
||||
diagnostics.Diagnostics,
|
||||
nic_details='invalid type')
|
||||
self.assertRaises(exception.InvalidInput,
|
||||
diagnostics.Diagnostics,
|
||||
nic_details=['invalid entry'])
|
||||
self.assertRaises(exception.InvalidInput,
|
||||
diagnostics.Diagnostics,
|
||||
disk_details='invalid type')
|
||||
self.assertRaises(exception.InvalidInput,
|
||||
diagnostics.Diagnostics,
|
||||
disk_details=['invalid entry'])
|
||||
|
|
|
@ -15,6 +15,9 @@
|
|||
|
||||
import six
|
||||
|
||||
from nova import exception
|
||||
from nova.i18n import _
|
||||
|
||||
|
||||
class CpuDiagnostics(object):
|
||||
|
||||
|
@ -119,19 +122,31 @@ class Diagnostics(object):
|
|||
self.uptime = uptime
|
||||
self.config_drive = config_drive
|
||||
if cpu_details:
|
||||
self._validate_type(cpu_details, CpuDiagnostics, 'cpu_details')
|
||||
self.cpu_details = cpu_details
|
||||
else:
|
||||
self.cpu_details = []
|
||||
if nic_details:
|
||||
self._validate_type(nic_details, NicDiagnostics, 'nic_details')
|
||||
self.nic_details = nic_details
|
||||
else:
|
||||
self.nic_details = []
|
||||
if disk_details:
|
||||
self._validate_type(disk_details, DiskDiagnostics, 'disk_details')
|
||||
self.disk_details = disk_details
|
||||
else:
|
||||
self.disk_details = []
|
||||
self.memory_details = MemoryDiagnostics()
|
||||
|
||||
def _validate_type(self, input, type, str_input):
|
||||
if not isinstance(input, list):
|
||||
reason = _("Invalid type for %s") % str_input
|
||||
raise exception.InvalidInput(reason=reason)
|
||||
for i in input:
|
||||
if not isinstance(i, type):
|
||||
reason = _("Invalid type for %s entry") % str_input
|
||||
raise exception.InvalidInput(reason=reason)
|
||||
|
||||
def add_cpu(self, time=0):
|
||||
self.cpu_details.append(CpuDiagnostics(time=time))
|
||||
|
||||
|
|
Loading…
Reference in New Issue