Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions actions/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
SteveRestartAgeSort SteveFeature = "Pod restart-age sorting"
SteveNamespacedPatch SteveFeature = "namespaced PATCH namespace fix"
SteveReplicaSetAssociatedData SteveFeature = "ReplicaSet associated Pod data"
SteveAdditionalFieldIndexes SteveFeature = "CRD group, singular name, and resource description indexes"
SteveBasicSummary SteveFeature = "basic SQL summaries"
SteveArrayMembershipFilters SteveFeature = "CONTAINS and NOTCONTAINS filters"
StevePodRestartIndexes SteveFeature = "Pod nested restart indexes"
Expand Down Expand Up @@ -54,6 +55,7 @@ var steveFeatureRules = map[SteveFeature]steveFeatureRule{
SteveRestartAgeSort: {minimum: "v0.9.6"},
SteveNamespacedPatch: {minimum: "v0.9.15"},
SteveReplicaSetAssociatedData: {minimum: "v0.10.1"},
SteveAdditionalFieldIndexes: {minimum: "v0.10.8"},
SteveBasicSummary: {minimum: "v0.8.1"},
SteveArrayMembershipFilters: {minimum: "v0.8.18"},
StevePodRestartIndexes: {minimum: "v0.8.15"},
Expand Down
31 changes: 31 additions & 0 deletions tests/indexedfields/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,34 @@ func configMap(keys int) map[string]any {
}
return map[string]any{"apiVersion": "v1", "kind": "ConfigMap", "data": data}
}

const (
crdResource = "apiextensions.k8s.io.customresourcedefinition"
descriptionAnnotation = "field.cattle.io/description"
descriptionField = "metadata.annotations[" + descriptionAnnotation + "]"
)

func customResourceDefinition(plural, group, singular string) map[string]any {
return map[string]any{
"apiVersion": "apiextensions.k8s.io/v1", "kind": "CustomResourceDefinition",
"metadata": map[string]any{"name": plural + "." + group},
"spec": map[string]any{
"group": group, "scope": "Namespaced",
"names": map[string]any{"plural": plural, "singular": singular, "kind": "Indexed" + plural},
"versions": []any{map[string]any{
"name": "v1", "served": true, "storage": true,
"schema": map[string]any{"openAPIV3Schema": map[string]any{"type": "object"}},
}},
},
}
}

func withDescription(body map[string]any, description string) map[string]any {
metadata, _ := body["metadata"].(map[string]any)
if metadata == nil {
metadata = make(map[string]any)
body["metadata"] = metadata
}
metadata["annotations"] = map[string]any{descriptionAnnotation: description}
return body
}
149 changes: 116 additions & 33 deletions tests/indexedfields/indexedfields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package indexedfields
import (
"fmt"
"net/url"
"strconv"
"testing"

"github.com/rancher/steve-tests/actions"
Expand All @@ -12,17 +13,20 @@ import (
// Add a row for a new indexed field. Supply two valid resource bodies whose
// field values sort low < high under the index's type, and an update that changes low to high.
// The same contract checks filtering, both sort directions, and index refresh.
// Immutable fields omit update and check index refresh after deletion instead.
func TestSQLIndexedFields(t *testing.T) {
t.Parallel()
configMapLow, configMapHigh := 2, 10
group := actions.UniqueName(t, "indexed") + ".steve-tests.invalid"
lowGroup, highGroup := "alpha."+group, "bravo."+group
if !actions.SteveSupports(actions.SteveNumericConfigMapCount) {
configMapLow, configMapHigh = 10, 2
}
tests := []struct {
name, resource, field string
low, high map[string]any
lowValue, highValue string
update func(map[string]any) error
update, remove func(map[string]any) error
feature actions.SteveFeature
}{
// HPA replica indexes use TEXT, so "10" sorts before "2".
Expand All @@ -31,15 +35,15 @@ func TestSQLIndexedFields(t *testing.T) {
field: "spec.minReplicas",
low: hpa(10, 20), high: hpa(2, 20),
lowValue: "10", highValue: "2",
update: updateSpecField("minReplicas", 2),
update: updateField(2, "spec", "minReplicas"),
feature: actions.SteveNumericFieldIndexes,
},
{
name: "hpa max replicas", resource: "autoscaling.horizontalpodautoscaler",
field: "spec.maxReplicas",
low: hpa(1, 10), high: hpa(1, 2),
lowValue: "10", highValue: "2",
update: updateSpecField("maxReplicas", 2),
update: updateField(2, "spec", "maxReplicas"),
feature: actions.SteveNumericFieldIndexes,
},
// ConfigMap data counts use INT on newer releases and TEXT on older ones.
Expand All @@ -53,6 +57,42 @@ func TestSQLIndexedFields(t *testing.T) {
return nil
},
},
// rancher/rancher#55811. Different groups allow singular names to converge
// without colliding with another CRD in the same API group.
{
name: "crd group", resource: crdResource, field: "spec.group",
low: customResourceDefinition("zlows", lowGroup, "low"),
high: customResourceDefinition("ahighs", highGroup, "high"),
lowValue: lowGroup, highValue: highGroup,
feature: actions.SteveAdditionalFieldIndexes,
// spec.group is immutable; the contract checks deletion instead.
},
{
name: "crd singular", resource: crdResource, field: "spec.names.singular",
low: customResourceDefinition("zsingulars", lowGroup, "alpha"),
high: customResourceDefinition("asingulars", highGroup, "bravo"),
lowValue: "alpha", highValue: "bravo",
update: updateField("bravo", "spec", "names", "singular"),
feature: actions.SteveAdditionalFieldIndexes,
},
{
name: "configmap description", resource: "configmap", field: descriptionField,
low: withDescription(configMap(1), "alpha description"),
high: withDescription(configMap(1), "bravo description"),
lowValue: "alpha description", highValue: "bravo description",
update: updateField("bravo description", "metadata", "annotations", descriptionAnnotation),
remove: updateField(nil, "metadata", "annotations", descriptionAnnotation),
feature: actions.SteveAdditionalFieldIndexes,
},
{
name: "crd description", resource: crdResource, field: descriptionField,
low: withDescription(customResourceDefinition("zdescriptions", lowGroup, "lowdescription"), "alpha description"),
high: withDescription(customResourceDefinition("adescriptions", highGroup, "highdescription"), "bravo description"),
lowValue: "alpha description", highValue: "bravo description",
update: updateField("bravo description", "metadata", "annotations", descriptionAnnotation),
remove: updateField(nil, "metadata", "annotations", descriptionAnnotation),
feature: actions.SteveAdditionalFieldIndexes,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand All @@ -61,36 +101,48 @@ func TestSQLIndexedFields(t *testing.T) {
actions.RequireSteveFeature(t, test.feature)
}
cl := actions.RequireSQLMode(t).Client
if _, ok := cl.Schema(test.resource); !ok {
schema, ok := cl.Schema(test.resource)
if !ok {
t.Fatalf("required schema %q is missing", test.resource)
}
ns := actions.CreateTestNamespace(t, cl)
resources := cl.Resource(test.resource).Namespace(ns)
actions.RequireMutations(t)
resources := cl.Resource(test.resource)
ns := ""
if schema.Namespaced {
ns = actions.CreateTestNamespace(t, cl)
resources = resources.Namespace(ns)
} else {
actions.RequireClusterScopedCRDs(t)
}
caseID := actions.UniqueName(t, "indexed-field")
lowFilter := test.field + "=" + strconv.Quote(test.lowValue)
highFilter := test.field + "=" + strconv.Quote(test.highValue)
names := []string{"z-low", "a-high"}
// Reverse alphabetical names catch a sort that silently uses name instead
// of the requested field. 2 and 10 also distinguish numeric and text order.
for _, fixture := range []struct {
name string
body map[string]any
}{
{"z-low", test.low},
{"a-high", test.high},
} {
metadata, _ := fixture.body["metadata"].(map[string]any)
for i, body := range []map[string]any{test.low, test.high} {
metadata, _ := body["metadata"].(map[string]any)
if metadata == nil {
metadata = make(map[string]any)
}
metadata["name"], metadata["namespace"] = fixture.name, ns
if name, ok := metadata["name"].(string); ok {
names[i] = name // CRD names must be plural.group.
}
metadata["name"] = names[i]
if ns != "" {
metadata["namespace"] = ns
}
labels, _ := metadata["labels"].(map[string]string)
labels = actions.ManagedLabels(labels)
labels[actions.CaseLabel] = "indexed-field"
labels[actions.CaseLabel] = caseID
metadata["labels"] = labels
fixture.body["metadata"] = metadata
actions.CreateAndWait(t, resources, fixture.name, fixture.body)
body["metadata"] = metadata
actions.CreateAndWait(t, resources, names[i], body)
}
check := func(t *testing.T, filter, sort string, want []string) {
t.Helper()
values := url.Values{
"filter": {"metadata.labels." + actions.CaseLabel + "=indexed-field"},
"filter": {"metadata.labels." + actions.CaseLabel + "=" + caseID},
"sort": {sort},
}
if filter != "" {
Expand All @@ -102,36 +154,67 @@ func TestSQLIndexedFields(t *testing.T) {
name, filter, sort string
want []string
}{
{"filter low", test.field + "=" + test.lowValue, "metadata.name", []string{"z-low"}},
{"filter high", test.field + "=" + test.highValue, "metadata.name", []string{"a-high"}},
{"ascending", "", test.field + ",metadata.name", []string{"z-low", "a-high"}},
{"descending", "", "-" + test.field + ",metadata.name", []string{"a-high", "z-low"}},
{"filter low", lowFilter, "metadata.name", []string{names[0]}},
{"filter high", highFilter, "metadata.name", []string{names[1]}},
{"ascending", "", test.field + ",metadata.name", []string{names[0], names[1]}},
{"descending", "", "-" + test.field + ",metadata.name", []string{names[1], names[0]}},
} {
t.Run(assertion.name, func(t *testing.T) {
check(t, assertion.filter, assertion.sort, assertion.want)
})
}
if test.update == nil {
t.Run("delete refreshes index", func(t *testing.T) {
check(t, lowFilter, "metadata.name", []string{names[0]})
if err := resources.DeleteContext(t.Context(), names[0]); err != nil {
t.Fatalf("delete indexed resource: %v", err)
}
check(t, lowFilter, "metadata.name", []string{})
check(t, "", test.field, []string{names[1]})
})
return
}
// This subtest performs its own transition and does not rely on siblings.
t.Run("update refreshes index", func(t *testing.T) {
check(t, test.field+"="+test.lowValue, "metadata.name", []string{"z-low"})
if _, err := actions.UpdateWithConflictRetry(t.Context(), resources, "z-low", test.update); err != nil {
check(t, lowFilter, "metadata.name", []string{names[0]})
if _, err := actions.UpdateWithConflictRetry(t.Context(), resources, names[0], test.update); err != nil {
t.Fatalf("update indexed field: %v", err)
}
check(t, test.field+"="+test.highValue, "metadata.name", []string{"a-high", "z-low"})
check(t, test.field+"="+test.lowValue, "metadata.name", []string{})
check(t, "", test.field+",metadata.name", []string{"a-high", "z-low"})
check(t, highFilter, "metadata.name", []string{names[1], names[0]})
check(t, lowFilter, "metadata.name", []string{})
check(t, "", test.field+",metadata.name", []string{names[1], names[0]})
if test.remove != nil {
t.Run("remove annotation refreshes index", func(t *testing.T) {
if _, err := actions.UpdateWithConflictRetry(t.Context(), resources, names[0], test.remove); err != nil {
t.Fatalf("remove indexed field: %v", err)
}
check(t, highFilter, "metadata.name", []string{names[1]})
check(t, lowFilter, "metadata.name", []string{})
// A resource without this annotation must remain in the collection.
check(t, "", "metadata.name", []string{names[1], names[0]})
})
}
})
})
}
}

func updateSpecField(field string, value any) func(map[string]any) error {
// A nil value removes the field.
func updateField(value any, path ...string) func(map[string]any) error {
return func(current map[string]any) error {
spec, ok := current["spec"].(map[string]any)
if !ok {
return fmt.Errorf("resource has no spec object")
for _, key := range path[:len(path)-1] {
next, ok := current[key].(map[string]any)
if !ok {
return fmt.Errorf("resource has no %s object in %v", key, path)
}
current = next
}
key := path[len(path)-1]
if value == nil {
delete(current, key)
} else {
current[key] = value
}
spec[field] = value
return nil
}
}
Loading