Skip to content

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

Open
srebhan wants to merge 1 commit intoinfluxdata:masterfrom
srebhan:docker_migrate_moby
Open

test(inputs.docker): Refactor unit-tests#18814
srebhan wants to merge 1 commit 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
@telegraf-tiger
Copy link
Copy Markdown
Contributor

@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.

if _, err := os.Stat(filepath.Join(path, "inspect.json")); err == nil {
buf, err := os.ReadFile(filepath.Join(path, "inspect.json"))
if err != nil {
return nil, fmt.Errorf("reading inspection datafailed: %w", 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.

Suggested change
return nil, fmt.Errorf("reading inspection datafailed: %w", err)
return nil, fmt.Errorf("reading inspection data failed: %w", err)

err := model.Start(&acc)
model.Stop()
require.ErrorContains(t, err, "failed to ping Docker daemon")
require.ErrorContains(t, model.Start(&acc), "failed to ping Docker daemon")
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.

Both TestStartupErrorBehaviorError and TestStartupErrorBehaviorIgnore drop model.Stop(). should we stop the model just to make sure we don't leak anything? TestStartSuccess keeps it

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.

}
}

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.

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