67 lines
3.9 KiB
Python
67 lines
3.9 KiB
Python
import copy
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import subprocess
|
|
import unittest
|
|
|
|
spec = importlib.util.spec_from_file_location('pod_evidence', Path(__file__).resolve().parents[2] / 'scripts/doctor-pod-evidence.py')
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
|
|
|
|
class StalePodCandidateTests(unittest.TestCase):
|
|
def setUp(self):
|
|
uid = '00000000-0000-0000-0000-000000000001'
|
|
self.pod = {'metadata': {'name': 'app-abc', 'namespace': 'demo', 'uid': uid,
|
|
'ownerReferences': [{'controller': True, 'kind': 'ReplicaSet', 'name': 'app-rs'}]},
|
|
'spec': {'volumes': [{'persistentVolumeClaim': {'claimName': 'data'}}]},
|
|
'status': {'containerStatuses': [{'ready': False, 'restartCount': 2, 'state': {'waiting': {'reason': 'CrashLoopBackOff'}}}]}}
|
|
self.pvc = {'metadata': {'name': 'data', 'namespace': 'demo', 'uid': 'claim-id'},
|
|
'spec': {'volumeName': 'pv-data'}, 'status': {'phase': 'Bound'}}
|
|
self.pv = {'metadata': {'name': 'pv-data'}, 'spec': {'local': {'path': '/data/openebs/pv-data'},
|
|
'claimRef': {'name': 'data', 'namespace': 'demo', 'uid': 'claim-id'}}, 'status': {'phase': 'Bound'}}
|
|
self.mounts = f'/data /dev/sdb1 8:17 rw,relatime\n/var/lib/kubelet/pods/{uid}/volumes/kubernetes.io~local-volume/pv-data /dev/sda1[/openebs/pv-data] 8:1 ro,relatime'
|
|
|
|
def candidate(self):
|
|
return module.recovery_candidate([self.pod], 'app-abc', 'demo', self.pvc, self.pv, self.mounts)
|
|
|
|
def test_grounded_normal_recreation_suggestion_only(self):
|
|
candidate = self.candidate()
|
|
self.assertIn('delete pod app-abc --wait=false', candidate['candidate_command'])
|
|
self.assertNotIn('--force', candidate['candidate_command'])
|
|
self.assertNotIn('--grace-period', candidate['candidate_command'])
|
|
self.assertIn('Recheck', candidate['before_repair'])
|
|
|
|
def test_verification_commands_use_observed_names_and_check_directory_without_reading_keys(self):
|
|
candidate = module.recovery_candidate([self.pod], 'app-abc', 'demo', self.pvc, self.pv,
|
|
self.mounts, 'user@node')
|
|
check = candidate['verification_command']
|
|
self.assertIn('get pod app-abc', check)
|
|
self.assertIn('get replicaset app-rs', check)
|
|
self.assertIn('user@node', check)
|
|
self.assertIn('sudo -n test -r /data/openebs/pv-data', check)
|
|
self.assertNotIn('cat ', check)
|
|
self.assertNotIn('delete', check)
|
|
for command in [check, candidate['candidate_command']]:
|
|
with self.subTest(command=command):
|
|
subprocess.run(['bash', '-n', '-c', command], check=True, capture_output=True)
|
|
|
|
def test_identity_state_and_mount_mismatches_produce_no_candidate(self):
|
|
original = copy.deepcopy((self.pod, self.pvc, self.pv, self.mounts))
|
|
for fault in ('owner', 'healthy', 'claim-uid', 'unbound', 'same-device', 'data-ro', 'intentional-ro', 'wrong-path'):
|
|
self.pod, self.pvc, self.pv, self.mounts = copy.deepcopy(original)
|
|
if fault == 'owner': self.pod['metadata']['ownerReferences'] = []
|
|
elif fault == 'healthy': self.pod['status']['containerStatuses'][0]['ready'] = True
|
|
elif fault == 'claim-uid': self.pv['spec']['claimRef']['uid'] = 'other-claim'
|
|
elif fault == 'unbound': self.pvc['status']['phase'] = 'Pending'
|
|
elif fault == 'same-device': self.mounts = self.mounts.replace('8:1 ', '8:17 ')
|
|
elif fault == 'data-ro': self.mounts = self.mounts.replace('rw,', 'ro,')
|
|
elif fault == 'intentional-ro': self.pod['spec']['volumes'][0]['persistentVolumeClaim']['readOnly'] = True
|
|
elif fault == 'wrong-path': self.pv['spec']['local']['path'] = '/data/other'
|
|
with self.subTest(fault=fault):
|
|
self.assertIsNone(self.candidate())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|