Skip to content

Commit

Permalink
add more testcases, minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hroyrh committed May 27, 2022
1 parent fde03d4 commit 2061aa4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 43 deletions.
24 changes: 2 additions & 22 deletions apis/metal3.io/v1alpha1/baremetalhost_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package v1alpha1
import (
"fmt"
"net"
"net/url"
"regexp"
"strings"

"github.com/google/uuid"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -156,27 +154,9 @@ func validateDNSName(hostaddress string) error {
return nil
}

if !strings.Contains(hostaddress, "://") {
hostaddress = "ipmi://" + hostaddress
}

hurl, err := url.Parse(hostaddress)
_, err := bmc.GetParsedURL(hostaddress)
if err != nil {
return fmt.Errorf("Url parsing error, Host DNS name is invalid")
}

host, port, err := net.SplitHostPort(hurl.Host)

if port == "" {
if !strings.Contains(err.Error(), "missing port in address") {
return fmt.Errorf("Error while splitting host and port, Host DNS name is invalid")
}
host = hurl.Host
}
valid, _ := regexp.MatchString(`^([a-zA-Z]+\:\/\/)?(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])(\:[0-9]+)?$`, hostname)

if !valid {
return fmt.Errorf("Host DNS name is invalid")
return fmt.Errorf("host DNS name is invalid")
}

return nil
Expand Down
87 changes: 66 additions & 21 deletions apis/metal3.io/v1alpha1/baremetalhost_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,41 +104,27 @@ func TestValidateCreate(t *testing.T) {
wantedErr: "hardwareRAIDVolumes and softwareRAIDVolumes can not be set at the same time",
},
{
<<<<<<< HEAD
name: "supportBMCType",
=======
name: "validDNSName",
>>>>>>> 260d5ce5... add more testcases, minor fixes
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
<<<<<<< HEAD
BootMACAddress: "01:02:03:04:05:06",
BMC: BMCDetails{
Address: "irmc:127.0.1.1",
CredentialsName: "test1",
},
}},
=======
BMC: BMCDetails{
Address: "ipmi://host-0.example.com.org"}}},
>>>>>>> 260d5ce5... add more testcases, minor fixes
oldBMH: nil,
wantedErr: "",
},
{
<<<<<<< HEAD
name: "unsupportBMCType",
=======
name: "validDNSName2",
>>>>>>> 260d5ce5... add more testcases, minor fixes
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
<<<<<<< HEAD
Address: "test:127.0.1.1",
CredentialsName: "test1",
},
Expand Down Expand Up @@ -169,18 +155,11 @@ func TestValidateCreate(t *testing.T) {
CredentialsName: "test1",
},
}},
=======
Address: "ipmi://baremetalhost"}}},
>>>>>>> 260d5ce5... add more testcases, minor fixes
oldBMH: nil,
wantedErr: "",
},
{
<<<<<<< HEAD
name: "RAIDWithUnsupportBMC",
=======
name: "invalidDNSName",
>>>>>>> 260d5ce5... add more testcases, minor fixes
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Expand Down Expand Up @@ -370,6 +349,72 @@ func TestValidateCreate(t *testing.T) {
oldBMH: nil,
wantedErr: fmt.Sprintf("the 'numberOfPhysicalDisks'[%d] and number of 'physicalDisks'[2] is not same for volume 0", numberOfPhysicalDisks),
},
{
name: "validDNSName",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://host-0.example.com.org:6223"}}},
oldBMH: nil,
wantedErr: "",
},
{
name: "validDNSName2",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://baremetalhost"}}},
oldBMH: nil,
wantedErr: "",
},
{
name: "validDNSName3",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://[fe80::fc33:62ff:fe83:8a76]:6233"}}},
oldBMH: nil,
wantedErr: "",
},
{
name: "invalidDNSNameinvalidcharacter",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://-host.example.com.org"}}},
oldBMH: nil,
wantedErr: "host DNS name is invalid",
},
{
name: "invalidDNSNameinvalidcharacter2",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://host-.example.com.org"}}},
oldBMH: nil,
wantedErr: "host DNS name is invalid",
},
{
name: "invalidDNSNameinvalidipv6",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://[fe80::fc33:62ff:fe33:8xff]:6223"}}},
oldBMH: nil,
wantedErr: "host DNS name is invalid",
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 2061aa4

Please sign in to comment.