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

Add Expected Log validation #351

Open
GabriFedi97 opened this issue May 9, 2023 · 3 comments
Open

Add Expected Log validation #351

GabriFedi97 opened this issue May 9, 2023 · 3 comments

Comments

@GabriFedi97
Copy link

GabriFedi97 commented May 9, 2023

I'd like to use this tool to test if the main process of a docker image can start successfully, without issues. The fact is that the process is meant to not exit, since it has to keep the container alive, and so there is no exit code/output. Looking at the available features I didn't find anything allowing this.

The process logs something like this if starts successfully:

2023-05-09 13:20:48.242 UTC [1] LOG: database system is ready to accept connections

The container-structure-test test process gets stuck if used this way, because it waits for the container to exit which never happens.

It would be cool having an ExpectedLog field to be used along with a timeout. Something like this:

schemaVersion: '2.0.0'
commandTests:
  - name: "Test Postgres starting successfully"
    command: "bash"
    args:
      - -c
      - |
        docker-entrypoint.sh postgres
    envVars:
      - key: POSTGRES_PASSWORD
        value: password
    expectedLog: [".*database system is ready to accept connections.*"]
    timeOut: 20s
@coopernetes
Copy link
Contributor

coopernetes commented May 9, 2023

Logging is typically done through stderr. The expectedError field in the command test should allow you to perform a regex match over the stderr output.

Command tests have to run to completion in order to capture its state of the test container that is created from the image. You will need to invoke your process (psql, it looks like?) in a way that runs to completion. Perhaps you can run the server process as a daemon/background process, get the PID then immediately send a kill -INT signal as part of your test?

@GabriFedi97
Copy link
Author

Thanks! The log can be redirected somehow through the stderr.
However, I don't think I can run the server process in background through this tool. I was wondering if it could be possible leveraging the setup field but I see that the function delegates the process invocation to the ProcessCommand, which waits for the command to exit.

@fparga
Copy link

fparga commented Jun 5, 2023

I've faced the same issue, and I agree that it would be nice to have built-in support for this use case in container-structure-test.

As a workaround, if available in the image, I've used the Linux timeout command to set a maximum duration for the command under test. It's a bit of a hack, but it works.

Something like that should do the trick:

command: "timeout"
args:
  - --preserve-status
  - 3s
  - docker-entrypoint.sh postgres

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

No branches or pull requests

3 participants