Skip to content

Commit 2bba901

Browse files
committed
Update tests for changed beahviour
Signed-off-by: Mangirdas Judeikis <Mangirdas@Judeikis.LT> On-behalf-of: @SAP mangirdas.judeikis@sap.com
1 parent b449811 commit 2bba901

File tree

10 files changed

+270
-218
lines changed

10 files changed

+270
-218
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ test-e2e-sharded-minimal: build-all
349349

350350
# This is just easy target to run 2 shard test server locally until manually killed.
351351
# You can targer test to it by running:
352-
# go test ./test/e2e/apibinding/... --kcp-kubeconfig=${WORK_DIR:-.}/.kcp/admin.kubeconfig --shard-kubeconfigs=root=${WORK_DIR:-.}/.kcp-0/admin.kubeconfig -run=^TestAPIBindingEndpointSlicesSharded$
352+
# go test ./test/e2e/apibinding/... --kcp-kubeconfig=$(pwd)/.kcp/admin.kubeconfig --shard-kubeconfigs=root=$(pwd)/.kcp-0/admin.kubeconfig -run=^TestAPIBinding$
353353
test-run-sharded-server: WORK_DIR ?= $(PWD)
354354
test-run-sharded-server: LOG_DIR ?= $(WORK_DIR)/.kcp
355355
test-run-sharded-server:

test/e2e/apibinding/apibinding_logicalcluster_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"testing"
2323
"time"
2424

25+
"github.com/davecgh/go-spew/spew"
2526
"github.com/stretchr/testify/require"
2627

2728
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -32,6 +33,7 @@ import (
3233
kcpapiextensionsclientset "github.com/kcp-dev/client-go/apiextensions/client"
3334
kcpdynamic "github.com/kcp-dev/client-go/dynamic"
3435

36+
"github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1"
3537
apisv1alpha2 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha2"
3638
corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
3739
"github.com/kcp-dev/kcp/sdk/apis/third_party/conditions/util/conditions"
@@ -132,8 +134,13 @@ func TestAPIBindingLogicalCluster(t *testing.T) {
132134
return kcpClusterClient.Cluster(consumerPath).ApisV1alpha2().APIBindings().Get(ctx, exportName, metav1.GetOptions{})
133135
}, kcptestinghelpers.Is(apisv1alpha2.PermissionClaimsValid), "unable to see valid claims")
134136

135-
export, err := kcpClusterClient.Cluster(providerPath).ApisV1alpha2().APIExports().Get(ctx, exportName, metav1.GetOptions{})
136-
require.NoError(t, err)
137+
t.Logf("Waiting for APIExportEndpointSlice to be available")
138+
var exportEndpointSlice *v1alpha1.APIExportEndpointSlice
139+
kcptestinghelpers.Eventually(t, func() (bool, string) {
140+
var err error
141+
exportEndpointSlice, err = kcpClusterClient.Cluster(providerPath).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, exportName, metav1.GetOptions{})
142+
return err == nil && len(exportEndpointSlice.Status.APIExportEndpoints) > 0, fmt.Sprintf("failed to get APIExportEndpointSlice: %v, %s", err, spew.Sdump(exportEndpointSlice))
143+
}, wait.ForeverTestTimeout, time.Second*1)
137144

