|
| 1 | +// Copyright 2024 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package v1alpha1 |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "net/netip" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestParseURL(t *testing.T) { |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + spec NutanixPrismCentralEndpointSpec |
| 19 | + expectedHost string |
| 20 | + expectedPort uint16 |
| 21 | + expectedErr error |
| 22 | + }{ |
| 23 | + { |
| 24 | + name: "Valid URL with port", |
| 25 | + spec: NutanixPrismCentralEndpointSpec{ |
| 26 | + URL: "https://192.168.1.1:9440", |
| 27 | + }, |
| 28 | + expectedHost: "192.168.1.1", |
| 29 | + expectedPort: 9440, |
| 30 | + expectedErr: nil, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "Valid URL without port", |
| 34 | + spec: NutanixPrismCentralEndpointSpec{ |
| 35 | + URL: "https://192.168.1.1", |
| 36 | + }, |
| 37 | + expectedHost: "192.168.1.1", |
| 38 | + expectedPort: 9440, |
| 39 | + expectedErr: nil, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "Invalid URL", |
| 43 | + spec: NutanixPrismCentralEndpointSpec{ |
| 44 | + URL: "invalid-url", |
| 45 | + }, |
| 46 | + expectedHost: "", |
| 47 | + expectedPort: 0, |
| 48 | + expectedErr: fmt.Errorf( |
| 49 | + "error parsing Prism Central URL: parse %q: invalid URI for request", |
| 50 | + "invalid-url", |
| 51 | + ), |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "Invalid port", |
| 55 | + spec: NutanixPrismCentralEndpointSpec{ |
| 56 | + URL: "https://192.168.1.1:invalid-port", |
| 57 | + }, |
| 58 | + expectedHost: "", |
| 59 | + expectedPort: 0, |
| 60 | + expectedErr: fmt.Errorf( |
| 61 | + "error parsing Prism Central URL: parse %q: invalid port %q after host", |
| 62 | + "https://192.168.1.1:invalid-port", |
| 63 | + ":invalid-port", |
| 64 | + ), |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tt := range tests { |
| 69 | + t.Run(tt.name, func(t *testing.T) { |
| 70 | + host, port, err := tt.spec.ParseURL() |
| 71 | + if tt.expectedErr != nil { |
| 72 | + require.EqualError(t, err, tt.expectedErr.Error()) |
| 73 | + } else { |
| 74 | + require.NoError(t, err) |
| 75 | + } |
| 76 | + assert.Equal(t, tt.expectedHost, host) |
| 77 | + assert.Equal(t, tt.expectedPort, port) |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestParseIP(t *testing.T) { |
| 83 | + tests := []struct { |
| 84 | + name string |
| 85 | + spec NutanixPrismCentralEndpointSpec |
| 86 | + expectedIP netip.Addr |
| 87 | + expectedErr error |
| 88 | + }{ |
| 89 | + { |
| 90 | + name: "Valid IP", |
| 91 | + spec: NutanixPrismCentralEndpointSpec{ |
| 92 | + URL: "https://192.168.1.1:9440", |
| 93 | + }, |
| 94 | + expectedIP: netip.MustParseAddr("192.168.1.1"), |
| 95 | + expectedErr: nil, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "Invalid URL", |
| 99 | + spec: NutanixPrismCentralEndpointSpec{ |
| 100 | + URL: "invalid-url", |
| 101 | + }, |
| 102 | + expectedIP: netip.Addr{}, |
| 103 | + expectedErr: fmt.Errorf( |
| 104 | + "error parsing Prism Central URL: parse %q: invalid URI for request", |
| 105 | + "invalid-url", |
| 106 | + ), |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "Invalid IP", |
| 110 | + spec: NutanixPrismCentralEndpointSpec{ |
| 111 | + URL: "https://invalid-ip:9440", |
| 112 | + }, |
| 113 | + expectedIP: netip.Addr{}, |
| 114 | + expectedErr: fmt.Errorf( |
| 115 | + "error parsing Prism Central IP: ParseAddr(%q): unable to parse IP", |
| 116 | + "invalid-ip", |
| 117 | + ), |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + for _, tt := range tests { |
| 122 | + t.Run(tt.name, func(t *testing.T) { |
| 123 | + ip, err := tt.spec.ParseIP() |
| 124 | + if tt.expectedErr != nil { |
| 125 | + require.EqualError(t, err, tt.expectedErr.Error()) |
| 126 | + } else { |
| 127 | + require.NoError(t, err) |
| 128 | + } |
| 129 | + assert.Equal(t, tt.expectedIP, ip) |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
0 commit comments