static PortMappingWithName parsePortMapping(final String portSpec) { final Matcher matcher = PATTERN.matcher(portSpec); if (!matcher.matches()) { throw new IllegalArgumentException("Bad port mapping: " + portSpec); } final String name = matcher.group("n"); final String ip = matcher.group("ip"); final int internal = Integer.parseInt(matcher.group("i")); final Integer external = nullOrInteger(matcher.group("e")); final String protocol = fromNullable(matcher.group("p")).or(TCP); return PortMappingWithName.create(name, PortMapping.builder() .ip(ip) .internalPort(internal) .externalPort(external) .protocol(protocol) .build()); }
@Test public void testPortMappingWithIp() throws Exception { startDefaultMaster(); startDefaultAgent(testHost()); awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS); // Create job with a portmapping that has an IP. Check it doesn't throw. createJobRawOutput(Job.newBuilder() .setName(testJobName) .setVersion(testJobVersion) .setImage(BUSYBOX) .setPorts(Collections.singletonMap("foo", PortMapping.builder() .ip("127.0.0.1") .internalPort(80) .externalPort(80) .build() )).build()); }
@Test public void testPortMappingWithBadIp() throws Exception { startDefaultMaster(); startDefaultAgent(testHost()); awaitHostStatus(testHost(), UP, LONG_WAIT_SECONDS, SECONDS); expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("foobar is not a valid IP address."); // Create job with a portmapping that has a bad IP. Check it throws. createJobRawOutput(Job.newBuilder() .setName(testJobName) .setVersion(testJobVersion) .setImage(BUSYBOX) .setPorts(Collections.singletonMap("foo", PortMapping.builder() .ip("foobar") .internalPort(80) .externalPort(80) .build() )).build()); } }