Skip to content

test(inputs.docker): Refactor unit-tests#18814

Open
srebhan wants to merge 3 commits intoinfluxdata:masterfrom
srebhan:docker_migrate_moby
Open

test(inputs.docker): Refactor unit-tests#18814
srebhan wants to merge 3 commits intoinfluxdata:masterfrom
srebhan:docker_migrate_moby

Conversation

@srebhan
Copy link
Copy Markdown
Member

@srebhan srebhan commented Apr 28, 2026

Summary

This PR refactor unit-tests to get rid of test-data-definitions in code. Instead the PR introduces JSON files containing the test data. Additionally, the PR moves testing to testutil function wherever possible, unifies naming and test-structure.
Follow-up PR(s) will then move tests into a test-case based form to ease addition of new tests and simplification of test-case.

This PR makes no functional changes!

Checklist

Related issues

@telegraf-tiger telegraf-tiger Bot added area/docker plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins test labels Apr 28, 2026
@srebhan srebhan added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Apr 28, 2026
Copy link
Copy Markdown
Contributor

@skartikey skartikey left a comment

Choose a reason for hiding this comment

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

@srebhan Thanks! Left a couple of review comments.

Comment thread plugins/inputs/docker/docker_test.go Outdated
Comment thread plugins/inputs/docker/docker_test.go
Comment on lines +2151 to +2153
return func(string, *tls.Config) (dockerClient, error) {
return newClientFromData(data, windows), err
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If readContainerData fails, data is nil but newClientFromData unconditionally reads data.stats / data.statsWindows first -> nil deref panic before the error is returned.

Suggested change
return func(string, *tls.Config) (dockerClient, error) {
return newClientFromData(data, windows), err
}
return func(string, *tls.Config) (dockerClient, error) {
if err != nil {
return nil, err
}
return newClientFromData(data, windows), nil
}

In practice the testdata files exist, so this won't fire today, but a missing/renamed fixture would produce a confusing panic instead of a clean error.

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.

Well I would like to keep the panic as this is a coding error, not an error in the plugin code or anything. Reading the data should never fail and I will fix the function in a follow up PR that introduces "test case" based tests.

}
}

require.Equal(t, expected, actual)
require.Subset(t, tt.expected, actual)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The new code uses one-directional require.Subset with inconsistent direction:

  • TestContainerNames: require.Subset(t, tt.expected, actual) -> only catches extra names; passes silently if expected names are missing (e.g. "Match all containers" expects 5 names, but actual = [etcd] would still pass).
  • TestContainerStateFilter: require.Subset(t, actual, tt.expected) -> only catches missing names; passes silently if extra/wrong-state containers leak through.
  • TestContainerLabels (map): fine - preserves the original per-key check.

For the two list cases, please use require.ElementsMatch(t, tt.expected, actual) to restore bidirectional, order-independent equality.

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.

Which is exactly what we want. We want to test if the expected metrics are among the actual metrics which is what the previous tests did. This will all go away with test-case based tests but I wanted to reproduce the previous behavior in those cases.

srebhan and others added 2 commits May 4, 2026 09:08
@srebhan srebhan requested a review from skartikey May 4, 2026 08:31
@telegraf-tiger
Copy link
Copy Markdown
Contributor

telegraf-tiger Bot commented May 4, 2026

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

Labels

area/docker plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants