Skip to content

Commit

Permalink
Merge pull request kubernetes#125545 from claudiubelu/unittests-12
Browse files Browse the repository at this point in the history
unittests: Fixes unit tests for Windows (part 12)
  • Loading branch information
k8s-ci-robot committed Jun 28, 2024
2 parents d40676c + b5e3b81 commit e832b70
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 29 deletions.
20 changes: 11 additions & 9 deletions cmd/kubeadm/app/util/config/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,10 @@ func TestMigrateOldConfig(t *testing.T) {
// - JoinConfiguration.Discovery.Timeout -> JoinConfiguration.Timeout.Discovery
func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
var (
gv = kubeadmapiv1old.SchemeGroupVersion.String()
gvNew = kubeadmapiv1.SchemeGroupVersion.String()
gv = kubeadmapiv1old.SchemeGroupVersion.String()
gvNew = kubeadmapiv1.SchemeGroupVersion.String()
criSocket = fmt.Sprintf("%s:///some-socket-path", kubeadmapiv1.DefaultContainerRuntimeURLScheme)
caCertPath = kubeadmapiv1.DefaultCACertPath

input = dedent.Dedent(fmt.Sprintf(`
apiVersion: %s
Expand All @@ -473,7 +475,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
advertiseAddress: 1.2.3.4
bindPort: 6443
nodeRegistration:
criSocket: unix:///some-socket-path
criSocket: %[2]s
kubeletExtraArgs: # MIGRATED
foo: bar
name: node
Expand All @@ -499,7 +501,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
apiVersion: %[1]s
kind: JoinConfiguration
nodeRegistration:
criSocket: unix:///some-socket-path
criSocket: %[2]s
imagePullPolicy: IfNotPresent
kubeletExtraArgs: # MIGRATED
foo: baz
Expand All @@ -512,7 +514,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
unsafeSkipCAVerification: true
tlsBootstrapToken: abcdef.0123456789abcdef
timeout: 2m10s # MIGRATED
`, gv))
`, gv, criSocket))

expectedOutput = dedent.Dedent(fmt.Sprintf(`
apiVersion: %s
Expand All @@ -529,7 +531,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
advertiseAddress: 1.2.3.4
bindPort: 6443
nodeRegistration:
criSocket: unix:///some-socket-path
criSocket: %[2]s
imagePullPolicy: IfNotPresent
imagePullSerial: true
kubeletExtraArgs:
Expand Down Expand Up @@ -582,7 +584,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
value: bar
---
apiVersion: %[1]s
caCertPath: /etc/kubernetes/pki/ca.crt
caCertPath: %[3]s
discovery:
bootstrapToken:
apiServerEndpoint: some-address:6443
Expand All @@ -591,7 +593,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
tlsBootstrapToken: abcdef.0123456789abcdef
kind: JoinConfiguration
nodeRegistration:
criSocket: unix:///some-socket-path
criSocket: %[2]s
imagePullPolicy: IfNotPresent
imagePullSerial: true
kubeletExtraArgs:
Expand All @@ -607,7 +609,7 @@ func TestMigrateV1Beta3WithBreakingChanges(t *testing.T) {
kubernetesAPICall: 1m0s
tlsBootstrap: 5m0s
upgradeManifests: 5m0s
`, gvNew))
`, gvNew, criSocket, caCertPath))
)

b, err := MigrateOldConfig([]byte(input), false, defaultEmptyMigrateMutators())
Expand Down
18 changes: 6 additions & 12 deletions pkg/kubelet/util/util_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ limitations under the License.
package util

import (
"context"
"net"
"fmt"
"reflect"
"runtime"
"strings"
"testing"

"github.com/Microsoft/go-winio"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -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) {
npipeDialPointer := reflect.ValueOf(npipeDial).Pointer()
expectedDialerName := runtime.FuncForPC(npipeDialPointer).Name()
functionPointer := reflect.ValueOf(util.GetAddressAndDialer).Pointer()
functionName := runtime.FuncForPC(functionPointer).Name()
expectedDialerName := fmt.Sprintf("%s.npipeDial", functionName[:strings.LastIndex(functionName, ".")])
expectedAddress := "//./pipe/kubelet-pod-resources"

fullPath, err := LocalEndpoint(`pod-resources`, "kubelet")
Expand All @@ -116,8 +112,6 @@ func TestLocalEndpointRoundTrip(t *testing.T) {
dialerPointer := reflect.ValueOf(dialer).Pointer()
actualDialerName := runtime.FuncForPC(dialerPointer).Name()

assert.Equalf(t, npipeDialPointer, dialerPointer,
"Expected dialer %s, but get %s", expectedDialerName, actualDialerName)

assert.Equal(t, expectedDialerName, actualDialerName)
assert.Equal(t, expectedAddress, address)
}
42 changes: 42 additions & 0 deletions pkg/proxy/apis/config/validation/validation_other_test.go
Original file line number Diff line number Diff line change
@@ -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")},
},
}
)
16 changes: 8 additions & 8 deletions pkg/proxy/apis/config/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
}
newPath := field.NewPath("KubeProxyConfiguration")

for name, testCase := range map[string]struct {
testCases := map[string]struct {
mutateConfigFunc func(*kubeproxyconfig.KubeProxyConfiguration)
expectedErrs field.ErrorList
}{
Expand Down Expand Up @@ -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")},
},
"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": {
mutateConfigFunc: func(config *kubeproxyconfig.KubeProxyConfiguration) {
config.DetectLocalMode = kubeproxyconfig.LocalModeInterfaceNamePrefix
Expand Down Expand Up @@ -193,7 +187,13 @@ func TestValidateKubeProxyConfiguration(t *testing.T) {
},
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) {
config := baseConfig.DeepCopy()
testCase.mutateConfigFunc(config)
Expand Down
32 changes: 32 additions & 0 deletions pkg/proxy/apis/config/validation/validation_windows_test.go
Original file line number Diff line number Diff line change
@@ -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
}{}
)

0 comments on commit e832b70

Please sign in to comment.