def test_is_ug_swift(self, mock_gid, mock_uid, mock_stat): d = check_mounts.Device('/dev/sd#', '/srv/node/disk#', 'label#') mock_gid.return_value = Mock(gr_name='swift') mock_uid.return_value = Mock(pw_name='swift') response = {} self.assertTrue(check_mounts.is_ug_swift(d, response)) self.assertDictEqual(response, {}) mock_gid.return_value = Mock(gr_name='not-swift') mock_uid.return_value = Mock(pw_name='swift') response = {} self.assertFalse(check_mounts.is_ug_swift(d, response)) self.assertDictEqual(response, {'user': '******', 'group': 'not-swift'}) mock_gid.return_value = Mock(gr_name='swift') mock_uid.return_value = Mock(pw_name='not-swift') response = {} self.assertFalse(check_mounts.is_ug_swift(d, response)) self.assertDictEqual(response, {'user': '******', 'group': 'swift'}) mock_gid.return_value = Mock(gr_name='not-swift') mock_uid.return_value = Mock(pw_name='not-swift') response = {} self.assertFalse(check_mounts.is_ug_swift(d, response)) self.assertDictEqual( response, {'user': '******', 'group': 'not-swift'} )
def test_get_diskusage(self, *args): expected = {'size': 532676608, 'used': 338939904, 'avail': 193736704, 'usage': 64.0} result = check_mounts.get_diskusage(check_mounts.Device('na', 'na', 'na')) self.assertEqual(expected, result)
class TestDiskUsage(unittest.TestCase): @mock.patch('os.statvfs', return_value=fake_statvfs_result) def test_get_diskusage(self, *args): expected = {'size': 532676608, 'used': 338939904, 'avail': 193736704, 'usage': 64.0} result = check_mounts.get_diskusage(check_mounts.Device('na', 'na', 'na')) self.assertEqual(expected, result) @mock.patch('os.statvfs', return_value=fake_statvfs_result) @mock.patch('swiftlm.systems.check_mounts.get_devices', return_value=[check_mounts.Device('na', 'na', 'na')]) @mock.patch('swiftlm.utils.metricdata.timestamp', lambda: 123456) def test_diskusage(self, *args): expected = [MetricData.single('swiftlm.diskusage.host.val.size', 532676608, dimensions={'mount': 'na', 'service': 'object-storage'}), MetricData.single('swiftlm.diskusage.host.val.used', 338939904, dimensions={'mount': 'na', 'service': 'object-storage'}), MetricData.single('swiftlm.diskusage.host.val.avail', 193736704, dimensions={'mount': 'na', 'service': 'object-storage'}), MetricData.single('swiftlm.diskusage.host.val.usage', 64.0, dimensions={'mount': 'na', 'service': 'object-storage'}), MetricData.single('swiftlm.diskusage.host.max.usage', 64.0, dimensions={'service': 'object-storage'}), MetricData.single('swiftlm.diskusage.host.min.usage', 64.0, dimensions={'service': 'object-storage'}), MetricData.single('swiftlm.diskusage.host.avg.usage', 64.0, dimensions={'service': 'object-storage'})] results = check_mounts.diskusage() for result in results: if result in expected: expected.remove(result) else: self.assertEqual(True, False, msg='Not expecting %s' % result.__repr__()) self.assertEqual(0, len(expected), msg='Missing: %s' % expected)
def test_is_mounted_775(self, mock_stat): d = check_mounts.Device('/dev/sd#', '/srv/node/disk#', 'label#') mock_stat.return_value = Mock(st_mode=16877) # oct(16877) = 040775 self.assertTrue(check_mounts.is_mounted_775(d, {})) mock_stat.return_value = Mock(st_mode=16804) # oct(16804) = 040644 response = {} self.assertFalse(check_mounts.is_mounted_775(d, response)) self.assertDictEqual(response, {'permissions': '644'})
def test_get_devices(self): j = '''{ "model": "Standard PC (i440FX + PIIX, 1996)", "hostname": "PADAWANBASE-CCP-T1-M1-NETCLM", "ip_addr": "192.168.245.4", "devices": [ { "size_gb": 18.625, "mounted": "True", "name": "/dev/sdb1", "swift_drive_name": "disk0" }, { "size_gb": 18.625, "mounted": "True", "name": "/dev/sdc1", "swift_drive_name": "disk1" } ] } ''' expected = [ check_mounts.Device( '/dev/sdb1', '/srv/node/disk0'), check_mounts.Device( '/dev/sdc1', '/srv/node/disk1'), ] with tempfile.NamedTemporaryFile('w+') as f: f.write(j) f.flush() f.seek(0) mock_open = mock.MagicMock(spec=BUILTIN_OPEN) handle = mock.MagicMock() handle.__enter__.return_value = f mock_open.return_value = handle with mock.patch(BUILTIN_OPEN, mock_open): actual = check_mounts.get_devices() self.assertEqual(expected, actual)