Skip to content

Commit 3268428

Browse files
committed
e2e: unify subtests to be compatible with Goland
1 parent 3bdf22c commit 3268428

File tree

12 files changed

+58
-56
lines changed

12 files changed

+58
-56
lines changed

test/e2e/apibinding/maximalpermissionpolicy_authorizer_test.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,10 @@ func TestMaximalPermissionPolicyAuthorizerSystemGroupProtection(t *testing.T) {
7575
test func(t *testing.T)
7676
}
7777

78-
for _, test := range []Test{
78+
for _, testCase := range []Test{
7979
{
8080
name: "APIBinding resources",
8181
test: func(t *testing.T) {
82-
t.Helper()
83-
t.Parallel()
84-
8582
t.Logf("Creating a WorkspaceType as user-1")
8683
userKcpClusterClient, err := kcpclientset.NewForConfig(framework.StaticTokenUserConfig("user-1", server.BaseConfig(t)))
8784
require.NoError(t, err, "failed to construct kcp cluster client for user-1")
@@ -110,9 +107,6 @@ func TestMaximalPermissionPolicyAuthorizerSystemGroupProtection(t *testing.T) {
110107
{
111108
name: "System CRDs",
112109
test: func(t *testing.T) {
113-
t.Helper()
114-
t.Parallel()
115-
116110
t.Logf("Creating a APIExport as user-1")
117111
userKcpClusterClient, err := kcpclientset.NewForConfig(framework.StaticTokenUserConfig("user-1", server.BaseConfig(t)))
118112
require.NoError(t, err, "failed to construct kcp cluster client for user-1")
@@ -139,7 +133,11 @@ func TestMaximalPermissionPolicyAuthorizerSystemGroupProtection(t *testing.T) {
139133
},
140134
},
141135
} {
142-
t.Run(test.name, test.test)
136+
test := testCase
137+
t.Run(test.name, func(t *testing.T) {
138+
t.Parallel()
139+
test.test(t)
140+
})
143141
}
144142
}
145143

test/e2e/authorizer/authorizer_test.go

+30-28
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
"github.com/stretchr/testify/require"
3131

3232
authorizationv1 "k8s.io/api/authorization/v1"
33-
v1 "k8s.io/api/core/v1"
33+
corev1 "k8s.io/api/core/v1"
3434
rbacv1 "k8s.io/api/rbac/v1"
3535
"k8s.io/apimachinery/pkg/api/errors"
3636
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -106,22 +106,25 @@ func TestAuthorizer(t *testing.T) {
106106
return err == nil
107107
}, 2*wait.ForeverTestTimeout, 100*time.Millisecond)
108108

109-
tests := map[string]func(t *testing.T){
110-
"as org member, workspace admin user-1 can access everything": func(t *testing.T) {
109+
testCases := []struct {
110+
name string
111+
run func(t *testing.T)
112+
}{
113+
{"as org member, workspace admin user-1 can access everything", func(t *testing.T) {
111114
_, err := user1KubeClusterClient.Cluster(org1.Join("workspace1")).CoreV1().ConfigMaps("default").List(ctx, metav1.ListOptions{})
112115
require.NoError(t, err)
113-
_, err = user1KubeClusterClient.Cluster(org1.Join("workspace1")).CoreV1().Namespaces().Create(ctx, &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test"}}, metav1.CreateOptions{})
116+
_, err = user1KubeClusterClient.Cluster(org1.Join("workspace1")).CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test"}}, metav1.CreateOptions{})
114117
require.NoError(t, err)
115-
_, err = user1KubeClusterClient.Cluster(org1.Join("workspace1")).CoreV1().ConfigMaps("test").Create(ctx, &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "test"}}, metav1.CreateOptions{})
118+
_, err = user1KubeClusterClient.Cluster(org1.Join("workspace1")).CoreV1().ConfigMaps("test").Create(ctx, &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "test"}}, metav1.CreateOptions{})
116119
require.NoError(t, err)
117-
},
118-
"with org access, workspace1 non-admin user-2 can access according to local policy": func(t *testing.T) {
119-
_, err := user2KubeClusterClient.Cluster(org1.Join("workspace1")).CoreV1().Namespaces().Create(ctx, &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test"}}, metav1.CreateOptions{})
120+
}},
121+
{"with org access, workspace1 non-admin user-2 can access according to local policy", func(t *testing.T) {
122+
_, err := user2KubeClusterClient.Cluster(org1.Join("workspace1")).CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "test"}}, metav1.CreateOptions{})
120123
require.Errorf(t, err, "user-2 should not be able to create namespace in %s", org1.Join("workspace1"))
121124
_, err = user2KubeClusterClient.Cluster(org1.Join("workspace1")).CoreV1().Secrets("default").List(ctx, metav1.ListOptions{})
122125
require.NoErrorf(t, err, "user-2 should be able to list secrets in %s as defined in the local policy", org1.Join("workspace1"))
123-
},
124-
"with org access, workspace1 non-admin user-2 can access /healthz, /livez, /readyz etc": func(t *testing.T) {
126+
}},
127+
{"with org access, workspace1 non-admin user-2 can access /healthz, /livez, /readyz etc", func(t *testing.T) {
125128
cl := user2KubeClusterClient.RESTClient()
126129
requestPath := org1.RequestPath()
127130
{
@@ -141,24 +144,24 @@ func TestAuthorizer(t *testing.T) {
141144
version := new(apimachineryversion.Info)
142145
require.NoError(t, json.Unmarshal(respBytes, version))
143146
}
144-
},
145-
"without org access, org1 workspace1 admin user-1 cannot access org2, not even discovery": func(t *testing.T) {
147+
}},
148+
{"without org access, org1 workspace1 admin user-1 cannot access org2, not even discovery", func(t *testing.T) {
146149
_, err := user1KubeClusterClient.Cluster(org2.Join("workspace1")).CoreV1().ConfigMaps("default").List(ctx, metav1.ListOptions{})
147150
require.Errorf(t, err, "user-1 should not be able to list configmaps in a different org (%s)", org2.Join("workspace1"))
148151
_, err = user1KubeDiscoveryClient.Cluster(org2.Join("workspace1")).ServerResourcesForGroupVersion("rbac.authorization.k8s.io/v1") // can't be core because that always returns nil
149152
require.Errorf(t, err, "user-1 should not be able to list server resources in a different org (%s)", org2.Join("workspace1"))
150-
},
151-
"as org member, workspace1 admin user-1 cannot access workspace2, not even discovery": func(t *testing.T) {
153+
}},
154+
{"as org member, workspace1 admin user-1 cannot access workspace2, not even discovery", func(t *testing.T) {
152155
_, err := user1KubeClusterClient.Cluster(org1.Join("workspace2")).CoreV1().ConfigMaps("default").List(ctx, metav1.ListOptions{})
153156
require.Errorf(t, err, "user-1 should not be able to list configmaps in a different workspace (%s)", org1.Join("workspace2"))
154157
_, err = user1KubeDiscoveryClient.Cluster(org2.Join("workspace1")).ServerResourcesForGroupVersion("rbac.authorization.k8s.io/v1") // can't be core because that always returns nil
155158
require.Errorf(t, err, "user-1 should not be able to list server resources in a different workspace (%s)", org1.Join("workspace2"))
156-
},
157-
"with org access, workspace2 admin user-2 can access workspace2": func(t *testing.T) {
159+
}},
160+
{"with org access, workspace2 admin user-2 can access workspace2", func(t *testing.T) {
158161
_, err := user2KubeClusterClient.Cluster(org1.Join("workspace2")).CoreV1().ConfigMaps("default").List(ctx, metav1.ListOptions{})
159162
require.NoError(t, err, "user-2 should be able to list configmaps in workspace2 (%s)", org1.Join("workspace2"))
160-
},
161-
"cluster admins can use wildcard clusters, non-cluster admin cannot": func(t *testing.T) {
163+
}},
164+
{"cluster admins can use wildcard clusters, non-cluster admin cannot", func(t *testing.T) {
162165
// create client talking directly to root shard to test wildcard requests
163166
rootKubeClusterClient, err := kcpkubernetesclientset.NewForConfig(rootShardCfg)
164167
require.NoError(t, err)
@@ -169,8 +172,8 @@ func TestAuthorizer(t *testing.T) {
169172
require.NoError(t, err)
170173
_, err = user1RootKubeClusterClient.CoreV1().Namespaces().List(ctx, metav1.ListOptions{})
171174
require.Error(t, err, "Only cluster admins can use all clusters at once")
172-
},
173-
"with system:admin permissions, workspace2 non-admin user-3 can list Namespaces with a bootstrap ClusterRole": func(t *testing.T) {
175+
}},
176+
{"with system:admin permissions, workspace2 non-admin user-3 can list Namespaces with a bootstrap ClusterRole", func(t *testing.T) {
174177
// get workspace2 shard and create a client to tweak the local bootstrap policy
175178
shardKubeClusterClient, err := kcpkubernetesclientset.NewForConfig(rootShardCfg)
176179
require.NoError(t, err)
@@ -223,8 +226,8 @@ func TestAuthorizer(t *testing.T) {
223226
}
224227
return true
225228
}, wait.ForeverTestTimeout, time.Millisecond*100, "User-3 should now be able to list Namespaces in %s", org1.Join("workspace2"))
226-
},
227-
"without org access, a deep SAR with user-1 against org2 succeeds even without org access for user-1": func(t *testing.T) {
229+
}},
230+
{"without org access, a deep SAR with user-1 against org2 succeeds even without org access for user-1", func(t *testing.T) {
228231
t.Logf("try to list ConfigMap as user-1 in %q without access, should fail", org2.Join("workspace1"))
229232
_, err := user1KubeClusterClient.Cluster(org2.Join("workspace1")).CoreV1().ConfigMaps("default").List(ctx, metav1.ListOptions{})
230233
require.Errorf(t, err, "user-1 should not be able to list configmaps in %q", org2.Join("workspace1"))
@@ -254,15 +257,14 @@ func TestAuthorizer(t *testing.T) {
254257
resp, err = deepSARClient.Cluster(org2.Join("workspace1")).AuthorizationV1().SubjectAccessReviews().Create(ctx, sar, metav1.CreateOptions{})
255258
require.NoError(t, err)
256259
require.Truef(t, resp.Status.Allowed, "SAR should answer hypothetically that user-1 could list configmaps in %q if it had access", org2.Join("workspace1"))
257-
},
260+
}},
258261
}
259262

