Beispiel #1
0
def test_combine_tasks():
  p1 = Process(name='p1')
  p2 = Process(name='p2')
  p3 = Process(name='p3')
  p4 = Process(name='p4')
  r100 = Resources(cpu=1, ram=0, disk=0)
  r010 = Resources(cpu=0, ram=1, disk=0)
  r001 = Resources(cpu=0, ram=0, disk=1)
  r111 = Units.resources_sum(r100, r010, r001)

  t1 = Task(name="p1p2", processes=[p1, p2], constraints=order(p1, p2),
            resources=Units.resources_sum(r100, r010))
  t2 = Task(name="p3p4", processes=[p3, p4], constraints=order(p3, p4),
            resources=r001)

  assert combine_tasks() == Task()
  assert combine_tasks(t1) == t1
  assert combine_tasks(t2) == t2

  t3 = combine_tasks(t1, t2)
  assert t3.name() == t2.name()
  assert t3.resources() == r111
  assert set(t3.processes()) == set([p1, p2, p3, p4])
  assert set(t3.constraints()) == set(order(p1, p2) + order(p3, p4))

  t4 = concat_tasks(t1, t2)
  assert t4.name() == t2.name()
  assert t4.resources() == r111
  assert set(t4.processes()) == set([p1, p2, p3, p4])
  assert set(t4.constraints()) == set(
      order(p1, p2) + order(p3, p4) + order(p1, p3) + order(p1, p4) +
      order(p2, p3) + order(p2, p4))
Beispiel #2
0
def test_add_resources():
  assert Units.resources_sum(Resources(), Resources()) == Resources(cpu=0, ram=0, disk=0)

  r100 = Resources(cpu=1, ram=0, disk=0)
  r010 = Resources(cpu=0, ram=1, disk=0)
  r001 = Resources(cpu=0, ram=0, disk=1)
  r111 = Resources(cpu=1, ram=1, disk=1)
  r222 = Resources(cpu=2, ram=2, disk=2)

  assert reduce(Units.resources_sum, [r100, r010, r001]) == r111
  assert Units.resources_sum(r111, r111) == r222
  assert r222 == Units.resources_sum(r100, r010, r001, r111, Resources())
Beispiel #3
0
def test_simple_task():
  name, command = 'simple_thing', 'echo hello'
  st = SimpleTask(name, command)
  assert isinstance(st, Task)
  assert st.name().get() == name
  assert len(st.constraints().get()) == 0
  assert len(st.processes().get()) == 1
  assert st.processes().get()[0]['cmdline'] == command
  assert st.resources() == Resources(cpu=Tasks.SIMPLE_CPU,
                                     ram=Tasks.SIMPLE_RAM,
                                     disk=Tasks.SIMPLE_DISK)
Beispiel #4
0
 def task(cls):
   base = Process(max_failures=2, min_duration=1)
   ex = base(cmdline="exit 1")
   hw = base(cmdline="echo hello world")
   task = Task(
     name = "failing_task",
     resources = Resources(cpu = 1.0, ram = 16*1024*1024, disk = 16*1024),
     max_failures = 0,
     processes = [ex(name='f1'), ex(name='f2'), ex(name='f3'),
                  hw(name='s1'), hw(name='s2'), hw(name='s3')])
   return task.interpolate()[0]
Beispiel #5
0
 def task(cls):
     hello_template = Process(cmdline="sleep 1")
     tsk = Task(name="complex",
                processes=[
                    hello_template(name="process1"),
                    hello_template(name="process2"),
                    hello_template(name="process3")
                ],
                resources=Resources(cpu=1.0,
                                    ram=16 * 1024 * 1024,
                                    disk=16 * 1024),
                max_concurrency=1)
     return tsk
Beispiel #6
0
 def task(cls):
   task = Task(
     name = "failing_task",
     resources = Resources(cpu = 1.0, ram = 16*1024*1024, disk = 16*1024),
     max_failures = 2,
     processes = [
       Process(name = "a", max_failures=1, min_duration=1, cmdline="echo hello world"),
       Process(name = "b", max_failures=1, min_duration=1, cmdline="exit 1"),
       Process(name = "c", max_failures=1, min_duration=1, cmdline="echo hello world")
     ],
     constraints = [{'order': ['a', 'b', 'c']}]
   )
   return task.interpolate()[0]
Beispiel #7
0
    def task(cls):
        setup_bashrc = Process(name="setup_bashrc",
                               cmdline=dedent("""
    mkdir -p .profile.d
    cat <<EOF > .thermos_profile
    for i in .profile.d/*.sh ; do
      if [ -r "\\$i" ]; then
        . \\$i
      fi
    done
    EOF
    """))

        setup_foo = Process(name="setup_foo",
                            cmdline=dedent("""
    cat <<EOF > .profile.d/setup_foo.sh
    export FOO=1
    EOF
    """))

        setup_bar = Process(name="setup_bar",
                            cmdline=dedent("""
    cat <<EOF > .profile.d/setup_bar.sh
    export BAR=2
    EOF
    """))

        foo_recipe = SequentialTask(processes=[setup_bashrc, setup_foo])
        bar_recipe = SequentialTask(processes=[setup_bashrc, setup_bar])
        all_recipes = Tasks.combine(foo_recipe, bar_recipe)

        run = Process(name="run",
                      cmdline=dedent("""
    echo $FOO $BAR > expected_output.txt
    """))

        my_task = Task(processes=[run],
                       resources=Resources(cpu=1.0,
                                           ram=16 * 1024 * 1024,
                                           disk=16 * 1024))
        return Tasks.concat(all_recipes, my_task, name="my_task")
