Files
cozystack/packages/system/backupstrategy-controller/tests/rendering_test.yaml
T
Myasnikov Daniil 7204992452 fix(backups): gate every default Strategy CR on its CRD
The chart ships its strategy CRDs as ordinary templates, and Helm maps
every document of a release through the RESTMapper before it applies
any. A default Strategy CR gated only on the bucket name therefore
fails Build() with "no matches for kind" whenever the bucket name
resolves before the CRD is served, the release applies nothing, the
CRDs are never created, and every retry renders the same manifest.

That happens on the upgrade that first ships a kind: the Kafka,
Rabbitmq and Redis CRs leave a v1.6 cluster's HelmRelease stuck in
UpgradeFailed on the way to v1.7. It also happens on the first render
of an external-S3 install (provisionBucket=false), where the bucket
name comes from values and every older default CR renders next to its
absent CRD.

MongoDB already gates on crdEstablished for this reason. Give every
default Strategy the same gate. On an installed cluster the CRDs are
established and the render is unchanged; otherwise the first revision
creates the CRDs and skips the CRs, and DefaultObjectsGate, which reads
the cozy-default BackupClass routes, forces the follow-up upgrade that
renders them once the kinds are served.

Assisted-by: LLM
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
2026-10-14 13:31:45 +00:00

513 lines
25 KiB
YAML

