Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration option to execute docker:stop in case of the failing build #915

Open
ivansenic opened this issue Dec 28, 2017 · 26 comments · Fixed by #1492
Open

Configuration option to execute docker:stop in case of the failing build #915

ivansenic opened this issue Dec 28, 2017 · 26 comments · Fixed by #1492

Comments

@ivansenic
Copy link

Description

We use the plugin the start some docker images in the pre-integration-test phase and stop them in the post-integration-test. This works very well. However, if any of the integration tests fails the build, then the created docker containers remain running. Thus, it would be great if there would be a configuration option to run docker:stop in case the build fails. This way everything would be cleaned up correctly.

This is a feature request, however I am not sure how hard it is to implement this or if it's possible at all.

@rhuss
Copy link
Collaborator

rhuss commented Jan 1, 2018

Are you using the surefire or failsafe plugin for running the tests ? I asks because the failsafe plugin is supposed to call post-integration-test even when the the test fails.

In the docs:

Multiple containers can be managed at once, which can be linked together or share data via volumes. Containers are created and started with the docker:start goal and stopped and destroyed with the docker:stop goal. For integration tests both goals are typically bound to the the pre-integration-test and post-integration-test phase, respectively. It is recommended to use the maven-failsafe-plugin for integration testing in order to stop the docker container even when the tests fail.

@ivansenic
Copy link
Author

We are not using it.. If this solution does work, then you can close the issue.. Thanks @rhuss, I will try this out..

@rhuss
Copy link
Collaborator

rhuss commented Jan 10, 2018

Cool. Let me know if this works for you, so that we can close this issue.

@ivansenic
Copy link
Author

This actually does not work in my case with the failsafe plugin.

I hook the flyway-maven-plugin to the integration-test phase in order to verify flyway migrations against the mysql database version we use. However, if the flyway plugin fails the build, then post-integration-test is not called as expected. Here's my output:

