Skip to content

api/types/container: refactor to use strings.Cut, DRY, move tests and fix validation#44379

Merged
corhere merged 4 commits intomoby:masterfrom
thaJeztah:container_strings_cut
Dec 30, 2022
Merged

api/types/container: refactor to use strings.Cut, DRY, move tests and fix validation#44379
corhere merged 4 commits intomoby:masterfrom
thaJeztah:container_strings_cut

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

hostconfig: move unit tests to api/types/container

These types were moved to api/types/container in 7ac4232 (#18762), but the unit-tests for them were not moved. This patch moves the unit-tests back together with the types.

api/types/container: rewrite tests to use subtests and asserts

api/types/container: use strings.Cut() and DRY

api/types/container: fix validation for UTSMode, UsernsMode, PidMode

The IPCMode type was added in 497fc88 (#9074), and from that patch, the intent was to allow host (without :), "" (empty, default) or container:<container ID>, but the Valid() function seems to be too relaxed and accepting both :, as well as host:<anything>. No unit-tests were added in that patch, and integration-tests only tested for valid values.

Later on, PidMode, and UTSMode were added in 23feaaa ( #9339 / #10080) and f2e5207 (#12667), both of which were implemented as a straight copy of the IPCMode implementation, copying the same bug.

Finally, commit d4aec5f (#14134) implemented unit-tests for these types, but testing for the wrong behavior of the implementation.

This patch updates the validation to correctly invalidate host[:<anything>] and empty (:) types.

@thaJeztah thaJeztah added status/2-code-review kind/bugfix PR's that fix bugs kind/refactor PR's that refactor, or clean-up code labels Nov 1, 2022
@thaJeztah thaJeztah added this to the v-next milestone Nov 1, 2022
@thaJeztah thaJeztah force-pushed the container_strings_cut branch from 9e80171 to 6d50608 Compare December 27, 2022 12:10
These types were moved to api/types/container in 7ac4232,
but the unit-tests for them were not moved. This patch moves the unit-tests back together
with the types.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah force-pushed the container_strings_cut branch from 6d50608 to e7d4ff3 Compare December 27, 2022 21:31
@thaJeztah
Copy link
Copy Markdown
Member Author

@rumpl @corhere PTAL

Copy link
Copy Markdown
Contributor

@corhere corhere left a comment

Choose a reason for hiding this comment

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

Per Hyrum's Law there is inevitably going to be someone (likely with a buggy script) who is going to be upset that we "broke" them. We should call this fix out in the changelog since it is technically a breaking API change.

Comment on lines +441 to +444
func containerID(val string) (idOrName string, ok bool) {
k, idOrName, ok := strings.Cut(val, ":")
return idOrName, ok && k == "container"
}
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.

A light sprinkling of generics would let you DRY the code a touch more by pushing the string type-cast into the implementation.

Suggested change
func containerID(val string) (idOrName string, ok bool) {
k, idOrName, ok := strings.Cut(val, ":")
return idOrName, ok && k == "container"
}
func containerID[S ~string](val S) (idOrName string, ok bool) {
k, idOrName, ok := strings.Cut(string(val), ":")
return idOrName, ok && k == "container"
}

The compiler is sensible enough to monomorphize the function, too.

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.

Didn't want to go all overboard on DRY 😂 I think I'll keep that for a future exercise;

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.

Fair. I only really looked into this to sate my curiosity.

The IPCMode type was added in 497fc88, and from
that patch, the intent was to allow `host` (without `:`), `""` (empty, default)
or `container:<container ID>`, but the `Valid()` function seems to be too relaxed
and accepting both `:`, as well as `host:<anything>`. No unit-tests were added
in that patch, and integration-tests only tested for valid values.

Later on, `PidMode`, and `UTSMode` were added in 23feaaa
and f2e5207, both of which were implemented as
a straight copy of the `IPCMode` implementation, copying the same bug.

Finally, commit d4aec5f implemented unit-tests
for these types, but testing for the wrong behavior of the implementation.

This patch updates the validation to correctly invalidate `host[:<anything>]`
and empty (`:`) types.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah
Copy link
Copy Markdown
Member Author

Per Hyrum's Law there is inevitably going to be someone (likely with a buggy script) who is going to be upset that we "broke" them. We should call this fix out in the changelog since it is technically a breaking API change.

@corhere I added impact/changelog, and removed the redundant string() casts; PTAL 🤗

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

Labels

impact/changelog kind/bugfix PR's that fix bugs kind/refactor PR's that refactor, or clean-up code status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants