Skip to content

Run unit tests as part of build flow #6758

Closed

Description

@rhcarvalho

We want to support developers and teams using OpenShift to implement CI/CD workflows.

We want to close gaps and offer seamless transitions from simply running unit tests to more complex scenarios including integration with Jenkins.

This issue is meant to discuss how we add support for running unit tests as part of the build flow.

Requirements:

  • Should require a minimum amount of user input, no boilerplate (think rake test);
  • Should require no changes to source repositories (solutions like .travis.yml files are discarded);
  • Should work the same for Source and Docker builds;
  • If the test execution returns a non-zero exit code, the build will be marked as failed;
  • Logs shall be streamed to the build logs, just like S2I streams logs from the assemble script;
  • It should be possible to install test-only dependencies as part of the test script, and these dependencies will not be committed to the final image.

Related Trello cards:


Assumptions

  • The unit tests will be executed on a fresh container running the Docker image that has been built, right after the last commit and prior to pushing to a registry (think docker run image@sha test command);
  • The working directory is not changed, so the tests will be executed in the default working directory of the image;
  • For this test command to work, assume that source code or whatever other fundamental artifacts are present in the image.
  • Test execution time is not limited, nor is the scope of what the test script can do. In fact it could be abused to do anything a bash script can do. Timeouts, and other resource quota related concerns are taken care by the platform, and the user uses her quota the way she wants.

How existing CI tools work w.r.t. running unit tests

Drone.io

droneio-cmd

  • User enter one or more lines in a text area 👍
    • In drone.io, you need to include all the steps required before the command that run tests (install systems packages, dependencies, etc) 👎
    • In OpenShift, we don't need those extra steps, npm test would be enough 👍
  • It is possible to pass in extra environment variables (but, instead of this, perhaps users could add lines export FOO=bar in their list of commands to run?)
  • The working directory where the command is run is clearly shown in the UI 👍
  • Choosing a language automatically fill in the "build command" with some default value for the given language 👍
  • Commands are run in Bash (mentioned in their intro video, but a written specification could not be found in their official docs)

Circle CI

circleci-cmd-test

  • User enter one or more lines in a text area 👍
  • It is clearly specified that tests will be run with Bash on Ubuntu 12.04 (make it a predictable experience) 👍
  • If user don't provide their own script, Circle CI tries to infer the commands based on the software stack

Travis

https://docs.travis-ci.com/user/customizing-the-build/#Customizing-the-Build-Step

script:
  - bundle exec rake build
  - bundle exec rake builddoc
  • Requires writing a .travis.yml file 👎
  • Allows specifying, in a concise way, multiple scripts that are sequentially executed even if a previous command returns non-zero exit code 👍
  • "Each command in script is processed by a special bash function." (predictable experience) 👍

