kubekey/pkg/utils/ip_test.go
liujian 348c9b2d15
feat: enhance precheck tasks for image registry and network validation (#2676)
* feat: enhance precheck tasks for image registry and network validation

- Added a task to ensure successful authentication to the image registry.
- Updated existing tasks to provide clearer failure messages for required configurations.
- Improved validation for network interfaces and CIDR configurations, ensuring dual-stack support.
- Enhanced error handling in the resource handler for playbook creation.

Signed-off-by: joyceliu <joyceliu@yunify.com>

* feat: enhance configuration and query handling

- Added `-trimpath` flag to Go build configuration for improved binary paths.
- Updated REST configuration to set QPS and Burst limits for better performance.
- Refactored query handling to use string types for field and value, improving type consistency.
- Enhanced error handling in resource configuration updates and improved parsing of request bodies.

Signed-off-by: joyceliu <joyceliu@yunify.com>

* feat: check inventory when it's changed

Signed-off-by: joyceliu <joyceliu@yunify.com>

* feat: enhance playbook execution and query handling

- Added a new optional query parameter `promise` to the playbook and inventory endpoints, allowing for asynchronous execution control.
- Introduced a new result state `ResultPending` to indicate ongoing operations.
- Refactored the executor function to handle the `promise` parameter, enabling conditional execution of playbooks.
- Improved error handling and logging during playbook execution.

Signed-off-by: joyceliu <joyceliu@yunify.com>

---------

Signed-off-by: joyceliu <joyceliu@yunify.com>
2025-08-04 15:27:22 +08:00

132 lines
3.0 KiB
Go

/*
Copyright 2023 The KubeSphere Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseIP(t *testing.T) {
testcases := []struct {
name string
ipRange string
excepted func() []string
}{
{
name: "parse cidr",
ipRange: "192.168.1.0/30",
excepted: func() []string {
// 192.168.1.1 - 192.168.1.2
return []string{"192.168.1.1", "192.168.1.2"}
},
},
{
name: "parse single host cidr",
ipRange: "10.0.0.1/32",
excepted: func() []string {
return []string{"10.0.0.1"}
},
},
{
name: "parse range",
ipRange: "192.168.1.1-192.168.1.3",
excepted: func() []string {
return []string{"192.168.1.1", "192.168.1.2", "192.168.1.3"}
},
},
{
name: "parse single ip",
ipRange: "8.8.8.8",
excepted: func() []string {
return []string{"8.8.8.8"}
},
},
{
name: "parse ipv6 cidr",
ipRange: "2001:db8::/126",
excepted: func() []string {
return []string{"2001:db8::", "2001:db8::1", "2001:db8::2", "2001:db8::3"}
},
},
{
name: "parse ipv6 range",
ipRange: "2001:db8::1-2001:db8::3",
excepted: func() []string {
return []string{"2001:db8::1", "2001:db8::2", "2001:db8::3"}
},
},
{
name: "parse ipv6 single",
ipRange: "2001:db8::1",
excepted: func() []string {
return []string{"2001:db8::1"}
},
},
{
name: "parse ip with mask",
ipRange: "192.168.1.0/255.255.255.252",
excepted: func() []string {
return []string{"192.168.1.1", "192.168.1.2"}
},
},
{
name: "invalid input",
ipRange: "invalid",
excepted: func() []string {
return []string{"invalid"}
},
},
{
name: "parse large cidr (truncated check)",
ipRange: "192.168.0.0/18",
excepted: func() []string {
// 192.168.0.1 - 192.168.63.254
var ips []string
for i := range 64 {
for j := range 256 {
ips = append(ips, fmt.Sprintf("192.168.%d.%d", i, j))
}
}
return ips[1 : len(ips)-1]
},
},
{
name: "parse large range (truncated check)",
ipRange: "192.168.0.1-192.168.63.254",
excepted: func() []string {
// 192.168.0.1 - 192.168.63.254
var ips []string
for i := range 64 {
for j := range 256 {
ips = append(ips, fmt.Sprintf("192.168.%d.%d", i, j))
}
}
return ips[1 : len(ips)-1]
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.excepted(), ParseIP(tc.ipRange))
})
}
}