-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathmock.py
More file actions
57 lines (44 loc) · 1.91 KB
/
mock.py
File metadata and controls
57 lines (44 loc) · 1.91 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
import json
from . import AbstractSchedulerClient
from .states import JobState, TransitionError
# HACK: MockSchedulerClient is not persistent across requests
jobs = {}
class MockSchedulerClient(AbstractSchedulerClient):
def create(self, name, image, command, **kwargs):
"""Create a new container."""
jobs.setdefault(name, {})['state'] = JobState.created
def destroy(self, name):
"""Destroy a container."""
jobs.setdefault(name, {})['state'] = JobState.destroyed
def run(self, name, image, entrypoint, command):
"""Run a one-off command."""
# dump input into a json object for testing purposes
return 0, json.dumps({
'name': name,
'image': image,
'entrypoint': entrypoint,
'command': command,
})
def start(self, name):
"""Start a container."""
if self.state(name) not in [JobState.created,
JobState.up,
JobState.down,
JobState.crashed,
JobState.error]:
raise TransitionError(self.state(name),
JobState.up,
'the container must be stopped or up to start')
jobs.setdefault(name, {})['state'] = JobState.up
def state(self, name):
"""Display the given job's running state."""
return jobs.get(name, {}).get('state', JobState.initialized)
def stop(self, name):
"""Stop a container."""
job = jobs.get(name, {})
if job.get('state') not in [JobState.up, JobState.crashed, JobState.error]:
raise TransitionError(job.get('state'),
JobState.up,
'the container must be up to stop')
job['state'] = JobState.down
SchedulerClient = MockSchedulerClient