Skip to content

Commit

Permalink
Fix case-sensitivity bug for port numbers in command (#524)
Browse files Browse the repository at this point in the history
* Fix case-sensitivity bug for port numbers in command

This bug shows itself when a port number contains letters in the hexadecimal conversion.

* Make grep ignore casing

* Rename and cleanup Nginx test configuration file

* Update the changelog for PR #524

* Fix a typo in the changelog
  • Loading branch information
dnno authored and bsideup committed Dec 17, 2017
1 parent fcefad3 commit ed64c1f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,9 @@
# Change Log
All notable changes to this project will be documented in this file.

### Fixed
- Fixed problem with case-sensitivity when checking internal port. ([\#524](https://github.com/testcontainers/testcontainers-java/pull/524))

### Changed
- Added `getDatabaseName` method to JdbcDatabaseContainer, MySQLContainer, PostgreSQLContainer ([\#473](https://github.com/testcontainers/testcontainers-java/issues/473))

Expand Down
Expand Up @@ -29,7 +29,7 @@ public Boolean call() {

private void tryPort(Integer internalPort) {
String[][] commands = {
{"/bin/sh", "-c", format("cat /proc/net/tcp | awk '{print $2}' | grep :%x && echo %s", internalPort, SUCCESS_MARKER)},
{"/bin/sh", "-c", format("cat /proc/net/tcp | awk '{print $2}' | grep -i :%x && echo %s", internalPort, SUCCESS_MARKER)},
{"/bin/sh", "-c", format("nc -vz -w 1 localhost %d && echo %s", internalPort, SUCCESS_MARKER)},
{"/bin/bash", "-c", format("</dev/tcp/localhost/%d && echo %s", internalPort, SUCCESS_MARKER)}
};
Expand Down
Expand Up @@ -3,19 +3,23 @@
import com.google.common.collect.ImmutableSet;
import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;

import static org.rnorth.visibleassertions.VisibleAssertions.assertThrows;
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;

public class InternalCommandPortListeningCheckTest {

// Linking a custom configuration into the container so that nginx is listening on port 8080. This is necessary to proof
// that the command formatting uses the correct casing for hexadecimal numberd (i.e. 1F90 and not 1f90).
@Rule
public GenericContainer nginx = new GenericContainer<>("nginx:1.9.4");
public GenericContainer nginx = new GenericContainer<>("nginx:1.9.4")
.withClasspathResourceMapping("nginx_on_8080.conf", "/etc/nginx/conf.d/default.conf", BindMode.READ_ONLY);

@Test
public void singleListening() {
final InternalCommandPortListeningCheck check = new InternalCommandPortListeningCheck(nginx, ImmutableSet.of(80));
final InternalCommandPortListeningCheck check = new InternalCommandPortListeningCheck(nginx, ImmutableSet.of(8080));

final Boolean result = check.call();

Expand All @@ -24,7 +28,7 @@ public void singleListening() {

@Test
public void nonListening() {
final InternalCommandPortListeningCheck check = new InternalCommandPortListeningCheck(nginx, ImmutableSet.of(80, 1234));
final InternalCommandPortListeningCheck check = new InternalCommandPortListeningCheck(nginx, ImmutableSet.of(8080, 1234));

assertThrows("InternalCommandPortListeningCheck detects a non-listening port among many",
IllegalStateException.class,
Expand Down
5 changes: 5 additions & 0 deletions core/src/test/resources/nginx_on_8080.conf
@@ -0,0 +1,5 @@
# This configuration makes Nginx listen on port port 8080

server {
listen 8080;
}

0 comments on commit ed64c1f

Please sign in to comment.