Skip to content

pkg/namespaces: add tests for UsernsMode and NetworkMode#28953

Open
ROKUMATE wants to merge 1 commit into
podman-container-tools:mainfrom
ROKUMATE:test-pkg-namespaces
Open

pkg/namespaces: add tests for UsernsMode and NetworkMode#28953
ROKUMATE wants to merge 1 commit into
podman-container-tools:mainfrom
ROKUMATE:test-pkg-namespaces

Conversation

@ROKUMATE

Copy link
Copy Markdown
Contributor

What

Add unit tests for the UsernsMode and NetworkMode methods in pkg/namespaces.
These parse the --userns / --network mode strings and had no test coverage.

Why

The methods parse user-supplied "mode:options" input (keep-id options, ns:,
container:, pasta, user-defined names, etc.). Tests lock the parsing and
predicate behavior in.

@Honny1 Honny1 left a comment

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.

Thanks, I have some comments.

Comment thread pkg/namespaces/namespaces_test.go Outdated
Comment on lines +25 to +26
// "default" is the default *value* but is not in Valid()'s allowed list.
{mode: "default", isDefault: true, isPrivate: true, valid: false},

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.

Is this bug?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

its a bit of a non-case acc to me the Valid() has no callers and also "default" isnt actually a valid --userns value as the parseUserNamespace dont accept it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Rather than lock in behavior. .. . I have dropped that row

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

now the empty-string "" case still covers the default value

Comment thread pkg/namespaces/namespaces_test.go Outdated
isDefault bool
isPrivate bool
isNS bool
isContainer bool

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.

Could this be done better? New type, for example: modeType

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yupp i had thought of usernsModeTest and the networkModeTest types for that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

just lemme know if you had a different structure in mind regarding that to what i have implemented in that

}

func TestNetworkModeNS(t *testing.T) {
assert.True(t, NetworkMode("ns:/run/netns/x").IsNS())

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.

I check the implementation of IsNS. And the network version would also accept only this string ns. Is this correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

what i fell is that it's inconsistent not sure

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

NetworkMode used HasPrefix(n, "ns") ... so a network named for ex nsproxy was wrongly treated as a namespace and whereas UsernsMode requires ns: .... I tried to fix NetworkMode to require the ns: prefix and added a test specifically for thatt

Add unit tests for the UsernsMode and NetworkMode predicate/parser methods.

While testing, NetworkMode.IsNS matched any value starting with "ns" (so a network named e.g. "nsproxy" was treated as a namespace path), unlike UsernsMode.IsNS which requires the "ns:" prefix. Require the "ns:" prefix in NetworkMode.IsNS too so only a real namespace path matches.

Fixes: podman-container-tools#28952
Signed-off-by: ROKUMATE <rohitkumawat0110@gmail.com>
@ROKUMATE ROKUMATE force-pushed the test-pkg-namespaces branch from 4ac7615 to 1d960ce Compare June 22, 2026 23:29
@packit-as-a-service

Copy link
Copy Markdown

[NON-BLOCKING] Packit jobs failed. @containers/packit-build please check. Everyone else, feel free to ignore.

// IsNS indicates a network namespace passed in by path (ns:<path>)
func (n NetworkMode) IsNS() bool {
return strings.HasPrefix(string(n), nsType)
return strings.HasPrefix(string(n), nsType+":")

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.

I would put this change into a separate commit.

Comment on lines +11 to +19
mode UsernsMode
isHost bool
isKeepID bool
isNoMap bool
isAuto bool
isDefault bool
isPrivate bool
isNS bool
isContainer bool

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.

Nonblocking: Is there a better way to define this testcase struct?

isNS bool
}

func TestNetworkMode(t *testing.T) {

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 networkModeTest could include a userDefined field and an nsPath field to avoid needing a separate test that seems to be a duplicate.


func TestUsernsMode(t *testing.T) {
tests := []usernsModeTest{
{mode: "", isDefault: true, isPrivate: true, valid: true},

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.

This test case has an empty test name. I am not sure if it is a good idea. Maybe some if when it is "" set name (empty string) or something like that?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants