Skip to content

Commit 0310e51

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> fix(unikernels): simplify CommandString and use table-driven tests PR: #674 Signed-off-by: mdryaan <alikhurshid842001@gmail.com> Reviewed-by: Charalampos Mainas <cmainas@nubificus.co.uk> Approved-by: Charalampos Mainas <cmainas@nubificus.co.uk>
1 parent 5c9b935 commit 0310e51

2 files changed

Lines changed: 62 additions & 2 deletions

File tree

pkg/unikontainers/unikernels/mewz.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ 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+
if m.Net.Address != "" {
40+
return fmt.Sprintf("ip=%s/%d gateway=%s", m.Net.Address, m.Net.Mask, m.Net.Gateway), nil
41+
}
42+
return "", nil
4143
}
4244

4345
func (m *Mewz) SupportsBlock() bool {
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 unikernels
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
"github.com/stretchr/testify/require"
22+
)
23+
24+
func TestMewzCommandString(t *testing.T) {
25+
t.Parallel()
26+
27+
testCases := []struct {
28+
name string
29+
mewz *Mewz
30+
expected string
31+
}{
32+
{
33+
name: "no network configured",
34+
mewz: &Mewz{},
35+
expected: "",
36+
},
37+
{
38+
name: "with network configured",
39+
mewz: &Mewz{
40+
Net: MewzNet{
41+
Address: "10.0.0.2",
42+
Mask: 24,
43+
Gateway: "10.0.0.1",
44+
},
45+
},
46+
expected: "ip=10.0.0.2/24 gateway=10.0.0.1",
47+
},
48+
}
49+
50+
for _, tc := range testCases {
51+
t.Run(tc.name, func(t *testing.T) {
52+
t.Parallel()
53+
result, err := tc.mewz.CommandString()
54+
require.NoError(t, err)
55+
assert.Equal(t, tc.expected, result)
56+
})
57+
}
58+
}

0 commit comments

Comments
 (0)