From 52439f61dae8c29df5ec58ad35de2302d610efac Mon Sep 17 00:00:00 2001 From: mdryaan Date: Wed, 13 May 2026 23:20:37 +0530 Subject: [PATCH 1/2] 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 5286ee42..23e2f3b4 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 00000000..b164efe9 --- /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 d02e914e1241139821af06524789475ab0b08516 Mon Sep 17 00:00:00 2001 From: mdryaan Date: Fri, 15 May 2026 01:00:57 +0000 Subject: [PATCH 2/2] 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 23e2f3b4..cd512de3 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 b164efe9..c4ac12a0 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) + }) + } }