Skip to content

Commit 8dbc673

Browse files
authored
Merge pull request kubernetes#130151 from marosset/windows-unit-tests-externaljwt-plugin-fixes
fixing k8s.io/kubernetes/pkg/serviceaccount/externaljwt/plugin unit tests on Windows
2 parents d935836 + 5e6611a commit 8dbc673

File tree

5 files changed

+53
-13
lines changed

5 files changed

+53
-13
lines changed

pkg/controlplane/apiserver/options/options_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/google/go-cmp/cmp/cmpopts"
3333
"github.com/spf13/pflag"
3434
noopoteltrace "go.opentelemetry.io/otel/trace/noop"
35+
utilnettesting "k8s.io/apimachinery/pkg/util/net/testing"
3536
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3637
"k8s.io/apiserver/pkg/admission"
3738
apiserveroptions "k8s.io/apiserver/pkg/server/options"
@@ -483,7 +484,7 @@ func TestCompleteForServiceAccount(t *testing.T) {
483484
options := NewOptions()
484485
if tc.externalSigner {
485486
// create and start mock signer.
486-
socketPath := fmt.Sprintf("@mock-external-jwt-signer-%d.sock", time.Now().Nanosecond())
487+
socketPath := utilnettesting.MakeSocketNameForTest(t, fmt.Sprintf("mock-external-jwt-signer-%d.sock", time.Now().Nanosecond()))
487488
mockSigner := v1alpha1testing.NewMockSigner(t, socketPath)
488489
defer mockSigner.CleanUp()
489490

pkg/serviceaccount/externaljwt/plugin/keycache_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"fmt"
2222
"net"
23-
"os"
2423
"strings"
2524
"sync/atomic"
2625
"testing"
@@ -35,6 +34,8 @@ import (
3534
"k8s.io/apimachinery/pkg/util/wait"
3635
externaljwtv1alpha1 "k8s.io/externaljwt/apis/v1alpha1"
3736
"k8s.io/kubernetes/pkg/serviceaccount"
37+
38+
utilnettesting "k8s.io/apimachinery/pkg/util/net/testing"
3839
)
3940

4041
func TestExternalPublicKeyGetter(t *testing.T) {
@@ -169,8 +170,7 @@ func TestExternalPublicKeyGetter(t *testing.T) {
169170
t.Run(tc.desc, func(t *testing.T) {
170171
ctx := context.Background()
171172

172-
sockname := fmt.Sprintf("@test-external-public-key-getter-%d-%d.sock", time.Now().Nanosecond(), i)
173-
t.Cleanup(func() { _ = os.Remove(sockname) })
173+
sockname := utilnettesting.MakeSocketNameForTest(t, fmt.Sprintf("test-external-public-key-getter-%d-%d.sock", time.Now().Nanosecond(), i))
174174

175175
addr := &net.UnixAddr{Name: sockname, Net: "unix"}
176176
listener, err := net.ListenUnix(addr.Network(), addr)
@@ -240,8 +240,7 @@ func TestExternalPublicKeyGetter(t *testing.T) {
240240
func TestInitialFill(t *testing.T) {
241241
ctx := context.Background()
242242

243-
sockname := fmt.Sprintf("@test-initial-fill-%d.sock", time.Now().Nanosecond())
244-
t.Cleanup(func() { _ = os.Remove(sockname) })
243+
sockname := utilnettesting.MakeSocketNameForTest(t, fmt.Sprintf("test-initial-fill-%d.sock", time.Now().Nanosecond()))
245244

246245
addr := &net.UnixAddr{Name: sockname, Net: "unix"}
247246
listener, err := net.ListenUnix(addr.Network(), addr)
@@ -306,8 +305,7 @@ func TestInitialFill(t *testing.T) {
306305
func TestReflectChanges(t *testing.T) {
307306
ctx := context.Background()
308307

309-
sockname := fmt.Sprintf("@test-reflect-changes-%d.sock", time.Now().Nanosecond())
310-
t.Cleanup(func() { _ = os.Remove(sockname) })
308+
sockname := utilnettesting.MakeSocketNameForTest(t, fmt.Sprintf("test-reflect-changes-%d.sock", time.Now().Nanosecond()))
311309

312310
addr := &net.UnixAddr{Name: sockname, Net: "unix"}
313311
listener, err := net.ListenUnix(addr.Network(), addr)

pkg/serviceaccount/externaljwt/plugin/plugin_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"encoding/json"
2626
"fmt"
2727
"net"
28-
"os"
2928
"strings"
3029
"sync"
3130
"testing"
@@ -39,6 +38,7 @@ import (
3938

4039
"k8s.io/kubernetes/pkg/serviceaccount"
4140

41+
utilnettesting "k8s.io/apimachinery/pkg/util/net/testing"
4242
externaljwtv1alpha1 "k8s.io/externaljwt/apis/v1alpha1"
4343
)
4444

@@ -258,8 +258,7 @@ func TestExternalTokenGenerator(t *testing.T) {
258258
t.Run(tc.desc, func(t *testing.T) {
259259
ctx := context.Background()
260260

261-
sockname := fmt.Sprintf("@test-external-token-generator-%d-%d.sock", time.Now().Nanosecond(), i)
262-
t.Cleanup(func() { _ = os.Remove(sockname) })
261+
sockname := utilnettesting.MakeSocketNameForTest(t, fmt.Sprintf("test-external-token-generator-%d-%d.sock", time.Now().Nanosecond(), i))
263262

264263
addr := &net.UnixAddr{Name: sockname, Net: "unix"}
265264
listener, err := net.ListenUnix(addr.Network(), addr)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package nettesting contains utilities for testing networking functionality.
18+
// Don't use these utilities in production code. They have not been security
19+
// reviewed.
20+
package nettesting
21+
22+
import (
23+
"os"
24+
goruntime "runtime"
25+
"testing"
26+
)
27+
28+
// MakeSocketNameForTest returns a socket name to use for the duration of a test.
29+
// On Operating systems that support abstract sockets, it the name is prefixed with `@` to make it an abstract socket.
30+
// On Operating systems that do not support abstract sockets, the name is treated as a filename and a cleanup hook is
31+
// registered to delete the socket at the end of the test.
32+
func MakeSocketNameForTest(t testing.TB, name string) string {
33+
var sockname = name
34+
switch goruntime.GOOS {
35+
case "darwin", "windows":
36+
t.Cleanup(func() { _ = os.Remove(sockname) })
37+
default:
38+
sockname = "@" + name
39+
}
40+
return sockname
41+
}

test/integration/serviceaccount/external_jwt_signer_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
authv1 "k8s.io/api/authentication/v1"
3131
corev1 "k8s.io/api/core/v1"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33+
utilnettesting "k8s.io/apimachinery/pkg/util/net/testing"
3334
"k8s.io/apimachinery/pkg/util/wait"
3435
utilfeature "k8s.io/apiserver/pkg/util/feature"
3536
"k8s.io/client-go/kubernetes"
@@ -61,7 +62,7 @@ func TestExternalJWTSigningAndAuth(t *testing.T) {
6162
defer cancel()
6263

6364
// create and start mock signer.
64-
socketPath := fmt.Sprintf("@mock-external-jwt-signer-%d.sock", time.Now().Nanosecond())
65+
socketPath := utilnettesting.MakeSocketNameForTest(t, fmt.Sprintf("mock-external-jwt-signer-%d.sock", time.Now().Nanosecond()))
6566
t.Cleanup(func() { _ = os.Remove(socketPath) })
6667
mockSigner := v1alpha1testing.NewMockSigner(t, socketPath)
6768
defer mockSigner.CleanUp()
@@ -277,7 +278,7 @@ func TestDelayedStartForSigner(t *testing.T) {
277278
defer cancel()
278279

279280
// Schedule signer to start on socket after 20 sec
280-
socketPath := "@mock-external-jwt-signer.sock"
281+
socketPath := utilnettesting.MakeSocketNameForTest(t, "mock-external-jwt-signer.sock")
281282
t.Cleanup(func() { _ = os.Remove(socketPath) })
282283
go func() {
283284
time.Sleep(20 * time.Second)

0 commit comments

Comments
 (0)