Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/contributors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ users:
vinayakjeet:
name: vinayakjeet
email: vinayakjeetog@gmail.com
Mysterio-17:
name: Mradul Tiwari
email: mradultiwari1708@gmail.com
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ test: unittest e2etest

## unittest Run all unit tests
.PHONY: unittest
unittest: test_unikontainers test_metrics
unittest: test_unikontainers test_metrics test_network

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

## test_network Run unit tests for network package
test_network:
@echo "Unit testing in pkg/network"
@GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./pkg/network -v
@echo " "

## test_nerdctl Run all end-to-end tests with nerdctl
.PHONY: test_nerdctl
test_nerdctl:
Expand Down
58 changes: 58 additions & 0 deletions pkg/network/network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2023-2026, 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 TestNewNetworkManager(t *testing.T) {
tests := []struct {
name string
networkType string
expectedErr bool
}{
{
name: "static network manager",
networkType: "static",
expectedErr: false,
},
{
name: "dynamic network manager",
networkType: "dynamic",
expectedErr: false,
},
{
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")
}
})
}
}
157 changes: 157 additions & 0 deletions pkg/unikontainers/rootfs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Copyright (c) 2023-2026, 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")
})
}
}
Loading
Loading