...
[INFO] --- docker-maven-plugin:0.23.0:start (start-mysql) @ xyz ---
[INFO] DOCKER> [mysql:5.5.44] "mysql-flyway-test": Start container 901a6abd4d32
[INFO] DOCKER> [mysql:5.5.44] "mysql-flyway-test": Waiting for ports [3306] directly on container with IP (172.17.0.3).
[INFO] DOCKER> [mysql:5.5.44] "mysql-flyway-test": Waited on tcp port '[/172.17.0.3:3306]' 6545 ms
[INFO] 
[INFO] --- flyway-maven-plugin:3.2.1:migrate (default) @ xyz ---
[INFO] Flyway 3.2.1 by Boxfuse
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 40.937 s
[INFO] Finished at: 2018-01-10T11:45:28+01:00
[INFO] Final Memory: 67M/893M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.flywaydb:flyway-maven-plugin:3.2.1:migrate (default) on project xyz: org.flywaydb.core.api.FlywayException: Unable to obtain Jdbc connection from DataSource (jdbc:mysql://localhostsmth:13306/flyway?createDatabaseIfNotExist=true) for user 'root': Communications link failure

I am not sure if the setup of failsafe is correct, I simply copied the one from the official page:

   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.20.1</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

However, post-integration-test is not called. Any suggestions?

@akg268
Copy link

akg268 commented Oct 15, 2018

+1 the container is not stopping incase of test failures. But I use surefire to run my test.

@stefanwendelmann
Copy link

Same Problem here.

  1. Docker Container starts
  2. Database is Created
  3. flyway:migrate Fails and Build is stopped

Container is still up and running

pom.xml

 <plugin>
        <groupId>io.fabric8</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <version>0.28.0</version>
        <configuration>
          <images>
            <image>
              <name>mcr.microsoft.com/mssql/server</name>
              <alias>mssql</alias>
              <run>
                <env>
                  <ACCEPT_EULA>Y</ACCEPT_EULA>
                  <SA_PASSWORD>${mssql.test.pw}</SA_PASSWORD>
                </env>
                <ports>
                  <port>${mssql.test.port}:1433</port>
                </ports>
                <wait>
                  <log>SQL Server is now ready for client connections</log>
                  <time>20000</time>
                </wait>
              </run>
            </image>
          </images>
        </configuration>
        <executions>
          <execution>
            <id>start</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>start</goal>
            </goals>
          </execution>
          <execution>
            <id>stop</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>stop</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
        
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>sql-maven-plugin</artifactId>
        <version>1.5</version>
        <dependencies>
          <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.0.0.jre8</version> 
          </dependency>
        </dependencies>
        <configuration>
          <driver>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver>
          <url>jdbc:sqlserver://${mssql.test.ip}:${mssql.test.port}</url>
          <username>sa</username>
          <password>${mssql.test.pw}</password>
        </configuration>
        <executions>
          <execution>
            <id>create-db</id>
            <phase>integration-test</phase>
            <goals>
              <goal>execute</goal>
            </goals>
            <configuration>
              <autocommit>true</autocommit>
              <sqlCommand>CREATE DATABASE QuoLoco COLLATE Latin1_General_CS_AS</sqlCommand>
            </configuration>
          </execution>
        </executions>
      </plugin>
      
      <plugin>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-maven-plugin</artifactId>
        <version>5.1.4</version>
        <executions>
          <execution>
            <phase>integration-test</phase>
            <goals>
              <goal>migrate</goal>
            </goals>
            <configuration>
              <url>jdbc:sqlserver://${mssql.test.ip}:${mssql.test.port};databaseName=QuoLoco</url>
              <user>sa</user>
              <password>${mssql.test.pw}</password>
              <serverId>flyway-db-docker-test</serverId>
              <baselineOnMigrate>true</baselineOnMigrate>
              <baselineVersion>0</baselineVersion>
              <table>flyway_schema_history</table>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.0.0.jre8</version> 
          </dependency>
        </dependencies>
      </plugin>
      
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>3.0.0-M2</version>
        <executions>
          <execution>
            <id>integration-test</id>
            <goals>
              <goal>integration-test</goal>
            </goals>
          </execution>
          <execution>
            <id>verify</id>
            <goals>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

@sweetavard
Copy link

Subscribed to this thread as I am facing the same problem.

@andylowry
Copy link

Same situation here, but I'm not running in integration-test phase. I'm running flyway migrate goal followed by jooq generate goal in maven's generate-sources phase. Former creates a temporary database and applies migrations to bring the schema up to date. The latter scans the live database and generates code. Then flyway's clean goal runs in process-sources phase to drop the temp database.

Around this I'm trying to wrap fabric8's start goal in initialization phase and stop in process-sources (following flyway in the pom file).

I'm also left with live containers if there are any failures, e.g. a SQL error in a flyway migration.

@luisdanielmesa
Copy link

I had the same issue, I fixed it by separating the goals integration-test and verify into their own executions. Spent a few hours on this and then I got the hint after RTFM

Maybe it's not a bad idea to stop the container after some time/phase by default.

            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    ...
                </configuration>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                        <configuration>
                            ...
                        </configuration>
                    </execution>
                    <execution>
                        <id>verify</id>
                        <goals>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            ...
                        </configuration>
                    </execution>
                </executions>
            </plugin>

I'm using it to start a server that connects to an external SOAP API.

@mlindhout
Copy link

Same issue here

@balazs-zsoldos
Copy link
Contributor

Would be nice if docker containers that were started by the plugin would stopped in case of a graceful JVM shutdown (implementing a shutdown hook in the plugin). I use this plugin for generate-sources and I have no place to stop the container.

@rohanKanojia
Copy link
Member

@balazs-zsoldos : Would it be possible for you to contribute this to our project?

@balazs-zsoldos
Copy link
Contributor

@rohanKanojia Sure. I will try to have a look in the following days at the source and send a PR.

@balazs-zsoldos
Copy link
Contributor

@rohanKanojia I only found slightly hacky way. See #1492

@rohanKanojia
Copy link
Member

@ivansenic @stefanwendelmann @arunkumargan : Hi guys, @balazs-zsoldos has proposed a fix for this bug #1492 via a configuration flag. Would it be possible for you to review it or maybe try out his fix by building a SNAPSHOT locally? Would appreciate it if you could provide some feedback regarding this 🙏

@rohanKanojia
Copy link
Member

rohanKanojia commented Nov 8, 2021

The latest version v0.38.0 should contain the fix provided by @balazs-zsoldos . Would appreciate if you could try out and provide feedback 🙏

In case it doesn't fix the issue, I can reopen the issue

@alahaouas
Copy link

@rohanKanojia I am able to test the fix because I am on v0.38.1 and I reproduce the issue but you don't explain here or in #1492
how to enable it.
Is this usable via the pom ?

@rohanKanojia
Copy link
Member

rohanKanojia commented Jan 5, 2022

@alahaouas : I think it was enabled by docker.executeStopOnVMShutdown . Would appreciate it if you could try it out and provide feedback.

You can read about in provided stop configuration[0]

[0] http://dmp.fabric8.io/#docker:stop

@alahaouas
Copy link

@rohanKanojia the fix isn't working : the maven build fails before the stop execution is triggered.

[INFO] --- docker-maven-plugin:0.38.1:start (prepare-it-database) @  ---
[INFO] DOCKER> [postgres:14.1] "it-database": Start container 9c7282cd335f
[INFO] DOCKER> Pattern '(?s)database system is ready to accept connections.*database system is ready to accept connections' matched for container 9c7282cd335f
[INFO] DOCKER> [postgres:14.1] "it-database": Waited on log out '(?s)database system is ready to accept connections.*database system is ready to accept connections' 1509 ms
[INFO] 
[INFO] --- flyway-maven-plugin:8.3.0:clean (start) @ --
[INFO] Flyway Community Edition 8.3.0 by Redgate
[INFO] Database: jdbc:postgresql://127.0.0.1:18801/postgres (PostgreSQL 14.1)
[INFO] Successfully dropped pre-schema database level objects (execution time 00:00.000s)
[INFO] Successfully cleaned schema "public" (execution time 00:00.014s)
[INFO] Successfully cleaned schema "public" (execution time 00:00.008s)
[INFO] Successfully dropped post-schema database level objects (execution time 00:00.000s)
[INFO] 
[INFO] --- flyway-maven-plugin:8.3.0:migrate (start) @  ---
[INFO] Flyway Community Edition 8.3.0 by Redgate
[INFO] Database: jdbc:postgresql://127.0.0.1:18801/postgres (PostgreSQL 14.1)
[INFO] Successfully validated 217 migrations (execution time 00:00.197s)
[INFO] Creating Schema History table "public"."flyway_schema_history" ...
[INFO] Current version of schema "public": << Empty Schema >>
[INFO] Migrating schema "public" to version "1 - create mdm code list tables"
[ERROR] Migration of schema "public" to version "1 - create mdm code list tables" failed! Changes successfully rolled back.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE

@alahaouas
Copy link

alahaouas commented Jan 5, 2022

I guess the fix would be for the docker:start configuration.
Or a global setup

@alahaouas
Copy link

To reproduce, couple flyway with the plugin and add a faulty sql file

@alahaouas
Copy link

Your doc says to use maven-failsafe-plugin to achieve a proper cleanup.
How can we do that ?

@rohanKanojia
Copy link
Member

@alahaouas : We have a sample with maven-failsafe-plugin https://github.com/fabric8io/docker-maven-plugin/blob/master/samples/cargo-jolokia/pom.xml#L164 . Could you please review this to see if it fits your use case?

@alahaouas
Copy link

@rohanKanojia I already use maven-failsafe-plugin in my pom. Just using it isn't working as a work-around.
I thought you had some specific configuration to use maven-failsafe-plugin so that it kills the container if the build fails.

@alahaouas
Copy link

@rohanKanojia the fix from #1492 doesn't work for me. Do you have an alternative solution ?

@errodrigues
Copy link

Same here. Can't this issue be reopened?

@rohanKanojia rohanKanojia reopened this Jan 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.