~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/socialweb-computer-console

console.socialweb.computer: kcp, kubectl and ssh compiled to wasm in a browser tab with a terminal; a kcp fork

0
0
main
79 lines · 3.0 KB Raw History
/*
Copyright 2025 The kcp 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 builder

import (
	"testing"

	"github.com/stretchr/testify/require"

	genericapirequest "k8s.io/apiserver/pkg/endpoints/request"

	"github.com/kcp-dev/virtual-workspace-framework/pkg/dynamic/context"
)

func TestDigestUrl(t *testing.T) {
	t.Parallel()
	rootPathPrefix := "/services/replication/"
	testCases := []struct {
		urlPath             string
		expectedAccept      bool
		expectedCluster     genericapirequest.Cluster
		expectedKey         context.APIDomainKey
		expectedLogicalPath string
	}{
		{
			urlPath:             "/services/replication/my-cluster/my-clustercachedresource/clusters/my-cluster/apis",
			expectedAccept:      true,
			expectedKey:         "my-cluster/my-clustercachedresource",
			expectedCluster:     genericapirequest.Cluster{Name: "my-cluster", Wildcard: false},
			expectedLogicalPath: "/services/replication/my-cluster/my-clustercachedresource/clusters/my-cluster",
		},
		{
			urlPath:             "/services/replication/my-cluster/my-clustercachedresource/clusters/my-cluster",
			expectedAccept:      true,
			expectedKey:         "my-cluster/my-clustercachedresource",
			expectedCluster:     genericapirequest.Cluster{Name: "my-cluster", Wildcard: false},
			expectedLogicalPath: "/services/replication/my-cluster/my-clustercachedresource/clusters/my-cluster",
		},
		{
			urlPath:             "/services/replication/my-cluster/my-clustercachedresource/clusters/other-cluster",
			expectedAccept:      true,
			expectedKey:         "my-cluster/my-clustercachedresource",
			expectedCluster:     genericapirequest.Cluster{Name: "other-cluster", Wildcard: false},
			expectedLogicalPath: "/services/replication/my-cluster/my-clustercachedresource/clusters/other-cluster",
		},
		{
			urlPath:             "/services/replication/my-cluster/my-clustercachedresource/clusters",
			expectedAccept:      false,
			expectedKey:         "",
			expectedCluster:     genericapirequest.Cluster{},
			expectedLogicalPath: "",
		},
	}

	for _, tc := range testCases {
		t.Run(tc.urlPath, func(t *testing.T) {
			t.Parallel()
			clusterName, key, logicalPath, accepted := digestURL(tc.urlPath, rootPathPrefix)
			require.Equal(t, tc.expectedAccept, accepted, "Accepted should match expected value")
			require.Equal(t, tc.expectedKey, key, "Key should match expected value")
			require.Equal(t, tc.expectedCluster, clusterName, "cluster name should match expected value")
			require.Equal(t, tc.expectedLogicalPath, logicalPath, "LogicalPath should match expected value")
		})
	}
}