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