@@ -19,9 +19,10 @@ package authentication
1919import (
2020 "context"
2121 "errors"
22- "fmt"
2322 "testing"
2423
24+ "github.com/stretchr/testify/require"
25+
2526 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
2728 "github.com/kcp-dev/logicalcluster/v3"
@@ -32,8 +33,11 @@ import (
3233 conditionsv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/third_party/conditions/apis/conditions/v1alpha1"
3334)
3435
35- func TestDeleteShard (t * testing.T ) {
36- const shardName = "shard-1"
36+ func TestCrossShardWorkspaceType (t * testing.T ) {
37+ const (
38+ shardName = "shard-1"
39+ teamCluster = "logicalteamcluster"
40+ )
3741
3842 ctx , cancel := context .WithCancelCause (context .Background ())
3943 defer cancel (errors .New ("test has ended" ))
@@ -52,171 +56,21 @@ func TestDeleteShard(t *testing.T) {
5256 authIndex .UpsertWorkspaceType ("root" , newWorkspaceType ("custom-type" , "root" ))
5357
5458 // setup the team workspace (ws is on root shard in root workspace, cluster is on the 2nd shard)
55- ws := newWorkspace ("team1" , "root" , "logicalteamcluster" )
59+ ws := newWorkspace ("team1" , "root" , teamCluster )
5660 ws .Spec .Type = & tenancyv1alpha1.WorkspaceTypeReference {
5761 Name : "custom-type" ,
5862 Path : "root" ,
5963 }
6064 clusterIndex .UpsertWorkspace ("root" , ws )
61- clusterIndex .UpsertLogicalCluster (shardName , newLogicalCluster ("logicalteamcluster" , "root:custom-type" ))
65+ clusterIndex .UpsertLogicalCluster (shardName , newLogicalCluster (teamCluster , "root:custom-type" ))
6266
6367 r , found := clusterIndex .Lookup (logicalcluster .NewPath ("root:team1" ))
64- fmt .Printf ("result: %+v\n " , r )
65- fmt .Printf ("found: %v\n " , found )
68+ require .True (t , found )
69+ require .Equal (t , shardName , r .Shard )
70+ require .Equal (t , teamCluster , r .Cluster .String ())
71+ require .Equal (t , "root:custom-type" , r .Type .String ())
6672}
6773
68- // func TestDeleteLogicalCluster(t *testing.T) {
69- // target := New(nil)
70-
71- // // ensure deleting not existent logical cluster won't blow up
72- // target.DeleteLogicalCluster("root", newLogicalCluster("34"))
73-
74- // target.UpsertShard("root", "https://root.io")
75- // target.UpsertWorkspace("root", newWorkspace("org", "root", "34"))
76- // target.UpsertLogicalCluster("root", newLogicalCluster("root"))
77- // target.UpsertLogicalCluster("root", newLogicalCluster("34"))
78-
79- // r, found := target.Lookup(logicalcluster.NewPath("root:org"))
80- // validateLookupOutput(t, logicalcluster.NewPath("root:org"), r.Shard, r.Cluster, r.URL, found, "root", "34", "", true)
81-
82- // // ensure that after deleting the logical cluster it cannot be looked up
83- // target.DeleteLogicalCluster("root", newLogicalCluster("34"))
84-
85- // r, found = target.Lookup(logicalcluster.NewPath("root:org"))
86- // validateLookupOutput(t, logicalcluster.NewPath("root:org"), r.Shard, r.Cluster, r.URL, found, "", "", "", false)
87-
88- // r, found = target.Lookup(logicalcluster.NewPath("root"))
89- // validateLookupOutput(t, logicalcluster.NewPath("root:org"), r.Shard, r.Cluster, r.URL, found, "root", "root", "", true)
90- // }
91-
92- // func TestDeleteWorkspace(t *testing.T) {
93- // target := New(nil)
94-
95- // // ensure deleting not existent workspace won't blow up
96- // target.DeleteWorkspace("root", newWorkspace("org", "root", "34"))
97-
98- // target.UpsertShard("root", "https://root.io")
99- // target.UpsertWorkspace("root", newWorkspace("org", "root", "34"))
100- // target.UpsertWorkspace("root", newWorkspace("org1", "root", "43"))
101- // target.UpsertLogicalCluster("root", newLogicalCluster("root"))
102- // target.UpsertLogicalCluster("root", newLogicalCluster("34"))
103- // target.UpsertLogicalCluster("root", newLogicalCluster("43"))
104-
105- // r, found := target.Lookup(logicalcluster.NewPath("root:org"))
106- // validateLookupOutput(t, logicalcluster.NewPath("root:org"), r.Shard, r.Cluster, r.URL, found, "root", "34", "", true)
107-
108- // target.DeleteWorkspace("root", newWorkspace("org", "root", "34"))
109-
110- // r, found = target.Lookup(logicalcluster.NewPath("root:org"))
111- // validateLookupOutput(t, logicalcluster.NewPath("root:org"), r.Shard, r.Cluster, r.URL, found, "", "", "", false)
112-
113- // r, found = target.Lookup(logicalcluster.NewPath("root:org1"))
114- // validateLookupOutput(t, logicalcluster.NewPath("root:org"), r.Shard, r.Cluster, r.URL, found, "root", "43", "", true)
115- // }
116-
117- // func TestUpsertLogicalCluster(t *testing.T) {
118- // target := New(nil)
119-
120- // target.UpsertShard("root", "https://root.io")
121- // target.UpsertShard("amber", "https://amber.io")
122- // target.UpsertWorkspace("root", newWorkspace("org", "root", "34"))
123- // target.UpsertLogicalCluster("root", newLogicalCluster("root"))
124- // target.UpsertLogicalCluster("root", newLogicalCluster("34"))
125-
126- // r, found := target.Lookup(logicalcluster.NewPath("root:org"))
127- // validateLookupOutput(t, logicalcluster.NewPath("root:org"), r.Shard, r.Cluster, r.URL, found, "root", "34", "", true)
128-
129- // target.UpsertLogicalCluster("amber", newLogicalCluster("34"))
130- // r, found = target.Lookup(logicalcluster.NewPath("root:org"))
131- // validateLookupOutput(t, logicalcluster.NewPath("root:org"), r.Shard, r.Cluster, r.URL, found, "amber", "34", "", true)
132- // }
133-
134- // // Since LookupURL uses Lookup method the following test is just a smoke tests.
135- // func TestLookupURL(t *testing.T) {
136- // target := New(nil)
137-
138- // target.UpsertShard("root", "https://root.io")
139- // target.UpsertWorkspace("root", newWorkspace("org", "root", "34"))
140- // target.UpsertLogicalCluster("root", newLogicalCluster("root"))
141- // target.UpsertLogicalCluster("root", newLogicalCluster("34"))
142-
143- // r, found := target.LookupURL(logicalcluster.NewPath("root:org"))
144- // if !found {
145- // t.Fatalf("expected to find a URL for %q path", "root:org")
146- // }
147- // if r.URL != "https://root.io/clusters/34" {
148- // t.Fatalf("unexpected url. returned = %v, expected = %v for %q path", r.URL, "https://root.io/clusters/34", "root:org")
149- // }
150-
151- // r, found = target.LookupURL(logicalcluster.NewPath("root:org:rh"))
152- // if found {
153- // t.Fatalf("didn't expected to find a URL for %q path", "root:org:rh")
154- // }
155- // if len(r.URL) > 0 {
156- // t.Fatalf("unexpected url = %v returned for %q path", r.URL, "root:org:rh")
157- // }
158- // }
159-
160- // func TestUpsertShard(t *testing.T) {
161- // target := New(nil)
162-
163- // target.UpsertShard("root", "https://root.io")
164- // target.UpsertWorkspace("root", newWorkspace("org", "root", "34"))
165- // target.UpsertLogicalCluster("root", newLogicalCluster("root"))
166- // target.UpsertLogicalCluster("root", newLogicalCluster("34"))
167-
168- // r, found := target.LookupURL(logicalcluster.NewPath("root:org"))
169- // if !found {
170- // t.Fatalf("expected to find a URL for %q path", "root:org")
171- // }
172- // if r.URL != "https://root.io/clusters/34" {
173- // t.Fatalf("unexpected url = %v returned, expected = %v for %q path", r.URL, "https://root.io/clusters/34", "root:org")
174- // }
175-
176- // target.UpsertShard("root", "https://new-root.io")
177- // r, found = target.LookupURL(logicalcluster.NewPath("root:org"))
178- // if !found {
179- // t.Fatalf("expected to find a URL for %q path", "root:org")
180- // }
181- // if r.URL != "https://new-root.io/clusters/34" {
182- // t.Fatalf("unexpected url = %v returned, expected = %v for %q path", r.URL, "https://new-root.io/clusters/34", "root:org")
183- // }
184- // }
185-
186- // func TestUpsertWorkspace(t *testing.T) {
187- // target := New(nil)
188-
189- // target.UpsertShard("root", "https://root.io")
190- // target.UpsertWorkspace("root", newWorkspace("org", "root", "34"))
191- // target.UpsertLogicalCluster("root", newLogicalCluster("root"))
192- // target.UpsertLogicalCluster("root", newLogicalCluster("34"))
193- // target.UpsertLogicalCluster("root", newLogicalCluster("44"))
194-
195- // r, found := target.Lookup(logicalcluster.NewPath("root:org"))
196- // validateLookupOutput(t, logicalcluster.NewPath("root:org"), r.Shard, r.Cluster, r.URL, found, "root", "34", "", true)
197-
198- // target.UpsertWorkspace("root", newWorkspace("org", "root", "44"))
199- // r, found = target.Lookup(logicalcluster.NewPath("root:org"))
200- // validateLookupOutput(t, logicalcluster.NewPath("root:org"), r.Shard, r.Cluster, r.URL, found, "root", "44", "", true)
201- // }
202-
203- // func validateLookupOutput(t *testing.T, path logicalcluster.Path, shard string, cluster logicalcluster.Name, url string, found bool, expectedShard string, expectedCluster logicalcluster.Name, expectedURL string, expectToFind bool) {
204- // t.Helper()
205-
206- // if found != expectToFind {
207- // t.Fatalf("found != expectedToFind, for %q path", path)
208- // }
209- // if cluster != expectedCluster {
210- // t.Fatalf("unexpected logical cluster = %v, expected = %v for %q path", cluster, expectedCluster, path)
211- // }
212- // if shard != expectedShard {
213- // t.Fatalf("unexpected shard = %v, expected = %v, for %q path", shard, expectedShard, path)
214- // }
215- // if url != expectedURL {
216- // t.Fatalf("unexpected url = %v, expected = %v, for %q path", url, expectedURL, path)
217- // }
218- // }
219-
22074func newWorkspaceType (name , cluster string ) * tenancyv1alpha1.WorkspaceType {
22175 return & tenancyv1alpha1.WorkspaceType {
22276 ObjectMeta : metav1.ObjectMeta {Name : name , Annotations : map [string ]string {"kcp.io/cluster" : cluster }},
@@ -232,27 +86,11 @@ func newWorkspace(name, cluster, scheduledCluster string) *tenancyv1alpha1.Works
23286 }
23387}
23488
235- func newWorkspaceWithMount (name , cluster , scheduledCluster string , ref tenancyv1alpha1.ObjectReference ) * tenancyv1alpha1.Workspace {
236- ws := newWorkspace (name , cluster , scheduledCluster )
237- ws .Spec .Mount = & tenancyv1alpha1.Mount {Reference : ref }
238- return ws
239- }
240-
24189func WithCondition (ws * tenancyv1alpha1.Workspace , condition conditionsv1alpha1.Condition ) * tenancyv1alpha1.Workspace {
24290 ws .Status .Conditions = []conditionsv1alpha1.Condition {condition }
24391 return ws
24492}
24593
246- func withPhase (ws * tenancyv1alpha1.Workspace , phase corev1alpha1.LogicalClusterPhaseType ) * tenancyv1alpha1.Workspace {
247- ws .Status .Phase = phase
248- return ws
249- }
250-
251- func withURL (ws * tenancyv1alpha1.Workspace , url string ) * tenancyv1alpha1.Workspace {
252- ws .Spec .URL = url
253- return ws
254- }
255-
25694func newLogicalCluster (cluster string , fqType string ) * corev1alpha1.LogicalCluster {
25795 return & corev1alpha1.LogicalCluster {
25896 ObjectMeta : metav1.ObjectMeta {
0 commit comments