Skip to content

Commit 63aa0e4

Browse files
committed
test: APIBinding: wrap listing VWs between shards with Eventually
The user gets Forbidden error even though it should have access to the resources. Retrying listing on the correct shard will eventually make it succeed. On-behalf-of: @SAP [email protected] Signed-off-by: Robert Vasek <[email protected]>
1 parent 4628ed5 commit 63aa0e4

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

test/e2e/apibinding/apibinding_test.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -442,29 +442,46 @@ func TestAPIBinding(t *testing.T) {
442442
export2, err := kcpClusterClient.Cluster(provider2Path).ApisV1alpha2().APIExports().Get(ctx, exportName, metav1.GetOptions{})
443443
require.NoError(t, err)
444444

445-
foundOnShards := 0
446-
//nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
447-
for _, vw := range export2.Status.VirtualWorkspaces {
448-
vw2ClusterClient, err := kcpdynamic.NewForConfig(apiexportVWConfig(t, rawConfig, vw.URL))
449-
require.NoError(t, err)
445+
kcptestinghelpers.Eventually(t, func() (bool, string) {
446+
foundOnShards := 0
447+
var listErrs []error
448+
449+
//nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
450+
for _, vw := range export2.Status.VirtualWorkspaces {
451+
vw2ClusterClient, err := kcpdynamic.NewForConfig(apiexportVWConfig(t, rawConfig, vw.URL))
452+
require.NoError(t, err)
453+
454+
t.Logf("Listing %s|%s cowboys via virtual workspace %s/clusters/%s", provider2Path, exportName, vw.URL, consumer3ClusterName)
455+
gvr := wildwestv1alpha1.SchemeGroupVersion.WithResource("cowboys")
456+
list, err := vw2ClusterClient.Cluster(consumer3ClusterName.Path()).Resource(gvr).Namespace("").List(ctx, metav1.ListOptions{})
457+
if err != nil {
458+
t.Logf("Error: %v", err)
459+
listErrs = append(listErrs, err)
460+
continue
461+
}
462+
require.Equal(t, 1, len(list.Items), "unexpected # of cowboys through virtual workspace with explicit workspace")
463+
foundOnShards++
450464

451-
t.Logf("Listing %s|%s cowboys via virtual workspace %s/clusters/%s", provider2Path, exportName, vw.URL, consumer3ClusterName)
452-
gvr := wildwestv1alpha1.SchemeGroupVersion.WithResource("cowboys")
453-
list, err := vw2ClusterClient.Cluster(consumer3ClusterName.Path()).Resource(gvr).Namespace("").List(ctx, metav1.ListOptions{})
454-
if err != nil {
455-
t.Logf("Error: %v", err)
456-
continue
465+
t.Logf("Listing %s|%s cowboys via virtual workspace wildcard list", provider2Path, exportName)
466+
list, err = vw2ClusterClient.Resource(gvr).List(ctx, metav1.ListOptions{})
467+
require.NoError(t, err, "error listing through virtual workspace wildcard")
468+
require.Equal(t, 1, len(list.Items), "unexpected # of cowboys through virtual workspace with wildcard")
457469
}
458-
require.Equal(t, 1, len(list.Items), "unexpected # of cowboys through virtual workspace with explicit workspace")
459-
foundOnShards++
460470

461-
t.Logf("Listing %s|%s cowboys via virtual workspace wildcard list", provider2Path, exportName)
462-
list, err = vw2ClusterClient.Resource(gvr).List(ctx, metav1.ListOptions{})
463-
require.NoError(t, err, "error listing through virtual workspace wildcard")
464-
require.Equal(t, 1, len(list.Items), "unexpected # of cowboys through virtual workspace with wildcard")
465-
}
466-
//nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
467-
require.Equal(t, 1, foundOnShards, "cowboys not found exactly on one shard, but on %d/%d", foundOnShards, len(export2.Status.VirtualWorkspaces))
471+
if foundOnShards == 0 {
472+
for i := range listErrs {
473+
if !errors.IsForbidden(listErrs[i]) {
474+
t.Fatalf("cowboys not found on any shards, with unexpected error: %v", listErrs[i])
475+
}
476+
}
477+
return false, "couldn't list via virtual workspaces because the user is not ready yet"
478+
}
479+
480+
//nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
481+
require.Equal(t, 1, foundOnShards, "cowboys not found exactly on one shard, but on %d/%d", foundOnShards, len(export2.Status.VirtualWorkspaces))
482+
483+
return true, ""
484+
}, wait.ForeverTestTimeout, 100*time.Millisecond, "expected to have cowboys exactly on one shard")
468485
}
469486

470487
func apiexportVWConfig(t *testing.T, kubeconfig clientcmdapi.Config, url string) *rest.Config {

0 commit comments

Comments
 (0)