-
Notifications
You must be signed in to change notification settings - Fork 8
fix: validates CONTROL_PLANE_ENDPOINT_IP and NUTANIX_ENDPOINT are distinct #1002
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cbd2591
fix: validates control plane and prism central IP are distinct
manoj-nutanix 3f14b50
fix: do not error out if control plane endpoint is hostname
manoj-nutanix 4314fe9
refactor: renames func to VirtualIPAddress
manoj-nutanix db904f3
refactor: incorporates review comments
manoj-nutanix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: returning an empty struct here instead of
nilkinda smellsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, looks weird at first.. I just followed
net/netippkg semantic, maybeAddrstruct doesn't hold much data so they return empty struct instead of nil everywhere.