Skip to content

Conversation

@0xgouda
Copy link
Collaborator

@0xgouda 0xgouda commented Dec 2, 2025

  • Add a new testutil package that contains helper functions for different tests.
  • Create a new SetupPostgresContainer() function that contains the pg container setup and termination logic.
  • Move RPC tests servers setup, and mock Receiver to testutil/rpc.go.
  • Create a separate sinks/test_main.go file containing the TestMain() for sinks tests.

@0xgouda 0xgouda added enhancement New feature or request test New test case or request sinks Where and how to store monitored data labels Dec 2, 2025
@0xgouda 0xgouda requested a review from pashagolub December 2, 2025 17:07
@coveralls
Copy link

coveralls commented Dec 2, 2025

Pull Request Test Coverage Report for Build 19968467613

Details

  • 83 of 88 (94.32%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.4%) to 73.972%

Changes Missing Coverage Covered Lines Changed/Added Lines %
internal/testutil/setup.go 59 64 92.19%
Totals Coverage Status
Change from base Build 19930713686: 0.4%
Covered Lines: 3777
Relevant Lines: 5106

💛 - Coveralls

@pashagolub
Copy link
Collaborator

Great idea! Was on my todo list for a long time! :-)

Let's go further! We need the same functions in many test packages anyway, not only sinks!

The idea is:

  • add /internal/testutil special package
  • put only xxx_test.go files in it (e.g. docker_test.go, mock_test.go), so we can sure it won't be ever used in production
  • add SetupXXX and TearDownXXX functions, e..g
     	func SetupPostgresDocker(t *testing.T) *Environment {
     		t.Helper()
     		var err error
     		const ImageName = "docker.io/postgres:17-alpine"
     		pgContainer, err = postgres.Run(...)
  • each setup helper will return unique env for test, this way we are safe to run many tests in parallel

@0xgouda
Copy link
Collaborator Author

0xgouda commented Dec 3, 2025

  • each setup helper will return unique env for test, this way we are safe to run many tests in parallel

I am not sure I understand this point.

@0xgouda
Copy link
Collaborator Author

0xgouda commented Dec 3, 2025

  • put only xxx_test.go files in it (e.g. docker_test.go, mock_test.go), so we can sure it won't be ever used in production

This means we will be moving all our *_test.go files to this /interna/testutil dir, right?

@0xgouda 0xgouda self-assigned this Dec 3, 2025
@pashagolub
Copy link
Collaborator

I am not sure I understand this point.

If you setup a docker container, you want to return it to the test, right? If you setup a mock, you want the test to use it. That's what I mean under "return the environment". Every setup function should return test environment to the test case. No global vars or something like that.

@pashagolub
Copy link
Collaborator

This means we will be moving all our *_test.go files to this /interna/testutil dir, right?

No, testutil is exactly what the name says: "utilities to be used by tests".

For example instead of

func TestReadMetricSchemaType(t *testing.T) {
	conn, err := pgxmock.NewPool()
	assert.NoError(t, err)
...
}

We could

func TestReadMetricSchemaType(t *testing.T) {
	conn, tearDown := testutil.SetupMock()
	defer tearDown()
...
}

or in case of the docker:

func TestFoo(t *testing.T) {
	pg, tearDown := testutil.SetupPgContainer()
	defer tearDown()
	connStr, err = pg.ConnectionString(ctx)
	assert.NoError(t, err)
	assert.NoError(t, db.Ping(ctx, connStr))
}

Setup returns everything test case needs to run (environment) and the tearDown() function that test calls at the end to free resources, cleanup temp files, etc.

@0xgouda 0xgouda changed the title [*] move pg container setup logic to TestMain() [+] add new testutil package Dec 4, 2025
@pashagolub
Copy link
Collaborator

One more thing. I don't like helper_vars.go name. It might be just types.go, the same way as in other packages

Copy link
Collaborator

@pashagolub pashagolub left a comment

Choose a reason for hiding this comment

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

+1

@pashagolub pashagolub merged commit 1e7b487 into master Dec 5, 2025
8 checks passed
@pashagolub pashagolub deleted the update-test-main branch December 5, 2025 16:05
@0xgouda 0xgouda mentioned this pull request Dec 5, 2025
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request sinks Where and how to store monitored data test New test case or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants