github.com/rvaralda/deis@v1.4.1/controller/scheduler/mock.py (about)

     1  import json
     2  from cStringIO import StringIO
     3  from .states import JobState, TransitionNotAllowed
     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, JobState.up, JobState.down]:
    59              raise TransitionNotAllowed
    60          job = jobs.get(name, {})
    61          job.update({'state': JobState.up})
    62          jobs[name] = job
    63          return
    64  
    65      def state(self, name):
    66          """
    67          Display the given job's running state
    68          """
    69          state = JobState.initialized
    70          job = jobs.get(name)
    71          if job:
    72              state = job.get('state')
    73          return state
    74  
    75      def stop(self, name):
    76          """
    77          Stop a container
    78          """
    79          job = jobs.get(name, {})
    80          if job.get('state') != JobState.up:
    81              raise TransitionNotAllowed
    82          job.update({'state': JobState.stopped})
    83          jobs[name] = job
    84          return
    85  
    86  SchedulerClient = MockSchedulerClient