github.com/rvaralda/deis@v1.4.1/controller/scheduler/chaos.py (about) 1 import random 2 from .mock import MockSchedulerClient, jobs 3 from .states import JobState 4 5 6 CREATE_ERROR_RATE = 0 7 DESTROY_ERROR_RATE = 0 8 START_ERROR_RATE = 0 9 STOP_ERROR_RATE = 0 10 11 12 class ChaosSchedulerClient(MockSchedulerClient): 13 14 def create(self, name, image, command, **kwargs): 15 if random.random() < CREATE_ERROR_RATE: 16 job = jobs.get(name, {}) 17 job.update({'state': JobState.error}) 18 jobs[name] = job 19 return 20 return super(ChaosSchedulerClient, self).create(name, image, command, **kwargs) 21 22 def destroy(self, name): 23 """ 24 Destroy an existing job 25 """ 26 if random.random() < DESTROY_ERROR_RATE: 27 job = jobs.get(name, {}) 28 job.update({'state': JobState.error}) 29 jobs[name] = job 30 return 31 return super(ChaosSchedulerClient, self).destroy(name) 32 33 def run(self, name, image, entrypoint, command): 34 """ 35 Run a one-off command 36 """ 37 if random.random() < CREATE_ERROR_RATE: 38 raise RuntimeError('exit code 1') 39 return super(ChaosSchedulerClient, self).run(name, image, entrypoint, command) 40 41 def start(self, name): 42 """ 43 Start an idle job 44 """ 45 if random.random() < START_ERROR_RATE: 46 job = jobs.get(name, {}) 47 job.update({'state': JobState.crashed}) 48 jobs[name] = job 49 return 50 return super(ChaosSchedulerClient, self).start(name) 51 52 def stop(self, name): 53 """ 54 Stop a running job 55 """ 56 if random.random() < STOP_ERROR_RATE: 57 job = jobs.get(name, {}) 58 job.update({'state': JobState.error}) 59 jobs[name] = job 60 return 61 return super(ChaosSchedulerClient, self).stop(name) 62 63 SchedulerClient = ChaosSchedulerClient