suite: rendering gates — bucketName helper, BackupClass always-on, strategy/BSL gating
# This whole chart is lookup-driven conditional rendering: the
# backupstrategy-controller.bucketName helper decides whether the Strategy
# CRs and the Velero BSL materialise, while the cozy-default BackupClass and
# the cozy-backups Bucket follow different rules. helm-unittest renders with
# no live cluster, so `lookup` returns nil — exactly the bootstrap window
# these cases pin.
templates:
- templates/backupclass-default.yaml
- templates/bucket.yaml
- templates/velero-bsl.yaml
- templates/strategy-cnpg-default.yaml
- templates/strategy-mariadb-default.yaml
- templates/strategy-etcd-default.yaml
- templates/strategy-altinity-default.yaml
- templates/strategy-mongodb-default.yaml
- templates/strategy-rabbitmq-default.yaml
- templates/strategy-redis-default.yaml
- templates/strategy-kafka-default.yaml
- templates/strategy-foundationdb-default.yaml
- templates/strategy-velero-vminstance-default.yaml
- templates/strategy-velero-vmdisk-default.yaml
release:
name: t
namespace: cozy-backup-controller
tests:
- it: "bootstrap window (no resolvable bucket name): only Bucket + BackupClass render"
# defaults: provisionBucket=true, bucketNameOverride="" -> lookup nil -> $bucketName=""
asserts:
- hasDocuments:
count: 1
template: templates/backupclass-default.yaml
- hasDocuments:
count: 1
template: templates/bucket.yaml
- equal:
path: metadata.namespace
value: tenant-root
template: templates/bucket.yaml
- hasDocuments:
count: 0
template: templates/strategy-cnpg-default.yaml
- hasDocuments:
count: 0
template: templates/strategy-mariadb-default.yaml
- hasDocuments:
count: 0
template: templates/strategy-etcd-default.yaml
- hasDocuments:
count: 0
template: templates/strategy-altinity-default.yaml
# MongoDB is gated like every sibling, and this count is the assertion
# that keeps the chart installable. Its Strategy carries the system-bucket
# coordinates ($bucketName), so it needs the gate for the same reason the
# others do: this chart ships its own CRDs as templates, Helm resolves the
# whole manifest through the RESTMapper before applying any of it, and one
# ungated Strategy CR makes the first install apply zero objects with "no
# matches for kind" and never recover. A count of 1 here is that bug,
# green, which is how it shipped once already.
- hasDocuments:
count: 0
template: templates/strategy-mongodb-default.yaml
# RabbitMQ Job strategy carries bucket coordinates (endpoint, bucket,
# object key), so it MUST stay off the first render like the others -
# an ungated Strategy CR trips the same "no matches for kind" install
# failure described above.
- hasDocuments:
count: 0
template: templates/strategy-rabbitmq-default.yaml
# Kafka metadata Job strategy carries bucket coordinates too, so it stays
# off the first render for the same ordering reason as its siblings.
- hasDocuments:
count: 0
template: templates/strategy-kafka-default.yaml
- hasDocuments:
count: 0
template: templates/strategy-redis-default.yaml
- hasDocuments:
count: 0
template: templates/strategy-foundationdb-default.yaml
- hasDocuments:
count: 0
template: templates/strategy-velero-vminstance-default.yaml
- hasDocuments:
count: 0
template: templates/strategy-velero-vmdisk-default.yaml
- hasDocuments:
count: 0
template: templates/velero-bsl.yaml
- it: "cozy-default BackupClass is the Day-1 tenant interface: renders unconditionally with all eleven routes (FoundationDB intentionally unbound)"
asserts:
- hasDocuments:
count: 1
template: templates/backupclass-default.yaml
- equal:
path: metadata.name
value: cozy-default
template: templates/backupclass-default.yaml
- lengthEqual:
path: spec.strategies
count: 11
template: templates/backupclass-default.yaml
- contains:
path: spec.strategies
content:
application:
apiGroup: apps.cozystack.io
kind: RabbitMQ
strategyRef:
apiGroup: strategy.backups.cozystack.io
kind: Rabbitmq
name: cozy-default-rabbitmq
template: templates/backupclass-default.yaml
- contains:
path: spec.strategies
content:
application:
apiGroup: apps.cozystack.io
kind: Redis
strategyRef:
apiGroup: strategy.backups.cozystack.io
kind: Redis
name: cozy-default-redis
template: templates/backupclass-default.yaml
- contains:
path: spec.strategies
content:
application:
apiGroup: apps.cozystack.io
kind: Valkey
strategyRef:
apiGroup: strategy.backups.cozystack.io
kind: Redis
name: cozy-default-redis
template: templates/backupclass-default.yaml
- contains:
path: spec.strategies
content:
application:
apiGroup: apps.cozystack.io
kind: Kafka
strategyRef:
apiGroup: strategy.backups.cozystack.io
kind: Kafka
name: cozy-default-kafka
template: templates/backupclass-default.yaml
- it: "all Strategy CRs and the Velero BSL render once a bucket name resolves"
# bucketNameOverride marks an offline render, and the MongoDB count below is
# what pins that the CRD gate honours it: without that short-circuit no
# `helm template` or CI diff could ever produce this Strategy, since neither
# has an apiserver to ask about the CRD.
set:
backupStorage.bucketNameOverride: test-bucket
asserts:
- hasDocuments:
count: 1
template: templates/strategy-cnpg-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-mariadb-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-etcd-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-altinity-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-mongodb-default.yaml
- equal:
path: spec.template.s3.bucket
value: test-bucket
template: templates/strategy-mongodb-default.yaml
- equal:
path: spec.template.s3.credentialsSecret
value: cozy-backups-creds
template: templates/strategy-mongodb-default.yaml
- equal:
path: spec.template.s3.region
value: us-east-1
template: templates/strategy-mongodb-default.yaml
- equal:
path: spec.template.s3.forcePathStyle
value: true
template: templates/strategy-mongodb-default.yaml
# insecureSkipTLSVerify is plumbed end-to-end (strategy -> driver-injected
# storage entry). This case pins the default (knob off -> false); the
# "renders insecureSkipTLSVerify from the platform knob" case below flips
# the knob on, so the pair discriminates the rendered value from a
# hardcoded literal (either would redden one of the two).
- equal:
path: spec.template.s3.insecureSkipTLSVerify
value: false
template: templates/strategy-mongodb-default.yaml
# prefix must reach the CR as a literal go-template string: Helm must NOT
# evaluate it, so the driver templates it per-application at BackupJob time.
# A stray brace would empty it with every doc-count assertion still green.
- equal:
path: spec.template.s3.prefix
value: "{{ .Application.metadata.namespace }}/{{ .Application.metadata.name }}"
template: templates/strategy-mongodb-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-rabbitmq-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-redis-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-kafka-default.yaml
- isKind:
of: Kafka
template: templates/strategy-kafka-default.yaml
- equal:
path: metadata.name
value: cozy-default-kafka
template: templates/strategy-kafka-default.yaml
- equal:
path: spec.artifactURITemplate
value: "s3://test-bucket/{{ .Release.Namespace }}/{{ .Release.Name }}/{{ .BackupName }}/kafka-metadata.txt"
template: templates/strategy-kafka-default.yaml
# The chart pins no client image: the controller fills the placeholder with
# the target broker's own image at reconcile time.
- equal:
path: spec.template.spec.containers[0].image
value: "{{ .ClientImage }}"
template: templates/strategy-kafka-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-foundationdb-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-velero-vminstance-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-velero-vmdisk-default.yaml
- hasDocuments:
count: 1
template: templates/velero-bsl.yaml
- equal:
path: spec.objectStorage.bucket
value: test-bucket
template: templates/velero-bsl.yaml
- equal:
# s3ForcePathStyle is rendered as a quoted string, not a bool
path: spec.config.s3ForcePathStyle
value: "true"
template: templates/velero-bsl.yaml
- it: "the MongoDB strategy renders insecureSkipTLSVerify from the platform knob"
# Pairs with the default (false) assertion above: with the knob flipped on,
# the injected storage entry must carry true. A template that hardcoded either
# literal would redden one of the two cases, so together they prove the value
# tracks backupStorage.insecureSkipTLSVerify rather than a constant.
set:
backupStorage.bucketNameOverride: test-bucket
backupStorage.insecureSkipTLSVerify: true
asserts:
- equal:
path: spec.template.s3.insecureSkipTLSVerify
value: true
template: templates/strategy-mongodb-default.yaml
- it: "external S3 (provisionBucket=false): Strategy CRs render against the configured bucketName, no Bucket CR is created"
# The CRDs are reported Established, as on any render after the first: the
# CRD gate is what holds these CRs back on the first one.
set:
backupStorage.provisionBucket: false
backupStorage.bucketName: ext-bucket
kubernetesProvider:
scheme:
"apiextensions.k8s.io/v1/CustomResourceDefinition":
gvr:
group: apiextensions.k8s.io
version: v1
resource: customresourcedefinitions
namespaced: false
objects:
- apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: cnpgs.strategy.backups.cozystack.io
status:
conditions: [{type: Established, status: "True"}]
- apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: mariadbs.strategy.backups.cozystack.io
status:
conditions: [{type: Established, status: "True"}]
asserts:
- hasDocuments:
count: 0
template: templates/bucket.yaml
- hasDocuments:
count: 1
template: templates/strategy-cnpg-default.yaml
- matchRegex:
path: spec.template.barmanObjectStore.destinationPath
pattern: ^s3://ext-bucket/
template: templates/strategy-cnpg-default.yaml
- equal:
path: spec.template.storage.s3.bucket
value: ext-bucket
template: templates/strategy-mariadb-default.yaml
- it: "Strategies gate on their CRD as well as on the bucket, so a render whose CRDs are not served yet cannot wedge the release"
# provisionBucket=false is what makes this case load-bearing: it resolves
# $bucketName WITHOUT the offline bucketNameOverride, which is the shape of a
# cluster that has been running for releases, and of the first render of an
# external-S3 install — bucket gate open, and only the CRD gate still able
# to hold the CR back. helm-unittest renders with no apiserver, so the CRD
# lookup answers "absent", which is the same answer a real cluster gives on
# the upgrade that first ships that CRD. Rendering the CR there fails
# Build() for the WHOLE release ("no matches for kind MongoDB") and applies
# zero objects, including the CRD that would have fixed it, so a count of 1
# for any of these is that wedge, green. hack/backupstrategy-crd-gate.bats
# holds the same property for every template, listed here or not.
set:
backupStorage.provisionBucket: false
backupStorage.bucketName: ext-bucket
asserts:
- hasDocuments:
count: 0
template: templates/strategy-mongodb-default.yaml
- hasDocuments:
count: 0
template: templates/strategy-kafka-default.yaml
- hasDocuments:
count: 0
template: templates/strategy-rabbitmq-default.yaml
- hasDocuments:
count: 0
template: templates/strategy-redis-default.yaml
- hasDocuments:
count: 0
template: templates/strategy-cnpg-default.yaml
- {hasDocuments: {count: 0}, template: templates/strategy-mariadb-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-etcd-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-altinity-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-foundationdb-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-velero-vminstance-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-velero-vmdisk-default.yaml}
# The ungated BackupClass keeps rendering, which is what makes the counts
# above an assertion about the CRD gate rather than about a chart-wide
# render failure.
- hasDocuments:
count: 1
template: templates/backupclass-default.yaml
- it: "the CRD-gated Strategies render once the apiserver reports their CRDs Established"
# The counterpart of the case above, and the only one that resolves the CRD
# gate through a real lookup: bucketNameOverride short-circuits it, so every
# other case would stay green with a misspelt CRD name that never resolves on
# a live cluster, leaving the CR absent and DefaultObjectsGate forcing
# upgrades for good.
set:
backupStorage.provisionBucket: false
backupStorage.bucketName: ext-bucket
kubernetesProvider:
scheme:
"apiextensions.k8s.io/v1/CustomResourceDefinition":
gvr:
group: apiextensions.k8s.io
version: v1
resource: customresourcedefinitions
namespaced: false
objects:
- apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: mongodbs.strategy.backups.cozystack.io
status:
conditions: [{type: Established, status: "True"}]
- apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: kafkas.strategy.backups.cozystack.io
status:
conditions: [{type: Established, status: "True"}]
- apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: rabbitmqs.strategy.backups.cozystack.io
status:
conditions: [{type: Established, status: "True"}]
- apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: redis.strategy.backups.cozystack.io
status:
conditions: [{type: Established, status: "True"}]
asserts:
- hasDocuments:
count: 1
template: templates/strategy-mongodb-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-kafka-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-rabbitmq-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-redis-default.yaml
- it: "with every CRD Established, the bucket gate alone still holds every Strategy CR back until the BucketClaim resolves"
# The CRD gate is closed in every render that has no apiserver, so the
# bootstrap-window case above would stay green with any bucket gate deleted.
# Only a render that serves the CRDs and leaves the BucketClaim unresolved
# pins that gate, and its absence ships a Strategy CR with an empty bucket.
kubernetesProvider:
scheme:
"apiextensions.k8s.io/v1/CustomResourceDefinition":
gvr: {group: apiextensions.k8s.io, version: v1, resource: customresourcedefinitions}
namespaced: false
"objectstorage.k8s.io/v1alpha1/BucketClaim":
gvr: {group: objectstorage.k8s.io, version: v1alpha1, resource: bucketclaims}
namespaced: true
"v1/Secret":
gvr: {group: "", version: v1, resource: secrets}
namespaced: true
objects:
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: altinities.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: cnpgs.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: etcds.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: foundationdbs.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: kafkas.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: mariadbs.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: mongodbs.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: rabbitmqs.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: redis.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: veleroes.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
asserts:
- {hasDocuments: {count: 0}, template: templates/strategy-altinity-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-cnpg-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-etcd-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-foundationdb-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-kafka-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-mariadb-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-mongodb-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-rabbitmq-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-redis-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-velero-vminstance-default.yaml}
- {hasDocuments: {count: 0}, template: templates/strategy-velero-vmdisk-default.yaml}
- it: "with every CRD Established and the BucketClaim resolved, every Strategy CR renders against the COSI bucket name"
# The positive half of the case above, through the same live lookups and no
# offline override: the state every forced upgrade after bootstrap renders.
kubernetesProvider:
scheme:
"apiextensions.k8s.io/v1/CustomResourceDefinition":
gvr: {group: apiextensions.k8s.io, version: v1, resource: customresourcedefinitions}
namespaced: false
"objectstorage.k8s.io/v1alpha1/BucketClaim":
gvr: {group: objectstorage.k8s.io, version: v1alpha1, resource: bucketclaims}
namespaced: true
"v1/Secret":
gvr: {group: "", version: v1, resource: secrets}
namespaced: true
objects:
- {apiVersion: objectstorage.k8s.io/v1alpha1, kind: BucketClaim, metadata: {name: bucket-cozy-backups, namespace: tenant-root}, status: {bucketName: cozy-backups-0123}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: altinities.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: cnpgs.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: etcds.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: foundationdbs.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: kafkas.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: mariadbs.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: mongodbs.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: rabbitmqs.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: redis.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
- {apiVersion: apiextensions.k8s.io/v1, kind: CustomResourceDefinition, metadata: {name: veleroes.strategy.backups.cozystack.io}, status: {conditions: [{type: Established, status: "True"}]}}
asserts:
- {hasDocuments: {count: 1}, template: templates/strategy-altinity-default.yaml}
- {hasDocuments: {count: 1}, template: templates/strategy-cnpg-default.yaml}
- {matchRegex: {path: spec.template.barmanObjectStore.destinationPath, pattern: ^s3://cozy-backups-0123/}, template: templates/strategy-cnpg-default.yaml}
- {hasDocuments: {count: 1}, template: templates/strategy-etcd-default.yaml}
- {hasDocuments: {count: 1}, template: templates/strategy-foundationdb-default.yaml}
- {hasDocuments: {count: 1}, template: templates/strategy-kafka-default.yaml}
- {hasDocuments: {count: 1}, template: templates/strategy-mariadb-default.yaml}
- {hasDocuments: {count: 1}, template: templates/strategy-mongodb-default.yaml}
- {hasDocuments: {count: 1}, template: templates/strategy-rabbitmq-default.yaml}
- {hasDocuments: {count: 1}, template: templates/strategy-redis-default.yaml}
- {hasDocuments: {count: 1}, template: templates/strategy-velero-vminstance-default.yaml}
- {hasDocuments: {count: 1}, template: templates/strategy-velero-vmdisk-default.yaml}
- it: "velero.bslEnabled=false: Velero strategies and BSL drop out while other strategies keep rendering"
set:
backupStorage.bucketNameOverride: test-bucket
velero.bslEnabled: false
asserts:
- hasDocuments:
count: 0
template: templates/velero-bsl.yaml
- hasDocuments:
count: 0
template: templates/strategy-velero-vminstance-default.yaml
- hasDocuments:
count: 0
template: templates/strategy-velero-vmdisk-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-cnpg-default.yaml
- hasDocuments:
count: 1
template: templates/strategy-etcd-default.yaml