Files
cozystack/pkg/registry/apps/application/rest_spec_schema_test.go
Aleksei Sviridkin 5af5962edf feat(apps): mark stateful-app storageClass fields as immutable
Annotate storageClass as immutable across every stateful app whose
chart exposes one — clickhouse, foundationdb, harbor, http-cache,
kafka (Kafka + ZooKeeper), kubernetes (top-level only), mariadb,
mongodb, nats, openbao, opensearch, postgres, qdrant, rabbitmq,
redis, vm-disk. Without coverage across the catalogue the UI would
render storageClass read-only on some apps and editable on others —
a worse mental model than uniform behaviour.

The annotation lands at the schema layer (CEL XValidation rule
'self == oldSelf' on values.schema.json / types.go / embedded
openAPISchema in cozyrds/<app>.yaml). The cozystack aggregated
apiserver does not yet evaluate CEL on application-backed resources;
that is tracked at cozystack/cozystack#2657. The UI
(cozystack/cozystack-ui#6) consumes the marker today.

The PVC-pinning argument is identical for every chart: Kubernetes
fixes a PVC's storageClassName at creation time, so editing this
field on an existing resource never migrated data. The schema
annotation makes that contract explicit.

kubernetes.nodeGroups[name].storageClass is intentionally NOT
annotated: the field is optional and undefaulted, so a strict
'self == oldSelf' rule would block any future attempt to set it on
an existing node group. A richer ratcheting form is needed there —
see docs/storage-immutability.md.

A refactor in pkg/registry/apps/application/rest.go extracts the
inline schema-build path into buildSpecSchema so it can be
unit-tested. Three regression tests in rest_spec_schema_test.go pin
the contract that x-kubernetes-validations survives the
v1->internal->structural conversion (loaded from the actual MariaDB
ApplicationDefinition fixture on disk, not a hand-edited mock).

Each per-app README gains a one-line blockquote pointing to a new
docs/storage-immutability.md that explains the contract and which
consumers enforce it today.

Requires cozystack/cozyvalues-gen with @immutable support — bump the
pre-commit pin in .github/workflows/pre-commit.yml once the release
exists.

Assisted-By: Claude <noreply@anthropic.com>
Signed-off-by: Aleksei Sviridkin <f@lex.la>
2026-05-26 23:51:11 +03:00

109 lines
3.7 KiB
Go

package application
import (
"os"
"path/filepath"
"strings"
"testing"
"sigs.k8s.io/yaml"
)
// TestBuildSpecSchema_FromCRDsWithCELRule pins the contract that an
// ApplicationDefinition's openAPISchema carrying an
// x-kubernetes-validations entry (the @immutable annotation on MariaDB
// storageClass) builds into a valid structural schema. Without this,
// any future change to the k8s apiextensions structural-schema rules
// could silently degrade specSchema to nil and defaulting for the
// affected Application kind would stop working without an obvious
// failure mode — just a klog.Errorf and no observable contract break.
func TestBuildSpecSchema_FromCRDsWithCELRule(t *testing.T) {
raw := readEmbeddedOpenAPISchema(t,
"../../../../packages/system/mariadb-rd/cozyrds/mariadb.yaml")
if !strings.Contains(raw, "x-kubernetes-validations") {
t.Fatalf("test fixture lost its x-kubernetes-validations entry; " +
"the @immutable annotation on storageClass must reach the embedded schema")
}
got, err := buildSpecSchema(raw)
if err != nil {
t.Fatalf("buildSpecSchema returned error on a real cluster-shipped schema: %v", err)
}
if got == nil {
t.Fatal("buildSpecSchema returned nil structural schema for a non-empty input — defaulting would be a no-op")
}
sc, ok := got.Properties["storageClass"]
if !ok {
t.Fatal("structural schema lost the storageClass property after v1->internal conversion")
}
// The CEL rule must survive v1→internal→structural. Asserting only that
// the property exists is not enough: a future upstream change could
// drop XValidations during conversion and the property would still be
// present, silently degrading the schema we ship to clients.
if len(sc.XValidations) == 0 {
t.Fatal("storageClass lost its x-kubernetes-validations entries after v1->internal->structural conversion")
}
var foundImmutable bool
for _, rule := range sc.XValidations {
if rule.Rule == "self == oldSelf" {
foundImmutable = true
break
}
}
if !foundImmutable {
t.Fatalf("expected 'self == oldSelf' rule on storageClass, got %+v", sc.XValidations)
}
}
// TestBuildSpecSchema_EmptyRawReturnsNilNoError pins the "no embedded
// schema" path: callers (NewREST) must see (nil, nil), not an error.
func TestBuildSpecSchema_EmptyRawReturnsNilNoError(t *testing.T) {
for _, in := range []string{"", " ", "\n\t"} {
s, err := buildSpecSchema(in)
if err != nil {
t.Errorf("buildSpecSchema(%q) returned error: %v", in, err)
}
if s != nil {
t.Errorf("buildSpecSchema(%q) returned non-nil structural schema for empty input", in)
}
}
}
func TestBuildSpecSchema_MalformedJSONReturnsError(t *testing.T) {
_, err := buildSpecSchema("{not json")
if err == nil {
t.Fatal("buildSpecSchema accepted malformed JSON without error")
}
}
// readEmbeddedOpenAPISchema loads ApplicationDefinition YAML on disk and
// extracts spec.application.openAPISchema. Returning the raw JSON string
// keeps the test honest — it exercises the exact byte sequence shipped
// to the cluster, not a hand-edited mock.
func readEmbeddedOpenAPISchema(t *testing.T, relPath string) string {
t.Helper()
abs, err := filepath.Abs(relPath)
if err != nil {
t.Fatalf("resolve fixture path: %v", err)
}
data, err := os.ReadFile(abs)
if err != nil {
t.Fatalf("read fixture %s: %v", abs, err)
}
var doc struct {
Spec struct {
Application struct {
OpenAPISchema string `json:"openAPISchema"`
} `json:"application"`
} `json:"spec"`
}
if err := yaml.Unmarshal(data, &doc); err != nil {
t.Fatalf("unmarshal fixture %s: %v", abs, err)
}
if doc.Spec.Application.OpenAPISchema == "" {
t.Fatalf("fixture %s has empty spec.application.openAPISchema", abs)
}
return doc.Spec.Application.OpenAPISchema
}