-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #305 from pobtastic/172-add-networks
#172: Additional argument to use specific docker network
- Loading branch information
Showing
8 changed files
with
139 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/cd/go/contrib/elasticagents/docker/Networks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package cd.go.contrib.elasticagents.docker; | ||
|
||
import static cd.go.contrib.elasticagents.docker.utils.Util.splitIntoLinesAndTrimSpaces; | ||
import static org.apache.commons.lang.StringUtils.isBlank; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import com.spotify.docker.client.messages.Network; | ||
|
||
public class Networks { | ||
|
||
private Networks() {} | ||
|
||
public static String firstMatching(String networkConfig, List<Network> dockerNetworks) { | ||
if (isBlank(networkConfig)) { | ||
return null; | ||
} | ||
|
||
final List<String> networkEntries = splitIntoLinesAndTrimSpaces(networkConfig); | ||
|
||
final Collection<String> missingNetworks = networkEntries | ||
.stream() | ||
.filter(networkEntry -> dockerNetworks.stream().noneMatch(network -> network.name().equals(networkEntry))) | ||
.collect(Collectors.toList()); | ||
|
||
if (!missingNetworks.isEmpty()) { | ||
throw new RuntimeException(String.format("Networks %s do not exist.", missingNetworks)); | ||
} | ||
|
||
return networkEntries.get(0); | ||
} | ||
|
||
public static Collection<String> getAdditionalNetworks(String networkConfig) { | ||
if (isBlank(networkConfig)) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
final List<String> networkEntries = splitIntoLinesAndTrimSpaces(networkConfig); | ||
|
||
return networkEntries.isEmpty() ? networkEntries : networkEntries.subList(1, networkEntries.size()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/test/java/cd/go/contrib/elasticagents/docker/NetworksTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package cd.go.contrib.elasticagents.docker; | ||
|
||
import com.spotify.docker.client.messages.Network; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.mockito.Mockito.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class NetworksTest { | ||
|
||
@Test | ||
public void shouldReturnNullWhenNetworkConfigIsNotProvided() { | ||
assertNull(Networks.firstMatching(null, Collections.emptyList())); | ||
assertNull(Networks.firstMatching("", Collections.emptyList())); | ||
} | ||
|
||
@Test | ||
public void shouldReturnFirstNetworkFromString() { | ||
Network network = mock(Network.class); | ||
when(network.name()).thenReturn("gocd-net"); | ||
when(network.id()).thenReturn("network-id-1"); | ||
|
||
List<Network> networks = new ArrayList<>(); | ||
networks.add(network); | ||
|
||
String result = Networks.firstMatching("gocd-net", networks); | ||
|
||
assertNotNull(result); | ||
assertEquals("gocd-net", result); | ||
} | ||
|
||
@Test | ||
public void shouldThrowExceptionWhenNetworkDoesNotExist() { | ||
RuntimeException exception = assertThrows(RuntimeException.class, () -> { | ||
Networks.firstMatching("gocd-net", Collections.emptyList()); | ||
}); | ||
|
||
assertEquals("Networks [gocd-net] do not exist.", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
public void shouldReturnEmptyListForAdditionalNetworksWhenOnlyOneNetwork() { | ||
assertTrue(Networks.getAdditionalNetworks("single-network").isEmpty()); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAdditionalNetworksWhenMultipleNetworksProvided() { | ||
String networkConfig = "network1\nnetwork2\nnetwork3"; | ||
var additionalNetworks = Networks.getAdditionalNetworks(networkConfig); | ||
|
||
assertEquals(2, additionalNetworks.size()); | ||
assertTrue(additionalNetworks.contains("network2")); | ||
assertTrue(additionalNetworks.contains("network3")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters