Skip to content

Commit

Permalink
Fix tests by splitting integration and unit tests. (prometheus#396)
Browse files Browse the repository at this point in the history
Use JDK8.

Signed-off-by: Brian Brazil <[email protected]>
  • Loading branch information
brian-brazil authored Jul 4, 2019
1 parent a44cdb0 commit 3a59cb7
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 94 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
language: java

jdk:
- oraclejdk8

script: mvn verify
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Collections;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

public class JavaAgentIT {
Expand Down Expand Up @@ -113,97 +112,4 @@ private String resolveRelativePathToResource(String resource) {
final File workingDir = new File(new File(".").getAbsolutePath());
return "." + configwk.replace(workingDir.getParentFile().getAbsolutePath(), "");
}

/**
* Test that the agent string argument is parsed properly. We expect the agent argument in one of these forms...
* <pre>
* {@code <port>:<yaml configuration file>}
* </pre>
* <pre>
* {@code <host>:<port>:<yaml configuration file>}
* </pre>
* Since the ':' character is part of the spec for this arg, Windows-style paths could cause an issue with parsing.
* See https://github.com/prometheus/jmx_exporter/issues/312.
*/
@Test
public void testAgentStringParsing() {
final String DEFAULT_HOST = "0.0.0.0";

JavaAgent.Config config = JavaAgent.parseConfig("8080:config.yaml", DEFAULT_HOST);
Assert.assertEquals(DEFAULT_HOST, config.host);
Assert.assertEquals("config.yaml", config.file);
Assert.assertEquals(8080, config.port);

config = JavaAgent.parseConfig("8080:/unix/path/config.yaml", DEFAULT_HOST);
Assert.assertEquals(DEFAULT_HOST, config.host);
Assert.assertEquals("/unix/path/config.yaml", config.file);
Assert.assertEquals(8080, config.port);

config = JavaAgent.parseConfig("google.com:8080:/unix/path/config.yaml", DEFAULT_HOST);
Assert.assertEquals("google.com", config.host);
Assert.assertEquals("/unix/path/config.yaml", config.file);
Assert.assertEquals(8080, config.port);

config = JavaAgent.parseConfig("127.0.0.1:8080:/unix/path/config.yaml", DEFAULT_HOST);
Assert.assertEquals("127.0.0.1", config.host);
Assert.assertEquals("/unix/path/config.yaml", config.file);
Assert.assertEquals(8080, config.port);

config = JavaAgent.parseConfig("8080:\\Windows\\Local\\Drive\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals(DEFAULT_HOST, config.host);
Assert.assertEquals("\\Windows\\Local\\Drive\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);

// the following check was previously failing to parse the file correctly
config = JavaAgent.parseConfig("8080:C:\\Windows\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals(DEFAULT_HOST, config.host);
Assert.assertEquals("C:\\Windows\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);

// the following check was previously failing to parse the file correctly
config = JavaAgent.parseConfig("google.com:8080:C:\\Windows\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals("google.com", config.host);
Assert.assertEquals("C:\\Windows\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);

// the following check was previously failing to parse the file correctly
config = JavaAgent.parseConfig("127.0.0.1:8080:C:\\Windows\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals("127.0.0.1", config.host);
Assert.assertEquals("C:\\Windows\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);
}

/**
* If someone is specifying an ipv6 address and a host name, this should be rejected.
*/
@Test(expected = IllegalArgumentException.class)
public void testRejectInvalidInput() {
JavaAgent.parseConfig("[2001:0db8:0000:0042:0000:8a2e:0370:7334]:localhost:8080:config.yaml", "0.0.0.0");
}

/**
* Similarly to the test above, two host names
*/
@Test(expected = IllegalArgumentException.class)
public void testRejectInvalidInput2() {
JavaAgent.parseConfig("127.0.0.1:localhost:8080:config.yaml", "0.0.0.0");
}

/**
* Exercise the existing Ipv6 parsing logic. The current logic leaves the brackets on the host.
*/
@Test
public void testIpv6AddressParsing() {
final String DEFAULT_HOST = "0.0.0.0";

JavaAgent.Config config = JavaAgent.parseConfig("[1:2:3:4]:8080:config.yaml", DEFAULT_HOST);
Assert.assertEquals("[1:2:3:4]", config.host);
Assert.assertEquals("config.yaml", config.file);
Assert.assertEquals(8080, config.port);

config = JavaAgent.parseConfig("[2001:0db8:0000:0042:0000:8a2e:0370:7334]:8080:C:\\Windows\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals("[2001:0db8:0000:0042:0000:8a2e:0370:7334]", config.host);
Assert.assertEquals("C:\\Windows\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package io.prometheus.jmx;

import org.junit.Assert;
import org.junit.Test;

public class TestJavaAgent {
/**
* Test that the agent string argument is parsed properly. We expect the agent argument in one of these forms...
* <pre>
* {@code <port>:<yaml configuration file>}
* </pre>
* <pre>
* {@code <host>:<port>:<yaml configuration file>}
* </pre>
* Since the ':' character is part of the spec for this arg, Windows-style paths could cause an issue with parsing.
* See https://github.com/prometheus/jmx_exporter/issues/312.
*/
@Test
public void testAgentStringParsing() {
final String DEFAULT_HOST = "0.0.0.0";

JavaAgent.Config config = JavaAgent.parseConfig("8080:config.yaml", DEFAULT_HOST);
Assert.assertEquals(DEFAULT_HOST, config.host);
Assert.assertEquals("config.yaml", config.file);
Assert.assertEquals(8080, config.port);

config = JavaAgent.parseConfig("8080:/unix/path/config.yaml", DEFAULT_HOST);
Assert.assertEquals(DEFAULT_HOST, config.host);
Assert.assertEquals("/unix/path/config.yaml", config.file);
Assert.assertEquals(8080, config.port);

config = JavaAgent.parseConfig("google.com:8080:/unix/path/config.yaml", DEFAULT_HOST);
Assert.assertEquals("google.com", config.host);
Assert.assertEquals("/unix/path/config.yaml", config.file);
Assert.assertEquals(8080, config.port);

config = JavaAgent.parseConfig("127.0.0.1:8080:/unix/path/config.yaml", DEFAULT_HOST);
Assert.assertEquals("127.0.0.1", config.host);
Assert.assertEquals("/unix/path/config.yaml", config.file);
Assert.assertEquals(8080, config.port);

config = JavaAgent.parseConfig("8080:\\Windows\\Local\\Drive\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals(DEFAULT_HOST, config.host);
Assert.assertEquals("\\Windows\\Local\\Drive\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);

// the following check was previously failing to parse the file correctly
config = JavaAgent.parseConfig("8080:C:\\Windows\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals(DEFAULT_HOST, config.host);
Assert.assertEquals("C:\\Windows\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);

// the following check was previously failing to parse the file correctly
config = JavaAgent.parseConfig("google.com:8080:C:\\Windows\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals("google.com", config.host);
Assert.assertEquals("C:\\Windows\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);

// the following check was previously failing to parse the file correctly
config = JavaAgent.parseConfig("127.0.0.1:8080:C:\\Windows\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals("127.0.0.1", config.host);
Assert.assertEquals("C:\\Windows\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);
}

/**
* If someone is specifying an ipv6 address and a host name, this should be rejected.
*/
@Test(expected = IllegalArgumentException.class)
public void testRejectInvalidInput() {
JavaAgent.parseConfig("[2001:0db8:0000:0042:0000:8a2e:0370:7334]:localhost:8080:config.yaml", "0.0.0.0");
}

/**
* Similarly to the test above, two host names
*/
@Test(expected = IllegalArgumentException.class)
public void testRejectInvalidInput2() {
JavaAgent.parseConfig("127.0.0.1:localhost:8080:config.yaml", "0.0.0.0");
}

/**
* Exercise the existing Ipv6 parsing logic. The current logic leaves the brackets on the host.
*/
@Test
public void testIpv6AddressParsing() {
final String DEFAULT_HOST = "0.0.0.0";

JavaAgent.Config config = JavaAgent.parseConfig("[1:2:3:4]:8080:config.yaml", DEFAULT_HOST);
Assert.assertEquals("[1:2:3:4]", config.host);
Assert.assertEquals("config.yaml", config.file);
Assert.assertEquals(8080, config.port);

config = JavaAgent.parseConfig("[2001:0db8:0000:0042:0000:8a2e:0370:7334]:8080:C:\\Windows\\Path\\config.yaml", DEFAULT_HOST);
Assert.assertEquals("[2001:0db8:0000:0042:0000:8a2e:0370:7334]", config.host);
Assert.assertEquals("C:\\Windows\\Path\\config.yaml", config.file);
Assert.assertEquals(8080, config.port);
}
}

0 comments on commit 3a59cb7

Please sign in to comment.