138145
rawConfig, err := server.RawConfig()
139146
require.NoError(t, err)
@@ -143,8 +150,7 @@ func TestAPIBindingLogicalCluster(t *testing.T) {
143150
kcptestinghelpers.Eventually(t, func() (bool, string) {
144151
items := []unstructured.Unstructured{}
145152

146-
//nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
147-
for _, vw := range export.Status.VirtualWorkspaces {
153+
for _, vw := range exportEndpointSlice.Status.APIExportEndpoints {
148154
vwClusterClient, err := kcpdynamic.NewForConfig(apiexportVWConfig(t, rawConfig, vw.URL))
149155
require.NoError(t, err)
150156

@@ -265,8 +271,13 @@ func TestAPIBindingCRDs(t *testing.T) {
265271
return kcpClusterClient.Cluster(consumerPath).ApisV1alpha2().APIBindings().Get(ctx, exportName, metav1.GetOptions{})
266272
}, kcptestinghelpers.Is(apisv1alpha2.PermissionClaimsValid), "unable to see valid claims")
267273

268-
export, err := kcpClusterClient.Cluster(providerPath).ApisV1alpha2().APIExports().Get(ctx, exportName, metav1.GetOptions{})
269-
require.NoError(t, err)
274+
t.Logf("Waiting for APIExportEndpointSlice to be available")
275+
var exportEndpointSlice *v1alpha1.APIExportEndpointSlice
276+
kcptestinghelpers.Eventually(t, func() (bool, string) {
277+
var err error
278+
exportEndpointSlice, err = kcpClusterClient.Cluster(providerPath).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, exportName, metav1.GetOptions{})
279+
return err == nil && len(exportEndpointSlice.Status.APIExportEndpoints) > 0, fmt.Sprintf("failed to get APIExportEndpointSlice: %v. %s", err, spew.Sdump(exportEndpointSlice))
280+
}, time.Second*60, time.Second*1)
270281

271282
rawConfig, err := server.RawConfig()
272283
require.NoError(t, err)
@@ -276,8 +287,7 @@ func TestAPIBindingCRDs(t *testing.T) {
276287
kcptestinghelpers.Eventually(t, func() (bool, string) {
277288
items := []unstructured.Unstructured{}
278289

279-
//nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
280-
for _, vw := range export.Status.VirtualWorkspaces {
290+
for _, vw := range exportEndpointSlice.Status.APIExportEndpoints {
281291
vwClusterClient, err := kcpdynamic.NewForConfig(apiexportVWConfig(t, rawConfig, vw.URL))
282292
require.NoError(t, err)
283293

test/e2e/apibinding/apibinding_protected_test.go

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ import (
3030
"k8s.io/client-go/restmapper"
3131

3232
kcpdynamic "github.com/kcp-dev/client-go/dynamic"
33-
"github.com/kcp-dev/logicalcluster/v3"
3433

3534
"github.com/kcp-dev/kcp/config/helpers"
3635
apisv1alpha2 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha2"
37-
"github.com/kcp-dev/kcp/sdk/apis/tenancy"
3836
"github.com/kcp-dev/kcp/sdk/apis/third_party/conditions/util/conditions"
3937
kcpclientset "github.com/kcp-dev/kcp/sdk/client/clientset/versioned/cluster"
4038
kcptesting "github.com/kcp-dev/kcp/sdk/testing"
@@ -129,38 +127,3 @@ func TestProtectedAPI(t *testing.T) {
129127
return resourceExists(resources, "tlsroutes")
130128
}, wait.ForeverTestTimeout, time.Millisecond*100, "consumer workspace %q discovery is missing tlsroutes resource", consumerPath)
131129
}
132-
133-
// TestProtectedAPIFromServiceExports is testing if we can access service exports
134-
// from root workspace. See https://github.com/kcp-dev/kcp/issues/2184 for more details.
135-
func TestProtectedAPIFromServiceExports(t *testing.T) {
136-
t.Parallel()
137-
framework.Suite(t, "control-plane")
138-
139-
server := kcptesting.SharedKcpServer(t)
140-
141-
ctx, cancel := context.WithCancel(context.Background())
142-
t.Cleanup(cancel)
143-
144-
rootWorkspace := logicalcluster.NewPath("root")
145-
cfg := server.BaseConfig(t)
146-
147-
kcpClusterClient, err := kcpclientset.NewForConfig(cfg)
148-
require.NoError(t, err, "failed to construct kcp cluster client for server")
149-
150-
t.Logf("Get tenancy APIExport from root workspace")
151-
tenanciesRoot, err := kcpClusterClient.ApisV1alpha2().APIExports().Cluster(rootWorkspace).Get(ctx, tenancy.GroupName, metav1.GetOptions{})
152-
require.NoError(t, err)
153-
154-
t.Logf("Construct VirtualWorkspace client")
155-
//nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
156-
vwURL := tenanciesRoot.Status.VirtualWorkspaces[0].URL
157-
cfgVW := server.RootShardSystemMasterBaseConfig(t)
158-
cfgVW.Host = vwURL
159-
160-
vwClient, err := kcpclientset.NewForConfig(cfgVW)
161-
require.NoError(t, err)
162-
163-
t.Logf("Make sure we can access tenancy API from VirtualWorkspace")
164-
_, err = vwClient.TenancyV1alpha1().Workspaces().List(ctx, metav1.ListOptions{})
165-
require.NoError(t, err)
166-
}

test/e2e/apibinding/apibinding_test.go

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net/url"
2424
"path"
2525
"reflect"
26+
"sort"
2627
"strings"
2728
"testing"
2829
"time"
@@ -49,6 +50,7 @@ import (
4950
apisv1alpha2 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha2"
5051
"github.com/kcp-dev/kcp/sdk/apis/core"
5152
corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
53+
tenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1"
5254
"github.com/kcp-dev/kcp/sdk/apis/third_party/conditions/util/conditions"
5355
kcpclientset "github.com/kcp-dev/kcp/sdk/client/clientset/versioned/cluster"
5456
kcptesting "github.com/kcp-dev/kcp/sdk/testing"
@@ -163,21 +165,47 @@ func TestAPIBinding(t *testing.T) {
163165
t.Parallel()
164166

165167
server := kcptesting.SharedKcpServer(t)
168+
cfg := server.BaseConfig(t)
166169

167170
ctx, cancel := context.WithCancel(context.Background())
168171
t.Cleanup(cancel)
169172

173+
t.Logf("Check if we can access shards")
174+
var shards *corev1alpha1.ShardList
175+
{
176+
kcpClusterClient, err := kcpclientset.NewForConfig(cfg)
177+
require.NoError(t, err, "failed to construct kcp cluster client for server")
178+
179+
shards, err = kcpClusterClient.Cluster(core.RootCluster.Path()).CoreV1alpha1().Shards().List(ctx, metav1.ListOptions{})
180+
require.NoError(t, err, "failed to list shards")
181+
}
182+
183+
t.Logf("Shards: %v", shards.Items)
184+
170185
orgPath, _ := framework.NewOrganizationFixture(t, server) //nolint:staticcheck // TODO: switch to NewWorkspaceFixture.
171-
provider1Path, provider1 := kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("service-provider-1"))
186+
var provider1Path, provider2Path logicalcluster.Path
187+
var provider1, provider2 *tenancyv1alpha1.Workspace
188+
if len(shards.Items) == 1 {
189+
provider1Path, provider1 = kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("service-provider-1"), kcptesting.WithShard(shards.Items[0].Name))
190+
provider2Path, provider2 = kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("service-provider-2"), kcptesting.WithShard(shards.Items[0].Name))
191+
} else {
192+
provider1Path, provider1 = kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("service-provider-1"), kcptesting.WithShard(shards.Items[0].Name))
193+
provider2Path, provider2 = kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("service-provider-2"), kcptesting.WithShard(shards.Items[1].Name))
194+
}
172195
provider1ClusterName := logicalcluster.Name(provider1.Spec.Cluster)
173-
provider2Path, provider2 := kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("service-provider-2"))
174196
provider2ClusterName := logicalcluster.Name(provider2.Spec.Cluster)
175-
consumer1Path, _ := kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("consumer-1-bound-against-1"))
176-
consumer2Path, _ := kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("consumer-2-bound-against-1"))
177-
consumer3Path, consumer3Workspace := kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("consumer-3-bound-against-2"))
178-
consumer3ClusterName := logicalcluster.Name(consumer3Workspace.Spec.Cluster)
179197

