Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couchbase in docker for integration tests: Make the ports 8092, 8093, 8094 and 8095 configurable to be able to use docker’s random ports

I am using Couchbase java client SDK 2.7.9 and am running into a problem while trying to run automated integration tests. In such test we usually use random ports to be able to run the same thing on the same Jenkins slave (using docker for example).

But, with the client, we can specify many custom ports but not the 8092, 8093, 8094 and 8095.

The popular TestContainers modules mention as well that those port have to remain static in their Couchbase module: https://www.testcontainers.org/modules/databases/couchbase/ 1

Apparently it is also possible to change those ports at the server level.

Example:

Docker-compose.yml

version: '3.0'
services:
  rapid_test_cb:
    build:
      context: ""
      dockerfile: cb.docker
    ports:
      - "8091"
      - "8092"
      - "8093"
      - "11210"

The docker image is ‘couchbase:community-5.1.1’

Internally the ports are the ones written above but externally they are random. At the client level you can set up bootstrapHttpDirectPort and bootstrapCarrierDirectPort but apparently the 8092 and 8093 ports are taken from the server-side (who does not know which port was assigned to him).

I would like to ask you whether it is possible to change those ports at the client level and, if not, to seriously consider adding that feature.

like image 341
Arnaud Villevieille Avatar asked Dec 11 '19 00:12

Arnaud Villevieille


1 Answers

So, as discussed with the Couchbase team here,

it is not really possible. So we found a way to make it work using Gradle's docker compose plugin but I imagine it would work in different situations (TestContainer could use a similar system).

docker-compose.yml:

version: '3.0'
services:
  rapid_test_cb:
    build:
      context: ""
      dockerfile: cb.docker
    ports:
      - "${COUCHBASE_RANDOM_PORT_8091}:${COUCHBASE_RANDOM_PORT_8091}"
      - "${COUCHBASE_RANDOM_PORT_8092}:${COUCHBASE_RANDOM_PORT_8092}"
      - "${COUCHBASE_RANDOM_PORT_8093}:${COUCHBASE_RANDOM_PORT_8093}"
      - "${COUCHBASE_RANDOM_PORT_11210}:${COUCHBASE_RANDOM_PORT_11210}"
    environment:
      COUCHBASE_RANDOM_PORT_8091: ${COUCHBASE_RANDOM_PORT_8091}
      COUCHBASE_RANDOM_PORT_8092: ${COUCHBASE_RANDOM_PORT_8092}
      COUCHBASE_RANDOM_PORT_8093: ${COUCHBASE_RANDOM_PORT_8093}
      COUCHBASE_RANDOM_PORT_11210: ${COUCHBASE_RANDOM_PORT_11210}

cb.docker:

FROM couchbase:community-5.1.1
COPY configure-node.sh /opt/couchbase
#HEALTHCHECK --interval=5s --timeout=3s CMD curl --fail http://localhost:8091/pools || exit 1
RUN chmod u+x /opt/couchbase/configure-node.sh
RUN echo "{rest_port, 8091}.\n{query_port, 8093}.\n{memcached_port, 11210}." >> /opt/couchbase/etc/couchbase/static_config
CMD ["/opt/couchbase/configure-node.sh"]

configure-node.sh:

#!/bin/bash

poll() {
  # The argument supplied to the function is invoked using "$@", we check the return value with $?
  "$@"
  while [ $? -ne 0 ]
    do
      echo 'waiting for couchbase to start'
      sleep 1
      "$@"
  done
}

set -x
set -m
if [[ -n "${COUCHBASE_RANDOM_PORT_8092}" ]]; then
    sed -i "s|8092|${COUCHBASE_RANDOM_PORT_8092}|g" /opt/couchbase/etc/couchdb/default.d/capi.ini
fi
if [[ -n "${COUCHBASE_RANDOM_PORT_8091}" ]]; then
    sed -i "s|8091|${COUCHBASE_RANDOM_PORT_8091}|g" /opt/couchbase/etc/couchbase/static_config
fi
if [[ -n "${COUCHBASE_RANDOM_PORT_8093}" ]]; then
    sed -i "s|8093|${COUCHBASE_RANDOM_PORT_8093}|g" /opt/couchbase/etc/couchbase/static_config
fi
if [[ -n "${COUCHBASE_RANDOM_PORT_11210}" ]]; then
    sed -i "s|11210|${COUCHBASE_RANDOM_PORT_11210}|g" /opt/couchbase/etc/couchbase/static_config
fi

/entrypoint.sh couchbase-server &
poll curl -s localhost:${COUCHBASE_RANDOM_PORT_8091:-8091}

# Setup index and memory quota
curl -v -X POST http://127.0.0.1:${COUCHBASE_RANDOM_PORT_8091:-8091}/pools/default --noproxy '127.0.0.1' -d memoryQuota=300 -d indexMemoryQuota=300
# Setup services
curl -v http://127.0.0.1:${COUCHBASE_RANDOM_PORT_8091:-8091}/node/controller/setupServices --noproxy '127.0.0.1' -d services=kv%2Cn1ql%2Cindex
# Setup credentials
curl -v http://127.0.0.1:${COUCHBASE_RANDOM_PORT_8091:-8091}/settings/web --noproxy '127.0.0.1' -d port=${couchbase_random_port_8091:-8091} -d username=Administrator -d password=password
# Load the rapid_test bucket
curl -X POST -u Administrator:password -d name=rapid_test -d ramQuotaMB=128 --noproxy '127.0.0.1' -d authType=sasl -d saslPassword=password -d replicaNumber=0 -d flushEnabled=1 -v http://127.0.0.1:${COUCHBASE_RANDOM_PORT_8091:-8091}/pools/default/buckets

fg 1

Gradle's docker compose configuration:

def findRandomOpenPortOnAllLocalInterfaces = {
    new ServerSocket(0).withCloseable { socket ->
        return socket.getLocalPort().intValue()
    }
}

dockerCompose {
    environment.put 'COUCHBASE_RANDOM_PORT_8091', findRandomOpenPortOnAllLocalInterfaces()
    environment.put 'COUCHBASE_RANDOM_PORT_8092', findRandomOpenPortOnAllLocalInterfaces()
    environment.put 'COUCHBASE_RANDOM_PORT_8093', findRandomOpenPortOnAllLocalInterfaces()
    environment.put 'COUCHBASE_RANDOM_PORT_11210', findRandomOpenPortOnAllLocalInterfaces()
}
integTest.doFirst {
    systemProperty 'com.couchbase.bootstrapHttpDirectPort', couchbase_random_port_8091
    systemProperty 'com.couchbase.bootstrapCarrierDirectPort', couchbase_random_port_11210
}
like image 103
Arnaud Villevieille Avatar answered Oct 17 '22 17:10

Arnaud Villevieille