github.com/spg/deis@v1.7.3/controller/scheduler/mock.py (about)

     1  import json
     2  from cStringIO import StringIO
     3  from .states import JobState, TransitionError
     4  
     5  
     6  # HACK: MockSchedulerClient is not persistent across requests
     7  jobs = {}
     8  
     9  
    10  class MockSchedulerClient(object):
    11  
    12      def __init__(self, target, auth, options, pkey):
    13          self.target = target
    14          self.auth = auth
    15          self.options = options
    16          self.pkey = pkey
    17  
    18      # container api
    19  
    20      def attach(self, name):
    21          """
    22          Attach to a job's stdin, stdout and stderr
    23          """
    24          return StringIO(), StringIO(), StringIO()
    25  
    26      def create(self, name, image, command, **kwargs):
    27          """
    28          Create a new container
    29          """
    30          job = jobs.get(name, {})
    31          job.update({'state': JobState.created})
    32          jobs[name] = job
    33          return
    34  
    35      def destroy(self, name):
    36          """
    37          Destroy a container
    38          """
    39          job = jobs.get(name, {})
    40          job.update({'state': JobState.destroyed})
    41          jobs[name] = job
    42          return
    43  
    44      def run(self, name, image, entrypoint, command):
    45          """
    46          Run a one-off command
    47          """
    48          # dump input into a json object for testing purposes
    49          return 0, json.dumps({'name': name,
    50                                'image': image,
    51                                'entrypoint': entrypoint,
    52                                'command': command})
    53  
    54      def start(self, name):
    55          """
    56          Start a container
    57          """
    58          if self.state(name) not in [JobState.created,
    59                                      JobState.up,
    60                                      JobState.down,
    61                                      JobState.crashed,
    62                                      JobState.error]:
    63              raise TransitionError(self.state(name),
    64                                    JobState.up,
    65                                    'the container must be stopped or up to start')
    66          job = jobs.get(name, {})
    67          job.update({'state': JobState.up})
    68          jobs[name] = job
    69          return
    70  
    71      def state(self, name):
    72          """
    73          Display the given job's running state
    74          """
    75          state = JobState.initialized
    76          job = jobs.get(name)
    77          if job:
    78              state = job.get('state')
    79          return state
    80  
    81      def stop(self, name):
    82          """
    83          Stop a container
    84          """
    85          job = jobs.get(name, {})
    86          if job.get('state') not in [JobState.up, JobState.crashed, JobState.error]:
    87              raise TransitionError(job.get('state'),
    88                                    JobState.up,
    89                                    'the container must be up to stop')
    90          job.update({'state': JobState.down})
    91          jobs[name] = job
    92          return
    93  
    94  SchedulerClient = MockSchedulerClient