180-
cfg := server.BaseConfig(t)
198+
consumer1Path, _ := kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("consumer-1-bound-against-1"), kcptesting.WithShard(shards.Items[0].Name))
199+
var consumer2Path, consumer3Path logicalcluster.Path
200+
var consumer3Workspace *tenancyv1alpha1.Workspace
201+
if len(shards.Items) == 1 {
202+
consumer2Path, _ = kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("consumer-2-bound-against-1"), kcptesting.WithShard(shards.Items[0].Name))
203+
consumer3Path, consumer3Workspace = kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("consumer-3-bound-against-2"), kcptesting.WithShard(shards.Items[0].Name))
204+
} else {
205+
consumer2Path, _ = kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("consumer-2-bound-against-1"), kcptesting.WithShard(shards.Items[1].Name))
206+
consumer3Path, consumer3Workspace = kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("consumer-3-bound-against-2"), kcptesting.WithShard(shards.Items[1].Name))
207+
}
208+
consumer3ClusterName := logicalcluster.Name(consumer3Workspace.Spec.Cluster)
181209

182210
kcpClusterClient, err := kcpclientset.NewForConfig(cfg)
183211
require.NoError(t, err, "failed to construct kcp cluster client for server")
@@ -187,8 +215,6 @@ func TestAPIBinding(t *testing.T) {
187215

188216
shardVirtualWorkspaceURLs := sets.New[string]()
189217
t.Logf("Getting a list of VirtualWorkspaceURLs assigned to Shards")
190-
shards, err := kcpClusterClient.Cluster(core.RootCluster.Path()).CoreV1alpha1().Shards().List(ctx, metav1.ListOptions{})
191-
require.NoError(t, err)
192218
// Filtering out shards that are not schedulable
193219
var shardItems []corev1alpha1.Shard
194220
for _, s := range shards.Items {
@@ -365,25 +391,27 @@ func TestAPIBinding(t *testing.T) {
365391
expectedURLs = append(expectedURLs, u.String())
366392
}
367393

368-
t.Logf("Make sure the APIExport gets status.virtualWorkspaceURLs set")
394+
t.Logf("Make sure the APIExportEndpointSlice gets status.virtualWorkspaceURLs set")
369395
kcptestinghelpers.Eventually(t, func() (bool, string) {
370-
e, err := kcpClusterClient.Cluster(serviceProviderClusterName.Path()).ApisV1alpha2().APIExports().Get(ctx, exportName, metav1.GetOptions{})
396+
apiExportEndpointSlice, err := kcpClusterClient.Cluster(serviceProviderClusterName.Path()).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, exportName, metav1.GetOptions{})
371397
if err != nil {
372-
t.Logf("Unexpected error getting APIExport %s|%s: %v", serviceProviderClusterName.Path(), exportName, err)
398+
t.Logf("Unexpected error getting APIExportEndpointSlice %s|%s: %v", serviceProviderClusterName.Path(), exportName, err)
373399
}
374400

375401
var actualURLs []string
376-
//nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
377-
for _, u := range e.Status.VirtualWorkspaces {
402+
for _, u := range apiExportEndpointSlice.Status.APIExportEndpoints {
378403
actualURLs = append(actualURLs, u.URL)
379404
}
380-
405+
sort.Strings(expectedURLs)
406+
sort.Strings(actualURLs)
407+
t.Logf("Expected URLs: %v", expectedURLs)
408+
t.Logf("Actual URLs: %v", actualURLs)
381409
if !reflect.DeepEqual(expectedURLs, actualURLs) {
382410
return false, fmt.Sprintf("Unexpected URLs. Diff: %s", cmp.Diff(expectedURLs, actualURLs))
383411
}
384412

385413
return true, ""
386-
}, wait.ForeverTestTimeout, 100*time.Millisecond, "APIExport %s|%s didn't get status.virtualWorkspaceURLs set correctly",
414+
}, wait.ForeverTestTimeout, 100*time.Millisecond, "APIExportEndpointSlices %s|%s didn't get status.virtualWorkspaceURLs set correctly",
387415
serviceProviderClusterName, exportName)
388416
}
389417