Activity

  1. rhcarvalho commented on Jan 21, 2016

    @rhcarvalho
    ContributorAuthor

    @bparees @mfojtik @PI-Victor @smarterclayton @liggitt @deads2k

    I'd like to take the API discussion from #6715 to this issue, and also include the @openshift/ui-review team, since to effectively achieve our objective of making it easy to include unit tests as part of a build, UI work will have to be done, as that's what end users will mostly interact with.

  2. deads2k commented on Jan 21, 2016

    @deads2k
    Contributor

    I don't see unit tests as part of a build. I see the build completing, pushing to its image stream, then having that trigger a series of jobs that are created to represent the various tests. Upon successful completion of the tests, the imagestreamtag is annotated with various pass indicators and maybe promoted by re-tagging.

    That flow fits with most systems I've worked with. The build and its output is considered successful regardless of test passes. Then a series of different tests are used to vet the build, adding information until various boundaries are passed that allow promotion to addition test activity.

    You could even use jobs to do the work we have in our test queue, where we have multiple test attempts and all of them have to pass.

  3. bparees commented on Jan 21, 2016

    @bparees
    Contributor

    @deads2k that ship has sailed (during extensive discussions about what this feature/card is for) so please don't derail this discussion. The feature is:
    image has been built and committed but not pushed, I want to run a command against the image to perform some basic unit test/validation. I do not want to have to push the image to a repository and deploy it somewhere and do complicated things.

  4. jwforres commented on Jan 21, 2016

    @jwforres
    Member

    It should be clear from something in the API response for the build that the reason for the build failure was the unit tests step, and not that the build itself failed, preferably not just a block of status details text, I'm thinking a "reason" field? You may want to visually flag these failures differently in the UI.

  5. rhcarvalho commented on Jan 21, 2016

    @rhcarvalho
    ContributorAuthor

    @jwforres 👍 I'd love to see builds that failed unit tests "colored" differently -- there are a few "new" cases to consider:

    • failed to run unit tests (because of configuration errors, typos, etc)
    • unit tests passed
    • unit tests failed
    • unit tests were not run (with a link to setup unit tests for the build config 😄 )
  6. mfojtik commented on Jan 21, 2016

    @mfojtik
    Contributor

    @rhcarvalho are you going to update the build annotation to deliver the message to UI?

  7. bparees commented on Jan 21, 2016

    @bparees
    Contributor

    @rhcarvalho now who's feature bloating.....

  8. jwforres commented on Jan 21, 2016

    @jwforres
    Member

    So we won't limit their execution time, but should we allow them to set a "kill my build unit tests if they take longer than X amount of time". Only reason would be that quota-ed users may not want to block their whole build queue with one borked test run. Obviously they could do this themselves in their test commands, but having a guarantee from the system that they will get killed can be comforting 😄

  9. rhcarvalho commented on Jan 21, 2016

    @rhcarvalho
    ContributorAuthor

    I'm thinking a "reason" field

    Fortunately it already exists, we'd just need to add more possible values:

    Reason StatusReason `json:"reason,omitempty" description:"brief CamelCase string describing a failure, meant for machine parsing and tidy display in the CLI"`

  10. rhcarvalho commented on Jan 21, 2016

    @rhcarvalho
    ContributorAuthor

    @bparees Web Console work are ideas for @jwforres to make we recognized by excellent UX.
    Of couse it doesn't affect the scope of my PR that will introduce the feature.

  11. bparees commented on Jan 21, 2016

    @bparees
    Contributor

    @rhcarvalho i'm not really clear on the point of this issue. If you want to drive the discussion about what the api for defining the test-cmd should be, let's do that in a specific discussion, not one that revisits the entire feature. (The UX discussion is a fine one to have, but that doesn't seem like your most pressing concern for getting the card done, right?)

  12. 11 remaining items

  13. jhadvig commented on Jan 21, 2016

    @jhadvig
    Member
  14. smarterclayton commented on Jan 22, 2016

    @smarterclayton
    Contributor
  15. smarterclayton commented on Jan 22, 2016

    @smarterclayton
    Contributor
  16. rhcarvalho commented on Jan 22, 2016

    @rhcarvalho
    ContributorAuthor

    IIUC what @smarterclayton is saying, I agree with the fact that setting an entrypoint is an antipattern, as I haven't seen a good reason not to document and always use "bash -c" yet.
    My initial inclusion of "Bash" in the name of the field was aimed at helping users identify and understand what they are supposed to write in their templates, and to understand at first sight when they read a template (what will happen more often), without having to RTFM all the time.


    the next thing i'd expect us to need to add would be support for an explicit entrypoint, primarily to handle a scenario like "i'm doing a docker strategy build of a jenkins image. my dockerfile sets the ENTRYPOINT to "java -jar jenkins.jar", but i also want to run a post-build command to check some stuff in the image. which means i need to override the ENTRYPOINT so it doesn't just start jenkins and ignore my test commands".

    @bparees IMHO this wouldn't be a problem if we had a fixed entrypoint. No conditionals, no multiple paths and possibilities to document, less confused users.

    If we can make it work for all images that contain bash, why limit ourselves to images that set the entrypoint to "something convenient"?!

    But even if we don't offer that, they can do what everyone else does and have their ENTRYPOINT be a shell script that, for no arguments, runs "java -jar jenkins.jar" and when invoked with arguments, runs a shell with those arguments.

    I don't think we can/should force users to change the way they build their images / write their Dockerfiles just to satisfy our API to run unit tests.


    Let's not plan to have two parallel way to describe running a command

    @liggitt, the "future" I mentioned in my previous comment/proposal might simply never happen.
    That's my Agile/eXtreme Programming brain at work. Perhaps it's not a good fit here.

    I contrasted runBash with a generic run just to show that one doesn't exclude the other.
    If what everyone gets today is a bash script in Drone.io, Circle CI, Travis, and we're all fine and productive with it, why shall we complicate the matters by offering more choices?

    I want a PaaS that does what I need in a concise and easy to understand way, not one that has millions of features that I don't understand how to use.


    I'm going to update the PR based on the feedback so far. Will be making it look more like ExecAction.

  17. rhcarvalho commented on Jan 22, 2016

    @rhcarvalho
    ContributorAuthor

    the "future" I mentioned in my previous comment/proposal might simply never happen.
    That's my Agile/eXtreme Programming brain at work. Perhaps it's not a good fit here.

    Maybe this was too broad a comment. What I had in mind specifically was YAGNI:
    https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it

  18. mfojtik commented on Jan 22, 2016

    @mfojtik
    Contributor

    Agree with @bparees, the ExecAction structure is fine for the actual use-case. We can copy it to our type ExecPostCommitAction and provide better documentation. We don't have to override entrypoint (but we can add that in future, where there will be a use-case for that) as the entrypoint we use for s2i image is already bash (and upstream builder images will presumably follow that pattern).

  19. rhcarvalho commented on Jan 22, 2016

    @rhcarvalho
    ContributorAuthor

    For Docker builds, I expect the Dockerfile to set entrypoint and/or cmd such that running a container off the image will "run the app": start a database server, web server, etc. Our assumption was that people will not have to change their source repo (with their Dockerfile) in order to use this feature.

    Running a container with user-provided input as Docker's "cmd" and using whatever entrypoint is in the image does not solve this use case IHMO.

  20. bparees commented on Jan 22, 2016

    @bparees
    Contributor

    Running a container with user-provided input as Docker's "cmd" and using whatever entrypoint is in the image does not solve this use case IHMO.

    i'm sticking with "if that becomes a common use case, we will add an entrypoint field to our ExecAction struct".

    This is how I see the scenarios:

    1. image contains a proper entrypoint like our s2i images - cmd[] works great
    2. image contains no entrypoint, runs a CMD instead. - cmd[] works reasonably well (may need to specifying bash -c as part of the cmd, depending what you are doing) because we just ignore their CMD.
    3. image contains an ENTRYPOINT that does bad things (just starts the DB, doesn't look at other args). - we add an entrypoint override field in the future.
  21. smarterclayton commented on Feb 15, 2016

    @smarterclayton
    Contributor

    Sweet, now we just need the CLI for setting it

    On Mon, Feb 15, 2016 at 2:31 PM, OpenShift Bot [email protected]
    wrote:

    Closed #6758 #6758 via #6715
    #6715.

    —
    Reply to this email directly or view it on GitHub
    #6758 (comment).

  22. rhcarvalho commented on Feb 16, 2016

    @rhcarvalho
    ContributorAuthor

    @jwforres FYI this has been merged, would be nice to have in the Web Console as well to make it easy to use.

  23. spadgett commented on Feb 16, 2016

    @spadgett
    Member

    FYI this has been merged, would be nice to have in the Web Console as well to make it easy to use.

    @rhcarvalho A lot of history here and in the PR. Any doc or summary of how it works?

  24. PI-Victor commented on Feb 16, 2016

    @PI-Victor
    Contributor

    @spadgett docs will hopefully be ready today/tomorrow openshift/openshift-docs#1448

  25. rhcarvalho commented on Feb 17, 2016

    @rhcarvalho
    ContributorAuthor

    @spadgett apart from the docs link above, also please look at the screenshots in the original issue description. We want to make sure it's easy to specify and edit the command to be run, and that the working directory is clear. In case of using the script form, we should also make clear that it will be run using /bin/sh -c (so no Bash-only syntax, and the image needs to have the /bin/sh binary).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions