Return "" when NetDev.IpCIDR is empty
Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
committed by
Christian Goll
parent
ec61ffbacc
commit
87da57d1a2
@@ -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)
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user