From b77ce78b4d7076b50995538fefee7e78424fabea Mon Sep 17 00:00:00 2001 From: mdryaan Date: Wed, 13 May 2026 23:20:37 +0530 Subject: [PATCH 1/3] fix(unikernels): skip network args in Mewz CommandString when no network Mewz.CommandString() unconditionally formatted ip=%s/%d and gateway=%s even when no network was configured, producing "ip=/0 gateway= " as kernel boot parameters. Mewz does not handle this case and panics. Guard the network args behind an m.Net.Address != "" check, matching the existing pattern in Hermit.CommandString(). Add unit tests covering both the no-network and with-network cases. Signed-off-by: mdryaan Signed-off-by: mdryaan --- pkg/unikontainers/unikernels/mewz.go | 8 +++-- pkg/unikontainers/unikernels/mewz_test.go | 44 +++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 pkg/unikontainers/unikernels/mewz_test.go diff --git a/pkg/unikontainers/unikernels/mewz.go b/pkg/unikontainers/unikernels/mewz.go index 5286ee428..23e2f3b4d 100644 --- a/pkg/unikontainers/unikernels/mewz.go +++ b/pkg/unikontainers/unikernels/mewz.go @@ -36,8 +36,12 @@ type MewzNet struct { } func (m *Mewz) CommandString() (string, error) { - return fmt.Sprintf("ip=%s/%d gateway=%s ", m.Net.Address, m.Net.Mask, - m.Net.Gateway), nil + var parts []string + if m.Net.Address != "" { + parts = append(parts, fmt.Sprintf("ip=%s/%d", m.Net.Address, m.Net.Mask)) + parts = append(parts, fmt.Sprintf("gateway=%s", m.Net.Gateway)) + } + return strings.Join(parts, " "), nil } func (m *Mewz) SupportsBlock() bool { diff --git a/pkg/unikontainers/unikernels/mewz_test.go b/pkg/unikontainers/unikernels/mewz_test.go new file mode 100644 index 000000000..b164efe95 --- /dev/null +++ b/pkg/unikontainers/unikernels/mewz_test.go @@ -0,0 +1,44 @@ +// 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 unikernels + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMewzCommandStringNoNetwork(t *testing.T) { + t.Parallel() + m := &Mewz{} + result, err := m.CommandString() + require.NoError(t, err) + assert.Equal(t, "", result, "CommandString should return empty string when no network is configured") +} + +func TestMewzCommandStringWithNetwork(t *testing.T) { + t.Parallel() + m := &Mewz{ + Net: MewzNet{ + Address: "10.0.0.2", + Mask: 24, + Gateway: "10.0.0.1", + }, + } + result, err := m.CommandString() + require.NoError(t, err) + assert.Equal(t, "ip=10.0.0.2/24 gateway=10.0.0.1", result) +} From 98f56350f6c469934e436bd4ff892fe388d77583 Mon Sep 17 00:00:00 2001 From: mdryaan Date: Fri, 15 May 2026 01:00:57 +0000 Subject: [PATCH 2/3] fix(unikernels): simplify CommandString and use table-driven tests Signed-off-by: mdryaan --- pkg/unikontainers/unikernels/mewz.go | 6 +-- pkg/unikontainers/unikernels/mewz_test.go | 46 +++++++++++++++-------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/pkg/unikontainers/unikernels/mewz.go b/pkg/unikontainers/unikernels/mewz.go index 23e2f3b4d..cd512de36 100644 --- a/pkg/unikontainers/unikernels/mewz.go +++ b/pkg/unikontainers/unikernels/mewz.go @@ -36,12 +36,10 @@ type MewzNet struct { } func (m *Mewz) CommandString() (string, error) { - var parts []string if m.Net.Address != "" { - parts = append(parts, fmt.Sprintf("ip=%s/%d", m.Net.Address, m.Net.Mask)) - parts = append(parts, fmt.Sprintf("gateway=%s", m.Net.Gateway)) + return fmt.Sprintf("ip=%s/%d gateway=%s", m.Net.Address, m.Net.Mask, m.Net.Gateway), nil } - return strings.Join(parts, " "), nil + return "", nil } func (m *Mewz) SupportsBlock() bool { diff --git a/pkg/unikontainers/unikernels/mewz_test.go b/pkg/unikontainers/unikernels/mewz_test.go index b164efe95..c4ac12a07 100644 --- a/pkg/unikontainers/unikernels/mewz_test.go +++ b/pkg/unikontainers/unikernels/mewz_test.go @@ -21,24 +21,38 @@ import ( "github.com/stretchr/testify/require" ) -func TestMewzCommandStringNoNetwork(t *testing.T) { +func TestMewzCommandString(t *testing.T) { t.Parallel() - m := &Mewz{} - result, err := m.CommandString() - require.NoError(t, err) - assert.Equal(t, "", result, "CommandString should return empty string when no network is configured") -} -func TestMewzCommandStringWithNetwork(t *testing.T) { - t.Parallel() - m := &Mewz{ - Net: MewzNet{ - Address: "10.0.0.2", - Mask: 24, - Gateway: "10.0.0.1", + testCases := []struct { + name string + mewz *Mewz + expected string + }{ + { + name: "no network configured", + mewz: &Mewz{}, + expected: "", + }, + { + name: "with network configured", + mewz: &Mewz{ + Net: MewzNet{ + Address: "10.0.0.2", + Mask: 24, + Gateway: "10.0.0.1", + }, + }, + expected: "ip=10.0.0.2/24 gateway=10.0.0.1", }, } - result, err := m.CommandString() - require.NoError(t, err) - assert.Equal(t, "ip=10.0.0.2/24 gateway=10.0.0.1", result) + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + result, err := tc.mewz.CommandString() + require.NoError(t, err) + assert.Equal(t, tc.expected, result) + }) + } } From 02e88b535af7236e3f23d5a7f019aeda9ec853b5 Mon Sep 17 00:00:00 2001 From: Md Raiyan Date: Thu, 21 May 2026 12:08:58 +0000 Subject: [PATCH 3/3] chore(test): add unikernels package to unit test target Signed-off-by: Md Raiyan --- Makefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index dd694c7b0..0f87fa84c 100644 --- a/Makefile +++ b/Makefile @@ -233,7 +233,7 @@ test: unittest e2etest ## unittest Run all unit tests .PHONY: unittest -unittest: test_unikontainers test_metrics test_network test_hypervisors +unittest: test_unikontainers test_metrics test_network test_hypervisors test_unikernels ## e2etest Run all end-to-end tests .PHONY: e2etest @@ -263,6 +263,12 @@ test_hypervisors: @GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./pkg/unikontainers/hypervisors -v @echo " " +## test_unikernels Run unit tests for unikernels package +test_unikernels: + @echo "Unit testing in unikernels" + @GOFLAGS=$(TEST_FLAGS) $(GO) test $(TEST_OPTS) ./pkg/unikontainers/unikernels -v + @echo " " + ## test_nerdctl Run all end-to-end tests with nerdctl .PHONY: test_nerdctl test_nerdctl: