RancherDesktop 在 MacOs 上无法与 JUnit Testcontainers 配合使用。

huangapple go评论60阅读模式
英文:

RancherDesktop on MacOs not working with JUnit Testcontainers

问题

我正在尝试在我的运行Rancher Desktop的MacOS上使用Testcontainers运行JUnit测试。Docker CLI正常工作,因为我可以使用`docker run`运行任何容器。

JUnit测试的源代码如下(已简化和缩短):
````@SpringBootTest
@Testcontainers
class FactoryDiscoveryDataIT {

  @Container
  private static final PostgreSQLContainer POSTGRES_CONTAINER =  new PostgreSQLContainer("postgres:11.11-alpine")
        .withDatabaseName("psql-link-test");

错误消息如下:

2023-03-08 08:14:28,816 ERROR [main] org.testcontainers.dockerclient.DockerClientProviderStrategy: 无法找到有效的Docker环境。请检查配置。尝试的配置为:
2023-03-08 08:14:28,816 ERROR [main] org.testcontainers.dockerclient.DockerClientProviderStrategy: UnixSocketClientProviderStrategy: 由于异常InvalidConfigurationException(无法找到Unix域套接字),因此失败。根本原因是NoSuchFileException(/var/run/docker.sock)
2023-03-08 08:14:28,816 ERROR [main] org.testcontainers.dockerclient.DockerClientProviderStrategy: 由于找不到有效的配置,因此无法继续执行

测试被忽略。

java.lang.IllegalStateException: 无法找到有效的Docker环境。请查看日志并检查配置

  // 错误信息较长,已省略部分

我相当确定代码是正确的,因为它在我的minikube配置中起作用。对于minikube,我可以设置由minikube docker-env导出的环境变量,以将minikube用作构建环境,这很好用。

Rancher Desktop是否有类似的解决方案?是否有其他方法让JUnit Testcontainers使用我的Rancher Desktop安装?


<details>
<summary>英文:</summary>

I&#39;m trying to run a JUnit test using Testcontainers on my MacOs running Rancher Desktop. The docker CLI works fine as I can run any container using `docker run`. 

The source code of the JUnit test looks as follows (simplified and shortened):
````@SpringBootTest
@Testcontainers
class FactoryDiscoveryDataIT {

  @Container
  private static final PostgreSQLContainer POSTGRES_CONTAINER =  new PostgreSQLContainer(&quot;postgres:11.11-alpine&quot;)
        .withDatabaseName(&quot;psql-link-test&quot;);

The error message is the following:

2023-03-08 08:14:28,816 ERROR [main] org.testcontainers.dockerclient.DockerClientProviderStrategy: Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
2023-03-08 08:14:28,816 ERROR [main] org.testcontainers.dockerclient.DockerClientProviderStrategy:     UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (Could not find unix domain socket). Root cause NoSuchFileException (/var/run/docker.sock)
2023-03-08 08:14:28,816 ERROR [main] org.testcontainers.dockerclient.DockerClientProviderStrategy: As no valid configuration was found, execution cannot continue

Test ignored.

java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration

