Skip to content

Commit 9359db2

Browse files
Mysterio-17urunc-bot[bot]
authored andcommitted
test: add unit tests for network, rootfs and vaccel
Add unit tests for network, rootfs and vaccel. PR: #395 Signed-off-by: Mradul Tiwari <mradultiwari1708@gmail.com> Reviewed-by: Irving Mondragón <mirvingr@gmail.com> Reviewed-by: Charalampos Mainas <cmainas@nubificus.co.uk> Approved-by: Charalampos Mainas <cmainas@nubificus.co.uk>
1 parent 8b09f2e commit 9359db2

File tree

5 files changed

+395
-1
lines changed

5 files changed

+395
-1
lines changed

.github/contributors.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ users:
6868
vinayakjeet:
6969
name: vinayakjeet
7070
email: vinayakjeetog@gmail.com
71+
Mysterio-17:
72+
name: Mradul Tiwari
73+
email: mradultiwari1708@gmail.com

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ test: unittest e2etest
231231

232232
## unittest Run all unit tests
233233
.PHONY: unittest
234-
unittest: test_unikontainers test_metrics
234+
unittest: test_unikontainers test_metrics test_network
235235

236236
## e2etest Run all end-to-end tests
237237
.PHONY: e2etest
@@ -249,6 +249,12 @@ test_metrics:
249249
@GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./internal/metrics -v
250250
@echo " "
251251

252+
## test_network Run unit tests for network package
253+
test_network:
254+
@echo "Unit testing in pkg/network"
255+
@GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./pkg/network -v
256+
@echo " "
257+
252258
## test_nerdctl Run all end-to-end tests with nerdctl
253259
.PHONY: test_nerdctl
254260
test_nerdctl:

pkg/network/network_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) 2023-2026, Nubificus LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package network
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
)
22+
23+
func TestNewNetworkManager(t *testing.T) {
24+
tests := []struct {
25+
name string
26+
networkType string
27+
expectedErr bool
28+
}{
29+
{
30+
name: "static network manager",
31+
networkType: "static",
32+
expectedErr: false,
33+
},
34+
{
35+
name: "dynamic network manager",
36+
networkType: "dynamic",
37+
expectedErr: false,
38+
},
39+
{
40+
name: "invalid network type",
41+
networkType: "invalid",
42+
expectedErr: true,
43+
},
44+
}
45+
46+
for _, tt := range tests {
47+
t.Run(tt.name, func(t *testing.T) {
48+
t.Parallel()
49+
got, err := NewNetworkManager(tt.networkType)
50+
if tt.expectedErr {
51+
assert.Error(t, err, "NewNetworkManager() should return an error")
52+
} else {
53+
assert.NoError(t, err, "NewNetworkManager() should not return an error")
54+
assert.NotNil(t, got, "NewNetworkManager() should return a non-nil manager")
55+
}
56+
})
57+
}
58+
}

pkg/unikontainers/rootfs_test.go

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// Copyright (c) 2023-2026, Nubificus LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package unikontainers
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
"github.com/urunc-dev/urunc/pkg/unikontainers/types"
22+
)
23+
24+
func TestNewRootfsResult(t *testing.T) {
25+
expected := types.RootfsParams{
26+
Type: "initrd",
27+
Path: "/path/to/initrd",
28+
MountedPath: "/mnt/rootfs",
29+
MonRootfs: "/run/urunc/mon",
30+
}
31+
32+
got := newRootfsResult("initrd", "/path/to/initrd", "/mnt/rootfs", "/run/urunc/mon")
33+
34+
assert.Equal(t, expected.Type, got.Type, "Type should match")
35+
assert.Equal(t, expected.Path, got.Path, "Path should match")
36+
assert.Equal(t, expected.MountedPath, got.MountedPath, "MountedPath should match")
37+
assert.Equal(t, expected.MonRootfs, got.MonRootfs, "MonRootfs should match")
38+
}
39+
40+
func TestRootfsSelector_TryInitrd(t *testing.T) {
41+
tests := []struct {
42+
name string
43+
annot map[string]string
44+
expectedFound bool
45+
expectedType string
46+
expectedPath string
47+
}{
48+
{
49+
name: "initrd present",
50+
annot: map[string]string{
51+
annotInitrd: "/path/to/initrd.img",
52+
},
53+
expectedFound: true,
54+
expectedType: "initrd",
55+
expectedPath: "/path/to/initrd.img",
56+
},
57+
{
58+
name: "initrd missing",
59+
annot: map[string]string{},
60+
expectedFound: false,
61+
},
62+
{
63+
name: "initrd empty",
64+
annot: map[string]string{
65+
annotInitrd: "",
66+
},
67+
expectedFound: false,
68+
},
69+
}
70+
71+
for _, tt := range tests {
72+
t.Run(tt.name, func(t *testing.T) {
73+
t.Parallel()
74+
rs := &rootfsSelector{
75+
annot: tt.annot,
76+
cntrRootfs: "/container/rootfs",
77+
}
78+
79+
got, found := rs.tryInitrd()
80+
81+
assert.Equal(t, tt.expectedFound, found, "tryInitrd() found mismatch")
82+
83+
if found {
84+
assert.Equal(t, tt.expectedType, got.Type, "tryInitrd() Type mismatch")
85+
assert.Equal(t, tt.expectedPath, got.Path, "tryInitrd() Path mismatch")
86+
}
87+
})
88+
}
89+
}
90+
91+
func TestRootfsSelector_ShouldMountContainerRootfs(t *testing.T) {
92+
tests := []struct {
93+
name string
94+
annot map[string]string
95+
expected bool
96+
}{
97+
{
98+
name: "mount rootfs true",
99+
annot: map[string]string{
100+
annotMountRootfs: "true",
101+
},
102+
expected: true,
103+
},
104+
{
105+
name: "mount rootfs 1",
106+
annot: map[string]string{
107+
annotMountRootfs: "1",
108+
},
109+
expected: true,
110+
},
111+
{
112+
name: "mount rootfs false",
113+
annot: map[string]string{
114+
annotMountRootfs: "false",
115+
},
116+
expected: false,
117+
},
118+
{
119+
name: "mount rootfs 0",
120+
annot: map[string]string{
121+
annotMountRootfs: "0",
122+
},
123+
expected: false,
124+
},
125+
{
126+
name: "mount rootfs missing",
127+
annot: map[string]string{},
128+
expected: false,
129+
},
130+
{
131+
name: "mount rootfs empty",
132+
annot: map[string]string{
133+
annotMountRootfs: "",
134+
},
135+
expected: false,
136+
},
137+
{
138+
name: "mount rootfs invalid",
139+
annot: map[string]string{
140+
annotMountRootfs: "invalid",
141+
},
142+
expected: false,
143+
},
144+
}
145+
146+
for _, tt := range tests {
147+
t.Run(tt.name, func(t *testing.T) {
148+
t.Parallel()
149+
rs := &rootfsSelector{
150+
annot: tt.annot,
151+
}
152+
153+
got := rs.shouldMountContainerRootfs()
154+
assert.Equal(t, tt.expected, got, "shouldMountContainerRootfs() mismatch")
155+
})
156+
}
157+
}

0 commit comments

Comments
 (0)