Beispiel #8
0
def simplerun(args, options):
    """Run a simple command line as a thermos task.

    Usage: thermos simplerun [options] [--] commandline
    Options:
      --user=USER		   run as this user.  if not $USER, must have setuid privilege.
      --name=STRING		   the name to give this task. ('simple' by default)
      --task_id=STRING		   the id to which this task should be bound, synthesized from the
                                   task name if none provided.
      -P/--port=NAME:PORT	   bind the named port NAME to port number PORT (may be specified
                                   multiple times to bind multiple names.)
      -E/--environment=NAME=VALUE  bind the configuration environment variable NAME to
                                   VALUE.
      --daemon			   Fork and daemonize the task.
  """
    try:
        cutoff = args.index('--')
        cmdline = ' '.join(args[cutoff + 1:])
    except ValueError:
        cmdline = ' '.join(args)

    print("Running command: '%s'" % cmdline)

    thermos_task = ThermosTaskWrapper(
        Task(name=options.name,
             resources=Resources(cpu=1.0, ram=256 * 1024 * 1024, disk=0),
             processes=[Process(name=options.name, cmdline=cmdline)]))

    _really_run(thermos_task,
                options.root,
                None,
                task_id=options.task_id,
                user=options.user,
                prebound_ports=options.prebound_ports,
                chroot=False,
                daemon=options.daemon)
 def task(cls):
     ping_template = Process(
         name="{{process_name}}",
         min_duration=1,
         max_failures=5,
         cmdline=
         "echo {{process_name}} pinging;                                "
         "echo ping >> {{process_name}};                                "
         "echo current count $(cat {{process_name}} | wc -l);           "
         "if [ $(cat {{process_name}} | wc -l) -eq {{num_runs}} ]; then "
         "  exit 0;                                             "
         "else                                                  "
         "  exit 1;                                             "
         "fi                                                    ")
     tsk = Task(name="pingping",
                resources=Resources(cpu=1.0,
                                    ram=16 * 1024 * 1024,
                                    disk=16 * 1024),
                processes=[
                    ping_template.bind(process_name="p1", num_runs=1),
                    ping_template.bind(process_name="p2", num_runs=2),
                    ping_template.bind(process_name="p3", num_runs=3),
                ])
     return tsk.interpolate()[0]
Beispiel #10
0
def test_config_with_bad_resources():
    MB = 1048576
    hwtask = HELLO_WORLD.task()

    convert_pystachio_to_thrift(HELLO_WORLD)

    good_resources = [Resources(cpu=1.0, ram=1 * MB, disk=1 * MB)]

    bad_resources = [
        Resources(cpu=0, ram=1 * MB, disk=1 * MB),
        Resources(cpu=1, ram=0 * MB, disk=1 * MB),
        Resources(cpu=1, ram=1 * MB, disk=0 * MB),
        Resources(cpu=1, ram=1 * MB - 1, disk=1 * MB),
        Resources(cpu=1, ram=1 * MB, disk=1 * MB - 1)
    ]

    for resource in good_resources:
        convert_pystachio_to_thrift(
            HELLO_WORLD(task=hwtask(resources=resource)))

    for resource in bad_resources:
        with pytest.raises(ValueError):
            convert_pystachio_to_thrift(
                HELLO_WORLD(task=hwtask(resources=resource)))
Beispiel #11
0
from pystachio import Map, String
from pystachio.naming import frozendict
import pytest

HELLO_WORLD = Job(name='hello_world',
                  role='john_doe',
                  environment='staging66',
                  cluster='smf1-test',
                  task=Task(
                      name='main',
                      processes=[
                          Process(name='hello_world',
                                  cmdline='echo {{mesos.instance}}')
                      ],
                      resources=Resources(cpu=0.1,
                                          ram=64 * 1048576,
                                          disk=64 * 1048576),
                  ))


def test_simple_config():
    job = convert_pystachio_to_thrift(HELLO_WORLD)
    assert job.instanceCount == 1
    tti = job.taskConfig
    assert job.key == JobKey(role=HELLO_WORLD.role().get(),
                             environment=HELLO_WORLD.environment().get(),
                             name=HELLO_WORLD.name().get())
    assert job.owner == Identity(role=HELLO_WORLD.role().get(),
                                 user=getpass.getuser())
    assert job.cronSchedule == ''
    assert tti.jobName == 'hello_world'