prosource

테스트 컨테이너 테스트 케이스가 "유효한 도커 환경을 찾을 수 없습니다"로 인해 실패합니다.

probook 2023. 2. 27. 22:28
반응형

테스트 컨테이너 테스트 케이스가 "유효한 도커 환경을 찾을 수 없습니다"로 인해 실패합니다.

저는 테스트 용기를 사용하는 것이 매우 처음입니다.나의 테스트는 다음과 같은 예외로 불합격하고 있다.

Running com.mastercard.example.testcontainers.testcontainersexampple.DemoControllerTest
2020-04-08 14:27:08.441  INFO   --- [           main] o.s.t.c.support.AbstractContextLoader    
: Could not detect default resource locations for test class 
resource found for suffixes {-context.xml, Context.groovy}.
2020-04-08 14:27:08.449  INFO   --- [           main] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [com.mastercard.example.testcontainers.testcontainersexampple.DemoControllerTest]: DemoControllerTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
2020-04-08 14:27:08.611  INFO   --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Found @SpringBootConfiguration com.mastercard.example.testcontainers.testcontainersexampple.TestContainersExampleApplication for test class com.mastercard.example.testcontainers.testcontainersexampple.DemoControllerTest
2020-04-08 14:27:08.701  INFO   --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
2020-04-08 14:27:08.725  INFO   --- [           main] .b.t.c.SpringBootTestContextBootstrapper : Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@117159c0, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@3e27ba32, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@7ef82753, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@3b0fe47a, org.springframework.test.context.support.DirtiesContextTestExecutionListener@202b0582, org.springframework.test.context.transaction.TransactionalTestExecutionListener@235ecd9f, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@1ca3b418, org.springframework.test.context.event.EventPublishingTestExecutionListener@58cbafc2, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@2034b64c, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@75d3a5e0, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@74d1dc36, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@7161d8d1, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@74e28667]
2020-04-08 14:27:08.781 ERROR   --- [           main] o.t.d.DockerClientProviderStrategy       : Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
2020-04-08 14:27:08.782 ERROR   --- [           main] o.t.d.DockerClientProviderStrategy       :     UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (ping failed). Root cause NoSuchFileException (/var/run/docker.sock)
2020-04-08 14:27:08.782 ERROR   --- [           main] o.t.d.DockerClientProviderStrategy       : As no valid configuration was found, execution cannot continue

질문입니다만, 테스트 컨테이너를 사용하려면 도커를 로컬에 설치해야 합니까?그렇다면 Jenkins와 같은 파이프라인에서 어떻게 작동합니까?

Caused by: java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration
at org.testcontainers.dockerclient.DockerClientProviderStrategy.lambda$getFirstValidStrategy$3(DockerClientProviderStrategy.java:158)
at java.util.Optional.orElseThrow(Optional.java:290)
at org.testcontainers.dockerclient.DockerClientProviderStrategy.getFirstValidStrategy(DockerClientProviderStrategy.java:150)
at org.testcontainers.DockerClientFactory.client(DockerClientFactory.java:111)
at org.testcontainers.containers.GenericContainer.<init>(GenericContainer.java:175)
at org.testcontainers.containers.JdbcDatabaseContainer.<init>(JdbcDatabaseContainer.java:36)
at org.testcontainers.containers.PostgreSQLContainer.<init>(PostgreSQLContainer.java:32)
at com.mastercard.example.testcontainers.testcontainersexampple.DemoControllerTest.<clinit>(DemoControllerTest.java:27)
... 25 more

업데이트 로컬에 도커를 설치했지만(테스트를 위해) 이미지를 다운로드할 수 없어 테스트 케이스를 실행할 수 없습니다.회사 네트워크가 문제가 될 수 있습니다.위의 상황에 대한 좋은 해결책을 가지고 있는 사람이 있습니까?

Caused by: com.github.dockerjava.api.exception.DockerClientException: Could not pull image: unexpected EOF
    at com.github.dockerjava.core.command.PullImageResultCallback.checkDockerClientPullSuccessful(PullImageResultCallback.java:96)
    at com.github.dockerjava.core.command.PullImageResultCallback.throwFirstError(PullImageResultCallback.java:111)
    at com.github.dockerjava.core.async.ResultCallbackTemplate.awaitCompletion(ResultCallbackTemplate.java:91)
    at com.github.dockerjava.core.command.PullImageResultCallback.awaitSuccess(PullImageResultCallback.java:124)

이 에러는 루트 권한으로 인해 발생합니다.ubuntu를 사용하는 경우 도커는 컨테이너를 만들 수 있는 권한을 원하고 테스트 컨테이너는 런타임에 이 권한을 요청할 수 없습니다.그래서 도커(도커)를 sudo 없이 운영할 수 있게 만들어야 합니다.

도커 그룹을 만들고 연결된 사용자를 추가하려면 다음 단계를 수행하십시오.

$ sudo groupadd docker
$ sudo gpasswd -a $USER docker
$ sudo service docker restart

Ubuntu 14.04-15.10 을 사용하고 있는 경우는, 순서 3 에서 이것을 시험해 주세요.

$ sudo service docker.io restart

모든 단계를 마친 후 다음과 같이 sudo를 사용하지 않고 터미널에서 도커 명령을 시도합니다.

$ docker ps

권한 오류가 발생할 경우 컴퓨터를 재시작하여 수정해야 합니다.

인터넷을 통한 다른 해답은 통하지 않았습니다.아마도 제 사례가 좀 특이했기 때문일 겁니다.저는 이 답변이 이 이상하고 특별한 시나리오에 있는 다른 사람들이 무엇이 문제인지 알 수 있기를 바랍니다.

그래서 만약:

  1. Linux에 접속하고 있다
  2. IDE를 통해 테스트를 실행하고 있습니다.
  3. platpak, snap 또는 샌드박스를 실행하는 다른 패키지 관리자를 통해 IDE를 설치했습니다.

이 문제는 응용 프로그램의 패키지 관리자로부터 샌드박스를 받는 것입니다./var/run/docker에 소켓이 있는 경우.sock(기본값)이지만 테스트에서는 검출할 수 없습니다.이것이 원인입니다.이 파일은 샌드박스에 존재하지 않고 호스트 파일 시스템에만 존재합니다.패키지 매니저에 따라서는 /var/run이 샌드박스 내에 마운트될 수 있으므로 해당 매뉴얼을 참조하십시오.이 문제를 해결하는 가장 간단한 방법은 샌드박스 외부에 tarball 등을 통해 IDE를 설치하는 것입니다.

다음과 같은 몇 가지 문제가 있을 수 있습니다.

  • 제안대로 불안정한 네트워크 연결
  • 터미널에서 도커허브에 로그인하지 않았습니다.

이 경우 다음 링크에 따라 도커 사용자 그룹에도 사용자를 추가해야 합니다.

  • 또, 만약을 위해서, 테스트 전에 이미지를 끌어당겨 보았습니다.
  • 또한 Test Containers 인스턴스를 초기화할 때 다음과 같이 이미지 이름을 지정해야 했습니다.

여기에 이미지 설명 입력

이 문제는 도커의 Demon이 동작하고 있지 않은 경우에도 발생합니다.이전에 정상적으로 동작했을 경우는, 기동해, 테스트를 다시 실행하면 문제가 해결됩니다.

언급URL : https://stackoverflow.com/questions/61108655/test-container-test-cases-are-failing-due-to-could-not-find-a-valid-docker-envi

반응형