@@ -438,15 +466,14 @@ func TestAPIBinding(t *testing.T) {
438466
t.Logf("=== Verify that %s|%s export virtual workspace shows cowboys", provider2Path, exportName)
439467
rawConfig, err := server.RawConfig()
440468
require.NoError(t, err)
441-
export2, err := kcpClusterClient.Cluster(provider2Path).ApisV1alpha2().APIExports().Get(ctx, exportName, metav1.GetOptions{})
469+
apiExportEndpointSlice2, err := kcpClusterClient.Cluster(provider2Path).ApisV1alpha1().APIExportEndpointSlices().Get(ctx, exportName, metav1.GetOptions{})
442470
require.NoError(t, err)
443471

444472
kcptestinghelpers.Eventually(t, func() (bool, string) {
445473
foundOnShards := 0
446474
var listErrs []error
447475

448-
//nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
449-
for _, vw := range export2.Status.VirtualWorkspaces {
476+
for _, vw := range apiExportEndpointSlice2.Status.APIExportEndpoints {
450477
vw2ClusterClient, err := kcpdynamic.NewForConfig(apiexportVWConfig(t, rawConfig, vw.URL))
451478
require.NoError(t, err)
452479

@@ -476,8 +503,7 @@ func TestAPIBinding(t *testing.T) {
476503
return false, "couldn't list via virtual workspaces because the user is not ready yet"
477504
}
478505

479-
//nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
480-
require.Equal(t, 1, foundOnShards, "cowboys not found exactly on one shard, but on %d/%d", foundOnShards, len(export2.Status.VirtualWorkspaces))
506+
require.Equal(t, 1, foundOnShards, "cowboys not found exactly on one shard, but on %d/%d", foundOnShards, len(apiExportEndpointSlice2.Status.APIExportEndpoints))
481507

482508
return true, ""
483509
}, wait.ForeverTestTimeout, 100*time.Millisecond, "expected to have cowboys exactly on one shard")

test/e2e/framework/util.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
"github.com/kcp-dev/kcp/pkg/authorization"
3333
bootstrappolicy "github.com/kcp-dev/kcp/pkg/authorization/bootstrap"
3434
"github.com/kcp-dev/kcp/pkg/server"
35-
apisv1alpha2 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha2"
35+
apisv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1"
3636
tenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1"
3737
kcpclientset "github.com/kcp-dev/kcp/sdk/client/clientset/versioned/cluster"
3838
testing2 "github.com/kcp-dev/kcp/sdk/testing"
@@ -84,12 +84,10 @@ func VirtualWorkspaceURL(ctx context.Context, kcpClusterClient kcpclientset.Clus
8484
}
8585

8686
// ExportVirtualWorkspaceURLs returns the URLs of the virtual workspaces of the
87-
// given APIExport.
88-
func ExportVirtualWorkspaceURLs(export *apisv1alpha2.APIExport) []string {
89-
//nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
90-
urls := make([]string, 0, len(export.Status.VirtualWorkspaces))
91-
//nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
92-
for _, vw := range export.Status.VirtualWorkspaces {
87+
// given APIExportEndpointSlice.
88+
func ExportVirtualWorkspaceURLs(export *apisv1alpha1.APIExportEndpointSlice) []string {
89+
urls := make([]string, 0, len(export.Status.APIExportEndpoints))
90+
for _, vw := range export.Status.APIExportEndpoints {
9391
urls = append(urls, vw.URL)
9492
}
9593
return urls

test/e2e/reconciler/apiexportendpointslice/apiexportendpointslice_test.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -275,19 +275,6 @@ func TestAPIBindingEndpointSlicesSharded(t *testing.T) {
275275
}
276276
}
277277

278-
// TODO(mjudeikis): This will be deprecated when we deperecate APIExport urls.
279-
t.Logf("Check that APIExport has 2 virtual workspaces")
280-
{
281-
kcpClusterClient, err := kcpclientset.NewForConfig(cfg)
282-
require.NoError(t, err, "failed to construct kcp cluster client for server")
283-
284-
apiExport, err := kcpClusterClient.Cluster(providerPath).ApisV1alpha2().APIExports().Get(ctx, "today-cowboys", metav1.GetOptions{})
285-
require.NoError(t, err)
286-
287-
//nolint:staticcheck // SA1019 VirtualWorkspaces is deprecated but not removed yet
288-
require.Len(t, apiExport.Status.VirtualWorkspaces, len(shards.Items))
289-
}
290-
291278
t.Logf("Create a topology PartitionSet for the providers")
292279
var partition *topologyv1alpha1.Partition
293280
{

0 commit comments

Comments
 (0)