-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathchaos.py
More file actions
63 lines (53 loc) · 1.81 KB
/
chaos.py
File metadata and controls
63 lines (53 loc) · 1.81 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
51
52
53
54
55
56
57
58
59
60
61
62
63
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):
if random.random() < CREATE_ERROR_RATE:
job = jobs.get(name, {})
job.update({'state': JobState.error})
jobs[name] = job
return
return super(ChaosSchedulerClient, self).create(name, image, command, **kwargs)
def destroy(self, name):
"""
Destroy an existing job
"""
if random.random() < DESTROY_ERROR_RATE:
job = jobs.get(name, {})
job.update({'state': JobState.error})
jobs[name] = job
return
return 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')
return super(ChaosSchedulerClient, self).run(name, image, entrypoint, command)
def start(self, name):
"""
Start an idle job
"""
if random.random() < START_ERROR_RATE:
job = jobs.get(name, {})
job.update({'state': JobState.crashed})
jobs[name] = job
return
return super(ChaosSchedulerClient, self).start(name)
def stop(self, name):
"""
Stop a running job
"""
if random.random() < STOP_ERROR_RATE:
job = jobs.get(name, {})
job.update({'state': JobState.error})
jobs[name] = job
return
return super(ChaosSchedulerClient, self).stop(name)
SchedulerClient = ChaosSchedulerClient