Merge pull request #125545 from claudiubelu/unittests-12

unittests: Fixes unit tests for Windows (part 12)
This commit is contained in:
Kubernetes Prow Robot 2024-06-28 03:57:56 -07:00 committed by GitHub
commit e832b70230
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 99 additions and 29 deletions

View File

@ -457,6 +457,8 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
var ( var (
gv = kubeadmapiv1old.SchemeGroupVersion.String() gv = kubeadmapiv1old.SchemeGroupVersion.String()
gvNew = kubeadmapiv1.SchemeGroupVersion.String() gvNew = kubeadmapiv1.SchemeGroupVersion.String()
criSocket = fmt.Sprintf("%s:///some-socket-path", kubeadmapiv1.DefaultContainerRuntimeURLScheme)
caCertPath = kubeadmapiv1.DefaultCACertPath
input = dedent.Dedent(fmt.Sprintf(` input = dedent.Dedent(fmt.Sprintf(`
apiVersion: %s apiVersion: %s
@ -473,7 +475,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
advertiseAddress: 1.2.3.4 advertiseAddress: 1.2.3.4
bindPort: 6443 bindPort: 6443
nodeRegistration: nodeRegistration:
criSocket: unix:///some-socket-path criSocket: %[2]s
kubeletExtraArgs: # MIGRATED kubeletExtraArgs: # MIGRATED
foo: bar foo: bar
name: node name: node
@ -499,7 +501,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
apiVersion: %[1]s apiVersion: %[1]s
kind: JoinConfiguration kind: JoinConfiguration
nodeRegistration: nodeRegistration:
criSocket: unix:///some-socket-path criSocket: %[2]s
imagePullPolicy: IfNotPresent imagePullPolicy: IfNotPresent
kubeletExtraArgs: # MIGRATED kubeletExtraArgs: # MIGRATED
foo: baz foo: baz
@ -512,7 +514,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
unsafeSkipCAVerification: true unsafeSkipCAVerification: true
tlsBootstrapToken: abcdef.0123456789abcdef tlsBootstrapToken: abcdef.0123456789abcdef
timeout: 2m10s # MIGRATED timeout: 2m10s # MIGRATED
`, gv)) `, gv, criSocket))
expectedOutput = dedent.Dedent(fmt.Sprintf(` expectedOutput = dedent.Dedent(fmt.Sprintf(`
apiVersion: %s apiVersion: %s
@ -529,7 +531,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
advertiseAddress: 1.2.3.4 advertiseAddress: 1.2.3.4
bindPort: 6443 bindPort: 6443
nodeRegistration: nodeRegistration:
criSocket: unix:///some-socket-path criSocket: %[2]s
imagePullPolicy: IfNotPresent imagePullPolicy: IfNotPresent
imagePullSerial: true imagePullSerial: true
kubeletExtraArgs: kubeletExtraArgs:
@ -582,7 +584,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
value: bar value: bar
--- ---
apiVersion: %[1]s apiVersion: %[1]s
caCertPath: /etc/kubernetes/pki/ca.crt caCertPath: %[3]s
discovery: discovery:
bootstrapToken: bootstrapToken:
apiServerEndpoint: some-address:6443 apiServerEndpoint: some-address:6443
@ -591,7 +593,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
tlsBootstrapToken: abcdef.0123456789abcdef tlsBootstrapToken: abcdef.0123456789abcdef
kind: JoinConfiguration kind: JoinConfiguration
nodeRegistration: nodeRegistration:
criSocket: unix:///some-socket-path criSocket: %[2]s
imagePullPolicy: IfNotPresent imagePullPolicy: IfNotPresent
imagePullSerial: true imagePullSerial: true
kubeletExtraArgs: kubeletExtraArgs:
@ -607,7 +609,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
kubernetesAPICall: 1m0s kubernetesAPICall: 1m0s
tlsBootstrap: 5m0s tlsBootstrap: 5m0s
upgradeManifests: 5m0s upgradeManifests: 5m0s
`, gvNew)) `, gvNew, criSocket, caCertPath))
) )
b, err := MigrateOldConfig([]byte(input), false, defaultEmptyMigrateMutators()) b, err := MigrateOldConfig([]byte(input), false, defaultEmptyMigrateMutators())

View File

