Skip to content

Commit f6ddb03

Browse files
mdryaanurunc-bot[bot]
authored andcommitted
fix(unikernels): replace hardcoded /24 mask in Mirage init
PR: #703 Signed-off-by: Md Raiyan <alikhurshid842001@gmail.com> Reviewed-by: Charalampos Mainas <cmainas@nubificus.co.uk> Approved-by: Charalampos Mainas <cmainas@nubificus.co.uk>
1 parent 21afe92 commit f6ddb03

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

pkg/unikontainers/unikernels/mirage.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ func (m *Mirage) MonitorCli() types.MonitorCliArgs {
9999
func (m *Mirage) Init(data types.UnikernelParams) error {
100100
// if Mask is empty, there is no network support
101101
if data.Net.Mask != "" {
102-
m.Net.Address = "--ipv4=" + data.Net.IP + "/24"
102+
mask, err := subnetMaskToCIDR(data.Net.Mask)
103+
if err != nil {
104+
return err
105+
}
106+
m.Net.Address = fmt.Sprintf("--ipv4=%s/%d", data.Net.IP, mask)
103107
m.Net.Gateway = "--ipv4-gateway=" + data.Net.Gateway
104108
}
105109
m.Block = make([]MirageBlock, 0, len(data.Block))
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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/urunc-dev/urunc/pkg/unikontainers/types"
22+
)
23+
24+
func TestMirageInitSubnetMask(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
mask string
28+
ip string
29+
gateway string
30+
wantAddress string
31+
wantGateway string
32+
}{
33+
{
34+
name: "non-/24 mask is used correctly",
35+
mask: "255.255.255.240",
36+
ip: "10.0.0.1",
37+
gateway: "10.0.0.14",
38+
wantAddress: "--ipv4=10.0.0.1/28",
39+
wantGateway: "--ipv4-gateway=10.0.0.14",
40+
},
41+
{
42+
name: "/24 mask still works",
43+
mask: "255.255.255.0",
44+
ip: "192.168.1.5",
45+
gateway: "192.168.1.1",
46+
wantAddress: "--ipv4=192.168.1.5/24",
47+
wantGateway: "--ipv4-gateway=192.168.1.1",
48+
},
49+
{
50+
name: "no network when mask is empty",
51+
mask: "",
52+
ip: "",
53+
gateway: "",
54+
wantAddress: "",
55+
wantGateway: "",
56+
},
57+
}
58+
59+
for _, tt := range tests {
60+
t.Run(tt.name, func(t *testing.T) {
61+
m := newMirage()
62+
params := types.UnikernelParams{
63+
Net: types.NetDevParams{
64+
IP: tt.ip,
65+
Mask: tt.mask,
66+
Gateway: tt.gateway,
67+
},
68+
}
69+
err := m.Init(params)
70+
assert.NoError(t, err)
71+
assert.Equal(t, tt.wantAddress, m.Net.Address)
72+
assert.Equal(t, tt.wantGateway, m.Net.Gateway)
73+
})
74+
}
75+
}

0 commit comments

Comments
 (0)