Open fabriciorby opened 2 years ago
After 2 days of trying I was able to make it work.
It seems like it's some issue with the SDK or something like that, but here's the dirty fix to make it work with testContainers:
Creating the emulator:
static {
try {
Consumer<CreateContainerCmd> cmd =
e -> e.withPortBindings(
new PortBinding(Ports.Binding.bindPort(8081), new ExposedPort(8081)),
new PortBinding(Ports.Binding.bindPort(10251), new ExposedPort(10251)),
new PortBinding(Ports.Binding.bindPort(10252), new ExposedPort(10252)),
new PortBinding(Ports.Binding.bindPort(10253), new ExposedPort(10253)),
new PortBinding(Ports.Binding.bindPort(10254), new ExposedPort(10254))
);
emulator = new CosmosDBEmulatorContainer(
DockerImageName.parse("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator"))
.withCreateContainerCmdModifier(cmd)
.withExposedPorts(8081, 10251, 10252, 10253, 10254)
.withEnv("AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE", InetAddress.getLocalHost().getHostAddress())
.withEnv("AZURE_COSMOS_EMULATOR_PARTITION_COUNT", "3")
.withEnv("AZURE_COSMOS_EMULATOR_ENABLE_DATA_PERSISTENCE", "true");
emulator.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Creating the client:
public static CosmosAsyncClient createClient(final DirectConnectionConfig directConnectionConfig) {
return new CosmosClientBuilder()
.endpointDiscoveryEnabled(false)
.endpoint(emulator.getEmulatorEndpoint())
.key(emulator.getEmulatorKey())
.directMode(directConnectionConfig)
.contentResponseOnWriteEnabled(false)
.buildAsyncClient();
}
Would it be valuable for the project it I contribute by raising a PR even using this deprecated e.withPortBindings
method? If not, at least a note about this on the Testcontainers Azure module page would be nice.
I had to map to fixed ports, probably related to this issue on cosmos-emulator repo https://github.com/Azure/azure-cosmos-db-emulator-docker/issues/50
Hi @fabriciorby
No need to do all of that. I found that the sdk is looking for port 10251
which is not open on cosmodbemulator and there is no a way to configure it to a random port via env variable, for example. However, you can do something like this.
Add the following dependency com.github.terma:javaniotcpproxy:1.5
CosmosDBEmulatorContainer emulator = new CosmosDBEmulatorContainer(
DockerImageName.parse("mcr.microsoft.com/cosmosdb/linux/azure-cosmos-emulator:latest")
)
.withExposedPorts(8081, 10251)
.withEnv("AZURE_COSMOS_EMULATOR_IP_ADDRESS_OVERRIDE", InetAddress.getLocalHost().getHostAddress())
emulator.start();
StaticTcpProxyConfig tcpProxyConfig = new StaticTcpProxyConfig(10251, emulator.getHost(), emulator.getMappedPort(10251));
tcpProxyConfig.setWorkerCount(1);
TcpProxy tcpProxy = new TcpProxy(tcpProxyConfig);
tcpProxy.start();
I hope this can help. I have provided feedback via email to the cosmodb team about providing a way to override the port via env variable in the sdk.
Hey @eddumelendez, thanks for your reply!
I've tried the code above and it did not work. I tried running by exposing only the port 8081
and 10251
with my solution and it did not work as well.
Tried again proxying 10251
, 10252
, 10253
, 10254
and it was successful, I think it's needed to expose all 4 ports.
I liked this solution, so I'll keep it. πππ»
By the way, nice to know about this lib.
I think there is a different setup on your side which requires more ports. TBH, don't know much about cosmodb and the suggestion is only a workaround due to it will be fixing ports. We will be taking this issue as an enhancement to support direct mode.
I had same issue and Fabricio's solution works for me.
@fabriciorby to avoid deprecated methods, use e -> e.getHostConfig().withPortBindings
Module
Azure
Testcontainers version
1.17.2
Using the latest Testcontainers version?
Yes
Docker version
What happened?
Hello,
I want to run the testcontainer using direct connection instead of gateway connection for Cosmos DB. https://www.testcontainers.org/modules/azure/
After following this tutorial I was able to run on gateway mode, but when I try to run on direct I get
Fail to reach global gateway [https://localhost:54950]
I've exposed the missing ports
10251, 10252, 10253, 10254
but the error still goes on.When I use the image directly on Docker I am able to run the tests without any issues. https://docs.microsoft.com/en-us/azure/cosmos-db/linux-emulator?tabs=sql-api%2Cssl-netstd21
Relevant log output
Additional Information
No response