编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

Docker中的Couchbase进行集成测试:将端口8092、8093、8094和8095配置为可使用Docker的随机端口:Couchbase in docker for integration tests: Make the ports 8092, 8093, 8094 and 8095 configurable to be able to use docker’s random ports

Eryk Sun 云计算 2022-5-10 14:59 8人围观

腾讯云服务器
Docker中的Couchbase进行集成测试:将端口8092、8093、8094和8095配置为可使用Docker的随机端口的处理方法

我正在使用Couchbase Java客户端SDK 2.7.9,并且在尝试运行自动集成测试时遇到问题.在这种测试中,我们通常使用随机端口来在同一Jenkins从站上运行相同的事物(例如,使用docker).

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).

但是,对于客户端,我们可以指定许多自定义端口,但不能指定8092、8093、8094和8095.

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

流行的TestContainers模块还提到这些端口必须在其Couchbase模块中保持静态:https://www.testcontainers.org/modules/databases/couchbase/ 1

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.

示例:

Docker-compose.yml

Docker-compose.yml

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

泊坞窗图片为"couchbase:community-5.1.1"

The docker image is ‘couchbase:community-5.1.1’

内部端口是上面编写的端口,但外部端口是随机的.在客户端级别,您可以设置bootstrapHttpDirectPort和bootstrapCarrierDirectPort,但是显然8092和8093端口是从服务器端获取的(谁不知道为他分配了哪个端口).

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.

问题解答

因此,正如Couchbase小组所讨论的那样

So, as discussed with the Couchbase team here,

这是不可能的.因此,我们找到了一种使用Gradle的docker compose插件使其工作的方法,但我想它可以在不同的情况下工作(TestContainer可以使用类似的系统).

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:

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:

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:

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的docker compose配置:

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 } 

这篇关于Docker中的Couchbase进行集成测试:将端口8092、8093、8094和8095配置为可使用Docker的随机端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^