-
Notifications
You must be signed in to change notification settings - Fork 52
Feat/add unit tests #395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mysterio-17
wants to merge
4
commits into
urunc-dev:main
Choose a base branch
from
Mysterio-17:feat/add-unit-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feat/add unit tests #395
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7661cbe
feat: add netkit network device support
Mysterio-17 217cd44
test: add unit tests for vaccel, network utils, and rootfs selection
Mysterio-17 24d5f9f
chore: remove netkit changes from unit tests PR
Mysterio-17 8e50443
test: refactor unit tests per code review feedback
Mysterio-17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Copyright (c) 2023-2025, Nubificus LTD | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package network | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestGetTapIndex(t *testing.T) { | ||
| // This test just verifies the function doesn't panic | ||
| // The actual count depends on the system's network interfaces | ||
| index, err := getTapIndex() | ||
| assert.NoError(t, err, "getTapIndex() should not return an error") | ||
| assert.GreaterOrEqual(t, index, 0, "Index should be non-negative") | ||
| assert.LessOrEqual(t, index, 255, "Index should not exceed 255") | ||
| } | ||
|
|
||
| func TestNewNetworkManager(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| networkType string | ||
| expectedErr bool | ||
| expectedType string | ||
| }{ | ||
| { | ||
| name: "static network manager", | ||
| networkType: "static", | ||
| expectedErr: false, | ||
| expectedType: "*network.StaticNetwork", | ||
| }, | ||
| { | ||
| name: "dynamic network manager", | ||
| networkType: "dynamic", | ||
| expectedErr: false, | ||
| expectedType: "*network.DynamicNetwork", | ||
| }, | ||
| { | ||
| name: "invalid network type", | ||
| networkType: "invalid", | ||
| expectedErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| got, err := NewNetworkManager(tt.networkType) | ||
| if tt.expectedErr { | ||
| assert.Error(t, err, "NewNetworkManager() should return an error") | ||
| } else { | ||
| assert.NoError(t, err, "NewNetworkManager() should not return an error") | ||
| assert.NotNil(t, got, "NewNetworkManager() should return a non-nil manager") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| // Copyright (c) 2023-2025, Nubificus LTD | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package unikontainers | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/urunc-dev/urunc/pkg/unikontainers/types" | ||
| ) | ||
|
|
||
| func TestNewRootfsResult(t *testing.T) { | ||
| expected := types.RootfsParams{ | ||
| Type: "initrd", | ||
| Path: "/path/to/initrd", | ||
| MountedPath: "/mnt/rootfs", | ||
| MonRootfs: "/run/urunc/mon", | ||
| } | ||
|
|
||
| got := newRootfsResult("initrd", "/path/to/initrd", "/mnt/rootfs", "/run/urunc/mon") | ||
|
|
||
| assert.Equal(t, expected.Type, got.Type, "Type should match") | ||
| assert.Equal(t, expected.Path, got.Path, "Path should match") | ||
| assert.Equal(t, expected.MountedPath, got.MountedPath, "MountedPath should match") | ||
| assert.Equal(t, expected.MonRootfs, got.MonRootfs, "MonRootfs should match") | ||
| } | ||
|
|
||
| func TestRootfsSelector_TryInitrd(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| annot map[string]string | ||
| expectedFound bool | ||
| expectedType string | ||
| expectedPath string | ||
| }{ | ||
| { | ||
| name: "initrd present", | ||
| annot: map[string]string{ | ||
| annotInitrd: "/path/to/initrd.img", | ||
| }, | ||
| expectedFound: true, | ||
| expectedType: "initrd", | ||
| expectedPath: "/path/to/initrd.img", | ||
| }, | ||
| { | ||
| name: "initrd missing", | ||
| annot: map[string]string{}, | ||
| expectedFound: false, | ||
| }, | ||
| { | ||
| name: "initrd empty", | ||
| annot: map[string]string{ | ||
| annotInitrd: "", | ||
| }, | ||
| expectedFound: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| rs := &rootfsSelector{ | ||
| annot: tt.annot, | ||
| cntrRootfs: "/container/rootfs", | ||
| } | ||
|
|
||
| got, found := rs.tryInitrd() | ||
|
|
||
| assert.Equal(t, tt.expectedFound, found, "tryInitrd() found mismatch") | ||
|
|
||
| if found { | ||
| assert.Equal(t, tt.expectedType, got.Type, "tryInitrd() Type mismatch") | ||
| assert.Equal(t, tt.expectedPath, got.Path, "tryInitrd() Path mismatch") | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRootfsSelector_ShouldMountContainerRootfs(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| annot map[string]string | ||
| expected bool | ||
| }{ | ||
| { | ||
| name: "mount rootfs true", | ||
| annot: map[string]string{ | ||
| annotMountRootfs: "true", | ||
| }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "mount rootfs 1", | ||
| annot: map[string]string{ | ||
| annotMountRootfs: "1", | ||
| }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| name: "mount rootfs false", | ||
| annot: map[string]string{ | ||
| annotMountRootfs: "false", | ||
| }, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "mount rootfs 0", | ||
| annot: map[string]string{ | ||
| annotMountRootfs: "0", | ||
| }, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "mount rootfs missing", | ||
| annot: map[string]string{}, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "mount rootfs empty", | ||
| annot: map[string]string{ | ||
| annotMountRootfs: "", | ||
| }, | ||
| expected: false, | ||
| }, | ||
| { | ||
| name: "mount rootfs invalid", | ||
| annot: map[string]string{ | ||
| annotMountRootfs: "invalid", | ||
| }, | ||
| expected: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| rs := &rootfsSelector{ | ||
| annot: tt.annot, | ||
| } | ||
|
|
||
| got := rs.shouldMountContainerRootfs() | ||
| assert.Equal(t, tt.expected, got, "shouldMountContainerRootfs() mismatch") | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.