mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-11-28 03:47:34 +00:00
Remove Linux and Windows Kube-proxy Userspace mode
This commit is contained in:
@@ -1,8 +0,0 @@
|
||||
# See the OWNERS docs at https://go.k8s.io/owners
|
||||
|
||||
reviewers:
|
||||
- sig-network-reviewers
|
||||
approvers:
|
||||
- sig-network-approvers
|
||||
labels:
|
||||
- sig/network
|
||||
@@ -1,18 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes 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 netsh provides an interface and implementations for running Windows netsh commands.
|
||||
package netsh // import "k8s.io/kubernetes/pkg/util/netsh"
|
||||
@@ -1,209 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes 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 netsh
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"k8s.io/klog/v2"
|
||||
utilexec "k8s.io/utils/exec"
|
||||
)
|
||||
|
||||
// Interface is an injectable interface for running netsh commands. Implementations must be goroutine-safe.
|
||||
type Interface interface {
|
||||
// EnsurePortProxyRule checks if the specified redirect exists, if not creates it
|
||||
EnsurePortProxyRule(args []string) (bool, error)
|
||||
// DeletePortProxyRule deletes the specified portproxy rule. If the rule did not exist, return error.
|
||||
DeletePortProxyRule(args []string) error
|
||||
// EnsureIPAddress checks if the specified IP Address is added to vEthernet (HNSTransparent) interface, if not, add it. If the address existed, return true.
|
||||
EnsureIPAddress(args []string, ip net.IP) (bool, error)
|
||||
// DeleteIPAddress checks if the specified IP address is present and, if so, deletes it.
|
||||
DeleteIPAddress(args []string) error
|
||||
// Restore runs `netsh exec` to restore portproxy or addresses using a file.
|
||||
// TODO Check if this is required, most likely not
|
||||
Restore(args []string) error
|
||||
|
||||
// GetInterfaceToAddIP returns the interface name where Service IP needs to be added
|
||||
// IP Address needs to be added for netsh portproxy to redirect traffic
|
||||
// Reads Environment variable INTERFACE_TO_ADD_SERVICE_IP, if it is not defined then "vEthernet (HNSTransparent)" is returned
|
||||
GetInterfaceToAddIP() string
|
||||
}
|
||||
|
||||
const (
|
||||
cmdNetsh string = "netsh"
|
||||
)
|
||||
|
||||
// runner implements Interface in terms of exec("netsh").
|
||||
type runner struct {
|
||||
exec utilexec.Interface
|
||||
}
|
||||
|
||||
// New returns a new Interface which will exec netsh.
|
||||
func New(exec utilexec.Interface) Interface {
|
||||
runner := &runner{
|
||||
exec: exec,
|
||||
}
|
||||
return runner
|
||||
}
|
||||
|
||||
// EnsurePortProxyRule checks if the specified redirect exists, if not creates it.
|
||||
func (runner *runner) EnsurePortProxyRule(args []string) (bool, error) {
|
||||
klog.V(4).InfoS("Running netsh interface portproxy add v4tov4", "arguments", args)
|
||||
out, err := runner.exec.Command(cmdNetsh, args...).CombinedOutput()
|
||||
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if ee, ok := err.(utilexec.ExitError); ok {
|
||||
// netsh uses exit(0) to indicate a success of the operation,
|
||||
// as compared to a malformed commandline, for example.
|
||||
if ee.Exited() && ee.ExitStatus() != 0 {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return false, fmt.Errorf("error checking portproxy rule: %v: %s", err, out)
|
||||
|
||||
}
|
||||
|
||||
// DeletePortProxyRule deletes the specified portproxy rule. If the rule did not exist, return error.
|
||||
func (runner *runner) DeletePortProxyRule(args []string) error {
|
||||
klog.V(4).InfoS("Running netsh interface portproxy delete v4tov4", "arguments", args)
|
||||
out, err := runner.exec.Command(cmdNetsh, args...).CombinedOutput()
|
||||
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if ee, ok := err.(utilexec.ExitError); ok {
|
||||
// netsh uses exit(0) to indicate a success of the operation,
|
||||
// as compared to a malformed commandline, for example.
|
||||
if ee.Exited() && ee.ExitStatus() == 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("error deleting portproxy rule: %v: %s", err, out)
|
||||
}
|
||||
|
||||
// EnsureIPAddress checks if the specified IP Address is added to interface identified by Environment variable INTERFACE_TO_ADD_SERVICE_IP, if not, add it. If the address existed, return true.
|
||||
func (runner *runner) EnsureIPAddress(args []string, ip net.IP) (bool, error) {
|
||||
// Check if the ip address exists
|
||||
intName := runner.GetInterfaceToAddIP()
|
||||
argsShowAddress := []string{
|
||||
"interface", "ipv4", "show", "address",
|
||||
"name=" + intName,
|
||||
}
|
||||
|
||||
ipToCheck := ip.String()
|
||||
|
||||
exists, _ := checkIPExists(ipToCheck, argsShowAddress, runner)
|
||||
if exists {
|
||||
klog.V(4).InfoS("Not adding IP address, as it already exists", "IP", ipToCheck)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// IP Address is not already added, add it now
|
||||
klog.V(4).InfoS("Running netsh interface IPv4 add address", "IP", args)
|
||||
out, err := runner.exec.Command(cmdNetsh, args...).CombinedOutput()
|
||||
|
||||
if err == nil {
|
||||
// Once the IP Address is added, it takes a bit to initialize and show up when querying for it
|
||||
// Query all the IP addresses and see if the one we added is present
|
||||
// PS: We are using netsh interface IPv4 show address here to query all the IP addresses, instead of
|
||||
// querying net.InterfaceAddrs() as it returns the IP address as soon as it is added even though it is uninitialized
|
||||
klog.V(3).InfoS("Waiting until IP is added to the network adapter", "IP", ipToCheck)
|
||||
for {
|
||||
if exists, _ := checkIPExists(ipToCheck, argsShowAddress, runner); exists {
|
||||
return true, nil
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
if ee, ok := err.(utilexec.ExitError); ok {
|
||||
// netsh uses exit(0) to indicate a success of the operation,
|
||||
// as compared to a malformed commandline, for example.
|
||||
if ee.Exited() && ee.ExitStatus() != 0 {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
return false, fmt.Errorf("error adding IPv4 address: %v: %s", err, out)
|
||||
}
|
||||
|
||||
// DeleteIPAddress checks if the specified IP address is present and, if so, deletes it.
|
||||
func (runner *runner) DeleteIPAddress(args []string) error {
|
||||
klog.V(4).InfoS("Running netsh interface IPv4 delete address", "IP", args)
|
||||
out, err := runner.exec.Command(cmdNetsh, args...).CombinedOutput()
|
||||
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if ee, ok := err.(utilexec.ExitError); ok {
|
||||
// netsh uses exit(0) to indicate a success of the operation,
|
||||
// as compared to a malformed commandline, for example.
|
||||
if ee.Exited() && ee.ExitStatus() == 0 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("error deleting IPv4 address: %v: %s", err, out)
|
||||
}
|
||||
|
||||
// GetInterfaceToAddIP returns the interface name where Service IP needs to be added
|
||||
// IP Address needs to be added for netsh portproxy to redirect traffic
|
||||
// Reads Environment variable INTERFACE_TO_ADD_SERVICE_IP, if it is not defined then "vEthernet (HNS Internal NIC)" is returned
|
||||
func (runner *runner) GetInterfaceToAddIP() string {
|
||||
if iface := os.Getenv("INTERFACE_TO_ADD_SERVICE_IP"); len(iface) > 0 {
|
||||
return iface
|
||||
}
|
||||
return "vEthernet (HNS Internal NIC)"
|
||||
}
|
||||
|
||||
// Restore is part of Interface.
|
||||
func (runner *runner) Restore(args []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkIPExists checks if an IP address exists in 'netsh interface IPv4 show address' output
|
||||
func checkIPExists(ipToCheck string, args []string, runner *runner) (bool, error) {
|
||||
ipAddress, err := runner.exec.Command(cmdNetsh, args...).CombinedOutput()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
ipAddressString := string(ipAddress[:])
|
||||
klog.V(3).InfoS("Searching for IP in IP dump", "IP", ipToCheck, "IPDump", ipAddressString)
|
||||
showAddressArray := strings.Split(ipAddressString, "\n")
|
||||
for _, showAddress := range showAddressArray {
|
||||
if strings.Contains(showAddress, "IP") {
|
||||
ipFromNetsh := getIP(showAddress)
|
||||
if ipFromNetsh == ipToCheck {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// getIP gets ip from showAddress (e.g. "IP Address: 10.96.0.4").
|
||||
func getIP(showAddress string) string {
|
||||
list := strings.SplitN(showAddress, ":", 2)
|
||||
if len(list) != 2 {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(list[1])
|
||||
}
|
||||
@@ -1,483 +0,0 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes 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 netsh
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"k8s.io/utils/exec"
|
||||
fakeexec "k8s.io/utils/exec/testing"
|
||||
|
||||
"errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func fakeCommonRunner() *runner {
|
||||
fakeCmd := fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
// Success
|
||||
func() ([]byte, []byte, error) {
|
||||
return []byte{}, nil, nil
|
||||
},
|
||||
// utilexec.ExitError exists, and status is not 0
|
||||
func() ([]byte, []byte, error) {
|
||||
return nil, nil, &fakeexec.FakeExitError{Status: 1}
|
||||
},
|
||||
// utilexec.ExitError exists, and status is 0
|
||||
func() ([]byte, []byte, error) {
|
||||
return nil, nil, &fakeexec.FakeExitError{Status: 0}
|
||||
},
|
||||
// other error exists
|
||||
func() ([]byte, []byte, error) {
|
||||
return nil, nil, errors.New("not ExitError")
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return &runner{
|
||||
exec: &fakeexec.FakeExec{
|
||||
CommandScript: []fakeexec.FakeCommandAction{
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeCmd, cmd, args...)
|
||||
},
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeCmd, cmd, args...)
|
||||
},
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeCmd, cmd, args...)
|
||||
},
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeCmd, cmd, args...)
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsurePortProxyRule(t *testing.T) {
|
||||
runner := fakeCommonRunner()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
arguments []string
|
||||
expectedResult bool
|
||||
expectedError bool
|
||||
}{
|
||||
{"Success", []string{"ensure-port-proxy-rule"}, true, false},
|
||||
{"utilexec.ExitError exists, and status is not 0", []string{"ensure-port-proxy-rule"}, false, false},
|
||||
{"utilexec.ExitError exists, and status is 0", []string{"ensure-port-proxy-rule"}, false, true},
|
||||
{"other error exists", []string{"ensure-port-proxy-rule"}, false, true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result, err := runner.EnsurePortProxyRule(test.arguments)
|
||||
if test.expectedError {
|
||||
assert.Errorf(t, err, "Failed to test: %s", test.name)
|
||||
} else {
|
||||
if err != nil {
|
||||
assert.NoErrorf(t, err, "Failed to test: %s", test.name)
|
||||
} else {
|
||||
assert.EqualValuesf(t, test.expectedResult, result, "Failed to test: %s", test.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestDeletePortProxyRule(t *testing.T) {
|
||||
runner := fakeCommonRunner()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
arguments []string
|
||||
expectedError bool
|
||||
}{
|
||||
{"Success", []string{"delete-port-proxy-rule"}, false},
|
||||
{"utilexec.ExitError exists, and status is not 0", []string{"delete-port-proxy-rule"}, true},
|
||||
{"utilexec.ExitError exists, and status is 0", []string{"delete-port-proxy-rule"}, false},
|
||||
{"other error exists", []string{"delete-port-proxy-rule"}, true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
err := runner.DeletePortProxyRule(test.arguments)
|
||||
if test.expectedError {
|
||||
assert.Errorf(t, err, "Failed to test: %s", test.name)
|
||||
} else {
|
||||
assert.NoErrorf(t, err, "Failed to test: %s", test.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureIPAddress(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
arguments []string
|
||||
ip net.IP
|
||||
fakeCmdAction []fakeexec.FakeCommandAction
|
||||
expectedError bool
|
||||
expectedResult bool
|
||||
}{
|
||||
{
|
||||
"IP address exists",
|
||||
[]string{"delete-port-proxy-rule"},
|
||||
net.IPv4(10, 10, 10, 20),
|
||||
[]fakeexec.FakeCommandAction{
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
// IP address exists
|
||||
func() ([]byte, []byte, error) {
|
||||
return []byte("IP Address:10.10.10.10\nIP Address:10.10.10.20"), nil, nil
|
||||
},
|
||||
},
|
||||
}, cmd, args...)
|
||||
},
|
||||
},
|
||||
false,
|
||||
true,
|
||||
},
|
||||
|
||||
{
|
||||
"IP address not exists, but set successful(find it in the second time)",
|
||||
[]string{"ensure-ip-address"},
|
||||
net.IPv4(10, 10, 10, 20),
|
||||
[]fakeexec.FakeCommandAction{
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
// IP address not exists
|
||||
func() ([]byte, []byte, error) {
|
||||
return []byte("IP Address:10.10.10.10"), nil, nil
|
||||
},
|
||||
},
|
||||
}, cmd, args...)
|
||||
},
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
// Success to set ip
|
||||
func() ([]byte, []byte, error) {
|
||||
return []byte(""), nil, nil
|
||||
},
|
||||
},
|
||||
}, cmd, args...)
|
||||
},
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
// IP address still not exists
|
||||
func() ([]byte, []byte, error) {
|
||||
return []byte("IP Address:10.10.10.10"), nil, nil
|
||||
},
|
||||
},
|
||||
}, cmd, args...)
|
||||
},
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
// IP address exists
|
||||
func() ([]byte, []byte, error) {
|
||||
return []byte("IP Address:10.10.10.10\nIP Address:10.10.10.20"), nil, nil
|
||||
},
|
||||
},
|
||||
}, cmd, args...)
|
||||
},
|
||||
},
|
||||
false,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"IP address not exists, utilexec.ExitError exists, but status is not 0)",
|
||||
[]string{"ensure-ip-address"},
|
||||
net.IPv4(10, 10, 10, 20),
|
||||
[]fakeexec.FakeCommandAction{
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
// IP address not exists
|
||||
func() ([]byte, []byte, error) {
|
||||
return []byte("IP Address:10.10.10.10"), nil, nil
|
||||
},
|
||||
},
|
||||
}, cmd, args...)
|
||||
},
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
// Failed to set ip, utilexec.ExitError exists, and status is not 0
|
||||
func() ([]byte, []byte, error) {
|
||||
return nil, nil, &fakeexec.FakeExitError{Status: 1}
|
||||
},
|
||||
},
|
||||
}, cmd, args...)
|
||||
},
|
||||
},
|
||||
false,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"IP address not exists, utilexec.ExitError exists, and status is 0)",
|
||||
[]string{"ensure-ip-address"},
|
||||
net.IPv4(10, 10, 10, 20),
|
||||
[]fakeexec.FakeCommandAction{
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
// IP address not exists
|
||||
func() ([]byte, []byte, error) {
|
||||
return []byte("IP Address:10.10.10.10"), nil, nil
|
||||
},
|
||||
},
|
||||
}, cmd, args...)
|
||||
},
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
// Failed to set ip, utilexec.ExitError exists, and status is 0
|
||||
func() ([]byte, []byte, error) {
|
||||
return nil, nil, &fakeexec.FakeExitError{Status: 0}
|
||||
},
|
||||
},
|
||||
}, cmd, args...)
|
||||
},
|
||||
},
|
||||
true,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"IP address not exists, and error is not utilexec.ExitError)",
|
||||
[]string{"ensure-ip-address"},
|
||||
net.IPv4(10, 10, 10, 20),
|
||||
[]fakeexec.FakeCommandAction{
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
// IP address not exists
|
||||
func() ([]byte, []byte, error) {
|
||||
return []byte("IP Address:10.10.10.10"), nil, nil
|
||||
},
|
||||
},
|
||||
}, cmd, args...)
|
||||
},
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
// Failed to set ip, other error exists
|
||||
func() ([]byte, []byte, error) {
|
||||
return nil, nil, errors.New("not ExitError")
|
||||
},
|
||||
},
|
||||
}, cmd, args...)
|
||||
},
|
||||
},
|
||||
true,
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
runner := New(&fakeexec.FakeExec{CommandScript: test.fakeCmdAction})
|
||||
result, err := runner.EnsureIPAddress(test.arguments, test.ip)
|
||||
if test.expectedError {
|
||||
assert.Errorf(t, err, "Failed to test: %s", test.name)
|
||||
} else {
|
||||
if err != nil {
|
||||
assert.NoErrorf(t, err, "Failed to test: %s", test.name)
|
||||
} else {
|
||||
assert.EqualValuesf(t, test.expectedResult, result, "Failed to test: %s", test.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteIPAddress(t *testing.T) {
|
||||
runner := fakeCommonRunner()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
arguments []string
|
||||
expectedError bool
|
||||
}{
|
||||
{"Success", []string{"delete-ip-address"}, false},
|
||||
{"utilexec.ExitError exists, and status is not 0", []string{"delete-ip-address"}, true},
|
||||
{"utilexec.ExitError exists, and status is 0", []string{"delete-ip-address"}, false},
|
||||
{"other error exists", []string{"delete-ip-address"}, true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
err := runner.DeleteIPAddress(test.arguments)
|
||||
if test.expectedError {
|
||||
assert.Errorf(t, err, "Failed to test: %s", test.name)
|
||||
} else {
|
||||
assert.NoErrorf(t, err, "Failed to test: %s", test.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetInterfaceToAddIP(t *testing.T) {
|
||||
// backup env 'INTERFACE_TO_ADD_SERVICE_IP'
|
||||
backupValue := os.Getenv("INTERFACE_TO_ADD_SERVICE_IP")
|
||||
// recover env
|
||||
defer os.Setenv("INTERFACE_TO_ADD_SERVICE_IP", backupValue)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
envToBeSet string
|
||||
expectedResult string
|
||||
}{
|
||||
{"env_value_is_empty", "", "vEthernet (HNS Internal NIC)"},
|
||||
{"env_value_is_not_empty", "eth0", "eth0"},
|
||||
}
|
||||
|
||||
fakeExec := fakeexec.FakeExec{
|
||||
CommandScript: []fakeexec.FakeCommandAction{},
|
||||
}
|
||||
netsh := New(&fakeExec)
|
||||
|
||||
for _, test := range tests {
|
||||
os.Setenv("INTERFACE_TO_ADD_SERVICE_IP", test.envToBeSet)
|
||||
result := netsh.GetInterfaceToAddIP()
|
||||
|
||||
assert.EqualValuesf(t, test.expectedResult, result, "Failed to test: %s", test.name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestore(t *testing.T) {
|
||||
runner := New(&fakeexec.FakeExec{
|
||||
CommandScript: []fakeexec.FakeCommandAction{},
|
||||
})
|
||||
|
||||
result := runner.Restore([]string{})
|
||||
assert.NoErrorf(t, result, "The return value must be nil")
|
||||
}
|
||||
|
||||
func TestCheckIPExists(t *testing.T) {
|
||||
fakeCmd := fakeexec.FakeCmd{
|
||||
CombinedOutputScript: []fakeexec.FakeAction{
|
||||
// Error exists
|
||||
func() ([]byte, []byte, error) {
|
||||
return nil, nil, &fakeexec.FakeExitError{Status: 1}
|
||||
},
|
||||
// IP address string is empty
|
||||
func() ([]byte, []byte, error) {
|
||||
return []byte(""), nil, nil
|
||||
},
|
||||
// "IP Address:" field not exists
|
||||
func() ([]byte, []byte, error) {
|
||||
return []byte("10.10.10.10"), nil, nil
|
||||
},
|
||||
// IP not exists
|
||||
func() ([]byte, []byte, error) {
|
||||
return []byte("IP Address:10.10.10.10"), nil, nil
|
||||
},
|
||||
// IP exists
|
||||
func() ([]byte, []byte, error) {
|
||||
return []byte("IP Address:10.10.10.10\nIP Address:10.10.10.20"), nil, nil
|
||||
},
|
||||
},
|
||||
}
|
||||
fakeExec := fakeexec.FakeExec{
|
||||
CommandScript: []fakeexec.FakeCommandAction{
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeCmd, cmd, args...)
|
||||
},
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeCmd, cmd, args...)
|
||||
},
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeCmd, cmd, args...)
|
||||
},
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeCmd, cmd, args...)
|
||||
},
|
||||
func(cmd string, args ...string) exec.Cmd {
|
||||
return fakeexec.InitFakeCmd(&fakeCmd, cmd, args...)
|
||||
},
|
||||
},
|
||||
}
|
||||
fakeRunner := &runner{
|
||||
exec: &fakeExec,
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
ipToCheck string
|
||||
arguments []string
|
||||
expectedError bool
|
||||
expectedResult bool
|
||||
}{
|
||||
{"Error exists", "10.10.10.20", []string{"check-IP-exists"}, true, false},
|
||||
{"IP address string is empty", "10.10.10.20", []string{"check-IP-exists"}, false, false},
|
||||
{"'IP Address:' field not exists", "10.10.10.20", []string{"check-IP-exists"}, false, false},
|
||||
{"IP not exists", "10.10.10.20", []string{"check-IP-exists"}, false, false},
|
||||
{"IP exists", "10.10.10.20", []string{"check-IP-exists"}, false, true},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
result, err := checkIPExists(test.ipToCheck, test.arguments, fakeRunner)
|
||||
if test.expectedError {
|
||||
assert.Errorf(t, err, "Failed to test: %s", test.name)
|
||||
} else {
|
||||
assert.EqualValuesf(t, test.expectedResult, result, "Failed to test: %s", test.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetIP(t *testing.T) {
|
||||
testcases := []struct {
|
||||
name string
|
||||
showAddress string
|
||||
expectAddress string
|
||||
}{
|
||||
{
|
||||
name: "IP address displayed in Chinese",
|
||||
showAddress: "IP 地址: 10.96.0.2",
|
||||
expectAddress: "10.96.0.2",
|
||||
},
|
||||
{
|
||||
name: "IP address displayed in English",
|
||||
showAddress: "IP Address: 10.96.0.3",
|
||||
expectAddress: "10.96.0.3",
|
||||
},
|
||||
{
|
||||
name: "IP address without spaces",
|
||||
showAddress: "IP Address:10.96.0.4",
|
||||
expectAddress: "10.96.0.4",
|
||||
},
|
||||
{
|
||||
name: "Only 'IP Address:' field exists",
|
||||
showAddress: "IP Address:",
|
||||
expectAddress: "",
|
||||
},
|
||||
{
|
||||
name: "IP address without ':' separator",
|
||||
showAddress: "IP Address10.6.9.2",
|
||||
expectAddress: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
address := getIP(tc.showAddress)
|
||||
if address != tc.expectAddress {
|
||||
t.Errorf("expected address=%q, got %q", tc.expectAddress, address)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes 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 testing
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"k8s.io/kubernetes/pkg/util/netsh"
|
||||
)
|
||||
|
||||
// FakeNetsh is a no-op implementation of the netsh Interface
|
||||
type FakeNetsh struct {
|
||||
}
|
||||
|
||||
// NewFake returns a fakenetsh no-op implementation of the netsh Interface
|
||||
func NewFake() *FakeNetsh {
|
||||
return &FakeNetsh{}
|
||||
}
|
||||
|
||||
// EnsurePortProxyRule function implementing the netsh interface and always returns true and nil without any error
|
||||
func (*FakeNetsh) EnsurePortProxyRule(args []string) (bool, error) {
|
||||
// Do Nothing
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// DeletePortProxyRule deletes the specified portproxy rule. If the rule did not exist, return error.
|
||||
func (*FakeNetsh) DeletePortProxyRule(args []string) error {
|
||||
// Do Nothing
|
||||
return nil
|
||||
}
|
||||
|
||||
// EnsureIPAddress checks if the specified IP Address is added to vEthernet (HNSTransparent) interface, if not, add it. If the address existed, return true.
|
||||
func (*FakeNetsh) EnsureIPAddress(args []string, ip net.IP) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// DeleteIPAddress checks if the specified IP address is present and, if so, deletes it.
|
||||
func (*FakeNetsh) DeleteIPAddress(args []string) error {
|
||||
// Do Nothing
|
||||
return nil
|
||||
}
|
||||
|
||||
// Restore runs `netsh exec` to restore portproxy or addresses using a file.
|
||||
// TODO Check if this is required, most likely not
|
||||
func (*FakeNetsh) Restore(args []string) error {
|
||||
// Do Nothing
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetInterfaceToAddIP returns the interface name where Service IP needs to be added
|
||||
// IP Address needs to be added for netsh portproxy to redirect traffic
|
||||
// Reads Environment variable INTERFACE_TO_ADD_SERVICE_IP, if it is not defined then "vEthernet (HNSTransparent)" is returned
|
||||
func (*FakeNetsh) GetInterfaceToAddIP() string {
|
||||
return "Interface 1"
|
||||
}
|
||||
|
||||
var _ = netsh.Interface(&FakeNetsh{})
|
||||
Reference in New Issue
Block a user