260-
for tcName, tcFunc := range tests {
261-
tcName := tcName
262-
tcFunc := tcFunc
263-
t.Run(tcName, func(t *testing.T) {
263+
for i := range testCases {
264+
testCase := testCases[i]
265+
t.Run(testCase.name, func(t *testing.T) {
264266
t.Parallel()
265-
tcFunc(t)
267+
testCase.run(t)
266268
})
267269
}
268270
}

test/e2e/authorizer/serviceaccounts_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,10 @@ func TestServiceAccounts(t *testing.T) {
153153
}
154154
for i, ttc := range testCases {
155155
i := i
156+
ttc := ttc
156157
t.Run(ttc.name, func(t *testing.T) {
157158
t.Parallel()
159+
158160
saRestConfig := framework.ConfigWithToken(ttc.token(t), server.BaseConfig(t))
159161
saKubeClusterClient, err := kcpkubernetesclientset.NewForConfig(saRestConfig)
160162
require.NoError(t, err)

test/e2e/authorizer/workspace_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ func TestWorkspaces(t *testing.T) {
140140
},
141141
}
142142

143-
for i := range testCases {
144-
testCase := testCases[i]
143+
for _, testCase := range testCases {
144+
testCase := testCase
145145
t.Run(testCase.name, func(t *testing.T) {
146146
t.Parallel()
147147

test/e2e/homeworkspaces/home_workspaces_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func TestUserHomeWorkspaces(t *testing.T) {
8686
},
8787
}
8888

89-
for i := range testCases {
90-
testCase := testCases[i]
89+
for _, testCase := range testCases {
90+
testCase := testCase
9191
t.Run(testCase.name, func(t *testing.T) {
9292
t.Parallel()
9393
tokenAuthFile := framework.WriteTokenAuthFile(t)

test/e2e/reconciler/cluster/controller_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ func TestClusterController(t *testing.T) {
166166
source := framework.SharedKcpServer(t)
167167
orgPath, _ := framework.NewOrganizationFixture(t, source, framework.TODO_WithoutMultiShardSupport())
168168

169-
for i := range testCases {
170-
testCase := testCases[i]
169+
for _, testCase := range testCases {
170+
testCase := testCase
171171
t.Run(testCase.name, func(t *testing.T) {
172172
t.Parallel()
173173

test/e2e/reconciler/namespace/controller_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ func TestNamespaceScheduler(t *testing.T) {
250250
server := framework.SharedKcpServer(t)
251251
orgPath, _ := framework.NewOrganizationFixture(t, server, framework.TODO_WithoutMultiShardSupport())
252252

253-
for i := range testCases {
254-
testCase := testCases[i]
253+
for _, testCase := range testCases {
254+
testCase := testCase
255255

256256
t.Run(testCase.name, func(t *testing.T) {
257257
t.Parallel()

test/e2e/reconciler/shard/controller_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func TestWorkspaceShardController(t *testing.T) {
5151

5252
sharedServer := framework.SharedKcpServer(t)
5353

54-
for i := range testCases {
55-
testCase := testCases[i]
54+
for _, testCase := range testCases {
55+
testCase := testCase
5656
t.Run(testCase.name, func(t *testing.T) {
5757
t.Parallel()
5858

test/e2e/reconciler/workspace/controller_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ func TestWorkspaceController(t *testing.T) {
153153

154154
sharedServer := framework.SharedKcpServer(t)
155155

156-
for i := range testCases {
157-
testCase := testCases[i]
156+
for _, testCase := range testCases {
157+
testCase := testCase
158158
t.Run(testCase.name, func(t *testing.T) {
159159
t.Parallel()
160160

test/e2e/reconciler/workspacedeletion/controller_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ func TestWorkspaceDeletion(t *testing.T) {
281281

282282
sharedServer := framework.SharedKcpServer(t)
283283

284-
for i := range testCases {
285-
testCase := testCases[i]
284+
for _, testCase := range testCases {
285+
testCase := testCase
286286
t.Run(testCase.name, func(t *testing.T) {
287287
t.Parallel()
288288

test/e2e/virtual/syncer/virtualworkspace_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -1553,8 +1553,8 @@ func TestSyncerVirtualWorkspace(t *testing.T) {
15531553
},
15541554
}
15551555

1556-
for i := range testCases {
1557-
testCase := testCases[i]
1556+
for _, testCase := range testCases {
1557+
testCase := testCase
15581558
t.Run(testCase.name, func(t *testing.T) {
15591559
t.Parallel()
15601560

@@ -1904,8 +1904,8 @@ func TestUpsyncerVirtualWorkspace(t *testing.T) {
19041904
},
19051905
}
19061906

1907-
for i := range testCases {
1908-
testCase := testCases[i]
1907+
for _, testCase := range testCases {
1908+
testCase := testCase
19091909
t.Run(testCase.name, func(t *testing.T) {
19101910
t.Parallel()
19111911
framework.Suite(t, "transparent-multi-cluster")

test/e2e/workspacetype/controller_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,8 @@ func TestWorkspaceTypes(t *testing.T) {
326326

327327
server := framework.SharedKcpServer(t)
328328

329-
for i := range testCases {
330-
testCase := testCases[i]
329+
for _, testCase := range testCases {
330+
testCase := testCase
331331
t.Run(testCase.name, func(t *testing.T) {
332332
t.Parallel()
333333

0 commit comments

Comments
 (0)