Merge pull request #1720 from anderbubble/netdev-ipcidr-empty

Return "" when NetDev.IpCIDR is empty
This commit is contained in:
Christian Goll
2025-02-10 10:37:11 +01:00
committed by GitHub
6 changed files with 45 additions and 6 deletions

View File

@@ -20,6 +20,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Removed
- Removed partial support for regex searches in node and profile lists. #1635
### Fixed
- Return "" when NetDev.IpCIDR is empty.
## v4.6.0rc2, 2025-02-07

View File

@@ -71,7 +71,7 @@ type Transformer struct{}
func (t Transformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
if typ == reflect.TypeOf(net.IP{}) {
return func(dst, src reflect.Value) error {
if !src.IsValid() || src.IsZero() {
if !src.IsValid() || src.IsNil() {
return nil
}
dst.Set(src)

View File

@@ -412,7 +412,7 @@ Return the ipv4 address and mask in CIDR format. Aimed for the use in
templates.
*/
func (netdev *NetDev) IpCIDR() string {
if netdev.Ipaddr.IsUnspecified() || netdev.Netmask.IsUnspecified() {
if netdev.Ipaddr == nil || netdev.Ipaddr.IsUnspecified() || netdev.Netmask == nil || netdev.Netmask.IsUnspecified() {
return ""
}
ipCIDR := net.IPNet{

View File

@@ -7,6 +7,44 @@ import (
"github.com/stretchr/testify/assert"
)
func Test_IpCIDR(t *testing.T) {
tests := map[string]struct {
ipaddr net.IP
netmask net.IP
cidr string
}{
"nil": {
ipaddr: nil,
netmask: nil,
cidr: "",
},
"ip only": {
ipaddr: net.ParseIP("192.168.1.1"),
netmask: nil,
cidr: "",
},
"netmask only": {
ipaddr: nil,
netmask: net.ParseIP("255.255.255.0"),
cidr: "",
},
"working": {
ipaddr: net.ParseIP("192.168.1.1"),
netmask: net.ParseIP("255.255.255.0"),
cidr: "192.168.1.1/24",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
n := new(NetDev)
n.Ipaddr = tt.ipaddr
n.Netmask = tt.netmask
assert.Equal(t, tt.cidr, n.IpCIDR())
})
}
}
func Test_Empty(t *testing.T) {
var netdev NetDev
var netdevPtr *NetDev

View File

@@ -152,7 +152,6 @@ autoconnect=true
[ethernet]
[ipv4]
method=manual
address=<nil>
route1=192.168.1.0/24,192.168.2.254
[vlan]
interface-name=eth0.902
@@ -176,7 +175,6 @@ autoconnect=true
[ethernet]
[ipv4]
method=manual
address=<nil>
[ipv6]
addr-gen-mode=stable-privacy

View File

@@ -153,7 +153,7 @@ This file is autogenerated by warewulf
</ipv4>
<ipv4:static>
<address>
<local><nil></local>
<local></local>
</address>
<route>
<destination>192.168.1.0/24</destination>
@@ -189,7 +189,7 @@ This file is autogenerated by warewulf
</ipv4>
<ipv4:static>
<address>
<local><nil></local>
<local></local>
</address>
</ipv4:static>
<ipv6>