	at org.testcontainers.dockerclient.DockerClientProviderStrategy.lambda$getFirstValidStrategy$6(DockerClientProviderStrategy.java:257)
	at java.base/java.util.Optional.orElseThrow(Optional.java:403)
	at org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:247)
	at org.testcontainers.DockerClientFactory.getOrInitializeStrategy(DockerClientFactory.java:135)
	at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:171)
	at org.testcontainers.DockerClientFactory$1.getDockerClient(DockerClientFactory.java:89)
	at com.github.dockerjava.api.DockerClientDelegate.authConfig(DockerClientDelegate.java:108)
	at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:321)
	at org.testcontainers.junit.jupiter.TestcontainersExtension$StoreAdapter.start(TestcontainersExtension.java:242)
	at org.testcontainers.junit.jupiter.TestcontainersExtension$StoreAdapter.access$200(TestcontainersExtension.java:229)
	at org.testcontainers.junit.jupiter.TestcontainersExtension.lambda$null$1(TestcontainersExtension.java:56)
	at org.junit.jupiter.engine.execution.ExtensionValuesStore.lambda$getOrComputeIfAbsent$4(ExtensionValuesStore.java:86)
	at org.junit.jupiter.engine.execution.ExtensionValuesStore$MemoizingSupplier.computeValue(ExtensionValuesStore.java:223)
	at org.junit.jupiter.engine.execution.ExtensionValuesStore$MemoizingSupplier.get(ExtensionValuesStore.java:211)
	at org.junit.jupiter.engine.execution.ExtensionValuesStore$StoredValue.evaluate(ExtensionValuesStore.java:191)
	at org.junit.jupiter.engine.execution.ExtensionValuesStore$StoredValue.access$100(ExtensionValuesStore.java:171)
	at org.junit.jupiter.engine.execution.ExtensionValuesStore.getOrComputeIfAbsent(ExtensionValuesStore.java:89)
	at org.junit.jupiter.engine.execution.NamespaceAwareStore.getOrComputeIfAbsent(NamespaceAwareStore.java:53)
	at org.testcontainers.junit.jupiter.TestcontainersExtension.lambda$beforeAll$2(TestcontainersExtension.java:56)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at org.testcontainers.junit.jupiter.TestcontainersExtension.beforeAll(TestcontainersExtension.java:55)
	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllCallbacks$10(ClassBasedTestDescriptor.java:381)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeBeforeAllCallbacks(ClassBasedTestDescriptor.java:381)
	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:205)
	at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:80)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:148)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
	at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
	at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
	at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

I'm quite sure the code is correct, as it works with my minikube config. For minikube I can set the env variables exported by minikube docker-env to use minikube as a build environment, which works fine.

Is there a similar solution for Rancher Desktop? Is there any other way to let JUnit Testcontainers use my Rancher Desktop installation?

答案1

得分: 3

这个问题与JUnit无关,而是关于为Rancher Desktop配置Testcontainers。

其他Testcontainers用户报告,只要正确配置(切换到Moby容器运行时),Rancher Desktop即可正常工作,因为它在主机上创建了一个套接字位于/var/run/docker.sock,所以请确保没有设置任何Testcontainers环境变量配置。

英文:

This issue is unrelated to JUnit, it is about configuring Testcontainers for Rancher Desktop.

Other Testcontainers users report, that Rancher Desktop works out of the box once it is configured correctly (switch to Moby container runtime) since it creates a socket on the host at /var/run/docker.sock, so make sure to not have any of the Testcontainers environment variable configs set.

答案2

得分: 1

根据上面的评论提到,请确保在Rancher桌面偏好设置中启用**“管理访问权限”**,否则它将持续失败,并显示Caused by: java.lang.IllegalStateException: Could not find a valid Docker environment.

英文:

As mentioned on the comment above, make sure you enable "Administrative Access" on Rancher Desktop Preferences, otherwise it will keep failing with Caused by: java.lang.IllegalStateException: Could not find a valid Docker environment.

答案3

得分: 0

Using:

  • macOS Ventura 13.4.1
  • Apple M2 Pro
  • Rancher Desktop 1.9.1 - dockerd (moby)
  • TestContainers 1.18.3

Execute:

sudo ln -s $HOME/.rd/docker.sock /var/run/docker.sock

Then should work.

英文:

Using:

  • macOS Ventura 13.4.1
  • Apple M2 Pro
  • Rancher Desktop 1.9.1 - dockerd (moby)
  • TestContainers 1.18.3

Execute:

sudo ln -s $HOME/.rd/docker.sock /var/run/docker.sock

Then should work.

huangapple
  • 本文由 发表于 2023年3月8日 15:27:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定