// Create job
final JobId jobId = createJob(testJobName, testJobVersion, image, IDLE_COMMAND, env, ports,
registration);
// Query for job
final Job expected = Job.newBuilder()
.setName(testJobName)
.setVersion(testJobVersion)
.setImage(image)
.setCommand(IDLE_COMMAND)
.setEnv(env)
.setPorts(ports)
.setRegistration(registration)
.setCreatingUser(TEST_USER)
.build();
final String inspectOutput = cli("inspect", "--json", jobId.toString());
final Job parsed = Json.read(inspectOutput, Job.class);
assertJobEquals(expected, parsed);
assertThat(cli("jobs", testJobName, "-q"), containsString(jobId.toString()));
assertThat(cli("jobs", testJobName + ":" + testJobVersion, "-q"),
containsString(jobId.toString()));
assertEquals("job pattern foozbarz matched no jobs", cli("jobs", "foozbarz").trim());
assertTrue(cli("jobs", "foozbarz", "-q").isEmpty());
// Create a new job using the first job as a template
final Job expectedCloned = expected.toBuilder()
.setVersion(expected.getId().getVersion() + "-cloned")
.build();
final JobId clonedJobId = JobId.parse(WHITESPACE.trimFrom(
cli("create", "-q", "-t",
testJobName + ":" + testJobVersion,
testJobName + ":" + testJobVersion + "-cloned")));
final String clonedInspectOutput = cli("inspect", "--json", clonedJobId.toString());
final Job clonedParsed = Json.read(clonedInspectOutput, Job.class);
assertEquals(expectedCloned, clonedParsed);
// Verify that port mapping and environment variables are correct
final String statusString = cli("status", "--job", jobId.toString(), "--json");
final Map<JobId, JobStatus> statuses = Json.read(statusString, STATUSES_TYPE);
final Job job = statuses.get(jobId).getJob();
assertEquals(ServicePorts.of("foo"),
job.getRegistration().get(ServiceEndpoint.of("foo-service", "hm")));
assertEquals(ServicePorts.of("bar"),
job.getRegistration().get(ServiceEndpoint.of("bar-service", "http")));
assertEquals(4711, job.getPorts().get("foo").getInternalPort());
assertEquals(PortMapping.of(5000, externalPort), job.getPorts().get("bar"));
assertEquals("f00d", job.getEnv().get("BAD"));
final String duplicateJob = cli("create", testJobName + ":" + testJobVersion, image, "--",
IDLE_COMMAND);
assertThat(duplicateJob, containsString("JOB_ALREADY_EXISTS"));