File: transport_test.go

package info (click to toggle)
golang-k8s-client-go 0.33.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 19,780 kB
  • sloc: makefile: 8; sh: 3
file content (149 lines) | stat: -rw-r--r-- 4,246 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Copyright 2022 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 rest

import (
	"context"
	"net"
	"strings"
	"sync"
	"testing"

	clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

// TestTransportForThreadSafe is meant to be run with the race detector
// to detect that the Transport generation for client-go is safe to
// call from multiple goroutines.
func TestTransportForThreadSafe(t *testing.T) {
	const (
		rootCACert = `-----BEGIN CERTIFICATE-----
MIIC4DCCAcqgAwIBAgIBATALBgkqhkiG9w0BAQswIzEhMB8GA1UEAwwYMTAuMTMu
MTI5LjEwNkAxNDIxMzU5MDU4MB4XDTE1MDExNTIxNTczN1oXDTE2MDExNTIxNTcz
OFowIzEhMB8GA1UEAwwYMTAuMTMuMTI5LjEwNkAxNDIxMzU5MDU4MIIBIjANBgkq
hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAunDRXGwsiYWGFDlWH6kjGun+PshDGeZX
xtx9lUnL8pIRWH3wX6f13PO9sktaOWW0T0mlo6k2bMlSLlSZgG9H6og0W6gLS3vq
s4VavZ6DbXIwemZG2vbRwsvR+t4G6Nbwelm6F8RFnA1Fwt428pavmNQ/wgYzo+T1
1eS+HiN4ACnSoDSx3QRWcgBkB1g6VReofVjx63i0J+w8Q/41L9GUuLqquFxu6ZnH
60vTB55lHgFiDLjA1FkEz2dGvGh/wtnFlRvjaPC54JH2K1mPYAUXTreoeJtLJKX0
ycoiyB24+zGCniUmgIsmQWRPaOPircexCp1BOeze82BT1LCZNTVaxQIDAQABoyMw
ITAOBgNVHQ8BAf8EBAMCAKQwDwYDVR0TAQH/BAUwAwEB/zALBgkqhkiG9w0BAQsD
ggEBADMxsUuAFlsYDpF4fRCzXXwrhbtj4oQwcHpbu+rnOPHCZupiafzZpDu+rw4x
YGPnCb594bRTQn4pAu3Ac18NbLD5pV3uioAkv8oPkgr8aUhXqiv7KdDiaWm6sbAL
EHiXVBBAFvQws10HMqMoKtO8f1XDNAUkWduakR/U6yMgvOPwS7xl0eUTqyRB6zGb
K55q2dejiFWaFqB/y78txzvz6UlOZKE44g2JAVoJVM6kGaxh33q8/FmrL4kuN3ut
W+MmJCVDvd4eEqPwbp7146ZWTqpIJ8lvA6wuChtqV8lhAPka2hD/LMqY8iXNmfXD
uml0obOEy+ON91k+SWTJ3ggmF/U=
-----END CERTIFICATE-----`
		authPluginName = "auth-plugin-tr"
	)

	err := RegisterAuthProviderPlugin(authPluginName, pluginAProvider)
	// the plugins are stored in a global variable that fails if the test
	// runs with the flag -count N, with N > 1
	if err != nil && !strings.Contains(err.Error(), "was registered twice") {
		t.Errorf("Unexpected error: failed to register pluginA: %v", err)
	}

	configurations := []struct {
		name   string
		config *Config
	}{
		{
			name: "simple config",
			config: &Config{
				Host:     "localhost:8080",
				Username: "gopher",
				Password: "g0ph3r",
			},
		},
		{
			name: "not cacheable config",
			config: &Config{
				Host:     "localhost:8080",
				Username: "gopher",
				Password: "g0ph3r",
				Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
					return nil, nil
				},
			},
		},
		{
			name: "with TLS config",
			config: &Config{
				Host:     "localhost:8080",
				Username: "gopher",
				Password: "g0ph3r",
				TLSClientConfig: TLSClientConfig{
					CAData: []byte(rootCACert),
				},
			},
		},
		{
			name: "with auth provider",
			config: &Config{
				Host:     "localhost:8080",
				Username: "gopher",
				Password: "g0ph3r",
				TLSClientConfig: TLSClientConfig{
					CAData: []byte(rootCACert),
				},
				AuthProvider: &clientcmdapi.AuthProviderConfig{
					Name:   authPluginName,
					Config: map[string]string{"secret": "s3cr3t"},
				},
			},
		},
		{
			name: "with exec provider",
			config: &Config{
				Host:     "localhost:8080",
				Username: "gopher",
				Password: "g0ph3r",
				TLSClientConfig: TLSClientConfig{
					CAData: []byte(rootCACert),
				},
				ExecProvider: &clientcmdapi.ExecConfig{
					Args:       []string{"secret"},
					Env:        []clientcmdapi.ExecEnvVar{{Name: "secret", Value: "s3cr3t"}},
					APIVersion: "client.authentication.k8s.io/v1beta1",
				},
			},
		},
	}
	var wg sync.WaitGroup

	for _, tt := range configurations {
		tt := tt
		wg.Add(1)
		go func() {
			defer wg.Done()
			for i := 1; i <= 50; i++ {
				wg.Add(1)
				go func() {
					defer wg.Done()
					_, err := TransportFor(tt.config)
					if err != nil {
						t.Errorf("Config: %s TransportFor() error = %v", tt.name, err)
					}
				}()
			}

		}()
	}
	wg.Wait()
}