@ -20,13 +20,12 @@ limitations under the License.
package util package util
import ( import (
"context" "fmt"
"net"
"reflect" "reflect"
"runtime" "runtime"
"strings"
"testing" "testing"
"github.com/Microsoft/go-winio"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -98,13 +97,10 @@ func TestLocalEndpoint(t *testing.T) {
} }
} }
func npipeDial(ctx context.Context, addr string) (net.Conn, error) {
return winio.DialPipeContext(ctx, addr)
}
func TestLocalEndpointRoundTrip(t *testing.T) { func TestLocalEndpointRoundTrip(t *testing.T) {
npipeDialPointer := reflect.ValueOf(npipeDial).Pointer() functionPointer := reflect.ValueOf(util.GetAddressAndDialer).Pointer()
expectedDialerName := runtime.FuncForPC(npipeDialPointer).Name() functionName := runtime.FuncForPC(functionPointer).Name()
expectedDialerName := fmt.Sprintf("%s.npipeDial", functionName[:strings.LastIndex(functionName, ".")])
expectedAddress := "//./pipe/kubelet-pod-resources" expectedAddress := "//./pipe/kubelet-pod-resources"
fullPath, err := LocalEndpoint(`pod-resources`, "kubelet") fullPath, err := LocalEndpoint(`pod-resources`, "kubelet")
@ -116,8 +112,6 @@ func TestLocalEndpointRoundTrip(t *testing.T) {
dialerPointer := reflect.ValueOf(dialer).Pointer() dialerPointer := reflect.ValueOf(dialer).Pointer()
actualDialerName := runtime.FuncForPC(dialerPointer).Name() actualDialerName := runtime.FuncForPC(dialerPointer).Name()
assert.Equalf(t, npipeDialPointer, dialerPointer, assert.Equal(t, expectedDialerName, actualDialerName)
"Expected dialer %s, but get %s", expectedDialerName, actualDialerName)
assert.Equal(t, expectedAddress, address) assert.Equal(t, expectedAddress, address)
} }

View File

@ -0,0 +1,42 @@
//go:build !windows
// +build !windows
/*
Copyright 2024 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 validation
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
)
var (
kubeProxyConfigNewPath = field.NewPath("KubeProxyConfiguration")
osKubeProxyConfigTestCases = map[string]struct {
mutateConfigFunc func(*kubeproxyconfig.KubeProxyConfiguration)
expectedErrs field.ErrorList
}{
// Windows doesn't support IPVS, so this test will fail.
"IPVS mode selected without providing required SyncPeriod": {
mutateConfigFunc: func(config *kubeproxyconfig.KubeProxyConfiguration) {
config.Mode = kubeproxyconfig.ProxyModeIPVS
},
expectedErrs: field.ErrorList{field.Invalid(kubeProxyConfigNewPath.Child("KubeProxyIPVSConfiguration.SyncPeriod"), metav1.Duration{Duration: 0}, "must be greater than 0")},
},
}
)

View File

@ -55,7 +55,7 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
} }
newPath := field.NewPath("KubeProxyConfiguration") newPath := field.NewPath("KubeProxyConfiguration")
for name, testCase := range map[string]struct { testCases := map[string]struct {
mutateConfigFunc func(*kubeproxyconfig.KubeProxyConfiguration) mutateConfigFunc func(*kubeproxyconfig.KubeProxyConfiguration)
expectedErrs field.ErrorList expectedErrs field.ErrorList
}{ }{
@ -155,12 +155,6 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
}, },
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("ConfigSyncPeriod"), metav1.Duration{Duration: -1 * time.Second}, "must be greater than 0")}, expectedErrs: field.ErrorList{field.Invalid(newPath.Child("ConfigSyncPeriod"), metav1.Duration{Duration: -1 * time.Second}, "must be greater than 0")},
}, },
"IPVS mode selected without providing required SyncPeriod": {
mutateConfigFunc: func(config *kubeproxyconfig.KubeProxyConfiguration) {
config.Mode = kubeproxyconfig.ProxyModeIPVS
},
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("KubeProxyIPVSConfiguration.SyncPeriod"), metav1.Duration{Duration: 0}, "must be greater than 0")},
},
"interfacePrefix is empty": { "interfacePrefix is empty": {
mutateConfigFunc: func(config *kubeproxyconfig.KubeProxyConfiguration) { mutateConfigFunc: func(config *kubeproxyconfig.KubeProxyConfiguration) {
config.DetectLocalMode = kubeproxyconfig.LocalModeInterfaceNamePrefix config.DetectLocalMode = kubeproxyconfig.LocalModeInterfaceNamePrefix
@ -193,7 +187,13 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
}, },
expectedErrs: field.ErrorList{field.Invalid(newPath.Child("logging.format"), "unsupported format", "Unsupported log format")}, expectedErrs: field.ErrorList{field.Invalid(newPath.Child("logging.format"), "unsupported format", "Unsupported log format")},
}, },
} { }
for name, testCase := range osKubeProxyConfigTestCases {
testCases[name] = testCase
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
config := baseConfig.DeepCopy() config := baseConfig.DeepCopy()
testCase.mutateConfigFunc(config) testCase.mutateConfigFunc(config)

View File

@ -0,0 +1,32 @@
//go:build windows
// +build windows
/*
Copyright 2024 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 validation
import (
"k8s.io/apimachinery/pkg/util/validation/field"
kubeproxyconfig "k8s.io/kubernetes/pkg/proxy/apis/config"
)
var (
osKubeProxyConfigTestCases = map[string]struct {
mutateConfigFunc func(*kubeproxyconfig.KubeProxyConfiguration)
expectedErrs field.ErrorList
}{}
)