Skip to content

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

Closed
mdelapenya wants to merge 43 commits intotestcontainers:mainfrom
mdelapenya:examples
Closed

feat: provide a tool to generate examples from code#603
mdelapenya wants to merge 43 commits intotestcontainers:mainfrom
mdelapenya:examples

Conversation

@mdelapenya
Copy link
Copy Markdown
Member

@mdelapenya mdelapenya commented Nov 2, 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 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.

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.

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.

@mdelapenya mdelapenya requested a review from a team as a code owner November 2, 2022 07:05
@mdelapenya mdelapenya added the documentation Docs, docs, docs. label Nov 2, 2022
@mdelapenya mdelapenya self-assigned this Nov 2, 2022
@mdelapenya mdelapenya changed the title docs: convert examples into executable tests feat: provide a tool to generate examples from code Nov 2, 2022
@mdelapenya
Copy link
Copy Markdown
Member Author

Weird: the tests fail because of a difference of milliseconds in the create date:

38s
Run make test-examples
Running example tests...
make -C examples test
make[1]: Entering directory '/home/runner/work/testcontainers-go/testcontainers-go/examples'
make -C cockroachdb test
make[2]: Entering directory '/home/runner/work/testcontainers-go/testcontainers-go/examples/cockroachdb'
make test-cockroachdb
make[3]: Entering directory '/home/runner/work/testcontainers-go/testcontainers-go/examples/cockroachdb'
Running cockroachdb tests...
go run gotest.tools/gotestsum \
	--format short-verbose \
	--rerun-fails=5 \
	--packages="./..." \
	--junitfile TEST-cockroachdb.xml
go: downloading github.com/jackc/pgx/v4 v4.17.2
go: downloading github.com/jackc/pgconn v1.13.0
go: downloading github.com/jackc/pgtype v1.12.0
go: downloading golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
go: downloading github.com/jackc/chunkreader/v2 v2.0.1
go: downloading github.com/jackc/pgio v1.0.0
go: downloading github.com/jackc/pgpassfile v1.0.0
go: downloading github.com/jackc/pgproto3/v2 v2.3.1
go: downloading github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b
=== RUN   TestIntegrationDBInsertSelect
2022/11/02 11:54:32 github.com/testcontainers/testcontainers-go - Connected to docker: 
  Server Version: 20.10.18+azure-2
  API Version: 1.41
  Operating System: Ubuntu 20.04.5 LTS
  Total Memory: 6944 MB
2022/11/02 11:54:32 Starting container id: 59544397cbd3 image: docker.io/testcontainers/ryuk:0.3.4
2022/11/02 11:54:32 Waiting for container id 59544397cbd3 image: docker.io/testcontainers/ryuk:0.3.4
2022/11/02 11:54:32 Container is ready id: 59544397cbd3 image: docker.io/testcontainers/ryuk:0.3.4
2022/11/02 11:54:37 Starting container id: 1c0657415303 image: cockroachdb/cockroach:latest-v21.1
2022/11/02 11:54:38 Waiting for container id 1c0657415303 image: cockroachdb/cockroach:latest-v21.1
2022/11/02 11:54:38 Container is ready id: 1c0657415303 image: cockroachdb/cockroach:latest-v21.1
    cockroachdb_test.go:99: Saved task is not the same:
          cockroachdb.task{
          	ID:          "1415f232-710c-40af-a415-d119dfb9c3d0",
          	Description: "Update resumé",
          	DateDue:     nil,
        - 	DateCreated: s"2022-11-02 11:54:38.498883824 +0000 UTC m=+6.247304161",
        + 	DateCreated: s"2022-11-02 11:54:38.498883 +0000 UTC",
        - 	DateUpdated: s"2022-11-02 11:54:38.498883824 +0000 UTC m=+6.247304161",
        + 	DateUpdated: s"2022-11-02 11:54:38.498883 +0000 UTC",
          }
--- FAIL: TestIntegrationDBInsertSelect (6.46s)
FAIL TestIntegrationDBInsertSelect (6.46s)
FAIL .

DONE 1 tests, 1 failure in 17.610s

=== RUN   TestIntegrationDBInsertSelect
2022/11/02 11:54:41 github.com/testcontainers/testcontainers-go - Connected to docker: 
  Server Version: 20.10.18+azure-2
  API Version: 1.41
  Operating System: Ubuntu 20.04.5 LTS
  Total Memory: 6944 MB
