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

feat: provide a tool to generate examples from code #618

Merged
merged 49 commits into from Nov 23, 2022

Conversation

mdelapenya
Copy link
Collaborator

@mdelapenya mdelapenya commented Nov 10, 2022

What does this PR do?

It moves the existing examples' code (cockroachDB, nginx, pulsar and redis) to separate example Go modules that will be automatically included into the docs site.

Besides, we have provided a command line tool to generate the scaffolding for the code of a new example. This tool will generate:

  • a Go module for the example, including:
    • go.mod and go.sum files
    • a Go package named after the example, in lowercase
    • a Go file for the creation of the container, using a dedicated struct.
    • a Go test file for running a simple test for your container, consuming the above struct.
    • a Makefile to run the tests in a consistent manner
    • a tools.go file including the build tools (i.e. gotestsum) used to build/run the example.
  • a GH action workflow for the example, which runs the tests for the example to ensure compilation and green tests. It runs for pushes and pull-request events.
  • a markdown file in the docs/examples directory including the snippets for both the creation of the container and a simple test.

This tool is not doing the following:

  • If the example already exists, it will exit without updating the existing files.
  • You have to manually add the markdown entry in the docs to the mkdocs.yml file in the root directory of the project. It will generate the navigation menu for the docs website. Implemented in this PR 🚀

In this PR we are adding a Make goal to run all the examples' tests, in order to check if they are still valid and not outdated. The make goal, which calls each example's Makefile, is used in the CI pipeline for each example.

Why is it important?

Examples embedded into the docs could get outdated and they could not even compile. With this approach, the tests will be part of the test suite.

We expect the code generation tool will simplify how to start an example, creating the scaffolding for it.

Related issues

Follow-up questions

  • Do we want to run the tests for the examples for the two supported versions of Go (current and current-1)?
  • Do we want to run the tests for the examples in separated pipelines? I think so
  • What's the frequency in which we want to run the tests for the examples? Pull Requests, pushes, tags...
  • Do we want to add the examples GH checks to the branch protection settings to make them mandatory?

Not adding value to the example
* main:
  docs: networking basics (#612)
  docs: wording in project name (#610)
  feat: Auth config for build images (#602)
  chore: sync governance files (#608)
@mdelapenya mdelapenya requested a review from a team as a code owner November 10, 2022 22:58
* main:
  docs: update method to `nginxC.Terminate` (#627)
  fix: do not prepend garbage in the container.Exec response (#624)
  chore: retire podman pipeline (#625)
  update gotest.md - fix errors in the example (#623)
  chore: sync governance files (#622)
@@ -47,6 +47,10 @@ jobs:
if: ${{ matrix.platform == 'ubuntu-latest' }}
run: make test-e2e

- name: Run Example tests
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm tempted to have a dedicated pipeline for each example, instead of adding build time to the main one. Wdyt? If that's the case, should the checks be mandatory for the review?

@mdelapenya
Copy link
Collaborator Author

@eddumelendez and I had a chat on this PR, and we are going to merge as is, creating follow-up PRs adding more examples.

@mdelapenya mdelapenya merged commit 84832a6 into testcontainers:main Nov 23, 2022
@mdelapenya mdelapenya deleted the examples-work branch November 23, 2022 16:04
mdelapenya referenced this pull request in mdelapenya/testcontainers-go Nov 29, 2022
* main:
  Add toxiproxy example (testcontainers#643)
  Add spanner example (testcontainers#642)
  chore: sync governance files (testcontainers#641)
  Add pubsub example (testcontainers#640)
  chore: adjust generator for the docs site (testcontainers#639)
  Add datastore example (testcontainers#638)
  Add firestore example (testcontainers#637)
  fix: avoid panics when checking container state and container.raw is nil (testcontainers#635)
  feat: provide a tool to generate examples from code (#618)
  chore: bump version in mkdocs (#630)
  docs: remove code snippets from main README (testcontainers#631)
  docs: document replace directive for Docker Compose (testcontainers#632)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Docs, docs, docs. enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant