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 Nov 25, 2021
1 parent 4c7f094 commit 260d5ce
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
6 changes: 3 additions & 3 deletions apis/metal3.io/v1alpha1/baremetalhost_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func validateDNSName(hostaddress string) error {
}

hurl, _ := url.Parse(hostaddress)
host, _, err := net.SplitHostPort(hurl.Host)
host, port, _ := net.SplitHostPort(hurl.Host)

if err != nil {
if port == "" {
host = hurl.Host
}
valid, _ := regexp.MatchString(`^(([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])$`, host)

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

Expand Down
35 changes: 34 additions & 1 deletion apis/metal3.io/v1alpha1/baremetalhost_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,47 @@ func TestValidateCreate(t *testing.T) {
oldBMH: nil,
wantedErr: "hardwareRAIDVolumes and softwareRAIDVolumes can not be set at the same time",
},
{
name: "validDNSName",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://host-0.example.com.org"}}},
oldBMH: nil,
wantedErr: "",
},
{
name: "validDNSName2",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://baremetalhost"}}},
oldBMH: nil,
wantedErr: "",
},
{
name: "invalidDNSName",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://-a10.10.57.19"}}},
Address: "ipmi://-host.example.com.org"}}},
oldBMH: nil,
wantedErr: "Host DNS name is invalid",
},
{
name: "invalidDNSName2",
newBMH: &BareMetalHost{
TypeMeta: tm,
ObjectMeta: om,
Spec: BareMetalHostSpec{
BMC: BMCDetails{
Address: "ipmi://host-.example.com.org"}}},
oldBMH: nil,
wantedErr: "Host DNS name is invalid",
},
Expand Down

0 comments on commit 260d5ce

Please sign in to comment.