-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathchaos.py
More file actions
50 lines (39 loc) · 1.56 KB
/
chaos.py
File metadata and controls
50 lines (39 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import random
from .mock import MockSchedulerClient, jobs
from .states import JobState
CREATE_ERROR_RATE = 0
DESTROY_ERROR_RATE = 0
START_ERROR_RATE = 0
STOP_ERROR_RATE = 0
class ChaosSchedulerClient(MockSchedulerClient):
def create(self, name, image, command, **kwargs):
"""Create a new container."""
if random.random() < CREATE_ERROR_RATE:
jobs.setdefault(name, {})['state'] = JobState.error
else:
super(ChaosSchedulerClient, self).create(name, image, command, **kwargs)
def destroy(self, name):
"""Destroy a container."""
if random.random() < DESTROY_ERROR_RATE:
jobs.setdefault(name, {})['state'] = JobState.error
else:
super(ChaosSchedulerClient, self).destroy(name)
def run(self, name, image, entrypoint, command):
"""Run a one-off command."""
if random.random() < CREATE_ERROR_RATE:
raise RuntimeError('exit code 1')
else:
super(ChaosSchedulerClient, self).run(name, image, entrypoint, command)
def start(self, name):
"""Start a container."""
if random.random() < START_ERROR_RATE:
jobs.setdefault(name, {})['state'] = JobState.crashed
else:
super(ChaosSchedulerClient, self).start(name)
def stop(self, name):
"""Stop a container."""
if random.random() < STOP_ERROR_RATE:
jobs.setdefault(name, {})['state'] = JobState.crashed
else:
super(ChaosSchedulerClient, self).stop(name)
SchedulerClient = ChaosSchedulerClient