Skip to content

Commit 52439f6

Browse files
committed
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 <alikhurshid4u@gmail.com> Signed-off-by: mdryaan <alikhurshid842001@gmail.com>
1 parent 11e894a commit 52439f6

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

pkg/unikontainers/unikernels/mewz.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ type MewzNet struct {
3636
}
3737

3838
func (m *Mewz) CommandString() (string, error) {
39-
return fmt.Sprintf("ip=%s/%d gateway=%s ", m.Net.Address, m.Net.Mask,
40-
m.Net.Gateway), nil
39+
var parts []string
40+
if m.Net.Address != "" {
41+
parts = append(parts, fmt.Sprintf("ip=%s/%d", m.Net.Address, m.Net.Mask))
42+
parts = append(parts, fmt.Sprintf("gateway=%s", m.Net.Gateway))
43+
}
44+
return strings.Join(parts, " "), nil
4145
}
4246

4347
func (m *Mewz) SupportsBlock() bool {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 unikernels
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
"github.com/stretchr/testify/require"
22+
)
23+
24+
func TestMewzCommandStringNoNetwork(t *testing.T) {
25+
t.Parallel()
26+
m := &Mewz{}
27+
result, err := m.CommandString()
28+
require.NoError(t, err)
29+
assert.Equal(t, "", result, "CommandString should return empty string when no network is configured")
30+
}
31+
32+
func TestMewzCommandStringWithNetwork(t *testing.T) {
33+
t.Parallel()
34+
m := &Mewz{
35+
Net: MewzNet{
36+
Address: "10.0.0.2",
37+
Mask: 24,
38+
Gateway: "10.0.0.1",
39+
},
40+
}
41+
result, err := m.CommandString()
42+
require.NoError(t, err)
43+
assert.Equal(t, "ip=10.0.0.2/24 gateway=10.0.0.1", result)
44+
}

0 commit comments

Comments
 (0)