Closed amimas closed 6 years ago
I figured out the problem. I didn't have the proper dependency testcontainers-spock
setup on my build script, which should come from jetpack.io.
Is there any reason for not publishing this to maven central? Also, it'd be very useful if you could release using semantic versioning instead of using commit hash
Hi @amimas, there is an open issue for the maven central release (#22). I simply didn't came around integrating our automated release for maven central with this module.
Assuming we would start to provide Git tags for use with jitpack.io (using semver of course :wink:), would you still benefit a lot from a maven central version in this case?
@kiview - Thanks a lot for taking the time to respond to this. I think using git tags of semantic versioning will be useful. That way it's can be much clearer to understand when the time comes to upgrade.
I don't have much experience with jitpack.io. My understanding is that in maven, one cannot overwrite something that has been released already. For me as a consumer, I can be sure that every test/build will be using the same artifact. Does jitpack.io provide that level protection?
@kiview - Unfortunately I'm running into same issue again but this time I'm sure it's not a dependency issue I had before.
This is what my spock test looks script looks like, which I started to play with using the sample tests you have in this project.
package com.groovycoder.spockdockerextension
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.client.methods.CloseableHttpResponse
import org.testcontainers.containers.*
import spock.lang.Shared
import spock.lang.Specification
@Testcontainers
class ComposeContainerIT extends Specification {
// @Shared
DockerComposeContainer composeContainer = new DockerComposeContainer(
new File("src/test/resources/myservices.yml"))
.withEnv("COMPOSE_PROJECT_NAME","project1")
// @Shared
String serviceHost = "localhost"
// @Shared
int servicePort = 9000
def "services should be up and running"() {
given: "a http client"
def client = HttpClientBuilder.create().build()
when: "accessing web server"
def response = client.execute(new HttpGet("http://$serviceHost:$servicePort"))
then: "service is running and returns http status code 200"
response.statusLine.statusCode == 200
}
def "any docker image can be run even if the service is not used"() {
given: "running a web server docker image without connecting to any services"
GenericContainer aContainer = new GenericContainer("httpd:2.4-alpine")
.withExposedPorts(80)
// .waitingFor(Wait.forHttp("/"))
when: "accessing the web server"
def client = HttpClientBuilder.create().build()
String ip = aContainer.containerIpAddress
String port = aContainer.getMappedPort(80)
def response = client.execute(new HttpGet("http://$ip:$port"))
then: "web server returns http status code 200"
response.statusLine.statusCode == 200
}
}
With this setup, the initial services comes up just fine, which is started using docker compose. But, in the 2nd test case, I'm trying to start a web server container using GenericContainer
. That's where I keep getting the same exception:
com.groovycoder.spockdockerextension.ComposeContainerIT > any docker image can be run even if the service is not used FAILED
java.lang.IllegalStateException: Mapped port can only be obtained after the container is started
at org.testcontainers.shaded.com.google.common.base.Preconditions.checkState(Preconditions.java:174)
at org.testcontainers.containers.GenericContainer.getMappedPort(GenericContainer.java:791)
at com.groovycoder.spockdockerextension.ComposeContainerIT.any docker image can be run even if the service is not used(ComposeContainerIT.groovy:93)
I was wondering if this error is coming up because the test tries to access a port before the container finishes start up. That's why I added .waitingFor(Wait.forHttp("/")
to GenericContainer
configuration but that gives this error:
com.groovycoder.spockdockerextension.ComposeContainerIT > any docker image can be run even if the service is not used FAILED
groovy.lang.MissingPropertyException: No such property: Wait for class: com.groovycoder.spockdockerextension.ComposeContainerIT
at com.groovycoder.spockdockerextension.ComposeContainerIT.any docker image can be run even if the service is not used(ComposeContainerIT.groovy:87)
Did I miss importing any package? Also, I found that if I declare new container using GenericContainer
globally (i.e. not inside a test case as I have in the above example), then everything works. Is there any reason why I'm forced to create new container not inside the setup of a particular test function?
Okay, so testcontainers-spock
works as follows:
It will automatically start/stop all containers (and compose-containers), that are defined as field of your Specification
if the @Testcontainers
annotation is present on the test class. If you also use the @Shared
annotation, this means containers won't restart between tests of this class.
Your second test is failing, because you don't start the container using the start()
method. If you define the container as a field, testcontainers-spock
will do this for you, but in this case, you have to do it manually, since you are already in your test-execution phase.
Hope this helps 🙂
Thank you so much for your reply. I will take a look at start() method and see if I can get this to work.
I used the start()
method for the GenericContainer
inside of spock's feature method's setup and that did the trick. The container started successfully. Thanks a lot.
Glad you could get it working 🙂
Hi,
I'm investigating to run integration testing on multiple docker containers/services in my project. I came across testcontainers which seems very interesting.
I cloned this repository and was able to run
gradlew test
, which successfully completes all the spock tests in this repository. I copied your spock test namedComposeContainerIT
to my project. Didn't add any other logic for my own test case yet as I'm trying to just get started. I keep getting the following error message.I've created a sample github repo with this issue. I'm not quite sure what missed. I can bring up the container manually by running
docker-compose
command. But, the spock test seems to be unable to start the container. The exact same test case runs fine on my machine when I run it from this repository. So, it's not an environment issue. Is there anything else that might be causing this error?https://github.com/amimas/testcontainers-spock