def test_create_job_with_type(self): job = Job("hello", type="system") self.assertEqual(job.Type, "system") job = Job("hello", type="batch") self.assertEqual(job.Type, "batch") with self.assertRaises(ValueError): Job("hello", type="hello")
def test_create_minimal_job(self): job = Job("hello") self.assertEqual(job.ID, "hello") self.assertEqual(job.Name, "hello") self.assertEqual(job.Type, "service") self.assertEqual(job.Region, "global") self.assertEqual(len(job.TaskGroups), 0) self.assertEqual(len(job.Datacenters), 0) self.assertEqual(len(job.Constraints), 0)
def test_add_task_group(self): job = Job("hello") task = DockerTask("helloworld", "hashicorp/http-echo") job.addTask(task) self.assertEqual(len(job.TaskGroups), 1) tg = job.TaskGroups[0] self.assertEqual(tg.Name, "servers") new_group_name = "myname" job.addTask(task, new_group_name) tg2 = job.TaskGroups[job.getIndexOrCreateTaskGroup(new_group_name)] self.assertEqual(tg2.Name, new_group_name)
def get_random_job(): r = randomword(20) job = Job(r) job.Datacenters = ["dc1"] job.setWorkersCount(1) task = ExecTask(r, change_mode="restart") task.Driver = "raw_exec" task.addTemplate( task.makeTemplate( "local/start.sh", """#!/bin/bash python -m SimpleHTTPServer ${NOMAD_PORT_http} \ || python -m http.server ${NOMAD_PORT_http} """, ) ) task.Config.command = "local/start.sh" task.addService(r, port_label="http", checks=[http_check]) task.addTemplate(task.makeTemplate("local/test", r)) job.addTask(task) return job
#!/usr/bin/env nomadgen.pex from nomadgen.api import Job, DockerTask from nomadgen.util import export_if_last # Create a job from nomadgen.api.Job job = Job("consul") # Set the Datacenters where the job should run in. job.Datacenters = ["dc1"] # Set the amount of workers you need. It's recommended to use N + 3, when N is # the amount of workers required to serve at peak time. job.setWorkersCount(1) task = DockerTask("consul", "consul:1.3.0").setPort("http", 8500, 8500) task.Config.command_args = ["agent", "-dev"] # task.Config.network_mode = 'host' job.addTask(task) export_if_last(job)
def setUp(self): self.job = Job("hello") self.task = DockerTask("helloworld", "hashicorp/http-echo") self.job.addTask(self.task) pass
class JobTestCase(unittest.TestCase): def setUp(self): self.job = Job("hello") self.task = DockerTask("helloworld", "hashicorp/http-echo") self.job.addTask(self.task) pass def getTaskGroup(self, group="servers"): return self.job.TaskGroups[self.job.getIndexOrCreateTaskGroup(group)] def test_create_minimal_job(self): job = Job("hello") self.assertEqual(job.ID, "hello") self.assertEqual(job.Name, "hello") self.assertEqual(job.Type, "service") self.assertEqual(job.Region, "global") self.assertEqual(len(job.TaskGroups), 0) self.assertEqual(len(job.Datacenters), 0) self.assertEqual(len(job.Constraints), 0) def test_create_job_with_region(self): job = Job("hello", region="us-east-1") self.assertEqual(job.Region, "us-east-1") def test_create_job_with_type(self): job = Job("hello", type="system") self.assertEqual(job.Type, "system") job = Job("hello", type="batch") self.assertEqual(job.Type, "batch") with self.assertRaises(ValueError): Job("hello", type="hello") def test_add_task_group(self): job = Job("hello") task = DockerTask("helloworld", "hashicorp/http-echo") job.addTask(task) self.assertEqual(len(job.TaskGroups), 1) tg = job.TaskGroups[0] self.assertEqual(tg.Name, "servers") new_group_name = "myname" job.addTask(task, new_group_name) tg2 = job.TaskGroups[job.getIndexOrCreateTaskGroup(new_group_name)] self.assertEqual(tg2.Name, new_group_name) def test_set_workers_count(self): self.job.setWorkersCount(7) self.assertEqual(self.getTaskGroup().Count, 7) def test_set_canries(self): self.job.setCanaries(7) self.assertEqual(self.getTaskGroup().Update.Canary, 7) def test_set_ephemeral_disk(self): with self.assertRaises(AssertionError): self.job.setEphemeralDisk("hello") disk = EphemeralDisk(SizeMB=300) self.job.setEphemeralDisk(disk) tg = self.getTaskGroup() self.assertEqual(tg.EphemeralDisk, disk) def test_constraints(self): self.job.distinctHosts() self.assertEqual(self.job.Constraints[0].Operand, "distinct_hosts") self.assertEqual(self.job.Constraints[0].RTarget, "true") self.job.Constraints = [] self.job.addConstraint("a", "b") self.assertEqual(self.job.Constraints[0].Operand, "=") self.assertEqual(self.job.Constraints[0].RTarget, "b") self.assertEqual(self.job.Constraints[0].LTarget, "a")
def test_create_job_with_region(self): job = Job("hello", region="us-east-1") self.assertEqual(job.Region, "us-east-1")
#!/usr/bin/env nomadgen.pex from nomadgen.api import Job, DockerTask from nomadgen.jobspec.ttypes import Template from nomadgen.util import export_if_last # Create a job from nomadgen.api.Job job = Job("traefik") # Set the Datacenters where the job should run in. job.Datacenters = ["dc1"] # Set the amount of workers you need. It's recommended to use N + 3, when N is # the amount of workers required to serve at peak time. job.setWorkersCount(1) task = ( DockerTask("traefik", "traefik:latest") .setPort("http", 80) .setPort("traefik", 19999, map_to=19999) ) task.Leader = True task.Config.network_mode = "host" task.Config.command_args = ["-c", "/local/traefik.toml", "--api"] config_template = Template( DestPath="local/traefik.toml", EmbeddedTmpl=""" logLevel = "ERROR" defaultEntryPoints = ["http", "https"] [entryPoints]