github.com/jiasir/deis@v1.12.2/controller/scheduler/mock.py (about)

     1  import json
     2  
     3  from . import AbstractSchedulerClient
     4  from .states import JobState, TransitionError
     5  
     6  
     7  # HACK: MockSchedulerClient is not persistent across requests
     8  jobs = {}
     9  
    10  
    11  class MockSchedulerClient(AbstractSchedulerClient):
    12  
    13      def create(self, name, image, command, **kwargs):
    14          """Create a new container."""
    15          jobs.setdefault(name, {})['state'] = JobState.created
    16  
    17      def destroy(self, name):
    18          """Destroy a container."""
    19          jobs.setdefault(name, {})['state'] = JobState.destroyed
    20  
    21      def run(self, name, image, entrypoint, command):
    22          """Run a one-off command."""
    23          # dump input into a json object for testing purposes
    24          return 0, json.dumps({
    25              'name': name,
    26              'image': image,
    27              'entrypoint': entrypoint,
    28              'command': command,
    29          })
    30  
    31      def start(self, name):
    32          """Start a container."""
    33          if self.state(name) not in [JobState.created,
    34                                      JobState.up,
    35                                      JobState.down,
    36                                      JobState.crashed,
    37                                      JobState.error]:
    38              raise TransitionError(self.state(name),
    39                                    JobState.up,
    40                                    'the container must be stopped or up to start')
    41          jobs.setdefault(name, {})['state'] = JobState.up
    42  
    43      def state(self, name):
    44          """Display the given job's running state."""
    45          return jobs.get(name, {}).get('state', JobState.initialized)
    46  
    47      def stop(self, name):
    48          """Stop a container."""
    49          job = jobs.get(name, {})
    50          if job.get('state') not in [JobState.up, JobState.crashed, JobState.error]:
    51              raise TransitionError(job.get('state'),
    52                                    JobState.up,
    53                                    'the container must be up to stop')
    54          job['state'] = JobState.down
    55  
    56  
    57  SchedulerClient = MockSchedulerClient