-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: validates CONTROL_PLANE_ENDPOINT_IP and NUTANIX_ENDPOINT are dis…
…tinct (#1002) **What problem does this PR solve?**: webhook errors out if NUTANIX_ENDPOINT IP is same as PC IP. It only implements dumb check which compares PC IP with control-plane IP. **Which issue(s) this PR fixes**: Fixes # https://jira.nutanix.com/browse/NCN-102626
- Loading branch information
1 parent
ce6738a
commit 6e96d3c
Showing
6 changed files
with
388 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2023 Nutanix. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package v1alpha1 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestVirtualIPAddress(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
spec ControlPlaneEndpointSpec | ||
expected string | ||
}{ | ||
{ | ||
name: "Virtual IP specified", | ||
spec: ControlPlaneEndpointSpec{ | ||
VirtualIPSpec: &ControlPlaneVirtualIPSpec{ | ||
Configuration: &ControlPlaneVirtualIPConfiguration{ | ||
Address: "192.168.1.1", | ||
}, | ||
}, | ||
Host: "192.168.1.2", | ||
}, | ||
expected: "192.168.1.1", | ||
}, | ||
{ | ||
name: "VirtualIPSpec struct not specified", | ||
spec: ControlPlaneEndpointSpec{ | ||
VirtualIPSpec: nil, | ||
Host: "192.168.1.2", | ||
}, | ||
expected: "192.168.1.2", | ||
}, | ||
{ | ||
name: "ControlPlaneVirtualIPConfiguration struct not specified", | ||
spec: ControlPlaneEndpointSpec{ | ||
VirtualIPSpec: &ControlPlaneVirtualIPSpec{ | ||
Configuration: nil, | ||
}, | ||
Host: "192.168.1.2", | ||
}, | ||
expected: "192.168.1.2", | ||
}, | ||
{ | ||
name: "Virtual IP specified as empty string", | ||
spec: ControlPlaneEndpointSpec{ | ||
VirtualIPSpec: &ControlPlaneVirtualIPSpec{ | ||
Configuration: &ControlPlaneVirtualIPConfiguration{ | ||
Address: "", | ||
}, | ||
}, | ||
Host: "192.168.1.2", | ||
}, | ||
expected: "192.168.1.2", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := tt.spec.VirtualIPAddress() | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2024 Nutanix. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package v1alpha1 | ||
|
||
import ( | ||
"fmt" | ||
"net/netip" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseURL(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
spec NutanixPrismCentralEndpointSpec | ||
expectedHost string | ||
expectedPort uint16 | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "Valid URL with port", | ||
spec: NutanixPrismCentralEndpointSpec{ | ||
URL: "https://192.168.1.1:9440", | ||
}, | ||
expectedHost: "192.168.1.1", | ||
expectedPort: 9440, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "Valid URL without port", | ||
spec: NutanixPrismCentralEndpointSpec{ | ||
URL: "https://192.168.1.1", | ||
}, | ||
expectedHost: "192.168.1.1", | ||
expectedPort: 9440, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "Invalid URL", | ||
spec: NutanixPrismCentralEndpointSpec{ | ||
URL: "invalid-url", | ||
}, | ||
expectedHost: "", | ||
expectedPort: 0, | ||
expectedErr: fmt.Errorf( | ||
"error parsing Prism Central URL: parse %q: invalid URI for request", | ||
"invalid-url", | ||
), | ||
}, | ||
{ | ||
name: "Invalid port", | ||
spec: NutanixPrismCentralEndpointSpec{ | ||
URL: "https://192.168.1.1:invalid-port", | ||
}, | ||
expectedHost: "", | ||
expectedPort: 0, | ||
expectedErr: fmt.Errorf( | ||
"error parsing Prism Central URL: parse %q: invalid port %q after host", | ||
"https://192.168.1.1:invalid-port", | ||
":invalid-port", | ||
), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
host, port, err := tt.spec.ParseURL() | ||
if tt.expectedErr != nil { | ||
require.EqualError(t, err, tt.expectedErr.Error()) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
assert.Equal(t, tt.expectedHost, host) | ||
assert.Equal(t, tt.expectedPort, port) | ||
}) | ||
} | ||
} | ||
|
||
func TestParseIP(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
spec NutanixPrismCentralEndpointSpec | ||
expectedIP netip.Addr | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "Valid IP", | ||
spec: NutanixPrismCentralEndpointSpec{ | ||
URL: "https://192.168.1.1:9440", | ||
}, | ||
expectedIP: netip.MustParseAddr("192.168.1.1"), | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "Invalid URL", | ||
spec: NutanixPrismCentralEndpointSpec{ | ||
URL: "invalid-url", | ||
}, | ||
expectedIP: netip.Addr{}, | ||
expectedErr: fmt.Errorf( | ||
"error parsing Prism Central URL: parse %q: invalid URI for request", | ||
"invalid-url", | ||
), | ||
}, | ||
{ | ||
name: "Invalid IP", | ||
spec: NutanixPrismCentralEndpointSpec{ | ||
URL: "https://invalid-ip:9440", | ||
}, | ||
expectedIP: netip.Addr{}, | ||
expectedErr: fmt.Errorf( | ||
"error parsing Prism Central IP: ParseAddr(%q): unable to parse IP", | ||
"invalid-ip", | ||
), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ip, err := tt.spec.ParseIP() | ||
if tt.expectedErr != nil { | ||
require.EqualError(t, err, tt.expectedErr.Error()) | ||
} else { | ||
require.NoError(t, err) | ||
} | ||
assert.Equal(t, tt.expectedIP, ip) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.