Skip to content

Commit

Permalink
Merge pull request #3089 from mjudeikis/mjudeikis/index.nit.test
Browse files Browse the repository at this point in the history
🌱 add mount test into index
  • Loading branch information
kcp-ci-bot committed Mar 12, 2024
2 parents 39c8843 + d67f101 commit f8f0a65
Showing 1 changed file with 112 additions and 67 deletions.
179 changes: 112 additions & 67 deletions pkg/index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestLookup(t *testing.T) {
expectedShard string
expectedCluster logicalcluster.Name
expectFound bool
expectedURL string
}{
{
name: "an empty indexer is usable",
Expand All @@ -55,177 +56,210 @@ func TestLookup(t *testing.T) {
name: "a ns must be scheduled to be considered",
initialShardsToUpsert: []shardStub{{
name: "root",
url: "https://root.io",
url: "https://root.kcp.dev",
}},
initialWorkspacesToUpsert: map[string][]*tenancyv1alpha1.Workspace{
"root": {func() *tenancyv1alpha1.Workspace {
ws := newWorkspace("org", "root", "34")
ws := newWorkspace("org", "root", "organization")
ws.Status.Phase = corev1alpha1.LogicalClusterPhaseScheduling
return ws
}()},
},
initialLogicalClustersToUpsert: map[string][]*corev1alpha1.LogicalCluster{
"root": {newLogicalCluster("root"), newLogicalCluster("34")},
"root": {newLogicalCluster("root"), newLogicalCluster("organization")},
},
targetPath: logicalcluster.NewPath("root:org"),
targetPath: logicalcluster.NewPath("root:organization"),
},
{
name: "single shard: a logical cluster for root:org workspace is found",
name: "single shard: a logical cluster for root:organization workspace is found",
initialShardsToUpsert: []shardStub{{
name: "root",
url: "https://root.io",
url: "https://root.kcp.dev",
}},
initialWorkspacesToUpsert: map[string][]*tenancyv1alpha1.Workspace{
"root": {newWorkspace("org", "root", "34"), newWorkspace("rh", "34", "2v")},
"root": {newWorkspace("org", "root", "one"), newWorkspace("rh", "one", "two")},
},
initialLogicalClustersToUpsert: map[string][]*corev1alpha1.LogicalCluster{
"root": {newLogicalCluster("root"), newLogicalCluster("34"), newLogicalCluster("2v")},
"root": {newLogicalCluster("root"), newLogicalCluster("one"), newLogicalCluster("two")},
},
targetPath: logicalcluster.NewPath("root:org"),
expectFound: true,
expectedCluster: "34",
expectedCluster: "one",
expectedShard: "root",
},
{
name: "multiple shards: a logical cluster for root:org workspace is found",
initialShardsToUpsert: []shardStub{
{
name: "root",
url: "https://root.io",
url: "https://root.kcp.dev",
},
{
name: "amber",
url: "https://amber.io",
name: "beta",
url: "https://beta.kcp.dev",
},
},
initialWorkspacesToUpsert: map[string][]*tenancyv1alpha1.Workspace{
"root": {newWorkspace("org", "root", "34"), newWorkspace("rh", "34", "2v")},
"root": {newWorkspace("org", "root", "one"), newWorkspace("rh", "one", "two")},
},
initialLogicalClustersToUpsert: map[string][]*corev1alpha1.LogicalCluster{
"root": {newLogicalCluster("root")},
"amber": {newLogicalCluster("34"), newLogicalCluster("2v")},
"root": {newLogicalCluster("root")},
"beta": {newLogicalCluster("one"), newLogicalCluster("two")},
},
targetPath: logicalcluster.NewPath("root:org"),
expectFound: true,
expectedCluster: "34",
expectedShard: "amber",
expectedCluster: "one",
expectedShard: "beta",
},
{
name: "multiple shards: a logical cluster for root:org:rh workspace is found",
initialShardsToUpsert: []shardStub{
{
name: "root",
url: "https://root.io",
url: "https://root.kcp.dev",
},
{
name: "amber",
url: "https://amber.io",
name: "beta",
url: "https://beta.kcp.dev",
},
{
name: "silver",
url: "https://silver.io",
name: "gama",
url: "https://gama.kcp.dev",
},
},
initialWorkspacesToUpsert: map[string][]*tenancyv1alpha1.Workspace{
"root": {newWorkspace("org", "root", "34")},
"amber": {newWorkspace("rh", "34", "2v")},
"root": {newWorkspace("org", "root", "one")},
"beta": {newWorkspace("rh", "one", "two")},
},
initialLogicalClustersToUpsert: map[string][]*corev1alpha1.LogicalCluster{
"root": {newLogicalCluster("root")},
"amber": {newLogicalCluster("34")},
"silver": {newLogicalCluster("2v")},
"root": {newLogicalCluster("root")},
"beta": {newLogicalCluster("one")},
"gama": {newLogicalCluster("two")},
},
targetPath: logicalcluster.NewPath("root:org:rh"),
expectFound: true,
expectedCluster: "2v",
expectedShard: "silver",
expectedCluster: "two",
expectedShard: "gama",
},
{
name: "multiple shards: a logical cluster for 34:rh workspace is found",
name: "multiple shards: a logical cluster for one:rh workspace is found",
initialShardsToUpsert: []shardStub{
{
name: "root",
url: "https://root.io",
url: "https://root.kcp.dev",
},
{
name: "amber",
url: "https://amber.io",
name: "beta",
url: "https://beta.kcp.dev",
},
{
name: "silver",
url: "https://silver.io",
name: "gama",
url: "https://gama.kcp.dev",
},
},
initialWorkspacesToUpsert: map[string][]*tenancyv1alpha1.Workspace{
"root": {newWorkspace("org", "root", "34")},
"amber": {newWorkspace("rh", "34", "2v")},
"root": {newWorkspace("org", "root", "one")},
"beta": {newWorkspace("rh", "one", "two")},
},
initialLogicalClustersToUpsert: map[string][]*corev1alpha1.LogicalCluster{
"root": {newLogicalCluster("root")},
"amber": {newLogicalCluster("34")},
"silver": {newLogicalCluster("2v")},
"root": {newLogicalCluster("root")},
"beta": {newLogicalCluster("one")},
"gama": {newLogicalCluster("two")},
},
targetPath: logicalcluster.NewPath("34:rh"),
targetPath: logicalcluster.NewPath("one:rh"),
expectFound: true,
expectedCluster: "2v",
expectedShard: "silver",
expectedCluster: "two",
expectedShard: "gama",
},
{
name: "multiple shards: a logical cluster for 666:rh workspace is NOT found",
name: "multiple shards: a logical cluster for does-not-exists:rh workspace is NOT found",
initialShardsToUpsert: []shardStub{
{
name: "root",
url: "https://root.io",
url: "https://root.kcp.dev",
},
{
name: "amber",
url: "https://amber.io",
name: "beta",
url: "https://beta.kcp.dev",
},
{
name: "silver",
url: "https://silver.io",
name: "gama",
url: "https://gama.kcp.dev",
},
},
initialWorkspacesToUpsert: map[string][]*tenancyv1alpha1.Workspace{
"root": {newWorkspace("org", "root", "34")},
"amber": {newWorkspace("rh", "34", "2v")},
"root": {newWorkspace("org", "root", "one")},
"beta": {newWorkspace("rh", "one", "two")},
},
initialLogicalClustersToUpsert: map[string][]*corev1alpha1.LogicalCluster{
"root": {newLogicalCluster("root")},
"amber": {newLogicalCluster("34")},
"silver": {newLogicalCluster("2v")},
"root": {newLogicalCluster("root")},
"beta": {newLogicalCluster("one")},
"gama": {newLogicalCluster("two")},
},
targetPath: logicalcluster.NewPath("666:rh"),
targetPath: logicalcluster.NewPath("does-not-exists:rh"),
expectFound: false,
},
{
name: "multiple shards: a logical cluster for root:34:rh workspace is NOT found",
name: "multiple shards: a logical cluster for root:one:rh workspace is NOT found",
initialShardsToUpsert: []shardStub{
{
name: "root",
url: "https://root.io",
url: "https://root.kcp.dev",
},
{
name: "amber",
url: "https://amber.io",
name: "beta",
url: "https://beta.kcp.dev",
},
{
name: "silver",
url: "https://silver.io",
name: "gama",
url: "https://gama.kcp.dev",
},
},
initialWorkspacesToUpsert: map[string][]*tenancyv1alpha1.Workspace{
"root": {newWorkspace("org", "root", "34")},
"amber": {newWorkspace("rh", "34", "2v")},
"root": {newWorkspace("org", "root", "one")},
"beta": {newWorkspace("rh", "one", "two")},
},
initialLogicalClustersToUpsert: map[string][]*corev1alpha1.LogicalCluster{
"root": {newLogicalCluster("root")},
"amber": {newLogicalCluster("34")},
"silver": {newLogicalCluster("2v")},
"root": {newLogicalCluster("root")},
"beta": {newLogicalCluster("one")},
"gama": {newLogicalCluster("two")},
},
targetPath: logicalcluster.NewPath("root:34:rh"),
targetPath: logicalcluster.NewPath("root:one:rh"),
expectFound: false,
},
{
name: "multiple shards: a logical cluster for one:rh workspace is found",
initialShardsToUpsert: []shardStub{
{
name: "root",
url: "https://root.kcp.dev",
},
{
name: "beta",
url: "https://beta.kcp.dev",
},
{
name: "gama",
url: "https://gama.kcp.dev",
},
},
initialWorkspacesToUpsert: map[string][]*tenancyv1alpha1.Workspace{
"root": {newWorkspace("org", "root", "one")},
"beta": {newWorkspaceWithAnnotation("rh", "one", "two", map[string]string{
"experimental.tenancy.kcp.io/mount": `{"spec":{"ref":{"kind":"KubeCluster","name":"prod-cluster","apiVersion":"proxy.kcp.dev/v1alpha1"}},"status":{"phase":"Ready","url":"https://kcp.dev.local/services/custom-url/proxy"}}`,
})},
},
initialLogicalClustersToUpsert: map[string][]*corev1alpha1.LogicalCluster{
"root": {newLogicalCluster("root")},
"beta": {newLogicalCluster("one")},
"gama": {newLogicalCluster("two")},
},
targetPath: logicalcluster.NewPath("one:rh"),
expectFound: true,
expectedCluster: "",
expectedShard: "",
expectedURL: "https://kcp.dev.local/services/custom-url/proxy",
},
}

for _, scenario := range scenarios {
Expand Down Expand Up @@ -261,6 +295,9 @@ func TestLookup(t *testing.T) {
if r.Cluster != scenario.expectedCluster {
t.Errorf("unexpected cluster = %v, for path = %v, expected = %v", r.Cluster, scenario.targetPath, scenario.expectedCluster)
}
if r.URL != scenario.expectedURL {
t.Errorf("unexpected url = %v, for path = %v, expected = %v", r.URL, scenario.targetPath, scenario.expectedURL)
}
})
}
}
Expand Down Expand Up @@ -448,6 +485,14 @@ func newWorkspace(name, cluster, scheduledCluster string) *tenancyv1alpha1.Works
}
}

func newWorkspaceWithAnnotation(name, cluster, scheduledCluster string, annotations map[string]string) *tenancyv1alpha1.Workspace {
ws := newWorkspace(name, cluster, scheduledCluster)
for k, v := range annotations {
ws.Annotations[k] = v
}
return ws
}

func newLogicalCluster(cluster string) *corev1alpha1.LogicalCluster {
return &corev1alpha1.LogicalCluster{
ObjectMeta: metav1.ObjectMeta{Name: "cluster", Annotations: map[string]string{"kcp.io/cluster": cluster}},
Expand Down

0 comments on commit f8f0a65

Please sign in to comment.