|
| 1 | +/* |
| 2 | +Copyright 2025 The KCP 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 workspace |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | + |
| 27 | + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/util/wait" |
| 29 | + |
| 30 | + kcpkubernetesclientset "github.com/kcp-dev/client-go/kubernetes" |
| 31 | + |
| 32 | + "github.com/kcp-dev/kcp/pkg/server/filters" |
| 33 | + "github.com/kcp-dev/kcp/sdk/apis/core" |
| 34 | + kcpclientset "github.com/kcp-dev/kcp/sdk/client/clientset/versioned/cluster" |
| 35 | + kcptesting "github.com/kcp-dev/kcp/sdk/testing" |
| 36 | + kcptestinghelpers "github.com/kcp-dev/kcp/sdk/testing/helpers" |
| 37 | + "github.com/kcp-dev/kcp/test/e2e/framework" |
| 38 | +) |
| 39 | + |
| 40 | +func TestInactiveLogicalCluster(t *testing.T) { |
| 41 | + t.Parallel() |
| 42 | + framework.Suite(t, "control-plane") |
| 43 | + |
| 44 | + // TODO(ntnn): Repalce with t.Context in go1.24 |
| 45 | + ctx, cancel := context.WithCancel(context.Background()) |
| 46 | + t.Cleanup(cancel) |
| 47 | + |
| 48 | + server := kcptesting.SharedKcpServer(t) |
| 49 | + cfg := server.BaseConfig(t) |
| 50 | + orgPath, _ := kcptesting.NewWorkspaceFixture(t, server, core.RootCluster.Path(), kcptesting.WithType(core.RootCluster.Path(), "organization")) |
| 51 | + |
| 52 | + kcpClient, err := kcpclientset.NewForConfig(cfg) |
| 53 | + require.NoError(t, err) |
| 54 | + kubeClient, err := kcpkubernetesclientset.NewForConfig(cfg) |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + t.Log("Get the logicalcluster") |
| 58 | + lc, err := kcpClient.Cluster(orgPath).CoreV1alpha1().LogicalClusters().Get(ctx, "cluster", v1.GetOptions{}) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + t.Log("Mark the logicalcluster as inactive") |
| 62 | + lc.Annotations[filters.InactiveAnnotation] = "true" |
| 63 | + lc, err = kcpClient.Cluster(orgPath).CoreV1alpha1().LogicalClusters().Update(ctx, lc, v1.UpdateOptions{}) |
| 64 | + require.NoError(t, err) |
| 65 | + |
| 66 | + t.Log("Verify that normal requests fail") |
| 67 | + kcptestinghelpers.Eventually(t, func() (bool, string) { |
| 68 | + _, err := kubeClient.Cluster(orgPath).CoreV1().Namespaces().List(ctx, v1.ListOptions{}) |
| 69 | + if err == nil { |
| 70 | + return false, "expected error when accessing an inactive logical cluster" |
| 71 | + } |
| 72 | + return true, "" |
| 73 | + }, wait.ForeverTestTimeout, time.Millisecond*100) |
| 74 | + |
| 75 | + t.Log("Remove inactive annotation again") |
| 76 | + delete(lc.Annotations, filters.InactiveAnnotation) |
| 77 | + _, err = kcpClient.Cluster(orgPath).CoreV1alpha1().LogicalClusters().Update(ctx, lc, v1.UpdateOptions{}) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + t.Log("Verify that normal requests succeed again") |
| 81 | + assert.NoError(t, err, "expected no error when accessing an active logical cluster") |
| 82 | + kcptestinghelpers.Eventually(t, func() (bool, string) { |
| 83 | + _, err := kubeClient.Cluster(orgPath).CoreV1().Namespaces().List(ctx, v1.ListOptions{}) |
| 84 | + if err != nil { |
| 85 | + return false, err.Error() |
| 86 | + } |
| 87 | + return true, "" |
| 88 | + }, wait.ForeverTestTimeout, time.Millisecond*100) |
| 89 | +} |
0 commit comments