2022/11/02 11:54:41 Starting container id: fdbcd1bdd8a2 image: docker.io/testcontainers/ryuk:0.3.4
2022/11/02 11:54:41 Waiting for container id fdbcd1bdd8a2 image: docker.io/testcontainers/ryuk:0.3.4
2022/11/02 11:54:41 Container is ready id: fdbcd1bdd8a2 image: docker.io/testcontainers/ryuk:0.3.4
2022/11/02 11:54:42 Starting container id: da1e07d8a1d9 image: cockroachdb/cockroach:latest-v21.1
2022/11/02 11:54:42 Waiting for container id da1e07d8a1d9 image: cockroachdb/cockroach:latest-v21.1
2022/11/02 11:54:42 Container is ready id: da1e07d8a1d9 image: cockroachdb/cockroach:latest-v21.1
    cockroachdb_test.go:99: Saved task is not the same:
          cockroachdb.task{
          	ID:          "d5b11f4a-b1d0-46d4-92c5-c0fdc87c326d",
          	Description: "Update resumé",
          	DateDue:     nil,
        - 	DateCreated: s"2022-11-02 11:54:42.615584654 +0000 UTC m=+1.118725029",
        + 	DateCreated: s"2022-11-02 11:54:42.615584 +0000 UTC",
        - 	DateUpdated: s"2022-11-02 11:54:42.615584654 +0000 UTC m=+1.118725029",
        + 	DateUpdated: s"2022-11-02 11:54:42.615584 +0000 UTC",
          }
--- FAIL: TestIntegrationDBInsertSelect (1.33s)
FAIL TestIntegrationDBInsertSelect (re-run 1) (1.33s)
FAIL .

DONE 2 runs, 2 tests, 2 failures in 21.718s

=== RUN   TestIntegrationDBInsertSelect
2022/11/02 11:54:45 github.com/testcontainers/testcontainers-go - Connected to docker: 
  Server Version: 20.10.18+azure-2
  API Version: 1.41
  Operating System: Ubuntu 20.04.5 LTS
  Total Memory: 6944 MB
2022/11/02 11:54:45 Starting container id: c6366d9a062b image: docker.io/testcontainers/ryuk:0.3.4
2022/11/02 11:54:46 Waiting for container id c6366d9a062b image: docker.io/testcontainers/ryuk:0.3.4
2022/11/02 11:54:46 Container is ready id: c6366d9a062b image: docker.io/testcontainers/ryuk:0.3.4
2022/11/02 11:54:46 Starting container id: 7e8edd34adf2 image: cockroachdb/cockroach:latest-v21.1
2022/11/02 11:54:46 Waiting for container id 7e8edd34adf2 image: cockroachdb/cockroach:latest-v21.1
2022/11/02 11:54:46 Container is ready id: 7e8edd34adf2 image: cockroachdb/cockroach:latest-v21.1
    cockroachdb_test.go:99: Saved task is not the same:
          cockroachdb.task{
          	ID:          "ec01ebba-f8e9-4a58-9246-087cce19e013",
          	Description: "Update resumé",
          	DateDue:     nil,
        - 	DateCreated: s"2022-11-02 11:54:46.737085172 +0000 UTC m=+1.109263215",
        + 	DateCreated: s"2022-11-02 11:54:46.737085 +0000 UTC",
        - 	DateUpdated: s"2022-11-02 11:54:46.737085172 +0000 UTC m=+1.109263215",
        + 	DateUpdated: s"2022-11-02 11:54:46.737085 +0000 UTC",
          }
--- FAIL: TestIntegrationDBInsertSelect (1.31s)

eddumelendez
eddumelendez previously approved these changes Nov 7, 2022
Copy link
Copy Markdown
Member

@eddumelendez eddumelendez left a comment

Choose a reason for hiding this comment

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

nice!

* 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 mentioned this pull request Nov 9, 2022
Comment on lines +30 to +32
wait.ForHTTP("/admin/v2/clusters").WithPort("8080/tcp").WithResponseMatcher(matchAdminResponse).WithStartupTimeout(defaultTimeout),
wait.ForLog("Successfully updated the policies on namespace public/default").WithStartupTimeout(defaultTimeout),
).WithStartupTimeout(defaultTimeout),
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@clive-jevons please take a look at this startup timeout

)

func TestPulsar(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@clive-jevons same here

@mdelapenya mdelapenya added the enhancement New feature or request label Nov 10, 2022
@mdelapenya
Copy link
Copy Markdown
Member Author

Superseded by #618

@mdelapenya mdelapenya closed this Nov 10, 2022
@mdelapenya mdelapenya deleted the examples branch November 23, 2022 16:03
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.

2 participants