mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 12:13:55 +09:00
Merge branch 'main' into ci/skip-tooling-paths
Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
337 files changed
+21834
-4924
No files matched your search
@@ -139,13 +139,14 @@ rd-presets-check:
|
||||
migrations-target-check:
|
||||
hack/check-migrations-target.sh
|
||||
|
||||
# Scoped go test over the cozystack-api surface that this repo owns. Kept
|
||||
# narrow intentionally - running `go test ./...` pulls in generated code
|
||||
# round-trip suites whose behavior depends on tool versions outside this
|
||||
# repo's control (kubebuilder, openapi-gen, etc.) and is better exercised
|
||||
# from their generator workflows.
|
||||
# Go tests of the cozystack-api surface and of the standalone packages and
|
||||
# commands listed here, whose tests need no cluster. Kept narrow
|
||||
# intentionally - running `go test ./...` pulls in generated code round-trip
|
||||
# suites whose behavior depends on tool versions outside this repo's control
|
||||
# (kubebuilder, openapi-gen, etc.) and is better exercised from their
|
||||
# generator workflows.
|
||||
go-unit-tests:
|
||||
go test ./pkg/registry/... ./pkg/config/... ./pkg/cmd/server/...
|
||||
go test ./pkg/registry/... ./pkg/config/... ./pkg/cmd/server/... ./pkg/lineage/... ./pkg/ovnstatus/... ./pkg/reservation/... ./cmd/cozypkg/... ./cmd/check-readiness/...
|
||||
|
||||
# The nested Go modules (image sources and the published API types) are
|
||||
# outside ./... of the root module, so no other target reaches their tests.
|
||||
@@ -158,9 +159,7 @@ go-module-tests:
|
||||
done
|
||||
|
||||
# Go tests for the controllers and supporting packages under ./internal.
|
||||
# Excludes ./pkg/... and ./cmd/... — those are run separately by
|
||||
# go-unit-tests above (pkg subset) and skipped (cmd) until their tests
|
||||
# stabilise. CI schedules this target in the same four-slot make invocation as
|
||||
# CI schedules this target in the same four-slot make invocation as
|
||||
# unit-tests; locally invoke it directly or chain the two targets.
|
||||
test-controllers:
|
||||
go test ./internal/... -count=1
|
||||
@@ -195,10 +194,10 @@ test-controllers-envtest: $(SETUP_ENVTEST)
|
||||
test-check-readiness:
|
||||
go test ./test/check-readiness/ -count=1
|
||||
|
||||
# ./cmd/... is excluded from go-unit-tests, so the batch tool needs its own target.
|
||||
# It is not a prerequisite of unit-tests: the checks job in pull-requests.yaml
|
||||
# runs it only when the diff touches a path listed in
|
||||
# cmd/backport-batch/test-inputs.txt.
|
||||
# The batch tool's suite drives real git repositories for minutes, so it is
|
||||
# kept out of go-unit-tests in a target of its own. It is not a prerequisite
|
||||
# of unit-tests: the checks job in pull-requests.yaml runs it only when the
|
||||
# diff touches a path listed in cmd/backport-batch/test-inputs.txt.
|
||||
test-backport-batch:
|
||||
go test ./cmd/backport-batch/... -count=1
|
||||
|
||||
|
||||
Generated
+3
@@ -25,6 +25,9 @@ type ConfigSpec struct {
|
||||
// Deploys the web UI (s3manager) with its Service and its Ingress or HTTPRoute at `<bucket>.<tenant host>`. Set to `false` to run the bucket without them.
|
||||
// +kubebuilder:default:=true
|
||||
Ui bool `json:"ui"`
|
||||
// Serves the bucket's objects to anyone without credentials at `<endpoint>/<bucket>/<key>`. Anonymous callers can read objects but cannot list the bucket or write to it.
|
||||
// +kubebuilder:default:=false
|
||||
PublicRead bool `json:"publicRead"`
|
||||
// Users configuration map.
|
||||
// +kubebuilder:default:={}
|
||||
Users map[string]User `json:"users,omitempty"`
|
||||
|
||||
Generated
+1
-1
@@ -124,7 +124,7 @@ type Backup struct {
|
||||
// DEPRECATED. See `s3AccessKey`.
|
||||
// +kubebuilder:default:=""
|
||||
S3SecretKey string `json:"s3SecretKey,omitempty"`
|
||||
// Legacy. Cron schedule (CNPG 6-field format) for the chart-emitted ScheduledBackup. Empty means no chart-managed schedule, which is the recommended setup when a `BackupClass` from `backups.cozystack.io` already drives backup orchestration. In the legacy chart-managed flow `spec.plugins` plus the barman-cloud ObjectStore is rendered when `backup.enabled=true` AND `useSystemBucket=false` AND `destinationPath` is non-empty AND inline-or-external creds are supplied; in the platform `useSystemBucket=true` flow the chart skips emitting `spec.plugins` and the CNPG driver SSA-applies the ObjectStore and patches `spec.plugins` onto the live Cluster at first BackupJob time.
|
||||
// Legacy. Cron schedule for the chart-emitted ScheduledBackup, either a standard five-field cron such as `0 2 * * *` for every day at 02:00, or six fields with leading seconds such as `0 0 2 * * *` for the same time. Empty means no chart-managed schedule, which is the recommended setup when a `BackupClass` from `backups.cozystack.io` already drives backup orchestration. In the legacy chart-managed flow `spec.plugins` plus the barman-cloud ObjectStore is rendered when `backup.enabled=true` AND `useSystemBucket=false` AND `destinationPath` is non-empty AND inline-or-external creds are supplied; in the platform `useSystemBucket=true` flow the chart skips emitting `spec.plugins` and the CNPG driver SSA-applies the ObjectStore and patches `spec.plugins` onto the live Cluster at first BackupJob time.
|
||||
// +kubebuilder:default:=""
|
||||
Schedule string `json:"schedule,omitempty"`
|
||||
// Opt-in: when true, the chart-emitted `<release>-s3-creds` Secret is skipped AND `spec.plugins` (plus the barman-cloud ObjectStore) is left UNSET in the chart-rendered Cluster — the cozy-default BackupClass driver SSA-applies an ObjectStore (carrying destinationPath/endpointURL/credentials) and patches `spec.plugins` on the live Cluster when the first BackupJob runs. Consequence: plugin WAL archiving is NOT active until that first BackupJob fires; WAL accumulates on the PVC in the meantime, so fire an ad-hoc BackupJob immediately after enabling the flag on existing releases. Use together with the platform `cozy-default` BackupClass — tenants do not need to fill `s3AccessKey`/`s3SecretKey` or `destinationPath`/`endpointURL`. The destination path automatically scopes to `s3://cozy-backups/<namespace>/<release>/`.
|
||||
|
||||
Generated
+11
@@ -51,6 +51,14 @@ type ConfigSpec struct {
|
||||
// Redis `min-replicas-max-lag`, in seconds: a replica whose last acknowledgement is older than this does not count towards `minReplicasToWrite`. Unset by default, which leaves the Redis default (10). Has no effect unless `minReplicasToWrite` is set. Must be at least 1 when `minReplicasToWrite` is set: Redis treats 0 as disabling the check, so the chart refuses it.
|
||||
// +kubebuilder:validation:Minimum=0
|
||||
MinReplicasMaxLag *int `json:"minReplicasMaxLag,omitempty"`
|
||||
// Redis `maxmemory`: the dataset size at which Redis applies `maxmemoryPolicy`. Unset by default, which renders `maxmemory 0` (no limit, the Redis default), so a value set by hand with `CONFIG SET` is reset on the next reconcile. Must be at most 90% of the memory request of each Redis pod (from `resources` or `resourcesPreset`): the operator silently drops a larger value, so the chart refuses it. Leave headroom below that for replication and snapshot buffers.
|
||||
Maxmemory resource.Quantity `json:"maxmemory,omitempty"`
|
||||
// Redis `maxmemory-policy`: what Redis does once `maxmemory` is reached. `noeviction` refuses writes; `allkeys-lru` evicts the least recently used keys, which suits an instance used purely as a cache. Has no effect while `maxmemory` is unset.
|
||||
// +kubebuilder:default:="noeviction"
|
||||
MaxmemoryPolicy MaxmemoryPolicy `json:"maxmemoryPolicy"`
|
||||
// Write RDB snapshots to the persistent volume (the Redis `save` directive, with the Redis default schedule). Set to `false` for an instance used purely as a cache: `save ""` is rendered and the volume is kept, so no further scheduled snapshot is written while replicas still have room for the full-sync RDB they write to disk. An RDB file already on the volume is not removed, and Redis loads it on restart, so turning snapshots off does not by itself empty the dataset. Always off when `size` is empty.
|
||||
// +kubebuilder:default:=true
|
||||
Snapshots bool `json:"snapshots"`
|
||||
// Sentinel configuration. When `sentinel.resources` is empty and `sentinel.resourcesPreset` is unset, the Sentinel pods are sized like the Redis pods (`resources` and `resourcesPreset`). An empty `sentinel.resources` map counts as unset, as an empty top-level `resources` does.
|
||||
// +kubebuilder:default:={}
|
||||
Sentinel Sentinel `json:"sentinel,omitempty"`
|
||||
@@ -80,6 +88,9 @@ type TLS struct {
|
||||
// +kubebuilder:validation:Enum="no";"optional";"yes"
|
||||
type AuthClients string
|
||||
|
||||
// +kubebuilder:validation:Enum="noeviction";"allkeys-lru";"allkeys-lfu";"allkeys-random";"volatile-lru";"volatile-lfu";"volatile-random";"volatile-ttl"
|
||||
type MaxmemoryPolicy string
|
||||
|
||||
// +kubebuilder:validation:Enum="t1.nano";"t1.micro";"t1.small";"t1.medium";"t1.large";"t1.xlarge";"t1.2xlarge";"t1.4xlarge";"c1.nano";"c1.micro";"c1.small";"c1.medium";"c1.large";"c1.xlarge";"c1.2xlarge";"c1.4xlarge";"s1.nano";"s1.micro";"s1.small";"s1.medium";"s1.large";"s1.xlarge";"s1.2xlarge";"s1.4xlarge";"u1.nano";"u1.micro";"u1.small";"u1.medium";"u1.large";"u1.xlarge";"u1.2xlarge";"u1.4xlarge";"m1.nano";"m1.micro";"m1.small";"m1.medium";"m1.large";"m1.xlarge";"m1.2xlarge";"m1.4xlarge";"nano";"micro";"small";"medium";"large";"xlarge";"2xlarge"
|
||||
type ResourcesPreset string
|
||||
|
||||
|
||||
@@ -66,6 +66,7 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
|
||||
*out = new(int)
|
||||
**out = **in
|
||||
}
|
||||
out.Maxmemory = in.Maxmemory.DeepCopy()
|
||||
in.Sentinel.DeepCopyInto(&out.Sentinel)
|
||||
}
|
||||
|
||||
|
||||
Generated
+3
@@ -74,6 +74,9 @@ type ConfigSpec struct {
|
||||
}
|
||||
|
||||
type Disk struct {
|
||||
// Boot priority of the disk, 1 being tried first. Unset falls back to the disk's position in `disks` (the first disk gets 1), so set it to boot from a disk other than the first, such as an attached installer or rescue ISO, without reordering the list. Effective boot orders must be unique across disks: when one disk is given a value that another takes from its position, set both.
|
||||
// +kubebuilder:validation:Minimum=1
|
||||
BootOrder *int `json:"bootOrder,omitempty"`
|
||||
// Disk bus type (e.g. "sata").
|
||||
Bus string `json:"bus,omitempty"`
|
||||
// Disk name.
|
||||
|
||||
+8
-1
@@ -61,7 +61,9 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
|
||||
if in.Disks != nil {
|
||||
in, out := &in.Disks, &out.Disks
|
||||
*out = make([]Disk, len(*in))
|
||||
copy(*out, *in)
|
||||
for i := range *in {
|
||||
(*in)[i].DeepCopyInto(&(*out)[i])
|
||||
}
|
||||
}
|
||||
if in.Networks != nil {
|
||||
in, out := &in.Networks, &out.Networks
|
||||
@@ -100,6 +102,11 @@ func (in *ConfigSpec) DeepCopy() *ConfigSpec {
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *Disk) DeepCopyInto(out *Disk) {
|
||||
*out = *in
|
||||
if in.BootOrder != nil {
|
||||
in, out := &in.BootOrder, &out.BootOrder
|
||||
*out = new(int)
|
||||
**out = **in
|
||||
}
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Disk.
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// Package v1alpha1 defines strategy.backups.cozystack.io API types.
|
||||
//
|
||||
// Group: strategy.backups.cozystack.io
|
||||
// Version: v1alpha1
|
||||
package v1alpha1
|
||||
|
||||
import (
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
func init() {
|
||||
SchemeBuilder.Register(func(s *runtime.Scheme) error {
|
||||
s.AddKnownTypes(GroupVersion,
|
||||
&Kubernetes{},
|
||||
&KubernetesList{},
|
||||
)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
const (
|
||||
KubernetesStrategyKind = "Kubernetes"
|
||||
)
|
||||
|
||||
// +kubebuilder:object:root=true
|
||||
// +kubebuilder:subresource:status
|
||||
// +kubebuilder:resource:scope=Cluster
|
||||
|
||||
// Kubernetes defines a backup strategy for a tenant Kubernetes application.
|
||||
// A tenant cluster keeps its state in two places the platform can reach: the
|
||||
// cluster's key prefix in the tenant's shared etcd DataStore, and host-side
|
||||
// objects (control-plane PKI Secrets, the application HelmReleases, and the
|
||||
// DataVolumes that back the tenant's PersistentVolumes). The strategy captures
|
||||
// the first with a one-shot Job and the second with Velero. The two are taken
|
||||
// one after another, so a backup is crash-consistent at best.
|
||||
type Kubernetes struct {
|
||||
metav1.TypeMeta `json:",inline"`
|
||||
metav1.ObjectMeta `json:"metadata,omitempty"`
|
||||
|
||||
Spec KubernetesSpec `json:"spec,omitempty"`
|
||||
Status KubernetesStatus `json:"status,omitempty"`
|
||||
}
|
||||
|
||||
// +kubebuilder:object:root=true
|
||||
|
||||
// KubernetesList contains a list of Kubernetes backup strategies.
|
||||
type KubernetesList struct {
|
||||
metav1.TypeMeta `json:",inline"`
|
||||
metav1.ListMeta `json:"metadata,omitempty"`
|
||||
Items []Kubernetes `json:"items"`
|
||||
}
|
||||
|
||||
// KubernetesSpec specifies the desired behavior of a Kubernetes backup strategy.
|
||||
type KubernetesSpec struct {
|
||||
// Template holds a PodTemplateSpec for the Job that dumps the cluster's
|
||||
// key prefix from its etcd DataStore to object storage, writes it back on
|
||||
// restore, and deletes the object on cleanup. The Job runs in the
|
||||
// application namespace. Helm-like Go templates are supported in every
|
||||
// string field and are rendered against:
|
||||
// - `.Release.Name` and `.Release.Namespace` — the application's name and
|
||||
// namespace; `.Release.Prefix` is `kubernetes-`.
|
||||
// - `.Mode` — `"backup"`, `"restore"` or `"cleanup"`.
|
||||
// - `.ObjectKey` — the object-storage key of this backup's dump.
|
||||
// - `.Datastore.Prefix` — the cluster's key prefix, without slashes.
|
||||
// - `.Datastore.Endpoints` — comma-separated etcd client URLs.
|
||||
// - `.Datastore.CertificateSecret` — the Secret holding the cluster's
|
||||
// etcd client certificate (`ca.crt`, `server.crt`, `server.key`).
|
||||
// - `.Parameters` — the resolved BackupClass strategy parameters.
|
||||
//
|
||||
// Stored schemaless for the same CRD-size reason as redis_types.go.
|
||||
//
|
||||
// +kubebuilder:validation:Schemaless
|
||||
// +kubebuilder:pruning:PreserveUnknownFields
|
||||
Template corev1.PodTemplateSpec `json:"template"`
|
||||
|
||||
// Velero holds the Velero Backup and Restore specs for the host-side
|
||||
// objects and volumes. Rendered against `.Application` and `.Parameters`,
|
||||
// like the Velero strategy.
|
||||
//
|
||||
// +kubebuilder:validation:Schemaless
|
||||
// +kubebuilder:pruning:PreserveUnknownFields
|
||||
Velero VeleroTemplate `json:"velero"`
|
||||
|
||||
// CertificateWarningThreshold is how close to expiry a control-plane
|
||||
// certificate may be before a backup records a warning. Defaults to 720h.
|
||||
// +optional
|
||||
CertificateWarningThreshold *metav1.Duration `json:"certificateWarningThreshold,omitempty"`
|
||||
}
|
||||
|
||||
type KubernetesStatus struct {
|
||||
Conditions []metav1.Condition `json:"conditions,omitempty"`
|
||||
}
|
||||
+103
@@ -839,6 +839,109 @@ func (in *KafkaStatus) DeepCopy() *KafkaStatus {
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *Kubernetes) DeepCopyInto(out *Kubernetes) {
|
||||
*out = *in
|
||||
out.TypeMeta = in.TypeMeta
|
||||
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
|
||||
in.Spec.DeepCopyInto(&out.Spec)
|
||||
in.Status.DeepCopyInto(&out.Status)
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Kubernetes.
|
||||
func (in *Kubernetes) DeepCopy() *Kubernetes {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(Kubernetes)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
|
||||
func (in *Kubernetes) DeepCopyObject() runtime.Object {
|
||||
if c := in.DeepCopy(); c != nil {
|
||||
return c
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *KubernetesList) DeepCopyInto(out *KubernetesList) {
|
||||
*out = *in
|
||||
out.TypeMeta = in.TypeMeta
|
||||
in.ListMeta.DeepCopyInto(&out.ListMeta)
|
||||
if in.Items != nil {
|
||||
in, out := &in.Items, &out.Items
|
||||
*out = make([]Kubernetes, len(*in))
|
||||
for i := range *in {
|
||||
(*in)[i].DeepCopyInto(&(*out)[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KubernetesList.
|
||||
func (in *KubernetesList) DeepCopy() *KubernetesList {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(KubernetesList)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object.
|
||||
func (in *KubernetesList) DeepCopyObject() runtime.Object {
|
||||
if c := in.DeepCopy(); c != nil {
|
||||
return c
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *KubernetesSpec) DeepCopyInto(out *KubernetesSpec) {
|
||||
*out = *in
|
||||
in.Template.DeepCopyInto(&out.Template)
|
||||
in.Velero.DeepCopyInto(&out.Velero)
|
||||
if in.CertificateWarningThreshold != nil {
|
||||
in, out := &in.CertificateWarningThreshold, &out.CertificateWarningThreshold
|
||||
*out = new(v1.Duration)
|
||||
**out = **in
|
||||
}
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KubernetesSpec.
|
||||
func (in *KubernetesSpec) DeepCopy() *KubernetesSpec {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(KubernetesSpec)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *KubernetesStatus) DeepCopyInto(out *KubernetesStatus) {
|
||||
*out = *in
|
||||
if in.Conditions != nil {
|
||||
in, out := &in.Conditions, &out.Conditions
|
||||
*out = make([]v1.Condition, len(*in))
|
||||
for i := range *in {
|
||||
(*in)[i].DeepCopyInto(&(*out)[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KubernetesStatus.
|
||||
func (in *KubernetesStatus) DeepCopy() *KubernetesStatus {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(KubernetesStatus)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *MariaDB) DeepCopyInto(out *MariaDB) {
|
||||
*out = *in
|
||||
|
||||
@@ -39,6 +39,10 @@ type WorkloadMonitorSpec struct {
|
||||
// that is not operational because a DataVolume it selects is not ready.
|
||||
const WorkloadMonitorReasonDataVolumeNotReady = "DataVolumeNotReady"
|
||||
|
||||
// WorkloadMonitorReasonBucketClaimNotReady is the status Reason of a monitor
|
||||
// that is not operational because a BucketClaim it selects is not provisioned.
|
||||
const WorkloadMonitorReasonBucketClaimNotReady = "BucketClaimNotReady"
|
||||
|
||||
// WorkloadMonitorStatus defines the observed state of WorkloadMonitor
|
||||
type WorkloadMonitorStatus struct {
|
||||
// Operational indicates if the workload meets all operational requirements
|
||||
|
||||
@@ -27,16 +27,22 @@ Requires `git` and an authenticated `gh` on PATH, and a remote called `origin`.
|
||||
|
||||
Build the binary wherever an exit code matters: `go run` turns any non-zero exit into its own `1`, so under it a check that could not be completed looks the same as one that found something missing.
|
||||
|
||||
Output is written before a push or a publish. Once a write to stdout or stderr fails, a closed pipe included, the run changes nothing more: no push, no GitHub write, no local branch move, and no new pick; a pick already in flight still finishes its rollback. It exits 2 where it would have exited 0, saying on stderr, while it still can, that the output was lost. A confirmation lost after a push or a publish fails the run the same way.
|
||||
|
||||
### run
|
||||
|
||||
Switches the current working tree to `backport-batch/<line>-<yyyymmdd>`, created at `origin/<line>`, and picks onto it one original at a time, in the order they merged into `main`, as one unit each on the branch's first-parent line. A pull request merged into `main` with a merge commit `M` goes on as its own commits, `git rev-list --reverse --topo-order --no-merges M^1..M^2`, each picked with `git cherry-pick -x --signoff`, so each keeps its author, message and sign-offs and gains the `(cherry picked from commit …)` line; the merges that brought `main` into its branch are left out, since `main` already has what they brought. A merge commit then joins the picks to the batch: its first parent is the batch before the original, its second the last pick, and its message is `M`'s with a paragraph of its own, `Backported-from: M`. A pull request squash-merged into `main` goes on as one pick. When any commit of an original conflicts, the pick is aborted, the branch goes back to where it was before that original, and the original is recorded with the commit that failed and its files; the run goes on with the next one. A commit the branch already carries is skipped as empty, and an original none of whose commits changes anything is reported as carried. The tree has to be clean. A local branch of that name is reset to where the run starts; when that drops commits only it has, the run first prints its tip and the `git branch` command that restores them, and so does `--defer`. Signing follows your git configuration; hooks are off for the picks and the merge, so each commit carries its message as git writes it.
|
||||
|
||||
The pull request body goes to stdout, built from the line's own `.github/PULL_REQUEST_TEMPLATE.md`: the originals on the branch, each with how many commits it brought and which of them differ from the commit they were picked from by `git patch-id`, a resolution or a pick git had to fit, the deferred conflicts with the commit that failed and its files, the skipped pull requests counted by rule with the lists collapsed, the originals' release notes, and two blocks of JSON: the [line's record](#the-lines-record-and-reports) in `backport-batch`, which later runs, `check` and `notify` act on, and the run's report in `backport-batch-report`. The title is `fix(backport): batch for <line>, <date>`. A body that would pass GitHub's 65536-byte limit leaves out its longest skipped lists, and the run prints them on stderr instead; one that what it has to carry still puts over the limit is refused before anything is pushed.
|
||||
The pull request body goes to stdout, built from the line's own `.github/PULL_REQUEST_TEMPLATE.md`: the originals on the branch, each with how many commits it brought and which of them differ from the commit they were picked from by `git patch-id`, a resolution or a pick git had to fit, the deferred conflicts with the commit that failed and its files, what is parked and why, the skipped pull requests counted by rule with the lists collapsed, the originals' release notes, and two blocks of JSON. An original whose unit differs from it, a pick whose patch-id differs from its commit's or a unit whose net patch-id differs from the original merge's, as when a first commit came out empty because the line already had that change, keeps its release note out of the release-note block: it goes under "Check these notes", with a `git range-diff --creation-factor=999 <commit>^! <pick>^!` for each pick that differs, which pairs the two however much the pick lost, and the `git diff` of the original's merge and of its unit, and the record and the report mark it `changed`. The two blocks of JSON are the [line's record](#the-lines-record-and-reports) in `backport-batch`, which later runs, `check` and `notify` act on, and the run's report in `backport-batch-report`. The title is `fix(backport): batch for <line>, <date>`. A body that would pass GitHub's 65536-byte limit leaves out its longest skipped lists, and the run prints them on stderr instead; one that what it has to carry still puts over the limit is refused before anything is pushed.
|
||||
|
||||
`--onto SHA` continues a batch already pushed instead of starting a new one. SHA has to be the head the remote has for a batch branch of the line right now, read with `git ls-remote`, so a head someone pushed onto since is refused. What that branch carries stays, and only what it does not carry yet is picked on top, so the next push is a fast-forward. `--continue` takes SHA from the open batch pull request, and starts a new batch when none is open, which is what the scheduled run does.
|
||||
|
||||
`--no-workflow-files` is for a token that may not push a change to `.github/workflows/`, which the scheduled run's App token may not. An original that changes anything there is not picked: the body lists it under "Waiting for a maintainer" with those files, and the `backport-batch` block records it with `"workflows": true`, so `check`, `notify` and a run that continues the batch know it waits. It is never a conflict. A run without the flag, a maintainer's with their own token, picks it like any other original, onto the open batch when it continues one.
|
||||
|
||||
A clean pick can still carry only part of its original: git deletes a file the line never had without a word, and leaves alone a file the line lacks. So every path the original modifies or deletes, `M` or `D` in `git diff --name-status --no-renames M^1 M`, is judged at its counterpart on the line: the path itself, or the one a rename `git diff -M` finds between the line's fork point and `M^1` on `main`, between the fork point and the line, or both in turn. A modified path counts as left without effect when its counterpart exists neither before nor after the pick; when the pick leaves an existing file as it was, the file counts as there only when its blob is the original's, binary files included, or when `git merge-file` of the original's preimage, the line's file and the original's postimage merges cleanly back into the line's file. A deleted path counts as left without effect when its counterpart is absent before a pick that changes something, a deletion the line already made being no change of that pick, or still there after it, whatever the pick changed in it; when the original adds that path itself, as when the line made the same rename, it is judged as a modified file against what the original makes of that path. Picks that change nothing carry the original only when the line already has what it does to every file it modifies or deletes, so a change made where its branch merged `main`, which no pick brings, is still missing. What cannot be judged, a mode or type change, content `merge-file` refuses, or a rename git skipped looking for, is shown with the original and never counts against it. An original whose net merge onto the line, `git merge-tree --merge-base=M^1`, leaves something without effect is not tried, and one whose picks do is taken back off the branch: either is parked as partial, listed under "Parked" with those paths, and its release note stays out of the body. A conflict is never called partial. `backport/include` lifts no park.
|
||||
|
||||
Before it tries an original, `run` looks at what the original [builds on](#what-an-original-builds-on) that the branch lacks, in merge order, so what became of each prerequisite in this run is known. A prerequisite the line or the open batch carries, or that this run picked or found empty, counts as there; once earlier picks moved the branch, the original is judged at the branch's head, so a line another unit brought counts too. Of what is left, a direct push, an original rules 2 to 4, the line's start, `backport/skip` or a drop keep out, and one that is itself parked never come: the original is parked, not tried, and listed under "Parked" with what it builds on. In the scheduled mode an original that changes a workflow, whose missing dependencies that never come are only tests or docs and whose workflow lines build on nothing missing, waits for a maintainer instead, with a note saying so. Prerequisites still to come, a conflict, a deferral, a wait for a maintainer, one rule 6 holds, make the original wait: it is listed under "Waiting on another original", recorded with `needs`, the prerequisites of the kind that decides, in the order not on the line, waiting for a maintainer, deferred, rule 6, and tried again by a later run. A reason given with `--defer` comes before all of this, and a partial park before any of it.
|
||||
|
||||
`--defer N[,N] --reason TEXT` takes originals out of the open batch pull request, usually the ones a failing check was pinned on. It builds the batch branch again off `origin/<line>` without their units: while the batch still sits on the line's head, the units before the first one it drops stay as they are, and every later one, a pushed resolution included, is picked again in its own shape, a merge's picks one by one and then the merge with its message. When dropping one leaves a later commit that no longer applies, the run stops and names that commit's original, to defer as well. It adds no new original. It also takes out an original that got `backport/skip` after the batch picked it, which a run never removes on its own and the body lists as still on the branch. The body records each deferred original with the reason in the `backport-batch` block; a run that continues this batch keeps them out, and the next batch tries them again. A batch that carries a merge commit other than an original's, as GitHub's update-branch button makes, is refused, since picking such a merge again would lose what it resolved. When it takes out everything the batch carries, there is nothing left to push, and `--push` closes the batch pull request instead, as [the line's record](#the-lines-record-and-reports) describes.
|
||||
|
||||
`--drop N[,N] --reason TEXT` drops originals from the line: where `--defer` holds one out of this batch, a drop keeps it out of every later batch of the line too, until `--undrop N[,N]` withdraws it. A drop is recorded in the [line's record](#the-lines-record-and-reports), so both need an open batch pull request that carries one, and exit 2 without it; once a batch merges, its drops are carried into the next batch's record. A dropped original the open batch carries is taken off it as `--defer` would, and the rebuild is pushed the same way; any other drop or undrop changes only the record, so the batch stays as it is even when the line moved since. An undropped original is missing until the next run tries it. A drop names the original's merge commit on `main` as well as its number, and a record whose drop does not match what that merge commit merged stops every command with exit 2. An original the line already carries cannot be dropped, and neither can a number that merged into `main` before the line branched. The order is the rules', then the drop: what `backport/skip`, rules 2 to 4 or the line itself decide comes first, with the drop kept on record beside it, and `backport/include` does not lift a drop. The body lists dropped originals under their own heading among the skipped ones, `check` prints a note for each, and `fold` refuses one.
|
||||
@@ -51,18 +57,26 @@ Exits 1 and prints the URL of every pull request the rules take that is neither
|
||||
|
||||
An original with `backport/skip` that the open batch picked before the label came prints a warning in either mode, naming the batch and `run --defer`, which takes it out; it fails neither.
|
||||
|
||||
Every other original the policy takes that neither the line nor the open batch carries is decided in merge order, as [`run`](#run) decides it, with the open batch's found picks counting as there and the mode the open record saved, the scheduled run's when no batch is open, whose workflow waits on a closed record wait on nothing any more, so the original is judged as the scheduled run judges it: deferred for a reason, a warning; parked, a note naming what it builds on; in the scheduled mode a workflow original whose tests alone build on what never comes, waiting as the open batch lists it, or missing with none; and one that waits on other originals by the prerequisite that decides it: missing, which fails both modes, waiting for a maintainer, which warns and fails `--cut`, deferred, which warns, or held by rule 6, a note. A wait on one that waits itself inherits what decides that one.
|
||||
|
||||
A unit on the open batch that leaves part of its original without effect, judged as [`run`](#run) judges a pick, at the unit's own first parent and whatever its original's rule, label or drop, prints a warning naming what it leaves out, and fails `--cut` ahead of any skip, drop or stale warning: take it out with `run --defer`. An original that is parked as partial prints as a note, `#N is parked: only part of it applies`, and fails neither. That includes one the open record parks as partial that the net merge at the line's head finds whole, since `run`'s picker found it after the picks before it: `check` and the operations that only change the record keep it parked while its net merge onto what the batch carries now still leaves something without effect, and call it missing once that went, as when the pick that made it partial was taken off. A park only the commit-by-commit replay sees, which no net merge shows, reads as missing in `check` until the next run.
|
||||
|
||||
### fold
|
||||
|
||||
Checks a hand-made resolution of a deferred conflict before it is pushed onto the batch, in the shape the run gives an original. For a merged pull request `--commit` is a merge whose first parent is the head of the open batch pull request and whose message names the original in exactly one `Backported-from:` trailer; its second parent ends a straight line from that head of picks of the pull request's own commits, in their order, each ending in the `(cherry picked from commit …)` line of the commit it came from, none claiming another commit of the same original, a commit of another pull request never among them. For a squash-merged one it is a single commit on that head whose `(cherry picked from commit …)` line names the original's commit on `main`. Either has to change something, and only files the original changes. The [conflict task](../../docs/release/backport-tasks/resolve-conflict.md) gives the commands. fold prints `ok` and exits 0, or prints why not and exits 1. It never commits or pushes; push one checked resolution at a time, since the next one has to sit on the new head.
|
||||
Checks a hand-made resolution of a deferred conflict before it is pushed onto the batch, in the shape the run gives an original. For a merged pull request `--commit` is a merge whose first parent is the head of the open batch pull request and whose message names the original in exactly one `Backported-from:` trailer; its second parent ends a straight line from that head of picks of the pull request's own commits, in their order, each ending in the `(cherry picked from commit …)` line of the commit it came from, none claiming another commit of the same original, a commit of another pull request never among them. For a squash-merged one it is a single commit on that head whose `(cherry picked from commit …)` line names the original's commit on `main`. An original parked for what it builds on folds like any other when the resolution is complete. Either has to change something, and only files the original changes or the paths a rename on `main` or on the line gave them, and has to leave nothing the original modifies or deletes without effect, judged as [`run`](#run) judges a pick: a resolution that ports what the line has and leaves out what it lacks is refused, since such a port goes to the line as a pull request of its own. The [conflict task](../../docs/release/backport-tasks/resolve-conflict.md) gives the commands. fold prints `ok` and exits 0, or prints why not and exits 1. It never commits or pushes; push one checked resolution at a time, since the next one has to sit on the new head.
|
||||
|
||||
### verify
|
||||
|
||||
Holds every unit of a batch to the shape fold checks for one, the clean picks as well as resolutions: it walks the batch's first-parent line from `origin/<line>` to `--head`, or to the head of the open batch pull request, and requires each unit to be one original, merged into `main` after the line branched, in that shape, and no original twice. A commit that carries no original fails it, and so does a merge of the line into the batch. It prints one line per unit, and exits 0 when all pass, 1 when one does not, and 2 when it cannot tell. With `--head` it reads git alone, nothing from GitHub. fold runs only where someone resolved a conflict and chose to run it; [`pr-labeler.yaml`](../../.github/workflows/pr-labeler.yaml) runs verify on every push to a batch pull request, from a build of `main`, with the batch head fetched as data.
|
||||
Holds every unit of a batch to the shape fold checks for one, the clean picks as well as resolutions: it walks the batch's first-parent line from `origin/<line>` to `--head`, or to the head of the open batch pull request, and requires each unit to be one original, merged into `main` after the line branched, in that shape, and no original twice. A commit that carries no original fails it, and so does a merge of the line into the batch, and a unit that leaves part of its original without effect, judged as [`run`](#run) judges a pick. It prints one line per unit, and exits 0 when all pass, 1 when one does not, and 2 when it cannot tell. With `--head` it reads git alone, nothing from GitHub. fold runs only where someone resolved a conflict and chose to run it; [`pr-labeler.yaml`](../../.github/workflows/pr-labeler.yaml) runs verify on every push to a batch pull request, from a build of `main`, with the batch head fetched as data.
|
||||
|
||||
### notify
|
||||
|
||||
Reads the [line's record](#the-lines-record-and-reports) first, as the other commands do, and exits 2 when it cannot read it or when it says the open batch is being closed, with a batch open or not. Then it reads the open batch pull request of the line, trusted as described below, and prints one line for each thing that waits on it: a batch open for three days or more, the shortest gap between two scheduled runs, so one the previous run already saw; the conflicts it deferred; and the originals waiting for a maintainer because they change workflows. Nothing open prints nothing.
|
||||
|
||||
An original the record has wait on others is listed once, as `#M, after #F`, where what decides its wait is listed: after a deferred conflict with the conflicts, after a workflow wait with the waits; one waiting on what is missing or held by rule 6 is not listed. A unit on the batch that carries only part of its original is listed apart, to be taken out before the cut.
|
||||
|
||||
Each park the record of the open batch holds is named once. `run` marks a park `announced` when the line's record it found, open or closed, holds the same park announced: the same merge, kind, prerequisites, commits and files; a park that changed is news again. `notify` lists the parks not yet marked, and with `--push`, once the comment is written, marks them in the record with `gh pr edit --body-file`, only while the pull request is open and its body still carries the record `notify` read. A mark that fails after the comment exits 2, and the next `notify` names those parks once more. With no batch open nothing is named, and the next batch's first `notify` names what it parks.
|
||||
|
||||
With `--push` it writes the same into one comment on the batch pull request: the conflicts, each to be resolved as the [conflict task](../../docs/release/backport-tasks/resolve-conflict.md) describes, and the originals that wait because they change workflows.
|
||||
|
||||
The comment starts with `<!-- backport-batch:notify -->` and is one per pull request: a later run edits it in place rather than adding another, and once nothing waits it says so. Only a comment whose author the batch trusts counts as it, so one someone else wrote with the same first line is neither edited nor believed. It mentions `--mention`, an `org/team` or a login, with or without `@`, only while something waits. A failed batch CI is not among what it reports: the run has no trigger on the batch's CI finishing.
|
||||
@@ -105,6 +119,15 @@ Only the line's own commits count, the ones after the point where it branched fr
|
||||
|
||||
Titles and bodies are read from the merge commits on `main`, which hold them as a maintainer merged them; the live pull request stays editable by its author.
|
||||
|
||||
## What an original builds on
|
||||
|
||||
An original the policy takes can build on code its line does not have: another pull request merged into `main` after the line branched that the line never got, or a commit pushed to `main` directly. For each original the policy takes that neither the line nor the open batch carries, `run` reads what it builds on from git alone, in two ways:
|
||||
|
||||
- A file it modifies or deletes whose counterpart, as [`run`](#run) follows renames, the line does not have is named by the first-parent commit on `main` that added it since the line branched: `git --literal-pathspecs log --first-parent --diff-merges=first-parent --no-renames --diff-filter=A -1 <fork point>..M^1 -- <path>`. A file nothing added since the fork point is shown with the original, since no other pull request brought it.
|
||||
- A line it removes or changes in a file the line has is given by `git blame --porcelain -w`, at `M^1` named by its full commit, to a commit, which counts through the first-parent commit on `main` that brought it: each commit of `main`'s first-parent line since the fork point brings itself and every commit of the range reachable from its other parents that no earlier one brought. A commit from before the fork point, which the line has, and the original's own count for nothing. The commit counts only while one of its lines, with its edges trimmed and nothing else, is absent from the line's version of the file, so a hand backport under another commit clears it. A file the line already has as the original makes it, the same blob or a clean `git merge-file` back into the line's file, builds on nothing.
|
||||
|
||||
Lines a tool wrote are not blamed: a generated file whole, `zz_generated*`, one with a `Code generated … DO NOT EDIT` marker in its first 100 lines, `go.sum` and the package-manager lock files, and in an ApplicationDefinition under `packages/system/*-rd/cozyrds/` the keys `hack/update-crd.sh` writes, found by their indentation. Controller-gen CRDs and `values.schema.json` are blamed like any other file. Each `git log`, `git diff` and `git blame` the detector and the rename detection run has two minutes, and rename detection that runs out of time counts as skipped, and an original has at most 256 files, missing or blamed, 20 000 blamed lines and 8 MiB per file read for it. Evidence it cannot read, past one of those bounds, a binary file, a symlink, a submodule, a commit blame names that did not come through `main`, a path that is not UTF-8, or a rename git skipped looking for, is shown with the original, whatever becomes of it, under its line in the body and in the report's `diag`. Output of git the tool cannot read whole stops it with exit 2, and is never taken for an absent dependency. A dependency carried only by lines an original adds is not seen. `run` prints how long reading the line took on stderr. What follows from a dependency, a park or a wait, is in [`run`](#run) and [`check`](#check).
|
||||
|
||||
## The line's record and reports
|
||||
|
||||
The line's record is the `backport-batch` block of its newest batch pull request: the newest pull request against the line from a `backport-batch/<line>-<yyyymmdd>` branch of this repository, opened by an author the batch trusts as above, whether it is open, merged or closed. `run`, `check`, `fold` and `notify` find it in GitHub's list of the line's pull requests, newest first, reading at most ten pages of 100. A batch pull request a person opened without the block is passed over. A block whose author the batch does not trust, a bot's batch without its block, a block that cannot be read whole, a closed record newer than the open batch, or ten pages with no record stop the command with exit 2, naming the pull request, rather than fall back to an older record.
|
||||
@@ -113,7 +136,7 @@ The record is written only while its pull request is open: a merged or closed ba
|
||||
|
||||
The record is only as trustworthy as the body it sits in. Anyone with write access can edit any body, the bot's included, and neither the bot's authorship nor anything in the block proves who edited it last. A hand edit that leaves a readable record is believed; one that breaks it stops every command with exit 2, naming the pull request. A reason or a file name in the record counts only in the form a run writes it, one line with no control, zero-width or bidi character, so text someone typed into the body never reaches a terminal, a body or a comment as they typed it. When the newest batch pull request is deleted and no later one exists, nothing shows it was there: the older record governs, and what was recorded since is lost.
|
||||
|
||||
The record holds only what later commands act on. Every run that classifies the line also makes a report, one line of JSON in a `backport-batch-report` block: every original merged into `main` since the line branched in exactly one category, picked, missing, deferred with what it was deferred for, or skipped with the rule that skipped it, each drop the record keeps with the merge commit it is bound to, its reason and the batch pull request that recorded it, the commits on `main` that merged no pull request, the labels, and what the classification was made of: the heads of `main`, the line and the newer line, the policy, the batch head it describes, the tool's commit, the Actions run and the flags. Its ID is a hash of all that but the time and the run, so two runs that find the same make the same report, and it names no pull request, so a new batch's report is whole before GitHub numbers the pull request. The body carries the latest report beside the record, and a comment on the open batch pull request keeps each one: `<!-- backport-batch:report <id> -->` and the same block, folded away. A report already posted is not posted again, and the tool never edits or deletes such a comment, so a report the next run replaces in the body is still read back by `report`, also after the batch merged. A comment that looks like one but whose author the batch does not trust is not read. When a run stops after writing the body but before posting its comment, the next `run --push` on the open batch posts that report first, byte for byte; once the batch is closed nothing is written to it, and the report stays in its body. A run that picks nothing while no batch is open keeps no report.
|
||||
The record holds only what later commands act on. Version 3, which this tool writes, adds what is parked and why, with the merge commit each park is bound to, the units on the batch that do only part of their original, the picks that differ from theirs, and what each wait on another original waits on; it reads versions 1 to 3, and a tool that reads only 1 and 2 refuses version 3 with exit 2. Every run that classifies the line also makes a report, one line of JSON in a `backport-batch-report` block: every original merged into `main` since the line branched in exactly one category, picked, missing, deferred with what it was deferred for, or skipped with the rule that skipped it, each drop the record keeps with the merge commit it is bound to, its reason and the batch pull request that recorded it, the commits on `main` that merged no pull request, the labels, and what the classification was made of: the heads of `main`, the line and the newer line, the policy, the batch head it describes, the tool's commit, the Actions run and the flags. Its ID is a hash of all that but the time and the run, so two runs that find the same make the same report, and it names no pull request, so a new batch's report is whole before GitHub numbers the pull request. The body carries the latest report beside the record, and a comment on the open batch pull request keeps each one: `<!-- backport-batch:report <id> -->` and the same block, folded away. A report already posted is not posted again, and the tool never edits or deletes such a comment, so a report the next run replaces in the body is still read back by `report`, also after the batch merged. A comment that looks like one but whose author the batch does not trust is not read. When a run stops after writing the body but before posting its comment, the next `run --push` on the open batch posts that report first, byte for byte; once the batch is closed nothing is written to it, and the report stays in its body. A run that picks nothing while no batch is open keeps no report.
|
||||
|
||||
A `--defer` that leaves the batch carrying nothing closes it, and writes everything while the pull request is still open: the report comment, then the body, whose record keeps the deferrals and says the batch is closing, with its number, branch and the head the rebuild started from. It then reads the pull request and the branch again, closes the pull request while both are at that head, and deletes the branch with `--force-with-lease` on it, printing `closed <url>`. The next batch tries what was deferred again. A close that stops part way is finished by the next `run --push`, before it does anything else: it closes and deletes when the pull request is open at the frozen head, deletes the branch when the pull request is closed and the branch still at that head, and goes on as usual once both are gone. A run that finishes a close stops there, with exit 0, or with exit 2 when it was asked for `--defer`, `--drop` or `--undrop`, which it does not apply. It never takes a head it did not freeze: with the branch moved, it stops with exit 2 and names both heads. `run` without `--push`, `check` and `fold` stop with exit 2 while the pull request is still open; once it is closed they go on, warning about a branch the close left, and `notify` says nothing. A closed pull request is never written to, so its record says it was closing for good, and a closed pull request whose branch is gone counts as closed.
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ func apiSnapshot(commit string) (apigate.Snapshot, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer os.RemoveAll(dir)
|
||||
defer func() { _ = os.RemoveAll(dir) }()
|
||||
for name, blob := range paths {
|
||||
file := filepath.Join(dir, filepath.FromSlash(name))
|
||||
if err := os.MkdirAll(filepath.Dir(file), 0o700); err != nil {
|
||||
|
||||
+157
-18
@@ -33,6 +33,8 @@ type pick struct {
|
||||
files []string
|
||||
// failed is the commit whose pick conflicted.
|
||||
failed string
|
||||
// needs are the originals still to come it waits on.
|
||||
needs []int
|
||||
// more counts the conflicting files a recorded conflict no longer names.
|
||||
more int
|
||||
reason string
|
||||
@@ -138,6 +140,7 @@ func (a *app) run(args []string) int {
|
||||
if err := fetch(); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
started := time.Now()
|
||||
b, err := a.prepare(l)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
@@ -145,6 +148,12 @@ func (a *app) run(args []string) int {
|
||||
if goOn, code := a.resumeClose(b, *push, deferred != nil || drops != nil); !goOn {
|
||||
return code
|
||||
}
|
||||
if err := b.analyze(); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
// The scheduled job has half an hour for every line; this says where it
|
||||
// goes.
|
||||
_, _ = fmt.Fprintf(a.stderr, "prepared %s in %s, %s of it judging partial units and what originals build on\n", l.branch(), time.Since(started).Round(time.Millisecond), b.detecting.Round(time.Millisecond))
|
||||
if drops != nil {
|
||||
return a.redrop(b, drops, reason, *undropArg != "", *push)
|
||||
}
|
||||
@@ -197,19 +206,62 @@ func (a *app) run(args []string) int {
|
||||
return a.fail(err)
|
||||
}
|
||||
}
|
||||
// cls is what each original became in this run, to an original that
|
||||
// builds on it; the rules decide the rest.
|
||||
mode := analysisMode{noWorkflowFiles: *noWorkflows}
|
||||
cls := map[*verdict]edgeClass{}
|
||||
classOf := func(f *verdict) edgeClass {
|
||||
if c, ok := b.ruleClass(f); ok {
|
||||
return c
|
||||
}
|
||||
if c, ok := cls[f]; ok {
|
||||
return c
|
||||
}
|
||||
return edgeMissing
|
||||
}
|
||||
for _, v := range b.qualifying() {
|
||||
on := false
|
||||
if carried != nil {
|
||||
if on, err = b.onBranch(carried, v); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case carried != nil && found(carried, v):
|
||||
case on:
|
||||
r.onBranch = append(r.onBranch, v)
|
||||
cls[v] = edgeSatisfied
|
||||
continue
|
||||
case held[v.o.number].Reason != "":
|
||||
r.held = append(r.held, pick{v: v, status: "held", reason: held[v.o.number].Reason})
|
||||
cls[v] = edgeDeferred
|
||||
continue
|
||||
case v.onBatch != "":
|
||||
v.rule, v.reason = ruleOnLine, fmt.Sprintf("on the open batch pull request #%d", b.pr.Number)
|
||||
r.skipped[ruleOnLine] = append(r.skipped[ruleOnLine], v)
|
||||
continue
|
||||
}
|
||||
if v.partial != nil {
|
||||
// Judged at the line's head; what the batch picked since may
|
||||
// supply what it lacked there.
|
||||
if err := a.partialAtHead(b, v); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if v.partial != nil {
|
||||
r.parked = append(r.parked, v)
|
||||
cls[v] = edgeNever
|
||||
continue
|
||||
}
|
||||
}
|
||||
if len(v.deps) > 0 {
|
||||
kind, err := a.dependsAt(b, r, v, mode, classOf)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if kind != edgeSatisfied {
|
||||
cls[v] = kind
|
||||
continue
|
||||
}
|
||||
}
|
||||
if *noWorkflows {
|
||||
files, err := workflowFiles(v.commits)
|
||||
if err != nil {
|
||||
@@ -217,18 +269,30 @@ func (a *app) run(args []string) int {
|
||||
}
|
||||
if len(files) > 0 {
|
||||
r.waiting = append(r.waiting, pick{v: v, status: "waiting", files: files})
|
||||
cls[v] = edgeWorkflow
|
||||
continue
|
||||
}
|
||||
}
|
||||
p, err := pickOriginal(v)
|
||||
p, err := a.pickUnit(v, func(base, head string) ([]string, error) {
|
||||
omitted, diag, err := b.omittedEffects(v, base, head)
|
||||
v.addDiag(diag...)
|
||||
return omitted, err
|
||||
})
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
switch p.status {
|
||||
case "partial":
|
||||
v.partial = p.files
|
||||
v.park = &parkedEntry{Original: v.o.number, Merge: v.o.merge, Kind: parkPartial, Files: sortedClean(p.files)}
|
||||
r.parked = append(r.parked, v)
|
||||
cls[v] = edgeNever
|
||||
case "picked":
|
||||
r.onBranch = append(r.onBranch, v)
|
||||
cls[v] = edgeSatisfied
|
||||
case "conflict":
|
||||
r.deferred = append(r.deferred, p)
|
||||
cls[v] = edgeDeferred
|
||||
case "empty":
|
||||
v.rule, v.reason = ruleOnLine, "the earlier picks already carry it"
|
||||
r.skipped[ruleOnLine] = append(r.skipped[ruleOnLine], v)
|
||||
@@ -246,10 +310,67 @@ func (a *app) run(args []string) int {
|
||||
if !descends {
|
||||
return a.fail(fmt.Errorf("%.12s does not descend from --onto %.12s", head, *onto))
|
||||
}
|
||||
if err := b.markPartial(r, carried.head); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
}
|
||||
return a.finish(b, r, branch, date, *push, false)
|
||||
}
|
||||
|
||||
// dependsAt decides, from what v builds on that the branch lacks, whether
|
||||
// the run leaves it out: waiting for a maintainer in the scheduled mode
|
||||
// when only tests or docs build on what never comes, parked when anything
|
||||
// else never comes, or waiting on prerequisites still to come. What is
|
||||
// there, the line's, the batch's, or what this run picked or found empty,
|
||||
// counts for nothing; once earlier picks moved the branch, v is judged at
|
||||
// its head. It returns what v became to an original that builds on it,
|
||||
// edgeSatisfied when the run goes on to pick it.
|
||||
func (a *app) dependsAt(b *batch, r *report, v *verdict, mode analysisMode, classOf func(*verdict) edgeClass) (edgeClass, error) {
|
||||
deps := v.deps
|
||||
head, err := resolve("HEAD")
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if head != b.lineHead {
|
||||
if deps, err = b.depsAt(v, head); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
never, pending := edges(deps, classOf)
|
||||
primary, err := workflowPrimary(mode, v, never, pending)
|
||||
switch {
|
||||
case err != nil:
|
||||
return 0, err
|
||||
case primary:
|
||||
files, err := workflowFiles(v.commits)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
v.addDiag(testsOnly(v, never, b.line))
|
||||
r.waiting = append(r.waiting, pick{v: v, status: "waiting", files: files})
|
||||
return edgeWorkflow, nil
|
||||
case len(never) > 0:
|
||||
v.park = dependencyPark(v, never)
|
||||
r.parked = append(r.parked, v)
|
||||
return edgeNever, nil
|
||||
case len(pending) > 0:
|
||||
class, needs := strongest(pending)
|
||||
r.needs = append(r.needs, pick{v: v, status: "waiting", needs: numbers(needs)})
|
||||
return class, nil
|
||||
}
|
||||
return edgeSatisfied, nil
|
||||
}
|
||||
|
||||
// partialAtHead judges v again where the run has got to, when earlier picks
|
||||
// moved the branch past the line's head.
|
||||
func (a *app) partialAtHead(b *batch, v *verdict) error {
|
||||
head, err := resolve("HEAD")
|
||||
if err != nil || head == b.lineHead {
|
||||
return err
|
||||
}
|
||||
return b.partialAt(v, head)
|
||||
}
|
||||
|
||||
// keptOut lists the originals with backport/skip, or dropped, that the batch
|
||||
// still carries: a run never takes a commit off the batch, --defer does.
|
||||
func keptOut(b *batch, carried *history) ([]*verdict, error) {
|
||||
@@ -261,7 +382,11 @@ func keptOut(b *batch, carried *history) ([]*verdict, error) {
|
||||
if err := b.load(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if found(carried, v) {
|
||||
on, err := b.onBranch(carried, v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if on {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
@@ -276,7 +401,7 @@ func found(h *history, v *verdict) bool {
|
||||
// newReport starts the report of a run with what the rules left out and what
|
||||
// the labels overrode.
|
||||
func newReport(b *batch) *report {
|
||||
r := &report{line: b.line, newer: b.newer, skipped: map[int][]*verdict{}, other: len(b.other), start: b.start}
|
||||
r := &report{line: b.line, newer: b.newer, skipped: map[int][]*verdict{}, other: len(b.other), start: b.start, carried: b.src.rec}
|
||||
for _, v := range b.verdicts {
|
||||
if v.rule != 0 {
|
||||
r.skipped[v.rule] = append(r.skipped[v.rule], v)
|
||||
@@ -324,20 +449,20 @@ func (a *app) finish(b *batch, r *report, branch string, date time.Time, push, r
|
||||
}
|
||||
body, left := r.body(template)
|
||||
p.body = body
|
||||
fmt.Fprint(a.stdout, body)
|
||||
_, _ = fmt.Fprint(a.stdout, body)
|
||||
skipped := 0
|
||||
for _, items := range r.skipped {
|
||||
skipped += len(items)
|
||||
}
|
||||
for _, rule := range left {
|
||||
for _, v := range r.skipped[rule] {
|
||||
fmt.Fprintf(a.stderr, "skipped by %s: %s %s (%s)\n", ruleName(rule), v.o.url(), terminalSafe(v.o.title), terminalSafe(v.reason))
|
||||
_, _ = fmt.Fprintf(a.stderr, "skipped by %s: %s %s (%s)\n", ruleName(rule), v.o.url(), terminalSafe(v.o.title), terminalSafe(v.reason))
|
||||
}
|
||||
}
|
||||
for _, p := range r.waiting {
|
||||
fmt.Fprintf(a.stderr, "waiting for a maintainer: %s %s changes %s\n", p.v.o.url(), terminalSafe(p.v.o.title), terminalSafe(strings.Join(p.files, ", ")))
|
||||
_, _ = fmt.Fprintf(a.stderr, "waiting for a maintainer: %s %s changes %s\n", p.v.o.url(), terminalSafe(p.v.o.title), terminalSafe(strings.Join(p.files, ", ")))
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "%s at %.12s: %d on the branch, %d deferred, %d waiting for a maintainer, %d skipped\ntitle: %s\nreport: %s\n",
|
||||
_, _ = fmt.Fprintf(a.stderr, "%s at %.12s: %d on the branch, %d deferred, %d waiting for a maintainer, %d skipped\ntitle: %s\nreport: %s\n",
|
||||
branch, head, len(r.onBranch), len(r.deferred)+len(r.held), len(r.waiting), skipped, p.title, doc.ID)
|
||||
if !push {
|
||||
return 0
|
||||
@@ -346,8 +471,8 @@ func (a *app) finish(b *batch, r *report, branch string, date time.Time, push, r
|
||||
// Picking nothing is not a failure of the run; check still reports
|
||||
// whatever the line lacks. Merged and closed batches are never
|
||||
// written, so there is nowhere to keep what the run found.
|
||||
fmt.Fprintf(a.stderr, "%s carries nothing beyond %s; nothing to push\n", branch, b.line.branch())
|
||||
fmt.Fprintf(a.stderr, "no batch pull request is open on %s; this run's classification is not retained\n", b.line.branch())
|
||||
_, _ = fmt.Fprintf(a.stderr, "%s carries nothing beyond %s; nothing to push\n", branch, b.line.branch())
|
||||
_, _ = fmt.Fprintf(a.stderr, "no batch pull request is open on %s; this run's classification is not retained\n", b.line.branch())
|
||||
return 0
|
||||
}
|
||||
if err := a.publish(b, p, rebuilt); err != nil {
|
||||
@@ -365,7 +490,8 @@ func (r *report) lineRecord(doc reportDoc) lineRecord {
|
||||
dropped = []droppedEntry{}
|
||||
}
|
||||
slices.SortFunc(dropped, func(a, b droppedEntry) int { return a.Original - b.Original })
|
||||
return lineRecord{Version: 2, Line: doc.Line, Built: &built, Report: doc.ID, Closing: r.closing, Deferred: slices.Clone(doc.Primary.Deferred), Dropped: dropped, Stale: slices.Clone(doc.Overlays.Stale)}
|
||||
return lineRecord{Version: recordVersion, Line: doc.Line, Built: &built, Report: doc.ID, Closing: r.closing, Deferred: slices.Clone(doc.Primary.Deferred), Dropped: dropped, Stale: slices.Clone(doc.Overlays.Stale),
|
||||
Parked: carryAnnounced(doc.Primary.Parked, r.carried), Partial: slices.Clone(doc.Overlays.Partial), Changed: slices.Clone(doc.Overlays.Changed)}
|
||||
}
|
||||
|
||||
// branchDate is the date a batch branch was opened under.
|
||||
@@ -403,6 +529,9 @@ func cleanTree() error {
|
||||
// the move drops commits of and how to get them back: a clean tree says
|
||||
// nothing about commits only a local branch has.
|
||||
func (a *app) warnReset(branch, start string) error {
|
||||
if err := a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
old, err := lookup("refs/heads/" + branch)
|
||||
if err != nil || old == "" || old == start {
|
||||
return err
|
||||
@@ -411,7 +540,11 @@ func (a *app) warnReset(branch, start string) error {
|
||||
if err != nil || kept {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "resetting the local %s from %s, dropping commits %.12s does not carry; git branch %s-%.12s %s restores them\n", branch, old, start, branch, old, old)
|
||||
// The hint is the only way back to the dropped commits, so the reset does
|
||||
// not run when it could not be written.
|
||||
if _, err := fmt.Fprintf(a.stderr, "resetting the local %s from %s, dropping commits %.12s does not carry; git branch %s-%.12s %s restores them\n", branch, old, start, branch, old, old); err != nil {
|
||||
return fmt.Errorf("not resetting the local %s: %w", branch, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -593,6 +726,9 @@ func (a *app) publish(b *batch, p publication, rebuilt bool) error {
|
||||
}
|
||||
}
|
||||
if pushed != p.head {
|
||||
if err := a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := git(pushArgs(p.branch, p.head, lease)...); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -601,13 +737,13 @@ func (a *app) publish(b *batch, p publication, rebuilt bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(name)
|
||||
defer func() { _ = os.Remove(name) }()
|
||||
if b.pr == nil {
|
||||
n, url, err := a.gh.createPull(b.line.branch(), p.branch, p.title, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "opened draft %s\n", url)
|
||||
_, _ = fmt.Fprintf(a.stderr, "opened draft %s\n", url)
|
||||
if err := a.postReport(n, p.report); err != nil {
|
||||
return fmt.Errorf("posting report %.12s on #%d: %w; the body keeps it, and the next run --push posts it", p.report.doc.ID, n, err)
|
||||
}
|
||||
@@ -619,7 +755,7 @@ func (a *app) publish(b *batch, p publication, rebuilt bool) error {
|
||||
if err := a.gh.editPull(b.pr.Number, p.title, name); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "updated %s\n", pullURL(b.pr))
|
||||
_, _ = fmt.Fprintf(a.stderr, "updated %s\n", pullURL(b.pr))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -702,7 +838,7 @@ func (a *app) closeBatch(b *batch, p publication) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(name)
|
||||
defer func() { _ = os.Remove(name) }()
|
||||
if err := a.stillOpen(c.PR); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -735,7 +871,7 @@ func (a *app) finishClose(l line, url string, c closing) error {
|
||||
if err := a.deleteBranch(l, c); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "closed %s: without what is deferred it carries nothing beyond %s; the next batch tries them again\n", url, l.branch())
|
||||
_, _ = fmt.Fprintf(a.stderr, "closed %s: without what is deferred it carries nothing beyond %s; the next batch tries them again\n", url, l.branch())
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -752,6 +888,9 @@ func (a *app) deleteBranch(l line, c closing) error {
|
||||
case h != c.Head:
|
||||
return movedBranch(c, h)
|
||||
}
|
||||
if err := a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := git(deleteArgs(c.Branch, c.Head)...); err != nil {
|
||||
return fmt.Errorf("deleting %s, left by the close of #%d: %w; backport-batch run --line %s --push deletes it", c.Branch, c.PR, err, l.branch())
|
||||
}
|
||||
@@ -807,7 +946,7 @@ func (a *app) resumeClose(b *batch, push, changing bool) (bool, int) {
|
||||
if err := a.deleteBranch(b.line, c); err != nil {
|
||||
return false, a.fail(err)
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "deleted %s, which the close of #%d left\n", c.Branch, c.PR)
|
||||
_, _ = fmt.Fprintf(a.stderr, "deleted %s, which the close of #%d left\n", c.Branch, c.PR)
|
||||
return false, a.finished(c, changing)
|
||||
}
|
||||
|
||||
|
||||
+128
-10
@@ -176,6 +176,9 @@ type deferredEntry struct {
|
||||
More int `json:"more,omitempty"`
|
||||
Reason string `json:"reason,omitempty"`
|
||||
Workflows bool `json:"workflows,omitempty"`
|
||||
// Needs, from version 3, are the originals still to come that it waits
|
||||
// on, those of the kind that decides the wait.
|
||||
Needs []int `json:"needs,omitempty"`
|
||||
}
|
||||
|
||||
// How many conflicting files of one original the body names, in the list and
|
||||
@@ -194,7 +197,7 @@ type deferredRecord struct {
|
||||
// had a version. encoding/json leaves bidi and zero-width characters as they
|
||||
// are, so file names go through clean first, as the reason does.
|
||||
func deferredBlock(l line, conflicts, held, waiting []pick) string {
|
||||
data, _ := json.Marshal(deferredRecord{Line: l.branch(), Deferred: deferredEntries(conflicts, held, waiting)})
|
||||
data, _ := json.Marshal(deferredRecord{Line: l.branch(), Deferred: deferredEntries(conflicts, held, waiting, nil)})
|
||||
return deferredFence + "\n" + string(data) + "\n```"
|
||||
}
|
||||
|
||||
@@ -231,6 +234,7 @@ type report struct {
|
||||
deferred []pick // conflicts
|
||||
held []pick // what --defer took out, with the reason
|
||||
waiting []pick // what changes workflows, left to a maintainer
|
||||
needs []pick // what waits on other originals still to come
|
||||
skipped map[int][]*verdict
|
||||
other int
|
||||
// overrides are the originals a backport/ label kept out or let in, and
|
||||
@@ -238,8 +242,10 @@ type report struct {
|
||||
overrides []*verdict
|
||||
stale []*verdict
|
||||
start time.Time
|
||||
// shapes says, for an original on the branch, what it brought.
|
||||
shapes map[*verdict]string
|
||||
// shapes says, for an original on the branch, what it brought, and
|
||||
// partialFiles what a partial unit leaves without effect.
|
||||
shapes map[*verdict]unitShape
|
||||
partialFiles map[*verdict][]string
|
||||
// missing are originals the policy takes that the run neither tried nor
|
||||
// found anywhere.
|
||||
missing []*verdict
|
||||
@@ -253,6 +259,14 @@ type report struct {
|
||||
closing *closing
|
||||
// dropped are the maintainers' drops the record keeps.
|
||||
dropped []droppedEntry
|
||||
// parked are what the batch leaves out because it cannot land whole,
|
||||
// each with its park, and partial the originals on the branch whose unit
|
||||
// does only part of what they do on main.
|
||||
parked []*verdict
|
||||
partial []*verdict
|
||||
// carried is the line's record this run found, whose announcements the
|
||||
// parks that did not change keep.
|
||||
carried *lineRecord
|
||||
}
|
||||
|
||||
// ruleName is how output names what kept an original out: the policy's
|
||||
@@ -299,16 +313,16 @@ func (r *report) ruleTitle(rule int) string {
|
||||
// in details written out.
|
||||
func (r *report) what(details map[int]bool) string {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "Backports to %s from `main`, picked by the rules of the [backport policy](%s) in merge order. A merged pull request comes as its own commits, picked with `git cherry-pick -x` so each keeps its author, message and sign-offs, and a merge commit that carries the original's merge message with a `%s:` trailer naming it; a squash-merged one comes as one pick. `backport-batch verify` checks that shape. A pick listed as changed differs from the commit it came from; `git range-diff <merge>^1..<merge>^2 <batch merge>^1..<batch merge>^2` shows how. A conflict or a failing check here is a [backport task](%sREADME.md).\n\n", codeSpan(r.line.branch()), policyURL, batchTrailer, tasksURL)
|
||||
fmt.Fprintf(&b, "Backports to %s from `main`, picked by the rules of the [backport policy](%s) in merge order. A merged pull request comes as its own commits, picked with `git cherry-pick -x` so each keeps its author, message and sign-offs, and a merge commit that carries the original's merge message with a `%s:` trailer naming it; a squash-merged one comes as one pick. `backport-batch verify` checks that shape. A pick listed as changed differs from the commit it came from, and its release note waits under Check these notes. A conflict or a failing check here is a [backport task](%sREADME.md).\n\n", codeSpan(r.line.branch()), policyURL, batchTrailer, tasksURL)
|
||||
if len(r.onBranch) == 0 {
|
||||
b.WriteString("Nothing is picked yet.\n")
|
||||
}
|
||||
for _, v := range r.onBranch {
|
||||
shape := ""
|
||||
if s := r.shapes[v]; s != "" {
|
||||
shape = ", " + s
|
||||
if s, ok := r.shapes[v]; ok {
|
||||
shape = ", " + s.String()
|
||||
}
|
||||
fmt.Fprintf(&b, "- %s %s%s\n", prLink(v.o.number), titleSpan(v.o.title), shape)
|
||||
fmt.Fprintf(&b, "- %s %s%s\n%s", prLink(v.o.number), titleSpan(v.o.title), shape, diagItems(v))
|
||||
}
|
||||
if len(r.deferred) > 0 {
|
||||
fmt.Fprintf(&b, "\n### Deferred conflicts\n\nThese do not pick cleanly onto this branch. Each names the first of its commits that conflicted and the files it conflicted in, and nothing of it is on the branch. Each moves to the next batch, unless a resolution checked with `backport-batch fold` is pushed here first, as [resolving a conflict](%sresolve-conflict.md) describes.\n\n", tasksURL)
|
||||
@@ -324,13 +338,13 @@ func (r *report) what(details map[int]bool) string {
|
||||
if p.failed != "" {
|
||||
at = " at " + codeSpan(p.failed[:12])
|
||||
}
|
||||
fmt.Fprintf(&b, "- %s %s%s: %s\n", prLink(p.v.o.number), titleSpan(p.v.o.title), at, strings.Join(files, ", "))
|
||||
fmt.Fprintf(&b, "- %s %s%s: %s\n%s", prLink(p.v.o.number), titleSpan(p.v.o.title), at, strings.Join(files, ", "), diagItems(p.v))
|
||||
}
|
||||
}
|
||||
if len(r.held) > 0 {
|
||||
b.WriteString("\n### Deferred to the next batch\n\nTaken out of this batch with `backport-batch run --defer`, each for the reason given. This pull request keeps them out; the next batch picks them again.\n\n")
|
||||
for _, p := range r.held {
|
||||
fmt.Fprintf(&b, "- %s %s: %s\n", prLink(p.v.o.number), titleSpan(p.v.o.title), titleSpan(p.reason))
|
||||
fmt.Fprintf(&b, "- %s %s: %s\n%s", prLink(p.v.o.number), titleSpan(p.v.o.title), titleSpan(p.reason), diagItems(p.v))
|
||||
}
|
||||
}
|
||||
if len(r.waiting) > 0 {
|
||||
@@ -343,7 +357,41 @@ func (r *report) what(details map[int]bool) string {
|
||||
if more := len(p.files) - len(files) + p.more; more > 0 {
|
||||
files = append(files, fmt.Sprintf("%d more", more))
|
||||
}
|
||||
fmt.Fprintf(&b, "- %s %s: %s\n", prLink(p.v.o.number), titleSpan(p.v.o.title), strings.Join(files, ", "))
|
||||
fmt.Fprintf(&b, "- %s %s: %s\n%s", prLink(p.v.o.number), titleSpan(p.v.o.title), strings.Join(files, ", "), diagItems(p.v))
|
||||
}
|
||||
}
|
||||
if changed := r.changed(); len(changed) > 0 {
|
||||
b.WriteString("\n### Check these notes\n\nThese picks differ from their originals, so their release notes are left out of the release-note block until someone checks that each still holds for this line. Each comes with what to compare: every pick that differs against the commit it came from, and the original's change against the unit's.\n")
|
||||
for _, v := range changed {
|
||||
s := r.shapes[v]
|
||||
fmt.Fprintf(&b, "\n%s %s, carried by %s:\n\n", prLink(v.o.number), titleSpan(v.o.title), codeSpan(s.unit[:12]))
|
||||
if n := releaseNote(v.o.body); n != "" {
|
||||
fmt.Fprintf(&b, "```text\n%s\n```\n\n", neutralNote(n))
|
||||
} else {
|
||||
b.WriteString("It has no release note.\n\n")
|
||||
}
|
||||
for _, p := range s.pairs {
|
||||
fmt.Fprintf(&b, "- %s\n", codeSpan(fmt.Sprintf("git range-diff --creation-factor=999 %s^! %s^!", p.source[:12], p.pick[:12])))
|
||||
}
|
||||
fmt.Fprintf(&b, "- %s\n- %s\n", codeSpan(fmt.Sprintf("git diff %s^1 %s", v.o.merge[:12], v.o.merge[:12])), codeSpan(fmt.Sprintf("git diff %s^1 %s", s.unit[:12], s.unit[:12])))
|
||||
}
|
||||
}
|
||||
if len(r.partial) > 0 {
|
||||
fmt.Fprintf(&b, "\n### Still on this branch: only part of it applies\n\nEach of these is on this branch but leaves files of its original as they were on %s, so it carries only part of what it does on `main`. Take each out with `backport-batch run --defer` before the cut; what applies of it goes as a pull request of its own.\n\n", codeSpan(r.line.branch()))
|
||||
for _, v := range r.partial {
|
||||
fmt.Fprintf(&b, "- %s %s: leaves %s without effect\n", prLink(v.o.number), titleSpan(v.o.title), fileSpans(r.partialFiles[v]))
|
||||
}
|
||||
}
|
||||
if len(r.parked) > 0 {
|
||||
fmt.Fprintf(&b, "\n### Parked\n\nThese cannot land on %s whole, so the batch does not pick them: each would leave part of it without effect there, or builds on a change that never reaches the line. A maintainer ports what applies as a pull request of its own, or drops the original with `backport-batch run --drop`; `backport/include` does not lift a park.\n\n", codeSpan(r.line.branch()))
|
||||
for _, v := range r.parked {
|
||||
fmt.Fprintf(&b, "- %s %s: %s\n%s", prLink(v.o.number), titleSpan(v.o.title), parkReason(*v.park), diagItems(v))
|
||||
}
|
||||
}
|
||||
if len(r.needs) > 0 {
|
||||
fmt.Fprintf(&b, "\n### Waiting on another original\n\nThese build on another original that has not reached %s yet, so the batch does not try them; each names the one it waits on, and the batch tries it again once that one is there.\n\n", codeSpan(r.line.branch()))
|
||||
for _, p := range r.needs {
|
||||
fmt.Fprintf(&b, "- %s %s: after %s\n%s", prLink(p.v.o.number), titleSpan(p.v.o.title), prLinks(p.needs), diagItems(p.v))
|
||||
}
|
||||
}
|
||||
if len(r.overrides) > 0 {
|
||||
@@ -384,6 +432,8 @@ func (r *report) what(details map[int]bool) string {
|
||||
// outcome is what became of an original a label overrode.
|
||||
func (r *report) outcome(v *verdict) string {
|
||||
switch {
|
||||
case slices.Contains(r.partial, v):
|
||||
return "still on this branch, but only part of it applies; take it out with `backport-batch run --defer`"
|
||||
case slices.Contains(r.stale, v) && v.rule == ruleDropped:
|
||||
return "dropped, but still on this branch; take it out with `backport-batch run --defer`"
|
||||
case slices.Contains(r.stale, v):
|
||||
@@ -398,15 +448,83 @@ func (r *report) outcome(v *verdict) string {
|
||||
return "deferred to the next batch"
|
||||
case slices.ContainsFunc(r.waiting, func(p pick) bool { return p.v == v }):
|
||||
return "waiting for a maintainer: it changes " + codeSpan(workflowsDir)
|
||||
case slices.Contains(r.parked, v):
|
||||
return "parked: " + parkReason(*v.park)
|
||||
case slices.ContainsFunc(r.needs, func(p pick) bool { return p.v == v }):
|
||||
i := slices.IndexFunc(r.needs, func(p pick) bool { return p.v == v })
|
||||
return "waiting on " + prLinks(r.needs[i].needs)
|
||||
case v.rule != 0:
|
||||
return "skipped by " + ruleName(v.rule) + ", " + titleSpan(v.reason)
|
||||
}
|
||||
return "not picked"
|
||||
}
|
||||
|
||||
// diagItems lists, under an original's line, what could not be judged about
|
||||
// it or what it was seen to build on, each as a code span.
|
||||
func diagItems(v *verdict) string {
|
||||
var b strings.Builder
|
||||
for _, d := range v.diag {
|
||||
fmt.Fprintf(&b, " - %s\n", titleSpan(d))
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// fileSpans lists files as code spans, the first few of them.
|
||||
func fileSpans(files []string) string {
|
||||
var out []string
|
||||
for _, f := range files[:min(len(files), filesListed)] {
|
||||
out = append(out, codeSpan(f))
|
||||
}
|
||||
if more := len(files) - len(out); more > 0 {
|
||||
out = append(out, fmt.Sprintf("%d more", more))
|
||||
}
|
||||
return strings.Join(out, ", ")
|
||||
}
|
||||
|
||||
// prLinks links to pull requests by number.
|
||||
func prLinks(numbers []int) string {
|
||||
var out []string
|
||||
for _, n := range numbers {
|
||||
out = append(out, prLink(n))
|
||||
}
|
||||
return strings.Join(out, ", ")
|
||||
}
|
||||
|
||||
// parkReason says why a park leaves its original out, for the body.
|
||||
func parkReason(p parkedEntry) string {
|
||||
if p.Kind == parkPartial {
|
||||
return "only part of it applies; it would leave " + fileSpans(p.Files) + " without effect"
|
||||
}
|
||||
var needs []string
|
||||
for _, n := range p.Needs {
|
||||
needs = append(needs, prLink(n))
|
||||
}
|
||||
for _, c := range p.Commits {
|
||||
needs = append(needs, codeSpan(c[:12]))
|
||||
}
|
||||
return "builds on " + strings.Join(needs, ", ") + ", which never reaches this line"
|
||||
}
|
||||
|
||||
// changed lists the originals on the branch whose unit differs from them, in
|
||||
// merge order.
|
||||
func (r *report) changed() []*verdict {
|
||||
var out []*verdict
|
||||
for _, v := range r.onBranch {
|
||||
if s, ok := r.shapes[v]; ok && s.changed() {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// notes are the release notes of what the branch carries as its originals
|
||||
// made it; a changed pick's note waits under Check these notes.
|
||||
func (r *report) notes() string {
|
||||
var notes []string
|
||||
for _, v := range r.onBranch {
|
||||
if s, ok := r.shapes[v]; ok && s.changed() {
|
||||
continue
|
||||
}
|
||||
if n := releaseNote(v.o.body); n != "" {
|
||||
notes = append(notes, neutralNote(n))
|
||||
}
|
||||
|
||||
+113
-15
@@ -20,6 +20,7 @@ import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// check says whether every original the policy takes is on the line, on the
|
||||
@@ -50,7 +51,13 @@ func (a *app) check(args []string) int {
|
||||
if err := a.warnLeftBranch(b); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
disp, err := b.dispositions(*cut)
|
||||
// The scheduled run's mode, as the open batch saved it; with none open,
|
||||
// the scheduled run's, which is what the next batch is built in.
|
||||
mode := analysisMode{noWorkflowFiles: true}
|
||||
if b.src.open {
|
||||
mode.noWorkflowFiles = b.src.savedNoWorkflowFiles()
|
||||
}
|
||||
disp, err := b.dispositions(mode, *cut)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
@@ -66,14 +73,14 @@ func (a *app) check(args []string) int {
|
||||
case d.kind == outcomeWarning:
|
||||
a.warn("%s", d.text)
|
||||
case d.kind == outcomeNote:
|
||||
fmt.Fprintf(a.stderr, "note: %s\n", d.text)
|
||||
_, _ = fmt.Fprintf(a.stderr, "note: %s\n", d.text)
|
||||
}
|
||||
}
|
||||
if len(missing)+len(failing) == 0 {
|
||||
if *cut {
|
||||
fmt.Fprintf(a.stdout, "%s: no change the policy takes is missing before the cut\n", l.branch())
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: no change the policy takes is missing before the cut\n", l.branch())
|
||||
} else {
|
||||
fmt.Fprintf(a.stdout, "%s: every change the policy takes is on the line or on the batch\n", l.branch())
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: every change the policy takes is on the line or on the batch\n", l.branch())
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -82,13 +89,13 @@ func (a *app) check(args []string) int {
|
||||
where = "nor on batch pull request #" + strconv.Itoa(b.pr.Number)
|
||||
}
|
||||
if len(missing) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %d changes the policy takes are not on the line, %s:\n", l.branch(), len(missing), where)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d changes the policy takes are not on the line, %s:\n", l.branch(), len(missing), where)
|
||||
for _, text := range missing {
|
||||
fmt.Fprintf(a.stdout, " %s\n", text)
|
||||
_, _ = fmt.Fprintf(a.stdout, " %s\n", text)
|
||||
}
|
||||
}
|
||||
for _, text := range failing {
|
||||
fmt.Fprintf(a.stdout, "%s: %s\n", l.branch(), text)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %s\n", l.branch(), text)
|
||||
}
|
||||
return 1
|
||||
}
|
||||
@@ -137,8 +144,11 @@ type disposition struct {
|
||||
|
||||
// dispositions says what check reports about each original that needs a line
|
||||
// of its own; the others pass in silence. The text is what follows the line's
|
||||
// name, or the original alone for one that is missing.
|
||||
func (b *batch) dispositions(cut bool) (map[*verdict]disposition, error) {
|
||||
// name, or the original alone for one that is missing. Originals are taken in
|
||||
// merge order, each by the first of: a unit on the batch that does only part
|
||||
// of it, a reason a person deferred it for, a park, a wait for a maintainer
|
||||
// or on other originals, and what the record says of it.
|
||||
func (b *batch) dispositions(mode analysisMode, cut bool) (map[*verdict]disposition, error) {
|
||||
if c := b.src.closingOpen(); c != nil {
|
||||
return nil, fmt.Errorf("batch pull request #%d is being closed; backport-batch run --line %s --push finishes it", c.PR, b.line.branch())
|
||||
}
|
||||
@@ -158,11 +168,45 @@ func (b *batch) dispositions(cut bool) (map[*verdict]disposition, error) {
|
||||
on += " (" + prState(c) + ")"
|
||||
}
|
||||
}
|
||||
if err := b.analyze(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if b.pr != nil {
|
||||
if err := b.keepRecordedPartials(b.prHead); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
held := map[int]string{}
|
||||
for n, e := range deferred {
|
||||
if e.Reason != "" {
|
||||
held[n] = e.Reason
|
||||
}
|
||||
}
|
||||
states, err := b.classify(mode, held, deferred, b.src.open)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// waitsOn names what an original waits on, as check says it.
|
||||
waitsOn := func(s *state) string {
|
||||
var names []string
|
||||
for _, f := range s.needs {
|
||||
names = append(names, fmt.Sprintf("#%d", f.o.number))
|
||||
}
|
||||
return strings.Join(names, " and ")
|
||||
}
|
||||
out := map[*verdict]disposition{}
|
||||
for _, v := range b.verdicts {
|
||||
named := v.o.url() + " " + terminalSafe(v.o.title)
|
||||
e, isDeferred := deferred[v.o.number]
|
||||
s := states[v]
|
||||
if s == nil {
|
||||
s = &state{kind: stOwn}
|
||||
}
|
||||
switch {
|
||||
case b.partialUnits[v] != nil && cut:
|
||||
out[v] = disposition{outcomeUnmerged, fmt.Sprintf("#%d is on batch #%d, but only part of it applies to %s: it leaves %s without effect; take it out with backport-batch run --defer before the cut: %s", v.o.number, b.pr.Number, b.line.branch(), quotedFiles(b.partialUnits[v]), named)}
|
||||
case b.partialUnits[v] != nil:
|
||||
out[v] = disposition{outcomeWarning, fmt.Sprintf("#%d is on batch #%d, but only part of it applies to %s: it leaves %s without effect; take it out with backport-batch run --defer: %s", v.o.number, b.pr.Number, b.line.branch(), quotedFiles(b.partialUnits[v]), named)}
|
||||
case slices.Contains(stale, v):
|
||||
out[v] = disposition{outcomeWarning, fmt.Sprintf("#%d is still on batch #%d; take it out with backport-batch run --defer: %s", v.o.number, b.pr.Number, named)}
|
||||
case v.rule == ruleDropped:
|
||||
@@ -173,15 +217,41 @@ func (b *batch) dispositions(cut bool) (map[*verdict]disposition, error) {
|
||||
if cut {
|
||||
out[v] = disposition{outcomeUnmerged, fmt.Sprintf("#%d is only on open batch PR #%d, merge it before the cut: %s", v.o.number, b.pr.Number, named)}
|
||||
}
|
||||
case s.kind == stHeld:
|
||||
out[v] = disposition{outcomeWarning, fmt.Sprintf("deferred on %s to the next batch: %s (%s)", on, named, terminalSafe(e.Reason))}
|
||||
case s.kind == stPartial:
|
||||
out[v] = disposition{outcomeNote, fmt.Sprintf("#%d is parked: only part of it applies to %s, which would leave %s without effect; port what applies as a pull request of its own: %s", v.o.number, b.line.branch(), quotedFiles(v.park.Files), named)}
|
||||
case s.kind == stWorkflowPrimary && s.class == edgeMissing:
|
||||
out[v] = disposition{outcomeMissing, named}
|
||||
case s.kind == stParked:
|
||||
out[v] = disposition{outcomeNote, fmt.Sprintf("#%d is parked: it builds on %s, which never reaches %s; port what applies as a pull request of its own, or drop it: %s", v.o.number, parkNames(*v.park), b.line.branch(), named)}
|
||||
case s.kind == stNeeds && s.wait == edgeMissing:
|
||||
out[v] = disposition{outcomeMissing, fmt.Sprintf("%s (it waits on %s, which is not on the line either)", named, waitsOn(s))}
|
||||
case s.kind == stNeeds && s.wait == edgeWorkflow && cut:
|
||||
out[v] = disposition{outcomeWaiting, fmt.Sprintf("#%d waits on %s, which changes %s and waits on batch PR %s for a maintainer; bring it over, or defer both, before the cut: %s", v.o.number, waitsOn(s), workflowsDir, on, named)}
|
||||
case s.kind == stNeeds && s.wait == edgeWorkflow:
|
||||
out[v] = disposition{outcomeWarning, fmt.Sprintf("#%d waits on %s, which waits on %s for a maintainer: %s", v.o.number, waitsOn(s), on, named)}
|
||||
case s.kind == stNeeds && s.wait == edgeDeferred:
|
||||
what := "a deferred conflict on " + on
|
||||
if f := s.needs[0]; deferred[f.o.number].Reason != "" {
|
||||
what = "deferred on " + on + " to the next batch"
|
||||
} else if states[f] != nil && states[f].kind == stNeeds {
|
||||
what = "which waits itself"
|
||||
}
|
||||
out[v] = disposition{outcomeWarning, fmt.Sprintf("#%d waits on %s, %s: %s", v.o.number, waitsOn(s), what, named)}
|
||||
case s.kind == stNeeds:
|
||||
newer := "the newer line"
|
||||
if b.newer != nil {
|
||||
newer = b.newer.branch()
|
||||
}
|
||||
out[v] = disposition{outcomeNote, fmt.Sprintf("#%d waits on %s, which is not on %s yet: %s", v.o.number, waitsOn(s), newer, named)}
|
||||
case isDeferred && e.Workflows && !b.src.open:
|
||||
out[v] = disposition{outcomeMissing, named}
|
||||
case isDeferred && e.Workflows && cut:
|
||||
out[v] = disposition{outcomeWaiting, fmt.Sprintf("#%d changes %s and waits on batch PR %s for a maintainer, bring it over or defer it before the cut: %s", v.o.number, workflowsDir, on, named)}
|
||||
case isDeferred && e.Workflows:
|
||||
out[v] = disposition{outcomeWarning, fmt.Sprintf("waiting on %s for a maintainer, since it changes %s: %s", on, workflowsDir, named)}
|
||||
case isDeferred && e.Reason != "":
|
||||
out[v] = disposition{outcomeWarning, fmt.Sprintf("deferred on %s to the next batch: %s (%s)", on, named, terminalSafe(e.Reason))}
|
||||
case isDeferred:
|
||||
case isDeferred && len(e.Needs) == 0:
|
||||
out[v] = disposition{outcomeWarning, fmt.Sprintf("deferred conflict on %s: %s", on, named)}
|
||||
default:
|
||||
out[v] = disposition{outcomeMissing, named}
|
||||
@@ -190,6 +260,30 @@ func (b *batch) dispositions(cut bool) (map[*verdict]disposition, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// parkNames names what a park waits on for a terminal.
|
||||
func parkNames(p parkedEntry) string {
|
||||
var out []string
|
||||
for _, n := range p.Needs {
|
||||
out = append(out, fmt.Sprintf("#%d", n))
|
||||
}
|
||||
for _, c := range p.Commits {
|
||||
out = append(out, c[:12])
|
||||
}
|
||||
return strings.Join(out, ", ")
|
||||
}
|
||||
|
||||
// quotedFiles names files for a terminal, each quoted, the first few of them.
|
||||
func quotedFiles(files []string) string {
|
||||
var out []string
|
||||
for _, f := range files[:min(len(files), filesListed)] {
|
||||
out = append(out, strconv.Quote(f))
|
||||
}
|
||||
if more := len(files) - len(out); more > 0 {
|
||||
out = append(out, fmt.Sprintf("%d more", more))
|
||||
}
|
||||
return strings.Join(out, ", ")
|
||||
}
|
||||
|
||||
// terminalSafe keeps a title from carrying escape sequences into a terminal.
|
||||
func terminalSafe(s string) string { return capped(oneLine(clean(s, false)), titleLimit) }
|
||||
|
||||
@@ -232,10 +326,10 @@ func (a *app) fold(args []string) int {
|
||||
return a.fail(err)
|
||||
}
|
||||
if why != "" {
|
||||
fmt.Fprintf(a.stdout, "refused: %s\n", why)
|
||||
_, _ = fmt.Fprintf(a.stdout, "refused: %s\n", why)
|
||||
return 1
|
||||
}
|
||||
fmt.Fprintln(a.stdout, "ok")
|
||||
_, _ = fmt.Fprintln(a.stdout, "ok")
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -289,5 +383,9 @@ func refuseFold(b *batch, n int, commit string) (string, error) {
|
||||
case carried != v:
|
||||
return fmt.Sprintf("%.12s carries #%d, not #%d", commit, carried.o.number, n), nil
|
||||
}
|
||||
return "", nil
|
||||
omitted, _, err := b.omittedEffects(v, u.parents[0], u.commit)
|
||||
if err != nil || len(omitted) == 0 {
|
||||
return "", err
|
||||
}
|
||||
return leftOut(v, b.line, omitted), nil
|
||||
}
|
||||
@@ -78,7 +78,7 @@ func (a *app) rebuild(b *batch, numbers []int, reason string, push bool) int {
|
||||
switch v := b.find(n); {
|
||||
case v == nil:
|
||||
return a.fail(fmt.Errorf("#%d did not merge into main after %s branched", n, b.line.branch()))
|
||||
case v.rule != 0 && !slices.Contains(stale, v):
|
||||
case v.rule != 0 && !slices.Contains(stale, v) && b.partialUnits[v] == nil:
|
||||
return a.fail(fmt.Errorf("#%d is not one the batch takes: %s, %s", n, ruleName(v.rule), terminalSafe(v.reason)))
|
||||
}
|
||||
hold[n] = reason
|
||||
@@ -102,7 +102,7 @@ func (a *app) reassemble(b *batch, out, hold map[int]string, drops []droppedEntr
|
||||
return a.fail(err)
|
||||
}
|
||||
for _, u := range replay {
|
||||
failed, files, err := replayUnit(u)
|
||||
failed, files, err := a.replayNext(u)
|
||||
switch {
|
||||
case err != nil:
|
||||
return a.fail(err)
|
||||
@@ -138,16 +138,50 @@ func (a *app) reclassify(b *batch, picks *history, hold map[int]string, drops []
|
||||
if r.stale, err = keptOut(b, picks); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
// What the rebuild took off is no longer there for what builds on it.
|
||||
units, err := b.unitsOn(picks.head)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
for _, v := range b.verdicts {
|
||||
v.batched = units[v]
|
||||
}
|
||||
on := map[*verdict]bool{}
|
||||
for _, v := range b.qualifying() {
|
||||
if on[v], err = b.onBranch(picks, v); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if !on[v] {
|
||||
v.onBatch = ""
|
||||
}
|
||||
}
|
||||
if err := b.keepRecordedPartials(picks.head); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
states, err := b.classify(analysisMode{noWorkflowFiles: r.opts.NoWorkflowFiles}, hold, recorded, true)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
for _, v := range b.qualifying() {
|
||||
e := recorded[v.o.number]
|
||||
s := states[v]
|
||||
if s == nil {
|
||||
s = &state{kind: stOwn}
|
||||
}
|
||||
switch {
|
||||
case found(picks, v):
|
||||
case on[v]:
|
||||
r.onBranch = append(r.onBranch, v)
|
||||
case hold[v.o.number] != "":
|
||||
r.held = append(r.held, pick{v: v, status: "held", reason: hold[v.o.number]})
|
||||
case s.kind == stPartial || s.kind == stParked:
|
||||
r.parked = append(r.parked, v)
|
||||
case s.kind == stNeeds:
|
||||
r.needs = append(r.needs, pick{v: v, status: "waiting", needs: numbers(s.needs)})
|
||||
case s.kind == stWorkflowPrimary && !e.Workflows:
|
||||
r.missing = append(r.missing, v)
|
||||
case e.Workflows:
|
||||
r.waiting = append(r.waiting, pick{v: v, status: "waiting", files: e.Files, more: e.More})
|
||||
case len(e.Files) > 0:
|
||||
case len(e.Files) > 0 && len(e.Needs) == 0:
|
||||
r.deferred = append(r.deferred, pick{v: v, status: "conflict", files: e.Files, more: e.More, failed: e.Commit})
|
||||
default:
|
||||
// Merged into main since the batch was assembled, or recorded
|
||||
@@ -155,6 +189,9 @@ func (a *app) reclassify(b *batch, picks *history, hold map[int]string, drops []
|
||||
r.missing = append(r.missing, v)
|
||||
}
|
||||
}
|
||||
if err := b.markPartial(r, picks.head); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
return a.finish(b, r, b.pr.Head.Ref, date, push, true)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,912 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"path"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// What an original builds on that its line lacks is read from git alone, in
|
||||
// two ways: a file it modifies or deletes that the line does not have, named
|
||||
// by the first-parent commit on main that added it (V1), and a line it
|
||||
// removes or changes that git blame gives to a commit the line lacks, kept
|
||||
// only while that line's text is absent from the line's file (V2c). Either
|
||||
// names the first-parent commit that brought the evidence to main: the merge
|
||||
// of another original, or a commit that merged no pull request.
|
||||
|
||||
// Bounds per original, past which the evidence is incomplete rather than
|
||||
// read at any cost.
|
||||
const (
|
||||
maxDepFiles = 256
|
||||
maxBlamedLines = 20000
|
||||
maxDepBlob = 8 << 20
|
||||
gitDeadline = 2 * time.Minute
|
||||
)
|
||||
|
||||
// dependency is a first-parent commit on main an original builds on, with
|
||||
// the original's paths the evidence is in.
|
||||
type dependency struct {
|
||||
merge string
|
||||
// v is the original merge merged, nil for a commit that merged none.
|
||||
v *verdict
|
||||
paths []string
|
||||
// added says some of the evidence is a file the line lacks (V1).
|
||||
added bool
|
||||
}
|
||||
|
||||
// errDeadline is a git call that ran out of time: incomplete evidence, not
|
||||
// a failure.
|
||||
var errDeadline = errors.New("git took longer than " + gitDeadline.String())
|
||||
|
||||
// gitTimed runs git under the deadline the detector gives each call.
|
||||
func gitTimed(args ...string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), gitDeadline)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "git", args...)
|
||||
cmd.Env = append(cmd.Environ(), commandEnv...)
|
||||
var out, errOut bytes.Buffer
|
||||
cmd.Stdout, cmd.Stderr = &out, &errOut
|
||||
err := cmd.Run()
|
||||
switch {
|
||||
case ctx.Err() != nil:
|
||||
return "", errDeadline
|
||||
case err != nil:
|
||||
return "", fmt.Errorf("git %s: %w: %s", quotedArgs(args), err, terminalSafe(errOut.String()))
|
||||
}
|
||||
return out.String(), nil
|
||||
}
|
||||
|
||||
// quotedArgs names a git command's arguments for an error, each quoted: a
|
||||
// file name can hold a newline, and the error reaches a terminal and a CI
|
||||
// log, which would take the rest for a line of their own.
|
||||
func quotedArgs(args []string) string {
|
||||
out := make([]string, len(args))
|
||||
for i, a := range args {
|
||||
out[i] = strconv.Quote(a)
|
||||
}
|
||||
return strings.Join(out, " ")
|
||||
}
|
||||
|
||||
// gitStatusTimed is gitStatus under the detector's deadline: a non-zero
|
||||
// exit is an answer, and running out of time is errDeadline.
|
||||
func gitStatusTimed(args ...string) (stdout, stderr string, code int, err error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), gitDeadline)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "git", args...)
|
||||
cmd.Env = append(cmd.Environ(), commandEnv...)
|
||||
var out, errOut bytes.Buffer
|
||||
cmd.Stdout, cmd.Stderr = &out, &errOut
|
||||
err = cmd.Run()
|
||||
var ee *exec.ExitError
|
||||
switch {
|
||||
case ctx.Err() != nil:
|
||||
return "", "", -1, errDeadline
|
||||
case errors.As(err, &ee):
|
||||
return out.String(), errOut.String(), ee.ExitCode(), nil
|
||||
case err != nil:
|
||||
return "", "", -1, fmt.Errorf("git %s: %w", quotedArgs(args), err)
|
||||
}
|
||||
return out.String(), errOut.String(), 0, nil
|
||||
}
|
||||
|
||||
// broughtBy maps every commit in base..mainHead to the first-parent commit
|
||||
// that brought it to main: each first-parent commit claims itself, then
|
||||
// every commit of the range reachable from its other parents that no
|
||||
// earlier one claimed. A commit outside the map is an ancestor of base.
|
||||
func broughtBy(base, mainHead string) (map[string]string, error) {
|
||||
first, err := revList("--first-parent", "--reverse", "--end-of-options", base+".."+mainHead)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := git("rev-list", "--parents", "--topo-order", "--end-of-options", base+".."+mainHead)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
parents := map[string][]string{}
|
||||
for ln := range strings.Lines(out) {
|
||||
f := strings.Fields(ln)
|
||||
if len(f) == 0 {
|
||||
continue
|
||||
}
|
||||
for _, sha := range f {
|
||||
if checkSHA(sha) != nil {
|
||||
return nil, fmt.Errorf("rev-list: %q is not a commit", sha)
|
||||
}
|
||||
}
|
||||
parents[f[0]] = f[1:]
|
||||
}
|
||||
by := map[string]string{}
|
||||
for _, fp := range first {
|
||||
if _, ok := parents[fp]; !ok {
|
||||
return nil, fmt.Errorf("rev-list: %s is on main's first-parent line but not in its range", fp)
|
||||
}
|
||||
by[fp] = fp
|
||||
stack := slices.Clone(parents[fp][min(1, len(parents[fp])):])
|
||||
for len(stack) > 0 {
|
||||
c := stack[len(stack)-1]
|
||||
stack = stack[:len(stack)-1]
|
||||
if _, inRange := parents[c]; !inRange || by[c] != "" {
|
||||
continue
|
||||
}
|
||||
by[c] = fp
|
||||
stack = append(stack, parents[c]...)
|
||||
}
|
||||
}
|
||||
return by, nil
|
||||
}
|
||||
|
||||
// hunk is an old range of a diff, a,+b in git blame's terms.
|
||||
type hunk struct{ start, count int }
|
||||
|
||||
var hunkRE = regexp.MustCompile(`^@@ -(\d+)(?:,(\d+))? \+\d+(?:,\d+)? @@`)
|
||||
|
||||
// oldRanges reads the removed or changed line ranges of a -U0 diff of one
|
||||
// file, and whether git called it binary. A header it cannot read is an
|
||||
// error, never evidence of absence.
|
||||
func oldRanges(diff string) ([]hunk, bool, error) {
|
||||
var out []hunk
|
||||
for ln := range strings.Lines(diff) {
|
||||
ln = strings.TrimSuffix(ln, "\n")
|
||||
switch {
|
||||
case strings.HasPrefix(ln, "Binary files ") && strings.HasSuffix(ln, " differ"):
|
||||
return nil, true, nil
|
||||
case strings.HasPrefix(ln, "@@"):
|
||||
m := hunkRE.FindStringSubmatch(ln)
|
||||
if m == nil {
|
||||
return nil, false, fmt.Errorf("unreadable hunk header %q", terminalSafe(ln))
|
||||
}
|
||||
start, _ := strconv.Atoi(m[1])
|
||||
count := 1
|
||||
if m[2] != "" {
|
||||
count, _ = strconv.Atoi(m[2])
|
||||
}
|
||||
if count > 0 {
|
||||
out = append(out, hunk{start, count})
|
||||
}
|
||||
}
|
||||
}
|
||||
return out, false, nil
|
||||
}
|
||||
|
||||
// blameArgs blames ranges of file at rev, a full commit, which git takes for
|
||||
// a revision whatever follows: blame refuses --end-of-options.
|
||||
func blameArgs(rev, file string, ranges []hunk) ([]string, error) {
|
||||
if err := checkSHA(rev); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
args := []string{"blame", "--porcelain", "-w"}
|
||||
for _, r := range ranges {
|
||||
args = append(args, "-L", fmt.Sprintf("%d,+%d", r.start, r.count))
|
||||
}
|
||||
return append(args, rev, "--", file), nil
|
||||
}
|
||||
|
||||
// blamedLine is one line git blame gives to a commit.
|
||||
type blamedLine struct{ commit, text string }
|
||||
|
||||
// parseBlame reads git blame --porcelain: a header per line, the commit's
|
||||
// fields the first time it appears, and the line itself after a tab.
|
||||
// Anything else is an error, never evidence of absence.
|
||||
func parseBlame(out string) ([]blamedLine, error) {
|
||||
var lines []blamedLine
|
||||
commit := ""
|
||||
for ln := range strings.Lines(out) {
|
||||
ln = strings.TrimSuffix(ln, "\n")
|
||||
switch {
|
||||
case strings.HasPrefix(ln, "\t"):
|
||||
if commit == "" {
|
||||
return nil, errors.New("git blame: a line with no header")
|
||||
}
|
||||
lines = append(lines, blamedLine{commit: commit, text: ln[1:]})
|
||||
commit = ""
|
||||
case commit == "":
|
||||
f := strings.Fields(ln)
|
||||
if len(f) < 3 || len(f) > 4 || checkSHA(f[0]) != nil {
|
||||
return nil, fmt.Errorf("git blame: unreadable header %q", terminalSafe(ln))
|
||||
}
|
||||
for _, n := range f[1:] {
|
||||
if v, err := strconv.Atoi(n); err != nil || v <= 0 {
|
||||
return nil, fmt.Errorf("git blame: unreadable header %q", terminalSafe(ln))
|
||||
}
|
||||
}
|
||||
commit = f[0]
|
||||
}
|
||||
}
|
||||
if commit != "" {
|
||||
return nil, errors.New("git blame: a header with no line")
|
||||
}
|
||||
return lines, nil
|
||||
}
|
||||
|
||||
var generatedMarkerRE = regexp.MustCompile(`Code generated .* DO NOT EDIT`)
|
||||
|
||||
// generatedFiles are written whole by a tool, whatever they hold.
|
||||
var generatedFiles = []string{"go.sum", "package-lock.json", "yarn.lock", "pnpm-lock.yaml"}
|
||||
|
||||
// cozyRDGenerated are the keys hack/update-crd.sh writes into an
|
||||
// ApplicationDefinition, the rest of which people write.
|
||||
var cozyRDGenerated = []string{
|
||||
"apiVersion", "kind", "metadata.name", "spec.application.openAPISchema", "spec.release.prefix",
|
||||
"spec.release.chartRef.kind", "spec.release.chartRef.name", "spec.release.chartRef.namespace",
|
||||
"spec.dashboard.description", "spec.dashboard.icon", "spec.dashboard.keysOrder",
|
||||
}
|
||||
|
||||
var yamlKeyRE = regexp.MustCompile(`^( *)([A-Za-z0-9_.-]+):(?:\s|$)`)
|
||||
|
||||
// generatedLines says which lines of a file at a commit a tool wrote: all of
|
||||
// them for a generated file, the keys hack/update-crd.sh sets for an
|
||||
// ApplicationDefinition, found by indentation, and none otherwise. ok is
|
||||
// false for an ApplicationDefinition the scan cannot read, whose lines are
|
||||
// then not blamed at all.
|
||||
func generatedLines(file string, blob []byte) (skip map[int]bool, whole, ok bool) {
|
||||
base := path.Base(file)
|
||||
if strings.HasPrefix(base, "zz_generated") || slices.Contains(generatedFiles, base) {
|
||||
return nil, true, true
|
||||
}
|
||||
lines := strings.Split(string(blob), "\n")
|
||||
for _, ln := range lines[:min(len(lines), 100)] {
|
||||
if generatedMarkerRE.MatchString(ln) {
|
||||
return nil, true, true
|
||||
}
|
||||
}
|
||||
if rd, _ := path.Match(cozyRDGlob, file); !rd {
|
||||
return nil, false, true
|
||||
}
|
||||
skip = map[int]bool{}
|
||||
type key struct {
|
||||
indent int
|
||||
name string
|
||||
}
|
||||
var stack []key
|
||||
genIndent := -1
|
||||
for i, ln := range lines {
|
||||
n := i + 1
|
||||
trimmed := strings.TrimLeft(ln, " ")
|
||||
indent := len(ln) - len(trimmed)
|
||||
switch {
|
||||
case strings.HasPrefix(trimmed, "\t") || strings.HasPrefix(ln, "---") && i > 0:
|
||||
return nil, false, false
|
||||
case trimmed == "" || strings.HasPrefix(trimmed, "#"):
|
||||
if genIndent >= 0 {
|
||||
skip[n] = true
|
||||
}
|
||||
continue
|
||||
case genIndent >= 0 && indent > genIndent:
|
||||
skip[n] = true
|
||||
continue
|
||||
}
|
||||
genIndent = -1
|
||||
m := yamlKeyRE.FindStringSubmatch(ln)
|
||||
if m == nil {
|
||||
continue
|
||||
}
|
||||
for len(stack) > 0 && stack[len(stack)-1].indent >= indent {
|
||||
stack = stack[:len(stack)-1]
|
||||
}
|
||||
stack = append(stack, key{indent, m[2]})
|
||||
var names []string
|
||||
for _, k := range stack {
|
||||
names = append(names, k.name)
|
||||
}
|
||||
if slices.Contains(cozyRDGenerated, strings.Join(names, ".")) {
|
||||
skip[n] = true
|
||||
genIndent = indent
|
||||
}
|
||||
}
|
||||
return skip, false, true
|
||||
}
|
||||
|
||||
// trimmedLines is the set of a blob's lines with their edges trimmed, which
|
||||
// is all a reformat of indentation or trailing space changes.
|
||||
func trimmedLines(blob []byte) map[string]bool {
|
||||
set := map[string]bool{}
|
||||
for ln := range strings.Lines(string(blob)) {
|
||||
set[strings.TrimSpace(ln)] = true
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
// shown is a path as a diagnostic names it.
|
||||
func shown(p string) string { return strconv.Quote(p) }
|
||||
|
||||
// incomplete is a diagnostic about evidence the detector could not read.
|
||||
func incomplete(format string, args ...any) string {
|
||||
return oneLineText("incomplete evidence: " + fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
// dependencies reads what v builds on that target lacks, with what it could
|
||||
// not read. V1 looks at the paths whose counterpart target lacks, V2c at
|
||||
// the ones it has.
|
||||
func (b *batch) dependencies(v *verdict, target string) ([]dependency, []string, error) {
|
||||
if err := b.load(v); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if b.brought == nil {
|
||||
var err error
|
||||
if b.brought, err = broughtBy(b.base, b.mainHead); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
corr, unknown, err := b.correspondence(v, target)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var diag []string
|
||||
for _, p := range unknown {
|
||||
diag = append(diag, incomplete("git skipped rename detection for %s", shown(p)))
|
||||
}
|
||||
var at []string
|
||||
for _, c := range corr {
|
||||
at = append(at, c)
|
||||
}
|
||||
have, err := treeEntries(target, at)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
parent, err := resolve(v.o.merge + "^1")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
byMerge := map[string]*dependency{}
|
||||
add := func(merge, p string, added bool) {
|
||||
d := byMerge[merge]
|
||||
if d == nil {
|
||||
d = &dependency{merge: merge, v: b.findMerge(merge)}
|
||||
byMerge[merge] = d
|
||||
}
|
||||
if !slices.Contains(d.paths, p) {
|
||||
d.paths = append(d.paths, p)
|
||||
}
|
||||
d.added = d.added || added
|
||||
}
|
||||
files, blamed := 0, 0
|
||||
for _, c := range v.changes {
|
||||
cp, ok := corr[c.path]
|
||||
if !ok || c.status != 'M' && c.status != 'D' {
|
||||
continue
|
||||
}
|
||||
if !utf8.ValidString(c.path) {
|
||||
diag = append(diag, incomplete("a path that is not UTF-8, %s", shown(c.path)))
|
||||
continue
|
||||
}
|
||||
if files++; files > maxDepFiles {
|
||||
diag = append(diag, incomplete("more than %d files to read", maxDepFiles))
|
||||
break
|
||||
}
|
||||
cur, present := have[cp]
|
||||
if !present {
|
||||
out, err := gitTimed("--literal-pathspecs", "log", "--first-parent", "--diff-merges=first-parent", "--no-renames", "--diff-filter=A", "--format=%H", "--no-patch", "-1", "--end-of-options", b.base+".."+parent, "--", c.path)
|
||||
switch {
|
||||
case errors.Is(err, errDeadline):
|
||||
diag = append(diag, incomplete("%s, finding what added %s", err, shown(c.path)))
|
||||
continue
|
||||
case err != nil:
|
||||
return nil, nil, err
|
||||
}
|
||||
adder := strings.TrimSpace(out)
|
||||
if adder == "" {
|
||||
diag = append(diag, oneLineText(fmt.Sprintf("%s is not on the line, and nothing merged into main since it branched added it", shown(c.path))))
|
||||
continue
|
||||
}
|
||||
if checkSHA(adder) != nil {
|
||||
return nil, nil, fmt.Errorf("git log for %s: %q is not a commit", shown(c.path), terminalSafe(adder))
|
||||
}
|
||||
add(adder, c.path, true)
|
||||
continue
|
||||
}
|
||||
found, more, err := b.blamed(v, parent, c.path, cur, target, maxBlamedLines-blamed)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
blamed += more.lines
|
||||
diag = append(diag, more.diag...)
|
||||
for merge := range found {
|
||||
add(merge, c.path, false)
|
||||
}
|
||||
if more.capped {
|
||||
break
|
||||
}
|
||||
}
|
||||
var deps []dependency
|
||||
for _, d := range byMerge {
|
||||
slices.Sort(d.paths)
|
||||
deps = append(deps, *d)
|
||||
// The rules decide before the line is looked at, so whether the
|
||||
// line carries a prerequisite they keep out is asked here.
|
||||
if f := d.v; f != nil && f.rule != 0 && f.rule != ruleOnLine && f.rule != ruleNewer && b.lineHist != nil {
|
||||
if _, asked := b.lineCarries[f]; !asked {
|
||||
if err := b.load(f); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
how, err := b.lineHist.carries(f.o, f.commits)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if b.lineCarries == nil {
|
||||
b.lineCarries = map[*verdict]bool{}
|
||||
}
|
||||
b.lineCarries[f] = how != ""
|
||||
}
|
||||
}
|
||||
}
|
||||
slices.SortFunc(deps, func(x, y dependency) int { return strings.Compare(x.merge, y.merge) })
|
||||
return deps, diag, nil
|
||||
}
|
||||
|
||||
// blameResult is what blaming one file read: how many lines, what it could
|
||||
// not read, and whether the original's line budget ran out.
|
||||
type blameResult struct {
|
||||
lines int
|
||||
diag []string
|
||||
capped bool
|
||||
}
|
||||
|
||||
// blamed gives each line of file that v removes or changes, at parent, to
|
||||
// the first-parent commit that brought it, and keeps the commits one of
|
||||
// whose lines target's counterpart of the file, cur, lacks.
|
||||
func (b *batch) blamed(v *verdict, parent, file string, cur treeEntry, target string, budget int) (map[string]bool, blameResult, error) {
|
||||
var res blameResult
|
||||
entries, err := treeEntries(parent, []string{file})
|
||||
if err != nil {
|
||||
return nil, res, err
|
||||
}
|
||||
pre := entries[file]
|
||||
switch {
|
||||
case pre.mode == "120000" || cur.mode == "120000":
|
||||
res.diag = append(res.diag, incomplete("%s is a symlink", shown(file)))
|
||||
return nil, res, nil
|
||||
case pre.kind == "commit" || cur.kind == "commit":
|
||||
res.diag = append(res.diag, incomplete("%s is a submodule", shown(file)))
|
||||
return nil, res, nil
|
||||
case !pre.regular() || !cur.regular():
|
||||
res.diag = append(res.diag, incomplete("%s is no file git can blame", shown(file)))
|
||||
return nil, res, nil
|
||||
}
|
||||
// A file the line already has as the original makes it builds on
|
||||
// nothing the line lacks, whoever wrote the lines it replaced.
|
||||
if v.o.merge != "" && cur.oid != pre.oid {
|
||||
post, err := treeEntries(v.o.merge, []string{file})
|
||||
if err != nil {
|
||||
return nil, res, err
|
||||
}
|
||||
if p1, ok := post[file]; ok && p1.regular() {
|
||||
if cur.oid == p1.oid {
|
||||
return nil, res, nil
|
||||
}
|
||||
applied, _, err := b.alreadyApplied(pre.oid, cur.oid, p1.oid)
|
||||
if err != nil {
|
||||
return nil, res, err
|
||||
}
|
||||
if applied {
|
||||
return nil, res, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, oid := range []string{pre.oid, cur.oid} {
|
||||
out, err := git("cat-file", "-s", oid)
|
||||
if err != nil {
|
||||
return nil, res, err
|
||||
}
|
||||
size, err := strconv.Atoi(strings.TrimSpace(out))
|
||||
if err != nil {
|
||||
return nil, res, fmt.Errorf("git cat-file -s %s: %q is no size", oid, terminalSafe(out))
|
||||
}
|
||||
if size > maxDepBlob {
|
||||
res.diag = append(res.diag, incomplete("%s is larger than %d bytes", shown(file), maxDepBlob))
|
||||
return nil, res, nil
|
||||
}
|
||||
}
|
||||
diff, err := gitTimed("--literal-pathspecs", "diff", "--no-ext-diff", "--no-textconv", "--no-renames", "-U0", "--end-of-options", parent, v.o.merge, "--", file)
|
||||
if errors.Is(err, errDeadline) {
|
||||
res.diag = append(res.diag, incomplete("%s, diffing %s", err, shown(file)))
|
||||
return nil, res, nil
|
||||
} else if err != nil {
|
||||
return nil, res, err
|
||||
}
|
||||
ranges, binary, err := oldRanges(diff)
|
||||
switch {
|
||||
case err != nil:
|
||||
return nil, res, fmt.Errorf("the diff of %s in #%d: %w", shown(file), v.o.number, err)
|
||||
case binary:
|
||||
res.diag = append(res.diag, incomplete("%s is binary", shown(file)))
|
||||
return nil, res, nil
|
||||
case len(ranges) == 0:
|
||||
return nil, res, nil
|
||||
}
|
||||
blobs, err := readObjects([]string{pre.oid, cur.oid}, "blob")
|
||||
if err != nil {
|
||||
return nil, res, err
|
||||
}
|
||||
skip, whole, ok := generatedLines(file, blobs[pre.oid])
|
||||
switch {
|
||||
case whole:
|
||||
return nil, res, nil
|
||||
case !ok:
|
||||
res.diag = append(res.diag, incomplete("%s, whose generated keys could not be told apart", shown(file)))
|
||||
return nil, res, nil
|
||||
}
|
||||
var kept []hunk
|
||||
for _, r := range ranges {
|
||||
for n := r.start; n < r.start+r.count; n++ {
|
||||
if skip[n] {
|
||||
continue
|
||||
}
|
||||
if l := len(kept) - 1; l >= 0 && kept[l].start+kept[l].count == n {
|
||||
kept[l].count++
|
||||
} else {
|
||||
kept = append(kept, hunk{n, 1})
|
||||
}
|
||||
res.lines++
|
||||
}
|
||||
}
|
||||
if res.lines > budget {
|
||||
res.diag = append(res.diag, incomplete("more than %d lines to blame", maxBlamedLines))
|
||||
res.capped = true
|
||||
return nil, res, nil
|
||||
}
|
||||
if len(kept) == 0 {
|
||||
return nil, res, nil
|
||||
}
|
||||
args, err := blameArgs(parent, file, kept)
|
||||
if err != nil {
|
||||
return nil, res, err
|
||||
}
|
||||
out, err := gitTimed(args...)
|
||||
if errors.Is(err, errDeadline) {
|
||||
res.diag = append(res.diag, incomplete("%s, blaming %s", err, shown(file)))
|
||||
return nil, res, nil
|
||||
} else if err != nil {
|
||||
return nil, res, err
|
||||
}
|
||||
lines, err := parseBlame(out)
|
||||
if err != nil {
|
||||
return nil, res, fmt.Errorf("blaming %s for #%d: %w", shown(file), v.o.number, err)
|
||||
}
|
||||
onTarget := trimmedLines(blobs[cur.oid])
|
||||
found := map[string]bool{}
|
||||
unmapped := false
|
||||
for _, ln := range lines {
|
||||
merge, inRange := b.brought[ln.commit]
|
||||
switch {
|
||||
case !inRange:
|
||||
// An ancestor of the line's fork point, which the line has, or
|
||||
// a commit main does not have at all.
|
||||
before, err := b.beforeBase(ln.commit)
|
||||
if err != nil {
|
||||
return nil, res, err
|
||||
}
|
||||
unmapped = unmapped || !before
|
||||
case merge == v.o.merge:
|
||||
case !onTarget[strings.TrimSpace(ln.text)]:
|
||||
found[merge] = true
|
||||
}
|
||||
}
|
||||
if unmapped {
|
||||
res.diag = append(res.diag, incomplete("git blame gives lines of %s to a commit that did not come through main", shown(file)))
|
||||
}
|
||||
return found, res, nil
|
||||
}
|
||||
|
||||
// timed adds what has passed since start to the time the detector took.
|
||||
func (b *batch) timed(start time.Time) { b.detecting += time.Since(start) }
|
||||
|
||||
// beforeBase says whether commit is an ancestor of the line's fork point,
|
||||
// once per commit.
|
||||
func (b *batch) beforeBase(commit string) (bool, error) {
|
||||
if ok, seen := b.ancestors[commit]; seen {
|
||||
return ok, nil
|
||||
}
|
||||
ok, err := isAncestor(commit, b.base)
|
||||
if err == nil {
|
||||
if b.ancestors == nil {
|
||||
b.ancestors = map[string]bool{}
|
||||
}
|
||||
b.ancestors[commit] = ok
|
||||
}
|
||||
return ok, err
|
||||
}
|
||||
|
||||
// analyze reads what every original the policy takes and nothing carries
|
||||
// builds on that the line lacks, and keeps it with the original, for the
|
||||
// parks and waits that follow from it.
|
||||
func (b *batch) analyze() error {
|
||||
defer b.timed(time.Now())
|
||||
for _, v := range b.qualifying() {
|
||||
if v.onBatch != "" {
|
||||
continue
|
||||
}
|
||||
deps, diag, err := b.dependencies(v, b.lineHead)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v.deps = deps
|
||||
v.addDiag(diag...)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// analysisMode is how a classification treats an original that changes a
|
||||
// workflow: as the scheduled run does, whose token cannot push one, or as a
|
||||
// maintainer's run.
|
||||
type analysisMode struct{ noWorkflowFiles bool }
|
||||
|
||||
// edgeClass is what a prerequisite is to an original that builds on it,
|
||||
// from what never matters to what decides first.
|
||||
type edgeClass int
|
||||
|
||||
const (
|
||||
edgeSatisfied edgeClass = iota
|
||||
// edgeNewer is held by rule 6 on this line.
|
||||
edgeNewer
|
||||
// edgeDeferred is a deferred conflict, or deferred for a reason.
|
||||
edgeDeferred
|
||||
// edgeWorkflow waits on the open batch for a maintainer.
|
||||
edgeWorkflow
|
||||
// edgeMissing is on neither the line, the batch nor the record.
|
||||
edgeMissing
|
||||
// edgeNever never reaches the line.
|
||||
edgeNever
|
||||
)
|
||||
|
||||
// edge is a dependency whose prerequisite has not reached the line, with
|
||||
// what that prerequisite is.
|
||||
type edge struct {
|
||||
dep dependency
|
||||
class edgeClass
|
||||
}
|
||||
|
||||
// ruleClass is what the rules alone make of a prerequisite, or ok false when
|
||||
// it depends on what this classification makes of it: one the line or the
|
||||
// open batch carries is there, whatever its label, drop or rule says, and a
|
||||
// direct push and an original the rules keep out never come.
|
||||
func (b *batch) ruleClass(f *verdict) (edgeClass, bool) {
|
||||
switch {
|
||||
case f == nil:
|
||||
return edgeNever, true
|
||||
case f.rule == ruleOnLine || f.onBatch != "" || f.batched != "" || b.lineCarries[f]:
|
||||
return edgeSatisfied, true
|
||||
case f.rule == ruleNewer:
|
||||
return edgeNewer, true
|
||||
case f.rule != 0:
|
||||
return edgeNever, true
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
|
||||
// edges sorts dependencies by what classOf says of each prerequisite into
|
||||
// those that never come and those still to come, dropping the ones there.
|
||||
func edges(deps []dependency, classOf func(*verdict) edgeClass) (never, pending []edge) {
|
||||
for _, d := range deps {
|
||||
switch c := classOf(d.v); c {
|
||||
case edgeSatisfied:
|
||||
case edgeNever:
|
||||
never = append(never, edge{d, c})
|
||||
default:
|
||||
pending = append(pending, edge{d, c})
|
||||
}
|
||||
}
|
||||
return never, pending
|
||||
}
|
||||
|
||||
// strongest is the class of prerequisite that decides a wait, and the
|
||||
// prerequisites of that class, in merge order.
|
||||
func strongest(pending []edge) (edgeClass, []*verdict) {
|
||||
top := edgeSatisfied
|
||||
for _, e := range pending {
|
||||
top = max(top, e.class)
|
||||
}
|
||||
var out []*verdict
|
||||
for _, e := range pending {
|
||||
if e.class == top && !slices.Contains(out, e.dep.v) {
|
||||
out = append(out, e.dep.v)
|
||||
}
|
||||
}
|
||||
return top, out
|
||||
}
|
||||
|
||||
// auxiliary says whether a path holds tests or documentation, which an
|
||||
// original can change without building on what it needs to work.
|
||||
func auxiliary(p string) bool {
|
||||
parts := strings.Split(p, "/")
|
||||
base := parts[len(parts)-1]
|
||||
for _, d := range parts[:len(parts)-1] {
|
||||
if d == "test" || d == "tests" || d == "testdata" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case strings.HasSuffix(base, "_test.go"), strings.Contains(base, ".test."), strings.Contains(base, ".spec."), strings.HasSuffix(base, ".bats"):
|
||||
return true
|
||||
case len(parts) > 1 && parts[0] == "docs":
|
||||
return true
|
||||
}
|
||||
return len(parts) == 1 && strings.HasSuffix(base, ".md")
|
||||
}
|
||||
|
||||
// workflowPrimary says whether v waits for a maintainer rather than parks:
|
||||
// in the scheduled mode, an original that changes a workflow, all of whose
|
||||
// missing dependencies that never come are tests or docs, and whose
|
||||
// workflow lines build on nothing still missing. The workflow is what it is
|
||||
// for, and a maintainer brings over what applies. Only what is still
|
||||
// missing counts: never and pending are what is left once what is there was
|
||||
// taken out.
|
||||
func workflowPrimary(mode analysisMode, v *verdict, never, pending []edge) (bool, error) {
|
||||
if !mode.noWorkflowFiles || len(never) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
for _, e := range never {
|
||||
if slices.ContainsFunc(e.dep.paths, func(p string) bool { return !auxiliary(p) }) {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
for _, e := range append(slices.Clone(never), pending...) {
|
||||
if slices.ContainsFunc(e.dep.paths, func(p string) bool { return strings.HasPrefix(p, workflowsDir) }) {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
files, err := workflowFiles(v.commits)
|
||||
return len(files) > 0, err
|
||||
}
|
||||
|
||||
// testsOnly is the note a workflow original gets that waits for a
|
||||
// maintainer although its tests build on what never comes.
|
||||
func testsOnly(v *verdict, never []edge, l line) string {
|
||||
var who []string
|
||||
for _, e := range never {
|
||||
if e.dep.v != nil {
|
||||
who = append(who, fmt.Sprintf("#%d", e.dep.v.o.number))
|
||||
} else {
|
||||
who = append(who, e.dep.merge[:12])
|
||||
}
|
||||
}
|
||||
slices.Sort(who)
|
||||
return oneLineText(fmt.Sprintf("only its tests or docs build on %s, which never reaches %s", strings.Join(slices.Compact(who), ", "), l.branch()))
|
||||
}
|
||||
|
||||
// dependencyPark is the park of v for prerequisites that never come.
|
||||
func dependencyPark(v *verdict, never []edge) *parkedEntry {
|
||||
p := &parkedEntry{Original: v.o.number, Merge: v.o.merge, Kind: parkDependency}
|
||||
for _, e := range never {
|
||||
if e.dep.v != nil {
|
||||
p.Needs = append(p.Needs, e.dep.v.o.number)
|
||||
} else {
|
||||
p.Commits = append(p.Commits, e.dep.merge)
|
||||
}
|
||||
}
|
||||
slices.Sort(p.Needs)
|
||||
p.Needs = slices.Compact(p.Needs)
|
||||
slices.Sort(p.Commits)
|
||||
p.Commits = slices.Compact(p.Commits)
|
||||
return p
|
||||
}
|
||||
|
||||
// depsAt is what v builds on that target lacks, read once per target: the
|
||||
// run asks again at its head once earlier picks moved the branch.
|
||||
func (b *batch) depsAt(v *verdict, target string) ([]dependency, error) {
|
||||
key := target + " " + v.o.merge
|
||||
if d, ok := b.depCache[key]; ok {
|
||||
return d, nil
|
||||
}
|
||||
deps, diag, err := b.dependencies(v, target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v.addDiag(diag...)
|
||||
if b.depCache == nil {
|
||||
b.depCache = map[string][]dependency{}
|
||||
}
|
||||
b.depCache[key] = deps
|
||||
return deps, nil
|
||||
}
|
||||
|
||||
// stateKind is what a classification makes of an original the policy takes
|
||||
// that nothing carries, in the order that decides it.
|
||||
type stateKind int
|
||||
|
||||
const (
|
||||
// stHeld is deferred for a reason a person gave.
|
||||
stHeld stateKind = iota + 1
|
||||
// stPartial would leave part of it without effect on the line.
|
||||
stPartial
|
||||
// stWorkflowPrimary waits for a maintainer, though its tests build on
|
||||
// what never comes.
|
||||
stWorkflowPrimary
|
||||
// stParked builds on what never comes.
|
||||
stParked
|
||||
// stNeeds waits on prerequisites still to come.
|
||||
stNeeds
|
||||
// stOwn is what the record says of it, or missing.
|
||||
stOwn
|
||||
)
|
||||
|
||||
// state is a classification of one original: what it is, what it is to an
|
||||
// original that builds on it, and for a wait the class that decides it and
|
||||
// the prerequisites of that class.
|
||||
type state struct {
|
||||
kind stateKind
|
||||
class edgeClass
|
||||
wait edgeClass
|
||||
needs []*verdict
|
||||
}
|
||||
|
||||
// classify decides, in merge order, what becomes of every original the
|
||||
// policy takes that neither the line nor the open batch carries, as an
|
||||
// operation that picks nothing sees it: check, and the ones that only
|
||||
// change the record. held are the reasons people deferred originals for,
|
||||
// recorded what the record defers, and listed says the record's workflow
|
||||
// waits still wait on an open batch. Parks it finds go into each verdict.
|
||||
func (b *batch) classify(mode analysisMode, held map[int]string, recorded map[int]deferredEntry, listed bool) (map[*verdict]*state, error) {
|
||||
states := map[*verdict]*state{}
|
||||
classOf := func(f *verdict) edgeClass {
|
||||
if c, ok := b.ruleClass(f); ok {
|
||||
return c
|
||||
}
|
||||
if s := states[f]; s != nil {
|
||||
return s.class
|
||||
}
|
||||
return edgeMissing
|
||||
}
|
||||
for _, v := range b.qualifying() {
|
||||
if v.onBatch != "" {
|
||||
continue
|
||||
}
|
||||
s := &state{}
|
||||
states[v] = s
|
||||
e := recorded[v.o.number]
|
||||
never, pending := edges(v.deps, classOf)
|
||||
primary, err := workflowPrimary(mode, v, never, pending)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch {
|
||||
case held[v.o.number] != "":
|
||||
s.kind, s.class = stHeld, edgeDeferred
|
||||
case v.park != nil && v.park.Kind == parkPartial:
|
||||
s.kind, s.class = stPartial, edgeNever
|
||||
case primary:
|
||||
s.kind, s.class = stWorkflowPrimary, edgeMissing
|
||||
if listed && e.Workflows {
|
||||
s.class = edgeWorkflow
|
||||
}
|
||||
case len(never) > 0:
|
||||
v.park = dependencyPark(v, never)
|
||||
s.kind, s.class = stParked, edgeNever
|
||||
case len(pending) > 0:
|
||||
s.kind = stNeeds
|
||||
s.wait, s.needs = strongest(pending)
|
||||
s.class = s.wait
|
||||
case listed && e.Workflows:
|
||||
s.kind, s.class = stOwn, edgeWorkflow
|
||||
case !e.Workflows && len(e.Needs) == 0 && (e.Commit != "" || len(e.Files) > 0):
|
||||
s.kind, s.class = stOwn, edgeDeferred
|
||||
default:
|
||||
s.kind, s.class = stOwn, edgeMissing
|
||||
}
|
||||
}
|
||||
return states, nil
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// depsOf reads what original n builds on that the line lacks.
|
||||
func depsOf(t testing.TB, f *fixture, n int) ([]dependency, []string) {
|
||||
t.Helper()
|
||||
f.git("fetch", "-q", "origin")
|
||||
b, err := shapeOf(line11)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
deps, diag, err := b.dependencies(b.find(n), b.lineHead)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return deps, diag
|
||||
}
|
||||
|
||||
// depNames lists dependencies as #N or a short commit, with their paths.
|
||||
func depNames(deps []dependency) []string {
|
||||
var out []string
|
||||
for _, d := range deps {
|
||||
who := d.merge[:12]
|
||||
if d.v != nil {
|
||||
who = fmt.Sprintf("#%d", d.v.o.number)
|
||||
}
|
||||
out = append(out, fmt.Sprintf("%s %v %v", who, d.paths, d.added))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Every commit main gained since the line branched is brought by one commit
|
||||
// of its first-parent line: a pull request's own commits by its merge, a
|
||||
// direct push by itself, and a commit from before the fork point by none.
|
||||
func TestBroughtBy(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
base := f.git("rev-parse", "origin/release-1.1")
|
||||
mc3 := f.prCommits(3, "feat(app): three", "",
|
||||
change{files: map[string]string{"app/3.txt": "3\n"}},
|
||||
change{mergeMain: true},
|
||||
change{files: map[string]string{"app/3.txt": "3b\n"}})
|
||||
f.write("app/push.txt", "pushed\n")
|
||||
f.git("add", "-A")
|
||||
f.git("commit", "-q", "-m", "chore: a direct push")
|
||||
f.git("push", "-q", "origin", "main")
|
||||
push := f.git("rev-parse", "HEAD")
|
||||
mc5 := f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
by, err := broughtBy(base, mc5)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
moved := f.git("rev-parse", mc3+"^1")
|
||||
for _, c := range strings.Fields(f.git("rev-list", mc3+"^1.."+mc3+"^2")) {
|
||||
if c == moved {
|
||||
continue
|
||||
}
|
||||
if by[c] != mc3 {
|
||||
t.Errorf("%.12s, a commit of #3's branch, is brought by %.12s, want #3's merge", c, by[c])
|
||||
}
|
||||
}
|
||||
for c, want := range map[string]string{mc3: mc3, moved: moved, push: push, mc5: mc5, f.git("rev-parse", mc5+"^2"): mc5} {
|
||||
if by[c] != want {
|
||||
t.Errorf("%.12s is brought by %.12s, want %.12s", c, by[c], want)
|
||||
}
|
||||
}
|
||||
if _, ok := by[base]; ok {
|
||||
t.Errorf("the fork point %.12s is in the map", base)
|
||||
}
|
||||
}
|
||||
|
||||
// V1: a file the original changes that the line does not have names the
|
||||
// first-parent commit on main that added it, a merge or a direct push.
|
||||
func TestDependenciesV1(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): x", "", map[string]string{"app/x.txt": appFile})
|
||||
f.write("app/y.txt", appFile)
|
||||
f.git("add", "-A")
|
||||
f.git("commit", "-q", "-m", "chore: y, pushed")
|
||||
f.git("push", "-q", "origin", "main")
|
||||
push := f.git("rev-parse", "HEAD")
|
||||
f.pr(5, "fix(app): fix x and y", "", map[string]string{"app/x.txt": "fixed\n", "app/y.txt": "fixed\n"})
|
||||
deps, diag := depsOf(t, f, 5)
|
||||
want := []string{push[:12] + " [app/y.txt] true", "#3 [app/x.txt] true"}
|
||||
slices.Sort(want)
|
||||
got := depNames(deps)
|
||||
slices.Sort(got)
|
||||
if !slices.Equal(got, want) || len(diag) != 0 {
|
||||
t.Errorf("dependencies = %q, %q; want %q", got, diag, want)
|
||||
}
|
||||
}
|
||||
|
||||
// v2World gives the line app/a.txt as the base had it, and main a feat that
|
||||
// changes its second line and a fix of that same line.
|
||||
func v2World(t testing.TB, onLine string) *fixture {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
if onLine != "" {
|
||||
f.onLine("release-1.1", "fix(app): a, by hand", map[string]string{"app/a.txt": onLine})
|
||||
}
|
||||
f.pr(3, "feat(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(5, "fix(app): an excited two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
return f
|
||||
}
|
||||
|
||||
// V2c: a line the original changes that blame gives to another original is
|
||||
// a dependency only while the line's file lacks that line's text.
|
||||
func TestDependenciesV2c(t *testing.T) {
|
||||
if deps, diag := depsOf(t, v2World(t, ""), 5); !slices.Equal(depNames(deps), []string{"#3 [app/a.txt] false"}) || len(diag) != 0 {
|
||||
t.Errorf("dependencies = %q, %q; want #3 through app/a.txt", depNames(deps), diag)
|
||||
}
|
||||
if deps, _ := depsOf(t, v2World(t, strings.Replace(appFile, "two", "TWO", 1)), 5); len(deps) != 0 {
|
||||
t.Errorf("dependencies with #3's line on the line by hand = %q; want none", depNames(deps))
|
||||
}
|
||||
}
|
||||
|
||||
// The content check trims a line's edges and nothing else: a reindented line
|
||||
// is there, a line with a space inside it is not.
|
||||
func TestDependenciesTrimOnlyEdges(t *testing.T) {
|
||||
if deps, _ := depsOf(t, v2World(t, strings.Replace(appFile, "two", " TWO\t", 1)), 5); len(deps) != 0 {
|
||||
t.Errorf("dependencies with #3's line reindented = %q; want none", depNames(deps))
|
||||
}
|
||||
if deps, _ := depsOf(t, v2World(t, strings.Replace(appFile, "two", "T WO", 1)), 5); !slices.Equal(depNames(deps), []string{"#3 [app/a.txt] false"}) {
|
||||
t.Errorf("dependencies with a space inside #3's line = %q; want #3", depNames(deps))
|
||||
}
|
||||
}
|
||||
|
||||
// Lines a tool wrote are not blamed: a generated file whole, and the keys
|
||||
// hack/update-crd.sh writes into an ApplicationDefinition, while the keys a
|
||||
// person writes there are.
|
||||
func TestDependenciesSkipGeneratedRanges(t *testing.T) {
|
||||
const rd = "packages/system/app-rd/cozyrds/app.yaml"
|
||||
rdFile := func(schema, category string) string {
|
||||
return "apiVersion: cozystack.io/v1alpha1\nkind: ApplicationDefinition\nmetadata:\n name: app\nspec:\n application:\n kind: App\n openAPISchema: |-\n {\"v\":" + schema + "}\n dashboard:\n category: " + category + "\n keysOrder: [[\"spec\", \"" + schema + "\"]]\n"
|
||||
}
|
||||
world := func(t *testing.T, before, feat, fix map[string]string) *fixture {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: generated files", "", before)
|
||||
f.lines()
|
||||
f.pr(3, "feat(api): regenerate", "", feat)
|
||||
f.pr(5, "fix(api): regenerate again", "", fix)
|
||||
return f
|
||||
}
|
||||
gen := "// Code generated by controller-gen. DO NOT EDIT.\n\npackage v1\n\nvar a = 1\n"
|
||||
f := world(t,
|
||||
map[string]string{"api/zz_generated.deepcopy.go": "package v1\n\nvar a = 1\n", "api/types_gen.go": gen, rd: rdFile("1", "A")},
|
||||
map[string]string{"api/zz_generated.deepcopy.go": "package v1\n\nvar a = 2\n", "api/types_gen.go": strings.Replace(gen, "= 1", "= 2", 1), rd: rdFile("2", "A")},
|
||||
map[string]string{"api/zz_generated.deepcopy.go": "package v1\n\nvar a = 3\n", "api/types_gen.go": strings.Replace(gen, "= 1", "= 3", 1), rd: rdFile("3", "A")})
|
||||
if deps, diag := depsOf(t, f, 5); len(deps) != 0 || len(diag) != 0 {
|
||||
t.Errorf("dependencies through generated lines = %q, %q; want none", depNames(deps), diag)
|
||||
}
|
||||
f = world(t, map[string]string{rd: rdFile("1", "A")}, map[string]string{rd: rdFile("2", "B")}, map[string]string{rd: rdFile("3", "C")})
|
||||
if deps, _ := depsOf(t, f, 5); !slices.Equal(depNames(deps), []string{"#3 [" + rd + "] false"}) {
|
||||
t.Errorf("dependencies through a key a person writes = %q; want #3", depNames(deps))
|
||||
}
|
||||
}
|
||||
|
||||
// Evidence the detector cannot read, a binary file or a symlink, is a
|
||||
// diagnostic and never a dependency.
|
||||
func TestDependenciesIncompleteNeverParks(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: an image", "", map[string]string{"app/a.bin": "a\x00one"})
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): a new image", "", map[string]string{"app/a.bin": "a\x00two"})
|
||||
f.pr(5, "fix(app): a fixed image", "", map[string]string{"app/a.bin": "a\x00three"})
|
||||
if deps, diag := depsOf(t, f, 5); len(deps) != 0 || !slices.Equal(diag, []string{`incomplete evidence: "app/a.bin" is binary`}) {
|
||||
t.Errorf("dependencies = %q, %q; want none and a diagnostic", depNames(deps), diag)
|
||||
}
|
||||
}
|
||||
|
||||
// Output of git the detector cannot read whole is an error, never taken
|
||||
// for an absent dependency.
|
||||
func TestDependenciesMalformedOutputIsAnError(t *testing.T) {
|
||||
sha := strings.Repeat("a", 40)
|
||||
for name, out := range map[string]string{
|
||||
"a short header": sha + " 1\n\tline\n",
|
||||
"a header of no commit": "aaaa 1 1 1\n\tline\n",
|
||||
"a header of no number": sha + " x 1 1\n\tline\n",
|
||||
"a line with no header": "\tline\n",
|
||||
"a header with no line": sha + " 1 1 1\nauthor x\n",
|
||||
} {
|
||||
if _, err := parseBlame(out); err == nil {
|
||||
t.Errorf("%s: parseBlame accepted %q", name, out)
|
||||
}
|
||||
}
|
||||
if lines, err := parseBlame(sha + " 2 2 1\nauthor x\nfilename a\n\ttwo\n" + sha + " 4 4\n\tfour\n"); err != nil || len(lines) != 2 || lines[1].text != "four" {
|
||||
t.Errorf("parseBlame of sound output = %+v, %v", lines, err)
|
||||
}
|
||||
if _, _, err := oldRanges("@@ -x +1 @@\n"); err == nil {
|
||||
t.Error("oldRanges accepted an unreadable hunk header")
|
||||
}
|
||||
}
|
||||
|
||||
// Paths go to git as they are: a name a pathspec would read as a pattern
|
||||
// means only itself.
|
||||
func TestDependenciesLiteralPaths(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): a bracketed file", "", map[string]string{"app/[x].txt": appFile})
|
||||
f.pr(4, "feat(app): x", "", map[string]string{"app/x.txt": appFile})
|
||||
f.pr(5, "fix(app): fix the bracketed file", "", map[string]string{"app/[x].txt": "fixed\n"})
|
||||
if deps, _ := depsOf(t, f, 5); !slices.Equal(depNames(deps), []string{"#3 [app/[x].txt] true"}) {
|
||||
t.Errorf("dependencies = %q; want #3 alone", depNames(deps))
|
||||
}
|
||||
}
|
||||
|
||||
// A file with no final newline blames its last line like any other.
|
||||
func TestDependenciesNoFinalNewline(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": "one\ntwo"})
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): a loud two", "", map[string]string{"app/a.txt": "one\nTWO"})
|
||||
f.pr(5, "fix(app): an excited two", "", map[string]string{"app/a.txt": "one\nTWO!"})
|
||||
if deps, diag := depsOf(t, f, 5); !slices.Equal(depNames(deps), []string{"#3 [app/a.txt] false"}) || len(diag) != 0 {
|
||||
t.Errorf("dependencies = %q, %q; want #3", depNames(deps), diag)
|
||||
}
|
||||
}
|
||||
|
||||
// Blame takes the revision as a full commit name, which git cannot read as
|
||||
// an option, and refuses --end-of-options, so the tool passes none.
|
||||
func TestBlameInvocation(t *testing.T) {
|
||||
sha := strings.Repeat("a", 40)
|
||||
args, err := blameArgs(sha, "-x", []hunk{{2, 3}, {9, 1}})
|
||||
if want := []string{"blame", "--porcelain", "-w", "-L", "2,+3", "-L", "9,+1", sha, "--", "-x"}; err != nil || !slices.Equal(args, want) {
|
||||
t.Errorf("blameArgs = %q, %v; want %q", args, err, want)
|
||||
}
|
||||
for _, rev := range []string{"HEAD", "--output=x", strings.Repeat("a", 39)} {
|
||||
if _, err := blameArgs(rev, "a", nil); err == nil {
|
||||
t.Errorf("blameArgs took %q for a revision", rev)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// What the detector could not read shows with the original whatever became
|
||||
// of it: picked, waiting for a maintainer, or a deferred conflict, in the
|
||||
// body and in the report.
|
||||
func TestDiagnosticsShowOnEveryOutcome(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: images", "", map[string]string{"app/p.bin": "p\x00one", "app/w.bin": "w\x00one", "app/c.bin": "c\x00one", "app/c.txt": "old\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): c, on the line", map[string]string{"app/c.txt": "line\n"})
|
||||
f.pr(5, "fix(app): picked", "", map[string]string{"app/p.bin": "p\x00two"})
|
||||
f.pr(6, "fix(ci): waiting", "", map[string]string{"app/w.bin": "w\x00two", ".github/workflows/six.yaml": "on: push\n"})
|
||||
f.pr(7, "fix(app): conflicting", "", map[string]string{"app/c.bin": "c\x00two", "app/c.txt": "seven\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1", "--no-workflow-files")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
for _, s := range []struct{ section, file string }{{"[#5]", "p.bin"}, {"### Deferred conflicts", "c.bin"}, {"### Waiting for a maintainer", "w.bin"}} {
|
||||
_, after, ok := strings.Cut(stdout, s.section)
|
||||
if want := " - `incomplete evidence: \"app/" + s.file + "\" is binary`"; !ok || !strings.Contains(after, want) {
|
||||
t.Errorf("the body does not show %q after %s:\n%s", want, s.section, stdout)
|
||||
}
|
||||
}
|
||||
for n, file := range map[int]string{5: "p.bin", 6: "w.bin", 7: "c.bin"} {
|
||||
if want := fmt.Sprintf(`"%d":["incomplete evidence: \"app/%s\" is binary"]`, n, file); !strings.Contains(stdout, want) {
|
||||
t.Errorf("the report lacks %s:\n%s", want, stdout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkAnalyze reads what forty originals build on, over a history
|
||||
// where half of them build on a feat the line lacks.
|
||||
func BenchmarkAnalyze(b *testing.B) {
|
||||
f := newFixture(b)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
for n := 10; n < 50; n++ {
|
||||
file := fmt.Sprintf("app/f%d.txt", n)
|
||||
if n%2 == 0 {
|
||||
f.pr(n, "feat(app): "+file, "", map[string]string{file: appFile})
|
||||
continue
|
||||
}
|
||||
prev := fmt.Sprintf("app/f%d.txt", n-1)
|
||||
f.pr(n, "fix(app): "+prev, "", map[string]string{prev: strings.Replace(appFile, "two", fmt.Sprint(n), 1), "app/a.txt": strings.Replace(appFile, "three", fmt.Sprint(n), 1)})
|
||||
}
|
||||
f.git("fetch", "-q", "origin")
|
||||
b.ResetTimer()
|
||||
for range b.N {
|
||||
bt, err := shapeOf(line11)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
if err := bt.analyze(); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Files the line lacks count toward the files an original may have read
|
||||
// for it, like the ones blamed.
|
||||
func TestDependenciesCapMissingFiles(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
added, fixed := map[string]string{}, map[string]string{}
|
||||
for i := range maxDepFiles + 1 {
|
||||
added[fmt.Sprintf("app/f%03d.txt", i)] = "v1\n"
|
||||
fixed[fmt.Sprintf("app/f%03d.txt", i)] = "v2\n"
|
||||
}
|
||||
f.pr(3, "feat(app): many files", "", added)
|
||||
f.pr(5, "fix(app): all of them", "", fixed)
|
||||
if _, diag := depsOf(t, f, 5); !slices.ContainsFunc(diag, func(d string) bool { return strings.Contains(d, fmt.Sprintf("more than %d files", maxDepFiles)) }) {
|
||||
t.Errorf("diag = %q, want the file cap", diag)
|
||||
}
|
||||
}
|
||||
@@ -117,6 +117,9 @@ func (a *app) redrop(b *batch, numbers []int, reason string, undrop bool, push b
|
||||
if err := b.load(v); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if err := b.rejudged(v); err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if undrop {
|
||||
continue
|
||||
}
|
||||
@@ -159,6 +162,38 @@ func (b *batch) bindDrops(merges []*original) (map[int]droppedEntry, error) {
|
||||
})
|
||||
}
|
||||
|
||||
// waitOrder refuses a record whose waits or parks need an original merged
|
||||
// after the one that needs it: a dependency is always on main first.
|
||||
func (b *batch) waitOrder(merges []*original) error {
|
||||
if b.src.rec == nil {
|
||||
return nil
|
||||
}
|
||||
at := map[int]int{}
|
||||
for i, o := range merges {
|
||||
at[o.number] = i
|
||||
}
|
||||
check := func(n int, needs []int) error {
|
||||
i, ok := at[n]
|
||||
for _, f := range needs {
|
||||
if j, known := at[f]; ok && known && j >= i {
|
||||
return fmt.Errorf("batch pull request #%d has #%d wait on #%d, which merged after it", b.src.carrier.Number, n, f)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for _, d := range b.src.rec.Deferred {
|
||||
if err := check(d.Original, d.Needs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, p := range b.src.rec.Parked {
|
||||
if err := check(p.Original, p.Needs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// bindEntries binds drops to main: one whose original is among merges, the
|
||||
// originals main merged since the line branched, has to name that
|
||||
// original's merge exactly; any other has to be a merge of its number on
|
||||
|
||||
@@ -0,0 +1,625 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A clean pick can still carry only part of an original: git deletes a file
|
||||
// the line never had without a word, and leaves alone a file the line lacks.
|
||||
// What follows tells such a unit from a whole one, by what it leaves without
|
||||
// effect of the files the original modifies or deletes.
|
||||
|
||||
// treeEntry is one record of git ls-tree.
|
||||
type treeEntry struct{ mode, kind, oid string }
|
||||
|
||||
// regular says whether git can merge the entry as text: a file, not a
|
||||
// symlink, a submodule or a tree.
|
||||
func (e treeEntry) regular() bool {
|
||||
return e.kind == "blob" && (e.mode == "100644" || e.mode == "100755")
|
||||
}
|
||||
|
||||
// treeEntries reads the entries paths have at treeish, from full ls-tree
|
||||
// records: a path that is a tree there, or absent, has none.
|
||||
func treeEntries(treeish string, paths []string) (map[string]treeEntry, error) {
|
||||
out := map[string]treeEntry{}
|
||||
want := map[string]bool{}
|
||||
for _, p := range paths {
|
||||
want[p] = true
|
||||
}
|
||||
const chunk = 500
|
||||
for len(paths) > 0 {
|
||||
n := min(chunk, len(paths))
|
||||
args := append([]string{"--literal-pathspecs", "ls-tree", "-r", "-z", "--full-tree", "--end-of-options", treeish, "--"}, paths[:n]...)
|
||||
raw, stderr, code, err := gitStatus(args...)
|
||||
switch {
|
||||
case err != nil:
|
||||
return nil, fmt.Errorf("git ls-tree %s did not run", terminalSafe(treeish))
|
||||
case code != 0:
|
||||
// The paths stay out of the error: they are file names someone
|
||||
// chose, and the error reaches a terminal.
|
||||
return nil, fmt.Errorf("git ls-tree %s: exit %d: %s", terminalSafe(treeish), code, terminalSafe(stderr))
|
||||
}
|
||||
for _, rec := range splitNUL(raw) {
|
||||
meta, path, ok := strings.Cut(rec, "\t")
|
||||
f := strings.Fields(meta)
|
||||
if !ok || len(f) != 3 || checkSHA(f[2]) != nil {
|
||||
return nil, fmt.Errorf("git ls-tree %s: unreadable record %q", treeish, terminalSafe(rec))
|
||||
}
|
||||
if want[path] {
|
||||
out[path] = treeEntry{mode: f[0], kind: f[1], oid: f[2]}
|
||||
}
|
||||
}
|
||||
paths = paths[n:]
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// renameSet is what git diff -M found renamed between two commits, old path
|
||||
// to new, and whether it looked at every candidate.
|
||||
type renameSet struct {
|
||||
moved map[string]string
|
||||
complete bool
|
||||
}
|
||||
|
||||
// renames reads the renames between from and to, once per pair. git says on
|
||||
// stderr when it skipped inexact detection for too many files; complete is
|
||||
// then false, and no path may be called absent for want of a rename.
|
||||
func (b *batch) renames(from, to string) (renameSet, error) {
|
||||
key := from + ".." + to
|
||||
if s, ok := b.renameCache[key]; ok {
|
||||
return s, nil
|
||||
}
|
||||
// -l0 lifts git's own limit, which the picker's merge does not share:
|
||||
// a rename the pick follows has to be one the checks of its shape see.
|
||||
stdout, stderr, code, err := gitStatusTimed("diff", "--no-ext-diff", "--no-textconv", "-M", "-l"+strconv.Itoa(b.renameLimit), "--name-status", "-z", "--diff-filter=R", "--end-of-options", from, to)
|
||||
switch {
|
||||
case errors.Is(err, errDeadline):
|
||||
// Out of time is rename detection skipped: no path is called
|
||||
// absent for want of it.
|
||||
s := renameSet{moved: map[string]string{}}
|
||||
if b.renameCache == nil {
|
||||
b.renameCache = map[string]renameSet{}
|
||||
}
|
||||
b.renameCache[key] = s
|
||||
return s, nil
|
||||
case err != nil:
|
||||
return renameSet{}, err
|
||||
case code != 0:
|
||||
return renameSet{}, fmt.Errorf("git diff -M %s %s: exit %d: %s", from, to, code, strings.TrimSpace(stderr))
|
||||
}
|
||||
f := splitNUL(stdout)
|
||||
if len(f)%3 != 0 {
|
||||
return renameSet{}, fmt.Errorf("unreadable renames between %s and %s", from, to)
|
||||
}
|
||||
s := renameSet{moved: map[string]string{}, complete: !strings.Contains(stderr, "detection was skipped")}
|
||||
for i := 0; i < len(f); i += 3 {
|
||||
if !strings.HasPrefix(f[i], "R") {
|
||||
return renameSet{}, fmt.Errorf("unreadable renames between %s and %s: %q", from, to, terminalSafe(f[i]))
|
||||
}
|
||||
s.moved[f[i+1]] = f[i+2]
|
||||
}
|
||||
if b.renameCache == nil {
|
||||
b.renameCache = map[string]renameSet{}
|
||||
}
|
||||
b.renameCache[key] = s
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// effectPaths are the paths v modifies or deletes; what it adds has nothing
|
||||
// to correspond to.
|
||||
func effectPaths(v *verdict) []string {
|
||||
var out []string
|
||||
for _, c := range v.changes {
|
||||
if c.status == 'M' || c.status == 'D' {
|
||||
out = append(out, c.path)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// correspondence maps a path of the original (at M^1) to the path holding the
|
||||
// same file on target, through a main-side rename base..M^1, a line-side
|
||||
// rename base..target, or both composed through the base path. A path with
|
||||
// no counterpart maps to itself, and one git skipped rename detection for is
|
||||
// left out and named in unknown: it is not known to be absent.
|
||||
func (b *batch) correspondence(v *verdict, target string) (map[string]string, []string, error) {
|
||||
paths := effectPaths(v)
|
||||
have, err := treeEntries(target, paths)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
corr := map[string]string{}
|
||||
var missing []string
|
||||
for _, p := range paths {
|
||||
if _, ok := have[p]; ok {
|
||||
corr[p] = p
|
||||
} else {
|
||||
missing = append(missing, p)
|
||||
}
|
||||
}
|
||||
if len(missing) == 0 {
|
||||
return corr, nil, nil
|
||||
}
|
||||
onMain, err := b.renames(b.base, v.o.merge+"^1")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
onLine, err := b.renames(b.base, target)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
fromBase := map[string]string{}
|
||||
for old, now := range onMain.moved {
|
||||
fromBase[now] = old
|
||||
}
|
||||
candidates := map[string]string{}
|
||||
var names []string
|
||||
for _, p := range missing {
|
||||
q := p
|
||||
if old, ok := fromBase[p]; ok {
|
||||
q = old
|
||||
}
|
||||
r := q
|
||||
if now, ok := onLine.moved[q]; ok {
|
||||
r = now
|
||||
}
|
||||
if r != p {
|
||||
candidates[p] = r
|
||||
names = append(names, r)
|
||||
}
|
||||
}
|
||||
found, err := treeEntries(target, names)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var unknown []string
|
||||
for _, p := range missing {
|
||||
if r, ok := candidates[p]; ok {
|
||||
if _, ok := found[r]; ok {
|
||||
corr[p] = r
|
||||
continue
|
||||
}
|
||||
}
|
||||
if !onMain.complete || !onLine.complete {
|
||||
unknown = append(unknown, p)
|
||||
continue
|
||||
}
|
||||
corr[p] = p
|
||||
}
|
||||
return corr, unknown, nil
|
||||
}
|
||||
|
||||
// omittedEffects lists the paths v modifies or deletes that the change from
|
||||
// before to after leaves without effect, each judged at its corresponding
|
||||
// path c(p) on before. A modified path is omitted when c(p) exists on neither
|
||||
// side, applied when the change touches it, and when the change leaves an
|
||||
// existing file alone, satisfied only if the line already has what the
|
||||
// original makes of it. A deleted path is omitted when c(p) is absent before:
|
||||
// a deletion the line already did is no change this unit makes. diag says
|
||||
// what could not be judged, which is never counted as omitted.
|
||||
func (b *batch) omittedEffects(v *verdict, before, after string) ([]string, []string, error) {
|
||||
if err := b.load(v); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
corr, unknown, err := b.correspondence(v, before)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var diag []string
|
||||
for _, p := range unknown {
|
||||
diag = append(diag, "git skipped rename detection, so where "+strconv.Quote(p)+" is on the line is not known")
|
||||
}
|
||||
added := map[string]bool{}
|
||||
for _, c := range v.changes {
|
||||
if c.status == 'A' {
|
||||
added[c.path] = true
|
||||
}
|
||||
}
|
||||
// A deleted path whose counterpart the original adds itself is a rename
|
||||
// the line already made the same way: it is judged as a modified file,
|
||||
// against what the original makes of that path.
|
||||
var at, own, moved []string
|
||||
movedTo := map[string]string{}
|
||||
for _, c := range v.changes {
|
||||
cp, ok := corr[c.path]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
at = append(at, cp)
|
||||
switch {
|
||||
case c.status == 'M':
|
||||
own = append(own, c.path)
|
||||
case c.status == 'D' && cp != c.path && added[cp]:
|
||||
own, moved = append(own, c.path), append(moved, cp)
|
||||
movedTo[c.path] = cp
|
||||
}
|
||||
}
|
||||
bef, err := treeEntries(before, at)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
aft, err := treeEntries(after, at)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
pre, err := treeEntries(v.o.merge+"^1", own)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
post, err := treeEntries(v.o.merge, append(slices.Clone(own), moved...))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
// A change that makes none is judged by its trees, whatever names them:
|
||||
// a commit and the tree a net merge wrote can be the same change.
|
||||
same := before == after
|
||||
if !same {
|
||||
bt, err := resolveTree(before)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
at, err := resolveTree(after)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
same = bt == at
|
||||
}
|
||||
var omitted []string
|
||||
// modified judges a path whose counterpart cp should end up as the
|
||||
// original's postimage p1 of it.
|
||||
modified := func(path, cp string, p0, p1 treeEntry) error {
|
||||
be, inBefore := bef[cp]
|
||||
ae, inAfter := aft[cp]
|
||||
switch {
|
||||
case !inBefore && !inAfter:
|
||||
omitted = append(omitted, path)
|
||||
return nil
|
||||
case inBefore != inAfter || be != ae:
|
||||
return nil
|
||||
case be.oid == p1.oid && (p0.mode == p1.mode || be.mode == p1.mode):
|
||||
return nil
|
||||
case !be.regular() || !p0.regular() || !p1.regular() || p0.mode != p1.mode:
|
||||
diag = append(diag, "the mode or type of "+strconv.Quote(path)+" changes, which only a person can judge")
|
||||
return nil
|
||||
}
|
||||
applied, unsupported, err := b.alreadyApplied(p0.oid, be.oid, p1.oid)
|
||||
switch {
|
||||
case err != nil:
|
||||
return fmt.Errorf("judging %s of #%d: %w", strconv.Quote(path), v.o.number, err)
|
||||
case unsupported:
|
||||
diag = append(diag, "git cannot merge "+strconv.Quote(path)+" as text, so whether the line has its change is not known")
|
||||
case !applied:
|
||||
omitted = append(omitted, path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
for _, c := range v.changes {
|
||||
cp, ok := corr[c.path]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
switch {
|
||||
case c.status == 'D' && movedTo[c.path] != "":
|
||||
if err := modified(c.path, cp, pre[c.path], post[cp]); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
case c.status == 'D':
|
||||
// Absent before: the line already made the deletion, which is
|
||||
// no change of a unit that makes others, and is all of one
|
||||
// that makes none. Still there after: the deletion did not
|
||||
// happen, whatever the unit changed in the file; keeping what
|
||||
// the line made of it is a port of its own.
|
||||
_, inBefore := bef[cp]
|
||||
_, inAfter := aft[cp]
|
||||
if !inBefore && !same || inAfter {
|
||||
omitted = append(omitted, c.path)
|
||||
}
|
||||
case c.status == 'M':
|
||||
if err := modified(c.path, cp, pre[c.path], post[c.path]); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
for i := range diag {
|
||||
diag[i] = oneLineText(diag[i])
|
||||
}
|
||||
return omitted, diag, nil
|
||||
}
|
||||
|
||||
// alreadyApplied says whether cur, a file the unit leaves alone, already
|
||||
// holds the original's change from pre to post: it is post itself, or git
|
||||
// merge-file of the three merges cleanly back into cur. unsupported is
|
||||
// content merge-file refuses, such as differing binary files, which says
|
||||
// nothing either way.
|
||||
func (b *batch) alreadyApplied(pre, cur, post string) (applied, unsupported bool, err error) {
|
||||
if cur == post {
|
||||
return true, false, nil
|
||||
}
|
||||
b.mergeFiles++
|
||||
blobs, err := readObjects([]string{pre, cur, post}, "blob")
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
dir, err := os.MkdirTemp("", "backport-batch-merge-file-*")
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
defer func() { _ = os.RemoveAll(dir) }()
|
||||
names := map[string]string{}
|
||||
for _, f := range []struct{ name, oid string }{{"cur", cur}, {"pre", pre}, {"post", post}} {
|
||||
names[f.name] = filepath.Join(dir, f.name)
|
||||
if err := os.WriteFile(names[f.name], blobs[f.oid], 0o600); err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
}
|
||||
stdout, stderr, code, err := gitStatus("merge-file", "-p", names["cur"], names["pre"], names["post"])
|
||||
switch {
|
||||
case err != nil:
|
||||
return false, false, err
|
||||
case code == 0:
|
||||
return stdout == string(blobs[cur]), false, nil
|
||||
case code > 0 && code < 128:
|
||||
// The number of conflicts.
|
||||
return false, false, nil
|
||||
case strings.Contains(stderr, "Cannot merge binary files"):
|
||||
return false, true, nil
|
||||
}
|
||||
return false, false, fmt.Errorf("git merge-file: exit %d: %s", code, strings.TrimSpace(stderr))
|
||||
}
|
||||
|
||||
// unitsOn maps each original the batch head carries a unit of to that unit,
|
||||
// by its Backported-from trailer or appended -x line: found needs every
|
||||
// commit of a merged original, and misses one whose first pick came out
|
||||
// empty because the line already had that change.
|
||||
func (b *batch) unitsOn(head string) (map[*verdict]string, error) {
|
||||
if m, ok := b.unitCache[head]; ok {
|
||||
return m, nil
|
||||
}
|
||||
units, err := batchUnits(head, b.lineHead)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m := map[*verdict]string{}
|
||||
for _, u := range units {
|
||||
if v := b.unitOriginal(u); v != nil && m[v] == "" {
|
||||
m[v] = u.commit
|
||||
}
|
||||
}
|
||||
if b.unitCache == nil {
|
||||
b.unitCache = map[string]map[*verdict]string{}
|
||||
}
|
||||
b.unitCache[head] = m
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// onBranch says whether the batch head h carries v.
|
||||
func (b *batch) onBranch(h *history, v *verdict) (bool, error) {
|
||||
if found(h, v) {
|
||||
return true, nil
|
||||
}
|
||||
m, err := b.unitsOn(h.head)
|
||||
return m[v] != "", err
|
||||
}
|
||||
|
||||
// partialStates finds what applies only in part on the line. Every unit on
|
||||
// the open batch is checked against its own first parent, whatever the rules
|
||||
// now say of its original: one that leaves an effect out fails the cut until
|
||||
// it is taken out. Every original the policy takes and nothing carries is
|
||||
// checked against the tree its clean net merge onto the line writes, so a
|
||||
// partial one is found with no record and no adder; a conflict is never
|
||||
// called partial.
|
||||
func (b *batch) partialStates() error {
|
||||
defer b.timed(time.Now())
|
||||
b.partialUnits = map[*verdict][]string{}
|
||||
if b.pr != nil {
|
||||
var err error
|
||||
if b.partialUnits, err = b.unitsPartial(b.prHead); err != nil {
|
||||
return err
|
||||
}
|
||||
on, err := b.unitsOn(b.prHead)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for v, commit := range on {
|
||||
v.batched = commit
|
||||
if v.rule == 0 && v.onBatch == "" {
|
||||
v.onBatch = commit
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, v := range b.qualifying() {
|
||||
if v.onBatch != "" {
|
||||
continue
|
||||
}
|
||||
if err := b.partialAt(v, b.lineHead); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// unitsPartial maps each original whose unit between the line's head and
|
||||
// head leaves part of it without effect to what it leaves.
|
||||
func (b *batch) unitsPartial(head string) (map[*verdict][]string, error) {
|
||||
units, err := batchUnits(head, b.lineHead)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := map[*verdict][]string{}
|
||||
for _, u := range units {
|
||||
v := b.unitOriginal(u)
|
||||
if v == nil || len(u.parents) == 0 {
|
||||
continue
|
||||
}
|
||||
omitted, diag, err := b.omittedEffects(v, u.parents[0], u.commit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v.addDiag(diag...)
|
||||
if len(omitted) > 0 {
|
||||
out[v] = append(out[v], omitted...)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// markPartial puts every original on the branch at head whose unit there
|
||||
// does only part of it among the partial units, and so among the stale
|
||||
// ones: still on the batch, and to be taken out before the cut.
|
||||
func (b *batch) markPartial(r *report, head string) error {
|
||||
units := b.partialUnits
|
||||
if b.pr == nil || head != b.prHead {
|
||||
var err error
|
||||
if units, err = b.unitsPartial(head); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
r.partialFiles = map[*verdict][]string{}
|
||||
for _, v := range b.verdicts {
|
||||
files := units[v]
|
||||
if len(files) == 0 {
|
||||
continue
|
||||
}
|
||||
r.partial = append(r.partial, v)
|
||||
r.partialFiles[v] = files
|
||||
if !slices.Contains(r.stale, v) {
|
||||
r.stale = append(r.stale, v)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// keepRecordedPartials judges again, at head, what the batch carries now,
|
||||
// each original the open record parks as partial that the line's head
|
||||
// judges whole: the run's picker found it partial after the picks before
|
||||
// it, which check and an operation that picks nothing do not replay. It
|
||||
// stays parked while its net merge onto head still leaves something
|
||||
// without effect, and is the line's again once that went, as when the pick
|
||||
// that made it partial was taken off.
|
||||
func (b *batch) keepRecordedPartials(head string) error {
|
||||
if !b.src.open || b.src.rec == nil {
|
||||
return nil
|
||||
}
|
||||
for _, p := range b.src.rec.Parked {
|
||||
v := b.find(p.Original)
|
||||
if p.Kind != parkPartial || v == nil || v.rule != 0 || v.onBatch != "" || v.park != nil || v.o.merge != p.Merge {
|
||||
continue
|
||||
}
|
||||
if err := b.partialAt(v, head); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// rejudged reads again what partialStates and analyze read for v once its
|
||||
// rules changed, as a withdrawn drop changes them: it is back where the
|
||||
// line puts it.
|
||||
func (b *batch) rejudged(v *verdict) error {
|
||||
if b.pr != nil {
|
||||
on, err := b.unitsOn(b.prHead)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v.batched = on[v]
|
||||
if v.rule == 0 && v.onBatch == "" {
|
||||
v.onBatch = on[v]
|
||||
}
|
||||
}
|
||||
if v.rule != 0 || v.onBatch != "" {
|
||||
return nil
|
||||
}
|
||||
if err := b.partialAt(v, b.lineHead); err != nil {
|
||||
return err
|
||||
}
|
||||
deps, diag, err := b.dependencies(v, b.lineHead)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v.deps = deps
|
||||
v.addDiag(diag...)
|
||||
return nil
|
||||
}
|
||||
|
||||
// partialAt judges v's clean net merge onto head, and parks it, or clears
|
||||
// the park, by what that merge leaves without effect.
|
||||
func (b *batch) partialAt(v *verdict, head string) error {
|
||||
tree, err := b.netTree(head, v.o)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v.partial = nil
|
||||
if v.park != nil && v.park.Kind == parkPartial {
|
||||
v.park = nil
|
||||
}
|
||||
if tree == "" {
|
||||
return nil
|
||||
}
|
||||
omitted, diag, err := b.omittedEffects(v, head, tree)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v.addDiag(diag...)
|
||||
if len(omitted) > 0 {
|
||||
v.partial = omitted
|
||||
v.park = &parkedEntry{Original: v.o.number, Merge: v.o.merge, Kind: parkPartial, Files: sortedClean(omitted)}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// netTree is the tree a clean net merge of o onto head writes, "" for a
|
||||
// conflict; the line's head reuses what judging the originals merged.
|
||||
func (b *batch) netTree(head string, o *original) (string, error) {
|
||||
if head == b.lineHead && b.lineHist != nil {
|
||||
return b.lineHist.netTree(o)
|
||||
}
|
||||
tree, _, err := noopPick(head, o)
|
||||
return tree, err
|
||||
}
|
||||
|
||||
// addDiag keeps what could not be judged of v, each line once.
|
||||
func (v *verdict) addDiag(lines ...string) {
|
||||
for _, ln := range lines {
|
||||
if ln != "" && !slices.Contains(v.diag, ln) {
|
||||
v.diag = append(v.diag, ln)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func sortedClean(files []string) []string {
|
||||
out := cleanFiles(files)
|
||||
slices.Sort(out)
|
||||
return slices.Compact(out)
|
||||
}
|
||||
|
||||
// leftOut is the refusal for a unit that leaves paths of v without effect.
|
||||
func leftOut(v *verdict, l line, omitted []string) string {
|
||||
quoted := make([]string, len(omitted))
|
||||
for i, p := range omitted {
|
||||
quoted[i] = strconv.Quote(p)
|
||||
}
|
||||
return fmt.Sprintf("#%d leaves %s without effect on %s; port only what the line has as a pull request of its own", v.o.number, strings.Join(quoted, ", "), l.branch())
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const appFile = "one\ntwo\nthree\nfour\nfive\n"
|
||||
|
||||
// renameWorld is the sign-off's rename history: the line keeps app/old.txt,
|
||||
// main renames it to app/new.txt with #3, a feat, and #5 fixes app/new.txt.
|
||||
func renameWorld(t *testing.T) (*fixture, string) {
|
||||
t.Helper()
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: an app file", "", map[string]string{"app/old.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): rename the app file", "", map[string]string{"app/old.txt": "", "app/new.txt": appFile})
|
||||
mc5 := f.pr(5, "fix(app): fix the app file", "", map[string]string{"app/new.txt": strings.Replace(appFile, "three", "THREE", 1)})
|
||||
return f, mc5
|
||||
}
|
||||
|
||||
// emptyBatch pushes the line's head as the batch branch and opens a batch
|
||||
// pull request on it with body, for a fold to land on.
|
||||
func emptyBatch(f *fixture, gh *fakeGH, body string) *foldWorld {
|
||||
f.t.Helper()
|
||||
f.git("fetch", "-q", "origin")
|
||||
f.git("push", "-q", "origin", "origin/release-1.1:refs/heads/"+batchBranch1)
|
||||
f.git("fetch", "-q", "origin")
|
||||
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, body)}
|
||||
return &foldWorld{f: f, gh: gh, head: f.git("rev-parse", "origin/"+batchBranch1)}
|
||||
}
|
||||
|
||||
// A fix of a file main renamed picks onto the file's old name on the line,
|
||||
// and every check of the batch's shape takes the old name as the same file:
|
||||
// verify, a hand fold, and a rebuild by --defer.
|
||||
func TestRenamePickThroughFoldAndVerify(t *testing.T) {
|
||||
f, _ := renameWorld(t)
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
head := openBatch(t, f, gh)
|
||||
if got := f.subjects("origin/release-1.1", head); !slices.Equal(got, []string{"fix(app): fix the app file (#5)", "fix(app): six (#6)"}) {
|
||||
t.Fatalf("the batch carries %q, want #5 and #6", got)
|
||||
}
|
||||
if got := f.git("show", head+":app/old.txt"); !strings.Contains(got, "THREE") {
|
||||
t.Fatalf("#5 did not land on app/old.txt: %q", got)
|
||||
}
|
||||
if code, out, stderr := f.exec(gh, "verify", "--line", "release-1.1", "--head", head); code != 0 {
|
||||
t.Errorf("verify = %d\n%s\n%s", code, out, stderr)
|
||||
}
|
||||
t.Setenv("GIT_COMMITTER_DATE", "2026-10-27T06:00:00Z")
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "6", "--reason", "fails the e2e", "--push"); code != 0 {
|
||||
t.Fatalf("run --defer 6 --push = %d\n%s", code, stderr)
|
||||
}
|
||||
if code, out, stderr := f.exec(gh, "verify", "--line", "release-1.1", "--head", f.remoteHead(batchBranch1)); code != 0 {
|
||||
t.Errorf("verify after the rebuild = %d\n%s\n%s", code, out, stderr)
|
||||
}
|
||||
|
||||
f, mc5 := renameWorld(t)
|
||||
w := emptyBatch(f, &fakeGH{}, "")
|
||||
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/old.txt": strings.Replace(appFile, "three", "THREE", 1)})
|
||||
fold := w.merge(w.head, pick, "fix(app): fix the app file (#5)\n\n"+batchTrailer+": "+mc5)
|
||||
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 0 || out != "ok\n" {
|
||||
t.Errorf("fold of #5 onto app/old.txt = %d, %q\n%s", code, out, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// shaped reads the line from the fixture's remote as verify does.
|
||||
func shaped(t *testing.T, f *fixture) *batch {
|
||||
t.Helper()
|
||||
f.git("fetch", "-q", "origin")
|
||||
b, err := shapeOf(line11)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// commitOn writes files onto parent and returns the commit, leaving every
|
||||
// branch where it was.
|
||||
func commitOn(f *fixture, parent string, files map[string]string) string {
|
||||
f.t.Helper()
|
||||
f.git("switch", "-q", "--detach", parent)
|
||||
f.apply(files)
|
||||
f.git("commit", "-q", "--allow-empty", "-m", "a unit")
|
||||
sha := f.git("rev-parse", "HEAD")
|
||||
f.git("switch", "-q", "main")
|
||||
return sha
|
||||
}
|
||||
|
||||
// A path of the original is followed to where the line keeps the same file:
|
||||
// through a rename on main, on the line, or both, composed through the path
|
||||
// at the base. Where git skipped rename detection the path is unknown, never
|
||||
// absent.
|
||||
func TestCorrespondence(t *testing.T) {
|
||||
corr := func(t *testing.T, f *fixture, n int, limit ...int) (map[string]string, []string) {
|
||||
t.Helper()
|
||||
b := shaped(t, f)
|
||||
if len(limit) > 0 {
|
||||
b.renameLimit = limit[0]
|
||||
}
|
||||
v := b.find(n)
|
||||
if err := b.load(v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, unknown, err := b.correspondence(v, b.lineHead)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return c, unknown
|
||||
}
|
||||
t.Run("renamed on main", func(t *testing.T) {
|
||||
f, _ := renameWorld(t)
|
||||
if c, unknown := corr(t, f, 5); c["app/new.txt"] != "app/old.txt" || len(unknown) != 0 {
|
||||
t.Errorf("correspondence = %v, %v; want app/new.txt on app/old.txt", c, unknown)
|
||||
}
|
||||
})
|
||||
t.Run("renamed on the line", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: an app file", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): the line renames it", map[string]string{"app/a.txt": "", "app/line.txt": appFile})
|
||||
f.pr(5, "fix(app): fix it", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
if c, unknown := corr(t, f, 5); c["app/a.txt"] != "app/line.txt" || len(unknown) != 0 {
|
||||
t.Errorf("correspondence = %v, %v; want app/a.txt on app/line.txt", c, unknown)
|
||||
}
|
||||
})
|
||||
t.Run("renamed on both", func(t *testing.T) {
|
||||
f, _ := renameWorld(t)
|
||||
f.onLine("release-1.1", "fix(app): the line renames it its own way", map[string]string{"app/old.txt": "", "app/release.txt": appFile})
|
||||
if c, unknown := corr(t, f, 5); c["app/new.txt"] != "app/release.txt" || len(unknown) != 0 {
|
||||
t.Errorf("correspondence = %v, %v; want app/new.txt on app/release.txt", c, unknown)
|
||||
}
|
||||
})
|
||||
t.Run("not renamed, absent", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): x", "", map[string]string{"app/x.txt": appFile})
|
||||
f.pr(5, "fix(app): fix x", "", map[string]string{"app/x.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
if c, unknown := corr(t, f, 5); c["app/x.txt"] != "app/x.txt" || len(unknown) != 0 {
|
||||
t.Errorf("correspondence = %v, %v; want app/x.txt on itself", c, unknown)
|
||||
}
|
||||
})
|
||||
t.Run("rename detection skipped", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: two app files", "", map[string]string{"app/a.txt": appFile, "app/b.txt": appFile + "six\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): the line renames both, changed", map[string]string{"app/a.txt": "", "app/b.txt": "", "app/x.txt": appFile + "x\n", "app/y.txt": appFile + "six\ny\n"})
|
||||
f.pr(5, "fix(app): fix a", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
// Past the limit, git says it skipped looking; a configured limit
|
||||
// of its own does not decide for the tool.
|
||||
f.git("config", "diff.renameLimit", "1")
|
||||
if c, unknown := corr(t, f, 5, 0); c["app/a.txt"] != "app/x.txt" || len(unknown) != 0 {
|
||||
t.Errorf("correspondence under diff.renameLimit=1 = %v, %v; want app/a.txt on app/x.txt", c, unknown)
|
||||
}
|
||||
if c, unknown := corr(t, f, 5, 1); !slices.Equal(unknown, []string{"app/a.txt"}) || c["app/a.txt"] != "" {
|
||||
t.Errorf("correspondence past the limit = %v, %v; want app/a.txt unknown", c, unknown)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// What a change leaves without effect: a modified file the line lacks on both
|
||||
// sides of it, a deleted file the line never had. A mode change that only a
|
||||
// person can judge is a diagnostic, not an omission.
|
||||
func TestOmittedEffects(t *testing.T) {
|
||||
omitted := func(t *testing.T, f *fixture, n int, files map[string]string) ([]string, []string) {
|
||||
t.Helper()
|
||||
b := shaped(t, f)
|
||||
after := commitOn(f, b.lineHead, files)
|
||||
om, diag, err := b.omittedEffects(b.find(n), b.lineHead, after)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return om, diag
|
||||
}
|
||||
t.Run("a modified file missing before and after", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): x", "", map[string]string{"app/x.txt": appFile})
|
||||
f.pr(5, "fix(app): fix x and five", "", map[string]string{"app/x.txt": "fixed\n", "app/five.txt": "5\n"})
|
||||
if om, diag := omitted(t, f, 5, map[string]string{"app/five.txt": "5\n"}); !slices.Equal(om, []string{"app/x.txt"}) || len(diag) != 0 {
|
||||
t.Errorf("omitted = %q, %q; want app/x.txt", om, diag)
|
||||
}
|
||||
})
|
||||
t.Run("a deleted file absent before", func(t *testing.T) {
|
||||
f, _ := policyWorld(t)
|
||||
if om, _ := omitted(t, f, 5, map[string]string{"chart/tests/values_test.yaml": "suite: values\n"}); !slices.Equal(om, []string{"chart/templates/policy.yaml", "chart/tests/policy_test.yaml"}) {
|
||||
t.Errorf("omitted = %q; want both policy files", om)
|
||||
}
|
||||
})
|
||||
t.Run("a mode change", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a script", "", map[string]string{"app/run.sh": "#!/bin/sh\n", "app/b.txt": "old\n"})
|
||||
f.lines()
|
||||
f.git("switch", "-q", "-c", "pr-5")
|
||||
if err := os.Chmod(filepath.Join(f.dir, "app/run.sh"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.write("app/b.txt", "fixed\n")
|
||||
f.git("add", "-A")
|
||||
f.git("commit", "-q", "-m", "run it")
|
||||
f.git("switch", "-q", "main")
|
||||
f.git("merge", "-q", "--no-ff", "-m", "fix(app): make the script runnable (#5)", "pr-5")
|
||||
f.git("push", "-q", "origin", "main")
|
||||
om, diag := omitted(t, f, 5, map[string]string{"app/b.txt": "fixed\n"})
|
||||
if len(om) != 0 || len(diag) != 1 || !strings.Contains(diag[0], `the mode or type of "app/run.sh" changes`) {
|
||||
t.Errorf("omitted = %q, %q; want a diagnostic for app/run.sh and nothing omitted", om, diag)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// What git says when it fails names file names as text, never as lines a
|
||||
// terminal or a CI log would take for its own.
|
||||
func TestGitErrorsQuoteFileNames(t *testing.T) {
|
||||
newFixture(t)
|
||||
const evil = "x\n::warning::forged"
|
||||
if _, err := gitTimed("log", "--no-such-option", "--", evil); err == nil || strings.Contains(err.Error(), "\n::warning") {
|
||||
t.Errorf("gitTimed error = %v, want the path quoted", err)
|
||||
}
|
||||
if _, err := treeEntries("no-such-revision", []string{evil}); err == nil || strings.Contains(err.Error(), "\n::warning") {
|
||||
t.Errorf("treeEntries error = %v, want the path quoted", err)
|
||||
}
|
||||
}
|
||||
|
||||
// fixedWorld is the sign-off's two-file case: the base has a and b as old,
|
||||
// #5 fixes both, and the line already has a as onLine says, if anything.
|
||||
func fixedWorld(t *testing.T, a, old, onLine, fixed string) (*fixture, string) {
|
||||
t.Helper()
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a and b", "", map[string]string{a: old, "app/b.txt": "old\n"})
|
||||
f.lines()
|
||||
if onLine != "" {
|
||||
f.onLine("release-1.1", "fix(app): a, on the line", map[string]string{a: onLine})
|
||||
}
|
||||
mc5 := f.pr(5, "fix(app): a and b", "", map[string]string{a: fixed, "app/b.txt": "fixed\n"})
|
||||
return f, mc5
|
||||
}
|
||||
|
||||
// The line can already have what an original does to a file: then a pick
|
||||
// that leaves the file alone carries the original whole. The check-time
|
||||
// pass, the picker, fold and verify all take it, binary files included,
|
||||
// without asking git merge-file what blob equality settles. A different fix
|
||||
// on the line, or a deletion of what the line never had, is still left out.
|
||||
func TestAlreadyAppliedFileIsNotOmitted(t *testing.T) {
|
||||
const textPost = "one\nfixed\nthree\nfour\nfive\n"
|
||||
accepted := func(t *testing.T, a, old, onLine, fixed string) {
|
||||
t.Helper()
|
||||
f, _ := fixedWorld(t, a, old, onLine, fixed)
|
||||
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/5 ") || strings.Contains(stderr, "parked") {
|
||||
t.Errorf("check = %d, want #5 missing and not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
gh := &fakeGH{}
|
||||
head := openBatch(t, f, gh)
|
||||
if got := f.subjects("origin/release-1.1", head); !slices.Equal(got, []string{"fix(app): a and b (#5)"}) {
|
||||
t.Errorf("picked %q, want #5", got)
|
||||
}
|
||||
if code, out, stderr := f.exec(gh, "verify", "--line", "release-1.1", "--head", head); code != 0 {
|
||||
t.Errorf("verify = %d\n%s\n%s", code, out, stderr)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || strings.Contains(stdout, "only part of it applies") {
|
||||
t.Errorf("check --cut = %d, want #5 only on the batch, whole:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f, mc5 := fixedWorld(t, a, old, onLine, fixed)
|
||||
w := emptyBatch(f, &fakeGH{}, "")
|
||||
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/b.txt": "fixed\n"})
|
||||
fold := w.merge(w.head, pick, "fix(app): a and b (#5)\n\n"+batchTrailer+": "+mc5)
|
||||
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 0 {
|
||||
t.Errorf("fold = %d, %q\n%s", code, out, stderr)
|
||||
}
|
||||
}
|
||||
t.Run("the line has the original's file", func(t *testing.T) {
|
||||
accepted(t, "app/a.txt", "old\n", "fixed\n", "fixed\n")
|
||||
})
|
||||
t.Run("the line has the fix beside lines of its own", func(t *testing.T) {
|
||||
accepted(t, "app/a.txt", strings.Replace(textPost, "fixed", "old", 1), textPost+"line only\n", textPost)
|
||||
})
|
||||
t.Run("the line has the original's binary file", func(t *testing.T) {
|
||||
accepted(t, "app/a.bin", "old\x00payload", "fixed\x00payload", "fixed\x00payload")
|
||||
f, _ := fixedWorld(t, "app/a.bin", "old\x00payload", "fixed\x00payload", "fixed\x00payload")
|
||||
b := shaped(t, f)
|
||||
om, diag, err := b.omittedEffects(b.find(5), b.lineHead, commitOn(f, b.lineHead, map[string]string{"app/b.txt": "fixed\n"}))
|
||||
if err != nil || len(om)+len(diag) != 0 || b.mergeFiles != 0 {
|
||||
t.Errorf("omitted = %q, %q, %v, merge-file ran %d times; want nothing, and merge-file never asked", om, diag, err, b.mergeFiles)
|
||||
}
|
||||
})
|
||||
t.Run("an earlier unit of the batch brought the file", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": "old\n", "app/b.txt": "old\n"})
|
||||
f.lines()
|
||||
f.pr(4, "fix(app): a", "", map[string]string{"app/a.txt": "fixed\n"})
|
||||
f.squash(40, "chore: put a back for now", "", map[string]string{"app/a.txt": "old\n"})
|
||||
f.pr(5, "fix(app): a and b", "", map[string]string{"app/a.txt": "fixed\n", "app/b.txt": "fixed\n"})
|
||||
gh := &fakeGH{}
|
||||
head := openBatch(t, f, gh)
|
||||
if got := f.subjects("origin/release-1.1", head); !slices.Equal(got, []string{"fix(app): a (#4)", "fix(app): a and b (#5)"}) {
|
||||
t.Errorf("picked %q, want #4 and #5", got)
|
||||
}
|
||||
if code, out, stderr := f.exec(gh, "verify", "--line", "release-1.1", "--head", head); code != 0 {
|
||||
t.Errorf("verify = %d\n%s\n%s", code, out, stderr)
|
||||
}
|
||||
})
|
||||
t.Run("the line has a different fix", func(t *testing.T) {
|
||||
f, mc5 := fixedWorld(t, "app/a.txt", "old\n", "other\n", "fixed\n")
|
||||
w := emptyBatch(f, &fakeGH{}, "")
|
||||
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/b.txt": "fixed\n"})
|
||||
fold := w.merge(w.head, pick, "fix(app): a and b (#5)\n\n"+batchTrailer+": "+mc5)
|
||||
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 1 || !strings.Contains(out, `leaves "app/a.txt" without effect`) {
|
||||
t.Errorf("fold = %d, %q, want it refused for app/a.txt\n%s", code, out, stderr)
|
||||
}
|
||||
})
|
||||
t.Run("the line has a different binary file", func(t *testing.T) {
|
||||
f, _ := fixedWorld(t, "app/a.bin", "old\x00payload", "other\x00payload", "fixed\x00payload")
|
||||
b := shaped(t, f)
|
||||
om, diag, err := b.omittedEffects(b.find(5), b.lineHead, commitOn(f, b.lineHead, map[string]string{"app/b.txt": "fixed\n"}))
|
||||
if err != nil || len(om) != 0 || len(diag) != 1 || !strings.Contains(diag[0], `cannot merge "app/a.bin" as text`) {
|
||||
t.Errorf("omitted = %q, %q, %v; want a diagnostic for app/a.bin, nothing omitted", om, diag, err)
|
||||
}
|
||||
})
|
||||
t.Run("a deletion of what the line never had", func(t *testing.T) {
|
||||
f, mc5 := policyWorld(t)
|
||||
w := emptyBatch(f, &fakeGH{}, "")
|
||||
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"chart/tests/values_test.yaml": "suite: values\n"})
|
||||
fold := w.merge(w.head, pick, "fix(chart): remove the policy a rebase brought back (#5)\n\n"+batchTrailer+": "+mc5)
|
||||
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 1 || !strings.Contains(out, "without effect") {
|
||||
t.Errorf("fold = %d, %q, want it refused\n%s", code, out, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A change that makes none, whatever names its two sides, leaves a deletion
|
||||
// the line already made as it is: the original is there, not partial.
|
||||
func TestDeletionAlreadyMadeByNoChange(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): the line drops the policy", map[string]string{"app/policy.yaml": ""})
|
||||
f.pr(5, "fix(app): drop the policy", "", map[string]string{"app/policy.yaml": ""})
|
||||
b := shaped(t, f)
|
||||
tree := f.git("rev-parse", b.lineHead+"^{tree}")
|
||||
for _, after := range []string{b.lineHead, tree} {
|
||||
if om, _, err := b.omittedEffects(b.find(5), b.lineHead, after); err != nil || len(om) != 0 {
|
||||
t.Errorf("omitted against %.12s = %q, %v; want nothing", after, om, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ import (
|
||||
// main and release lines shaped the way GitHub leaves them: every pull request
|
||||
// lands as a merge commit whose message is its title, its number and its body.
|
||||
type fixture struct {
|
||||
t *testing.T
|
||||
t testing.TB
|
||||
dir string
|
||||
remote string
|
||||
now time.Time
|
||||
@@ -41,7 +41,7 @@ type fixture struct {
|
||||
tool string
|
||||
}
|
||||
|
||||
func newFixture(t *testing.T) *fixture {
|
||||
func newFixture(t testing.TB) *fixture {
|
||||
t.Helper()
|
||||
// A hook or an outer repository can export these into the test process.
|
||||
for _, key := range []string{"GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_COMMON_DIR", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES"} {
|
||||
@@ -458,6 +458,22 @@ func (g *fakeGH) editPull(number int, title, bodyFile string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *fakeGH) editBody(number int, bodyFile string) error {
|
||||
if err := g.write(number, "a body edit"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.inject("editBody"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.readBody(bodyFile); err != nil {
|
||||
return err
|
||||
}
|
||||
if pr := g.find(number); pr != nil {
|
||||
pr.Body = g.bodies[len(g.bodies)-1]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *fakeGH) closePull(number int) error {
|
||||
if err := g.write(number, "a close"); err != nil {
|
||||
return err
|
||||
@@ -506,15 +522,6 @@ func (f *fixture) batchPR(n int, base, branch, body string) pullRequest {
|
||||
return pr
|
||||
}
|
||||
|
||||
// botBatchPR is batchPR opened by the bot the scheduled batch runs as.
|
||||
func (f *fixture) botBatchPR(gh *fakeGH, n int, base, branch, body string) pullRequest {
|
||||
f.t.Helper()
|
||||
gh.bot = defaultBatchBot
|
||||
pr := f.batchPR(n, base, branch, body)
|
||||
pr.User.Login, pr.User.Type, pr.AuthorAssociation = defaultBatchBot, "Bot", "CONTRIBUTOR"
|
||||
return pr
|
||||
}
|
||||
|
||||
// mergeBatch merges the open batch into release-1.1 with a merge commit, as
|
||||
// GitHub does, and deletes its branch, as the repository's
|
||||
// delete_branch_on_merge does. The pull request stays, merged, with its body
|
||||
|
||||
@@ -348,3 +348,75 @@ func TestFoldRefusesADroppedOriginal(t *testing.T) {
|
||||
t.Errorf("fold = %d, %q, %s; want a refusal naming the drop", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A resolution that ports what the line has of #5 and leaves alone a file
|
||||
// the line lacks is no fold: what #5 does to that file would be lost, and a
|
||||
// line-only port is a pull request of its own.
|
||||
func TestFoldRefusesAnOmittedEffect(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): a feature the line never gets", "", map[string]string{"app/feature2.txt": "v1\n"})
|
||||
mc5 := f.pr(5, "fix(app): five", "", map[string]string{"app/feature2.txt": "v2\n", "app/five.txt": "5\n"})
|
||||
w := emptyBatch(f, &fakeGH{}, "")
|
||||
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/five.txt": "5\n"})
|
||||
fold := w.merge(w.head, pick, "fix(app): five (#5)\n\n"+batchTrailer+": "+mc5)
|
||||
code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold)
|
||||
if code != 1 || out != "refused: #5 leaves \"app/feature2.txt\" without effect on release-1.1; port only what the line has as a pull request of its own\n" {
|
||||
t.Errorf("fold = %d, %q, want it refused for app/feature2.txt\n%s", code, out, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A resolution that keeps a file the original deletes leaves the deletion
|
||||
// without effect.
|
||||
func TestFoldRefusesAKeptDeletion(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: x", "", map[string]string{"app/x.txt": "x\n"})
|
||||
f.lines()
|
||||
mc5 := f.pr(5, "fix(app): five, and x goes", "", map[string]string{"app/x.txt": "", "app/five.txt": "5\n"})
|
||||
w := emptyBatch(f, &fakeGH{}, "")
|
||||
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/five.txt": "5\n"})
|
||||
fold := w.merge(w.head, pick, "fix(app): five, and x goes (#5)\n\n"+batchTrailer+": "+mc5)
|
||||
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 1 || !strings.Contains(out, `leaves "app/x.txt" without effect`) {
|
||||
t.Errorf("fold that keeps app/x.txt = %d, %q\n%s", code, out, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// An original parked for what it builds on can still land as a complete
|
||||
// hand adaptation: every file it changes, changed, in the batch's shape.
|
||||
func TestFoldAcceptsACompleteAdaptationOfAParkedOriginal(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
mc5 := f.pr(5, "fix(app): an excited two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
w := emptyBatch(f, &fakeGH{}, "")
|
||||
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/a.txt": strings.Replace(appFile, "two", "two!", 1)})
|
||||
fold := w.merge(w.head, pick, "fix(app): an excited two (#5)\n\n"+batchTrailer+": "+mc5)
|
||||
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 0 || out != "ok\n" {
|
||||
t.Errorf("fold of an adaptation of #5 = %d, %q\n%s", code, out, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A fold that keeps a file the original deletes leaves the deletion without
|
||||
// effect, whatever it changes in the file: a port that keeps what the line
|
||||
// made of it, or the file with a line added, is a pull request of its own.
|
||||
func TestFoldRefusesAChangedKeptDeletion(t *testing.T) {
|
||||
for name, kept := range map[string]string{
|
||||
"the line's own content": "line only\n",
|
||||
"the file, one line more": "feature\nline only\n# reviewed\n",
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a feature", "", map[string]string{"app/x.txt": "feature\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): a line-only setting beside the feature", map[string]string{"app/x.txt": "feature\nline only\n"})
|
||||
mc5 := f.pr(5, "fix(app): drop the feature, and five", "", map[string]string{"app/x.txt": "", "app/five.txt": "5\n"})
|
||||
w := emptyBatch(f, &fakeGH{}, "")
|
||||
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/x.txt": kept, "app/five.txt": "5\n"})
|
||||
fold := w.merge(w.head, pick, "fix(app): drop the feature, and five (#5)\n\n"+batchTrailer+": "+mc5)
|
||||
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 1 || !strings.Contains(out, `leaves "app/x.txt" without effect`) {
|
||||
t.Errorf("fold = %d, %q, want it refused\n%s", code, out, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -127,6 +127,8 @@ type github interface {
|
||||
// createPull opens the draft and returns its number and URL.
|
||||
createPull(base, head, title, bodyFile string) (int, string, error)
|
||||
editPull(number int, title, bodyFile string) error
|
||||
// editBody replaces the body of pull request number and nothing else.
|
||||
editBody(number int, bodyFile string) error
|
||||
closePull(number int) error
|
||||
comments(number int) ([]issueComment, error)
|
||||
createComment(number int, bodyFile string) error
|
||||
@@ -135,6 +137,54 @@ type github interface {
|
||||
batchBot() string
|
||||
}
|
||||
|
||||
// guardedGitHub refuses every GitHub write once the run's output was lost.
|
||||
type guardedGitHub struct {
|
||||
github
|
||||
a *app
|
||||
}
|
||||
|
||||
func (g guardedGitHub) createPull(base, head, title, bodyFile string) (int, string, error) {
|
||||
if err := g.a.mayAct(); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
return g.github.createPull(base, head, title, bodyFile)
|
||||
}
|
||||
|
||||
func (g guardedGitHub) editPull(number int, title, bodyFile string) error {
|
||||
if err := g.a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.github.editPull(number, title, bodyFile)
|
||||
}
|
||||
|
||||
func (g guardedGitHub) editBody(number int, bodyFile string) error {
|
||||
if err := g.a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.github.editBody(number, bodyFile)
|
||||
}
|
||||
|
||||
func (g guardedGitHub) closePull(number int) error {
|
||||
if err := g.a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.github.closePull(number)
|
||||
}
|
||||
|
||||
func (g guardedGitHub) createComment(number int, bodyFile string) error {
|
||||
if err := g.a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.github.createComment(number, bodyFile)
|
||||
}
|
||||
|
||||
func (g guardedGitHub) editComment(id int64, bodyFile string) error {
|
||||
if err := g.a.mayAct(); err != nil {
|
||||
return err
|
||||
}
|
||||
return g.github.editComment(id, bodyFile)
|
||||
}
|
||||
|
||||
type ghCLI struct{ bot string }
|
||||
|
||||
func (g ghCLI) batchBot() string { return g.bot }
|
||||
@@ -260,6 +310,11 @@ func (ghCLI) editPull(number int, title, bodyFile string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (ghCLI) editBody(number int, bodyFile string) error {
|
||||
_, err := capture("gh", "pr", "edit", strconv.Itoa(number), "--body-file", bodyFile)
|
||||
return err
|
||||
}
|
||||
|
||||
func (ghCLI) closePull(number int) error {
|
||||
_, err := capture("gh", "pr", "close", strconv.Itoa(number))
|
||||
return err
|
||||
|
||||
@@ -205,6 +205,9 @@ type history struct {
|
||||
picks []pickRef
|
||||
batches map[string]string
|
||||
backports map[int]string
|
||||
// trees caches what a net merge of an original onto head writes, by its
|
||||
// merge commit: "" for a conflict.
|
||||
trees map[string]string
|
||||
}
|
||||
|
||||
func readHistory(head, base string, textUntil time.Time) (*history, error) {
|
||||
@@ -225,7 +228,7 @@ func readHistory(head, base string, textUntil time.Time) (*history, error) {
|
||||
// readPicks reads only the messages of head's commits outside base, which is
|
||||
// all a batch branch is asked about; textUntil is the batch-only day.
|
||||
func readPicks(head, base string, textUntil time.Time) (*history, error) {
|
||||
h := &history{head: head, reachable: map[string]bool{}, appended: map[string]string{}, batches: map[string]string{}, backports: map[int]string{}}
|
||||
h := &history{head: head, reachable: map[string]bool{}, appended: map[string]string{}, batches: map[string]string{}, backports: map[int]string{}, trees: map[string]string{}}
|
||||
oids, err := revList("--end-of-options", head, "^"+base)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -332,34 +335,54 @@ func (h *history) carries(o *original, commits []string) (string, error) {
|
||||
if c, ok := h.backports[o.number]; ok {
|
||||
return `named by "Backport of" in ` + c[:12], nil
|
||||
}
|
||||
noop, err := noopPick(h.head, o)
|
||||
tree, err := h.netTree(o)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if noop {
|
||||
headTree, err := resolveTree(h.head)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if tree == headTree {
|
||||
return "a pick would change nothing", nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// noopPick reports whether cherry-picking the original onto head would leave
|
||||
// head's tree as it is: the content is there, whatever names it.
|
||||
func noopPick(head string, o *original) (bool, error) {
|
||||
// netTree is noopPick's tree for o onto the branch's head, read once.
|
||||
func (h *history) netTree(o *original) (string, error) {
|
||||
if tree, ok := h.trees[o.merge]; ok {
|
||||
return tree, nil
|
||||
}
|
||||
tree, _, err := noopPick(h.head, o)
|
||||
if err == nil {
|
||||
h.trees[o.merge] = tree
|
||||
}
|
||||
return tree, err
|
||||
}
|
||||
|
||||
// noopPick merges the original onto head as one change and returns the tree
|
||||
// that writes for a clean merge, "" for a conflict, and whether the tree is
|
||||
// head's own: the content is there, whatever names it.
|
||||
func noopPick(head string, o *original) (string, bool, error) {
|
||||
stdout, stderr, code, err := gitStatus("merge-tree", "--write-tree", "--no-messages", "--merge-base="+o.merge+"^1", "--end-of-options", head, o.merge)
|
||||
switch {
|
||||
case err != nil:
|
||||
return false, err
|
||||
return "", false, err
|
||||
case code == 1:
|
||||
return false, nil
|
||||
return "", false, nil
|
||||
case code != 0:
|
||||
return false, fmt.Errorf("git merge-tree for #%d: exit %d: %s", o.number, code, strings.TrimSpace(stderr))
|
||||
return "", false, fmt.Errorf("git merge-tree for #%d: exit %d: %s", o.number, code, strings.TrimSpace(stderr))
|
||||
}
|
||||
tree, _, _ := strings.Cut(stdout, "\n")
|
||||
if err := checkSHA(tree); err != nil {
|
||||
return "", false, fmt.Errorf("git merge-tree for #%d: %w", o.number, err)
|
||||
}
|
||||
headTree, err := resolveTree(head)
|
||||
if err != nil {
|
||||
return false, err
|
||||
return "", false, err
|
||||
}
|
||||
return tree == headTree, nil
|
||||
return tree, tree == headTree, nil
|
||||
}
|
||||
|
||||
func resolveTree(commit string) (string, error) {
|
||||
|
||||
@@ -142,11 +142,11 @@ func TestOptionInjection(t *testing.T) {
|
||||
// no bidi override reorders it and no zero-width character hides in it.
|
||||
func TestDeferredBlockCleansFileNames(t *testing.T) {
|
||||
l, _ := parseLine("release-1.6")
|
||||
files := []string{"app/txt.exe", "app/zerowidth.yaml"}
|
||||
files := []string{"app/\u202etxt.exe", "app/zero\u200bwidth.yaml"}
|
||||
conflict := pick{v: &verdict{o: &original{number: 5}}, files: files, failed: strings.Repeat("a", 40)}
|
||||
waiting := pick{v: &verdict{o: &original{number: 6}}, files: files}
|
||||
block := deferredBlock(l, []pick{conflict}, nil, []pick{waiting})
|
||||
if strings.ContainsAny(block, "") {
|
||||
if strings.ContainsAny(block, "\u202e\u200b") {
|
||||
t.Errorf("the block carries format characters: %q", block)
|
||||
}
|
||||
want := []string{"app/txt.exe", "app/zerowidth.yaml"}
|
||||
@@ -154,7 +154,7 @@ func TestDeferredBlockCleansFileNames(t *testing.T) {
|
||||
if err != nil || !slices.Equal(got[5].Files, want) || !slices.Equal(got[6].Files, want) {
|
||||
t.Errorf("parseDeferred = %+v, %v; want both originals with %q", got, err, want)
|
||||
}
|
||||
if files[0] != "app/txt.exe" {
|
||||
if files[0] != "app/\u202etxt.exe" {
|
||||
t.Errorf("deferredBlock rewrote the pick's own file names: %q", files)
|
||||
}
|
||||
}
|
||||
|
||||
+92
-10
@@ -24,7 +24,9 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -111,18 +113,42 @@ type app struct {
|
||||
policyRef string
|
||||
// tool is the commit backport-batch was built from, for the record.
|
||||
tool string
|
||||
// unwritten is the first error writing to stdout or stderr. Output goes
|
||||
// out before a push or a publish, so a run that lost it acts on nothing,
|
||||
// and one that lost a confirmation afterwards fails.
|
||||
unwritten error
|
||||
}
|
||||
|
||||
func main() {
|
||||
catchSIGPIPE()
|
||||
a := &app{gh: ghCLI{bot: batchBotLogin(os.LookupEnv)}, stdout: os.Stdout, stderr: os.Stderr, now: time.Now, tool: toolVersion()}
|
||||
os.Exit(a.main(os.Args[1:]))
|
||||
}
|
||||
|
||||
// catchSIGPIPE makes a write to a closed stdout or stderr fail with EPIPE like
|
||||
// any other write, so the run stops before it pushes or publishes and says
|
||||
// why, instead of dying silently. signal.Ignore would do the same here but
|
||||
// would also leave SIGPIPE ignored in the git and gh children, across exec.
|
||||
func catchSIGPIPE() {
|
||||
signal.Notify(make(chan os.Signal, 1), syscall.SIGPIPE)
|
||||
}
|
||||
|
||||
func (a *app) main(args []string) int {
|
||||
a.stdout, a.stderr = textWriter{a.stdout}, textWriter{a.stderr}
|
||||
a.gh = &permCache{github: a.gh, perms: map[string]string{}}
|
||||
a.stdout, a.stderr = textWriter{w: a.stdout, failed: &a.unwritten}, textWriter{w: a.stderr, failed: &a.unwritten}
|
||||
code := a.command(args)
|
||||
if a.unwritten != nil {
|
||||
_, _ = fmt.Fprintf(a.stderr, "backport-batch: output was lost: %v\n", a.unwritten)
|
||||
if code == 0 {
|
||||
code = 2
|
||||
}
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
func (a *app) command(args []string) int {
|
||||
a.gh = guardedGitHub{github: &permCache{github: a.gh, perms: map[string]string{}}, a: a}
|
||||
if len(args) == 0 {
|
||||
fmt.Fprint(a.stderr, usageText)
|
||||
_, _ = fmt.Fprint(a.stderr, usageText)
|
||||
return 2
|
||||
}
|
||||
if bot := a.gh.batchBot(); bot != "" && !botLoginRE.MatchString(bot) {
|
||||
@@ -144,20 +170,26 @@ func (a *app) main(args []string) int {
|
||||
case "lines":
|
||||
return a.lines(args[1:])
|
||||
case "-h", "--help", "help":
|
||||
fmt.Fprint(a.stdout, usageText)
|
||||
_, _ = fmt.Fprint(a.stdout, usageText)
|
||||
return 0
|
||||
}
|
||||
fmt.Fprintf(a.stderr, "unknown command %q\n\n%s", args[0], usageText)
|
||||
_, _ = fmt.Fprintf(a.stderr, "unknown command %q\n\n%s", args[0], usageText)
|
||||
return 2
|
||||
}
|
||||
|
||||
// textWriter keeps output fit for a terminal. Titles, file names and the
|
||||
// messages git relays come from other people, and a control sequence among
|
||||
// them would move the cursor or rewrite what was printed before.
|
||||
type textWriter struct{ w io.Writer }
|
||||
type textWriter struct {
|
||||
w io.Writer
|
||||
failed *error
|
||||
}
|
||||
|
||||
func (t textWriter) Write(p []byte) (int, error) {
|
||||
if _, err := io.WriteString(t.w, clean(string(p), true)); err != nil {
|
||||
if t.failed != nil && *t.failed == nil {
|
||||
*t.failed = err
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
return len(p), nil
|
||||
@@ -166,7 +198,7 @@ func (t textWriter) Write(p []byte) (int, error) {
|
||||
func (a *app) flags(name string) *flag.FlagSet {
|
||||
fs := flag.NewFlagSet(name, flag.ContinueOnError)
|
||||
fs.SetOutput(a.stderr)
|
||||
fs.Usage = func() { fmt.Fprint(a.stderr, usageText) }
|
||||
fs.Usage = func() { _, _ = fmt.Fprint(a.stderr, usageText) }
|
||||
return fs
|
||||
}
|
||||
|
||||
@@ -188,13 +220,24 @@ func parse(fs *flag.FlagSet, args []string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// mayAct refuses any further change once output was lost: the run would act
|
||||
// on what it printed while nobody read it. Every push and every move of a
|
||||
// local batch branch go through it, and every GitHub write through
|
||||
// guardedGitHub.
|
||||
func (a *app) mayAct() error {
|
||||
if a.unwritten != nil {
|
||||
return fmt.Errorf("output was lost: %w; nothing more is pushed or written to GitHub", a.unwritten)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *app) fail(err error) int {
|
||||
fmt.Fprintf(a.stderr, "backport-batch: %v\n", err)
|
||||
_, _ = fmt.Fprintf(a.stderr, "backport-batch: %v\n", err)
|
||||
return 2
|
||||
}
|
||||
|
||||
func (a *app) warn(format string, args ...any) {
|
||||
fmt.Fprintf(a.stderr, "warning: "+format+"\n", args...)
|
||||
_, _ = fmt.Fprintf(a.stderr, "warning: "+format+"\n", args...)
|
||||
}
|
||||
|
||||
// verdict is what the policy says about one original for the line.
|
||||
@@ -207,10 +250,22 @@ type verdict struct {
|
||||
// and by load for anything else.
|
||||
commits []string
|
||||
changes []fileChange
|
||||
// onBatch is the commit of the open batch pull request that picks it.
|
||||
// onBatch is the commit of the open batch pull request that picks it,
|
||||
// and batched the unit of the open batch that carries it whatever the
|
||||
// rules now say of it.
|
||||
onBatch string
|
||||
batched string
|
||||
// label is the maintainer override the original carries, if any.
|
||||
label string
|
||||
// partial lists what its clean net merge onto the line would leave
|
||||
// without effect, and park why the batch leaves it out, if it does.
|
||||
partial []string
|
||||
park *parkedEntry
|
||||
// deps is what it builds on that the line lacks.
|
||||
deps []dependency
|
||||
// diag is what could not be judged about it, shown whatever became of
|
||||
// it.
|
||||
diag []string
|
||||
}
|
||||
|
||||
// batch is everything read about a line before anything is written.
|
||||
@@ -242,6 +297,27 @@ type batch struct {
|
||||
// textUntil is the batch-only day: what a commit's text says counts as
|
||||
// evidence only on commits from before it.
|
||||
textUntil time.Time
|
||||
// partialUnits are the originals whose unit on the open batch leaves
|
||||
// what it lists without effect.
|
||||
partialUnits map[*verdict][]string
|
||||
// What one command read once and asks again: renames by commit pair,
|
||||
// units by batch head, and how often git merge-file ran.
|
||||
renameCache map[string]renameSet
|
||||
unitCache map[string]map[*verdict]string
|
||||
mergeFiles int
|
||||
// renameLimit is the -l rename detection runs with; 0, git's widest.
|
||||
renameLimit int
|
||||
// brought maps main's commits since the fork point to the first-parent
|
||||
// commit that brought each, and ancestors says which commits blame
|
||||
// named are older than the fork point.
|
||||
brought map[string]string
|
||||
ancestors map[string]bool
|
||||
// detecting is how long judging partial units and dependencies took.
|
||||
detecting time.Duration
|
||||
depCache map[string][]dependency
|
||||
// lineCarries says, for a prerequisite the rules keep out, whether the
|
||||
// line carries it all the same.
|
||||
lineCarries map[*verdict]bool
|
||||
}
|
||||
|
||||
// fetch moves to the top of the working tree first: every path the batch
|
||||
@@ -348,6 +424,9 @@ func (a *app) prepare(l line) (*batch, error) {
|
||||
if b.drops, err = b.bindDrops(merges); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := b.waitOrder(merges); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, o := range merges {
|
||||
v := &verdict{o: o, label: labels[o.number]}
|
||||
if err := b.judge(v, b.drops); err != nil {
|
||||
@@ -355,6 +434,9 @@ func (a *app) prepare(l line) (*batch, error) {
|
||||
}
|
||||
b.verdicts = append(b.verdicts, v)
|
||||
}
|
||||
if err := b.partialStates(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// releaseNoteBlock is the release-note block of a body.
|
||||
func releaseNoteBlock(t *testing.T, body string) string {
|
||||
t.Helper()
|
||||
m := regexp.MustCompile("(?s)```release-note\n(.*?)\n```").FindStringSubmatch(body)
|
||||
if m == nil {
|
||||
t.Fatalf("no release-note block in:\n%s", body)
|
||||
}
|
||||
return m[1]
|
||||
}
|
||||
|
||||
// A pick that differs from its original keeps its release note out of the
|
||||
// release-note block, under Check these notes with what to compare: one git
|
||||
// had to fit to a line that already had part of the change, and one whose
|
||||
// first commit came out empty because the line had resized the value itself.
|
||||
func TestChangedOriginalNoteIsSetApart(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): two, on the line", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.onLine("release-1.1", "fix(chart): two replicas, on the line", map[string]string{"chart/values.yaml": "replicas: 2\nimage: app:1\n"})
|
||||
f.pr(5, "fix(app): two and four", "```release-note\nfix(app): two and four are loud.\n```\n", map[string]string{"app/a.txt": strings.NewReplacer("two", "TWO", "four", "FOUR").Replace(appFile)})
|
||||
f.prCommits(6, "fix(chart): two replicas and six", "```release-note\nfix(chart): two replicas by default.\n```\n",
|
||||
change{files: map[string]string{"chart/values.yaml": "replicas: 2\nimage: app:1\n"}},
|
||||
change{files: map[string]string{"app/six.txt": "6\n"}})
|
||||
f.pr(7, "fix(app): seven", "```release-note\nfix(app): seven is calm.\n```\n", map[string]string{"app/seven.txt": "7\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): two and four (#5)", "fix(chart): two replicas and six (#6)", "fix(app): seven (#7)"}) {
|
||||
t.Fatalf("picked %q, want #5, #6 and #7", got)
|
||||
}
|
||||
notes := releaseNoteBlock(t, stdout)
|
||||
if !strings.Contains(notes, "seven is calm") || strings.Contains(notes, "two and four are loud") || strings.Contains(notes, "two replicas by default") {
|
||||
t.Errorf("the release-note block keeps a changed pick's note, or loses #7's:\n%s", notes)
|
||||
}
|
||||
_, check, ok := strings.Cut(stdout, "### Check these notes")
|
||||
check, _, _ = strings.Cut(check, "\n#")
|
||||
if !ok || !strings.Contains(check, "two and four are loud") || !strings.Contains(check, "two replicas by default") || strings.Contains(check, "seven is calm") {
|
||||
t.Errorf("Check these notes does not list #5 and #6 alone:\n%s", stdout)
|
||||
}
|
||||
if !strings.Contains(stdout, `"changed":[5,6]`) {
|
||||
t.Errorf("the record or the report does not mark #5 and #6 changed:\n%s", stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// The command printed for a changed pick pairs the pick with the commit it
|
||||
// came from, however much the pick lost: the default creation factor leaves
|
||||
// a pick that carries only #5088's test unpaired.
|
||||
func TestRangeDiffCommandPairsThePick(t *testing.T) {
|
||||
f, mc5 := policyWorld(t)
|
||||
gh := &fakeGH{}
|
||||
head := handBatch(f, gh, mc5)
|
||||
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue")
|
||||
if code != 0 {
|
||||
t.Fatalf("run --continue = %d\n%s", code, stderr)
|
||||
}
|
||||
m := regexp.MustCompile("`git range-diff --creation-factor=999 ([0-9a-f]{12})\\^! ([0-9a-f]{12})\\^!`").FindStringSubmatch(stdout)
|
||||
if m == nil {
|
||||
t.Fatalf("the body prints no range-diff for #5's pick:\n%s", stdout)
|
||||
}
|
||||
src, pick := f.git("rev-parse", mc5+"^2"), f.git("rev-parse", head+"^2")
|
||||
if !strings.HasPrefix(src, m[1]) || !strings.HasPrefix(pick, m[2]) {
|
||||
t.Errorf("the range-diff names %s and %s, want #5's commit %.12s and its pick %.12s", m[1], m[2], src, pick)
|
||||
}
|
||||
out, err := exec.Command("git", "range-diff", "--no-color", "--creation-factor=999", m[1]+"^!", m[2]+"^!").Output()
|
||||
if want := "1: " + f.git("rev-parse", "--short", src) + " ! 1: " + f.git("rev-parse", "--short", pick); err != nil || !strings.HasPrefix(string(out), want) {
|
||||
t.Errorf("the printed range-diff gives %q, %v; want it to start %q", out, err, want)
|
||||
}
|
||||
if out := f.git("range-diff", "--no-color", mc5+"^1.."+mc5+"^2", head+"^1.."+head+"^2"); !strings.Contains(out, " < ") {
|
||||
t.Errorf("the default creation factor pairs #5's pick after all, so the test shows nothing:\n%s", out)
|
||||
}
|
||||
}
|
||||
+151
-16
@@ -87,51 +87,186 @@ func (a *app) notify(args []string) int {
|
||||
return a.fail(err)
|
||||
}
|
||||
deferred := map[int]deferredEntry{}
|
||||
var partial []int
|
||||
var parks []parkedEntry
|
||||
if src.open {
|
||||
deferred = src.rec.deferred()
|
||||
deferred, partial = src.rec.deferred(), src.rec.Partial
|
||||
for _, p := range src.rec.Parked {
|
||||
if !p.Announced {
|
||||
parks = append(parks, p)
|
||||
}
|
||||
}
|
||||
}
|
||||
// An original that waits on others is listed once, after what decides
|
||||
// its wait, where that one is listed.
|
||||
var conflicts, waiting []int
|
||||
after := map[int][]int{}
|
||||
for n, e := range deferred {
|
||||
switch {
|
||||
case e.Workflows:
|
||||
switch placement(deferred, n, map[int]bool{}) {
|
||||
case "workflow":
|
||||
waiting = append(waiting, n)
|
||||
case e.Reason == "":
|
||||
case "conflict":
|
||||
conflicts = append(conflicts, n)
|
||||
}
|
||||
if len(e.Needs) > 0 {
|
||||
after[n] = e.Needs
|
||||
}
|
||||
}
|
||||
slices.Sort(conflicts)
|
||||
slices.Sort(waiting)
|
||||
item := func(n int) string {
|
||||
if needs := after[n]; len(needs) > 0 {
|
||||
return fmt.Sprintf("\n- %s, after %s", prLink(n), prLinks(needs))
|
||||
}
|
||||
return "\n- " + prLink(n)
|
||||
}
|
||||
count := func(ns []int) int {
|
||||
c := 0
|
||||
for _, n := range ns {
|
||||
if len(after[n]) == 0 {
|
||||
c++
|
||||
}
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
today := a.now().UTC().Truncate(24 * time.Hour)
|
||||
days := int(today.Sub(opened).Hours() / 24)
|
||||
stalled := days >= stalledAfter
|
||||
var text []string
|
||||
if stalled {
|
||||
fmt.Fprintf(a.stdout, "%s: batch pull request #%d has been open since %s, %d days\n", l.branch(), pr.Number, opened.Format(time.DateOnly), days)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: batch pull request #%d has been open since %s, %d days\n", l.branch(), pr.Number, opened.Format(time.DateOnly), days)
|
||||
text = append(text, fmt.Sprintf("This batch has been open since %s, %d days, through at least one scheduled run. Merge it, or close it and the next run starts a new one; until then each run adds what the rules take to it.", opened.Format(time.DateOnly), days))
|
||||
}
|
||||
if len(conflicts) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %d deferred conflicts on #%d wait for a maintainer\n", l.branch(), len(conflicts), pr.Number)
|
||||
if len(partial) > 0 {
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d units on #%d carry only part of their originals\n", l.branch(), len(partial), pr.Number)
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "These conflicts wait for a maintainer, as [resolving a conflict](%sresolve-conflict.md) describes; unresolved, each goes to the next batch:\n", tasksURL)
|
||||
b.WriteString("These are on the batch but carry only part of their originals on this line; take each out with `backport-batch run --defer` before the cut, and port what applies as a pull request of its own:\n")
|
||||
for _, n := range partial {
|
||||
b.WriteString(item(n))
|
||||
}
|
||||
text = append(text, b.String())
|
||||
}
|
||||
if len(conflicts) > 0 {
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d deferred conflicts on #%d wait for a maintainer\n", l.branch(), count(conflicts), pr.Number)
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "These conflicts wait for a maintainer, as [resolving a conflict](%sresolve-conflict.md) describes; unresolved, each goes to the next batch, and so does what waits on one:\n", tasksURL)
|
||||
for _, n := range conflicts {
|
||||
fmt.Fprintf(&b, "\n- %s", prLink(n))
|
||||
b.WriteString(item(n))
|
||||
}
|
||||
text = append(text, b.String())
|
||||
}
|
||||
if len(waiting) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %d originals on #%d change %s and wait for a maintainer\n", l.branch(), len(waiting), pr.Number, workflowsDir)
|
||||
if count(waiting) == len(waiting) {
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d originals on #%d change %s and wait for a maintainer\n", l.branch(), len(waiting), pr.Number, workflowsDir)
|
||||
} else {
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d originals on #%d change %s or wait on one that does\n", l.branch(), len(waiting), pr.Number, workflowsDir)
|
||||
}
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "These change `%s`, which the batch's token may not push, and wait for a maintainer to bring them over, as [changes to workflows](%s#changes-to-workflows) describes:\n", workflowsDir, policyURL)
|
||||
fmt.Fprintf(&b, "These change `%s`, which the batch's token may not push, and wait for a maintainer to bring them over, as [changes to workflows](%s#changes-to-workflows) describes, or wait on one that does:\n", workflowsDir, policyURL)
|
||||
for _, n := range waiting {
|
||||
fmt.Fprintf(&b, "\n- %s", prLink(n))
|
||||
b.WriteString(item(n))
|
||||
}
|
||||
text = append(text, b.String())
|
||||
}
|
||||
if len(parks) > 0 {
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d parked originals on #%d, named once\n", l.branch(), len(parks), pr.Number)
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "These cannot land on this line whole, so the batch parked them; each is named here once. A maintainer ports what applies as a pull request of its own, as [fixes that exist only on a release line](%s#fixes-that-exist-only-on-a-release-line) describes, or drops the original with `backport-batch run --drop`:\n", policyURL)
|
||||
for _, p := range parks {
|
||||
fmt.Fprintf(&b, "\n- %s: %s", prLink(p.Original), parkReason(p))
|
||||
}
|
||||
text = append(text, b.String())
|
||||
}
|
||||
if !*push {
|
||||
return 0
|
||||
}
|
||||
return a.comment(pr, *mention, today, text)
|
||||
if code := a.comment(pr, *mention, today, text); code != 0 || len(parks) == 0 {
|
||||
return code
|
||||
}
|
||||
if err := a.announced(pr, src.rec, parks); err != nil {
|
||||
return a.fail(fmt.Errorf("%w; the next notify names them again", err))
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// announced marks parks as named in the record of the open batch pull
|
||||
// request, which the comment just named: only while it is open and its body
|
||||
// still carries the record notify read, so a run that rewrote the body
|
||||
// meanwhile loses nothing; the next notify names those parks again.
|
||||
func (a *app) announced(pr *pullRequest, rec *lineRecord, parks []parkedEntry) error {
|
||||
marked := *rec
|
||||
marked.Parked = slices.Clone(rec.Parked)
|
||||
for i, p := range marked.Parked {
|
||||
if slices.ContainsFunc(parks, func(q parkedEntry) bool { return q.Original == p.Original && parkEvent(q) == parkEvent(p) }) {
|
||||
marked.Parked[i].Announced = true
|
||||
}
|
||||
}
|
||||
now, err := a.gh.pull(pr.Number)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if st := prState(&now); st != "open" {
|
||||
return fmt.Errorf("batch pull request #%d is %s now; nothing is written to it", pr.Number, st)
|
||||
}
|
||||
lines, err := fencedLines(now.Body, deferredFence)
|
||||
if err != nil || len(lines) != 1 {
|
||||
return fmt.Errorf("batch pull request #%d no longer carries the record notify read", pr.Number)
|
||||
}
|
||||
read, err := parseRecord(now.Body, notifyLineOf(rec))
|
||||
if err != nil || recordBlock(*read) != recordBlock(*rec) {
|
||||
return fmt.Errorf("batch pull request #%d no longer carries the record notify read", pr.Number)
|
||||
}
|
||||
// GitHub keeps the CRLF line ends a body edited on the web has; the
|
||||
// record is found, and the body written back, with LF ones.
|
||||
body := strings.ReplaceAll(now.Body, "\r\n", "\n")
|
||||
block := deferredFence + "\n" + lines[0] + "\n```"
|
||||
if strings.Count(body, block) != 1 {
|
||||
return fmt.Errorf("batch pull request #%d carries the record notify read, but not as one block it can mark", pr.Number)
|
||||
}
|
||||
body = strings.Replace(body, block, recordBlock(marked), 1)
|
||||
name, err := writeTemp("backport-batch-body-*.md", body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = os.Remove(name) }()
|
||||
return a.gh.editBody(pr.Number, name)
|
||||
}
|
||||
|
||||
// notifyLineOf is the line a record is for, which it was read against.
|
||||
func notifyLineOf(rec *lineRecord) line {
|
||||
l, _ := parseLine(rec.Line)
|
||||
return l
|
||||
}
|
||||
|
||||
// placement says under which list notify names original n of the record:
|
||||
// workflow for a wait for a maintainer, conflict for a deferred conflict, ""
|
||||
// for neither, a deferral for a reason among them. One that waits on others
|
||||
// goes where what decides its wait goes, followed down the record; one that
|
||||
// is not in the record is held by rule 6 or missing, and waits on nobody.
|
||||
func placement(deferred map[int]deferredEntry, n int, seen map[int]bool) string {
|
||||
e, ok := deferred[n]
|
||||
switch {
|
||||
case !ok || seen[n]:
|
||||
return ""
|
||||
case e.Workflows:
|
||||
return "workflow"
|
||||
case e.Reason != "":
|
||||
return ""
|
||||
case len(e.Needs) == 0:
|
||||
return "conflict"
|
||||
}
|
||||
seen[n] = true
|
||||
place := ""
|
||||
for _, f := range e.Needs {
|
||||
switch p := placement(deferred, f, seen); p {
|
||||
case "workflow":
|
||||
return p
|
||||
case "conflict":
|
||||
place = p
|
||||
}
|
||||
}
|
||||
return place
|
||||
}
|
||||
|
||||
// comment writes what waits into the one notify comment on the batch pull
|
||||
@@ -174,9 +309,9 @@ func (a *app) comment(pr *pullRequest, mention string, today time.Time, text []s
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
defer func() { _ = os.Remove(f.Name()) }()
|
||||
if _, err := f.WriteString(b.String()); err != nil {
|
||||
f.Close()
|
||||
_ = f.Close()
|
||||
return a.fail(err)
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
@@ -219,7 +354,7 @@ func (a *app) lines(args []string) int {
|
||||
return a.fail(err)
|
||||
}
|
||||
for _, l := range slices.Backward(lines) {
|
||||
fmt.Fprintln(a.stdout, l.branch())
|
||||
_, _ = fmt.Fprintln(a.stdout, l.branch())
|
||||
}
|
||||
return 0
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -334,3 +335,162 @@ func TestNotifyWritesNothingOnABatchClosedMeanwhile(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// parkedBody is an open batch body whose version 3 record parks p.
|
||||
func parkedBody(p ...parkedEntry) string {
|
||||
sha := strings.Repeat("a", 40)
|
||||
rec := lineRecord{Version: recordVersion, Line: "release-1.1", Report: strings.Repeat("b", 64),
|
||||
Built: &builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{Mode: modeContinue}},
|
||||
Deferred: []deferredEntry{}, Dropped: []droppedEntry{}, Stale: []int{}, Parked: p}
|
||||
return "batch\n\n" + recordBlock(rec) + "\n"
|
||||
}
|
||||
|
||||
// notifyAt runs notify at a time of day, with gh faked.
|
||||
func notifyAt(gh *fakeGH, at time.Time, args ...string) (int, string, string) {
|
||||
var stdout, stderr bytes.Buffer
|
||||
a := &app{gh: gh, stdout: &stdout, stderr: &stderr, now: func() time.Time { return at }}
|
||||
code := a.main(append([]string{"notify", "--line", "release-1.1"}, args...))
|
||||
return code, stdout.String(), stderr.String()
|
||||
}
|
||||
|
||||
// A park is announced once on the open batch: notify names it in its
|
||||
// comment and marks it announced in the record; a later notify is silent
|
||||
// about it. A mark that fails to land repeats the announcement once, the
|
||||
// next day. With no batch open nothing is announced, and the first notify of
|
||||
// the next batch does it.
|
||||
func TestNotifyAnnouncesAParkOnce(t *testing.T) {
|
||||
park := parkedEntry{Original: 5, Merge: strings.Repeat("c", 40), Kind: parkDependency, Needs: []int{3}}
|
||||
parked := func(gh *fakeGH) []parkedEntry {
|
||||
t.Helper()
|
||||
rec, err := parseRecord(gh.prs[0].Body, notifyLine)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return rec.Parked
|
||||
}
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}}
|
||||
code, stdout, stderr := notifyRun(gh, "--push")
|
||||
if code != 0 || !strings.Contains(stdout, "release-1.1: 1 parked originals on #90, named once") || len(gh.issueComments) != 1 || !strings.Contains(gh.issueComments[0].Body, "- [#5](https://github.com/cozystack/cozystack/pull/5): builds on [#3](") {
|
||||
t.Fatalf("notify = %d, want #5 announced:\n%s\n%s\n%v", code, stdout, stderr, gh.issueComments)
|
||||
}
|
||||
if p := parked(gh); len(p) != 1 || !p[0].Announced {
|
||||
t.Errorf("the record parks %+v, want #5 marked announced", p)
|
||||
}
|
||||
if code, stdout, _ = notifyRun(gh, "--push"); code != 0 || strings.Contains(stdout, "parked") || strings.Contains(gh.issueComments[0].Body, "[#5]") {
|
||||
t.Errorf("notify again = %d, want nothing about #5:\n%s\n%s", code, stdout, gh.issueComments[0].Body)
|
||||
}
|
||||
|
||||
gh = &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}, fail: map[string]error{"editBody": errors.New("HTTP 502")}}
|
||||
if code, _, _ := notifyRun(gh, "--push"); code != 2 || len(gh.issueComments) != 1 || parked(gh)[0].Announced {
|
||||
t.Fatalf("notify with a failing mark = %d, want 2 after the comment, the record unmarked", code)
|
||||
}
|
||||
code, _, stderr = notifyAt(gh, time.Date(2026, 10, 27, 0, 5, 0, 0, time.UTC), "--push")
|
||||
if code != 0 || !strings.Contains(gh.issueComments[0].Body, "[#5](") || !parked(gh)[0].Announced {
|
||||
t.Errorf("notify the next day = %d, want #5 named once more and marked:\n%s\n%s", code, stderr, gh.issueComments[0].Body)
|
||||
}
|
||||
|
||||
// Closed, or rewritten by a run, between the comment and the mark: the
|
||||
// mark is not written, and the next notify names #5 again.
|
||||
gh = &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}}
|
||||
gh.after = map[string]func(){"createComment": func() { gh.prs[0].State = "closed" }}
|
||||
if code, _, _ := notifyRun(gh, "--push"); code != 2 || len(gh.closedWrites) != 0 || parked(gh)[0].Announced {
|
||||
t.Errorf("notify on a batch closed meanwhile = %d, writes %q; want 2 and no mark", code, gh.closedWrites)
|
||||
}
|
||||
gh = &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}}
|
||||
rewritten := parkedBody(park, parkedEntry{Original: 6, Merge: strings.Repeat("d", 40), Kind: parkPartial, Files: []string{"x"}})
|
||||
gh.after = map[string]func(){"createComment": func() { gh.prs[0].Body = rewritten }}
|
||||
if code, _, _ := notifyRun(gh, "--push"); code != 2 || gh.prs[0].Body != rewritten || len(gh.bodies) != 0 {
|
||||
t.Errorf("notify on a body rewritten meanwhile = %d; want 2 and the new body left as it is", code)
|
||||
}
|
||||
|
||||
// A body someone edited on the web comes back with CRLF line ends; the
|
||||
// mark lands all the same.
|
||||
gh = &fakeGH{prs: []pullRequest{notifyPR("20261026", strings.ReplaceAll(parkedBody(park), "\n", "\r\n"))}}
|
||||
if code, _, stderr := notifyRun(gh, "--push"); code != 0 || !parked(gh)[0].Announced {
|
||||
t.Errorf("notify on a CRLF body = %d, want #5 marked:\n%s", code, stderr)
|
||||
}
|
||||
|
||||
closed := notifyPR("20261026", parkedBody(park))
|
||||
at := "2026-10-26T12:00:00Z"
|
||||
closed.State, closed.MergedAt = "closed", &at
|
||||
gh = &fakeGH{prs: []pullRequest{closed}}
|
||||
if code, stdout, _ := notifyRun(gh, "--push"); code != 0 || stdout != "" || len(gh.issueComments) != 0 || len(gh.bodies) != 0 {
|
||||
t.Errorf("notify with no batch open = %d, %q, %d comments, %d edits; want nothing", code, stdout, len(gh.issueComments), len(gh.bodies))
|
||||
}
|
||||
next := notifyPR("20261027", parkedBody(park))
|
||||
next.Number = 91
|
||||
gh.prs = append(gh.prs, next)
|
||||
if code, _, _ := notifyRun(gh, "--push"); code != 0 || len(gh.issueComments) != 1 || !strings.Contains(gh.issueComments[0].Body, "[#5](") {
|
||||
t.Errorf("the next batch's notify = %d, want #5 announced on it: %v", code, gh.issueComments)
|
||||
}
|
||||
}
|
||||
|
||||
// A park carries its announcement into the next record only while it is
|
||||
// the same park: the same merge, kind, prerequisites, commits and files.
|
||||
func TestParkEventIdentity(t *testing.T) {
|
||||
a, b := strings.Repeat("a", 40), strings.Repeat("b", 40)
|
||||
was := parkedEntry{Original: 5, Merge: a, Kind: parkDependency, Commits: []string{a}, Announced: true}
|
||||
carrier := &lineRecord{Parked: []parkedEntry{was}}
|
||||
for name, tc := range map[string]struct {
|
||||
now parkedEntry
|
||||
announced bool
|
||||
}{
|
||||
"the same park": {parkedEntry{Original: 5, Merge: a, Kind: parkDependency, Commits: []string{a}}, true},
|
||||
"another unnumbered commit": {parkedEntry{Original: 5, Merge: a, Kind: parkDependency, Commits: []string{b}}, false},
|
||||
"another kind": {parkedEntry{Original: 5, Merge: a, Kind: parkPartial, Files: []string{"x"}}, false},
|
||||
"another prerequisite": {parkedEntry{Original: 5, Merge: a, Kind: parkDependency, Commits: []string{a}, Needs: []int{3}}, false},
|
||||
"another merge": {parkedEntry{Original: 5, Merge: b, Kind: parkDependency, Commits: []string{a}}, false},
|
||||
"another original": {parkedEntry{Original: 6, Merge: a, Kind: parkDependency, Commits: []string{a}}, false},
|
||||
} {
|
||||
got := carryAnnounced([]parkedEntry{tc.now}, carrier)
|
||||
if got[0].Announced != tc.announced {
|
||||
t.Errorf("%s: announced = %v, want %v", name, got[0].Announced, tc.announced)
|
||||
}
|
||||
}
|
||||
if got := carryAnnounced([]parkedEntry{was}, nil); got[0].Announced {
|
||||
t.Error("a park with no carrier came out announced")
|
||||
}
|
||||
unannounced := was
|
||||
unannounced.Announced = false
|
||||
if got := carryAnnounced([]parkedEntry{was}, &lineRecord{Parked: []parkedEntry{unannounced}}); got[0].Announced {
|
||||
t.Error("a park the carrier holds unannounced came out announced")
|
||||
}
|
||||
}
|
||||
|
||||
// A park keeps its announcement through a continuation and into the next
|
||||
// batch, so notify does not name it again.
|
||||
func TestRolloverKeepsTheAnnouncement(t *testing.T) {
|
||||
f := featWorld(t)
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
if code, _, stderr := f.exec(gh, "notify", "--line", "release-1.1", "--push"); code != 0 || !strings.Contains(notifyComment(gh), "[#5](") {
|
||||
t.Fatalf("notify = %d, want #5 announced:\n%s", code, stderr)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 0 || !strings.Contains(stdout, `"needs":[3],"announced":true`) {
|
||||
t.Fatalf("run --continue = %d, want #5's park still announced:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
mergeBatch(f, gh)
|
||||
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
|
||||
gh.nextPR = 91
|
||||
f.now = f.now.Add(24 * time.Hour)
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--push"); code != 0 || !strings.Contains(stdout, `"needs":[3],"announced":true`) {
|
||||
t.Fatalf("the next batch's run = %d, want #5's park announced from the merged batch:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
gh.issueComments = nil
|
||||
if code, stdout, _ := f.exec(gh, "notify", "--line", "release-1.1", "--push"); code != 0 || strings.Contains(stdout, "parked") || strings.Contains(notifyComment(gh), "[#5](") {
|
||||
t.Errorf("notify on the next batch = %d, want nothing about #5:\n%s\n%s", code, stdout, notifyComment(gh))
|
||||
}
|
||||
}
|
||||
|
||||
// notifyComment is the notify comment on the fake, or "".
|
||||
func notifyComment(gh *fakeGH) string {
|
||||
for _, c := range slices.Backward(gh.issueComments) {
|
||||
if strings.HasPrefix(c.Body, notifyMarker) {
|
||||
return c.Body
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -41,12 +41,12 @@ func TestOntoContinues(t *testing.T) {
|
||||
f.lines()
|
||||
head := pushed(t, f)
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
code, stdout, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head, "--date", "2026-10-29")
|
||||
code, _, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head, "--date", "2026-10-29")
|
||||
if code != 2 || !strings.Contains(stderr, "not the head of backport-batch/release-1.1-20261029") {
|
||||
t.Errorf("--onto with another date = %d (%s), want a refusal", code, stderr)
|
||||
}
|
||||
f.now = f.now.AddDate(0, 0, 3)
|
||||
code, stdout, stderr = f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head)
|
||||
code, stdout, stderr := f.exec(&fakeGH{}, "run", "--line", "release-1.1", "--onto", head)
|
||||
if code != 0 {
|
||||
t.Fatalf("run --onto = %d\n%s", code, stderr)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,385 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type failingWriter struct{}
|
||||
|
||||
func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("broken pipe") }
|
||||
|
||||
// Output that never reached its reader fails the command, and stderr says why.
|
||||
func TestUnwritableOutputFailsTheCommand(t *testing.T) {
|
||||
var stderr strings.Builder
|
||||
a := &app{gh: &fakeGH{}, stdout: failingWriter{}, stderr: &stderr}
|
||||
if code := a.main([]string{"help"}); code != 2 {
|
||||
t.Fatalf("help with an unwritable stdout = %d, want 2", code)
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "broken pipe") {
|
||||
t.Errorf("stderr does not name the write failure:\n%s", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// A command that already failed keeps its own exit code when its output is
|
||||
// lost as well: check still says something is missing.
|
||||
func TestUnwritableOutputKeepsTheCommandsFailure(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
var stderr strings.Builder
|
||||
a := &app{gh: &fakeGH{remoteHead: f.remoteHead}, stdout: failingWriter{}, stderr: &stderr, now: func() time.Time { return f.now }, tool: f.tool}
|
||||
if code := a.main([]string{"check", "--line", "release-1.1"}); code != 1 {
|
||||
t.Fatalf("check with #6 missing and an unwritable stdout = %d, want 1\n%s", code, stderr.String())
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "broken pipe") {
|
||||
t.Errorf("stderr does not name the write failure:\n%s", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// A closed pipe on stdout does not kill the run before it can say what
|
||||
// already happened: the write fails like any other, and the run exits 2.
|
||||
func TestClosedStdoutPipeIsReported(t *testing.T) {
|
||||
if os.Getenv("BACKPORT_BATCH_CLOSED_STDOUT_HELPER") == "1" {
|
||||
os.Args = []string{"backport-batch", "help"}
|
||||
main()
|
||||
return
|
||||
}
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = r.Close()
|
||||
t.Cleanup(func() { _ = w.Close() })
|
||||
cmd := exec.Command(os.Args[0], "-test.run=^TestClosedStdoutPipeIsReported$")
|
||||
cmd.Env = append(os.Environ(), "BACKPORT_BATCH_CLOSED_STDOUT_HELPER=1")
|
||||
var stderr strings.Builder
|
||||
cmd.Stdout, cmd.Stderr = w, &stderr
|
||||
err = cmd.Run()
|
||||
var exit *exec.ExitError
|
||||
if !errors.As(err, &exit) || exit.ExitCode() != 2 {
|
||||
t.Fatalf("help into a closed pipe = %v, want exit 2\nstderr:\n%s", err, stderr.String())
|
||||
}
|
||||
if !strings.Contains(stderr.String(), "output was lost") {
|
||||
t.Errorf("stderr does not say the output was lost:\n%s", stderr.String())
|
||||
}
|
||||
}
|
||||
|
||||
// failOn fails every write that contains want, and takes every other one.
|
||||
type failOn struct {
|
||||
want string
|
||||
buf strings.Builder
|
||||
}
|
||||
|
||||
func (w *failOn) Write(p []byte) (int, error) {
|
||||
if strings.Contains(string(p), w.want) {
|
||||
return 0, errors.New("broken pipe")
|
||||
}
|
||||
return w.buf.Write(p)
|
||||
}
|
||||
|
||||
func runWith(f *fixture, gh *fakeGH, stdout, stderr io.Writer, args ...string) int {
|
||||
f.t.Helper()
|
||||
if gh.remoteHead == nil {
|
||||
gh.remoteHead = f.remoteHead
|
||||
}
|
||||
a := &app{gh: gh, stdout: stdout, stderr: stderr, now: func() time.Time { return f.now }, tool: f.tool}
|
||||
return a.main(args)
|
||||
}
|
||||
|
||||
// Output is written before the push; when it is lost, on either stream,
|
||||
// nothing is pushed and no pull request is opened.
|
||||
func TestLostOutputPushesNothing(t *testing.T) {
|
||||
for _, stream := range []string{"stdout", "stderr"} {
|
||||
t.Run(stream, func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
gh := &fakeGH{}
|
||||
var stdout, stderr io.Writer = &strings.Builder{}, &strings.Builder{}
|
||||
if stream == "stdout" {
|
||||
stdout = failingWriter{}
|
||||
} else {
|
||||
stderr = failingWriter{}
|
||||
}
|
||||
if code := runWith(f, gh, stdout, stderr, "run", "--line", "release-1.1", "--push"); code != 2 {
|
||||
t.Fatalf("run --push with a failing %s = %d, want 2", stream, code)
|
||||
}
|
||||
if head := f.remoteHead(batchBranch1); head != "" {
|
||||
t.Errorf("the batch was pushed at %.12s", head)
|
||||
}
|
||||
if len(gh.prs) != 0 {
|
||||
t.Errorf("a pull request was opened: %+v", gh.prs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A confirmation lost after the push fails the run: the pull request was
|
||||
// opened, and the run does not exit 0 without having said so.
|
||||
func TestLostConfirmationFailsTheRun(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
gh := &fakeGH{}
|
||||
if code := runWith(f, gh, &strings.Builder{}, &failOn{want: "opened draft"}, "run", "--line", "release-1.1", "--push"); code != 2 {
|
||||
t.Fatalf("run --push losing its confirmation = %d, want 2", code)
|
||||
}
|
||||
if len(gh.prs) != 1 {
|
||||
t.Errorf("%d pull requests, want the one the run opened", len(gh.prs))
|
||||
}
|
||||
if len(gh.issueComments) != 0 {
|
||||
t.Errorf("the report was posted after the confirmation was lost: %+v", gh.issueComments)
|
||||
}
|
||||
}
|
||||
|
||||
// notify writes what it found before it comments; when that is lost, it
|
||||
// does not comment.
|
||||
func TestLostOutputCommentsNothing(t *testing.T) {
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR("20261019", "")}}
|
||||
var stderr strings.Builder
|
||||
a := &app{gh: gh, stdout: failingWriter{}, stderr: &stderr, now: func() time.Time { return time.Date(2026, 10, 26, 6, 23, 0, 0, time.UTC) }}
|
||||
if code := a.main([]string{"notify", "--line", "release-1.1", "--push"}); code != 2 {
|
||||
t.Fatalf("notify --push with a failing stdout = %d, want 2\n%s", code, stderr.String())
|
||||
}
|
||||
if len(gh.issueComments) != 0 {
|
||||
t.Errorf("notify commented: %+v", gh.issueComments)
|
||||
}
|
||||
}
|
||||
|
||||
// writesGitHub records the GitHub writes that reach it.
|
||||
type writesGitHub struct {
|
||||
github
|
||||
writes []string
|
||||
}
|
||||
|
||||
func (g *writesGitHub) createPull(string, string, string, string) (int, string, error) {
|
||||
g.writes = append(g.writes, "createPull")
|
||||
return 1, "", nil
|
||||
}
|
||||
func (g *writesGitHub) editPull(int, string, string) error {
|
||||
g.writes = append(g.writes, "editPull")
|
||||
return nil
|
||||
}
|
||||
func (g *writesGitHub) editBody(int, string) error {
|
||||
g.writes = append(g.writes, "editBody")
|
||||
return nil
|
||||
}
|
||||
func (g *writesGitHub) closePull(int) error {
|
||||
g.writes = append(g.writes, "closePull")
|
||||
return nil
|
||||
}
|
||||
func (g *writesGitHub) createComment(int, string) error {
|
||||
g.writes = append(g.writes, "createComment")
|
||||
return nil
|
||||
}
|
||||
func (g *writesGitHub) editComment(int64, string) error {
|
||||
g.writes = append(g.writes, "editComment")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Every GitHub write is refused once output was lost. Every method of
|
||||
// github is either a read or in this table, so a write added later without
|
||||
// a guard fails here.
|
||||
func TestEveryGitHubWriteIsGuarded(t *testing.T) {
|
||||
reads := map[string]bool{"openPulls": true, "recentPulls": true, "permission": true, "labelled": true, "pull": true, "comments": true, "batchBot": true}
|
||||
inner := &writesGitHub{}
|
||||
g := guardedGitHub{github: inner, a: &app{unwritten: errors.New("broken pipe")}}
|
||||
writes := map[string]func() error{
|
||||
"createPull": func() error { _, _, err := g.createPull("b", "h", "t", "f"); return err },
|
||||
"editPull": func() error { return g.editPull(1, "t", "f") },
|
||||
"editBody": func() error { return g.editBody(1, "f") },
|
||||
"closePull": func() error { return g.closePull(1) },
|
||||
"createComment": func() error { return g.createComment(1, "f") },
|
||||
"editComment": func() error { return g.editComment(1, "f") },
|
||||
}
|
||||
iface := reflect.TypeFor[github]()
|
||||
for i := range iface.NumMethod() {
|
||||
name := iface.Method(i).Name
|
||||
if _, ok := writes[name]; !ok && !reads[name] {
|
||||
t.Errorf("github.%s is neither a known read nor a guarded write", name)
|
||||
}
|
||||
}
|
||||
for name, write := range writes {
|
||||
if err := write(); err == nil || !strings.Contains(err.Error(), "output was lost") {
|
||||
t.Errorf("%s with output lost = %v, want a refusal", name, err)
|
||||
}
|
||||
}
|
||||
if len(inner.writes) != 0 {
|
||||
t.Errorf("writes reached GitHub after output was lost: %q", inner.writes)
|
||||
}
|
||||
}
|
||||
|
||||
// A warning lost while the open batch is being updated stops the update:
|
||||
// nothing is pushed, posted or edited.
|
||||
func TestLostWarningLeavesTheOpenBatchAlone(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
gh := &fakeGH{}
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--push")
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
bad := issueComment{ID: 8, Body: reportMarker + "names none\n"}
|
||||
bad.User.Login, bad.User.Type = "maintainer", "User"
|
||||
gh.issueComments = append(gh.issueComments, bad)
|
||||
head, comments, body := f.remoteHead(batchBranch1), len(gh.issueComments), gh.find(90).Body
|
||||
f.now = f.now.AddDate(0, 0, 1)
|
||||
if code := runWith(f, gh, &strings.Builder{}, &failOn{want: "does not count as a report"}, "run", "--line", "release-1.1", "--continue", "--push"); code != 2 {
|
||||
t.Fatalf("run --continue --push losing a warning = %d, want 2", code)
|
||||
}
|
||||
if got := f.remoteHead(batchBranch1); got != head {
|
||||
t.Errorf("the batch moved to %.12s", got)
|
||||
}
|
||||
if len(gh.issueComments) != comments {
|
||||
t.Errorf("a comment was posted: %d comments, want %d", len(gh.issueComments), comments)
|
||||
}
|
||||
if gh.find(90).Body != body {
|
||||
t.Error("the batch pull request was edited")
|
||||
}
|
||||
}
|
||||
|
||||
// A warning lost while a batch is being closed leaves it open, with no
|
||||
// closing record half written.
|
||||
func TestLostWarningLeavesTheBatchUnclosed(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
gh := &fakeGH{}
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--push")
|
||||
bad := issueComment{ID: 8, Body: reportMarker + "names none\n"}
|
||||
bad.User.Login, bad.User.Type = "maintainer", "User"
|
||||
gh.issueComments = append(gh.issueComments, bad)
|
||||
body := gh.find(90).Body
|
||||
if code := runWith(f, gh, &strings.Builder{}, &failOn{want: "does not count as a report"}, "run", "--line", "release-1.1", "--defer", "5", "--reason", "x", "--push"); code != 2 {
|
||||
t.Fatalf("run --defer losing a warning = %d, want 2", code)
|
||||
}
|
||||
if gh.find(90).Body != body {
|
||||
t.Error("the batch pull request was edited")
|
||||
}
|
||||
if !gh.isOpen(90) {
|
||||
t.Error("the batch pull request was closed")
|
||||
}
|
||||
}
|
||||
|
||||
// The git and gh children keep the default SIGPIPE: catching it here must not
|
||||
// leave it ignored in them, as signal.Ignore would through exec.
|
||||
func TestChildrenKeepTheDefaultSIGPIPE(t *testing.T) {
|
||||
catchSIGPIPE()
|
||||
out, err := exec.Command("sh", "-c", "kill -PIPE $$; echo survived").Output()
|
||||
if err == nil || strings.Contains(string(out), "survived") {
|
||||
t.Fatalf("a child survived SIGPIPE: %q, %v", out, err)
|
||||
}
|
||||
}
|
||||
|
||||
// The branch a close left is not deleted once output was lost: a deletion
|
||||
// is a push like any other.
|
||||
func TestLostOutputDeletesNoBranch(t *testing.T) {
|
||||
f, gh, h1 := closeWorld(t)
|
||||
f.git("switch", "-q", "main")
|
||||
l, err := parseLine("release-1.1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a := &app{gh: gh, unwritten: errors.New("broken pipe")}
|
||||
if err := a.deleteBranch(l, closing{PR: 90, Branch: batchBranch1, Head: h1}); err == nil || !strings.Contains(err.Error(), "output was lost") {
|
||||
t.Fatalf("deleteBranch with output lost = %v, want a refusal", err)
|
||||
}
|
||||
if got := f.remoteHead(batchBranch1); got != h1 {
|
||||
t.Errorf("the branch is at %q, want it left at %.12s", got, h1)
|
||||
}
|
||||
}
|
||||
|
||||
// A park line lost on stdout stops notify before it writes: no comment, and
|
||||
// the record is not marked as announced.
|
||||
func TestLostParkLineWritesNothing(t *testing.T) {
|
||||
park := parkedEntry{Original: 5, Merge: strings.Repeat("c", 40), Kind: parkDependency, Needs: []int{3}}
|
||||
gh := &fakeGH{prs: []pullRequest{notifyPR("20261026", parkedBody(park))}}
|
||||
body := gh.prs[0].Body
|
||||
var stderr strings.Builder
|
||||
a := &app{gh: gh, stdout: &failOn{want: "parked originals"}, stderr: &stderr, now: func() time.Time { return time.Date(2026, 10, 26, 6, 23, 0, 0, time.UTC) }}
|
||||
if code := a.main([]string{"notify", "--line", "release-1.1", "--push"}); code != 2 {
|
||||
t.Fatalf("notify --push losing the park line = %d, want 2\n%s", code, stderr.String())
|
||||
}
|
||||
if len(gh.issueComments) != 0 {
|
||||
t.Errorf("notify commented: %+v", gh.issueComments)
|
||||
}
|
||||
if gh.prs[0].Body != body {
|
||||
t.Error("notify marked the park as announced")
|
||||
}
|
||||
}
|
||||
|
||||
// The hint naming the commits a reset drops is the only way back to them, so
|
||||
// the local branch is not reset when the hint is lost.
|
||||
func TestLostResetHintKeepsTheBranch(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.git("switch", "-q", "-C", batchBranch1, "origin/release-1.1")
|
||||
f.write("app/mine.txt", "unpushed\n")
|
||||
f.git("add", "-A")
|
||||
f.git("commit", "-q", "-m", "an unpushed resolution")
|
||||
tip := f.git("rev-parse", "HEAD")
|
||||
f.git("switch", "-q", "main")
|
||||
if code := runWith(f, &fakeGH{}, &strings.Builder{}, &failOn{want: "resetting the local"}, "run", "--line", "release-1.1"); code != 2 {
|
||||
t.Fatalf("run losing the reset hint = %d, want 2", code)
|
||||
}
|
||||
if got := f.git("rev-parse", batchBranch1); got != tip {
|
||||
t.Errorf("the local %s moved to %.12s, want it kept at %.12s", batchBranch1, got, tip)
|
||||
}
|
||||
}
|
||||
|
||||
// Once output is lost the run moves no local branch, even one a reset would
|
||||
// only fast-forward.
|
||||
func TestLostOutputMovesNoLocalBranch(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.git("branch", "-f", batchBranch1, "origin/release-1.1^")
|
||||
before := f.git("rev-parse", batchBranch1)
|
||||
if code := runWith(f, &fakeGH{}, &strings.Builder{}, &failOn{want: "prepared"}, "run", "--line", "release-1.1"); code != 2 {
|
||||
t.Fatalf("run losing an early line = %d, want 2", code)
|
||||
}
|
||||
if got := f.git("rev-parse", batchBranch1); got != before {
|
||||
t.Errorf("the local %s moved to %.12s, want it kept at %.12s", batchBranch1, got, before)
|
||||
}
|
||||
}
|
||||
|
||||
// No new unit starts once output is lost: neither a pick nor a replay touches
|
||||
// the branch.
|
||||
func TestLostOutputStartsNoUnit(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
before := f.git("rev-parse", "HEAD")
|
||||
a := &app{unwritten: errors.New("broken pipe")}
|
||||
if _, err := a.pickUnit(&verdict{}, nil); err == nil || !strings.Contains(err.Error(), "output was lost") {
|
||||
t.Errorf("pickUnit with output lost = %v, want a refusal", err)
|
||||
}
|
||||
if _, _, err := a.replayNext(unit{}); err == nil || !strings.Contains(err.Error(), "output was lost") {
|
||||
t.Errorf("replayNext with output lost = %v, want a refusal", err)
|
||||
}
|
||||
if got := f.git("rev-parse", "HEAD"); got != before {
|
||||
t.Errorf("HEAD moved to %.12s", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,796 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var line11, _ = parseLine("release-1.1")
|
||||
|
||||
// handBatch pushes a batch carrying merges in the batch's shape, as a run
|
||||
// made one before the partial guard, and opens its pull request with a
|
||||
// record that defers nothing.
|
||||
func handBatch(f *fixture, gh *fakeGH, merges ...string) string {
|
||||
f.t.Helper()
|
||||
f.git("fetch", "-q", "origin")
|
||||
f.git("switch", "-q", "--detach", "origin/release-1.1")
|
||||
for _, mc := range merges {
|
||||
if parents := strings.Fields(f.git("rev-list", "--parents", "-n", "1", mc)); len(parents) == 2 {
|
||||
f.git("cherry-pick", "-x", mc)
|
||||
continue
|
||||
}
|
||||
base := f.git("rev-parse", "HEAD")
|
||||
for _, c := range strings.Fields(f.git("rev-list", "--reverse", "--no-merges", mc+"^1.."+mc+"^2")) {
|
||||
f.git("cherry-pick", "-x", c)
|
||||
}
|
||||
merge := f.joined(base, "HEAD", f.git("log", "-1", "--format=%B", mc)+"\n\n"+batchTrailer+": "+mc)
|
||||
f.git("update-ref", "HEAD", merge)
|
||||
}
|
||||
f.git("push", "-q", "origin", "HEAD:refs/heads/"+batchBranch1)
|
||||
f.git("switch", "-q", "main")
|
||||
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, "batch\n\n"+deferredBlock(line11, nil, nil, nil)+"\n")}
|
||||
return f.remoteHead(batchBranch1)
|
||||
}
|
||||
|
||||
// policyWorld is #5088's shape: #3, a feat the line never gets, adds a
|
||||
// policy and its test; #5 deletes both again and adds a test of its own.
|
||||
func policyWorld(t *testing.T) (*fixture, string) {
|
||||
t.Helper()
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(3, "feat(chart): a network policy", "", map[string]string{"chart/templates/policy.yaml": "kind: NetworkPolicy\n", "chart/tests/policy_test.yaml": "suite: policy\n"})
|
||||
mc5 := f.pr(5, "fix(chart): remove the policy a rebase brought back", "```release-note\nfix(chart): the stray network policy is gone.\n```\n",
|
||||
map[string]string{"chart/templates/policy.yaml": "", "chart/tests/policy_test.yaml": "", "chart/tests/values_test.yaml": "suite: values\n"})
|
||||
return f, mc5
|
||||
}
|
||||
|
||||
// A clean pick that would delete files the line never had, and so carry only
|
||||
// #5's test, is withheld: #5 is parked as partial, with the paths it would
|
||||
// leave without effect, and its release note stays out of the body.
|
||||
func TestPartialPickIsWithheld(t *testing.T) {
|
||||
f, mc5 := policyWorld(t)
|
||||
f.pr(6, "fix(app): six", "```release-note\nfix(app): six works.\n```\n", map[string]string{"app/six.txt": "6\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): six (#6)"}) {
|
||||
t.Errorf("picked %q, want only #6", got)
|
||||
}
|
||||
want := `"parked":[{"original":5,"merge":"` + mc5 + `","kind":"partial","files":["chart/templates/policy.yaml","chart/tests/policy_test.yaml"]}]`
|
||||
if !strings.Contains(stdout, want) {
|
||||
t.Errorf("the record does not park #5 as partial (%s):\n%s", want, stdout)
|
||||
}
|
||||
if !strings.Contains(stdout, "### Parked") || !strings.Contains(stdout, "`chart/templates/policy.yaml`") {
|
||||
t.Errorf("the body does not say #5 is parked and why:\n%s", stdout)
|
||||
}
|
||||
if strings.Contains(stdout, "stray network policy") || !strings.Contains(stdout, "six works") {
|
||||
t.Errorf("the release notes are not #6's alone:\n%s", stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// A file the line removed itself, which no original added after the line
|
||||
// branched, still leaves #5's deletion without effect: check finds it with no
|
||||
// record and parks it, and run picks nothing.
|
||||
func TestPartialWithoutAnAdder(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: add a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): the line drops the policy", map[string]string{"app/policy.yaml": ""})
|
||||
f.pr(5, "fix(app): drop the policy", "", map[string]string{"app/policy.yaml": "", "app/policy_test.txt": "gone\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); len(got) != 0 {
|
||||
t.Errorf("picked %q, want nothing", got)
|
||||
}
|
||||
if !strings.Contains(stdout, "### Parked") {
|
||||
t.Errorf("the body does not park #5:\n%s", stdout)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
code, stdout, stderr = f.exec(nil, "check", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stderr, "note: #5 is parked: only part of it applies to release-1.1") {
|
||||
t.Errorf("check = %d, want 0 with a parked-partial note:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// The picker judges what its picks did, not what the original's merge did:
|
||||
// #5's first commit deletes a file the line no longer has and comes out
|
||||
// empty, its second adds a test, and the unit left would be the test alone.
|
||||
func TestPartialByAnEmptyOwnCommit(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: add a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): the line drops the policy", map[string]string{"app/policy.yaml": ""})
|
||||
f.prCommits(5, "fix(app): drop the policy and test it", "```release-note\nfix(app): no more policy.\n```\n",
|
||||
change{files: map[string]string{"app/policy.yaml": ""}},
|
||||
change{files: map[string]string{"app/policy_test.txt": "gone\n"}})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); len(got) != 0 {
|
||||
t.Errorf("picked %q, want #5 withheld", got)
|
||||
}
|
||||
if strings.Contains(stdout, "no more policy") || !strings.Contains(stdout, "### Parked") {
|
||||
t.Errorf("#5's note is in the body, or #5 is not parked:\n%s", stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// A batch built before the guard can already carry a partial unit. It fails
|
||||
// the cut and verify whatever its label says, and --defer takes it off,
|
||||
// closing a batch it was the last unit of.
|
||||
func TestExistingPartialUnitFailsTheCut(t *testing.T) {
|
||||
f, mc5 := policyWorld(t)
|
||||
gh := &fakeGH{}
|
||||
head := handBatch(f, gh, mc5)
|
||||
code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut")
|
||||
if code != 1 || !strings.Contains(stdout, "#5 is on batch #90, but only part of it applies to release-1.1") {
|
||||
t.Errorf("check --cut = %d, want 1 naming the partial unit:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
|
||||
}
|
||||
code, stdout, stderr = f.exec(gh, "verify", "--line", "release-1.1", "--head", head)
|
||||
if code != 1 || !strings.Contains(stdout, `#5 leaves "chart/templates/policy.yaml", "chart/tests/policy_test.yaml" without effect on release-1.1`) {
|
||||
t.Errorf("verify = %d, want 1 naming what #5 leaves out:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
gh.labels = map[string][]int{labelSkip: {5}}
|
||||
code, stdout, stderr = f.exec(gh, "check", "--line", "release-1.1", "--cut")
|
||||
if code != 1 || !strings.Contains(stdout, "#5 is on batch #90, but only part of it applies to release-1.1") {
|
||||
t.Errorf("check --cut with backport/skip = %d, want the partial unit to still fail it:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
|
||||
}
|
||||
code, _, stderr = f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "only part of it applies", "--push")
|
||||
if code != 0 || prState(gh.find(90)) != "closed" || f.remoteHead(batchBranch1) != "" {
|
||||
t.Errorf("run --defer 5 --push = %d, #90 %s, branch at %q; want the batch closed and its branch gone:\n%s", code, prState(gh.find(90)), f.remoteHead(batchBranch1), stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// An original whose first pick came out empty, because the line already had
|
||||
// that change, is on the batch all the same: check does not call it missing,
|
||||
// a continuation keeps it picked, and so does a rebuild without another
|
||||
// original.
|
||||
func TestOnBatchAfterAnEmptyFirstPick(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": "old\n", "app/b.txt": "old\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): a, on the line first", map[string]string{"app/a.txt": "fixed\n"})
|
||||
f.prCommits(5, "fix(app): a and b", "",
|
||||
change{files: map[string]string{"app/a.txt": "fixed\n"}},
|
||||
change{files: map[string]string{"app/b.txt": "fixed\n"}})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
head := openBatch(t, f, gh)
|
||||
if got := f.subjects("origin/release-1.1", head); !slices.Equal(got, []string{"fix(app): a and b (#5)", "fix(app): six (#6)"}) {
|
||||
t.Fatalf("the batch carries %q, want #5 and #6", got)
|
||||
}
|
||||
code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Errorf("check = %d, want 0 with #5 on the batch:\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
|
||||
}
|
||||
code, stdout, _ = f.exec(gh, "check", "--line", "release-1.1", "--cut")
|
||||
if code != 1 || !strings.Contains(stdout, "#5 is only on open batch PR #90") {
|
||||
t.Errorf("check --cut = %d, want #5 only on the batch:\n%s", code, stdout)
|
||||
}
|
||||
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--continue")
|
||||
if code != 0 || !strings.Contains(stdout, `"picked":[5,6]`) {
|
||||
t.Errorf("run --continue = %d, want #5 and #6 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--defer", "6", "--reason", "fails the e2e")
|
||||
if code != 0 || !strings.Contains(stdout, `"picked":[5]`) || !strings.Contains(stdout, `"missing":[]`) {
|
||||
t.Errorf("run --defer 6 = %d, want #5 still picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// featWorld is #5071's shape: #3, a feat the line never gets, adds a file,
|
||||
// and #5 fixes that file; #6 is a plain fix beside it.
|
||||
func featWorld(t *testing.T) *fixture {
|
||||
t.Helper()
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
|
||||
f.pr(5, "fix(app): fix the tap", "```release-note\nfix(app): the tap works.\n```\n", map[string]string{"app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
return f
|
||||
}
|
||||
|
||||
// A fix of a file a feat added on main, which the line never gets, is
|
||||
// parked, not tried, and check takes the park as a note.
|
||||
func TestParkAFixOfAFeatureTheLineLacks(t *testing.T) {
|
||||
f := featWorld(t)
|
||||
gh := &fakeGH{}
|
||||
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push")
|
||||
if code != 0 {
|
||||
t.Fatalf("run --push = %d\n%s", code, stderr)
|
||||
}
|
||||
if strings.Contains(stdout, "### Deferred conflicts") || !strings.Contains(stdout, `"parked":[{"original":5,`) || !strings.Contains(stdout, `"kind":"dependency","needs":[3]`) {
|
||||
t.Errorf("#5 is not parked on #3:\n%s", stdout)
|
||||
}
|
||||
if !strings.Contains(stdout, "### Parked") || !strings.Contains(stdout, "builds on [#3]") {
|
||||
t.Errorf("the body does not say #5 builds on #3:\n%s", stdout)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
code, stdout, stderr = f.exec(gh, "check", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stderr, "note: #5 is parked: it builds on #3, which never reaches release-1.1") {
|
||||
t.Errorf("check = %d, want 0 with a note that #5 is parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// backport/include lifts no park.
|
||||
func TestIncludeDoesNotLiftThePark(t *testing.T) {
|
||||
f := featWorld(t)
|
||||
code, stdout, stderr := f.exec(&fakeGH{labels: map[string][]int{labelInclude: {5}}}, "run", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stdout, `"parked":[{"original":5,`) || !strings.Contains(stdout, "`backport/include`, parked: builds on") {
|
||||
t.Errorf("run with backport/include on #5 = %d, want #5 still parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A prerequisite that is itself parked, but adapted onto the batch by hand,
|
||||
// satisfies what builds on it: #3 is parked on #2, a feat, and #5 changes a
|
||||
// line #3 wrote.
|
||||
func TestAdaptedPrerequisiteSatisfiesItsDependent(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": appFile, "app/b.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(20, "feat(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
mc3 := f.pr(3, "fix(app): an excited two, a loud three", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/b.txt": strings.Replace(appFile, "three", "THREE", 1)})
|
||||
f.pr(5, "fix(app): an excited three", "", map[string]string{"app/b.txt": strings.Replace(appFile, "three", "THREE!", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, `"kind":"dependency","needs":[3]`) {
|
||||
t.Fatalf("run = %d, want #5 parked as long as #3 is:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
w := emptyBatch(f, &fakeGH{}, "batch\n\n"+deferredBlock(line11, nil, nil, nil)+"\n")
|
||||
// #3 adapted by hand: every file it changes, changed, in the batch's
|
||||
// shape.
|
||||
pick := w.pick(w.head, f.git("rev-parse", mc3+"^2"), map[string]string{"app/a.txt": strings.Replace(appFile, "two", "two!", 1), "app/b.txt": strings.Replace(appFile, "three", "THREE", 1)})
|
||||
fold := w.merge(w.head, pick, "fix(app): an excited two, a loud three (#3)\n\n"+batchTrailer+": "+mc3)
|
||||
f.git("push", "-q", "origin", fold+":refs/heads/"+batchBranch1)
|
||||
w.gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, w.gh.prs[0].Body)}
|
||||
code, stdout, stderr := f.exec(w.gh, "check", "--line", "release-1.1")
|
||||
if code != 1 || strings.Contains(stderr, "#5 is parked") || strings.Contains(stdout+stderr, "waits on") || !strings.Contains(stdout, "/pull/5 ") {
|
||||
t.Errorf("check = %d, want #5 missing, not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
code, stdout, stderr = f.exec(w.gh, "run", "--line", "release-1.1", "--continue")
|
||||
if code != 0 || !strings.Contains(stdout, `"picked":[3,5]`) {
|
||||
t.Errorf("run --continue = %d, want #5 picked after #3's adaptation:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// What an earlier unit of the batch brought counts: #3 writes the line #8
|
||||
// changes, a feat takes it back and another feat, #6, writes it again, so
|
||||
// blame names #6, which never comes; once #3 is picked the branch has the
|
||||
// line all the same.
|
||||
func TestContentFromAnotherUnitSatisfies(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(3, "fix(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(4, "feat(app): a quiet two", "", map[string]string{"app/a.txt": appFile})
|
||||
f.pr(6, "feat(app): a loud two again", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(8, "fix(app): an excited two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
if got := f.subjects("origin/release-1.1", "HEAD"); !slices.Equal(got, []string{"fix(app): a loud two (#3)", "fix(app): an excited two (#8)"}) {
|
||||
t.Errorf("picked %q, want #3 and #8:\n%s", got, stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// auxWorld is #5142's shape: #3, a feat, adds a bats test; #5 fixes a
|
||||
// workflow and that test, so all it builds on that never comes is a test;
|
||||
// #6 is a plain fix.
|
||||
func auxWorld(t *testing.T) *fixture {
|
||||
t.Helper()
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a workflow", "", map[string]string{".github/workflows/release.yaml": "on: push\njobs: {}\n"})
|
||||
f.lines()
|
||||
f.pr(3, "feat(ci): test the changelog", "", map[string]string{"hack/changelog.bats": "@test x {\n true\n}\n"})
|
||||
f.pr(5, "fix(ci): run the release on tags", "", map[string]string{".github/workflows/release.yaml": "on: [push, tag]\njobs: {}\n", "hack/changelog.bats": "@test x {\n false\n}\n"})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
return f
|
||||
}
|
||||
|
||||
// A workflow original whose only missing dependency is a test waits for a
|
||||
// maintainer in the scheduled run, whose token cannot push workflows, and
|
||||
// check holds the cut on it. A maintainer's own run parks it. With no batch
|
||||
// open, or one whose body carries no record, it is missing; a record from
|
||||
// before the mode was saved says the scheduled mode by listing a wait.
|
||||
func TestWorkflowModeCarriesToCheck(t *testing.T) {
|
||||
f := auxWorld(t)
|
||||
gh := &fakeGH{}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 || strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `{"original":5,"files":[".github/workflows/release.yaml"],"workflows":true}`) {
|
||||
t.Fatalf("scheduled run = %d, want #5 waiting, not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#5 changes .github/workflows/ and waits on batch PR #90") {
|
||||
t.Errorf("check --cut = %d, want #5 waiting:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
v1 := "batch\n\n" + deferredBlock(line11, nil, nil, []pick{{v: &verdict{o: &original{number: 5}}, files: []string{".github/workflows/release.yaml"}}}) + "\n"
|
||||
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, v1)}
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#5 changes .github/workflows/ and waits on batch PR #90") {
|
||||
t.Errorf("check --cut with a version 1 record = %d, want #5 waiting:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
gh.prs = []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, "a batch someone wrote")}
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/5 ") || strings.Contains(stderr, "parked") {
|
||||
t.Errorf("check under a batch with no record = %d, want #5 missing:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
|
||||
f = auxWorld(t)
|
||||
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/5 ") || strings.Contains(stderr, "parked") {
|
||||
t.Errorf("check with no batch = %d, want #5 missing:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
gh = &fakeGH{}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Fatalf("a maintainer's run = %d, want #5 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "note: #5 is parked") {
|
||||
t.Errorf("check after a maintainer's run = %d, want 0 with #5 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// Evidence found only in a test file still parks an original that changes
|
||||
// no workflow: #4973's shape.
|
||||
func TestTestOnlyEvidenceParksOtherOriginals(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: nodes", "", map[string]string{"app/nodes.go": "package app\n", "app/nodes_test.go": "package app\n\nfunc a() {}\n"})
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): proxmox nodes", "", map[string]string{"app/nodes_test.go": "package app\n\nfunc a() { proxmox() }\n"})
|
||||
f.pr(5, "fix(app): nodes", "", map[string]string{"app/nodes.go": "package app\n\nvar fixed = true\n", "app/nodes_test.go": "package app\n\nfunc a() { proxmox(); fixed() }\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 || strings.Contains(stdout, "### Deferred conflicts") || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Errorf("run = %d, want #5 parked on #3:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// What an original builds on through lines it adds is not seen: #4973's
|
||||
// $isProxmox. The original is picked; CI on the batch is what catches it.
|
||||
func TestAddedLinesDependencyIsNotSeen(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: nodes", "", map[string]string{"app/nodes.tpl": "a\nb\n"})
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): define isProxmox", "", map[string]string{"app/proxmox.tpl": "isProxmox\n"})
|
||||
f.pr(5, "fix(app): use isProxmox", "", map[string]string{"app/nodes.tpl": "a\nif isProxmox\nb\n"})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `"picked":[5]`) {
|
||||
t.Errorf("run = %d, want #5 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// With no batch open, check reads a closed record whatever its version and
|
||||
// whatever mode it saved as the scheduled run's: the workflow original it
|
||||
// left waiting is missing, not parked, while its drops and its other
|
||||
// deferrals still count.
|
||||
func TestClosedRecordModeFallback(t *testing.T) {
|
||||
for _, version := range []int{2, 3} {
|
||||
for _, saved := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("version %d, noWorkflowFiles %v", version, saved), func(t *testing.T) {
|
||||
f := auxWorld(t)
|
||||
mc6 := f.git("log", "--first-parent", "--format=%H", "-1", "--grep", "(#6)$", "main")
|
||||
mc7 := f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
sha := f.git("rev-parse", "origin/main")
|
||||
rec := lineRecord{Version: version, Line: "release-1.1", Report: strings.Repeat("b", 64),
|
||||
Built: &builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{NoWorkflowFiles: saved, Mode: modeContinue}},
|
||||
Deferred: []deferredEntry{{Original: 5, Files: []string{".github/workflows/release.yaml"}, Workflows: true}, {Original: 7, Commit: f.git("rev-parse", mc7+"^2"), Files: []string{"app/seven.txt"}}},
|
||||
Dropped: []droppedEntry{{Original: 6, Merge: mc6, Reason: "not for this line", On: 90, Date: "2026-10-26"}}, Stale: []int{}}
|
||||
pr := f.batchPR(90, "release-1.1", batchBranch1, "batch\n\n"+recordBlock(rec)+"\n")
|
||||
at := "2026-10-26T12:00:00Z"
|
||||
pr.State, pr.MergedAt = "closed", &at
|
||||
code, stdout, stderr := f.exec(&fakeGH{prs: []pullRequest{pr}}, "check", "--line", "release-1.1")
|
||||
if code != 1 || !strings.Contains(stdout, "/pull/5 ") || strings.Contains(stderr, "#5 is parked") {
|
||||
t.Errorf("check = %d, want #5 missing and not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
if !strings.Contains(stderr, "note: #6 is dropped") || !strings.Contains(stderr, "warning: deferred conflict on #90 (merged): https://github.com/cozystack/cozystack/pull/7 ") {
|
||||
t.Errorf("check lost the closed record's drop or deferral:\n%s", stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Withdrawing a drop puts the original back where the rules and the line
|
||||
// put it: a partial original is parked again, in the record as in check.
|
||||
func TestUndropRestoresThePark(t *testing.T) {
|
||||
for name, world := range map[string]func(*testing.T) *fixture{
|
||||
"partial": func(t *testing.T) *fixture {
|
||||
f, _ := policyWorld(t)
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
return f
|
||||
},
|
||||
"dependency": featWorld,
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
f := world(t)
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--drop", "5", "--reason", "not for 1.1", "--push")
|
||||
reopen(f, gh)
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--undrop", "5", "--push")
|
||||
reopen(f, gh)
|
||||
if rec := recordOf(t, gh); len(rec.Parked) != 1 || rec.Parked[0].Original != 5 || rec.Parked[0].Kind != name {
|
||||
t.Errorf("after the undrop the record parks %+v, want #5 parked %s", rec.Parked, name)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "note: #5 is parked") {
|
||||
t.Errorf("check = %d, want #5 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A prerequisite the open batch carries is there for what builds on it,
|
||||
// whatever its label now says: check and run agree that #7 is not parked.
|
||||
func TestPrerequisiteStillOnTheBatchSatisfies(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
gh.labels = map[string][]int{labelSkip: {5}}
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || strings.Contains(stderr, "#7 is parked") || !strings.Contains(stdout, "/pull/7 ") {
|
||||
t.Errorf("check = %d, want #7 missing, not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue"); code != 0 || !strings.Contains(stdout, `"picked":[7]`) {
|
||||
t.Errorf("run --continue = %d, want #7 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// The picker judges what its picks did where the run has got to: #4
|
||||
// deletes the file #5 also deletes, so after #4 #5 would bring only its
|
||||
// test, which the net merge at the line's head cannot see.
|
||||
func TestPartialByAnEarlierPick(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.lines()
|
||||
f.pr(4, "fix(app): drop the policy", "", map[string]string{"app/policy.yaml": "", "app/four.txt": "4\n"})
|
||||
f.pr(40, "feat(app): the policy again", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.pr(5, "fix(app): drop the policy and test it", "", map[string]string{"app/policy.yaml": "", "app/policy_test.txt": "gone\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stdout, `"picked":[4]`) || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Errorf("run = %d, want #4 picked and #5 parked as partial:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// What an earlier pick brought clears a partial park the line's head
|
||||
// gave: the line removed the policy, #4 brings it back, and #5's deletion
|
||||
// then has something to delete.
|
||||
func TestPartialClearedByAnEarlierPick(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): the line drops the policy", map[string]string{"app/policy.yaml": ""})
|
||||
f.pr(3, "feat(app): no policy", "", map[string]string{"app/policy.yaml": ""})
|
||||
f.pr(4, "fix(app): the policy, back", "", map[string]string{"app/policy.yaml": "allow: some\n"})
|
||||
f.pr(5, "fix(app): drop the policy and test it", "", map[string]string{"app/policy.yaml": "", "app/policy_test.txt": "gone\n"})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stdout, `"picked":[4,5]`) {
|
||||
t.Errorf("run = %d, want #4 and #5 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A reason a person deferred an original for comes before its partial
|
||||
// park.
|
||||
func TestReasonComesBeforeThePartialPark(t *testing.T) {
|
||||
f, _ := policyWorld(t)
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "waits for the policy rework", "--push")
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "warning: deferred on #90 to the next batch: https://github.com/cozystack/cozystack/pull/5 ") || strings.Contains(stderr, "#5 is parked") {
|
||||
t.Errorf("check = %d, want #5 deferred for its reason:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// The workflow exception holds only while what never comes is tests or
|
||||
// docs, and while the workflow lines build on nothing missing: a
|
||||
// workflow original that also fixes a file a feat added, or whose workflow
|
||||
// line another original still to come wrote, parks in the scheduled run too.
|
||||
func TestWorkflowExceptionNeedsTestsOnly(t *testing.T) {
|
||||
t.Run("a never dependency outside tests", func(t *testing.T) {
|
||||
f := auxWorld(t)
|
||||
f.pr(20, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
|
||||
f.pr(7, "fix(ci): test the tap, and fix it", "", map[string]string{".github/workflows/tap.yaml": "on: push\n", "app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1", "--no-workflow-files"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":7,`) {
|
||||
t.Errorf("scheduled run = %d, want #7 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
t.Run("a workflow line still to come", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a workflow", "", map[string]string{".github/workflows/release.yaml": "on: push\njobs: {}\n", "app/four.txt": "old\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): four, the line's way", map[string]string{"app/four.txt": "line\n"})
|
||||
f.pr(3, "feat(ci): test the changelog", "", map[string]string{"hack/changelog.bats": "@test x {\n true\n}\n"})
|
||||
f.pr(4, "fix(ci): four", "", map[string]string{"app/four.txt": "four\n", ".github/workflows/release.yaml": "on: [push]\njobs: {}\n"})
|
||||
f.pr(5, "fix(ci): run the release on tags", "", map[string]string{".github/workflows/release.yaml": "on: [push, tag]\njobs: {}\n", "hack/changelog.bats": "@test x {\n false\n}\n"})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1", "--no-workflow-files"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Errorf("scheduled run = %d, want #5 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// What a rebuild takes off the batch is no longer there for what builds on
|
||||
// it: deferring #5 makes #7, which merged after the batch was built, wait
|
||||
// on it.
|
||||
func TestRebuildTakesThePrerequisiteAway(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "fails the e2e"); code != 0 || !strings.Contains(stdout, `{"original":7,"files":[],"needs":[5]}`) {
|
||||
t.Errorf("run --defer 5 = %d, want #7 waiting on #5:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A file the line renamed the way the original does is the original's own
|
||||
// rename, not a deletion left without effect.
|
||||
func TestSameRenameOnBothSidesIsWhole(t *testing.T) {
|
||||
const long = "l01\nl02\nl03\nl04\nl05\nl06\nl07\nl08\nl09\nl10\nl11\nl12\n"
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a file", "", map[string]string{"app/p.txt": long})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): the line renames p to q", map[string]string{"app/p.txt": "", "app/q.txt": long})
|
||||
f.pr(5, "fix(app): rename p to q, and fix l10", "", map[string]string{"app/p.txt": "", "app/q.txt": strings.Replace(long, "l10", "L10", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, `"picked":[5]`) {
|
||||
t.Errorf("run = %d, want #5 picked whole:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A partial park only the picker saw, where the net merge at the line's
|
||||
// head looks whole, holds in check and in the operations that pick nothing
|
||||
// as the open record keeps it: check does not call the original missing.
|
||||
func TestPickerParkHoldsInCheck(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a policy", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.lines()
|
||||
f.pr(4, "fix(app): drop the policy", "", map[string]string{"app/policy.yaml": "", "app/four.txt": "4\n"})
|
||||
f.pr(40, "feat(app): the policy again", "", map[string]string{"app/policy.yaml": "allow: all\n"})
|
||||
f.pr(5, "fix(app): drop the policy and test it", "", map[string]string{"app/policy.yaml": "", "app/policy_test.txt": "gone\n"})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "note: #5 is parked: only part of it applies") {
|
||||
t.Errorf("check = %d, want #5 parked as the record says:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "6", "--reason", "fails the e2e"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Errorf("run --defer 6 = %d, want #5 still parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
// Deferring #4, which made #5 partial, ends the park: #5 applies whole
|
||||
// on what the batch carries now.
|
||||
f.git("switch", "-q", "main")
|
||||
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "4", "--reason", "fails the e2e", "--push")
|
||||
if code != 0 || strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `"missing":[5]`) {
|
||||
t.Errorf("run --defer 4 = %d, want #5 missing, no longer parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || strings.Contains(stderr, "#5 is parked") || !strings.Contains(stdout, "/pull/5 ") {
|
||||
t.Errorf("check after deferring #4 = %d, want #5 missing:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A workflow original that waits for a maintainer only as the open record
|
||||
// lists it: one merged since the batch was built is missing, and so is
|
||||
// what waits on it.
|
||||
func TestUnlistedWorkflowWaitIsMissing(t *testing.T) {
|
||||
f := auxWorld(t)
|
||||
gh := &fakeGH{}
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push")
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
f.pr(8, "fix(ci): run the new release on tags", "", map[string]string{".github/workflows/new.yaml": "on: tag\n", "hack/changelog.bats": "@test x {\n false\n}\n# eight\n", "app/eight.txt": appFile + "TWO\n"})
|
||||
f.pr(9, "fix(app): nine", "", map[string]string{"app/eight.txt": appFile + "TWO!\n"})
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/9 fix(app): nine (it waits on #8, which is not on the line either)") {
|
||||
t.Errorf("check = %d, want #9 missing, waiting on #8:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// The line's own rename stands for the original's only with the original's
|
||||
// change in it: a fold that leaves the renamed file as it was is refused.
|
||||
func TestSameRenameStillNeedsItsFix(t *testing.T) {
|
||||
const long = "l01\nl02\nl03\nl04\nl05\nl06\nl07\nl08\nl09\nl10\nl11\nl12\n"
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a file", "", map[string]string{"app/p.txt": long})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): the line renames p to q", map[string]string{"app/p.txt": "", "app/q.txt": long})
|
||||
mc5 := f.pr(5, "fix(app): rename p to q, fix l10, and five", "", map[string]string{"app/p.txt": "", "app/q.txt": strings.Replace(long, "l10", "L10", 1), "app/five.txt": "5\n"})
|
||||
w := emptyBatch(f, &fakeGH{}, "")
|
||||
pick := w.pick(w.head, f.git("rev-parse", mc5+"^2"), map[string]string{"app/five.txt": "5\n"})
|
||||
fold := w.merge(w.head, pick, "fix(app): rename p to q, fix l10, and five (#5)\n\n"+batchTrailer+": "+mc5)
|
||||
if code, out, stderr := f.exec(w.gh, "fold", "--line", "release-1.1", "--original", "5", "--commit", fold); code != 1 || !strings.Contains(out, `leaves "app/p.txt" without effect`) {
|
||||
t.Errorf("fold without the fix in q = %d, %q\n%s", code, out, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A prerequisite the line carries is there, though the rules would keep it
|
||||
// out: a feat carried by a hand adaptation does not park what builds on it.
|
||||
func TestCarriedExcludedPrerequisiteSatisfies(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
mc3 := f.pr(3, "feat(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.onLine("release-1.1", "feat(app): a loud two, adapted\n\n(cherry picked from commit "+mc3+")", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "Two", 1)})
|
||||
f.pr(5, "fix(app): an excited two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"parked":[{`) {
|
||||
t.Errorf("run = %d, want #5 not parked on the carried #3:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || strings.Contains(stderr, "#5 is parked") {
|
||||
t.Errorf("check = %d, want #5 missing, not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// With no batch open, a workflow wait a closed record keeps waits on
|
||||
// nothing: the original is judged as the scheduled run judges it, so one
|
||||
// that also builds on a feat the line lacks is parked, in check as in run.
|
||||
func TestClosedWorkflowWaitIsJudgedAsRunJudgesIt(t *testing.T) {
|
||||
f := auxWorld(t)
|
||||
f.pr(20, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
|
||||
f.pr(7, "fix(ci): test the tap, and fix it", "", map[string]string{".github/workflows/tap.yaml": "on: push\n", "app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
sha := f.git("rev-parse", "origin/main")
|
||||
rec := lineRecord{Version: 2, Line: "release-1.1", Report: strings.Repeat("b", 64),
|
||||
Built: &builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{NoWorkflowFiles: true, Mode: modeContinue}},
|
||||
Deferred: []deferredEntry{{Original: 7, Files: []string{".github/workflows/tap.yaml"}, Workflows: true}}, Dropped: []droppedEntry{}, Stale: []int{}}
|
||||
pr := f.batchPR(90, "release-1.1", batchBranch1, "batch\n\n"+recordBlock(rec)+"\n")
|
||||
at := "2026-10-26T12:00:00Z"
|
||||
pr.State, pr.MergedAt = "closed", &at
|
||||
gh := &fakeGH{prs: []pullRequest{pr}}
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); strings.Contains(stdout, "/pull/7 ") || !strings.Contains(stderr, "note: #7 is parked: it builds on #20") {
|
||||
t.Errorf("check --cut = %d, want #7 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files"); code != 0 || !strings.Contains(stdout, `"parked":[{"original":7,`) {
|
||||
t.Errorf("scheduled run = %d, want #7 parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A pick that comes out empty is judged too: #5's own commit did half its
|
||||
// fix, which the line has, and a merge of main into its branch did the
|
||||
// rest, which no pick carries; #5 is not on the line.
|
||||
func TestEmptyPicksStillJudged(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: b", "", map[string]string{"app/b.txt": "old\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): b, half of it", map[string]string{"app/b.txt": "middle\n"})
|
||||
f.git("switch", "-q", "-c", "pr-5", "main")
|
||||
f.apply(map[string]string{"app/b.txt": "middle\n"})
|
||||
f.git("commit", "-q", "-m", "b, half of it")
|
||||
f.git("switch", "-q", "main")
|
||||
f.apply(map[string]string{"app/main.txt": "moved\n"})
|
||||
f.git("commit", "-q", "-m", "chore: move main along")
|
||||
f.git("switch", "-q", "pr-5")
|
||||
f.git("merge", "-q", "--no-ff", "--no-commit", "main")
|
||||
f.apply(map[string]string{"app/b.txt": "fixed\n"})
|
||||
f.git("commit", "-q", "-m", "Merge branch 'main' into pr-5")
|
||||
f.git("switch", "-q", "main")
|
||||
f.git("merge", "-q", "--no-ff", "-m", "fix(app): b (#5)", "pr-5")
|
||||
f.git("push", "-q", "origin", "main")
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"on-line":[5]`) || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Errorf("run = %d, want #5 parked as partial, not on the line:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// --defer takes off a unit that does only part of its original whatever
|
||||
// rule now keeps that original out, as check tells it to.
|
||||
func TestDeferTakesOffAPartialExcludedUnit(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(3, "feat(chart): a network policy", "", map[string]string{"chart/templates/policy.yaml": "kind: NetworkPolicy\n"})
|
||||
mc5 := f.pr(5, "feat(chart): remove the policy, and test values", "", map[string]string{"chart/templates/policy.yaml": "", "chart/tests/values_test.yaml": "suite: values\n"})
|
||||
gh := &fakeGH{}
|
||||
handBatch(f, gh, mc5)
|
||||
if code, stdout, _ := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "only part of it applies") {
|
||||
t.Fatalf("check --cut = %d, want the partial unit to fail it:\n%s", code, stdout)
|
||||
}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "only part of it applies", "--push"); code != 0 || prState(gh.find(90)) != "closed" {
|
||||
t.Errorf("run --defer 5 --push = %d, #90 %s; want the unit taken off and the batch closed:\n%s", code, prState(gh.find(90)), stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A stored wait on an original merged after the one that waits cannot be
|
||||
// what a run wrote.
|
||||
func TestForwardWaitIsRefused(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "5\n"})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/seven.txt": "7\n"})
|
||||
sha := f.git("rev-parse", "origin/main")
|
||||
rec := lineRecord{Version: recordVersion, Line: "release-1.1", Report: strings.Repeat("b", 64),
|
||||
Built: &builtFrom{Main: sha, Line: sha, Policy: sha, Batch: sha, At: "2026-10-26T06:00:00Z", Options: runOptions{Mode: modeContinue}},
|
||||
Deferred: []deferredEntry{{Original: 5, Files: []string{}, Needs: []int{7}}}, Dropped: []droppedEntry{}, Stale: []int{}}
|
||||
f.git("push", "-q", "origin", "origin/release-1.1:refs/heads/"+batchBranch1)
|
||||
gh := &fakeGH{prs: []pullRequest{f.batchPR(90, "release-1.1", batchBranch1, "batch\n\n"+recordBlock(rec)+"\n")}}
|
||||
if code, _, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 2 || !strings.Contains(stderr, "#5 wait on #7") {
|
||||
t.Errorf("check = %d, want 2 for a wait on a later original:\n%s", code, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// The workflow exception looks only at what still keeps the original from
|
||||
// the line: a workflow line its prerequisite wrote, which the open batch
|
||||
// carries though that prerequisite is labelled backport/skip, does not
|
||||
// stop it, in check as in run.
|
||||
func TestWorkflowExceptionIgnoresWhatIsThere(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a workflow", "", map[string]string{".github/workflows/release.yaml": "on: push\njobs: {}\n"})
|
||||
f.lines()
|
||||
f.pr(3, "feat(ci): test the changelog", "", map[string]string{"hack/changelog.bats": "@test x {\n true\n}\n"})
|
||||
f.pr(4, "fix(ci): run the release on pushes", "", map[string]string{".github/workflows/release.yaml": "on: [push]\njobs: {}\n"})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
f.pr(7, "fix(ci): run the release on tags too", "", map[string]string{".github/workflows/release.yaml": "on: [push, tag]\njobs: {}\n", "hack/changelog.bats": "@test x {\n false\n}\n"})
|
||||
gh := &fakeGH{}
|
||||
mustRun(t, f, gh, "run", "--line", "release-1.1", "--push")
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
gh.labels = map[string][]int{labelSkip: {4}}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--continue", "--no-workflow-files", "--push"); code != 0 || !strings.Contains(stdout, `{"original":7,"files":[".github/workflows/release.yaml"],"workflows":true}`) {
|
||||
t.Fatalf("scheduled run = %d, want #7 waiting for a maintainer:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#7 changes .github/workflows/ and waits on batch PR #90") {
|
||||
t.Errorf("check --cut = %d, want #7 waiting, as run has it:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// An empty pick leaves a deletion undone that only a merge of main into the
|
||||
// original's branch made: the original is not on the line.
|
||||
func TestEmptyPickLeavesADeletionUndone(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": "old feature\n", "app/b.txt": "old\n"})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): b, already", map[string]string{"app/b.txt": "fixed\n"})
|
||||
f.git("switch", "-q", "-c", "pr-5", "main")
|
||||
f.apply(map[string]string{"app/b.txt": "fixed\n"})
|
||||
f.git("commit", "-q", "-m", "b")
|
||||
f.git("switch", "-q", "main")
|
||||
f.apply(map[string]string{"app/main.txt": "moved\n"})
|
||||
f.git("commit", "-q", "-m", "chore: move main along")
|
||||
f.git("switch", "-q", "pr-5")
|
||||
f.git("merge", "-q", "--no-ff", "--no-commit", "main")
|
||||
f.git("rm", "-q", "app/a.txt")
|
||||
f.git("commit", "-q", "-m", "Merge branch 'main' into pr-5")
|
||||
f.git("switch", "-q", "main")
|
||||
f.git("merge", "-q", "--no-ff", "-m", "fix(app): b, and a goes (#5)", "pr-5")
|
||||
f.git("push", "-q", "origin", "main")
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"on-line":[5]`) || !strings.Contains(stdout, `"parked":[{"original":5,`) {
|
||||
t.Errorf("run = %d, want #5 parked as partial, not on the line:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A file the line already has as the original makes it builds on nothing
|
||||
// the line lacks: #5 fixes a.txt, which the line fixed the same way, and
|
||||
// b.txt, so it picks whole and is not parked on the feat that wrote a.txt.
|
||||
func TestAlreadyAppliedFileBuildsOnNothing(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: a and b", "", map[string]string{"app/a.txt": appFile, "app/b.txt": "old\n"})
|
||||
f.lines()
|
||||
f.pr(3, "feat(app): a loud two", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.onLine("release-1.1", "fix(app): two, the line's way", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
f.pr(5, "fix(app): an excited two, and b", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/b.txt": "fixed\n"})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `"picked":[5]`) {
|
||||
t.Errorf("run = %d, want #5 picked, not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
@@ -176,7 +176,7 @@ func TestPolicyPageMatchesCode(t *testing.T) {
|
||||
t.Errorf("the page's block gives %d released lines, want 2 written out", pol.released)
|
||||
}
|
||||
|
||||
rule2 := regexp.MustCompile("(?m)^2\\. Its title type is (.*)$").FindStringSubmatch(page)
|
||||
rule2 := regexp.MustCompile(`(?m)^2\. Its title type is (.*)$`).FindStringSubmatch(page)
|
||||
if rule2 == nil {
|
||||
t.Fatal("the page has no rule 2")
|
||||
}
|
||||
@@ -187,7 +187,7 @@ func TestPolicyPageMatchesCode(t *testing.T) {
|
||||
if !slices.Equal(types, takenTypes) {
|
||||
t.Errorf("rule 2 on the page takes %v, the code %v", types, takenTypes)
|
||||
}
|
||||
never := regexp.MustCompile("(?m)^### What waits for the next minor\\n\\n(.*)$").FindStringSubmatch(page)
|
||||
never := regexp.MustCompile(`(?m)^### What waits for the next minor\n\n(.*)$`).FindStringSubmatch(page)
|
||||
if never == nil {
|
||||
t.Fatal("the page has no list of what never goes into a patch release")
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"maps"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -34,7 +35,8 @@ import (
|
||||
// carries a record of its own.
|
||||
|
||||
// lineRecord is the one line of JSON in the block. Version 1, the block
|
||||
// before it had a version, holds only line and deferred.
|
||||
// before it had a version, holds only line and deferred; version 3 adds what
|
||||
// is parked and which units on the batch do only part of their original.
|
||||
type lineRecord struct {
|
||||
Version int `json:"version"`
|
||||
Line string `json:"line"`
|
||||
@@ -44,6 +46,115 @@ type lineRecord struct {
|
||||
Deferred []deferredEntry `json:"deferred"`
|
||||
Dropped []droppedEntry `json:"dropped"`
|
||||
Stale []int `json:"stale"`
|
||||
Parked []parkedEntry `json:"parked,omitempty"`
|
||||
Partial []int `json:"partial,omitempty"`
|
||||
// Changed are the picks that differ from their originals.
|
||||
Changed []int `json:"changed,omitempty"`
|
||||
}
|
||||
|
||||
// recordVersion is the version a run writes.
|
||||
const recordVersion = 3
|
||||
|
||||
// lineRecordV2 is version 2, which has none of version 3's keys.
|
||||
type lineRecordV2 struct {
|
||||
Version int `json:"version"`
|
||||
Line string `json:"line"`
|
||||
Built *builtFrom `json:"built,omitempty"`
|
||||
Report string `json:"report,omitempty"`
|
||||
Closing *closing `json:"closing"`
|
||||
Deferred []deferredEntry `json:"deferred"`
|
||||
Dropped []droppedEntry `json:"dropped"`
|
||||
Stale []int `json:"stale"`
|
||||
}
|
||||
|
||||
// The kinds of park: an original that builds on what never reaches the
|
||||
// line, and one whose clean pick would leave part of it without effect.
|
||||
const (
|
||||
parkDependency = "dependency"
|
||||
parkPartial = "partial"
|
||||
)
|
||||
|
||||
// parkedEntry is an original the batch leaves out because it cannot land on
|
||||
// the line whole: what it needs that never comes, or the paths it would
|
||||
// leave without effect. announced says notify has named it on the open
|
||||
// batch, for exactly this entry.
|
||||
type parkedEntry struct {
|
||||
Original int `json:"original"`
|
||||
Merge string `json:"merge"`
|
||||
Kind string `json:"kind"`
|
||||
Needs []int `json:"needs,omitempty"`
|
||||
Commits []string `json:"commits,omitempty"`
|
||||
Files []string `json:"files,omitempty"`
|
||||
Announced bool `json:"announced,omitempty"`
|
||||
}
|
||||
|
||||
// parkEvent is what makes a park the same park from one record to the
|
||||
// next: its merge, kind, prerequisites, commits and files. A park whose
|
||||
// event changed is news, and notify names it again.
|
||||
func parkEvent(p parkedEntry) string {
|
||||
data, _ := json.Marshal(struct {
|
||||
Merge string `json:"merge"`
|
||||
Kind string `json:"kind"`
|
||||
Needs []int `json:"needs"`
|
||||
Commits []string `json:"commits"`
|
||||
Files []string `json:"files"`
|
||||
}{p.Merge, p.Kind, p.Needs, p.Commits, p.Files})
|
||||
return string(data)
|
||||
}
|
||||
|
||||
// carryAnnounced marks each park announced that the carrier's record holds
|
||||
// announced as the same event, a closed carrier's too, so a new batch does
|
||||
// not name again what an earlier one named.
|
||||
func carryAnnounced(parked []parkedEntry, carrier *lineRecord) []parkedEntry {
|
||||
out := slices.Clone(parked)
|
||||
for i := range out {
|
||||
out[i].Announced = false
|
||||
}
|
||||
if carrier == nil {
|
||||
return out
|
||||
}
|
||||
for i := range out {
|
||||
out[i].Announced = slices.ContainsFunc(carrier.Parked, func(c parkedEntry) bool {
|
||||
return c.Announced && c.Original == out[i].Original && parkEvent(c) == parkEvent(out[i])
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// validate holds a parked entry to what a run writes. Its errors read after
|
||||
// "the block" or "report <id>".
|
||||
func (p parkedEntry) validate() error {
|
||||
sortedUnique := func(n int, less func(i int) bool) bool {
|
||||
for i := 1; i < n; i++ {
|
||||
if !less(i) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
switch {
|
||||
case checkSHA(p.Merge) != nil:
|
||||
return fmt.Errorf("parks #%d by merge %q", p.Original, p.Merge)
|
||||
case !sortedUnique(len(p.Needs), func(i int) bool { return p.Needs[i-1] < p.Needs[i] }) || slices.ContainsFunc(p.Needs, func(n int) bool { return n <= 0 || n == p.Original }):
|
||||
return fmt.Errorf("parks #%d for originals %v, which are not what a run writes", p.Original, p.Needs)
|
||||
case !sortedUnique(len(p.Commits), func(i int) bool { return p.Commits[i-1] < p.Commits[i] }) || slices.ContainsFunc(p.Commits, func(c string) bool { return checkSHA(c) != nil }):
|
||||
return fmt.Errorf("parks #%d for commits %q, which are not what a run writes", p.Original, p.Commits)
|
||||
case !sortedUnique(len(p.Files), func(i int) bool { return p.Files[i-1] < p.Files[i] }) || !slices.Equal(p.Files, cleanFiles(p.Files)):
|
||||
return fmt.Errorf("parks #%d for files %q, which are not what a run writes", p.Original, p.Files)
|
||||
}
|
||||
switch p.Kind {
|
||||
case parkPartial:
|
||||
if len(p.Files) == 0 || len(p.Needs)+len(p.Commits) > 0 {
|
||||
return fmt.Errorf("parks #%d as partial with no files it leaves without effect, or with what it needs", p.Original)
|
||||
}
|
||||
case parkDependency:
|
||||
if len(p.Needs)+len(p.Commits) == 0 || len(p.Files) > 0 {
|
||||
return fmt.Errorf("parks #%d for a dependency it does not name, or with files", p.Original)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("parks #%d as %q, which is no kind of park", p.Original, p.Kind)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// builtFrom is what a classification was made of; batch is the head it
|
||||
@@ -173,9 +284,16 @@ func parseRecord(body string, l line) (*lineRecord, error) {
|
||||
err = decodeStrict(blocks[0], &v1)
|
||||
rec = lineRecord{Version: 1, Line: v1.Line, Deferred: v1.Deferred}
|
||||
case 2:
|
||||
var v2 lineRecordV2
|
||||
err = decodeStrict(blocks[0], &v2)
|
||||
rec = lineRecordWithout(v2)
|
||||
case 3:
|
||||
err = decodeStrict(blocks[0], &rec)
|
||||
default:
|
||||
return nil, fmt.Errorf("the %s block is version %d, which this backport-batch does not read; it reads 1 and 2", deferredFence, version)
|
||||
return nil, fmt.Errorf("the %s block is version %d, which this backport-batch does not read; it reads 1 to %d", deferredFence, version, recordVersion)
|
||||
}
|
||||
if err == nil && version < 3 && slices.ContainsFunc(rec.Deferred, func(d deferredEntry) bool { return d.Needs != nil }) {
|
||||
err = fmt.Errorf("version %d has no waits on other originals", version)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading the %s block: %w", deferredFence, err)
|
||||
@@ -213,7 +331,7 @@ func (rec *lineRecord) validate(l line) error {
|
||||
// A conflict names the commit that failed and its files, a deferral
|
||||
// its reason, and a wait its workflow files.
|
||||
kinds := 0
|
||||
for _, is := range []bool{d.Commit != "" || len(d.Files) > 0 && !d.Workflows && d.Reason == "", d.Reason != "", d.Workflows} {
|
||||
for _, is := range []bool{d.Commit != "" || len(d.Files) > 0 && !d.Workflows && d.Reason == "" && len(d.Needs) == 0, d.Reason != "", d.Workflows, len(d.Needs) > 0} {
|
||||
if is {
|
||||
kinds++
|
||||
}
|
||||
@@ -231,6 +349,13 @@ func (rec *lineRecord) validate(l line) error {
|
||||
return fmt.Errorf("names files of #%d that are not as a run writes them: %q", d.Original, d.Files)
|
||||
case d.More < 0:
|
||||
return fmt.Errorf("counts %d more files for #%d", d.More, d.Original)
|
||||
case len(d.Needs) > 0 && (len(d.Files) > 0 || d.More != 0):
|
||||
return fmt.Errorf("has #%d wait on other originals with files of its own", d.Original)
|
||||
}
|
||||
for i, n := range d.Needs {
|
||||
if n <= 0 || n == d.Original || i > 0 && d.Needs[i-1] >= n {
|
||||
return fmt.Errorf("has #%d wait on %v, which is not what a run writes", d.Original, d.Needs)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, d := range rec.Dropped {
|
||||
@@ -244,6 +369,14 @@ func (rec *lineRecord) validate(l line) error {
|
||||
return fmt.Errorf("drops #%d for %q, which is no reason a run writes", d.Original, d.Reason)
|
||||
}
|
||||
}
|
||||
for _, p := range rec.Parked {
|
||||
if err := once(p.Original); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
staleSeen := map[int]bool{}
|
||||
for _, n := range rec.Stale {
|
||||
if n <= 0 || staleSeen[n] {
|
||||
@@ -251,6 +384,42 @@ func (rec *lineRecord) validate(l line) error {
|
||||
}
|
||||
staleSeen[n] = true
|
||||
}
|
||||
changedSeen := map[int]bool{}
|
||||
for _, n := range rec.Changed {
|
||||
if n <= 0 || changedSeen[n] {
|
||||
return fmt.Errorf("lists %d as a changed pick more than once or not at all", n)
|
||||
}
|
||||
changedSeen[n] = true
|
||||
}
|
||||
if err := rec.waitsHold(); err != nil {
|
||||
return err
|
||||
}
|
||||
// What is on the batch is neither deferred nor parked.
|
||||
left := map[int]bool{}
|
||||
for _, d := range rec.Deferred {
|
||||
left[d.Original] = true
|
||||
}
|
||||
for _, p := range rec.Parked {
|
||||
left[p.Original] = true
|
||||
if slices.Contains(p.Commits, p.Merge) {
|
||||
return fmt.Errorf("parks #%d for its own merge", p.Original)
|
||||
}
|
||||
}
|
||||
for _, list := range [][]int{rec.Partial, rec.Changed} {
|
||||
for _, n := range list {
|
||||
if left[n] {
|
||||
return fmt.Errorf("has #%d on the batch and left off it at once", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
partialSeen := map[int]bool{}
|
||||
for _, n := range rec.Partial {
|
||||
// A unit that does only part of its original is still on the batch.
|
||||
if !staleSeen[n] || partialSeen[n] {
|
||||
return fmt.Errorf("lists #%d as a partial unit more than once, or not as still on the batch", n)
|
||||
}
|
||||
partialSeen[n] = true
|
||||
}
|
||||
if rec.Version < 2 {
|
||||
return nil
|
||||
}
|
||||
@@ -297,6 +466,79 @@ func (b *builtFrom) validate() error {
|
||||
|
||||
var runURLRE = regexp.MustCompile(`^https://[^/\s]+/[^/\s]+/[^/\s]+/actions/runs/[0-9]+(/attempts/[0-9]+)?$`)
|
||||
|
||||
// waitsHold holds what waits and parks need to what the record says of the
|
||||
// originals they need: a wait needs ones still to come, so none parked or
|
||||
// dropped, a park ones that never come, so none deferred, and no wait ends
|
||||
// up waiting on itself. Its errors read after "the block".
|
||||
func (rec *lineRecord) waitsHold() error {
|
||||
deferred, never := map[int][]int{}, map[int]bool{}
|
||||
for _, d := range rec.Deferred {
|
||||
deferred[d.Original] = d.Needs
|
||||
}
|
||||
for _, p := range rec.Parked {
|
||||
never[p.Original] = true
|
||||
}
|
||||
for _, d := range rec.Dropped {
|
||||
never[d.Original] = true
|
||||
}
|
||||
for _, d := range rec.Deferred {
|
||||
for _, n := range d.Needs {
|
||||
if never[n] {
|
||||
return fmt.Errorf("has #%d wait on #%d, which never comes", d.Original, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, p := range rec.Parked {
|
||||
for _, n := range p.Needs {
|
||||
if _, ok := deferred[n]; ok {
|
||||
return fmt.Errorf("parks #%d for #%d, which is still to come", p.Original, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
needs := map[int][]int{}
|
||||
for _, d := range rec.Deferred {
|
||||
needs[d.Original] = d.Needs
|
||||
}
|
||||
for _, p := range rec.Parked {
|
||||
needs[p.Original] = p.Needs
|
||||
}
|
||||
return waitCycle(needs)
|
||||
}
|
||||
|
||||
// waitCycle refuses waits that lead back to where they started, which no
|
||||
// run writes: a prerequisite merged before what needs it.
|
||||
func waitCycle(needs map[int][]int) error {
|
||||
state := map[int]int{} // 1 on the path, 2 done
|
||||
var visit func(n int) error
|
||||
visit = func(n int) error {
|
||||
switch state[n] {
|
||||
case 1:
|
||||
return fmt.Errorf("has #%d wait on itself through what it waits on", n)
|
||||
case 2:
|
||||
return nil
|
||||
}
|
||||
state[n] = 1
|
||||
for _, f := range needs[n] {
|
||||
if err := visit(f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
state[n] = 2
|
||||
return nil
|
||||
}
|
||||
for _, n := range slices.Sorted(maps.Keys(needs)) {
|
||||
if err := visit(n); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// lineRecordWithout is a version 2 record as the newest version holds it.
|
||||
func lineRecordWithout(v2 lineRecordV2) lineRecord {
|
||||
return lineRecord{Version: v2.Version, Line: v2.Line, Built: v2.Built, Report: v2.Report, Closing: v2.Closing, Deferred: v2.Deferred, Dropped: v2.Dropped, Stale: v2.Stale}
|
||||
}
|
||||
|
||||
// recordBlock is the block that holds rec. One line of JSON: encoding/json
|
||||
// escapes newlines and <, > and &, so nothing in a file name or a reason can
|
||||
// end the fence or open HTML.
|
||||
|
||||
@@ -18,6 +18,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -272,7 +273,7 @@ func TestRecordNamesItsInputs(t *testing.T) {
|
||||
want := builtFrom{Main: f.git("rev-parse", "origin/main"), Line: f.git("rev-parse", "origin/release-1.0"), Newer: f.git("rev-parse", "origin/release-1.1"),
|
||||
Policy: f.git("rev-parse", "origin/main"), Batch: f.remoteHead(branch), Tool: f.tool, Run: "https://github.com/cozystack/cozystack/actions/runs/123/attempts/2",
|
||||
At: "2026-10-26T06:00:00Z", Options: runOptions{NoWorkflowFiles: true, Mode: modeNew}}
|
||||
if rec.Version != 2 || *rec.Built != want || bodyReportOf(t, stdout, l).doc.Built != want || rec.Report != bodyReportOf(t, stdout, l).doc.ID {
|
||||
if rec.Version != recordVersion || *rec.Built != want || bodyReportOf(t, stdout, l).doc.Built != want || rec.Report != bodyReportOf(t, stdout, l).doc.ID {
|
||||
t.Errorf("record = %+v, built %+v; want built %+v and the report it names", rec, rec.Built, want)
|
||||
}
|
||||
|
||||
@@ -366,3 +367,67 @@ func TestPRStateNamesOnlyKnownStates(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// v3 is v2 as version 3, with extra after the stale list.
|
||||
func v3(extra string) string {
|
||||
return strings.Replace(v2(extra), `"version":2`, `"version":3`, 1)
|
||||
}
|
||||
|
||||
// Version 3 adds what is parked and which units do only part of their
|
||||
// original, each held to what a run writes, and version 2 has neither.
|
||||
func TestRecordVersion3(t *testing.T) {
|
||||
l, _ := parseLine("release-1.1")
|
||||
block := func(json string) string { return "text\n\n" + deferredFence + "\n" + json + "\n```\n" }
|
||||
sha, other := strings.Repeat("a", 40), strings.Repeat("c", 40)
|
||||
parked := `,"parked":[{"original":10,"merge":"` + sha + `","kind":"partial","files":["a","b"]},{"original":11,"merge":"` + sha + `","kind":"dependency","needs":[3,4],"commits":["` + other + `"],"announced":true}],"partial":[9]`
|
||||
waits := strings.Replace(v3(parked), `{"original":6,"files":[],"reason":"fails the e2e"}`, `{"original":6,"files":[],"needs":[3,5]}`, 1)
|
||||
rec, err := parseRecord(block(waits), l)
|
||||
if err != nil || rec.Version != 3 || len(rec.Parked) != 2 || !rec.Parked[1].Announced || !slices.Equal(rec.Partial, []int{9}) || !slices.Equal(rec.deferred()[6].Needs, []int{3, 5}) {
|
||||
t.Fatalf("parseRecord of a whole v3 record = %+v, %v", rec, err)
|
||||
}
|
||||
for name, json := range map[string]string{
|
||||
"version 1 with a wait": `{"line":"release-1.1","deferred":[{"original":6,"files":[],"needs":[3,5]}]}`,
|
||||
"a wait with files": strings.Replace(waits, `"files":[],"needs":[3,5]`, `"files":["a"],"needs":[3,5]`, 1),
|
||||
"a wait that is a conflict": strings.Replace(waits, `"files":[],"needs":[3,5]`, `"commit":"`+sha+`","files":[],"needs":[3,5]`, 1),
|
||||
"a wait on itself": strings.Replace(waits, `"needs":[3,5]`, `"needs":[3,6]`, 1),
|
||||
"an unsorted wait": strings.Replace(waits, `"needs":[3,5]`, `"needs":[5,3]`, 1),
|
||||
"a wait on nothing": strings.Replace(waits, `"needs":[3,5]`, `"needs":[0]`, 1),
|
||||
"a wait cycle": strings.Replace(strings.Replace(waits, `"needs":[3,5]`, `"needs":[12]`, 1), `"deferred":[`, `"deferred":[{"original":12,"files":[],"needs":[6]},`, 1),
|
||||
"a wait on a parked one": strings.Replace(waits, `"needs":[3,5]`, `"needs":[10]`, 1),
|
||||
"a wait on a dropped one": strings.Replace(waits, `"needs":[3,5]`, `"needs":[8]`, 1),
|
||||
"a park on a deferred one": strings.Replace(waits, `"needs":[3,4]`, `"needs":[3,5]`, 1),
|
||||
"a partial unit also deferred": strings.Replace(strings.Replace(waits, `"stale":[9]`, `"stale":[9,5]`, 1), `"partial":[9]`, `"partial":[9,5]`, 1),
|
||||
"a changed pick also parked": v3(parked + `,"changed":[10]`),
|
||||
"a changed pick also deferred": v3(parked + `,"changed":[5]`),
|
||||
"a park on its own merge": strings.Replace(waits, `"commits":["`+other+`"]`, `"commits":["`+sha+`"]`, 1),
|
||||
"a park cycle": strings.Replace(strings.Replace(waits, `"needs":[3,4]`, `"needs":[3,13]`, 1), `"parked":[`, `"parked":[{"original":13,"merge":"`+sha+`","kind":"dependency","needs":[11]},`, 1),
|
||||
} {
|
||||
if _, err := parseRecord(block(json), l); err == nil {
|
||||
t.Errorf("%s: parseRecord accepted it", name)
|
||||
}
|
||||
}
|
||||
for name, json := range map[string]string{
|
||||
"version 2 with a park": v2(parked),
|
||||
"version 4": strings.Replace(v3(""), `"version":3`, `"version":4`, 1),
|
||||
"a park of a deferred original": v3(strings.Replace(parked, `"original":10`, `"original":5`, 1)),
|
||||
"a park of a dropped original": v3(strings.Replace(parked, `"original":10`, `"original":8`, 1)),
|
||||
"a partial park with no files": v3(strings.Replace(parked, `"files":["a","b"]`, `"files":[]`, 1)),
|
||||
"a partial park that needs": v3(strings.Replace(parked, `"files":["a","b"]`, `"files":["a"],"needs":[3]`, 1)),
|
||||
"unsorted files": v3(strings.Replace(parked, `"files":["a","b"]`, `"files":["b","a"]`, 1)),
|
||||
"a file that drives a terminal": v3(strings.Replace(parked, `"files":["a","b"]`, `"files":["a\u001b[2J"]`, 1)),
|
||||
"a dependency park with no need": v3(strings.Replace(strings.Replace(parked, `"needs":[3,4],`, ``, 1), `"commits":["`+other+`"],`, ``, 1)),
|
||||
"unsorted needs": v3(strings.Replace(parked, `"needs":[3,4]`, `"needs":[4,3]`, 1)),
|
||||
"a park that needs itself": v3(strings.Replace(parked, `"needs":[3,4]`, `"needs":[3,11]`, 1)),
|
||||
"a short commit": v3(strings.Replace(parked, `"commits":["`+other+`"]`, `"commits":["abc"]`, 1)),
|
||||
"a short merge": v3(strings.Replace(parked, `"merge":"`+sha+`","kind":"partial"`, `"merge":"abc","kind":"partial"`, 1)),
|
||||
"an unknown kind": v3(strings.Replace(parked, `"kind":"partial"`, `"kind":"later"`, 1)),
|
||||
"a partial unit not on the batch": v3(strings.Replace(parked, `"partial":[9]`, `"partial":[10]`, 1)),
|
||||
"a partial unit twice": v3(strings.Replace(parked, `"partial":[9]`, `"partial":[9,9]`, 1)),
|
||||
"a changed pick twice": v3(parked + `,"changed":[4,4]`),
|
||||
"a changed pick that is none": v3(parked + `,"changed":[0]`),
|
||||
} {
|
||||
if _, err := parseRecord(block(json), l); err == nil {
|
||||
t.Errorf("%s: parseRecord accepted it", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
+177
-44
@@ -23,6 +23,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"os"
|
||||
"regexp"
|
||||
"runtime/debug"
|
||||
@@ -43,7 +44,9 @@ import (
|
||||
const (
|
||||
reportFence = "```backport-batch-report"
|
||||
reportMarker = "<!-- backport-batch:report "
|
||||
reportFormat = 1
|
||||
// reportFormat is the format a run writes. Format 2 adds parked
|
||||
// originals and partial units; format 1, which has neither, is read too.
|
||||
reportFormat = 2
|
||||
// reportLimit keeps a report comment well inside GitHub's 65536.
|
||||
reportLimit = 60000
|
||||
)
|
||||
@@ -67,11 +70,11 @@ type reportOverrides struct {
|
||||
|
||||
// reportPrimary puts every original in exactly one place.
|
||||
type reportPrimary struct {
|
||||
Picked []int `json:"picked"`
|
||||
Missing []int `json:"missing"`
|
||||
Deferred []deferredEntry `json:"deferred"`
|
||||
Parked []json.RawMessage `json:"parked"`
|
||||
Skipped reportSkipped `json:"skipped"`
|
||||
Picked []int `json:"picked"`
|
||||
Missing []int `json:"missing"`
|
||||
Deferred []deferredEntry `json:"deferred"`
|
||||
Parked []parkedEntry `json:"parked"`
|
||||
Skipped reportSkipped `json:"skipped"`
|
||||
}
|
||||
|
||||
type reportSkipped struct {
|
||||
@@ -86,12 +89,15 @@ type reportSkipped struct {
|
||||
}
|
||||
|
||||
// reportOverlays mark originals that are also something else: stale ones
|
||||
// are excluded but still on the batch, and each drop is the record's entry,
|
||||
// with the merge commit it is bound to and its reason.
|
||||
// are excluded, or applied only in part, but still on the batch, partial
|
||||
// ones those whose unit there leaves part of them without effect, and each
|
||||
// drop is the record's entry, with the merge commit it is bound to and its
|
||||
// reason.
|
||||
type reportOverlays struct {
|
||||
Dropped []droppedEntry `json:"dropped"`
|
||||
Stale []int `json:"stale"`
|
||||
Changed []int `json:"changed"`
|
||||
Partial []int `json:"partial,omitempty"`
|
||||
}
|
||||
|
||||
func (s *reportSkipped) list(rule int) *[]int {
|
||||
@@ -139,7 +145,7 @@ func numbers(vs []*verdict) []int {
|
||||
|
||||
// deferredEntries is what the record and the report say about the conflicts,
|
||||
// the deferrals and the waits of a run.
|
||||
func deferredEntries(conflicts, held, waiting []pick) []deferredEntry {
|
||||
func deferredEntries(conflicts, held, waiting, needs []pick) []deferredEntry {
|
||||
out := []deferredEntry{}
|
||||
for _, p := range conflicts {
|
||||
files := cleanFiles(p.files[:min(len(p.files), filesKept)])
|
||||
@@ -152,6 +158,21 @@ func deferredEntries(conflicts, held, waiting []pick) []deferredEntry {
|
||||
files := cleanFiles(p.files[:min(len(p.files), filesKept)])
|
||||
out = append(out, deferredEntry{Original: p.v.o.number, Files: files, More: len(p.files) - len(files) + p.more, Workflows: true})
|
||||
}
|
||||
for _, p := range needs {
|
||||
out = append(out, deferredEntry{Original: p.v.o.number, Files: []string{}, Needs: slices.Sorted(slices.Values(p.needs))})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// parkedEntries is what the record and the report say about what a run
|
||||
// parked; whether notify named it is the record's alone.
|
||||
func parkedEntries(vs []*verdict) []parkedEntry {
|
||||
out := []parkedEntry{}
|
||||
for _, v := range vs {
|
||||
p := *v.park
|
||||
p.Announced = false
|
||||
out = append(out, p)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -170,7 +191,7 @@ func buildReport(b *batch, r *report) (reportDoc, error) {
|
||||
doc.Overrides.Skip = append(doc.Overrides.Skip, v.o.number)
|
||||
}
|
||||
}
|
||||
doc.Primary = reportPrimary{Picked: numbers(r.onBranch), Missing: numbers(r.missing), Deferred: deferredEntries(r.deferred, r.held, r.waiting), Parked: []json.RawMessage{}}
|
||||
doc.Primary = reportPrimary{Picked: numbers(r.onBranch), Missing: numbers(r.missing), Deferred: deferredEntries(r.deferred, r.held, r.waiting, r.needs), Parked: parkedEntries(r.parked)}
|
||||
for _, l := range doc.Primary.Skipped.lists() {
|
||||
*l.list = []int{}
|
||||
}
|
||||
@@ -181,7 +202,15 @@ func buildReport(b *batch, r *report) (reportDoc, error) {
|
||||
}
|
||||
*list = append(*list, numbers(vs)...)
|
||||
}
|
||||
doc.Overlays = reportOverlays{Dropped: []droppedEntry{}, Stale: numbers(r.stale), Changed: []int{}}
|
||||
doc.Overlays = reportOverlays{Dropped: []droppedEntry{}, Stale: numbers(r.stale), Changed: numbers(r.changed())}
|
||||
if len(r.partial) > 0 {
|
||||
doc.Overlays.Partial = numbers(r.partial)
|
||||
}
|
||||
for _, v := range b.verdicts {
|
||||
if len(v.diag) > 0 {
|
||||
doc.Diag[strconv.Itoa(v.o.number)] = slices.Clone(v.diag)
|
||||
}
|
||||
}
|
||||
doc.Overlays.Dropped = append(doc.Overlays.Dropped, r.dropped...)
|
||||
doc.canonical()
|
||||
where, err := doc.categories()
|
||||
@@ -203,7 +232,7 @@ func buildReport(b *batch, r *report) (reportDoc, error) {
|
||||
|
||||
// canonical sorts every list, so equal classifications are equal bytes.
|
||||
func (d *reportDoc) canonical() {
|
||||
lists := []*[]int{&d.Primary.Picked, &d.Primary.Missing, &d.Overrides.Include, &d.Overrides.Skip, &d.Overlays.Stale, &d.Overlays.Changed}
|
||||
lists := []*[]int{&d.Primary.Picked, &d.Primary.Missing, &d.Overrides.Include, &d.Overrides.Skip, &d.Overlays.Stale, &d.Overlays.Changed, &d.Overlays.Partial}
|
||||
for _, l := range d.Primary.Skipped.lists() {
|
||||
lists = append(lists, l.list)
|
||||
}
|
||||
@@ -211,6 +240,7 @@ func (d *reportDoc) canonical() {
|
||||
slices.Sort(*l)
|
||||
}
|
||||
slices.SortFunc(d.Primary.Deferred, func(a, b deferredEntry) int { return cmp.Compare(a.Original, b.Original) })
|
||||
slices.SortFunc(d.Primary.Parked, func(a, b parkedEntry) int { return cmp.Compare(a.Original, b.Original) })
|
||||
slices.SortFunc(d.Overlays.Dropped, func(a, b droppedEntry) int { return cmp.Compare(a.Original, b.Original) })
|
||||
slices.Sort(d.Other)
|
||||
}
|
||||
@@ -244,6 +274,11 @@ func (d *reportDoc) categories() (map[int]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
for _, e := range d.Primary.Parked {
|
||||
if err := put(e.Original, "parked"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
for _, l := range d.Primary.Skipped.lists() {
|
||||
for _, n := range *l.list {
|
||||
if err := put(n, "skipped."+l.name); err != nil {
|
||||
@@ -285,7 +320,7 @@ func parseReport(raw string, l line) (reportDoc, error) {
|
||||
if err := decodeStrict(raw, &doc); err != nil {
|
||||
return doc, fmt.Errorf("reading the report: %w", err)
|
||||
}
|
||||
if doc.Format != reportFormat {
|
||||
if doc.Format != 1 && doc.Format != reportFormat {
|
||||
return doc, fmt.Errorf("the report is format %d, which this backport-batch does not read", doc.Format)
|
||||
}
|
||||
if !reportIDRE.MatchString(doc.ID) || reportID(doc) != doc.ID {
|
||||
@@ -333,7 +368,20 @@ func (d *reportDoc) validate(l line) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, list := range [][]int{d.Overrides.Include, d.Overrides.Skip, d.Overlays.Stale, d.Overlays.Changed} {
|
||||
for _, p := range d.Primary.Parked {
|
||||
if p.Original <= 0 {
|
||||
return fmt.Errorf("names original %d", p.Original)
|
||||
}
|
||||
if err := p.validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
// Whether notify named it is the record's to say, not the
|
||||
// classification's.
|
||||
if p.Announced {
|
||||
return fmt.Errorf("has #%d announced", p.Original)
|
||||
}
|
||||
}
|
||||
for _, list := range [][]int{d.Overrides.Include, d.Overrides.Skip, d.Overlays.Stale, d.Overlays.Changed, d.Overlays.Partial} {
|
||||
for _, n := range list {
|
||||
if n <= 0 {
|
||||
return fmt.Errorf("names original %d", n)
|
||||
@@ -351,9 +399,43 @@ func (d *reportDoc) links() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// PR B gives parked originals a shape; until then none is written.
|
||||
if len(d.Primary.Parked) > 0 {
|
||||
return errors.New("parks originals, which this backport-batch does not do")
|
||||
// Format 1 was written before anything was parked.
|
||||
if d.Format == 1 && len(d.Primary.Parked)+len(d.Overlays.Partial) > 0 {
|
||||
return errors.New("parks originals, which format 1 does not")
|
||||
}
|
||||
// What a dependency park needs never reaches the line: it is excluded
|
||||
// by a rule, a label, a drop or a park of its own, or a commit on main
|
||||
// that merged no pull request.
|
||||
other := map[string]bool{}
|
||||
for _, sha := range d.Other {
|
||||
other[sha] = true
|
||||
}
|
||||
for _, p := range d.Primary.Parked {
|
||||
for _, n := range p.Needs {
|
||||
switch c := where[n]; c {
|
||||
case "parked", "skipped.before-start", "skipped.type", "skipped.breaking", "skipped.touches", "skipped.skip-label", "skipped.dropped":
|
||||
default:
|
||||
return fmt.Errorf("parks #%d for #%d, which it has %s", p.Original, n, or(c, "nowhere"))
|
||||
}
|
||||
}
|
||||
if other[p.Merge] || slices.Contains(p.Commits, p.Merge) {
|
||||
return fmt.Errorf("parks #%d by a merge it counts as no pull request's, or for its own merge", p.Original)
|
||||
}
|
||||
for _, c := range p.Commits {
|
||||
if !other[c] {
|
||||
return fmt.Errorf("parks #%d for %.12s, which it does not count among main's commits that merged no pull request", p.Original, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
// A partial unit is still on the batch.
|
||||
stale := map[int]bool{}
|
||||
for _, n := range d.Overlays.Stale {
|
||||
stale[n] = true
|
||||
}
|
||||
for _, n := range d.Overlays.Partial {
|
||||
if !stale[n] {
|
||||
return fmt.Errorf("marks #%d partial on the batch, but not still on it", n)
|
||||
}
|
||||
}
|
||||
drops := map[int]bool{}
|
||||
for _, e := range d.Overlays.Dropped {
|
||||
@@ -364,15 +446,15 @@ func (d *reportDoc) links() error {
|
||||
return fmt.Errorf("has #%d dropped, but keeps no drop of it", n)
|
||||
}
|
||||
}
|
||||
// Stale and changed mark something a run took or excluded.
|
||||
for _, o := range []struct {
|
||||
name string
|
||||
list []int
|
||||
}{{"stale", d.Overlays.Stale}, {"changed", d.Overlays.Changed}} {
|
||||
for _, n := range o.list {
|
||||
if c := where[n]; c != "picked" && !strings.HasPrefix(c, "skipped.") {
|
||||
return fmt.Errorf("marks #%d %s, which it has %s", n, o.name, or(c, "nowhere"))
|
||||
}
|
||||
// Stale marks something a run took or excluded, and changed a pick.
|
||||
for _, n := range d.Overlays.Stale {
|
||||
if c := where[n]; c != "picked" && !strings.HasPrefix(c, "skipped.") {
|
||||
return fmt.Errorf("marks #%d stale, which it has %s", n, or(c, "nowhere"))
|
||||
}
|
||||
}
|
||||
for _, n := range d.Overlays.Changed {
|
||||
if c := where[n]; c != "picked" {
|
||||
return fmt.Errorf("marks #%d changed, which it has %s", n, or(c, "nowhere"))
|
||||
}
|
||||
}
|
||||
// backport/skip decides before any rule, and backport/include lifts
|
||||
@@ -404,6 +486,28 @@ func (d *reportDoc) links() error {
|
||||
if e.Workflows && !d.Built.Options.NoWorkflowFiles {
|
||||
return fmt.Errorf("has #%d waiting for its workflow files, but was made bringing them over", e.Original)
|
||||
}
|
||||
// What a wait waits on is still to come: not on the line, deferred,
|
||||
// or held by rule 6.
|
||||
for _, n := range e.Needs {
|
||||
switch c := where[n]; c {
|
||||
case "missing", "deferred", "skipped.newer":
|
||||
default:
|
||||
return fmt.Errorf("has #%d wait on #%d, which it has %s", e.Original, n, or(c, "nowhere"))
|
||||
}
|
||||
}
|
||||
if d.Format == 1 && len(e.Needs) > 0 {
|
||||
return fmt.Errorf("has #%d wait on other originals, which format 1 does not", e.Original)
|
||||
}
|
||||
}
|
||||
needs := map[int][]int{}
|
||||
for _, e := range d.Primary.Deferred {
|
||||
needs[e.Original] = e.Needs
|
||||
}
|
||||
for _, p := range d.Primary.Parked {
|
||||
needs[p.Original] = p.Needs
|
||||
}
|
||||
if err := waitCycle(needs); err != nil {
|
||||
return err
|
||||
}
|
||||
if d.Built.Options.Mode == modeClose && len(d.Primary.Picked)+len(d.Overlays.Stale) > 0 {
|
||||
return errors.New("closes the batch, but still has originals on it")
|
||||
@@ -604,7 +708,7 @@ func (a *app) report(args []string) int {
|
||||
return a.fail(fmt.Errorf("batch pull request #%d keeps no report %.12s", pr.Number, *id))
|
||||
}
|
||||
if *asJSON {
|
||||
fmt.Fprintln(a.stdout, s.raw)
|
||||
_, _ = fmt.Fprintln(a.stdout, s.raw)
|
||||
return 0
|
||||
}
|
||||
a.printReport(&pr, s, inBody, inComment)
|
||||
@@ -617,29 +721,29 @@ func (a *app) report(args []string) int {
|
||||
func (a *app) printReport(pr *pullRequest, s storedReport, inBody *storedReport, inComment bool) {
|
||||
d, b := s.doc, s.doc.Built
|
||||
safe := terminalSafe
|
||||
fmt.Fprintf(a.stdout, "report %s for %s, made %s by a %s run", safe(d.ID), safe(d.Line), safe(b.At), safe(b.Options.Mode))
|
||||
_, _ = fmt.Fprintf(a.stdout, "report %s for %s, made %s by a %s run", safe(d.ID), safe(d.Line), safe(b.At), safe(b.Options.Mode))
|
||||
if b.Options.NoWorkflowFiles {
|
||||
fmt.Fprint(a.stdout, " with --no-workflow-files")
|
||||
_, _ = fmt.Fprint(a.stdout, " with --no-workflow-files")
|
||||
}
|
||||
fmt.Fprintln(a.stdout)
|
||||
fmt.Fprintf(a.stdout, " main %s, line %s, newer line %s, policy %s\n batch %s, tool %s, run %s\n", safe(b.Main), safe(b.Line), safe(or(b.Newer, "none")), safe(b.Policy), safe(b.Batch), safe(or(b.Tool, "unknown")), safe(or(b.Run, "none")))
|
||||
_, _ = fmt.Fprintln(a.stdout)
|
||||
_, _ = fmt.Fprintf(a.stdout, " main %s, line %s, newer line %s, policy %s\n batch %s, tool %s, run %s\n", safe(b.Main), safe(b.Line), safe(or(b.Newer, "none")), safe(b.Policy), safe(b.Batch), safe(or(b.Tool, "unknown")), safe(or(b.Run, "none")))
|
||||
current := "none"
|
||||
if inBody != nil {
|
||||
current = safe(inBody.doc.ID)
|
||||
}
|
||||
switch {
|
||||
case inBody != nil && inBody.doc.ID == d.ID:
|
||||
fmt.Fprintf(a.stdout, " #%d's body carries this report\n", pr.Number)
|
||||
_, _ = fmt.Fprintf(a.stdout, " #%d's body carries this report\n", pr.Number)
|
||||
case pr.Head.SHA == b.Batch:
|
||||
fmt.Fprintf(a.stdout, " #%d's head is %.12s, the batch this report describes, but its body carries report %.12s\n", pr.Number, safe(b.Batch), current)
|
||||
_, _ = fmt.Fprintf(a.stdout, " #%d's head is %.12s, the batch this report describes, but its body carries report %.12s\n", pr.Number, safe(b.Batch), current)
|
||||
default:
|
||||
fmt.Fprintf(a.stdout, " publication unconfirmed: #%d's head is %.12s, not %.12s, and its body carries report %.12s\n", pr.Number, safe(pr.Head.SHA), safe(b.Batch), current)
|
||||
_, _ = fmt.Fprintf(a.stdout, " publication unconfirmed: #%d's head is %.12s, not %.12s, and its body carries report %.12s\n", pr.Number, safe(pr.Head.SHA), safe(b.Batch), current)
|
||||
}
|
||||
if !inComment {
|
||||
if prState(pr) == "open" {
|
||||
fmt.Fprintln(a.stdout, " comment not posted; the next backport-batch run --push posts it")
|
||||
_, _ = fmt.Fprintln(a.stdout, " comment not posted; the next backport-batch run --push posts it")
|
||||
} else {
|
||||
fmt.Fprintf(a.stdout, " comment not posted; kept in the body only, since nothing is written to a %s pull request\n", prState(pr))
|
||||
_, _ = fmt.Fprintf(a.stdout, " comment not posted; kept in the body only, since nothing is written to a %s pull request\n", prState(pr))
|
||||
}
|
||||
}
|
||||
type line struct {
|
||||
@@ -658,6 +762,12 @@ func (a *app) printReport(pr *pullRequest, s storedReport, inBody *storedReport,
|
||||
switch {
|
||||
case e.Workflows:
|
||||
text += "waits for a maintainer, changes " + strings.Join(e.Files, ", ")
|
||||
case len(e.Needs) > 0:
|
||||
var needs []string
|
||||
for _, n := range e.Needs {
|
||||
needs = append(needs, "#"+strconv.Itoa(n))
|
||||
}
|
||||
text += "waits on " + strings.Join(needs, ", ")
|
||||
case e.Reason != "":
|
||||
text += e.Reason
|
||||
default:
|
||||
@@ -668,6 +778,23 @@ func (a *app) printReport(pr *pullRequest, s storedReport, inBody *storedReport,
|
||||
}
|
||||
lines = append(lines, line{e.Original, text})
|
||||
}
|
||||
for _, e := range d.Primary.Parked {
|
||||
text := "parked: "
|
||||
switch e.Kind {
|
||||
case parkPartial:
|
||||
text += "only part of it applies, it would leave " + strings.Join(e.Files, ", ") + " without effect"
|
||||
default:
|
||||
var needs []string
|
||||
for _, n := range e.Needs {
|
||||
needs = append(needs, "#"+strconv.Itoa(n))
|
||||
}
|
||||
for _, c := range e.Commits {
|
||||
needs = append(needs, c[:12])
|
||||
}
|
||||
text += "builds on " + strings.Join(needs, ", ") + ", which never reaches the line"
|
||||
}
|
||||
lines = append(lines, line{e.Original, text})
|
||||
}
|
||||
for _, l := range d.Primary.Skipped.lists() {
|
||||
for _, n := range *l.list {
|
||||
lines = append(lines, line{n, "skipped." + l.name})
|
||||
@@ -675,24 +802,30 @@ func (a *app) printReport(pr *pullRequest, s storedReport, inBody *storedReport,
|
||||
}
|
||||
slices.SortFunc(lines, func(x, y line) int { return cmp.Compare(x.n, y.n) })
|
||||
for _, l := range lines {
|
||||
fmt.Fprintf(a.stdout, "#%d %s\n", l.n, terminalSafe(l.text))
|
||||
_, _ = fmt.Fprintf(a.stdout, "#%d %s\n", l.n, terminalSafe(l.text))
|
||||
}
|
||||
overlay := func(name string, list []int) {
|
||||
if len(list) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %s\n", name, strings.Trim(fmt.Sprint(list), "[]"))
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %s\n", name, strings.Trim(fmt.Sprint(list), "[]"))
|
||||
}
|
||||
}
|
||||
overlay("stale, still on the batch", d.Overlays.Stale)
|
||||
overlay("only part of it applies, still on the batch", d.Overlays.Partial)
|
||||
for _, e := range d.Overlays.Dropped {
|
||||
fmt.Fprintf(a.stdout, "dropped: #%d, merged as %.12s, on #%d: %s\n", e.Original, terminalSafe(e.Merge), e.On, terminalSafe(e.Reason))
|
||||
_, _ = fmt.Fprintf(a.stdout, "dropped: #%d, merged as %.12s, on #%d: %s\n", e.Original, terminalSafe(e.Merge), e.On, terminalSafe(e.Reason))
|
||||
}
|
||||
overlay("changed in the pick", d.Overlays.Changed)
|
||||
for _, n := range slices.Sorted(maps.Keys(d.Diag)) {
|
||||
for _, ln := range d.Diag[n] {
|
||||
_, _ = fmt.Fprintf(a.stdout, "#%s: %s\n", terminalSafe(n), terminalSafe(ln))
|
||||
}
|
||||
}
|
||||
if len(d.Other) > 0 {
|
||||
other := make([]string, len(d.Other))
|
||||
for i, sha := range d.Other {
|
||||
other[i] = safe(sha)
|
||||
}
|
||||
fmt.Fprintf(a.stdout, "commits on main that merged no pull request: %s\n", strings.Join(other, ", "))
|
||||
_, _ = fmt.Fprintf(a.stdout, "commits on main that merged no pull request: %s\n", strings.Join(other, ", "))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -717,7 +850,7 @@ func (a *app) postReport(n int, s storedReport) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(name)
|
||||
defer func() { _ = os.Remove(name) }()
|
||||
return a.gh.createComment(n, name)
|
||||
}
|
||||
|
||||
@@ -740,12 +873,12 @@ func writeTemp(pattern, content string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
if _, err := f.WriteString(content); err != nil {
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
_ = f.Close()
|
||||
_ = os.Remove(f.Name())
|
||||
return "", err
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
os.Remove(f.Name())
|
||||
_ = os.Remove(f.Name())
|
||||
return "", err
|
||||
}
|
||||
return f.Name(), nil
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -225,7 +224,9 @@ func TestReportPartition(t *testing.T) {
|
||||
}
|
||||
t.Setenv("GIT_COMMITTER_DATE", "2026-01-01T00:00:00Z")
|
||||
f.pr(4, "fix(app): four, before the start", "", map[string]string{"app/four.txt": "4\n"})
|
||||
os.Unsetenv("GIT_COMMITTER_DATE")
|
||||
if err := os.Unsetenv("GIT_COMMITTER_DATE"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.setPolicy("release-1.0: 2026-06-01\n")
|
||||
f.onLine("release-1.0", "fix(app): a line-only eleven", map[string]string{"app/eleven.txt": "line\n"})
|
||||
pr(5, "fix(app): five", map[string]string{"app/five.txt": "5\n"}, true)
|
||||
@@ -603,7 +604,63 @@ func TestReportRefusesWhatItCannotVouchFor(t *testing.T) {
|
||||
d.Primary.Deferred = []deferredEntry{{Original: 15, Files: []string{".github/workflows/ci.yaml"}, Workflows: true}}
|
||||
},
|
||||
"a close that still picks": func(d *reportDoc) { d.Built.Options.Mode = modeClose },
|
||||
"a parked original": func(d *reportDoc) { d.Primary.Parked = []json.RawMessage{json.RawMessage(`{"original":16}`)} },
|
||||
"a park with no merge": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Kind: parkPartial, Files: []string{"a"}}}
|
||||
},
|
||||
"a parked original it picks": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 5, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a"}}}
|
||||
},
|
||||
"a park of an unknown kind": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: "later", Files: []string{"a"}}}
|
||||
},
|
||||
"a partial park that needs an original": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a"}, Needs: []int{5}}}
|
||||
},
|
||||
"a partial park of unsorted files": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"b", "a"}}}
|
||||
},
|
||||
"a partial park of a file that drives a terminal": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a\x1b[2J"}}}
|
||||
},
|
||||
"a park announced in the report": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a"}, Announced: true}}
|
||||
},
|
||||
"a park for an original it picks": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{5}}}
|
||||
},
|
||||
"a park that needs itself": func(d *reportDoc) {
|
||||
d.Primary.Skipped.Type = []int{17}
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{16, 17}}}
|
||||
},
|
||||
"a park for a commit that merged a pull request": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Commits: []string{strings.Repeat("c", 40)}}}
|
||||
},
|
||||
"a partial unit not on the batch": func(d *reportDoc) { d.Overlays.Partial = []int{5} },
|
||||
"a wait on an original it picks": func(d *reportDoc) {
|
||||
d.Primary.Deferred = []deferredEntry{{Original: 6, Files: []string{}, Needs: []int{5}}}
|
||||
},
|
||||
"a wait on an original it puts nowhere": func(d *reportDoc) {
|
||||
d.Primary.Deferred = []deferredEntry{{Original: 6, Files: []string{}, Needs: []int{19}}}
|
||||
},
|
||||
"a park on its own merge as a commit": func(d *reportDoc) {
|
||||
d.Other = append(d.Other, strings.Repeat("b", 40))
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Commits: []string{strings.Repeat("b", 40)}}}
|
||||
},
|
||||
"a park cycle": func(d *reportDoc) {
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{18}}, {Original: 18, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{16}}}
|
||||
},
|
||||
"a wait cycle": func(d *reportDoc) {
|
||||
d.Primary.Deferred = []deferredEntry{{Original: 6, Files: []string{}, Needs: []int{7}}, {Original: 7, Files: []string{}, Needs: []int{6}}}
|
||||
},
|
||||
"format 1 with a wait": func(d *reportDoc) {
|
||||
d.Format = 1
|
||||
d.Primary.Missing = []int{19}
|
||||
d.Primary.Deferred = []deferredEntry{{Original: 6, Files: []string{}, Needs: []int{19}}}
|
||||
},
|
||||
"format 1 with a park": func(d *reportDoc) {
|
||||
d.Format = 1
|
||||
d.Primary.Parked = []parkedEntry{{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"a"}}}
|
||||
},
|
||||
} {
|
||||
s := forged(change)
|
||||
c, _ := reportComment(storedReport{doc: forged(func(*reportDoc) {}).doc, raw: s.raw})
|
||||
@@ -621,6 +678,22 @@ func TestReportRefusesWhatItCannotVouchFor(t *testing.T) {
|
||||
if code, stdout, stderr := reportRun(&fakeGH{prs: []pullRequest{notifyPR("20261026", "text\n\n"+reportBlock(forged(func(*reportDoc) {}).raw)+"\n")}}, "--pr", "90"); code != 0 || !strings.Contains(stdout, "#5 picked") {
|
||||
t.Errorf("report of a sound one = %d:\n%s%s", code, stdout, stderr)
|
||||
}
|
||||
// What a run wrote before anything was parked reads as it was written.
|
||||
format1 := forged(func(d *reportDoc) { d.Format, d.Primary.Parked = 1, []parkedEntry{} })
|
||||
if code, stdout, stderr := reportRun(&fakeGH{prs: []pullRequest{notifyPR("20261026", "text\n\n"+reportBlock(format1.raw)+"\n")}}, "--pr", "90"); code != 0 || !strings.Contains(stdout, "#5 picked") {
|
||||
t.Errorf("report of a sound format 1 one = %d:\n%s%s", code, stdout, stderr)
|
||||
}
|
||||
parked := forged(func(d *reportDoc) {
|
||||
d.Primary.Skipped.Type = []int{17}
|
||||
d.Other = append(d.Other, strings.Repeat("c", 40))
|
||||
d.Primary.Parked = []parkedEntry{
|
||||
{Original: 16, Merge: strings.Repeat("b", 40), Kind: parkPartial, Files: []string{"chart/templates/policy.yaml"}},
|
||||
{Original: 18, Merge: strings.Repeat("b", 40), Kind: parkDependency, Needs: []int{16, 17}, Commits: []string{strings.Repeat("c", 40)}},
|
||||
}
|
||||
})
|
||||
if code, stdout, stderr := reportRun(&fakeGH{prs: []pullRequest{notifyPR("20261026", "text\n\n"+reportBlock(parked.raw)+"\n")}}, "--pr", "90"); code != 0 || !strings.Contains(stdout, "#16 parked: only part of it applies") || !strings.Contains(stdout, "#18 parked: builds on #16, #17, cccccccccccc") {
|
||||
t.Errorf("report of sound parks = %d:\n%s%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// What report prints stays one line per field and carries nothing a
|
||||
|
||||
+115
-24
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
@@ -33,10 +34,11 @@ import (
|
||||
|
||||
// pickOriginal picks one original onto HEAD in that shape. A conflict on any
|
||||
// of its commits takes the branch back to where it was before the original,
|
||||
// so nothing of it is left half applied. Hooks are off so that each commit
|
||||
// carries its message and -x line as git writes them; signing is left to the
|
||||
// user's configuration.
|
||||
func pickOriginal(v *verdict) (pick, error) {
|
||||
// so nothing of it is left half applied, and so does a clean pick validate
|
||||
// finds leaving something of the original without effect: partial, with
|
||||
// those paths. Hooks are off so that each commit carries its message and -x
|
||||
// line as git writes them; signing is left to the user's configuration.
|
||||
func pickOriginal(v *verdict, validate func(base, head string) ([]string, error)) (pick, error) {
|
||||
p := pick{v: v}
|
||||
base, err := resolve("HEAD")
|
||||
if err != nil {
|
||||
@@ -59,10 +61,34 @@ func pickOriginal(v *verdict) (pick, error) {
|
||||
switch {
|
||||
case err != nil:
|
||||
return p, err
|
||||
case head == base && validate != nil:
|
||||
// Picks that change nothing carry the original only when the line
|
||||
// already has its changes: a change made where its branch merged
|
||||
// main is in no pick.
|
||||
omitted, err := validate(base, head)
|
||||
if err != nil {
|
||||
return p, err
|
||||
}
|
||||
if len(omitted) > 0 {
|
||||
p.status, p.files = "partial", omitted
|
||||
return p, nil
|
||||
}
|
||||
p.status = "empty"
|
||||
return p, nil
|
||||
case head == base:
|
||||
p.status = "empty"
|
||||
return p, nil
|
||||
case v.o.parents < 2:
|
||||
}
|
||||
if validate != nil {
|
||||
omitted, err := validate(base, head)
|
||||
if err == nil && len(omitted) > 0 {
|
||||
p.status, p.files = "partial", omitted
|
||||
}
|
||||
if err != nil || len(omitted) > 0 {
|
||||
return p, errors.Join(err, resetTo(base))
|
||||
}
|
||||
}
|
||||
if v.o.parents < 2 {
|
||||
p.status, p.commit = "picked", head
|
||||
return p, nil
|
||||
}
|
||||
@@ -88,6 +114,23 @@ func mergeMessage(message, merge string) string {
|
||||
return strings.TrimRight(message, " \t\r\n") + "\n\n" + batchTrailer + ": " + merge + "\n"
|
||||
}
|
||||
|
||||
// pickUnit and replayNext start the next unit only while the run's output is
|
||||
// intact: once it was lost no new unit starts, and a unit already in flight
|
||||
// still finishes its rollback.
|
||||
func (a *app) pickUnit(v *verdict, validate func(base, head string) ([]string, error)) (pick, error) {
|
||||
if err := a.mayAct(); err != nil {
|
||||
return pick{}, err
|
||||
}
|
||||
return pickOriginal(v, validate)
|
||||
}
|
||||
|
||||
func (a *app) replayNext(u unit) (string, []string, error) {
|
||||
if err := a.mayAct(); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
return replayUnit(u)
|
||||
}
|
||||
|
||||
// commitMerge makes the merge of tip onto base, whose tree is tip's since tip
|
||||
// descends from base, and moves the current branch from tip to it.
|
||||
func commitMerge(base, tip, message string) (string, error) {
|
||||
@@ -95,9 +138,9 @@ func commitMerge(base, tip, message string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
defer func() { _ = os.Remove(f.Name()) }()
|
||||
if _, err := f.WriteString(message); err != nil {
|
||||
f.Close()
|
||||
_ = f.Close()
|
||||
return "", err
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
@@ -301,7 +344,8 @@ func checkMerge(b *batch, u unit) (string, *verdict, error) {
|
||||
}
|
||||
|
||||
// contained says why the change from base to tip is not inside the files v
|
||||
// changes, or "" when it is.
|
||||
// changes, or the paths a rename on main or on the line gave them at base,
|
||||
// or "" when it is.
|
||||
func contained(b *batch, v *verdict, base, tip string) (string, error) {
|
||||
if err := b.load(v); err != nil {
|
||||
return "", err
|
||||
@@ -313,7 +357,14 @@ func contained(b *batch, v *verdict, base, tip string) (string, error) {
|
||||
if len(changes) == 0 {
|
||||
return fmt.Sprintf("%.12s changes nothing", tip), nil
|
||||
}
|
||||
corr, _, err := b.correspondence(v, base)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
allowed := paths(v.changes)
|
||||
for _, c := range corr {
|
||||
allowed = append(allowed, c)
|
||||
}
|
||||
var extra []string
|
||||
for _, p := range paths(changes) {
|
||||
if !slices.Contains(allowed, p) {
|
||||
@@ -358,15 +409,50 @@ func replayUnit(u unit) (string, []string, error) {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
// summarize says, for each original the batch head carries in units, how many
|
||||
// commits it brought and which of them differ from the commit they were picked
|
||||
// from: a resolution, or a pick git had to fit to the line.
|
||||
func summarize(b *batch, head string, on []*verdict) (map[*verdict]string, error) {
|
||||
// pickPair is a pick on the batch and the commit it was picked from.
|
||||
type pickPair struct{ source, pick string }
|
||||
|
||||
// unitShape is what an original's unit on the batch brought: how many
|
||||
// commits, the picks that differ from the commit they came from, a
|
||||
// resolution or a pick git had to fit to the line, and whether the unit as a
|
||||
// whole makes another change than the original's merge.
|
||||
type unitShape struct {
|
||||
unit string
|
||||
commits int
|
||||
pairs []pickPair
|
||||
net bool
|
||||
}
|
||||
|
||||
// changed says whether the unit differs from its original, so its release
|
||||
// note may no longer hold.
|
||||
func (s unitShape) changed() bool { return len(s.pairs) > 0 || s.net }
|
||||
|
||||
// String is the shape as the body lists it.
|
||||
func (s unitShape) String() string {
|
||||
out := "1 commit"
|
||||
if s.commits != 1 {
|
||||
out = fmt.Sprintf("%d commits", s.commits)
|
||||
}
|
||||
if len(s.pairs) > 0 {
|
||||
var picks []string
|
||||
for _, p := range s.pairs {
|
||||
picks = append(picks, codeSpan(p.pick[:12]))
|
||||
}
|
||||
out += ", changed in the pick: " + strings.Join(picks, ", ")
|
||||
} else if s.net {
|
||||
out += ", changed as a whole: some of its change is not in the picks"
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// summarize says, for each original the batch head carries in units, what
|
||||
// its unit brought.
|
||||
func summarize(b *batch, head string, on []*verdict) (map[*verdict]unitShape, error) {
|
||||
units, err := batchUnits(head, b.lineHead)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := map[*verdict]string{}
|
||||
out := map[*verdict]unitShape{}
|
||||
for _, u := range units {
|
||||
v := b.unitOriginal(u)
|
||||
if v == nil || !slices.Contains(on, v) {
|
||||
@@ -380,7 +466,7 @@ func summarize(b *batch, head string, on []*verdict) (map[*verdict]string, error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var changed []string
|
||||
s := unitShape{unit: u.commit, commits: len(picks)}
|
||||
for _, c := range picks {
|
||||
source := appendedSource(msgs[c].message)
|
||||
if source == "" {
|
||||
@@ -391,16 +477,20 @@ func summarize(b *batch, head string, on []*verdict) (map[*verdict]string, error
|
||||
return nil, err
|
||||
}
|
||||
if !same {
|
||||
changed = append(changed, codeSpan(c[:12]))
|
||||
s.pairs = append(s.pairs, pickPair{source: source, pick: c})
|
||||
}
|
||||
}
|
||||
s := "1 commit"
|
||||
if len(picks) != 1 {
|
||||
s = fmt.Sprintf("%d commits", len(picks))
|
||||
// A pick that came out empty leaves no pick to differ, only a net
|
||||
// change short of the original's.
|
||||
mine, err := patchIDRange(v.o.merge+"^1", v.o.merge)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(changed) > 0 {
|
||||
s += ", changed in the pick: " + strings.Join(changed, ", ")
|
||||
theirs, err := patchIDRange(u.parents[0], u.commit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.net = mine != theirs
|
||||
out[v] = s
|
||||
}
|
||||
return out, nil
|
||||
@@ -417,16 +507,17 @@ func plural(n int, word string) string {
|
||||
// parents, by git patch-id of their diffs without context, so that a pick
|
||||
// that only lands among other lines on the line counts as the same.
|
||||
func samePatch(a, b string) (bool, error) {
|
||||
pa, err := patchID(a)
|
||||
pa, err := patchIDRange(a+"^", a)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
pb, err := patchID(b)
|
||||
pb, err := patchIDRange(b+"^", b)
|
||||
return pa == pb, err
|
||||
}
|
||||
|
||||
func patchID(c string) (string, error) {
|
||||
diff, err := git("diff", "--no-ext-diff", "--no-textconv", "--unified=0", "--end-of-options", c+"^", c)
|
||||
// patchIDRange is the patch-id of the change from one revision to another.
|
||||
func patchIDRange(from, to string) (string, error) {
|
||||
diff, err := git("diff", "--no-ext-diff", "--no-textconv", "--unified=0", "--end-of-options", from, to)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ func (a *app) verify(args []string) int {
|
||||
return a.fail(err)
|
||||
}
|
||||
if pr == nil {
|
||||
fmt.Fprintf(a.stdout, "%s: no batch pull request is open; nothing to verify\n", l.branch())
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: no batch pull request is open; nothing to verify\n", l.branch())
|
||||
return 0
|
||||
}
|
||||
if head, err = batchHead(pr); err != nil {
|
||||
@@ -91,9 +91,18 @@ func (a *app) verify(args []string) int {
|
||||
if why == "" && seen[v] != "" {
|
||||
why = fmt.Sprintf("carries #%d again, which %.12s already carries", v.o.number, seen[v])
|
||||
}
|
||||
if why == "" {
|
||||
omitted, _, err := b.omittedEffects(v, u.parents[0], u.commit)
|
||||
if err != nil {
|
||||
return a.fail(err)
|
||||
}
|
||||
if len(omitted) > 0 {
|
||||
why = leftOut(v, l, omitted)
|
||||
}
|
||||
}
|
||||
if why != "" {
|
||||
refused = append(refused, why)
|
||||
fmt.Fprintf(a.stdout, "refused %.12s: %s\n", u.commit, why)
|
||||
_, _ = fmt.Fprintf(a.stdout, "refused %.12s: %s\n", u.commit, why)
|
||||
continue
|
||||
}
|
||||
seen[v] = u.commit
|
||||
@@ -101,13 +110,13 @@ func (a *app) verify(args []string) int {
|
||||
if !u.merge() {
|
||||
n = 1
|
||||
}
|
||||
fmt.Fprintf(a.stdout, "ok %.12s #%d, %d %s\n", u.commit, v.o.number, n, plural(n, "commit"))
|
||||
_, _ = fmt.Fprintf(a.stdout, "ok %.12s #%d, %d %s\n", u.commit, v.o.number, n, plural(n, "commit"))
|
||||
}
|
||||
if len(refused) > 0 {
|
||||
fmt.Fprintf(a.stdout, "%s: %d of %d units at %.12s are not originals in the batch's shape\n", l.branch(), len(refused), len(units), head)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d of %d units at %.12s are not originals in the batch's shape\n", l.branch(), len(refused), len(units), head)
|
||||
return 1
|
||||
}
|
||||
fmt.Fprintf(a.stdout, "%s: %d %s at %.12s, each one original in the batch's shape\n", l.branch(), len(units), plural(len(units), "unit"), head)
|
||||
_, _ = fmt.Fprintf(a.stdout, "%s: %d %s at %.12s, each one original in the batch's shape\n", l.branch(), len(units), plural(len(units), "unit"), head)
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// chainWorld has #5 conflict on the line and change a shared line, and #7
|
||||
// change that line again: #7 builds on #5.
|
||||
func chainWorld(t *testing.T) (*fixture, string) {
|
||||
t.Helper()
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: five and shared", "", map[string]string{"app/five.txt": "old\n", "app/shared.txt": appFile})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): five, the line's way", map[string]string{"app/five.txt": "line\n"})
|
||||
mc5 := f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "five\n", "app/shared.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/shared.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
return f, mc5
|
||||
}
|
||||
|
||||
// An original that builds on a deferred conflict waits for it: check warns,
|
||||
// notify lists it after the conflict, and once the conflict lands, the next
|
||||
// run picks it.
|
||||
func TestWaitForADeferredDependency(t *testing.T) {
|
||||
f, mc5 := chainWorld(t)
|
||||
gh := &fakeGH{}
|
||||
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push")
|
||||
if code != 0 {
|
||||
t.Fatalf("run --push = %d\n%s", code, stderr)
|
||||
}
|
||||
if !strings.Contains(stdout, `{"original":7,"files":[],"needs":[5]}`) || !strings.Contains(stdout, "### Waiting on another original") {
|
||||
t.Errorf("#7 does not wait on #5:\n%s", stdout)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
code, stdout, stderr = f.exec(gh, "check", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stderr, "warning: #7 waits on #5, a deferred conflict on #90: https://github.com/cozystack/cozystack/pull/7 ") {
|
||||
t.Errorf("check = %d, want a warning that #7 waits on #5:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
code, stdout, _ = f.exec(gh, "notify", "--line", "release-1.1", "--push")
|
||||
if code != 0 || !strings.Contains(gh.issueComments[len(gh.issueComments)-1].Body, "- [#7](https://github.com/cozystack/cozystack/pull/7), after [#5](https://github.com/cozystack/cozystack/pull/5)") {
|
||||
t.Errorf("notify = %d, want #7 listed after #5:\n%s\n%v", code, stdout, gh.issueComments)
|
||||
}
|
||||
// The batch merges, and #5 lands on the line by hand, resolved: the
|
||||
// next batch picks #7.
|
||||
mergeBatch(f, gh)
|
||||
f.onLine("release-1.1", "fix(app): five (#5)\n\n(cherry picked from commit "+mc5+")", map[string]string{"app/five.txt": "five\n", "app/shared.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
code, stdout, stderr = f.exec(gh, "run", "--line", "release-1.1", "--continue")
|
||||
if code != 0 || !strings.Contains(stdout, `"picked":[7]`) {
|
||||
t.Errorf("run --continue after #5 landed = %d, want #7 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// Of several prerequisites still to come, the strongest decides: one not on
|
||||
// the line at all, then one waiting for a maintainer, then a conflict or a
|
||||
// deferral, then one rule 6 holds; a chain inherits what its end decides.
|
||||
func TestWaitReduction(t *testing.T) {
|
||||
t.Run("a conflict and a workflow wait", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/five.txt": "old\n", "app/a.txt": appFile, "app/b.txt": appFile})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): five, the line's way", map[string]string{"app/five.txt": "line\n"})
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "five\n", "app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(6, "fix(ci): six", "", map[string]string{".github/workflows/six.yaml": "on: push\n", "app/b.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/b.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
f.pr(8, "fix(app): eight", "", map[string]string{"app/eight.txt": "8\n"})
|
||||
gh := &fakeGH{}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 || !strings.Contains(stdout, `{"original":7,"files":[],"needs":[6]}`) {
|
||||
t.Fatalf("run = %d, want #7 waiting on #6 alone:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#7 waits on #6, which changes .github/workflows/ and waits on batch PR #90 for a maintainer") {
|
||||
t.Errorf("check --cut = %d, want #7 to hold the cut:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
if code, stdout, _ := f.exec(gh, "notify", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, "2 originals on #90 change .github/workflows/ or wait on one that does") {
|
||||
t.Errorf("notify = %d, want #7 among the waits:\n%s", code, stdout)
|
||||
}
|
||||
})
|
||||
t.Run("a conflict and one not on the line", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/five.txt": "old\n", "app/a.txt": appFile, "app/b.txt": appFile})
|
||||
f.lines()
|
||||
f.onLine("release-1.1", "fix(app): five, the line's way", map[string]string{"app/five.txt": "line\n"})
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/five.txt": "five\n", "app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(6, "fix(app): six", "", map[string]string{"app/six.txt": "6\n"})
|
||||
gh := &fakeGH{}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
f.pr(9, "fix(app): nine", "", map[string]string{"app/b.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(10, "fix(app): ten", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/b.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/10 fix(app): ten (it waits on #9, which is not on the line either)") {
|
||||
t.Errorf("check = %d, want #10 to fail it, waiting on #9:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
t.Run("a prerequisite on the open batch", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
gh := &fakeGH{}
|
||||
if code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--push"); code != 0 || !strings.Contains(stdout, `"picked":[5,7]`) {
|
||||
t.Fatalf("run = %d, want #5 and #7 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1", "--cut"); code != 1 || !strings.Contains(stdout, "#5 is only on open batch PR #90") || strings.Contains(stdout+stderr, "waits on") {
|
||||
t.Errorf("check --cut = %d, want #5 and #7 only on the batch and no wait:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
t.Run("a direct reason", func(t *testing.T) {
|
||||
f := featWorld(t)
|
||||
gh := &fakeGH{}
|
||||
openBatch(t, f, gh)
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "needs the tap", "--push"); code != 0 {
|
||||
t.Fatalf("run --defer = %d\n%s", code, stderr)
|
||||
}
|
||||
reopen(f, gh)
|
||||
if code, stdout, stderr := f.exec(gh, "check", "--line", "release-1.1"); code != 0 || !strings.Contains(stderr, "warning: deferred on #90 to the next batch: https://github.com/cozystack/cozystack/pull/5 ") || strings.Contains(stderr, "parked") {
|
||||
t.Errorf("check = %d, want #5 deferred for its reason, not parked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
t.Run("a prerequisite rule 6 holds", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
mc7 := f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/seven.txt": "7\n"})
|
||||
f.onLine("release-1.1", "fix(app): seven (#7)\n\n(cherry picked from commit "+mc7+")", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1), "app/seven.txt": "7\n"})
|
||||
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.0"); code != 0 || !strings.Contains(stderr, "note: #7 waits on #5, which is not on release-1.1 yet") {
|
||||
t.Errorf("check of release-1.0 = %d, want a note that #7 waits on #5:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
t.Run("a chain", func(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(4, "fix(app): four", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
f.pr(5, "fix(app): five", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!", 1)})
|
||||
f.pr(7, "fix(app): seven", "", map[string]string{"app/a.txt": strings.Replace(appFile, "two", "TWO!!", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "check", "--line", "release-1.1"); code != 1 || !strings.Contains(stdout, "/pull/7 fix(app): seven (it waits on #5, which is not on the line either)") {
|
||||
t.Errorf("check = %d, want #7 failing it through #5:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// run's report, check and notify give an original one disposition: a
|
||||
// workflow original whose only missing dependency is a test waits for a
|
||||
// maintainer, and one deferred for a reason stays deferred, though it
|
||||
// builds on what never comes; neither is parked.
|
||||
func TestDispositionOrderIsSharedByRunCheckNotify(t *testing.T) {
|
||||
f := auxWorld(t)
|
||||
f.pr(20, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
|
||||
f.pr(7, "fix(app): fix the tap", "", map[string]string{"app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
gh := &fakeGH{}
|
||||
if code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--no-workflow-files", "--push"); code != 0 {
|
||||
t.Fatalf("run = %d\n%s", code, stderr)
|
||||
}
|
||||
f.git("switch", "-q", "main")
|
||||
reopen(f, gh)
|
||||
code, stdout, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "7", "--reason", "waits for the tap", "--push")
|
||||
if code != 0 {
|
||||
t.Fatalf("run --defer = %d\n%s", code, stderr)
|
||||
}
|
||||
if strings.Contains(stdout, `"parked":[{`) || !strings.Contains(stdout, `{"original":5,"files":[".github/workflows/release.yaml"],"workflows":true}`) || !strings.Contains(stdout, `{"original":7,"files":[],"reason":"waits for the tap"}`) {
|
||||
t.Errorf("the report does not have #5 waiting and #7 deferred, unparked:\n%s", stdout)
|
||||
}
|
||||
reopen(f, gh)
|
||||
code, stdout, stderr = f.exec(gh, "check", "--line", "release-1.1")
|
||||
if code != 0 || strings.Contains(stderr, "parked") || !strings.Contains(stderr, "warning: waiting on #90 for a maintainer, since it changes .github/workflows/: https://github.com/cozystack/cozystack/pull/5 ") || !strings.Contains(stderr, "warning: deferred on #90 to the next batch: https://github.com/cozystack/cozystack/pull/7 ") {
|
||||
t.Errorf("check = %d, want #5 waiting and #7 deferred:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
code, stdout, _ = f.exec(gh, "notify", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stdout, "1 originals on #90 change .github/workflows/") || strings.Contains(stdout, "parked") || strings.Contains(stdout, "conflicts") {
|
||||
t.Errorf("notify = %d, want #5 waiting and nothing for #7:\n%s", code, stdout)
|
||||
}
|
||||
}
|
||||
|
||||
// A never and a pending prerequisite together park.
|
||||
func TestParkWinsOverWait(t *testing.T) {
|
||||
f, _ := chainWorld(t)
|
||||
f.pr(20, "feat(app): tap", "", map[string]string{"app/tap.go": appFile})
|
||||
f.pr(8, "fix(app): eight", "", map[string]string{"app/shared.txt": strings.Replace(appFile, "two", "TWO!!", 1), "app/tap.go": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1")
|
||||
if code != 0 || !strings.Contains(stdout, `"kind":"dependency","needs":[20]`) || strings.Contains(stdout, `"original":8,"files":[],"needs"`) {
|
||||
t.Errorf("run = %d, want #8 parked on #20, not waiting on #7:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
|
||||
// A prerequisite picked earlier in the same batch satisfies what builds on
|
||||
// it, and so does one whose pick came out empty because an earlier pick
|
||||
// already carried it: #4280 and #3938.
|
||||
func TestDependencySatisfiedEarlierInTheBatch(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(3, "fix(kafka): delete hooks", "", map[string]string{"app/hooks.yaml": appFile})
|
||||
f.pr(4, "fix(kafka): fix the delete hook", "", map[string]string{"app/hooks.yaml": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, `"picked":[3,4]`) {
|
||||
t.Errorf("run = %d, want #3 and #4 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
|
||||
// #30 adds back the file #2 brought and a feat took away, so its pick
|
||||
// comes out empty after #2's, and #4 builds on #30.
|
||||
f = newFixture(t)
|
||||
f.squash(2, "chore: files", "", map[string]string{"app/a.txt": appFile})
|
||||
f.lines()
|
||||
f.pr(20, "fix(kafka): delete hooks, and two", "", map[string]string{"app/hooks.yaml": appFile, "app/two.txt": "2\n"})
|
||||
f.pr(21, "feat(kafka): no delete hooks", "", map[string]string{"app/hooks.yaml": ""})
|
||||
f.pr(30, "fix(kafka): delete hooks again", "", map[string]string{"app/hooks.yaml": appFile})
|
||||
f.pr(4, "fix(kafka): fix the delete hook", "", map[string]string{"app/hooks.yaml": strings.Replace(appFile, "two", "TWO", 1)})
|
||||
if code, stdout, stderr := f.exec(nil, "run", "--line", "release-1.1"); code != 0 || !strings.Contains(stdout, `"picked":[4,20]`) || strings.Contains(stdout, `"needs"`) {
|
||||
t.Errorf("run with #30's pick empty = %d, want #4 picked:\n%s\n%s", code, stdout, stderr)
|
||||
}
|
||||
}
|
||||
+40
-15
@@ -22,6 +22,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
@@ -58,6 +59,10 @@ Options:
|
||||
--no-color Disable color output (auto-disabled on non-TTY)
|
||||
-h, --help Show this help
|
||||
|
||||
Exit status: 0 when everything is ready, 1 when something is not ready or a
|
||||
write to stdout fails, 2 on invalid arguments. A closed pipe ends the process
|
||||
with SIGPIPE, like other Unix tools.
|
||||
|
||||
`
|
||||
|
||||
// entry is a resource catalog item.
|
||||
@@ -181,10 +186,16 @@ func run(args []string) int {
|
||||
cfg.coreOnly = true
|
||||
i++
|
||||
case "-h", "--help":
|
||||
fmt.Fprint(os.Stdout, usageText)
|
||||
if _, err := fmt.Fprint(os.Stdout, usageText); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "writing usage: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case "--version":
|
||||
fmt.Fprintln(os.Stdout, Version)
|
||||
if _, err := fmt.Fprintln(os.Stdout, Version); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "writing version: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "Unknown argument: %s\n", a)
|
||||
@@ -213,14 +224,14 @@ func run(args []string) int {
|
||||
cfg.timeoutS = sec
|
||||
|
||||
if cfg.waitMode {
|
||||
return cfg.runWait()
|
||||
return cfg.runWait(os.Stdout)
|
||||
}
|
||||
if cfg.watch {
|
||||
cfg.runWatch()
|
||||
return 0
|
||||
return cfg.runWatch(os.Stdout)
|
||||
}
|
||||
if writeOutput(os.Stdout, cfg.runOnce()) != nil {
|
||||
return 1
|
||||
}
|
||||
out := cfg.runOnce()
|
||||
fmt.Print(out)
|
||||
if cfg.foundIssues {
|
||||
return 1
|
||||
}
|
||||
@@ -281,18 +292,31 @@ func parseTimeout(raw string) (int, bool) {
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (cfg *config) runWait() int {
|
||||
// writeOutput prints a result. A result its reader never got fails the run,
|
||||
// whatever the resources' state, since the exit code alone would vouch for it.
|
||||
func writeOutput(w io.Writer, parts ...string) error {
|
||||
for _, part := range parts {
|
||||
if _, err := io.WriteString(w, part); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "writing output: %v\n", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *config) runWait(w io.Writer) int {
|
||||
start := time.Now()
|
||||
for {
|
||||
output := cfg.runOnce()
|
||||
elapsed := int(time.Since(start).Seconds())
|
||||
if strings.Contains(output, "All resources are ready.") {
|
||||
fmt.Print(output)
|
||||
fmt.Printf("%sReady after %ds.%s\n", cfg.green, elapsed, cfg.reset)
|
||||
if writeOutput(w, output, fmt.Sprintf("%sReady after %ds.%s\n", cfg.green, elapsed, cfg.reset)) != nil {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
if elapsed >= cfg.timeoutS {
|
||||
fmt.Print(output)
|
||||
_ = writeOutput(w, output)
|
||||
fmt.Fprintf(os.Stderr, "%s%sTimeout after %ds — resources still not ready.%s\n", cfg.red, cfg.bold, cfg.timeoutS, cfg.reset)
|
||||
return 1
|
||||
}
|
||||
@@ -300,13 +324,14 @@ func (cfg *config) runWait() int {
|
||||
}
|
||||
}
|
||||
|
||||
func (cfg *config) runWatch() {
|
||||
func (cfg *config) runWatch(w io.Writer) int {
|
||||
for {
|
||||
output := cfg.runOnce()
|
||||
// Smooth clear is cosmetic and untested; just reprint.
|
||||
fmt.Printf("%sLast updated: %s (refreshing every %ds, Ctrl+C to stop)%s\n", cfg.bold, time.Now().Format(time.UnixDate), cfg.interval, cfg.reset)
|
||||
fmt.Println()
|
||||
fmt.Print(output)
|
||||
header := fmt.Sprintf("%sLast updated: %s (refreshing every %ds, Ctrl+C to stop)%s\n\n", cfg.bold, time.Now().Format(time.UnixDate), cfg.interval, cfg.reset)
|
||||
if writeOutput(w, header, output) != nil {
|
||||
return 1
|
||||
}
|
||||
time.Sleep(time.Duration(cfg.interval) * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHelpAndVersionSucceedOnWritableStdout(t *testing.T) {
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = r.Close() })
|
||||
stdout := os.Stdout
|
||||
os.Stdout = w
|
||||
t.Cleanup(func() { os.Stdout = stdout })
|
||||
read := make(chan string, 1)
|
||||
go func() {
|
||||
b, _ := io.ReadAll(r)
|
||||
read <- string(b)
|
||||
}()
|
||||
|
||||
for _, arg := range []string{"--help", "--version"} {
|
||||
if code := run([]string{arg}); code != 0 {
|
||||
t.Errorf("run(%s) = %d, want 0", arg, code)
|
||||
}
|
||||
}
|
||||
_ = w.Close()
|
||||
if out := <-read; !strings.Contains(out, "Usage:") || !strings.Contains(out, Version) {
|
||||
t.Errorf("stdout = %q, want the usage text and the version", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHelpAndVersionFailOnUnwritableStdout(t *testing.T) {
|
||||
r, w, err := os.Pipe()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = r.Close()
|
||||
_ = w.Close()
|
||||
stdout := os.Stdout
|
||||
os.Stdout = w
|
||||
t.Cleanup(func() { os.Stdout = stdout })
|
||||
|
||||
for _, arg := range []string{"--help", "--version"} {
|
||||
if code := run([]string{arg}); code == 0 {
|
||||
t.Errorf("run(%s) with an unwritable stdout = 0, want a failure", arg)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -390,7 +390,7 @@ func createPackageFromFile(ctx context.Context, k8sClient client.Client, filePat
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("Package %s not found in file", packageName)
|
||||
return fmt.Errorf("no Package %s in file", packageName)
|
||||
}
|
||||
|
||||
func installPackage(ctx context.Context, k8sClient client.Client, packageSourceName string) error {
|
||||
|
||||
+27
-21
@@ -19,7 +19,7 @@ package cmd
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"io"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
@@ -78,24 +78,25 @@ Use --components flag to show components on separate lines.`,
|
||||
}
|
||||
|
||||
if listCmdFlags.installed {
|
||||
return listPackages(ctx, k8sClient, listCmdFlags.components)
|
||||
return listPackages(ctx, k8sClient, cmd.OutOrStdout(), listCmdFlags.components)
|
||||
}
|
||||
return listPackageSources(ctx, k8sClient, listCmdFlags.components)
|
||||
return listPackageSources(ctx, k8sClient, cmd.OutOrStdout(), listCmdFlags.components)
|
||||
},
|
||||
}
|
||||
|
||||
func listPackageSources(ctx context.Context, k8sClient client.Client, showComponents bool) error {
|
||||
func listPackageSources(ctx context.Context, k8sClient client.Client, out io.Writer, showComponents bool) error {
|
||||
var psList cozyv1alpha1.PackageSourceList
|
||||
if err := k8sClient.List(ctx, &psList); err != nil {
|
||||
return fmt.Errorf("failed to list PackageSources: %w", err)
|
||||
}
|
||||
|
||||
// Use tabwriter for better column alignment
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
|
||||
defer w.Flush()
|
||||
w := tabwriter.NewWriter(out, 0, 0, 3, ' ', 0)
|
||||
|
||||
// Print header
|
||||
fmt.Fprintln(w, "NAME\tVARIANTS\tREADY\tSTATUS")
|
||||
if _, err := fmt.Fprintln(w, "NAME\tVARIANTS\tREADY\tSTATUS"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Print rows
|
||||
for _, ps := range psList.Items {
|
||||
@@ -123,24 +124,26 @@ func listPackageSources(ctx context.Context, k8sClient client.Client, showCompon
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", ps.Name, variantsStr, ready, status)
|
||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", ps.Name, variantsStr, ready, status); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Show components if requested
|
||||
if showComponents {
|
||||
for _, variant := range ps.Spec.Variants {
|
||||
for _, component := range variant.Components {
|
||||
fmt.Fprintf(w, " %s\t%s\t\t\n",
|
||||
fmt.Sprintf("%s.%s", ps.Name, component.Name),
|
||||
variant.Name)
|
||||
if _, err := fmt.Fprintf(w, " %s.%s\t%s\t\t\n", ps.Name, component.Name, variant.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return w.Flush()
|
||||
}
|
||||
|
||||
func listPackages(ctx context.Context, k8sClient client.Client, showComponents bool) error {
|
||||
func listPackages(ctx context.Context, k8sClient client.Client, out io.Writer, showComponents bool) error {
|
||||
var pkgList cozyv1alpha1.PackageList
|
||||
if err := k8sClient.List(ctx, &pkgList); err != nil {
|
||||
return fmt.Errorf("failed to list Packages: %w", err)
|
||||
@@ -160,11 +163,12 @@ func listPackages(ctx context.Context, k8sClient client.Client, showComponents b
|
||||
}
|
||||
|
||||
// Use tabwriter for better column alignment
|
||||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
|
||||
defer w.Flush()
|
||||
w := tabwriter.NewWriter(out, 0, 0, 3, ' ', 0)
|
||||
|
||||
// Print header
|
||||
fmt.Fprintln(w, "NAME\tVARIANT\tREADY\tSTATUS")
|
||||
if _, err := fmt.Fprintln(w, "NAME\tVARIANT\tREADY\tSTATUS"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Print rows
|
||||
for _, pkg := range pkgList.Items {
|
||||
@@ -187,7 +191,9 @@ func listPackages(ctx context.Context, k8sClient client.Client, showComponents b
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", pkg.Name, variant, ready, status)
|
||||
if _, err := fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", pkg.Name, variant, ready, status); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Show components if requested
|
||||
if showComponents {
|
||||
@@ -197,9 +203,9 @@ func listPackages(ctx context.Context, k8sClient client.Client, showComponents b
|
||||
for _, v := range ps.Spec.Variants {
|
||||
if v.Name == variant {
|
||||
for _, component := range v.Components {
|
||||
fmt.Fprintf(w, " %s\t%s\t\t\n",
|
||||
fmt.Sprintf("%s.%s", pkg.Name, component.Name),
|
||||
variant)
|
||||
if _, err := fmt.Fprintf(w, " %s.%s\t%s\t\t\n", pkg.Name, component.Name, variant); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -208,7 +214,7 @@ func listPackages(ctx context.Context, k8sClient client.Client, showComponents b
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return w.Flush()
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
Copyright 2026 The Cozystack Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
||||
"github.com/spf13/cobra"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
)
|
||||
|
||||
type failingWriter struct{}
|
||||
|
||||
func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("broken pipe") }
|
||||
|
||||
func TestPrintReportReturnsWriteError(t *testing.T) {
|
||||
r := &Report{Findings: []Finding{{Severity: SeverityError, Code: "X", Message: "m"}}}
|
||||
if err := printReport(r, failingWriter{}); err == nil {
|
||||
t.Fatal("printReport to an unwritable writer returned nil")
|
||||
}
|
||||
}
|
||||
|
||||
// The table is buffered until the tabwriter flushes, so a failing reader shows
|
||||
// up only as a Flush error, which list has to return.
|
||||
func TestListReturnsWriteError(t *testing.T) {
|
||||
s := runtime.NewScheme()
|
||||
utilruntime.Must(cozyv1alpha1.AddToScheme(s))
|
||||
ps := &cozyv1alpha1.PackageSource{ObjectMeta: metav1.ObjectMeta{Name: "acme.app"}}
|
||||
pkg := &cozyv1alpha1.Package{ObjectMeta: metav1.ObjectMeta{Name: "acme.app"}}
|
||||
cl := fake.NewClientBuilder().WithScheme(s).WithObjects(ps, pkg).Build()
|
||||
|
||||
for name, list := range map[string]func(io.Writer) error{
|
||||
"sources": func(w io.Writer) error { return listPackageSources(context.Background(), cl, w, true) },
|
||||
"packages": func(w io.Writer) error { return listPackages(context.Background(), cl, w, true) },
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
if err := list(failingWriter{}); err == nil {
|
||||
t.Fatal("list to an unwritable writer returned nil")
|
||||
}
|
||||
var out strings.Builder
|
||||
if err := list(&out); err != nil || !strings.Contains(out.String(), "acme.app") {
|
||||
t.Fatalf("list = %v, output %q; want the row", err, out.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A command that cannot print its validation report still fails, and a
|
||||
// refusal keeps validation as its reason with the write error joined to it.
|
||||
func TestValidationCommandsReportUnwritableReport(t *testing.T) {
|
||||
fakeFlux := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(fakeFlux, "flux"), []byte("#!/bin/sh\nexit 0\n"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PATH", fakeFlux+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
|
||||
emptyRepo := func(t *testing.T) string {
|
||||
root := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(root, "packages"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return root
|
||||
}
|
||||
run := func(c *cobra.Command, args ...string) error {
|
||||
c.SetOut(failingWriter{})
|
||||
t.Cleanup(func() { c.SetOut(nil) })
|
||||
return c.RunE(c, args)
|
||||
}
|
||||
push := func(root string) error {
|
||||
pushCmdFlags.path = root
|
||||
t.Cleanup(func() { pushCmdFlags.path = "." })
|
||||
return run(pushCmd, "oci://registry.example/acme/apps:v1")
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
run func(t *testing.T) error
|
||||
refusal string
|
||||
}{
|
||||
{name: "validate, valid", run: func(t *testing.T) error { return run(validateCmd, validRepo(t)) }},
|
||||
{name: "validate, invalid", run: func(t *testing.T) error { return run(validateCmd, emptyRepo(t)) }, refusal: "validation failed"},
|
||||
{name: "push, valid", run: func(t *testing.T) error { return push(validRepo(t)) }, refusal: "nothing was pushed"},
|
||||
{name: "push, invalid", run: func(t *testing.T) error { return push(emptyRepo(t)) }, refusal: "refusing to push"},
|
||||
{name: "tap, invalid", run: func(t *testing.T) error { return run(tapCmd, "oci://registry.example/acme/apps:v1") }, refusal: "refusing to tap"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.run(t)
|
||||
if err == nil || !strings.Contains(err.Error(), "broken pipe") {
|
||||
t.Fatalf("err = %v, want the write error", err)
|
||||
}
|
||||
if tc.refusal != "" && !strings.Contains(err.Error(), tc.refusal) {
|
||||
t.Errorf("err = %v, want the refusal %q kept", err, tc.refusal)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A tap that applied is reported as applied even when its summary is lost,
|
||||
// and the lost summary still fails the command.
|
||||
func TestTapSummaryReportsUnwritableOutput(t *testing.T) {
|
||||
err := writeTapSummary(failingWriter{}, "oci://registry.example/acme/apps:v1", "tap-acme", []string{"acme.app"})
|
||||
if err == nil || !strings.Contains(err.Error(), "tap applied") || !strings.Contains(err.Error(), "broken pipe") {
|
||||
t.Fatalf("err = %v, want the write error with the tap reported as applied", err)
|
||||
}
|
||||
var out strings.Builder
|
||||
if err := writeTapSummary(&out, "oci://registry.example/acme/apps:v1", "tap-acme", []string{"acme.app"}); err != nil || !strings.Contains(out.String(), "cozypkg add acme.app") {
|
||||
t.Fatalf("writeTapSummary = %v, output %q", err, out.String())
|
||||
}
|
||||
}
|
||||
|
||||
// A clean report has no finding lines, so its summary is its only write and
|
||||
// alone decides the result.
|
||||
func TestPrintReportReturnsSummaryWriteError(t *testing.T) {
|
||||
if err := printReport(&Report{}, failingWriter{}); err == nil {
|
||||
t.Fatal("printReport of a clean report to an unwritable writer returned nil")
|
||||
}
|
||||
}
|
||||
|
||||
// push runs flux only after its status line was written, with validation
|
||||
// skipped and after a report that was written.
|
||||
func TestPushStopsBeforeFluxWhenItsStatusIsLost(t *testing.T) {
|
||||
fakeFlux := t.TempDir()
|
||||
ran := filepath.Join(t.TempDir(), "flux-ran")
|
||||
if err := os.WriteFile(filepath.Join(fakeFlux, "flux"), []byte("#!/bin/sh\ntouch "+ran+"\n"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PATH", fakeFlux+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
for _, skip := range []bool{true, false} {
|
||||
t.Run(fmt.Sprintf("skip-validate=%v", skip), func(t *testing.T) {
|
||||
pushCmdFlags.path, pushCmdFlags.skipValidate = validRepo(t), skip
|
||||
pushCmdFlags.source, pushCmdFlags.revision = "https://example.com/acme.git", "v1"
|
||||
t.Cleanup(func() {
|
||||
pushCmdFlags.path, pushCmdFlags.skipValidate = ".", false
|
||||
pushCmdFlags.source, pushCmdFlags.revision = "", ""
|
||||
})
|
||||
pushCmd.SetOut(&failOn{want: "Pushing"})
|
||||
t.Cleanup(func() { pushCmd.SetOut(nil) })
|
||||
err := pushCmd.RunE(pushCmd, []string{"oci://registry.example/acme/apps:v1"})
|
||||
if err == nil || !strings.Contains(err.Error(), "nothing was pushed") {
|
||||
t.Fatalf("push losing its status = %v, want nothing pushed", err)
|
||||
}
|
||||
if _, statErr := os.Stat(ran); statErr == nil {
|
||||
t.Fatal("flux ran after the status line was lost")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// failOn fails every write that contains want, and takes every other one.
|
||||
type failOn struct {
|
||||
want string
|
||||
buf strings.Builder
|
||||
}
|
||||
|
||||
func (w *failOn) Write(p []byte) (int, error) {
|
||||
if strings.Contains(string(p), w.want) {
|
||||
return 0, errors.New("broken pipe")
|
||||
}
|
||||
return w.buf.Write(p)
|
||||
}
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
@@ -106,10 +107,13 @@ published. Source URL and revision are derived from git when not given.`,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printReport(rep, cmd.OutOrStdout())
|
||||
printErr := printReport(rep, cmd.OutOrStdout())
|
||||
if rep.HasErrors() {
|
||||
errs, _, _ := rep.Counts()
|
||||
return fmt.Errorf("refusing to push: validation failed with %d error(s) (use --skip-validate to override)", errs)
|
||||
return errors.Join(fmt.Errorf("refusing to push: validation failed with %d error(s) (use --skip-validate to override)", errs), printErr)
|
||||
}
|
||||
if printErr != nil {
|
||||
return fmt.Errorf("nothing was pushed: writing the validation report failed: %w", printErr)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,7 +133,9 @@ published. Source URL and revision are derived from git when not given.`,
|
||||
}
|
||||
|
||||
fluxArgs := buildFluxPushArgs(ref, packagesDir, source, revision, pushCmdFlags.reproducible)
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "Pushing %s (source=%s revision=%s)\n", ref, source, revision)
|
||||
if _, err := fmt.Fprintf(cmd.OutOrStdout(), "Pushing %s (source=%s revision=%s)\n", ref, source, revision); err != nil {
|
||||
return fmt.Errorf("nothing was pushed: writing the push status failed: %w", err)
|
||||
}
|
||||
flux := exec.Command("flux", fluxArgs...)
|
||||
flux.Stdout = cmd.OutOrStdout()
|
||||
flux.Stderr = cmd.ErrOrStderr()
|
||||
|
||||
+22
-12
@@ -18,6 +18,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"regexp"
|
||||
@@ -402,9 +403,8 @@ runs its charts in your management cluster: tap only sources you trust.`,
|
||||
return err
|
||||
}
|
||||
if rep.HasErrors() {
|
||||
printReport(rep, cmd.OutOrStdout())
|
||||
errs, _, _ := rep.Counts()
|
||||
return fmt.Errorf("refusing to tap: artifact failed validation with %d error(s) (use --skip-validate to override)", errs)
|
||||
return errors.Join(fmt.Errorf("refusing to tap: artifact failed validation with %d error(s) (use --skip-validate to override)", errs), printReport(rep, cmd.OutOrStdout()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -487,19 +487,29 @@ runs its charts in your management cluster: tap only sources you trust.`,
|
||||
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "warning: could not reset materialization state on OCIRepository/%s: %v\n", srcName, err)
|
||||
}
|
||||
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "Tapped %s\n OCIRepository/%s\n", fullRef, srcName)
|
||||
for _, n := range names {
|
||||
fmt.Fprintf(cmd.OutOrStdout(), " PackageSource/%s\n", n)
|
||||
}
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "Tapping installs nothing. Install an app with:\n")
|
||||
for _, n := range names {
|
||||
fmt.Fprintf(cmd.OutOrStdout(), " cozypkg add %s\n", n)
|
||||
}
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "A privileged install component is not installed until you confirm it with 'cozypkg add --allow-privileged'.\n")
|
||||
return nil
|
||||
return writeTapSummary(cmd.OutOrStdout(), fullRef, srcName, names)
|
||||
},
|
||||
}
|
||||
|
||||
// writeTapSummary reports what a tap created. The tap is applied by then, so a
|
||||
// summary that cannot be written fails the command while saying so.
|
||||
func writeTapSummary(w io.Writer, fullRef, srcName string, names []string) error {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "Tapped %s\n OCIRepository/%s\n", fullRef, srcName)
|
||||
for _, n := range names {
|
||||
fmt.Fprintf(&b, " PackageSource/%s\n", n)
|
||||
}
|
||||
b.WriteString("Tapping installs nothing. Install an app with:\n")
|
||||
for _, n := range names {
|
||||
fmt.Fprintf(&b, " cozypkg add %s\n", n)
|
||||
}
|
||||
b.WriteString("A privileged install component is not installed until you confirm it with 'cozypkg add --allow-privileged'.\n")
|
||||
if _, err := io.WriteString(w, b.String()); err != nil {
|
||||
return fmt.Errorf("tap applied, but writing its summary failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var untapCmd = &cobra.Command{
|
||||
Use: "untap <packagesource-name>",
|
||||
Short: "Remove a tapped repository",
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
@@ -535,16 +536,19 @@ func pullOCIArtifact(ref string) (string, func(), error) {
|
||||
return dir, cleanup, nil
|
||||
}
|
||||
|
||||
func printReport(r *Report, w io.Writer) {
|
||||
func printReport(r *Report, w io.Writer) error {
|
||||
for _, f := range r.Findings {
|
||||
loc := f.Location
|
||||
if loc == "" {
|
||||
loc = "-"
|
||||
}
|
||||
fmt.Fprintf(w, "[%s] %s (%s): %s\n", f.Severity, loc, f.Code, f.Message)
|
||||
if _, err := fmt.Fprintf(w, "[%s] %s (%s): %s\n", f.Severity, loc, f.Code, f.Message); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
errs, warns, infos := r.Counts()
|
||||
fmt.Fprintf(w, "\nSummary: %d error(s), %d warning(s), %d info\n", errs, warns, infos)
|
||||
_, err := fmt.Fprintf(w, "\nSummary: %d error(s), %d warning(s), %d info\n", errs, warns, infos)
|
||||
return err
|
||||
}
|
||||
|
||||
var validateCmdFlags struct {
|
||||
@@ -663,10 +667,13 @@ runs "helm lint" on every component chart.`,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printReport(rep, cmd.OutOrStdout())
|
||||
printErr := printReport(rep, cmd.OutOrStdout())
|
||||
if rep.HasErrors() {
|
||||
errs, _, _ := rep.Counts()
|
||||
return fmt.Errorf("validation failed: %d error(s)", errs)
|
||||
return errors.Join(fmt.Errorf("validation failed: %d error(s)", errs), printErr)
|
||||
}
|
||||
if printErr != nil {
|
||||
return fmt.Errorf("writing the validation report failed: %w", printErr)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
||||
@@ -663,7 +663,7 @@ func validateGitRef(refMap map[string]string) error {
|
||||
return fmt.Errorf("commit SHA should be at least 7 characters, got: %s", commit)
|
||||
}
|
||||
for _, c := range commit {
|
||||
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
|
||||
if (c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F') {
|
||||
return fmt.Errorf("commit SHA should be a hexadecimal string, got: %s", commit)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,9 +14,10 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// Command observability-proxy serves tenant logs from the VictoriaLogs
|
||||
// stores behind a Kubernetes TokenReview and SubjectAccessReview, with the
|
||||
// tenant's namespace scope forced into every upstream request.
|
||||
// Command observability-proxy serves tenant logs and metrics from the
|
||||
// VictoriaLogs and VictoriaMetrics stores behind a Kubernetes TokenReview
|
||||
// and SubjectAccessReview, with the tenant's namespace scope forced into
|
||||
// every upstream request.
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -37,6 +38,7 @@ import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
@@ -56,6 +58,7 @@ type options struct {
|
||||
tailIdleTimeout time.Duration
|
||||
shutdownTimeout time.Duration
|
||||
logs observabilityproxy.LogsLimits
|
||||
metrics observabilityproxy.MetricsConfig
|
||||
limiter observabilityproxy.LimiterConfig
|
||||
}
|
||||
|
||||
@@ -81,6 +84,14 @@ func parseFlags(args []string) (options, error) {
|
||||
fs.IntVar(&o.logs.DefaultLimit, "logs-default-limit", 1000, "entries returned when a request sets no limit")
|
||||
fs.IntVar(&o.logs.MaxLimit, "logs-max-limit", 10000, "largest limit a request may set")
|
||||
fs.DurationVar(&o.logs.MaxTailResume, "logs-max-tail-resume", 15*time.Minute, "how far back a tail may resume with since or start_offset")
|
||||
// Store names and retention follow metricsStorages in
|
||||
// packages/system/monitoring/values.yaml.
|
||||
fs.StringVar(&o.metrics.ShortTermStore, "metrics-shortterm-store", "shortterm", "VMCluster name of the short-term metrics store of the root tenant")
|
||||
fs.StringVar(&o.metrics.LongTermStore, "metrics-longterm-store", "longterm", "VMCluster name of the long-term metrics store of the root tenant")
|
||||
fs.DurationVar(&o.metrics.ShortTermRetention, "metrics-shortterm-retention", 72*time.Hour, "retention of the short-term store; older windows are read from the long-term one")
|
||||
fs.DurationVar(&o.metrics.Limits.MaxRange, "metrics-max-range", 30*24*time.Hour, "longest [start, end] and step a metrics request may use")
|
||||
fs.IntVar(&o.metrics.Limits.MaxPoints, "metrics-max-points", 11000, "most points per series a range query may return")
|
||||
fs.DurationVar(&o.metrics.Limits.MaxTimeout, "metrics-max-timeout", 30*time.Second, "timeout forwarded to vmselect; a longer client timeout is clamped; must be below --request-timeout when that is set")
|
||||
fs.IntVar(&o.limiter.MaxConcurrentPerUser, "max-concurrent-per-user", 10, "concurrent non-tail requests per user; 0 disables")
|
||||
fs.Float64Var(&o.limiter.RatePerUser, "rate-limit-per-user", 10, "sustained requests per second per user; 0 disables")
|
||||
fs.IntVar(&o.limiter.RateBurst, "rate-limit-burst", 20, "request burst per user")
|
||||
@@ -94,6 +105,13 @@ func parseFlags(args []string) (options, error) {
|
||||
if fs.NArg() > 0 {
|
||||
return o, fmt.Errorf("unexpected argument %q", fs.Arg(0))
|
||||
}
|
||||
o.metrics.RootTenant = o.rootTenant
|
||||
for _, store := range []string{o.metrics.ShortTermStore, o.metrics.LongTermStore} {
|
||||
// The name becomes part of a Service host name.
|
||||
if errs := validation.IsDNS1123Label("vmselect-" + store); store == "" || len(errs) > 0 {
|
||||
return o, fmt.Errorf("invalid metrics store name %q", store)
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case o.rootTenant == "":
|
||||
return o, errors.New("--root-tenant must not be empty")
|
||||
@@ -110,8 +128,17 @@ func parseFlags(args []string) (options, error) {
|
||||
return o, errors.New("--logs-max-range must be at least 3s")
|
||||
case o.logs.DefaultLimit <= 0 || o.logs.MaxLimit < o.logs.DefaultLimit:
|
||||
return o, errors.New("--logs-default-limit must be positive and not above --logs-max-limit")
|
||||
case o.metrics.ShortTermRetention <= 0 || o.metrics.Limits.MaxRange <= 0 || o.metrics.Limits.MaxTimeout <= 0:
|
||||
return o, errors.New("--metrics-shortterm-retention, --metrics-max-range and --metrics-max-timeout must be positive")
|
||||
case o.metrics.Limits.MaxPoints < 2:
|
||||
return o, errors.New("--metrics-max-points must be at least 2")
|
||||
case o.logs.MaxTailResume < 0:
|
||||
return o, errors.New("--logs-max-tail-resume must not be negative")
|
||||
// The proxy's deadline starts before the request reaches vmselect, so
|
||||
// at an equal or longer timeout it cuts a query vmselect is still
|
||||
// allowed to run and answers 504 for a store that is within its time.
|
||||
case o.requestTimeout > 0 && o.metrics.Limits.MaxTimeout >= o.requestTimeout:
|
||||
return o, errors.New("--metrics-max-timeout must be below --request-timeout")
|
||||
case o.requestTimeout < 0 || o.tailIdleTimeout < 0 || o.authCacheTTL < 0 || o.shutdownTimeout < 0:
|
||||
return o, errors.New("timeouts and the auth cache TTL must not be negative")
|
||||
// NaN passes every comparison and +Inf never empties a bucket; either
|
||||
@@ -167,6 +194,7 @@ func run(o options) error {
|
||||
proxy := observabilityproxy.NewServer(observabilityproxy.Config{
|
||||
ClusterDomain: o.clusterDomain,
|
||||
Logs: o.logs,
|
||||
Metrics: o.metrics,
|
||||
Limiter: o.limiter,
|
||||
RequestTimeout: o.requestTimeout,
|
||||
TailIdleTimeout: o.tailIdleTimeout,
|
||||
|
||||
@@ -32,6 +32,17 @@ func TestParseFlagsDefaults(t *testing.T) {
|
||||
if o.authCacheTTL != 10*time.Second || o.logs.MaxTailResume != 15*time.Minute || o.logs.MaxRange != 7*24*time.Hour {
|
||||
t.Errorf("limit defaults changed: %+v", o)
|
||||
}
|
||||
// Retention and store names of packages/system/monitoring/values.yaml.
|
||||
m := o.metrics
|
||||
if m.ShortTermStore != "shortterm" || m.LongTermStore != "longterm" || m.ShortTermRetention != 72*time.Hour {
|
||||
t.Errorf("metrics store defaults changed: %+v", m)
|
||||
}
|
||||
if m.Limits.MaxRange != 30*24*time.Hour || m.Limits.MaxPoints != 11000 || m.Limits.MaxTimeout != 30*time.Second {
|
||||
t.Errorf("metrics limit defaults changed: %+v", m.Limits)
|
||||
}
|
||||
if m.RootTenant != o.rootTenant {
|
||||
t.Errorf("metrics root tenant = %q, want --root-tenant", m.RootTenant)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsOverrides(t *testing.T) {
|
||||
@@ -48,6 +59,21 @@ func TestParseFlagsOverrides(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsMetricsOverrides(t *testing.T) {
|
||||
o, err := parseFlags([]string{
|
||||
"--root-tenant=tenant-x", "--metrics-shortterm-store=fast", "--metrics-longterm-store=slow",
|
||||
"--metrics-shortterm-retention=24h", "--metrics-max-range=168h", "--metrics-max-points=500", "--metrics-max-timeout=10s",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := o.metrics
|
||||
if m.RootTenant != "tenant-x" || m.ShortTermStore != "fast" || m.LongTermStore != "slow" || m.ShortTermRetention != 24*time.Hour ||
|
||||
m.Limits.MaxRange != 168*time.Hour || m.Limits.MaxPoints != 500 || m.Limits.MaxTimeout != 10*time.Second {
|
||||
t.Errorf("overrides not applied: %+v", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsInvalid(t *testing.T) {
|
||||
for _, args := range [][]string{
|
||||
{"--root-tenant="},
|
||||
@@ -69,7 +95,17 @@ func TestParseFlagsInvalid(t *testing.T) {
|
||||
{"--max-concurrent-per-user=-1"},
|
||||
{"--max-tails-per-user=-1"},
|
||||
{"--max-tails=-1"},
|
||||
{"--metrics-shortterm-store="},
|
||||
{"--metrics-longterm-store=Long.Term"},
|
||||
{"--metrics-shortterm-retention=0s"},
|
||||
{"--metrics-max-range=0s"},
|
||||
{"--metrics-max-points=1"},
|
||||
{"--metrics-max-timeout=0s"},
|
||||
{"--unknown"},
|
||||
// The proxy would cut the request while vmselect is still within
|
||||
// the timeout it was given.
|
||||
{"--request-timeout=10s", "--metrics-max-timeout=30s"},
|
||||
{"--request-timeout=30s", "--metrics-max-timeout=30s"},
|
||||
{"unexpected", "--root-tenant=tenant-x"},
|
||||
} {
|
||||
if _, err := parseFlags(args); err == nil {
|
||||
@@ -77,3 +113,16 @@ func TestParseFlagsInvalid(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The vmselect timeout must stay strictly below a request timeout, and is
|
||||
// free when the request has none.
|
||||
func TestParseFlagsTimeoutPairs(t *testing.T) {
|
||||
for _, args := range [][]string{
|
||||
{"--request-timeout=31s", "--metrics-max-timeout=30s"},
|
||||
{"--request-timeout=0", "--metrics-max-timeout=10m"},
|
||||
} {
|
||||
if _, err := parseFlags(args); err != nil {
|
||||
t.Errorf("parseFlags(%v) refused: %v", args, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -134,7 +134,7 @@ git log v0.37.1..v0.37.2 --pretty=format:"%h - %s (%an, %ar)"
|
||||
```
|
||||
|
||||
**⚠️ IMPORTANT: Check for backports.** They reach a release line in two shapes:
|
||||
- A backport batch ([`docs/release/backports.md`](../release/backports.md)): a merge commit titled `fix(backport): batch for release-X.Y, <date> (#BATCH_PR)`. Inside it each original is one merge commit that carries the original's merge message from `main`, `Merge pull request #ORIGINAL_PR from …` or `<original title> (#ORIGINAL_PR)`, and ends in a `Backported-from: <sha>` trailer, with the original's own commits, picked with `-x`, on its second parent; an original squash-merged into `main` is one commit with its subject, `<original title> (#ORIGINAL_PR)`, and a `(cherry picked from commit <sha>)` line. Read the original's number, title and author from those, list the batch with `git log --first-parent` inside it, and never write an entry for the batch PR itself or for a picked commit of an original.
|
||||
- A backport batch ([`docs/release/backports.md`](../release/backports.md)): a merge commit titled `fix(backport): batch for release-X.Y, <date> (#BATCH_PR)`. Inside it each original is one merge commit that carries the original's merge message from `main`, `Merge pull request #ORIGINAL_PR from …` or `<original title> (#ORIGINAL_PR)`, and ends in a `Backported-from: <sha>` trailer, with the original's own commits, picked with `-x`, on its second parent; an original squash-merged into `main` is one commit with its subject, `<original title> (#ORIGINAL_PR)`, and a `(cherry picked from commit <sha>)` line. Read the original's number, title and author from those, list the batch with `git log --first-parent` inside it, and never write an entry for the batch PR itself or for a picked commit of an original. An original whose pick differs from it is listed under "Check these notes" in the batch merge's message, which carries the batch PR's body, and in its record's `changed` list: write its entry from what its pick changed on the line, read with the `git range-diff` and `git diff` commands listed there, not from the original's release note.
|
||||
- A single backport PR, from before the batch (the retired label-driven bot) or a release-only fix: commits with "[Backport release-X.Y]" in the message, whose original PR number is in the backport commit message or PR description.
|
||||
- Use the original PR author (not the backport or batch PR author) when creating changelog entries
|
||||
- **MUST** combine the original and backport into a **single entry** with both PR numbers (e.g. `#1606, backport #1609`, where a batch's original reads `#1606, backport #BATCH_PR`). Do NOT list the original and the backport as two separate entries — that produces duplicates in the changelog.
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
<!--
|
||||
https://github.com/cozystack/cozystack/releases/tag/v1.7.1
|
||||
-->
|
||||
|
||||
# Cozystack v1.7.1
|
||||
|
||||
Cozystack v1.7.1 is a patch release on the v1.7 line. It fixes the COSI driver bug that could leave freshly granted S3 keys rejected for good, makes marketplace taps digest-pinned, makes MariaDB, PostgreSQL and ClickHouse backups report their real outcome, and repairs several tenant-facing issues: tenant cluster metrics, SecurityGroups on Kubernetes workers, Redis with a single replica, the Proxmox cloud-controller-manager and the tcp-balancer. Read the Migration and Upgrades section before upgrading.
|
||||
|
||||
## Fixes
|
||||
|
||||
* **SeaweedFS COSI driver v0.4.0**: bucket users granted together could get S3 keys that never work (`403 InvalidAccessKeyId`) because the v0.3.1 driver rewrote a legacy IAM file the S3 gateways were migrating. The driver is bumped to v0.4.0, which writes each grant to a per-identity IAM file. Keys already lost on running clusters stay broken: remove the user from the Bucket, wait until its BucketAccess is gone, and add it back ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #5284, backport #5290).
|
||||
* **Marketplace taps are pinned to a digest**: a tapped repository no longer follows a mutable tag, so moving the tag upstream cannot change what runs in the cluster. `tap connect` accepts `oci://...@sha256:...` references and an optional keyless cosign verify policy, anchored to an exact identity and preserved across re-connects; with the verify flags the CLI verifies the signature and pins the verified digest at tap time. Community index entries can record a digest too ([**@IvanHunters**](https://github.com/IvanHunters) in #5082, backport #5246).
|
||||
* **Tenant cluster metrics reach the tenant's Monitoring**: the vmagent of tenant Kubernetes clusters now writes to the tenant's vminsert and labels series with the owning tenant instead of `cozy-monitoring`; before, no tenant cluster metric reached the tenant's Monitoring ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5024, backport #5246).
|
||||
* **SecurityGroups apply to Kubernetes worker nodes**: the virt-launcher pods of Kubernetes worker nodes are now resolved to their Kubernetes application and marked as managed by Cozystack, so a SecurityGroup attached to a Kubernetes cluster applies to its workers, not only to its control plane ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5124, backport #5246).
|
||||
* **Membership-only SecurityGroups**: a SecurityGroup with attachments and no rules no longer leaves an invalid CiliumNetworkPolicy behind; its backing policy is accepted by Cilium and still allows or denies nothing ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5133, backport #5246).
|
||||
* **Proxmox CCM and CSI images**: the Proxmox cloud-controller-manager runs v0.16.1 and the Proxmox CCM and CSI driver images are pinned by digest. The v0.15.0 tag was re-pushed upstream to a build that panics on start, which left every new Proxmox worker with the uninitialized taint ([**@themoriarti**](https://github.com/themoriarti) in #4917, backport #5246).
|
||||
* **Autoscaler capacity of KubernetesNodes pools**: on the kubevirt substrate the cluster-autoscaler capacity annotation is planned on the cores the VM actually gets, so a leading-zero `resources.cpu` such as `"010"` (decoded as YAML, eight cores) no longer yields a wrong capacity. No pool rolls ([**@themoriarti**](https://github.com/themoriarti) in #4974, backport #5246).
|
||||
* **Tenant quota admission counts extended resources**: admission now reads the parent pool's usage of extended resources and hugepages under the `requests.*` key the controller counts, so a child tenant can no longer get a share the parent has already used ([**@lexfrei**](https://github.com/lexfrei) in #5184, backport #5246).
|
||||
* **Postgres backup jobs fail as soon as the CloudNativePG Backup fails**: a Postgres `BackupJob` now fails with the recorded error instead of after 30 minutes, and reports a `WaitingForCNPGBackup` condition with the Backup's phase while it waits ([**@scooby87**](https://github.com/scooby87) in #5211, backport #5246).
|
||||
* **MariaDB backups and restores report real failures**: a `BackupJob` or `RestoreJob` whose operator Job failed now ends Failed instead of Succeeded, and a failed backup no longer creates a Ready Backup without an archive ([**@scooby87**](https://github.com/scooby87) in #5207, backport #5246).
|
||||
* **ClickHouse restores use the right archive**: a `RestoreJob` restores the archive its Backup was taken as, not the newest one. New Backups record the archive name; Backups taken before this release are restored best effort from the newest archive stamped no later than their `takenAt`, with a warning in the restore pod log. A ClickHouse `BackupJob` created under a name whose Backup still exists now fails instead of reporting that Backup as its result ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #5208, backport #5246).
|
||||
* **Backup S3 endpoint changes are followed**: the backup controller re-renders the Velero BSL, the default backup strategies and the platform bucket's credentials Secret when their S3 endpoint is stale, so after the platform bucket's BucketAccess is re-granted for a new `publishing.host`, backups move to the new endpoint without forcing releases by hand ([**@scooby87**](https://github.com/scooby87) in #5230, backport #5246).
|
||||
* **Backup clients run on a dedicated image**: the ClickHouse (Altinity) and RabbitMQ backup strategy Pods now run a small `backup-http-client` image instead of the 349 MB `platform-migrations` image, so a platform migration change no longer re-digests the backup clients. Air-gapped mirrors need the new `ghcr.io/cozystack/cozystack/backup-http-client` repository ([**@androndo**](https://github.com/androndo) in #4756, backport #5246).
|
||||
* **PostgreSQL system databases**: `databases` refuses `postgres`, `template0` and `template1`, and the init job never runs `DROP DATABASE` on them, which used to fail the init job on every upgrade and leave the HelmRelease in `UpgradeFailed` ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5155, backport #5246).
|
||||
* **PostgreSQL init script is stable**: the declared databases render in a stable order, so an upgrade that leaves the databases unchanged no longer rewrites the init-script Secret ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5091, backport #5246).
|
||||
* **Redis with a single replica**: an instance with `replicas: 1` runs one Sentinel instead of three, without a Sentinel PodDisruptionBudget, so node drains are no longer blocked by it; instances with two replicas or more are unchanged ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5151, backport #5246).
|
||||
* **MariaDB operator on a custom cluster domain**: the operator uses the platform's `networking.clusterDomain` instead of a hard-coded `cozy.local`, so replication and User/Database/Grant reconciliation work on clusters with a non-default domain ([**@mattia-eleuteri**](https://github.com/mattia-eleuteri) in #5123, backport #5246).
|
||||
* **tcp-balancer no longer OOMs**: HAProxy takes its fd and thread caps from the replica's own memory and CPU limits instead of the node, so a `t1.nano` balancer is no longer OOMKilled on nodes with many CPUs ([**@yankawai**](https://github.com/yankawai) in #5005, backport #5246).
|
||||
* **etcd alert noise**: `etcdHighNumberOfFailedGRPCRequests` counts only the gRPC codes the etcd mixin counts, so the Canceled watch streams of a stopping Kubernetes API server no longer fire it on every member ([**@yankawai**](https://github.com/yankawai) in #4985, backport #5246).
|
||||
* **Cilium IPAM hook on larger clusters**: the hook passes the CiliumNode list to `jq` on stdin and no longer fails with "Argument list too long" on clusters with more than a few nodes, which blocked the cilium upgrade and every package depending on networking ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #5212, backport #5246).
|
||||
* **OIDC bootstrap Job with many users**: the Job no longer fails to start for a tenant cluster with about 240 or more `oidc.users`; the bindings now come from a ConfigMap the Job mounts ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #5213, backport #5246).
|
||||
* **Dashboard tenant list links to the tenant page**: the tenant name in the tenant list opens its detail page, where Delete lives, and top-level tenants get their Edit button back ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #4004, backport #5246).
|
||||
* **Dead `registries` platform value removed**: the platform chart no longer has the `registries` value or the `patch-containerd` Secret, which nothing read. Mirrors for tenant workers are the per-pool `talos.registryMirrors` of `KubernetesNodes` ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #5190, backport #5246).
|
||||
|
||||
## Security
|
||||
|
||||
* **Marketplace digest pinning and signature verification**: see the marketplace entry under Fixes (#5082).
|
||||
|
||||
## Development, Testing, and CI/CD
|
||||
|
||||
* **Chainsaw asserts survive transient API errors**: an e2e assert keeps polling through an error such as an etcd leader change instead of failing the suite early ([**@lexfrei**](https://github.com/lexfrei) in #4867, backport #5246).
|
||||
* **Proxmox e2e hygiene**: the Proxmox tenant kubeconfig is kept per run and removed afterwards, the HelmRelease guard reads the `KubernetesNodes` pool release, and the leak allowlist is validated against every suite layout ([**@themoriarti**](https://github.com/themoriarti) in #4971, #4970 and #4972, backports #5246).
|
||||
* **Frozen-line builds start with `GITHUB_TOKEN`**: the initial packages build dispatched when a release line is frozen now runs ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in #4888, backport #5246).
|
||||
* **Unit tests after the release image restamp**: two unit tests that read fields the release build rewrites stay green on `release-1.7`; this is a release-branch-only fix ([**@themoriarti**](https://github.com/themoriarti) in #5179).
|
||||
|
||||
## Documentation
|
||||
|
||||
* **[website] Documentation**: the v1.7 docs gain the guides that lived only in the cozystack repository, the pending v1.7 docs PRs, a DRBD note for the replicated storage class, a refreshed Chainsaw e2e guide, restored legacy URLs and a grouping of older doc versions ([**@myasnikovdaniil**](https://github.com/myasnikovdaniil) in cozystack/website#763, cozystack/website#782, cozystack/website#675, cozystack/website#611, cozystack/website#775 and cozystack/website#767).
|
||||
* **[website] Blog and demo**: a full-stack observability post for v1.7, the Cozystack 1.7 announcement, a three-board Raspberry Pi 5 post, and a collapsible navbar with a closable hint on the live demo page ([**@tym83**](https://github.com/tym83) in cozystack/website#790, cozystack/website#801 and cozystack/website#802; [**@kvaps**](https://github.com/kvaps) in cozystack/website#789).
|
||||
* **[website] Pages deploy**: the deploy workflow drops the Dart Sass install ([**@kvaps**](https://github.com/kvaps) in cozystack/website#798).
|
||||
* **[ansible-cozystack] Installer v1.7.0**: tracks the Cozystack installer image at v1.7.0, released in ansible-cozystack v1.7.0 ([**@app/renovate**](https://github.com/apps/renovate) in cozystack/ansible-cozystack#89).
|
||||
|
||||
## Migration and Upgrades
|
||||
|
||||
Platform migrations do not change in this release: `migrations.targetVersion` stays at 61.
|
||||
|
||||
* **Remove system databases from PostgreSQL `databases` before upgrading (#5155).** A release that declares `postgres`, `template0` or `template1` under `databases` is refused at render; remove the entry first. A system database a previous version took over is handed back to the superuser on the next upgrade: the event trigger is removed, ownership and the `public` schema are restored, and its `<db>_admin` and `<db>_readonly` roles are dropped.
|
||||
* **Tenant quota updates can be rejected after upgrading (#5184).** Admission now counts the parent's usage of extended resources and hugepages, so an update to an existing tenant can be rejected when its quota for these resources exceeds the parent's remaining budget, including an update that leaves the quota unchanged.
|
||||
* **Air-gapped mirrors need `backup-http-client` (#4756).** Mirror `ghcr.io/cozystack/cozystack/backup-http-client` before upgrading, or ClickHouse and RabbitMQ backup Pods cannot pull their image.
|
||||
* **tcp-balancer pods roll once (#5005).** The new HAProxy `global` section changes the rendered config; `maxconn` follows the memory limit at about 16 connections per MiB (2035 on `t1.nano`).
|
||||
* **Redis scaled to one replica loses its Sentinel PDB (#5151).** A post-upgrade hook deletes the Sentinel PodDisruptionBudget the operator created earlier for an instance with `replicas: 1`; scaling an instance between 1 and 2 replicas scales its Sentinel Deployment between 1 and 3.
|
||||
* **Backups created by failed MariaDB runs are not cleaned up (#5207).** Backups made by failed runs before this fix are not removed; restoring from one can fail or select another dump from the shared prefix.
|
||||
* **Keys lost to the COSI grant bug stay lost (#5284).** Re-add the affected user to the Bucket as described in the Fixes entry.
|
||||
* **`registries` in the platform Package is ignored (#5190).** A Package that still sets it keeps installing; the `patch-containerd` Secret in `cozy-system` is deleted by the release.
|
||||
* **SecurityGroups now reach Kubernetes workers (#5124).** A SecurityGroup attached to a `Kubernetes` application starts applying to its worker nodes after the upgrade, so review attached groups before upgrading.
|
||||
|
||||
## Contributors
|
||||
|
||||
We'd like to thank all contributors who made this release possible:
|
||||
|
||||
* [**@androndo**](https://github.com/androndo)
|
||||
* [**@IvanHunters**](https://github.com/IvanHunters)
|
||||
* [**@kvaps**](https://github.com/kvaps)
|
||||
* [**@lexfrei**](https://github.com/lexfrei)
|
||||
* [**@mattia-eleuteri**](https://github.com/mattia-eleuteri)
|
||||
* [**@myasnikovdaniil**](https://github.com/myasnikovdaniil)
|
||||
* [**@scooby87**](https://github.com/scooby87)
|
||||
* [**@themoriarti**](https://github.com/themoriarti)
|
||||
* [**@tym83**](https://github.com/tym83)
|
||||
* [**@yankawai**](https://github.com/yankawai)
|
||||
|
||||
**Full Changelog**: https://github.com/cozystack/cozystack/compare/v1.7.0...v1.7.1
|
||||
@@ -14,7 +14,7 @@ go build ./cmd/backport-batch
|
||||
./backport-batch check --line release-X.Y --cut
|
||||
```
|
||||
|
||||
[`backport-batch check`](../../cmd/backport-batch/README.md) applies the rules to every pull request merged into `main` since the line branched, and looks for each one they take on the line and then on the open batch pull request: by picks whose `-x` lines name its merge commit or every commit of its own, by a pick that would change nothing, and, on commits from before the policy's batch-only day, by what their text says, such as `Backport of #N`. With `--cut` a change found only on the open batch pull request fails the check too, since a cut tags the line and would not carry it: merge the batch pull request first. A conflict the batch deferred, or an original deferred to the next batch, prints as a warning without failing the check, also once that batch merged or closed. An original that waits on the open batch for a maintainer fails the cut. A change that reached the line by hand counts like any other, by what reached the line.
|
||||
[`backport-batch check`](../../cmd/backport-batch/README.md) applies the rules to every pull request merged into `main` since the line branched, and looks for each one they take on the line and then on the open batch pull request: by picks whose `-x` lines name its merge commit or every commit of its own, by a pick that would change nothing, and, on commits from before the policy's batch-only day, by what their text says, such as `Backport of #N`. With `--cut` a change found only on the open batch pull request fails the check too, since a cut tags the line and would not carry it: merge the batch pull request first. A conflict the batch deferred, or an original deferred to the next batch, prints as a warning without failing the check, also once that batch merged or closed. An original that waits on the open batch for a maintainer fails the cut, and so does a unit on the open batch that carries only part of its original. An original parked because it builds on a change the line never gets, or would land only in part, prints as a note; one that waits on another original fails, warns or notes by what it waits on. A change that reached the line by hand counts like any other, by what reached the line.
|
||||
|
||||
### Backporting code that touches monotonic counters
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ The [backport batch](../backports.md) is built by [`backport-batch`](../../../cm
|
||||
### Rules for every task
|
||||
|
||||
- Push nothing but the branch of the open batch pull request, `backport-batch/<line>-<yyyymmdd>` in this repository, and only what the task's check accepted. Never push to `main` or a `release-*` branch, and never open a second batch pull request: the scheduled run continues the open one.
|
||||
- Change no file the original pull request does not change. A conflict that cannot be resolved inside those files is not resolved: the original stays deferred.
|
||||
- Change no file the original pull request does not change, or the path a rename on `main` or on the line gave it. A conflict that cannot be resolved inside those files is not resolved: the original stays deferred.
|
||||
- Never rewrite the batch branch by hand. A plain push to it is a fast-forward; only `backport-batch run --defer --push` replaces it, leased on the head it rebuilt.
|
||||
- Leave the batch pull request's title and body to the tool: it rewrites both on every run, and a hand edit is lost or, inside the `backport-batch` block, makes `run`, `check`, `fold` and `notify` refuse the body, and inside the `backport-batch-report` block makes `run --push` and `backport-batch report` refuse it. Leave the tool's report comments as they are too: each keeps one classification of the line, and an edited one makes `backport-batch report` refuse it.
|
||||
- When the task cannot be done within these rules, push nothing and leave a comment on the batch pull request saying why, so the next maintainer to look finds it. Nothing is lost by waiting: a red batch does not merge, and a deferred original goes to the next batch.
|
||||
|
||||
@@ -34,7 +34,7 @@ git add <resolved files>
|
||||
git -c core.hooksPath=/dev/null cherry-pick --continue --no-edit
|
||||
```
|
||||
|
||||
When the original builds on something the line does not have, a helper, a value or another pull request, the resolution would have to bring that along, which this task does not allow: abort with `git cherry-pick --abort`, go back with `git switch --detach "$base"`, and comment on the batch pull request which change the original depends on; it stays deferred until that change is on the line. When that change will never reach the line, drop the original there instead, with `backport-batch run --line <line> --drop N --reason "<builds on …>" --push`, so later batches stop trying it. A commit whose change the line already has stops the pick as empty; skip it with `git cherry-pick --skip`.
|
||||
When the original builds on something the line does not have, a helper, a value or another pull request, the resolution would have to bring that along, which this task does not allow: abort with `git cherry-pick --abort`, go back with `git switch --detach "$base"`, and comment on the batch pull request which change the original depends on; it stays deferred until that change is on the line. An original the batch parked because it builds on such a change is the same task, and lands only as a complete adaptation: every file it changes changed, unless the line already has that file as the original makes it. When that change will never reach the line, drop the original there instead, with `backport-batch run --line <line> --drop N --reason "<builds on …>" --push`, so later batches stop trying it. A commit whose change the line already has stops the pick as empty; skip it with `git cherry-pick --skip`. A resolution that ports only what the line has and leaves a file of the original as it was, one the line does not have for instance, is refused by `fold`: such a port goes to the line as a pull request of its own, as [fixes that exist only on a release line](../backports.md#fixes-that-exist-only-on-a-release-line) describes, and the original is dropped there.
|
||||
|
||||
For a merged pull request, join the picks to the batch head with the merge commit:
|
||||
|
||||
|
||||
@@ -25,6 +25,10 @@ The batch reads the pull requests off `main`'s first-parent history, one per mer
|
||||
|
||||
A pull request the rules take for every line can still be wrong for one of them, for instance because it builds on code that line does not have. A maintainer drops it from that line with `./backport-batch run --line <line> --drop <number> --reason <why> --push`: the batch takes it off, if it carries it, and no later batch for that line picks it again. The drop is kept in the [line's record](#the-lines-record-and-reports), so it needs an open batch pull request to record it on; before a line's first batch pull request exists there is nothing to record it on, and the command refuses. `--undrop <number>` withdraws it, and the next batch tries the pull request again. A pull request the line already carries cannot be dropped. To land a hand-made port instead, open it as a pull request of its own, as [fixes that exist only on a release line](#fixes-that-exist-only-on-a-release-line) describes, and drop the original.
|
||||
|
||||
The batch also leaves a pull request out of a line on its own when it cannot land there whole. A pick that would leave part of what the pull request does on `main` without effect on the line, a change to a file the line does not have, or the deletion of a file the line never had or already removed, is not made: the pull request is parked, the batch pull request lists it under "Parked" with those files, and `check` prints it as a note without failing. A file the line already has as the pull request makes it counts as there, and so does a file `main` or the line renamed, under its other name. A unit already on the open batch that does only part of its original fails `check --cut` and `backport-batch verify`, whatever its label says, until `backport-batch run --defer` takes it out. What applies of a parked pull request goes to the line as a pull request of its own, as [fixes that exist only on a release line](#fixes-that-exist-only-on-a-release-line) describes.
|
||||
|
||||
A pull request is parked too when it builds on a change the line never gets: it changes a file a `feat` added on `main`, say, or a line such a change wrote, and the line has neither. `backport-batch` reads that from git, as [its README](../../cmd/backport-batch/README.md#what-an-original-builds-on) describes, and names the change in the batch pull request. One that builds on a change that is still to come, a deferred conflict, one waiting for a maintainer, one rule 6 holds or one not tried yet, waits for it instead, listed under "Waiting on another original", and the batch tries it again once that change is on the line; one picked earlier in the same batch counts as there. A complete hand adaptation of a parked pull request, every file it changes changed, still lands on the batch through [resolving a conflict](./backport-tasks/resolve-conflict.md).
|
||||
|
||||
### What waits for the next minor
|
||||
|
||||
`feat`, `chore` and `docs` pull requests, and dependency bumps (scope `deps`), ship with the next minor, not in a patch release; only a maintainer's `backport/include`, below, takes one. If a class of fixes should also wait for a minor, it gets a new rule on this page; a single pull request that the rules get wrong is what the overrides below are for.
|
||||
@@ -33,7 +37,7 @@ A pull request the rules take for every line can still be wrong for one of them,
|
||||
|
||||
A maintainer overrides the rules for one pull request with a label on the merged pull request on `main`:
|
||||
|
||||
- `backport/include` takes it although rules 2 to 4 keep it out, a `feat` for instance, or although it merged before its line started under this policy. It still has to pick cleanly or have its conflict resolved, pass CI on the batch, and, on an older line, already be on the newer one, and a line that already carries it does not take it again.
|
||||
- `backport/include` takes it although rules 2 to 4 keep it out, a `feat` for instance, or although it merged before its line started under this policy. It still has to pick cleanly or have its conflict resolved, pass CI on the batch, and, on an older line, already be on the newer one, and a line that already carries it does not take it again. It lifts no park, as [what the batch leaves out on one line](#what-the-batch-leaves-out-on-one-line) describes.
|
||||
- `backport/skip` keeps it out although the rules take it, and `backport-batch check` stops asking for it. An open batch that picked it before the label came keeps it until `backport-batch run --defer` takes it out; the batch pull request says so meanwhile.
|
||||
|
||||
A label decides for every line. To keep a pull request out of one line only, drop it there with `backport-batch run --drop`, as [what the batch leaves out on one line](#what-the-batch-leaves-out-on-one-line) describes; `backport/include` does not lift a drop.
|
||||
@@ -65,7 +69,7 @@ released-lines: 2
|
||||
|
||||
### Fixes that exist only on a release line
|
||||
|
||||
Some fixes go to a release line as a pull request of their own. Such pull requests are occasional, and maintainers open them: for a fix with no counterpart on `main`, for one that on `main` came as part of another pull request, or for one written for `main` rather than for the line. When the counterpart on `main` is a pull request of its own that the batch would take, the batch skips it as already on the line once picking it would change nothing there; otherwise a maintainer labels it `backport/skip`.
|
||||
Some fixes go to a release line as a pull request of their own. Such pull requests are occasional, and maintainers open them: for a fix with no counterpart on `main`, for one that on `main` came as part of another pull request, or for one written for `main` rather than for the line. When the counterpart on `main` is a pull request of its own that the batch would take, the batch skips it as already on the line once picking it would change nothing there; otherwise a maintainer labels it `backport/skip`. A port of only what a line has of a pull request from `main`, one that leaves out what the line lacks, is such a pull request as well: the batch carries no unit that does part of its original, and `backport-batch fold` refuses one.
|
||||
|
||||
A pull request opened against a `release-*` branch is closed automatically, with a comment that links to this page, when its author is a user account, not a bot, that GitHub names neither the repository's owner nor a member nor a collaborator, and whose access to the repository GitHub reports as read or none. Anyone with triage access or more keeps their pull request open. When GitHub's answer on the author's access cannot be read, the check closes nothing and fails, naming the read that failed. The check runs only when a pull request is opened, so a maintainer keeps a contributor's release-only fix by reopening it. [`hack/close-manual-backport.sh`](../../hack/close-manual-backport.sh) does this from [`pr-labeler.yaml`](../../.github/workflows/pr-labeler.yaml); run it with `--pr <number> --dry-run` to see what it would decide for a given pull request when it is opened.
|
||||
|
||||
@@ -87,11 +91,11 @@ A run by hand trusts the open batch pull request the scheduled run opened as `co
|
||||
|
||||
`./backport-batch lines` prints the lines, newest first. `run`, `check`, `fold` and `lines` read the policy block from this page on `main`, so until the page with its block is on `main` they need `--policy <ref>` naming a ref that has it, and the scheduled run, which passes none, fails.
|
||||
|
||||
`run` picks what the rules take onto `backport-batch/<line>-<yyyymmdd>` and opens a draft pull request against the line. A pull request merged into `main` with a merge commit goes onto the batch as its own commits, oldest first, each picked with `git cherry-pick -x --signoff` so it keeps its author, its message and its sign-offs and gains the sign-off of whoever runs the batch; commits that merged `main` into the pull request's branch are left out. A merge commit joins them to the batch: it carries the original's merge message from `main` and ends in a `Backported-from: <merge commit>` trailer. A pull request squash-merged into `main` goes on as one pick. When any commit of an original conflicts, nothing of that original stays on the batch. The batch pull request is merged into the line with a merge commit, so all of this reaches the line as it is. Its body lists the originals with how many commits each brought and which of them differ from the commit they came from, the conflicts it deferred with the commit that failed and its files, and what it skipped under which rule, and ends with the [line's record and the run's report](#the-lines-record-and-reports); `--push` prints the pull request's URL. While that pull request is open, the next run continues it, `--continue` reading its head, rather than opening another. CI on the batch pull request runs the line's build and tests, and a maintainer merges it. Since rule 6 reads the newer line itself, an older line takes a fix in the batch after the newer line's batch with that fix has merged.
|
||||
`run` picks what the rules take onto `backport-batch/<line>-<yyyymmdd>` and opens a draft pull request against the line. A pull request merged into `main` with a merge commit goes onto the batch as its own commits, oldest first, each picked with `git cherry-pick -x --signoff` so it keeps its author, its message and its sign-offs and gains the sign-off of whoever runs the batch; commits that merged `main` into the pull request's branch are left out. A merge commit joins them to the batch: it carries the original's merge message from `main` and ends in a `Backported-from: <merge commit>` trailer. A pull request squash-merged into `main` goes on as one pick. When any commit of an original conflicts, nothing of that original stays on the batch. The batch pull request is merged into the line with a merge commit, so all of this reaches the line as it is. Its body lists the originals with how many commits each brought and which of them differ from the commit they came from, whose release notes wait under "Check these notes" with what to compare, since a note may not hold for a pick that differs, the conflicts it deferred with the commit that failed and its files, what it parked and why, and what it skipped under which rule, and ends with the [line's record and the run's report](#the-lines-record-and-reports); `--push` prints the pull request's URL. While that pull request is open, the next run continues it, `--continue` reading its head, rather than opening another. CI on the batch pull request runs the line's build and tests, and a maintainer merges it. Since rule 6 reads the newer line itself, an older line takes a fix in the batch after the newer line's batch with that fix has merged.
|
||||
|
||||
A deferred conflict waits for the next batch. To land it sooner, pick the original's commits onto the batch head with `git cherry-pick -x`, resolving each conflict, and join them to the batch head with a merge commit that names the original; an original squash-merged into `main` is one pick of its commit there, with no merge. Check that merge or that pick with `./backport-batch fold --line <line> --original <number> --commit <sha>`, and push it onto the batch branch, as [resolving a conflict](./backport-tasks/resolve-conflict.md) describes. CI runs `backport-batch verify` on every push to a batch pull request, which holds every original on it to the same shape. When CI on the batch pull request fails, the original it comes from is taken out with `./backport-batch run --line <line> --defer <number> --reason <why> --push` and goes to the next batch, as [attributing a failed batch CI](./backport-tasks/failed-ci.md) describes; the rest of the batch merges without it. When it is all the batch carries, `--push` records why in the batch pull request, closes it and deletes its branch, and the next batch tries it again. `--drop` and `--undrop` change the record of the open batch pull request in the same way, and need one.
|
||||
|
||||
Before a patch release or an rc is cut from a line, `./backport-batch check --line <line> --cut` has to exit 0. A cut tags the line, so the check fails while a pull request the rules take is not on the line, including one that is only on the open batch pull request: merge the batch pull request first. It prints the URLs, and prints what the batch deferred as warnings, since deferring is a maintainer's call; so is a pull request labelled `backport/skip` that the open batch still carries. An original that waits on the batch for a maintainer fails the cut, also when it is all the batch has left. A cut that passes says so. Without `--cut`, as the scheduled run uses it, a change on the open batch pull request counts as carried.
|
||||
Before a patch release or an rc is cut from a line, `./backport-batch check --line <line> --cut` has to exit 0. A cut tags the line, so the check fails while a pull request the rules take is not on the line, including one that is only on the open batch pull request: merge the batch pull request first. It prints the URLs, and prints what the batch deferred as warnings, since deferring is a maintainer's call; so is a pull request labelled `backport/skip` that the open batch still carries. An original that waits on the batch for a maintainer fails the cut, also when it is all the batch has left, and so does a unit on the batch that carries only part of its original, whatever its label. A cut that passes says so. Without `--cut`, as the scheduled run uses it, a change on the open batch pull request counts as carried.
|
||||
|
||||
### The line's record and reports
|
||||
|
||||
@@ -101,7 +105,7 @@ Each run also reports where every pull request merged into `main` since the line
|
||||
|
||||
### Changes to workflows
|
||||
|
||||
The scheduled run's token may not push a commit that changes `.github/workflows/`, so the scheduled run leaves such an original out (`run --no-workflow-files`). The batch pull request lists it under "Waiting for a maintainer", with the workflow files it changes, and so does the batch's comment. It is no conflict. The policy still takes it: `check` warns about it while it waits, and `check --cut` fails on it. When nothing else is picked there is no batch pull request to list it on, and `check` reports it as missing.
|
||||
The scheduled run's token may not push a commit that changes `.github/workflows/`, so the scheduled run leaves such an original out (`run --no-workflow-files`). That holds also for one whose only missing dependency that never comes is a test or documentation, which a maintainer's run, with the token that may push the workflow, parks instead: what lands of it then is a pull request of its own; `check` reads which of the two the open batch was assembled as from its record, and with no batch open takes the scheduled run's. The batch pull request lists it under "Waiting for a maintainer", with the workflow files it changes, and so does the batch's comment. It is no conflict. The policy still takes it: `check` warns about it while it waits, and `check --cut` fails on it. When nothing else is picked there is no batch pull request to list it on, and `check` reports it as missing.
|
||||
|
||||
A maintainer brings it over with their own token, one that may push workflow changes (the Workflows permission, or the classic `workflow` scope), by continuing the open batch without the flag:
|
||||
|
||||
@@ -113,7 +117,7 @@ That picks it onto the batch in the batch's shape, after what the batch carries,
|
||||
|
||||
### The scheduled run
|
||||
|
||||
[`backport-batch.yaml`](../../.github/workflows/backport-batch.yaml) runs [`hack/backport-batch-scheduled.sh`](../../hack/backport-batch-scheduled.sh) on Monday and Thursday. For each line the batch runs for, newest first, it continues the open batch pull request or opens one, runs `check`, and then `backport-batch notify`, which reports what waits on the batch. A batch open for three days or more, so one the previous run already saw, gets a comment on its pull request saying so and a line in the run's summary. The comment is one per pull request, updated in place on every run. It also lists the deferred conflicts, which a maintainer resolves as [resolving a conflict](./backport-tasks/resolve-conflict.md) describes, and the originals that wait because they change workflows. A failed batch CI waits for a maintainer too, as [attributing a failed batch CI](./backport-tasks/failed-ci.md) describes; the comment does not list it, and it shows on the batch pull request's checks. `hack/backport-batch-scheduled.sh --dry-run` does the same in the current worktree and changes nothing on GitHub. Its `check` sees the line without the batch the dry run just built, so it can exit 1 where the real run's check passes.
|
||||
[`backport-batch.yaml`](../../.github/workflows/backport-batch.yaml) runs [`hack/backport-batch-scheduled.sh`](../../hack/backport-batch-scheduled.sh) on Monday and Thursday. For each line the batch runs for, newest first, it continues the open batch pull request or opens one, runs `check`, and then `backport-batch notify`, which reports what waits on the batch. A batch open for three days or more, so one the previous run already saw, gets a comment on its pull request saying so and a line in the run's summary. The comment is one per pull request, updated in place on every run. It also lists the deferred conflicts, which a maintainer resolves as [resolving a conflict](./backport-tasks/resolve-conflict.md) describes, and the originals that wait because they change workflows, and names each parked original once: a park named on one batch is not named again on the next while it stays the same park, and with no batch open nothing is named until the next batch opens. A failed batch CI waits for a maintainer too, as [attributing a failed batch CI](./backport-tasks/failed-ci.md) describes; the comment does not list it, and it shows on the batch pull request's checks. `hack/backport-batch-scheduled.sh --dry-run` does the same in the current worktree and changes nothing on GitHub. Its `check` sees the line without the batch the dry run just built, so it can exit 1 where the real run's check passes.
|
||||
|
||||
The batch runs as the cozystack-ci GitHub App, which the release workflows already use, and needs no secret of its own. The job mints a token from `COZYSTACK_CI_APP_ID` and `COZYSTACK_CI_PRIVATE_KEY` for this repository alone, with Contents and Pull requests write and nothing more, and fails when either secret is missing. Its pushes start CI on the batch pull request, which a push with the job's own `GITHUB_TOKEN` would not. Its commits carry the App's bot, `cozystack-ci[bot]`, whose batch pull request and comment `backport-batch` trusts: `BACKPORT_BATCH_BOT`, which the job sets from the token, names it, and a run without the variable trusts `cozystack-ci[bot]` too. The App's installation has to grant this repository Contents and Pull requests read and write, which an organization admin sees among the organization's installed GitHub Apps. It has no Workflows permission, so a change to `.github/workflows/` waits for a maintainer, as [changes to workflows](#changes-to-workflows) describes.
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ Actors are decomposed by **trust boundary**: two components are distinct actors
|
||||
| GitOps / Flux delivery layer | T1 — platform control plane | Reconciles HelmReleases/Kustomizations into arbitrary privileged manifests; effectively cluster-admin. | Tenants cannot create HelmReleases (RBAC); not tenant-network-reachable. |
|
||||
| `cozystack-api` aggregated API server | T1 — platform control plane | Converts tenant CRs into HelmReleases; can CRUD HelmReleases, Secrets, and Cilium policies cluster-wide. Acts as the policy-enforcing gateway. | Constrains tenants via fixed chart reference, name/quota/host validation, and admission. |
|
||||
| System operators / controllers | T1 — platform control plane | Domain-scoped cluster privileges (provision control planes, mint certs/secrets, program CNI). | Distinct ServiceAccounts; isolated from tenants by RBAC and network policy. |
|
||||
| Tenant administrator (OIDC `<tenant>-admin` / `<tenant>-super-admin`) | T2 — tenant boundary | CRUD on `apps.cozystack.io` application kinds and `sdn.cozystack.io/securitygroups` in its namespace subtree; read on pods/services/ingress plus `delete` on a fixed set (pods/services/endpoints/events); no create/update of arbitrary core resources. (The ClusterRole also lists `persistentvolumes`, but that grant is inert under the namespace-scoped RoleBinding, since PVs are cluster-scoped.) `super-admin` holds the `apps.cozystack.io/*` wildcard (and can create child `Tenant`s); `admin` has an enumerated allowlist excluding some kinds (e.g. `tenants`, `monitoring`, `etcd`, `ingress`). | Namespace-scoped RBAC + admission policies + quota; no HelmRelease or cross-tenant access. |
|
||||
| Tenant administrator (OIDC `<tenant>-admin` / `<tenant>-super-admin`) | T2 — tenant boundary | CRUD on `apps.cozystack.io` application kinds and `sdn.cozystack.io/securitygroups` in its namespace subtree; read on pods/services/ingress plus `delete` on a fixed set (pods/services/endpoints/events); `get` on persistentvolumeclaims and `create` on `upload.cdi.kubevirt.io/uploadtokenrequests`, the pair `virtctl image-upload` needs to upload into an existing VMDisk; no create/update of arbitrary core resources. (The ClusterRole also lists `persistentvolumes`, but that grant is inert under the namespace-scoped RoleBinding, since PVs are cluster-scoped.) `super-admin` holds the `apps.cozystack.io/*` wildcard (and can create child `Tenant`s); `admin` has an enumerated allowlist excluding some kinds (e.g. `tenants`, `monitoring`, `etcd`, `ingress`). | Namespace-scoped RBAC + admission policies + quota; no HelmRelease or cross-tenant access. |
|
||||
| Tenant user (OIDC `<tenant>-view` / `<tenant>-use`) | T2 — tenant boundary | Read-only view plus operational verbs (VM console/vnc/portforward, start/stop/restart). Cannot create Application CRs. | Strictly dominated by tenant-admin; cannot escalate. |
|
||||
| Parent vs child (nested) tenant | T2 — tenant boundary | Parent ServiceAccounts bind into descendant namespaces; children do not bind upward. Quota hierarchically capped. | Ancestor→descendant trust is one-way. |
|
||||
| Tenant workloads / managed services | T3 — workload boundary | Run tenant-controlled code; no cluster-API rights beyond their ServiceAccount. | Namespace-scoped RBAC/identity isolate API objects; on the network, Cilium egress confines each tenant pod to its own subtree + platform system services + `world`, so unrelated tenants cannot reach each other over the pod network (enforced on egress; ingress is open to `cluster` — see Non-goals). |
|
||||
|
||||
@@ -39,7 +39,7 @@ The actors below are decomposed **by trust boundary**, not by component: two com
|
||||
| GitOps / Flux delivery layer | T1 | Reconciles HelmReleases/Kustomizations into arbitrary privileged manifests; effectively cluster-admin. | Tenants cannot create HelmReleases/Kustomizations (RBAC); not tenant-network-reachable. |
|
||||
| `cozystack-api` aggregated API server (ServiceAccount) | T1 | CRUD HelmReleases, Secrets, and CiliumNetworkPolicies cluster-wide; reads Namespaces/RBAC/quotas/nodes. Acts as the policy-enforcing gateway that converts tenant CRs to HelmReleases. | A distinct, network-exposed attack surface from Flux; constrains tenants via fixed chart ref + name/quota/host validation + admission. |
|
||||
| System operators / controllers | T1 | Domain-scoped cluster privileges (provision control planes, mint certs/secrets, program CNI). | Distinct ServiceAccounts, but all cluster-admin-adjacent; isolated from tenants only by RBAC and network policy. |
|
||||
| Tenant administrator (OIDC groups `<tenant>-admin`, `<tenant>-super-admin`) | T2 | CRUD on `apps.cozystack.io` application kinds and `sdn.cozystack.io/securitygroups` within its namespace subtree; read on pods/services/ingress plus `delete` on a fixed set (pods/services/endpoints/events); no create/update of arbitrary core resources. (The ClusterRole also lists `persistentvolumes`, but that grant is inert under the namespace-scoped RoleBinding, since PVs are cluster-scoped.) The `<tenant>-super-admin` group holds the `apps.cozystack.io/*` wildcard (and so can create child `Tenant`s); the `<tenant>-admin` group has an enumerated allowlist that excludes some kinds (e.g. `tenants`, `monitoring`, `etcd`, `ingress`, `external-dns`, `bootbox`). | Namespace-scoped RoleBindings + admission policies + quota; no access to HelmReleases, arbitrary core writes, or other tenants. |
|
||||
| Tenant administrator (OIDC groups `<tenant>-admin`, `<tenant>-super-admin`) | T2 | CRUD on `apps.cozystack.io` application kinds and `sdn.cozystack.io/securitygroups` within its namespace subtree; read on pods/services/ingress plus `delete` on a fixed set (pods/services/endpoints/events); `get` on persistentvolumeclaims and `create` on `upload.cdi.kubevirt.io/uploadtokenrequests`, the pair `virtctl image-upload` needs to upload into an existing VMDisk; no create/update of arbitrary core resources. (The ClusterRole also lists `persistentvolumes`, but that grant is inert under the namespace-scoped RoleBinding, since PVs are cluster-scoped.) The `<tenant>-super-admin` group holds the `apps.cozystack.io/*` wildcard (and so can create child `Tenant`s); the `<tenant>-admin` group has an enumerated allowlist that excludes some kinds (e.g. `tenants`, `monitoring`, `etcd`, `ingress`, `external-dns`, `bootbox`). | Namespace-scoped RoleBindings + admission policies + quota; no access to HelmReleases, arbitrary core writes, or other tenants. |
|
||||
| Tenant user (OIDC groups `<tenant>-view`, `<tenant>-use`) | T2 | Read-only view of Application CRs, pods, and services, plus operational verbs only (VM console/vnc/portforward, start/stop/restart). Cannot create Application CRs. | Strictly dominated by tenant-admin; cannot escalate to create/delete, so it is a distinct actor. |
|
||||
| Parent tenant vs child (nested) tenant | T2 | Parent ServiceAccounts are bound into descendant namespaces; child ServiceAccounts are not bound upward. Quota is hierarchically capped. | Ancestor→descendant only; compromising a parent compromises its children, not the reverse. |
|
||||
| Tenant workloads / managed services | T3 | Run tenant-controlled code; no cluster-API rights beyond their ServiceAccount. | Namespace-scoped RBAC/identity isolate their API objects. On the network, Cilium egress confines each tenant pod to its own tenant subtree, platform system services, and `world` (out-of-cluster), so unrelated tenants cannot reach each other over the pod network — enforced on the egress side (tenant-pod ingress is open to `cluster`; see Non-goals for residual caveats). |
|
||||
@@ -92,7 +92,7 @@ The following are explicitly out of scope for the platform's security guarantees
|
||||
|
||||
The single most load-bearing isolation control is the combination of two facts:
|
||||
|
||||
1. The tenant ClusterRoles (`packages/system/cozystack-basics/templates/clusterroles.yaml`) grant **no** `helm.toolkit.fluxcd.io` access and **no** create/update of arbitrary core resources — only CRUD on `apps.cozystack.io` application kinds (super-admin holds the `*` wildcard; admin an enumerated allowlist) and `sdn.cozystack.io/securitygroups`, read access to pods/services/ingresses, and (for tenant-admins) `delete` on a fixed set of core objects (pods/services/endpoints/events; the `persistentvolumes` entry in the ClusterRole is inert under the namespace-scoped RoleBinding). Tenant isolation is enforced by the *absence* of privileged grants in RBAC, not by an admission policy that whitelists `apps.cozystack.io/*` writes.
|
||||
1. The tenant ClusterRoles (`packages/system/cozystack-basics/templates/clusterroles.yaml`) grant **no** `helm.toolkit.fluxcd.io` access and **no** create/update of arbitrary core resources — only CRUD on `apps.cozystack.io` application kinds (super-admin holds the `*` wildcard; admin an enumerated allowlist) and `sdn.cozystack.io/securitygroups`, read access to pods/services/ingresses, and (for tenant-admins) `delete` on a fixed set of core objects (pods/services/endpoints/events; the `persistentvolumes` entry in the ClusterRole is inert under the namespace-scoped RoleBinding), `get` on persistentvolumeclaims and `create` on `upload.cdi.kubevirt.io/uploadtokenrequests` (for uploading an image into an existing VMDisk with `virtctl image-upload`). Tenant isolation is enforced by the *absence* of privileged grants in RBAC, not by an admission policy that whitelists `apps.cozystack.io/*` writes.
|
||||
2. The Application-to-HelmRelease conversion (`pkg/registry/apps/application/rest.go`) takes the chart reference, release-name prefix, and `valuesFrom` from server-side configuration; the tenant controls only the Helm values (`app.Spec`).
|
||||
|
||||
If a future `ApplicationDefinition`, RBAC change, or API change ever let a tenant influence the chart reference, `valuesFrom`, or write HelmReleases directly, the T2→T1 isolation would collapse. Changes touching tenant RBAC or the Application REST layer should be reviewed against this invariant.
|
||||
|
||||
Executable
+94
@@ -0,0 +1,94 @@
|
||||
#!/bin/bash
|
||||
# Shared helpers for the tenant Kubernetes backup/restore walkthrough.
|
||||
# Source this file in other scripts: source "$(dirname "$0")/00-helpers.sh"
|
||||
|
||||
export RED='\033[0;31m'
|
||||
export GREEN='\033[0;32m'
|
||||
export YELLOW='\033[1;33m'
|
||||
export BLUE='\033[0;34m'
|
||||
export MAGENTA='\033[0;35m'
|
||||
export CYAN='\033[0;36m'
|
||||
export NC='\033[0m'
|
||||
export BOLD='\033[1m'
|
||||
|
||||
export NAMESPACE="${NAMESPACE:-tenant-root}"
|
||||
export CLUSTER_NAME="${CLUSTER_NAME:-backup-demo}"
|
||||
export POOL_NAME="${POOL_NAME:-md0}"
|
||||
export WORKER_DISK_SIZE="${WORKER_DISK_SIZE:-20Gi}"
|
||||
export BACKUPCLASS_NAME="${BACKUPCLASS_NAME:-cozy-default}"
|
||||
export BACKUPJOB_NAME="${BACKUPJOB_NAME:-kubernetes-backup-job}"
|
||||
export RESTOREJOB_INPLACE_NAME="${RESTOREJOB_INPLACE_NAME:-kubernetes-restore-inplace}"
|
||||
export RESTOREJOB_RECREATE_NAME="${RESTOREJOB_RECREATE_NAME:-kubernetes-restore-recreate}"
|
||||
export TENANT_API_PORT="${TENANT_API_PORT:-16443}"
|
||||
export MARKER_VALUE="${MARKER_VALUE:-roundtrip-7c2e91d4}"
|
||||
export RELEASE="kubernetes-${CLUSTER_NAME}"
|
||||
|
||||
log_info() { echo -e "${BLUE}i${NC} $*" >&2; }
|
||||
log_success() { echo -e "${GREEN}OK${NC} $*" >&2; }
|
||||
log_warning() { echo -e "${YELLOW}!${NC} $*" >&2; }
|
||||
log_error() { echo -e "${RED}x${NC} $*" >&2; }
|
||||
log_substep() { echo -e "${CYAN} -> $*${NC}" >&2; }
|
||||
|
||||
print_header() {
|
||||
echo -e "\n${MAGENTA}${BOLD}== $1 ==${NC}\n" >&2
|
||||
}
|
||||
|
||||
# shellcheck source-path=SCRIPTDIR source=../_lib/wait-helpers.sh
|
||||
source "$(dirname "${BASH_SOURCE[0]}")/../_lib/wait-helpers.sh"
|
||||
|
||||
# The admin kubeconfig points at the in-cluster Service, so the walkthrough
|
||||
# reaches the tenant API through a port-forward to it and keeps the TLS server
|
||||
# name the certificate is issued for.
|
||||
TENANT_KUBECONFIG="$(dirname "${BASH_SOURCE[0]}")/.tenant-kubeconfig"
|
||||
export TENANT_KUBECONFIG
|
||||
|
||||
tenant_connect() {
|
||||
kubectl -n "$NAMESPACE" get secret "${RELEASE}-admin-kubeconfig" \
|
||||
-o jsonpath='{.data.super-admin\.svc}' | base64 -d > "$TENANT_KUBECONFIG"
|
||||
chmod 600 "$TENANT_KUBECONFIG"
|
||||
local cluster
|
||||
cluster=$(kubectl --kubeconfig "$TENANT_KUBECONFIG" config view -o jsonpath='{.clusters[0].name}')
|
||||
kubectl --kubeconfig "$TENANT_KUBECONFIG" config set-cluster "$cluster" \
|
||||
--server="https://127.0.0.1:${TENANT_API_PORT}" \
|
||||
--tls-server-name="${RELEASE}.${NAMESPACE}.svc" >/dev/null
|
||||
pkill -f "port-forward svc/${RELEASE} ${TENANT_API_PORT}:6443" 2>/dev/null || true
|
||||
kubectl -n "$NAMESPACE" port-forward "svc/${RELEASE}" "${TENANT_API_PORT}:6443" >/dev/null 2>&1 &
|
||||
for _ in $(seq 1 30); do
|
||||
tkubectl get --raw /readyz >/dev/null 2>&1 && return 0
|
||||
sleep 2
|
||||
done
|
||||
log_error "tenant API of ${RELEASE} is not reachable through the port-forward"
|
||||
return 1
|
||||
}
|
||||
|
||||
tenant_disconnect() {
|
||||
pkill -f "port-forward svc/${RELEASE} ${TENANT_API_PORT}:6443" 2>/dev/null || true
|
||||
}
|
||||
|
||||
tkubectl() {
|
||||
kubectl --kubeconfig "$TENANT_KUBECONFIG" "$@"
|
||||
}
|
||||
|
||||
# The CA fingerprint is what proves a restore brought back the cluster rather
|
||||
# than a new one: nodes, kubeconfigs and tokens all trust this CA.
|
||||
cluster_ca_fingerprint() {
|
||||
kubectl -n "$NAMESPACE" get secret "${RELEASE}-ca" -o jsonpath='{.data.ca\.crt}' \
|
||||
| base64 -d | openssl x509 -noout -fingerprint -sha256 | cut -d= -f2
|
||||
}
|
||||
|
||||
# wait_tenant_marker checks both halves of the backup: the ConfigMap comes back
|
||||
# with etcd, the file on the PVC with the volumes.
|
||||
wait_tenant_marker() {
|
||||
local timeout="${1:-900}" elapsed=0 cm data
|
||||
while true; do
|
||||
cm=$(tkubectl -n demo get configmap marker -o jsonpath='{.data.marker}' 2>/dev/null || true)
|
||||
data=$(tkubectl -n demo exec deploy/writer -- cat /data/marker 2>/dev/null || true)
|
||||
if [[ "$cm" == "$MARKER_VALUE" && "$data" == "$MARKER_VALUE" ]]; then
|
||||
log_success "marker present in etcd (ConfigMap) and on the volume"
|
||||
return 0
|
||||
fi
|
||||
(( elapsed >= timeout )) && { log_error "marker not restored: configmap='${cm}' volume='${data}'"; return 1; }
|
||||
sleep 10
|
||||
elapsed=$((elapsed + 10))
|
||||
done
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
#!/bin/bash
|
||||
# Step 01: Create a tenant Kubernetes cluster with one worker pool and seed it
|
||||
# with a marker held in etcd (a ConfigMap) and on a PersistentVolume.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source "$SCRIPT_DIR/00-helpers.sh"
|
||||
|
||||
print_header "Step 01: Create Kubernetes '${CLUSTER_NAME}' and seed a marker"
|
||||
|
||||
kubectl apply -f - <<EOF
|
||||
apiVersion: apps.cozystack.io/v1alpha1
|
||||
kind: Kubernetes
|
||||
metadata:
|
||||
name: ${CLUSTER_NAME}
|
||||
namespace: ${NAMESPACE}
|
||||
spec:
|
||||
storageClass: replicated
|
||||
---
|
||||
apiVersion: apps.cozystack.io/v1alpha1
|
||||
kind: KubernetesNodes
|
||||
metadata:
|
||||
name: ${CLUSTER_NAME}-${POOL_NAME}
|
||||
namespace: ${NAMESPACE}
|
||||
spec:
|
||||
cluster: ${CLUSTER_NAME}
|
||||
minReplicas: 1
|
||||
maxReplicas: 1
|
||||
instanceType: u1.medium
|
||||
diskSize: ${WORKER_DISK_SIZE}
|
||||
EOF
|
||||
|
||||
wait_hr_ready "$RELEASE" 1200
|
||||
log_substep "Waiting for the worker node to join..."
|
||||
tenant_connect
|
||||
kubectl_ok=0
|
||||
for _ in $(seq 1 120); do
|
||||
if tkubectl wait --for=condition=Ready node --all --timeout=10s >/dev/null 2>&1 \
|
||||
&& [[ -n "$(tkubectl get nodes -o name)" ]] \
|
||||
&& [[ -n "$(tkubectl get storageclass -o name)" ]]; then
|
||||
kubectl_ok=1
|
||||
break
|
||||
fi
|
||||
sleep 15
|
||||
done
|
||||
(( kubectl_ok )) || { log_error "worker node or StorageClass did not appear"; exit 1; }
|
||||
|
||||
tkubectl apply -f - <<EOF
|
||||
apiVersion: v1
|
||||
kind: Namespace
|
||||
metadata:
|
||||
name: demo
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: marker
|
||||
namespace: demo
|
||||
data:
|
||||
marker: ${MARKER_VALUE}
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: PersistentVolumeClaim
|
||||
metadata:
|
||||
name: data
|
||||
namespace: demo
|
||||
spec:
|
||||
accessModes: [ReadWriteOnce]
|
||||
resources:
|
||||
requests:
|
||||
storage: 1Gi
|
||||
---
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: writer
|
||||
namespace: demo
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels: {app: writer}
|
||||
template:
|
||||
metadata:
|
||||
labels: {app: writer}
|
||||
spec:
|
||||
containers:
|
||||
- name: writer
|
||||
image: busybox:1.36
|
||||
command: ["sh", "-c", "[ -f /data/marker ] || echo -n ${MARKER_VALUE} > /data/marker; sync; sleep infinity"]
|
||||
volumeMounts:
|
||||
- {name: data, mountPath: /data}
|
||||
volumes:
|
||||
- name: data
|
||||
persistentVolumeClaim: {claimName: data}
|
||||
EOF
|
||||
|
||||
wait_tenant_marker 900
|
||||
cluster_ca_fingerprint > "$SCRIPT_DIR/.ca-fingerprint"
|
||||
log_success "cluster CA $(cat "$SCRIPT_DIR/.ca-fingerprint")"
|
||||
tenant_disconnect
|
||||
|
||||
echo -e "\n${GREEN}${BOLD}Next:${NC} ./02-create-backupjob.sh"
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
#!/bin/bash
|
||||
# Step 02: Back the cluster up through the cozy-default BackupClass. The
|
||||
# controller dumps the cluster's etcd prefix to the platform bucket with a
|
||||
# Job, then backs up its control-plane Secrets and volumes with Velero.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source "$SCRIPT_DIR/00-helpers.sh"
|
||||
|
||||
print_header "Step 02: Create BackupJob '${BACKUPJOB_NAME}'"
|
||||
|
||||
kubectl apply -f - <<EOF
|
||||
apiVersion: backups.cozystack.io/v1alpha1
|
||||
kind: BackupJob
|
||||
metadata:
|
||||
name: ${BACKUPJOB_NAME}
|
||||
namespace: ${NAMESPACE}
|
||||
spec:
|
||||
applicationRef:
|
||||
apiGroup: apps.cozystack.io
|
||||
kind: Kubernetes
|
||||
name: ${CLUSTER_NAME}
|
||||
backupClassName: ${BACKUPCLASS_NAME}
|
||||
EOF
|
||||
|
||||
wait_for_field backupjob "$BACKUPJOB_NAME" '{.status.phase}' Succeeded "$NAMESPACE" 1800 Failed
|
||||
|
||||
warn=$(kubectl -n "$NAMESPACE" get backupjob "$BACKUPJOB_NAME" \
|
||||
-o jsonpath='{.status.conditions[?(@.type=="CertificatesExpiring")].status}')
|
||||
if [[ "$warn" == "True" ]]; then
|
||||
log_warning "$(kubectl -n "$NAMESPACE" get backupjob "$BACKUPJOB_NAME" \
|
||||
-o jsonpath='{.status.conditions[?(@.type=="CertificatesExpiring")].message}')"
|
||||
fi
|
||||
log_success "Backup '$(kubectl -n "$NAMESPACE" get backupjob "$BACKUPJOB_NAME" -o jsonpath='{.status.backupRef.name}')' is Ready."
|
||||
|
||||
echo -e "\n${GREEN}${BOLD}Next:${NC} ./03-restore-in-place.sh"
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
#!/bin/bash
|
||||
# Step 03: Lose the demo namespace inside the live cluster and roll the cluster
|
||||
# back. The restore stops the control plane and the workers, writes the etcd
|
||||
# prefix and the volumes back, and returns the cluster to its replica counts.
|
||||
# Set RESTORE_OPTIONS='{"volumes":false}' (or '{"etcd":false}') to restore one
|
||||
# half only.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source "$SCRIPT_DIR/00-helpers.sh"
|
||||
|
||||
print_header "Step 03: Restore '${CLUSTER_NAME}' in place"
|
||||
|
||||
tenant_connect
|
||||
log_substep "Deleting namespace demo inside the tenant cluster..."
|
||||
tkubectl delete namespace demo --wait=true --timeout=10m
|
||||
tenant_disconnect
|
||||
|
||||
backup_ref=$(kubectl -n "$NAMESPACE" get backupjob "$BACKUPJOB_NAME" -o jsonpath='{.status.backupRef.name}')
|
||||
options="${RESTORE_OPTIONS:-}"
|
||||
[[ -n "$options" ]] || options='{}'
|
||||
kubectl apply -f - <<EOF
|
||||
apiVersion: backups.cozystack.io/v1alpha1
|
||||
kind: RestoreJob
|
||||
metadata:
|
||||
name: ${RESTOREJOB_INPLACE_NAME}
|
||||
namespace: ${NAMESPACE}
|
||||
spec:
|
||||
backupRef:
|
||||
name: ${backup_ref}
|
||||
options: ${options}
|
||||
EOF
|
||||
|
||||
wait_for_field restorejob "$RESTOREJOB_INPLACE_NAME" '{.status.phase}' Succeeded "$NAMESPACE" 2400 Failed
|
||||
wait_hr_ready "$RELEASE" 1200
|
||||
|
||||
tenant_connect
|
||||
wait_tenant_marker 1200
|
||||
[[ "$(cluster_ca_fingerprint)" == "$(cat "$SCRIPT_DIR/.ca-fingerprint")" ]] \
|
||||
|| { log_error "cluster CA changed across the restore"; exit 1; }
|
||||
log_success "same cluster CA, marker back in etcd and on the volume"
|
||||
tenant_disconnect
|
||||
|
||||
echo -e "\n${GREEN}${BOLD}Next:${NC} ./04-restore-after-delete.sh"
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
#!/bin/bash
|
||||
# Step 04: Delete the Kubernetes application and its worker pool, which also
|
||||
# deletes the cluster's etcd prefix and volumes, then rebuild it from the
|
||||
# backup under the same name and CA.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source "$SCRIPT_DIR/00-helpers.sh"
|
||||
|
||||
print_header "Step 04: Delete '${CLUSTER_NAME}' and rebuild it from the backup"
|
||||
|
||||
kubectl -n "$NAMESPACE" delete kubernetesnodeses.apps.cozystack.io "${CLUSTER_NAME}-${POOL_NAME}" --ignore-not-found --wait=false
|
||||
kubectl -n "$NAMESPACE" delete kuberneteses.apps.cozystack.io "$CLUSTER_NAME" --wait=false
|
||||
wait_deleted helmrelease "$RELEASE" "$NAMESPACE" 1200
|
||||
wait_deleted tenantcontrolplane "$RELEASE" "$NAMESPACE" 600
|
||||
log_substep "Removing what the application's deletion leaves behind, so the rebuild proves itself..."
|
||||
kubectl -n "$NAMESPACE" delete secret "${RELEASE}-talos-ca" "${RELEASE}-talos-tls-cert" "${RELEASE}-admin-kubeconfig" --ignore-not-found
|
||||
|
||||
backup_ref=$(kubectl -n "$NAMESPACE" get backupjob "$BACKUPJOB_NAME" -o jsonpath='{.status.backupRef.name}')
|
||||
kubectl apply -f - <<EOF
|
||||
apiVersion: backups.cozystack.io/v1alpha1
|
||||
kind: RestoreJob
|
||||
metadata:
|
||||
name: ${RESTOREJOB_RECREATE_NAME}
|
||||
namespace: ${NAMESPACE}
|
||||
spec:
|
||||
backupRef:
|
||||
name: ${backup_ref}
|
||||
EOF
|
||||
|
||||
wait_for_field restorejob "$RESTOREJOB_RECREATE_NAME" '{.status.phase}' Succeeded "$NAMESPACE" 2400 Failed
|
||||
wait_hr_ready "$RELEASE" 1200
|
||||
|
||||
tenant_connect
|
||||
wait_tenant_marker 1500
|
||||
[[ "$(cluster_ca_fingerprint)" == "$(cat "$SCRIPT_DIR/.ca-fingerprint")" ]] \
|
||||
|| { log_error "cluster CA changed across the rebuild"; exit 1; }
|
||||
log_success "cluster rebuilt with the same CA, marker back in etcd and on the volume"
|
||||
tenant_disconnect
|
||||
@@ -0,0 +1,68 @@
|
||||
# Tenant Kubernetes backup and restore
|
||||
|
||||
The `Kubernetes` strategy backs up a tenant Kubernetes application in two halves, taken one after the other, so a backup is crash-consistent at best:
|
||||
|
||||
- **etcd**: a Job dumps the cluster's key prefix from the tenant's etcd DataStore to the platform bucket. This is the cluster's API state.
|
||||
- **Velero**: the control-plane and Talos PKI Secrets, and the DataVolumes and PVCs that back the cluster's PersistentVolumes. Worker system disks are left out; workers are recreated and join with the restored PKI.
|
||||
|
||||
The Backup also records the HelmReleases of the cluster and of its worker pools. A backup warns, through a `CertificatesExpiring` condition on the BackupJob, when a control-plane CA expires before the backup's TTL runs out or a leaf certificate expires within `certificateWarningThreshold` (720h).
|
||||
|
||||
## Admin: prepare
|
||||
|
||||
Nothing to do when the platform default backups are on: `cozy-default` routes `Kubernetes` to the `cozy-default-kubernetes` strategy.
|
||||
|
||||
## User: back up
|
||||
|
||||
```yaml
|
||||
apiVersion: backups.cozystack.io/v1alpha1
|
||||
kind: BackupJob
|
||||
metadata:
|
||||
name: my-cluster-backup
|
||||
namespace: tenant-foo
|
||||
spec:
|
||||
applicationRef:
|
||||
apiGroup: apps.cozystack.io
|
||||
kind: Kubernetes
|
||||
name: my-cluster
|
||||
backupClassName: cozy-default
|
||||
```
|
||||
|
||||
A `Plan` with the same `applicationRef` schedules it.
|
||||
|
||||
## User: restore
|
||||
|
||||
A restore always goes into the application the backup was taken from.
|
||||
|
||||
```yaml
|
||||
apiVersion: backups.cozystack.io/v1alpha1
|
||||
kind: RestoreJob
|
||||
metadata:
|
||||
name: my-cluster-restore
|
||||
namespace: tenant-foo
|
||||
spec:
|
||||
backupRef:
|
||||
name: my-cluster-backup
|
||||
options:
|
||||
etcd: true # restore the API state (default)
|
||||
volumes: true # restore the PersistentVolume data (default)
|
||||
```
|
||||
|
||||
- **Cluster deleted**: the restore rebuilds it. It restores the PKI Secrets and the volumes, recreates the cluster, writes the etcd state under the new control plane's prefix, and then recreates the worker pools, so the cluster comes back on its old state and its old CA. The cluster and pool HelmReleases are recreated with the spec recorded at backup time, so a backup taken before a platform upgrade that changed the application's values brings back the older values, which the upgraded chart may need migrated by hand.
|
||||
- **Cluster live**: the restore rolls it back. It must be the cluster the backup was taken from: one deleted and created again under the same name has a new CA and service-account key, so the restore refuses it, and the way back to the backed-up cluster is to delete the application and run the restore again as a rebuild. It suspends the cluster's HelmReleases, stops the workers and the cluster-autoscaler when `volumes` are restored and then the control plane when `etcd` is, writes the selected halves back, and returns every replica count it changed. Workers are recreated, so expect the tenant's Pods to restart. Deleting the RestoreJob midway also returns the replica counts, unless its etcd write is under way.
|
||||
|
||||
Nothing is changed until the restore has confirmed that the backup's Velero half and etcd dump exist. If the etcd write fails partway, the control plane stays stopped and the RestoreJob points at the copy of the prefix it saved before writing; run a new RestoreJob to retry.
|
||||
|
||||
Restoring only one half leaves the other as it is now: `etcd` alone keeps the current volume contents, `volumes` alone keeps the current API objects.
|
||||
|
||||
The two halves differ in what a failed restore leaves behind. The etcd half saves the current prefix before it writes and can be retried from that copy. The volume half has no such copy: Velero cannot overwrite an existing claim, so a live-cluster restore deletes the current DataVolumes and PVCs before it restores them, and if the Velero restore then fails, the volume data exists only in the backup.
|
||||
|
||||
The etcd round trip scales with the number of keys under the cluster's prefix. The backup Job holds the whole dump in memory while it checks it, and the restore writes one key per `etcdctl` call: on a test cluster a 441-key restore took about 40 seconds including Pod start. At that rate the default `kubernetesBackupActiveDeadlineSeconds` (1h) covers a prefix of a few tens of thousands of keys. For a cluster with a larger API state, raise it together with the memory limit in `kubernetesBackupResources`.
|
||||
|
||||
## Walkthrough
|
||||
|
||||
```bash
|
||||
./run-all.sh # create, back up, roll back in place, rebuild after delete
|
||||
./cleanup.sh
|
||||
```
|
||||
|
||||
`NAMESPACE`, `CLUSTER_NAME` and `WORKER_DISK_SIZE` are read from the environment; see `00-helpers.sh`.
|
||||
Executable
+18
@@ -0,0 +1,18 @@
|
||||
#!/bin/bash
|
||||
# Remove everything the walkthrough created. Deleting the BackupJob's Backup
|
||||
# removes its Velero backup and its etcd dump object.
|
||||
set -uo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source "$SCRIPT_DIR/00-helpers.sh"
|
||||
|
||||
print_header "Cleanup"
|
||||
tenant_disconnect
|
||||
kubectl -n "$NAMESPACE" delete restorejob "$RESTOREJOB_INPLACE_NAME" "$RESTOREJOB_RECREATE_NAME" --ignore-not-found
|
||||
backup_ref=$(kubectl -n "$NAMESPACE" get backupjob "$BACKUPJOB_NAME" -o jsonpath='{.status.backupRef.name}' 2>/dev/null || true)
|
||||
kubectl -n "$NAMESPACE" delete backupjob "$BACKUPJOB_NAME" --ignore-not-found
|
||||
[[ -n "$backup_ref" ]] && kubectl -n "$NAMESPACE" delete backup "$backup_ref" --ignore-not-found
|
||||
kubectl -n "$NAMESPACE" delete kubernetesnodeses.apps.cozystack.io "${CLUSTER_NAME}-${POOL_NAME}" --ignore-not-found
|
||||
kubectl -n "$NAMESPACE" delete kuberneteses.apps.cozystack.io "$CLUSTER_NAME" --ignore-not-found
|
||||
rm -f "$TENANT_KUBECONFIG" "$SCRIPT_DIR/.ca-fingerprint"
|
||||
log_success "done"
|
||||
Executable
+29
@@ -0,0 +1,29 @@
|
||||
#!/bin/bash
|
||||
# End-to-end tenant Kubernetes backup/restore walkthrough. It proves the
|
||||
# restore brings back the same cluster: the CA fingerprint is unchanged, and a
|
||||
# marker kept in etcd and one kept on a PersistentVolume both come back, first
|
||||
# after rolling a live cluster back, then after rebuilding a deleted one.
|
||||
#
|
||||
# Needs the platform default backups (backupstrategy-controller with a Velero
|
||||
# BSL and the system bucket), so cozy-default routes Kubernetes to
|
||||
# cozy-default-kubernetes.
|
||||
#
|
||||
# Env knobs: see 00-helpers.sh. SKIP_RESTORE=1 stops after the backup.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
source "$SCRIPT_DIR/00-helpers.sh"
|
||||
|
||||
kubectl wait --for=create kubernetes.strategy.backups.cozystack.io/cozy-default-kubernetes --timeout=15m >/dev/null \
|
||||
|| { log_error "cozy-default-kubernetes strategy is missing: enable the platform default backups first"; exit 1; }
|
||||
|
||||
steps=(01 02 03 04)
|
||||
[[ "${SKIP_RESTORE:-0}" == "1" ]] && steps=(01 02)
|
||||
for step in "${steps[@]}"; do
|
||||
script="$(find "$SCRIPT_DIR" -maxdepth 1 -type f -name "${step}-*.sh" | sort | head -n1)"
|
||||
[[ -n "$script" ]] || { echo "no script found for step $step" >&2; exit 1; }
|
||||
echo "=== Running $(basename "$script") ==="
|
||||
bash "$script"
|
||||
done
|
||||
|
||||
print_header "Walkthrough complete"
|
||||
@@ -9,6 +9,10 @@ load test_helper
|
||||
# scope: they are re-vendored by `make update`, so a bad severity there is
|
||||
# fixed upstream or in patches/, not by editing the label.
|
||||
ALERT_SEVERITY_ALLOWED=" security critical major minor warning indeterminate informational normal ok cleared debug trace unknown "
|
||||
# The alerts that exist only to be dropped, the one place `none` is right.
|
||||
# InfoInhibitor is the source side of an Alertmanager inhibit rule, which the
|
||||
# platform config does not have, so delivering it at any severity is noise.
|
||||
ALERT_SEVERITY_BLACKHOLED=" InfoInhibitor "
|
||||
|
||||
helm_actions_stripped() {
|
||||
awk '
|
||||
@@ -104,6 +108,14 @@ alert_severity_contract() {
|
||||
break
|
||||
fi
|
||||
done
|
||||
if [ "$severity" = "none" ]; then
|
||||
for blackholed in $ALERT_SEVERITY_BLACKHOLED; do
|
||||
if [ "$blackholed" = "$alert" ]; then
|
||||
accepted=1
|
||||
break
|
||||
fi
|
||||
done
|
||||
fi
|
||||
if [ "$accepted" -eq 0 ]; then
|
||||
echo "$file: $alert -> '$severity'" >&2
|
||||
bad=$((bad + 1))
|
||||
@@ -128,6 +140,83 @@ alert_severity_contract() {
|
||||
alert_severity_contract
|
||||
}
|
||||
|
||||
@test "InfoInhibitor is labelled for the blackhole route" {
|
||||
rows=$(mktemp) || return 1
|
||||
if ! alert_severity_rows > "$rows"; then
|
||||
rm -f "$rows"
|
||||
return 1
|
||||
fi
|
||||
tab=$(printf '\t')
|
||||
found=$(grep -c "${tab}InfoInhibitor${tab}" "$rows" || true)
|
||||
delivered=$(grep "${tab}InfoInhibitor${tab}" "$rows" | grep -vc "${tab}none\$" || true)
|
||||
rm -f "$rows"
|
||||
[ "$found" -gt 0 ]
|
||||
[ "$delivered" -eq 0 ]
|
||||
}
|
||||
|
||||
@test "severity none passes only on an alert that exists to be dropped" {
|
||||
fixture=$(mktemp -d)
|
||||
mkdir -p "$fixture/packages/system/example/alerts"
|
||||
printf '%s\n' \
|
||||
'apiVersion: operator.victoriametrics.com/v1beta1' \
|
||||
'kind: VMRule' \
|
||||
'spec:' \
|
||||
' groups:' \
|
||||
' - name: general' \
|
||||
' rules:' \
|
||||
' - alert: InfoInhibitor' \
|
||||
' labels:' \
|
||||
' severity: none' \
|
||||
> "$fixture/packages/system/example/alerts/00-inhibitor.yaml"
|
||||
git -C "$fixture" init -q
|
||||
git -C "$fixture" add packages/system/example/alerts/00-inhibitor.yaml
|
||||
|
||||
inhibitor_succeeded=0
|
||||
ALERT_SEVERITY_ROOT="$fixture" alert_severity_contract >/dev/null 2>&1 || inhibitor_succeeded=$?
|
||||
|
||||
printf '%s\n' \
|
||||
'apiVersion: operator.victoriametrics.com/v1beta1' \
|
||||
'kind: VMRule' \
|
||||
'spec:' \
|
||||
' groups:' \
|
||||
' - name: silent' \
|
||||
' rules:' \
|
||||
' - alert: SilentAlert' \
|
||||
' labels:' \
|
||||
' severity: none' \
|
||||
> "$fixture/packages/system/example/alerts/01-silent.yaml"
|
||||
git -C "$fixture" add packages/system/example/alerts/01-silent.yaml
|
||||
|
||||
silent_succeeded=0
|
||||
ALERT_SEVERITY_ROOT="$fixture" alert_severity_contract >/dev/null 2>&1 || silent_succeeded=$?
|
||||
rm -rf "$fixture"
|
||||
[ "$inhibitor_succeeded" -eq 0 ]
|
||||
[ "$silent_succeeded" -ne 0 ]
|
||||
}
|
||||
|
||||
@test "an alert that exists to be dropped still fails on a refused severity" {
|
||||
fixture=$(mktemp -d)
|
||||
mkdir -p "$fixture/packages/system/example/alerts"
|
||||
printf '%s\n' \
|
||||
'apiVersion: operator.victoriametrics.com/v1beta1' \
|
||||
'kind: VMRule' \
|
||||
'spec:' \
|
||||
' groups:' \
|
||||
' - name: general' \
|
||||
' rules:' \
|
||||
' - alert: InfoInhibitor' \
|
||||
' labels:' \
|
||||
' severity: info' \
|
||||
> "$fixture/packages/system/example/alerts/00-inhibitor.yaml"
|
||||
git -C "$fixture" init -q
|
||||
git -C "$fixture" add packages/system/example/alerts/00-inhibitor.yaml
|
||||
|
||||
contract_succeeded=0
|
||||
ALERT_SEVERITY_ROOT="$fixture" alert_severity_contract >/dev/null 2>&1 || contract_succeeded=$?
|
||||
rm -rf "$fixture"
|
||||
[ "$contract_succeeded" -ne 0 ]
|
||||
}
|
||||
|
||||
@test "a parser failure cannot leave the contract green" {
|
||||
fixture=$(mktemp -d)
|
||||
mkdir -p "$fixture/packages/system/example/alerts"
|
||||
|
||||
@@ -561,3 +561,40 @@ data:
|
||||
version: "${version}"
|
||||
EOF
|
||||
}
|
||||
|
||||
@test "Tenant namespace delete policy refuses a direct delete and keeps the Tenant uninstall" {
|
||||
if ! kubectl api-resources --api-group=admissionregistration.k8s.io \
|
||||
2>/dev/null | grep -qw validatingadmissionpolicies; then
|
||||
skip "ValidatingAdmissionPolicy API not available on this cluster"
|
||||
fi
|
||||
kubectl get validatingadmissionpolicy cozystack-tenant-namespace-delete-policy
|
||||
kubectl get validatingadmissionpolicybinding cozystack-tenant-namespace-delete-policy
|
||||
|
||||
kubectl apply -f - <<EOF
|
||||
apiVersion: apps.cozystack.io/v1alpha1
|
||||
kind: Tenant
|
||||
metadata:
|
||||
name: deleteprobe
|
||||
namespace: tenant-root
|
||||
spec: {}
|
||||
EOF
|
||||
timeout 60 sh -ec 'until kubectl get hr/tenant-deleteprobe -n tenant-root >/dev/null 2>&1; do sleep 2; done'
|
||||
kubectl wait hr/tenant-deleteprobe -n tenant-root --timeout=1m --for=condition=ready
|
||||
|
||||
local selected output rc still_there
|
||||
selected=$(kubectl get namespaces --selector tenant.cozystack.io/tenant-root --field-selector metadata.name=tenant-deleteprobe -o name)
|
||||
[ "$selected" = "namespace/tenant-deleteprobe" ]
|
||||
|
||||
output=$(kubectl delete namespace tenant-deleteprobe --dry-run=server 2>&1) && rc=0 || rc=$?
|
||||
echo "kubectl delete exit=$rc, output=$output"
|
||||
[ "$rc" -ne 0 ]
|
||||
echo "$output" | grep -qF "ValidatingAdmissionPolicy 'cozystack-tenant-namespace-delete-policy'"
|
||||
echo "$output" | grep -qF 'kubectl delete tenants.apps.cozystack.io deleteprobe --namespace tenant-root.'
|
||||
|
||||
kubectl delete tenants.apps.cozystack.io deleteprobe -n tenant-root --timeout=5m
|
||||
kubectl wait namespace/tenant-deleteprobe --for=delete --timeout=5m
|
||||
if ! still_there=$(kubectl get namespace tenant-deleteprobe --ignore-not-found -o name); then
|
||||
echo "FAIL: reading the tenant-deleteprobe namespace did not answer"; false
|
||||
fi
|
||||
if [ -n "$still_there" ]; then echo "FAIL: deleting the Tenant must remove its namespace"; false; fi
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env bats
|
||||
# Replays the LINSTOR error-report alerts from
|
||||
# packages/system/piraeus-operator/alerts/piraeus-datastore.yaml through
|
||||
# vmalert-tool, pinned to the VictoriaMetrics release that the
|
||||
# victoria-metrics-operator deploys by default. Its increase() counts the first
|
||||
# samples of a series as new when they are small, and the report counts are
|
||||
# small, so the cases pin that a series which starts over after a controller
|
||||
# move, a label change or a disconnect does not report old error reports again.
|
||||
# Only pulling the image touches the network.
|
||||
|
||||
load test_helper
|
||||
|
||||
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
|
||||
VMALERT_TOOL_IMAGE="victoriametrics/vmalert-tool:v1.136.0@sha256:417346d7b6fdb66d8e3984887c2ad114535ec7d983eeef53381b56c776f333f1"
|
||||
|
||||
@test "LINSTOR error-report alerts pass their vmalert-tool cases" {
|
||||
docker info >/dev/null 2>&1 || {
|
||||
echo "docker is required to run the cases in $VMALERT_TOOL_IMAGE" >&2
|
||||
return 1
|
||||
}
|
||||
work=$(mktemp -d)
|
||||
yq '.spec' "$REPO_ROOT/packages/system/piraeus-operator/alerts/piraeus-datastore.yaml" > "$work/rules.yaml"
|
||||
cp "$REPO_ROOT/hack/testdata/linstor-error-report-alerts/unittest.yaml" "$work/unittest.yaml"
|
||||
rc=0
|
||||
docker run --rm --network none -v "$work:/w:ro" "$VMALERT_TOOL_IMAGE" \
|
||||
unittest --files /w/unittest.yaml > "$work/out" 2>&1 || rc=$?
|
||||
cat "$work/out"
|
||||
passed=$(grep -cx 'SUCCESS' "$work/out" || true)
|
||||
rm -rf "$work"
|
||||
[ "$rc" -eq 0 ]
|
||||
[ "$passed" -eq 1 ]
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
rule_files:
|
||||
- rules.yaml
|
||||
|
||||
evaluation_interval: 15s
|
||||
|
||||
tests:
|
||||
- name: controller pod moves to another node
|
||||
interval: 30s
|
||||
input_series:
|
||||
- series: 'up{job="linstor-controller", instance="10.0.0.1:3370", controller_node="node-a"}'
|
||||
values: '1x38 0 stale'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", controller_node="node-a", hostname="node-a", module="SATELLITE"}'
|
||||
values: '1x38 stale'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", controller_node="node-a", hostname="node-b", module="SATELLITE"}'
|
||||
values: '2x38 stale'
|
||||
- series: 'up{job="linstor-controller", instance="10.0.0.2:3370", controller_node="node-b"}'
|
||||
values: '_x41 1x79'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.2:3370", controller_node="node-b", hostname="node-a", module="SATELLITE"}'
|
||||
values: '_x41 1x58 2x20'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.2:3370", controller_node="node-b", hostname="node-b", module="SATELLITE"}'
|
||||
values: '_x41 2x79'
|
||||
alert_rule_test:
|
||||
- eval_time: 21m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
- eval_time: 28m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
- eval_time: 35m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
- eval_time: 45m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
- eval_time: 51m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
exp_alerts:
|
||||
- exp_labels:
|
||||
hostname: node-a
|
||||
severity: warning
|
||||
exp_annotations:
|
||||
description: |
|
||||
LINSTOR Satellite "node-a" reports 1 errors in the last 15 minutes.
|
||||
Use "linstor error-reports list --nodes node-a" to inspect the reports.
|
||||
|
||||
- name: an external label of the scrape changes
|
||||
interval: 30s
|
||||
input_series:
|
||||
- series: 'up{job="linstor-controller", instance="10.0.0.1:3370", cluster="cozystack"}'
|
||||
values: '1x39 stale'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", cluster="cozystack", hostname="node-a", module="SATELLITE"}'
|
||||
values: '1x39 stale'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", cluster="cozystack", hostname="linstor-controller-abc", module="CONTROLLER"}'
|
||||
values: '1x39 stale'
|
||||
- series: 'up{job="linstor-controller", instance="10.0.0.1:3370", cluster="cluster-a"}'
|
||||
values: '_x41 1x79'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", cluster="cluster-a", hostname="node-a", module="SATELLITE"}'
|
||||
values: '_x41 1x79'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", cluster="cluster-a", hostname="linstor-controller-abc", module="CONTROLLER"}'
|
||||
values: '_x41 1x79'
|
||||
alert_rule_test:
|
||||
- eval_time: 21m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
- eval_time: 30m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
- eval_time: 21m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorControllerErrorRate
|
||||
- eval_time: 30m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorControllerErrorRate
|
||||
|
||||
- name: a satellite drops out of the metric while it is disconnected
|
||||
interval: 30s
|
||||
input_series:
|
||||
- series: 'up{job="linstor-controller", instance="10.0.0.1:3370"}'
|
||||
values: '1x220'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", hostname="node-a", module="SATELLITE"}'
|
||||
values: '1x38 stale _x80 1x100'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", hostname="node-b", module="SATELLITE"}'
|
||||
values: '2x220'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", hostname="node-c", module="SATELLITE"}'
|
||||
values: '1x38 stale _x80 2x100'
|
||||
alert_rule_test:
|
||||
- eval_time: 61m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
exp_alerts:
|
||||
- exp_labels:
|
||||
hostname: node-c
|
||||
severity: warning
|
||||
exp_annotations:
|
||||
description: |
|
||||
LINSTOR Satellite "node-c" reports 1 errors in the last 15 minutes.
|
||||
Use "linstor error-reports list --nodes node-c" to inspect the reports.
|
||||
- eval_time: 70m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
exp_alerts:
|
||||
- exp_labels:
|
||||
hostname: node-c
|
||||
severity: warning
|
||||
exp_annotations:
|
||||
description: |
|
||||
LINSTOR Satellite "node-c" reports 1 errors in the last 15 minutes.
|
||||
Use "linstor error-reports list --nodes node-c" to inspect the reports.
|
||||
- eval_time: 76m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
|
||||
- name: a new report fires for the 15 minute window
|
||||
interval: 30s
|
||||
input_series:
|
||||
- series: 'up{job="linstor-controller", instance="10.0.0.1:3370"}'
|
||||
values: '1x120'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", hostname="node-a", module="SATELLITE"}'
|
||||
values: '1x39 2x80'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", hostname="node-b", module="SATELLITE"}'
|
||||
values: '2x120'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", hostname="node-c", module="SATELLITE"}'
|
||||
values: '0x39 1x80'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", hostname="linstor-controller-abc", module="CONTROLLER"}'
|
||||
values: '0x39 1x80'
|
||||
alert_rule_test:
|
||||
- eval_time: 21m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
exp_alerts:
|
||||
- exp_labels:
|
||||
hostname: node-a
|
||||
severity: warning
|
||||
exp_annotations:
|
||||
description: |
|
||||
LINSTOR Satellite "node-a" reports 1 errors in the last 15 minutes.
|
||||
Use "linstor error-reports list --nodes node-a" to inspect the reports.
|
||||
- exp_labels:
|
||||
hostname: node-c
|
||||
severity: warning
|
||||
exp_annotations:
|
||||
description: |
|
||||
LINSTOR Satellite "node-c" reports 1 errors in the last 15 minutes.
|
||||
Use "linstor error-reports list --nodes node-c" to inspect the reports.
|
||||
- eval_time: 34m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
exp_alerts:
|
||||
- exp_labels:
|
||||
hostname: node-a
|
||||
severity: warning
|
||||
exp_annotations:
|
||||
description: |
|
||||
LINSTOR Satellite "node-a" reports 1 errors in the last 15 minutes.
|
||||
Use "linstor error-reports list --nodes node-a" to inspect the reports.
|
||||
- exp_labels:
|
||||
hostname: node-c
|
||||
severity: warning
|
||||
exp_annotations:
|
||||
description: |
|
||||
LINSTOR Satellite "node-c" reports 1 errors in the last 15 minutes.
|
||||
Use "linstor error-reports list --nodes node-c" to inspect the reports.
|
||||
- eval_time: 36m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
- eval_time: 21m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorControllerErrorRate
|
||||
exp_alerts:
|
||||
- exp_labels:
|
||||
hostname: linstor-controller-abc
|
||||
severity: warning
|
||||
exp_annotations:
|
||||
description: |
|
||||
LINSTOR Controller reports 1 errors in the last 15 minutes.
|
||||
Use "linstor error-reports list --since 15minutes" to see them.
|
||||
- eval_time: 36m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorControllerErrorRate
|
||||
|
||||
- name: a failed controller scrape still holds the alert back
|
||||
interval: 30s
|
||||
input_series:
|
||||
- series: 'up{job="linstor-controller", instance="10.0.0.1:3370"}'
|
||||
values: '1x19 0x140 1x60'
|
||||
- series: 'linstor_error_reports_count{job="linstor-controller", instance="10.0.0.1:3370", hostname="node-a", module="SATELLITE"}'
|
||||
values: '1x19 stale _x140 1x60'
|
||||
alert_rule_test:
|
||||
- eval_time: 85m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
- eval_time: 93m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
- eval_time: 100m
|
||||
groupname: linstor.rules
|
||||
alertname: linstorSatelliteErrorRate
|
||||
@@ -0,0 +1,572 @@
|
||||
rule_files:
|
||||
- rules.yaml
|
||||
|
||||
evaluation_interval: 15s
|
||||
|
||||
tests:
|
||||
- name: two of three StatefulSet pods on one node
|
||||
interval: 1m
|
||||
input_series:
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="db-0", node="node-a", created_by_kind="StatefulSet", created_by_name="db"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="db-1", node="node-a", created_by_kind="StatefulSet", created_by_name="db"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="db-2", node="node-b", created_by_kind="StatefulSet", created_by_name="db"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="db-0", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="db-1", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="db-2", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
alert_rule_test:
|
||||
- eval_time: 59m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
- eval_time: 61m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
exp_alerts:
|
||||
- exp_labels:
|
||||
cluster: cozystack
|
||||
namespace: tenant-a
|
||||
owner_kind: StatefulSet
|
||||
owner_name: db
|
||||
node: node-a
|
||||
severity: warning
|
||||
exported_instance: tenant-a/db
|
||||
service: workload-placement
|
||||
exp_annotations:
|
||||
summary: Pods of one workload share a node.
|
||||
description: >-
|
||||
2 running pods of StatefulSet tenant-a/db are on node node-a in cluster cozystack,
|
||||
although the cluster has a schedulable node for each of its pods. Losing this node
|
||||
stops these pods together, which can cost a quorum-based workload its quorum. The
|
||||
scheduler does not move running pods, so they stay together until they are recreated.
|
||||
|
||||
- name: StatefulSet pods spread over three nodes
|
||||
interval: 1m
|
||||
input_series:
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="db-0", node="node-a", created_by_kind="StatefulSet", created_by_name="db"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="db-1", node="node-b", created_by_kind="StatefulSet", created_by_name="db"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="db-2", node="node-c", created_by_kind="StatefulSet", created_by_name="db"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="db-0", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="db-1", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="db-2", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
alert_rule_test:
|
||||
- eval_time: 61m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
- eval_time: 120m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
|
||||
- name: five ReplicaSet pods on three nodes
|
||||
interval: 1m
|
||||
input_series:
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="worker-7c8d9-aaaaa", node="node-a", created_by_kind="ReplicaSet", created_by_name="worker-7c8d9"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="worker-7c8d9-bbbbb", node="node-a", created_by_kind="ReplicaSet", created_by_name="worker-7c8d9"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="worker-7c8d9-ccccc", node="node-b", created_by_kind="ReplicaSet", created_by_name="worker-7c8d9"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="worker-7c8d9-ddddd", node="node-b", created_by_kind="ReplicaSet", created_by_name="worker-7c8d9"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="worker-7c8d9-eeeee", node="node-c", created_by_kind="ReplicaSet", created_by_name="worker-7c8d9"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="worker-7c8d9-aaaaa", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="worker-7c8d9-bbbbb", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="worker-7c8d9-ccccc", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="worker-7c8d9-ddddd", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="worker-7c8d9-eeeee", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
alert_rule_test:
|
||||
- eval_time: 61m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
- eval_time: 120m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
|
||||
- name: DaemonSet surge pod next to the old one
|
||||
interval: 1m
|
||||
input_series:
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="cozy-system", pod="node-agent-x7k2p", node="node-a", created_by_kind="DaemonSet", created_by_name="node-agent"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="cozy-system", pod="node-agent-q9z4m", node="node-a", created_by_kind="DaemonSet", created_by_name="node-agent"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="cozy-system", pod="node-agent-b5n8w", node="node-b", created_by_kind="DaemonSet", created_by_name="node-agent"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="cozy-system", pod="node-agent-x7k2p", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="cozy-system", pod="node-agent-q9z4m", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="cozy-system", pod="node-agent-b5n8w", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
alert_rule_test:
|
||||
- eval_time: 61m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
- eval_time: 120m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
|
||||
- name: two pods of one Job on one node
|
||||
interval: 1m
|
||||
input_series:
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="backup-29345678-h2k4m", node="node-a", created_by_kind="Job", created_by_name="backup-29345678"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="backup-29345678-p8r6t", node="node-a", created_by_kind="Job", created_by_name="backup-29345678"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="backup-29345678-h2k4m", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="backup-29345678-p8r6t", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
alert_rule_test:
|
||||
- eval_time: 61m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
|
||||
- name: etcd members of one EtcdCluster on one node
|
||||
interval: 1m
|
||||
input_series:
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="etcd-x7k2p", node="node-a", created_by_kind="EtcdMember", created_by_name="etcd-x7k2p"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="etcd-q9z4m", node="node-a", created_by_kind="EtcdMember", created_by_name="etcd-q9z4m"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="etcd-b5n8w", node="node-b", created_by_kind="EtcdMember", created_by_name="etcd-b5n8w"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-b", pod="etcd-0", node="node-b", created_by_kind="EtcdMember", created_by_name="etcd-0"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-b", pod="etcd-1", node="node-c", created_by_kind="EtcdMember", created_by_name="etcd-1"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-b", pod="etcd-2", node="node-c", created_by_kind="EtcdMember", created_by_name="etcd-2"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="etcd-x7k2p", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="etcd-q9z4m", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="etcd-b5n8w", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-b", pod="etcd-0", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-b", pod="etcd-1", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-b", pod="etcd-2", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
alert_rule_test:
|
||||
- eval_time: 61m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
exp_alerts:
|
||||
- exp_labels:
|
||||
cluster: cozystack
|
||||
namespace: tenant-a
|
||||
owner_kind: EtcdCluster
|
||||
owner_name: etcd
|
||||
node: node-a
|
||||
severity: warning
|
||||
exported_instance: tenant-a/etcd
|
||||
service: workload-placement
|
||||
exp_annotations:
|
||||
summary: Pods of one workload share a node.
|
||||
description: >-
|
||||
2 running pods of EtcdCluster tenant-a/etcd are on node node-a in cluster cozystack,
|
||||
although the cluster has a schedulable node for each of its pods. Losing this node
|
||||
stops these pods together, which can cost a quorum-based workload its quorum. The
|
||||
scheduler does not move running pods, so they stay together until they are recreated.
|
||||
- exp_labels:
|
||||
cluster: cozystack
|
||||
namespace: tenant-b
|
||||
owner_kind: EtcdCluster
|
||||
owner_name: etcd
|
||||
node: node-c
|
||||
severity: warning
|
||||
exported_instance: tenant-b/etcd
|
||||
service: workload-placement
|
||||
exp_annotations:
|
||||
summary: Pods of one workload share a node.
|
||||
description: >-
|
||||
2 running pods of EtcdCluster tenant-b/etcd are on node node-c in cluster cozystack,
|
||||
although the cluster has a schedulable node for each of its pods. Losing this node
|
||||
stops these pods together, which can cost a quorum-based workload its quorum. The
|
||||
scheduler does not move running pods, so they stay together until they are recreated.
|
||||
|
||||
- name: replacement pod next to its terminating predecessor for twenty minutes
|
||||
interval: 1m
|
||||
input_series:
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="web-6b8f4-aaaaa", node="node-a", created_by_kind="ReplicaSet", created_by_name="web-6b8f4"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="web-6b8f4-bbbbb", node="node-b", created_by_kind="ReplicaSet", created_by_name="web-6b8f4"}'
|
||||
values: '1x30 stale'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="web-6b8f4-ccccc", node="node-b", created_by_kind="ReplicaSet", created_by_name="web-6b8f4"}'
|
||||
values: '_x10 1x140'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="web-6b8f4-aaaaa", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="web-6b8f4-bbbbb", phase="Running", node="node-c"}'
|
||||
values: '1x30 stale'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="web-6b8f4-ccccc", phase="Running", node="node-c"}'
|
||||
values: '_x10 1x140'
|
||||
alert_rule_test:
|
||||
- eval_time: 29m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
- eval_time: 75m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
- eval_time: 120m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
|
||||
- name: the same overlap held past the hour
|
||||
interval: 1m
|
||||
input_series:
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="web-6b8f4-aaaaa", node="node-a", created_by_kind="ReplicaSet", created_by_name="web-6b8f4"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="web-6b8f4-bbbbb", node="node-b", created_by_kind="ReplicaSet", created_by_name="web-6b8f4"}'
|
||||
values: '1x100 stale'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="web-6b8f4-ccccc", node="node-b", created_by_kind="ReplicaSet", created_by_name="web-6b8f4"}'
|
||||
values: '_x10 1x140'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="web-6b8f4-aaaaa", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="web-6b8f4-bbbbb", phase="Running", node="node-c"}'
|
||||
values: '1x100 stale'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="web-6b8f4-ccccc", phase="Running", node="node-c"}'
|
||||
values: '_x10 1x140'
|
||||
alert_rule_test:
|
||||
- eval_time: 69m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
- eval_time: 71m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
exp_alerts:
|
||||
- exp_labels:
|
||||
cluster: cozystack
|
||||
namespace: tenant-a
|
||||
owner_kind: ReplicaSet
|
||||
owner_name: web-6b8f4
|
||||
node: node-b
|
||||
severity: warning
|
||||
exported_instance: tenant-a/web-6b8f4
|
||||
service: workload-placement
|
||||
exp_annotations:
|
||||
summary: Pods of one workload share a node.
|
||||
description: >-
|
||||
2 running pods of ReplicaSet tenant-a/web-6b8f4 are on node node-b in cluster cozystack,
|
||||
although the cluster has a schedulable node for each of its pods. Losing this node
|
||||
stops these pods together, which can cost a quorum-based workload its quorum. The
|
||||
scheduler does not move running pods, so they stay together until they are recreated.
|
||||
- eval_time: 105m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
|
||||
- name: a cordoned node does not count until it is uncordoned
|
||||
interval: 1m
|
||||
input_series:
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_spec_unschedulable{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '0x150'
|
||||
- series: 'kube_node_spec_unschedulable{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '0x150'
|
||||
- series: 'kube_node_spec_unschedulable{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x30 0x120'
|
||||
- series: 'kube_node_spec_taint{job="kube-state-metrics", cluster="cozystack", node="node-c", key="node.kubernetes.io/unschedulable", value="", effect="NoSchedule"}'
|
||||
values: '1x30 stale'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kv-0", node="node-a", created_by_kind="StatefulSet", created_by_name="kv"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kv-1", node="node-a", created_by_kind="StatefulSet", created_by_name="kv"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kv-2", node="node-b", created_by_kind="StatefulSet", created_by_name="kv"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kv-0", phase="Running", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kv-1", phase="Running", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kv-2", phase="Running", node="node-a"}'
|
||||
values: '1x150'
|
||||
alert_rule_test:
|
||||
- eval_time: 61m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
- eval_time: 90m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
- eval_time: 92m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
exp_alerts:
|
||||
- exp_labels:
|
||||
cluster: cozystack
|
||||
namespace: tenant-a
|
||||
owner_kind: StatefulSet
|
||||
owner_name: kv
|
||||
node: node-a
|
||||
severity: warning
|
||||
exported_instance: tenant-a/kv
|
||||
service: workload-placement
|
||||
exp_annotations:
|
||||
summary: Pods of one workload share a node.
|
||||
description: >-
|
||||
2 running pods of StatefulSet tenant-a/kv are on node node-a in cluster cozystack,
|
||||
although the cluster has a schedulable node for each of its pods. Losing this node
|
||||
stops these pods together, which can cost a quorum-based workload its quorum. The
|
||||
scheduler does not move running pods, so they stay together until they are recreated.
|
||||
|
||||
- name: a node that is down does not count
|
||||
interval: 1m
|
||||
input_series:
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_spec_unschedulable{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '0x150'
|
||||
- series: 'kube_node_spec_unschedulable{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '0x150'
|
||||
- series: 'kube_node_spec_unschedulable{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '0x150'
|
||||
- series: 'kube_node_spec_taint{job="kube-state-metrics", cluster="cozystack", node="node-c", key="node.kubernetes.io/unreachable", value="", effect="NoSchedule"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_spec_taint{job="kube-state-metrics", cluster="cozystack", node="node-c", key="node.kubernetes.io/unreachable", value="", effect="NoExecute"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kv-0", node="node-a", created_by_kind="StatefulSet", created_by_name="kv"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kv-1", node="node-a", created_by_kind="StatefulSet", created_by_name="kv"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kv-2", node="node-b", created_by_kind="StatefulSet", created_by_name="kv"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kv-0", phase="Running", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kv-1", phase="Running", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kv-2", phase="Running", node="node-a"}'
|
||||
values: '1x150'
|
||||
alert_rule_test:
|
||||
- eval_time: 61m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
- eval_time: 120m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
|
||||
- name: pods that are not running do not count
|
||||
interval: 1m
|
||||
input_series:
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="api-5c6d7-aaaaa", node="node-a", created_by_kind="ReplicaSet", created_by_name="api-5c6d7"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="api-5c6d7-bbbbb", node="node-a", created_by_kind="ReplicaSet", created_by_name="api-5c6d7"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="api-5c6d7-ccccc", node="node-b", created_by_kind="ReplicaSet", created_by_name="api-5c6d7"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="api-5c6d7-aaaaa", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="api-5c6d7-bbbbb", phase="Running", node="node-c"}'
|
||||
values: '0x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="api-5c6d7-bbbbb", phase="Failed", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="api-5c6d7-ccccc", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
alert_rule_test:
|
||||
- eval_time: 61m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
|
||||
- name: one pod each in two clusters that share workload and node names
|
||||
interval: 1m
|
||||
input_series:
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="kubernetes-example", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="kubernetes-example", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="kubernetes-example", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="kube-system", pod="coredns-5d78c9869d-x7k2p", node="node-a", created_by_kind="ReplicaSet", created_by_name="coredns-5d78c9869d"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="kubernetes-example", namespace="kube-system", pod="coredns-5d78c9869d-q9z4m", node="node-a", created_by_kind="ReplicaSet", created_by_name="coredns-5d78c9869d"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="kube-system", pod="coredns-5d78c9869d-x7k2p", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="kubernetes-example", namespace="kube-system", pod="coredns-5d78c9869d-q9z4m", phase="Running", node="node-b"}'
|
||||
values: '1x150'
|
||||
alert_rule_test:
|
||||
- eval_time: 61m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
|
||||
- name: ReplicaSet, CNPG Cluster and StrimziPodSet pods on one node
|
||||
interval: 1m
|
||||
input_series:
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-a"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-b"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_node_info{job="kube-state-metrics", cluster="cozystack", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="rfs-cache-6f7d8b9c5d-h2k4m", node="node-a", created_by_kind="ReplicaSet", created_by_name="rfs-cache-6f7d8b9c5d"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="rfs-cache-6f7d8b9c5d-p8r6t", node="node-a", created_by_kind="ReplicaSet", created_by_name="rfs-cache-6f7d8b9c5d"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="rfs-cache-6f7d8b9c5d-w3x5z", node="node-b", created_by_kind="ReplicaSet", created_by_name="rfs-cache-6f7d8b9c5d"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="postgres-db-1", node="node-a", created_by_kind="Cluster", created_by_name="postgres-db"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="postgres-db-2", node="node-b", created_by_kind="Cluster", created_by_name="postgres-db"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="postgres-db-3", node="node-b", created_by_kind="Cluster", created_by_name="postgres-db"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kafka-kafka-0", node="node-c", created_by_kind="StrimziPodSet", created_by_name="kafka-kafka"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kafka-kafka-1", node="node-c", created_by_kind="StrimziPodSet", created_by_name="kafka-kafka"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_info{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kafka-kafka-2", node="node-a", created_by_kind="StrimziPodSet", created_by_name="kafka-kafka"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="rfs-cache-6f7d8b9c5d-h2k4m", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="rfs-cache-6f7d8b9c5d-p8r6t", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="rfs-cache-6f7d8b9c5d-w3x5z", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="postgres-db-1", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="postgres-db-2", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="postgres-db-3", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kafka-kafka-0", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kafka-kafka-1", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
- series: 'kube_pod_status_phase{job="kube-state-metrics", cluster="cozystack", namespace="tenant-a", pod="kafka-kafka-2", phase="Running", node="node-c"}'
|
||||
values: '1x150'
|
||||
alert_rule_test:
|
||||
- eval_time: 61m
|
||||
groupname: workload-placement
|
||||
alertname: PodReplicasColocated
|
||||
exp_alerts:
|
||||
- exp_labels:
|
||||
cluster: cozystack
|
||||
namespace: tenant-a
|
||||
owner_kind: ReplicaSet
|
||||
owner_name: rfs-cache-6f7d8b9c5d
|
||||
node: node-a
|
||||
severity: warning
|
||||
exported_instance: tenant-a/rfs-cache-6f7d8b9c5d
|
||||
service: workload-placement
|
||||
exp_annotations:
|
||||
summary: Pods of one workload share a node.
|
||||
description: >-
|
||||
2 running pods of ReplicaSet tenant-a/rfs-cache-6f7d8b9c5d are on node node-a in cluster
|
||||
cozystack, although the cluster has a schedulable node for each of its pods. Losing this
|
||||
node stops these pods together, which can cost a quorum-based workload its quorum. The
|
||||
scheduler does not move running pods, so they stay together until they are recreated.
|
||||
- exp_labels:
|
||||
cluster: cozystack
|
||||
namespace: tenant-a
|
||||
owner_kind: Cluster
|
||||
owner_name: postgres-db
|
||||
node: node-b
|
||||
severity: warning
|
||||
exported_instance: tenant-a/postgres-db
|
||||
service: workload-placement
|
||||
exp_annotations:
|
||||
summary: Pods of one workload share a node.
|
||||
description: >-
|
||||
2 running pods of Cluster tenant-a/postgres-db are on node node-b in cluster cozystack,
|
||||
although the cluster has a schedulable node for each of its pods. Losing this node
|
||||
stops these pods together, which can cost a quorum-based workload its quorum. The
|
||||
scheduler does not move running pods, so they stay together until they are recreated.
|
||||
- exp_labels:
|
||||
cluster: cozystack
|
||||
namespace: tenant-a
|
||||
owner_kind: StrimziPodSet
|
||||
owner_name: kafka-kafka
|
||||
node: node-c
|
||||
severity: warning
|
||||
exported_instance: tenant-a/kafka-kafka
|
||||
service: workload-placement
|
||||
exp_annotations:
|
||||
summary: Pods of one workload share a node.
|
||||
description: >-
|
||||
2 running pods of StrimziPodSet tenant-a/kafka-kafka are on node node-c in cluster
|
||||
cozystack, although the cluster has a schedulable node for each of its pods. Losing
|
||||
this node stops these pods together, which can cost a quorum-based workload its quorum.
|
||||
The scheduler does not move running pods, so they stay together until they are
|
||||
recreated.
|
||||
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env bats
|
||||
|
||||
load test_helper
|
||||
|
||||
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
|
||||
VMALERT_TOOL_IMAGE="victoriametrics/vmalert-tool:v1.136.0@sha256:417346d7b6fdb66d8e3984887c2ad114535ec7d983eeef53381b56c776f333f1"
|
||||
|
||||
@test "workload placement alerts pass their vmalert-tool cases" {
|
||||
docker info >/dev/null 2>&1 || {
|
||||
echo "docker is required to run the cases in $VMALERT_TOOL_IMAGE" >&2
|
||||
return 1
|
||||
}
|
||||
work=$(mktemp -d)
|
||||
rc=0
|
||||
yq '.spec' "$REPO_ROOT/packages/system/monitoring-agents/alerts/workload-placement.yaml" > "$work/rules.yaml" || rc=$?
|
||||
cp "$REPO_ROOT/hack/testdata/workload-placement-alerts/unittest.yaml" "$work/unittest.yaml" || rc=$?
|
||||
if [ "$rc" -eq 0 ]; then
|
||||
docker run --rm --network none -v "$work:/w:ro" "$VMALERT_TOOL_IMAGE" \
|
||||
unittest --files /w/unittest.yaml > "$work/out" 2>&1 || rc=$?
|
||||
cat "$work/out"
|
||||
fi
|
||||
passed=$(grep -cx 'SUCCESS' "$work/out" 2>/dev/null || true)
|
||||
rm -rf "$work"
|
||||
[ "$rc" -eq 0 ]
|
||||
[ "$passed" -eq 1 ]
|
||||
}
|
||||
@@ -631,7 +631,7 @@ func (r *BackupJobReconciler) createAltinityBackupArtifact(
|
||||
// used name meets the earlier run's Backup here. Only one recording
|
||||
// this run's archive was created by an earlier reconcile of this run.
|
||||
if got := existing.Spec.DriverMetadata[altinityArchiveMetaKey]; got != archive {
|
||||
return nil, fmt.Errorf("Backup %s/%s already exists and records archive %q, not %q that this run wrote",
|
||||
return nil, fmt.Errorf("existing Backup %s/%s records archive %q, not %q that this run wrote",
|
||||
backup.Namespace, backup.Name, got, archive)
|
||||
}
|
||||
return existing, nil
|
||||
|
||||
@@ -183,6 +183,9 @@ func (r *BackupReconciler) cleanupOnDelete(ctx context.Context, backup *backupsv
|
||||
// deletes the object and WAITS for that delete before the Backup is
|
||||
// removed, so no object is orphaned (see cleanupKafkaBackup).
|
||||
return r.cleanupKafkaBackup(ctx, backup)
|
||||
case strategyv1alpha1.KubernetesStrategyKind:
|
||||
// Owns two artifacts: the Velero backup and the etcd dump object.
|
||||
return r.cleanupKubernetesBackup(ctx, backup)
|
||||
case strategyv1alpha1.VeleroStrategyKind:
|
||||
return ctrl.Result{}, r.cleanupVeleroBackup(ctx, backup)
|
||||
default:
|
||||
|
||||
@@ -214,6 +214,8 @@ func (r *BackupJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
|
||||
return r.reconcileRedis(ctx, j, resolved)
|
||||
case strategyv1alpha1.KafkaStrategyKind:
|
||||
return r.reconcileKafka(ctx, j, resolved)
|
||||
case strategyv1alpha1.KubernetesStrategyKind:
|
||||
return r.reconcileKubernetes(ctx, j, resolved)
|
||||
default:
|
||||
logger.V(1).Info("BackupJob resolved StrategyRef.Kind not supported, skipping",
|
||||
"backupjob", j.Name,
|
||||
@@ -240,6 +242,7 @@ func supportedBackupStrategyKinds() []string {
|
||||
strategyv1alpha1.RabbitmqStrategyKind,
|
||||
strategyv1alpha1.RedisStrategyKind,
|
||||
strategyv1alpha1.KafkaStrategyKind,
|
||||
strategyv1alpha1.KubernetesStrategyKind,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -45,6 +45,7 @@ func TestSupportedBackupStrategyKindsMatchesDispatch(t *testing.T) {
|
||||
strategyv1alpha1.RabbitmqStrategyKind,
|
||||
strategyv1alpha1.RedisStrategyKind,
|
||||
strategyv1alpha1.KafkaStrategyKind,
|
||||
strategyv1alpha1.KubernetesStrategyKind,
|
||||
}
|
||||
sort.Strings(got)
|
||||
sort.Strings(want)
|
||||
|
||||
@@ -222,13 +222,13 @@ func cnpgListStub(prefix string, seen func(*http.Request)) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/xml")
|
||||
if _, ok := req.URL.Query()["location"]; ok {
|
||||
fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?><LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/"></LocationConstraint>`)
|
||||
_, _ = fmt.Fprint(w, `<?xml version="1.0" encoding="UTF-8"?><LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/"></LocationConstraint>`)
|
||||
return
|
||||
}
|
||||
if seen != nil {
|
||||
seen(req)
|
||||
}
|
||||
fmt.Fprintf(w, `<?xml version="1.0" encoding="UTF-8"?>
|
||||
_, _ = fmt.Fprintf(w, `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<Name>bkt</Name><Prefix>%[1]s</Prefix><KeyCount>2</KeyCount><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated>
|
||||
<Contents><Key>%[1]sbackup.info</Key><Size>7</Size><ETag>"a"</ETag><LastModified>2026-10-21T10:01:00.000Z</LastModified></Contents>
|
||||
|
||||
@@ -2718,9 +2718,12 @@ func buildBarmanPlugin(objectStoreName, serverName string) cnpgtypes.PluginConfi
|
||||
// high-water mark on a 38 MB database is 254 MiB, so 128Mi cannot hold it. A
|
||||
// tenant that leaves resourceQuotas empty has no LimitRange, and there the
|
||||
// sidecar had no requests and no limit at all. 256Mi holds the measured working
|
||||
// set and 1Gi is four times the measurement. No CPU limit is set, matching the
|
||||
// entityOperator precedent in packages/apps/kafka: a throttled sidecar stalls
|
||||
// WAL archiving instead of failing it.
|
||||
// set and 1Gi is four times the measurement. The CPU limit is explicit for the
|
||||
// same reason: left unset, that LimitRange gives the sidecar its 250m default,
|
||||
// so gzip compression of full backups and WAL segments runs on a quarter of a
|
||||
// core (a 1.6 GB database took 30 minutes to back up). One core keeps the
|
||||
// sidecar from starving the instance while removing that ceiling; it counts
|
||||
// against the tenant's limits.cpu budget, one core per instance pod.
|
||||
//
|
||||
// Checksum: since botocore ~1.36 the default (when_supported) attaches a
|
||||
// flexible checksum to every PutObject, which non-AWS S3-compatible backends
|
||||
@@ -2740,6 +2743,7 @@ func barmanSidecarConfiguration() *cnpgtypes.InstanceSidecarConfiguration {
|
||||
corev1.ResourceMemory: resource.MustParse("256Mi"),
|
||||
},
|
||||
Limits: corev1.ResourceList{
|
||||
corev1.ResourceCPU: resource.MustParse("1"),
|
||||
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
},
|
||||
|
||||
@@ -2156,8 +2156,11 @@ func TestApplyClusterPluginBackup_PatchesExistingCluster(t *testing.T) {
|
||||
sc.Resources.Requests.Cpu().MilliValue() != 100 {
|
||||
t.Errorf("ObjectStore leaves Barman subject to undersized tenant resource defaults: %+v", sc.Resources)
|
||||
}
|
||||
if _, ok := sc.Resources.Limits[corev1.ResourceCPU]; ok {
|
||||
t.Errorf("ObjectStore caps sidecar CPU, which throttles WAL archiving instead of failing it: %+v", sc.Resources.Limits)
|
||||
// Without an explicit CPU limit the tenant LimitRange caps the sidecar at
|
||||
// its 250m default, and every backup and WAL upload compresses on a
|
||||
// quarter of a core.
|
||||
if sc.Resources.Limits.Cpu().MilliValue() != 1000 {
|
||||
t.Errorf("ObjectStore leaves the sidecar CPU limit to the tenant LimitRange default: %+v", sc.Resources.Limits)
|
||||
}
|
||||
// The ObjectStore must be owner-referenced to the Cluster so Kubernetes GC
|
||||
// removes it when the Cluster is deleted (no orphan in the platform flow,
|
||||
@@ -2830,7 +2833,7 @@ func TestReconcileCNPG_ObjectStoreApplyRecovers(t *testing.T) {
|
||||
}
|
||||
|
||||
failing = false
|
||||
j, _ = f.pass()
|
||||
_, _ = f.pass()
|
||||
f.settleDestination()
|
||||
j, backups := f.pass()
|
||||
if len(backups) != 1 {
|
||||
|
||||
@@ -593,12 +593,13 @@ func (g *DefaultObjectsGate) inspectObjects(ctx context.Context, backupClass *ba
|
||||
group = *strategy.StrategyRef.APIGroup
|
||||
}
|
||||
// When the Velero BSL is disabled (velero.bslEnabled=false, surfaced
|
||||
// here as an empty VeleroNamespace) the chart gates the Velero
|
||||
// Strategy CRs off the SAME flag, so they are never rendered. The
|
||||
// BackupClass still routes to them unconditionally, so counting them
|
||||
// as missing would force a Helm upgrade every MinForceInterval
|
||||
// forever against a render that can never produce them.
|
||||
if g.VeleroNamespace == "" && group == strategyAPIGroup && kind == "Velero" {
|
||||
// here as an empty VeleroNamespace) the chart gates the Strategy CRs
|
||||
// that back up through Velero (Velero, Kubernetes) off the SAME flag,
|
||||
// so they are never rendered. The BackupClass still routes to them
|
||||
// unconditionally, so counting them as missing would force a Helm
|
||||
// upgrade every MinForceInterval forever against a render that can
|
||||
// never produce them.
|
||||
if g.VeleroNamespace == "" && group == strategyAPIGroup && (kind == "Velero" || kind == "Kubernetes") {
|
||||
continue
|
||||
}
|
||||
key := fmt.Sprintf("%s/%s", kind, name)
|
||||
|
||||
@@ -37,6 +37,7 @@ var gateGVKs = []schema.GroupVersionKind{
|
||||
{Group: strategyAPIGroup, Version: "v1alpha1", Kind: "Etcd"},
|
||||
{Group: strategyAPIGroup, Version: "v1alpha1", Kind: "Altinity"},
|
||||
{Group: strategyAPIGroup, Version: "v1alpha1", Kind: "Velero"},
|
||||
{Group: strategyAPIGroup, Version: "v1alpha1", Kind: "Kubernetes"},
|
||||
{Group: "velero.io", Version: "v1", Kind: "BackupStorageLocation"},
|
||||
{Group: "helm.toolkit.fluxcd.io", Version: "v2", Kind: "HelmRelease"},
|
||||
}
|
||||
@@ -109,6 +110,7 @@ func cozyDefaultBackupClass() *backupsv1alpha1.BackupClass {
|
||||
ref("Altinity", "cozy-default-altinity"),
|
||||
ref("Velero", "cozy-default-velero-vminstance"),
|
||||
ref("Velero", "cozy-default-velero-vmdisk"),
|
||||
ref("Kubernetes", "cozy-default-kubernetes"),
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -149,6 +151,7 @@ func allDefaultObjects() []runtime.Object {
|
||||
strategyObject("Altinity", "cozy-default-altinity"),
|
||||
strategyObject("Velero", "cozy-default-velero-vminstance"),
|
||||
strategyObject("Velero", "cozy-default-velero-vmdisk"),
|
||||
strategyObject("Kubernetes", "cozy-default-kubernetes"),
|
||||
bslObject(),
|
||||
}
|
||||
}
|
||||
@@ -288,8 +291,8 @@ func TestCheckForcesWhenObjectsMissing(t *testing.T) {
|
||||
t.Fatal("expected a forced Helm upgrade when default objects are missing")
|
||||
}
|
||||
// 6 strategyRefs (Velero twice under different names) + the BSL.
|
||||
if len(missing) != 7 {
|
||||
t.Fatalf("missing = %v, want 7 entries", missing)
|
||||
if len(missing) != 8 {
|
||||
t.Fatalf("missing = %v, want 8 entries", missing)
|
||||
}
|
||||
ann := forceAnnotations(t, dyn)
|
||||
forceAt, ok := ann["reconcile.fluxcd.io/forceAt"]
|
||||
@@ -319,6 +322,7 @@ func TestCheckNoopWhenAllPresent(t *testing.T) {
|
||||
strategyObject("Altinity", "cozy-default-altinity"),
|
||||
strategyObject("Velero", "cozy-default-velero-vminstance"),
|
||||
strategyObject("Velero", "cozy-default-velero-vmdisk"),
|
||||
strategyObject("Kubernetes", "cozy-default-kubernetes"),
|
||||
bslObject(),
|
||||
)
|
||||
|
||||
@@ -454,8 +458,8 @@ func TestCheckThrottlesTheTwoReleasesIndependently(t *testing.T) {
|
||||
if !forced {
|
||||
t.Fatal("objects release throttled by the earlier credentials force")
|
||||
}
|
||||
if len(missing) != 7 {
|
||||
t.Fatalf("missing = %v, want the 6 strategies + the BSL", missing)
|
||||
if len(missing) != 8 {
|
||||
t.Fatalf("missing = %v, want the 7 strategies + the BSL", missing)
|
||||
}
|
||||
if ann := forceAnnotations(t, dyn); ann["reconcile.fluxcd.io/forceAt"] == "" {
|
||||
t.Errorf("own HelmRelease not forced: %v", ann)
|
||||
@@ -482,7 +486,7 @@ func TestForceSkipsSuspendedHelmRelease(t *testing.T) {
|
||||
if forced {
|
||||
t.Error("forced reported true for a suspended HelmRelease that ignores the annotations")
|
||||
}
|
||||
if len(missing) != 7 {
|
||||
if len(missing) != 8 {
|
||||
t.Fatalf("missing = %v, want the objects still reported", missing)
|
||||
}
|
||||
if ann := forceAnnotations(t, dyn); len(ann) != 0 {
|
||||
@@ -599,6 +603,7 @@ func TestMissingObjectsIgnoresUnmappedKinds(t *testing.T) {
|
||||
strategyObject("Altinity", "cozy-default-altinity"),
|
||||
strategyObject("Velero", "cozy-default-velero-vminstance"),
|
||||
strategyObject("Velero", "cozy-default-velero-vmdisk"),
|
||||
strategyObject("Kubernetes", "cozy-default-kubernetes"),
|
||||
bslObject(),
|
||||
)
|
||||
|
||||
@@ -658,6 +663,7 @@ func TestMissingObjectsSkipsBSLWhenVeleroAPIAbsent(t *testing.T) {
|
||||
strategyObject("Altinity", "cozy-default-altinity"),
|
||||
strategyObject("Velero", "cozy-default-velero-vminstance"),
|
||||
strategyObject("Velero", "cozy-default-velero-vmdisk"),
|
||||
strategyObject("Kubernetes", "cozy-default-kubernetes"),
|
||||
)
|
||||
// Rebuild the RESTMapper without BackupStorageLocation: the velero.io API
|
||||
// is not served on this cluster.
|
||||
@@ -1053,8 +1059,8 @@ func TestCheckKeepsForcingForUnannotatedObjects(t *testing.T) {
|
||||
g.lastForce = base.Add(-time.Minute)
|
||||
g.now = func() time.Time { return base }
|
||||
|
||||
if missing, forced, err := g.Check(context.Background()); err != nil || forced || len(missing) != 7 {
|
||||
t.Fatalf("throttled Check: missing = %v, forced = %v, err = %v; want 7 reported, none forced", missing, forced, err)
|
||||
if missing, forced, err := g.Check(context.Background()); err != nil || forced || len(missing) != len(allDefaultObjects()) {
|
||||
t.Fatalf("throttled Check: missing = %v, forced = %v, err = %v; want %d reported, none forced", missing, forced, err, len(allDefaultObjects()))
|
||||
}
|
||||
|
||||
g.now = func() time.Time { return base.Add(5 * time.Minute) }
|
||||
@@ -1065,8 +1071,8 @@ func TestCheckKeepsForcingForUnannotatedObjects(t *testing.T) {
|
||||
|
||||
g.now = func() time.Time { return base.Add(time.Hour) }
|
||||
missing, forced, err := g.Check(context.Background())
|
||||
if err != nil || !forced || len(missing) != 7 {
|
||||
t.Fatalf("after a render that left them unannotated: missing = %v, forced = %v, err = %v; want 7 reported and forced again", missing, forced, err)
|
||||
if err != nil || !forced || len(missing) != len(allDefaultObjects()) {
|
||||
t.Fatalf("after a render that left them unannotated: missing = %v, forced = %v, err = %v; want %d reported and forced again", missing, forced, err, len(allDefaultObjects()))
|
||||
}
|
||||
if again := forceAnnotations(t, dyn)["reconcile.fluxcd.io/forceAt"]; again == first {
|
||||
t.Errorf("not forced again for unannotated objects: forceAt still %q", first)
|
||||
|
||||
@@ -134,14 +134,14 @@ var etcdClusterGVR = schema.GroupVersionResource{
|
||||
// the wrong CRD via the typed client.
|
||||
func validateEtcdApplicationRef(ref corev1.TypedLocalObjectReference) error {
|
||||
if ref.Kind != etcdAppKind {
|
||||
return fmt.Errorf("Etcd strategy supports applicationRef.kind=%q, got %q", etcdAppKind, ref.Kind)
|
||||
return fmt.Errorf("the Etcd strategy supports applicationRef.kind=%q, got %q", etcdAppKind, ref.Kind)
|
||||
}
|
||||
apiGroup := ""
|
||||
if ref.APIGroup != nil {
|
||||
apiGroup = *ref.APIGroup
|
||||
}
|
||||
if apiGroup != "" && apiGroup != etcdapp.GroupName {
|
||||
return fmt.Errorf("Etcd strategy supports applicationRef.apiGroup=%q, got %q", etcdapp.GroupName, apiGroup)
|
||||
return fmt.Errorf("the Etcd strategy supports applicationRef.apiGroup=%q, got %q", etcdapp.GroupName, apiGroup)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -104,10 +104,10 @@ func kafkaClusterName(appName string) string {
|
||||
// apps.cozystack.io/Kafka application.
|
||||
func validateKafkaApplicationRef(ref corev1.TypedLocalObjectReference) error {
|
||||
if ref.Kind != kafkaApplicationKind {
|
||||
return fmt.Errorf("Kafka strategy only supports applicationRef.kind=%q, got %q", kafkaApplicationKind, ref.Kind)
|
||||
return fmt.Errorf("the Kafka strategy only supports applicationRef.kind=%q, got %q", kafkaApplicationKind, ref.Kind)
|
||||
}
|
||||
if ref.APIGroup != nil && *ref.APIGroup != "" && *ref.APIGroup != backupsv1alpha1.DefaultApplicationAPIGroup {
|
||||
return fmt.Errorf("Kafka strategy only supports applicationRef.apiGroup=%q, got %q", backupsv1alpha1.DefaultApplicationAPIGroup, *ref.APIGroup)
|
||||
return fmt.Errorf("the Kafka strategy only supports applicationRef.apiGroup=%q, got %q", backupsv1alpha1.DefaultApplicationAPIGroup, *ref.APIGroup)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -528,7 +528,7 @@ func (r *BackupJobReconciler) createKafkaBackupArtifact(
|
||||
// RabbitMQ driver's guard).
|
||||
if existing.Spec.ApplicationRef.Kind != backup.Spec.ApplicationRef.Kind ||
|
||||
existing.Spec.ApplicationRef.Name != backup.Spec.ApplicationRef.Name {
|
||||
return nil, fmt.Errorf("Backup %s/%s already exists for a different application (%s/%s, not %s/%s)",
|
||||
return nil, fmt.Errorf("existing Backup %s/%s belongs to a different application (%s/%s, not %s/%s)",
|
||||
backup.Namespace, backup.Name,
|
||||
existing.Spec.ApplicationRef.Kind, existing.Spec.ApplicationRef.Name,
|
||||
backup.Spec.ApplicationRef.Kind, backup.Spec.ApplicationRef.Name)
|
||||
|
||||
@@ -0,0 +1,862 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
package backupcontroller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"maps"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
batchv1 "k8s.io/api/batch/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
apimeta "k8s.io/apimachinery/pkg/api/meta"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/client-go/dynamic"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1"
|
||||
backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1"
|
||||
"github.com/cozystack/cozystack/internal/template"
|
||||
)
|
||||
|
||||
const (
|
||||
kubernetesAppKind = "Kubernetes"
|
||||
kubernetesReleasePrefix = "kubernetes-"
|
||||
|
||||
kubernetesLabelMode = "kubernetes.strategy.backups.cozystack.io/mode"
|
||||
kubernetesModeBackup = "backup"
|
||||
kubernetesModeRestore = "restore"
|
||||
kubernetesModeCleanup = "cleanup"
|
||||
// kubernetesModeVerify checks that the dump object exists before a restore
|
||||
// changes anything.
|
||||
kubernetesModeVerify = "verify"
|
||||
|
||||
// kubernetesCleanupAttemptsAnnotation counts failed cleanup Jobs on the
|
||||
// Backup, so a persistently failing delete (revoked credentials, a removed
|
||||
// bucket) ends in a released Backup and a Warning rather than a Job storm.
|
||||
kubernetesCleanupAttemptsAnnotation = "kubernetes.strategy.backups.cozystack.io/cleanup-attempts"
|
||||
kubernetesCleanupMaxAttempts = 5
|
||||
|
||||
kubernetesMetaPrefix = "kubernetes.strategy.backups.cozystack.io/"
|
||||
kubernetesObjectMetaKey = kubernetesMetaPrefix + "object"
|
||||
kubernetesDatastorePrefixMetaKey = kubernetesMetaPrefix + "datastore-prefix"
|
||||
kubernetesDatastoreEndpointsMetaKey = kubernetesMetaPrefix + "datastore-endpoints"
|
||||
kubernetesHelmReleasesMetaKey = kubernetesMetaPrefix + "helmreleases"
|
||||
kubernetesDataVolumesMetaKey = kubernetesMetaPrefix + "datavolumes"
|
||||
kubernetesCertWarningsMetaKey = kubernetesMetaPrefix + "certificate-warnings"
|
||||
kubernetesControlPlaneUIDMetaKey = kubernetesMetaPrefix + "tenantcontrolplane-uid"
|
||||
kubernetesParamPrefix = kubernetesMetaPrefix + "parameter/"
|
||||
|
||||
// kubernetesCondCertificates is set on the BackupJob once the control-plane
|
||||
// certificates have been checked; Status=True means at least one expires
|
||||
// inside the warning window.
|
||||
kubernetesCondCertificates = "CertificatesExpiring"
|
||||
|
||||
kubernetesPollInterval = 5 * time.Second
|
||||
// Leaf certificates are rotated by Kamaji and cert-manager, so a near
|
||||
// expiry is only worth a warning when it is this close.
|
||||
kubernetesDefaultCertThreshold = 720 * time.Hour
|
||||
// A CA must outlive every backup that may still be restored, so its
|
||||
// window is the Velero backup TTL; this is the fallback when none is set.
|
||||
kubernetesDefaultBackupTTL = 720 * time.Hour
|
||||
|
||||
helmReleaseNameAnnotation = "meta.helm.sh/release-name"
|
||||
clusterNameLabel = "cluster.x-k8s.io/cluster-name"
|
||||
helmInstanceLabel = "app.kubernetes.io/instance"
|
||||
kamajiNameLabel = "kamaji.clastix.io/name"
|
||||
)
|
||||
|
||||
var (
|
||||
tenantControlPlaneGVR = schema.GroupVersionResource{Group: "kamaji.clastix.io", Version: "v1alpha1", Resource: "tenantcontrolplanes"}
|
||||
kamajiDataStoreGVR = schema.GroupVersionResource{Group: "kamaji.clastix.io", Version: "v1alpha1", Resource: "datastores"}
|
||||
kamajiControlPlaneGVR = schema.GroupVersionResource{Group: "controlplane.cluster.x-k8s.io", Version: "v1alpha1", Resource: "kamajicontrolplanes"}
|
||||
machineDeploymentGVR = schema.GroupVersionResource{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machinedeployments"}
|
||||
machineGVR = schema.GroupVersionResource{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machines"}
|
||||
deploymentGVR = schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}
|
||||
)
|
||||
|
||||
// kubernetesCertificateSecrets lists, by suffix after the release name, the
|
||||
// Secrets whose certificates a backup checks. Kamaji and cert-manager rotate
|
||||
// the leaves; the CAs are irreplaceable, so a restore needs them valid.
|
||||
var kubernetesCertificateSecrets = []string{
|
||||
"ca",
|
||||
"front-proxy-ca-certificate",
|
||||
"api-server-certificate",
|
||||
"api-server-kubelet-client-certificate",
|
||||
"front-proxy-client-certificate",
|
||||
"talos-ca",
|
||||
"talos-tls-cert",
|
||||
}
|
||||
|
||||
// kubernetesUnlabelledSecrets are issued by cert-manager without the Helm
|
||||
// instance label the Velero selector relies on; the driver adds it before the
|
||||
// backup so the Talos PKI is not silently left out.
|
||||
var kubernetesUnlabelledSecrets = []string{"talos-ca", "talos-tls-cert"}
|
||||
|
||||
func kubernetesReleaseName(appName string) string {
|
||||
return kubernetesReleasePrefix + appName
|
||||
}
|
||||
|
||||
func validateKubernetesApplicationRef(ref corev1.TypedLocalObjectReference) error {
|
||||
if ref.Kind != kubernetesAppKind {
|
||||
return fmt.Errorf("kubernetes strategy supports applicationRef.kind %s, got %q", kubernetesAppKind, ref.Kind)
|
||||
}
|
||||
if ref.APIGroup != nil && *ref.APIGroup != "" && *ref.APIGroup != backupsv1alpha1.DefaultApplicationAPIGroup {
|
||||
return fmt.Errorf("kubernetes strategy supports applicationRef.apiGroup=%q, got %q", backupsv1alpha1.DefaultApplicationAPIGroup, *ref.APIGroup)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func kubernetesObjectKey(namespace, appName, backupName string) string {
|
||||
return fmt.Sprintf("%s/%s/%s.etcd.json", namespace, appName, backupName)
|
||||
}
|
||||
|
||||
// kubernetesDatastore is where the tenant cluster keeps its state: a key
|
||||
// prefix in the etcd DataStore shared by every cluster of the tenant.
|
||||
type kubernetesDatastore struct {
|
||||
// ControlPlaneUID identifies the cluster across a delete and re-create
|
||||
// under the same name, which Prefix does not on a Kamaji that keys the
|
||||
// schema on the namespace and name.
|
||||
ControlPlaneUID string
|
||||
Prefix string
|
||||
Endpoints []string
|
||||
CertificateSecret string
|
||||
// SourcePrefix is the prefix the restored dump was taken under. It
|
||||
// differs from Prefix when a deleted cluster is rebuilt: Kamaji keys a new
|
||||
// TenantControlPlane's schema on its UID.
|
||||
SourcePrefix string
|
||||
}
|
||||
|
||||
func (d kubernetesDatastore) templateContext() map[string]any {
|
||||
source := d.SourcePrefix
|
||||
if source == "" {
|
||||
source = d.Prefix
|
||||
}
|
||||
return map[string]any{
|
||||
"Prefix": d.Prefix,
|
||||
"SourcePrefix": source,
|
||||
"Endpoints": strings.Join(d.Endpoints, ","),
|
||||
"CertificateSecret": d.CertificateSecret,
|
||||
}
|
||||
}
|
||||
|
||||
// etcdEndpointURL gives a scheme to a DataStore endpoint: Kamaji lists them as
|
||||
// host:port and always talks TLS to an etcd DataStore.
|
||||
func etcdEndpointURL(endpoint string) string {
|
||||
if strings.Contains(endpoint, "://") {
|
||||
return endpoint
|
||||
}
|
||||
return "https://" + endpoint
|
||||
}
|
||||
|
||||
// resolveKubernetesDatastore reads the cluster's key prefix and client
|
||||
// certificate from its TenantControlPlane and the client URLs from the
|
||||
// DataStore it points at.
|
||||
func resolveKubernetesDatastore(ctx context.Context, dyn dynamic.Interface, namespace, appName string) (*kubernetesDatastore, error) {
|
||||
release := kubernetesReleaseName(appName)
|
||||
tcp, err := dyn.Resource(tenantControlPlaneGVR).Namespace(namespace).Get(ctx, release, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if driver, _, _ := unstructured.NestedString(tcp.Object, "status", "storage", "driver"); driver != "" && driver != "etcd" {
|
||||
return nil, fmt.Errorf("TenantControlPlane %s/%s uses the %q datastore driver; only etcd is supported", namespace, release, driver)
|
||||
}
|
||||
prefix, _, _ := unstructured.NestedString(tcp.Object, "status", "storage", "setup", "schema")
|
||||
if prefix == "" {
|
||||
prefix, _, _ = unstructured.NestedString(tcp.Object, "spec", "dataStoreSchema")
|
||||
}
|
||||
if prefix == "" {
|
||||
return nil, fmt.Errorf("TenantControlPlane %s/%s reports no datastore schema yet", namespace, release)
|
||||
}
|
||||
dsName, _, _ := unstructured.NestedString(tcp.Object, "status", "storage", "dataStoreName")
|
||||
if dsName == "" {
|
||||
dsName, _, _ = unstructured.NestedString(tcp.Object, "spec", "dataStore")
|
||||
}
|
||||
if dsName == "" {
|
||||
return nil, fmt.Errorf("TenantControlPlane %s/%s names no DataStore", namespace, release)
|
||||
}
|
||||
certSecret, _, _ := unstructured.NestedString(tcp.Object, "status", "storage", "certificate", "secretName")
|
||||
if certSecret == "" {
|
||||
certSecret = release + "-datastore-certificate"
|
||||
}
|
||||
ds, err := dyn.Resource(kamajiDataStoreGVR).Get(ctx, dsName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get DataStore %s: %w", dsName, err)
|
||||
}
|
||||
raw, _, _ := unstructured.NestedStringSlice(ds.Object, "spec", "endpoints")
|
||||
if len(raw) == 0 {
|
||||
return nil, fmt.Errorf("DataStore %s lists no endpoints", dsName)
|
||||
}
|
||||
endpoints := make([]string, 0, len(raw))
|
||||
for _, e := range raw {
|
||||
endpoints = append(endpoints, etcdEndpointURL(e))
|
||||
}
|
||||
return &kubernetesDatastore{ControlPlaneUID: string(tcp.GetUID()), Prefix: prefix, Endpoints: endpoints, CertificateSecret: certSecret}, nil
|
||||
}
|
||||
|
||||
// kubernetesCertificateWarnings reports every certificate in the given Secrets
|
||||
// that expires inside its window: caWindow for CAs, leafWindow for the rest.
|
||||
// Output is sorted so a condition message is stable across reconciles.
|
||||
func kubernetesCertificateWarnings(secrets []corev1.Secret, now time.Time, caWindow, leafWindow time.Duration) []string {
|
||||
seen := map[string]bool{}
|
||||
var out []string
|
||||
for _, s := range secrets {
|
||||
for key, data := range s.Data {
|
||||
if !strings.HasSuffix(key, ".crt") {
|
||||
continue
|
||||
}
|
||||
for rest := data; ; {
|
||||
var block *pem.Block
|
||||
block, rest = pem.Decode(rest)
|
||||
if block == nil {
|
||||
break
|
||||
}
|
||||
if block.Type != "CERTIFICATE" {
|
||||
continue
|
||||
}
|
||||
cert, err := x509.ParseCertificate(block.Bytes)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
window := leafWindow
|
||||
kind := "certificate"
|
||||
if cert.IsCA {
|
||||
window = caWindow
|
||||
kind = "CA"
|
||||
}
|
||||
if !cert.NotAfter.Before(now.Add(window)) {
|
||||
continue
|
||||
}
|
||||
msg := fmt.Sprintf("%s %s/%s (CN=%s) expires %s", kind, s.Name, key, cert.Subject.CommonName, cert.NotAfter.UTC().Format(time.RFC3339))
|
||||
if !seen[msg] {
|
||||
seen[msg] = true
|
||||
out = append(out, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// kubernetesHelmReleaseNames returns the HelmRelease of the cluster followed
|
||||
// by those of its worker pools. Pools live in separate kubernetes-nodes
|
||||
// releases; the MachineDeployments of the cluster name them, which also
|
||||
// covers the older layout where the cluster release owned its pools.
|
||||
func kubernetesHelmReleaseNames(mds []unstructured.Unstructured, release string) []string {
|
||||
names := []string{release}
|
||||
for _, md := range mds {
|
||||
if cluster, _, _ := unstructured.NestedString(md.Object, "spec", "clusterName"); cluster != release {
|
||||
continue
|
||||
}
|
||||
if owner := md.GetAnnotations()[helmReleaseNameAnnotation]; owner != "" && !slices.Contains(names, owner) {
|
||||
names = append(names, owner)
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// sanitizeHelmRelease keeps what is needed to recreate a HelmRelease and
|
||||
// drops server-set metadata and status.
|
||||
func sanitizeHelmRelease(hr *unstructured.Unstructured) map[string]any {
|
||||
labels := map[string]string{}
|
||||
for k, v := range hr.GetLabels() {
|
||||
if !strings.HasPrefix(k, "velero.io/") {
|
||||
labels[k] = v
|
||||
}
|
||||
}
|
||||
meta := map[string]any{
|
||||
"name": hr.GetName(),
|
||||
"namespace": hr.GetNamespace(),
|
||||
}
|
||||
if len(labels) > 0 {
|
||||
meta["labels"] = labels
|
||||
}
|
||||
spec, _, _ := unstructured.NestedMap(hr.Object, "spec")
|
||||
delete(spec, "suspend")
|
||||
return map[string]any{
|
||||
"apiVersion": hr.GetAPIVersion(),
|
||||
"kind": hr.GetKind(),
|
||||
"metadata": meta,
|
||||
"spec": spec,
|
||||
}
|
||||
}
|
||||
|
||||
// kubernetesSchemaPattern is every datastore schema Kamaji assigns: a
|
||||
// TenantControlPlane UID, or <namespace>_<name> on an older Kamaji. The Job
|
||||
// turns a schema into the prefix it runs `etcdctl del --prefix` on in the
|
||||
// etcd every cluster of the tenant shares, so a value that is empty, holds a
|
||||
// slash or a dot, or comes from an edited Backup must never reach it.
|
||||
var kubernetesSchemaPattern = regexp.MustCompile(`^[a-z0-9_-]+$`)
|
||||
|
||||
func validateKubernetesDatastore(ds kubernetesDatastore) error {
|
||||
if !kubernetesSchemaPattern.MatchString(ds.Prefix) {
|
||||
return fmt.Errorf("refusing datastore schema %q: want %s", ds.Prefix, kubernetesSchemaPattern)
|
||||
}
|
||||
if ds.SourcePrefix != "" && !kubernetesSchemaPattern.MatchString(ds.SourcePrefix) {
|
||||
return fmt.Errorf("refusing source datastore schema %q: want %s", ds.SourcePrefix, kubernetesSchemaPattern)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func renderKubernetesTemplate(
|
||||
tmpl corev1.PodTemplateSpec,
|
||||
appName, namespace, mode, objectKey string,
|
||||
ds kubernetesDatastore,
|
||||
parameters map[string]string,
|
||||
) (*corev1.PodTemplateSpec, error) {
|
||||
if err := validateKubernetesDatastore(ds); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return template.Template(&tmpl, map[string]any{
|
||||
"Release": map[string]string{
|
||||
"Name": appName,
|
||||
"Namespace": namespace,
|
||||
"Prefix": kubernetesReleasePrefix,
|
||||
},
|
||||
"Mode": mode,
|
||||
"ObjectKey": objectKey,
|
||||
"Datastore": ds.templateContext(),
|
||||
"Parameters": parameters,
|
||||
})
|
||||
}
|
||||
|
||||
func kubernetesStrategyParameters(b *backupsv1alpha1.Backup) map[string]string {
|
||||
out := map[string]string{}
|
||||
for k, v := range b.Spec.DriverMetadata {
|
||||
if p := strings.TrimPrefix(k, kubernetesParamPrefix); p != k && p != "" {
|
||||
out[p] = v
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func kubernetesBackupDatastore(b *backupsv1alpha1.Backup) kubernetesDatastore {
|
||||
ds := kubernetesDatastore{
|
||||
Prefix: b.Spec.DriverMetadata[kubernetesDatastorePrefixMetaKey],
|
||||
CertificateSecret: kubernetesReleaseName(b.Spec.ApplicationRef.Name) + "-datastore-certificate",
|
||||
}
|
||||
if e := b.Spec.DriverMetadata[kubernetesDatastoreEndpointsMetaKey]; e != "" {
|
||||
ds.Endpoints = strings.Split(e, ",")
|
||||
}
|
||||
return ds
|
||||
}
|
||||
|
||||
func kubernetesBackupTTL(s *strategyv1alpha1.Kubernetes) time.Duration {
|
||||
if s.Spec.Velero.Spec.TTL.Duration > 0 {
|
||||
return s.Spec.Velero.Spec.TTL.Duration
|
||||
}
|
||||
return kubernetesDefaultBackupTTL
|
||||
}
|
||||
|
||||
func kubernetesCertThreshold(s *strategyv1alpha1.Kubernetes) time.Duration {
|
||||
if s.Spec.CertificateWarningThreshold != nil && s.Spec.CertificateWarningThreshold.Duration > 0 {
|
||||
return s.Spec.CertificateWarningThreshold.Duration
|
||||
}
|
||||
return kubernetesDefaultCertThreshold
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// BackupJob path
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// reconcileKubernetes drives a BackupJob for a tenant Kubernetes application in
|
||||
// three steps, each observed on later reconciles: check the control-plane
|
||||
// certificates, dump the cluster's etcd prefix with a Job, then back up the
|
||||
// host-side Secrets and volumes with Velero. The Backup records what a restore
|
||||
// needs to put both halves back.
|
||||
func (r *BackupJobReconciler) reconcileKubernetes(ctx context.Context, j *backupsv1alpha1.BackupJob, resolved *ResolvedBackupConfig) (ctrl.Result, error) {
|
||||
logger := getLogger(ctx)
|
||||
logger.Debug("reconciling Kubernetes strategy", "backupjob", j.Name, "phase", j.Status.Phase)
|
||||
|
||||
if j.Status.Phase == backupsv1alpha1.BackupJobPhaseSucceeded ||
|
||||
j.Status.Phase == backupsv1alpha1.BackupJobPhaseFailed {
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
if err := validateKubernetesApplicationRef(j.Spec.ApplicationRef); err != nil {
|
||||
return r.markBackupJobFailed(ctx, j, err.Error())
|
||||
}
|
||||
|
||||
if j.Status.StartedAt == nil {
|
||||
fresh := &backupsv1alpha1.BackupJob{}
|
||||
if err := r.Get(ctx, types.NamespacedName{Namespace: j.Namespace, Name: j.Name}, fresh); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
if fresh.Status.StartedAt == nil {
|
||||
base := fresh.DeepCopy()
|
||||
now := metav1.Now()
|
||||
fresh.Status.StartedAt = &now
|
||||
fresh.Status.Phase = backupsv1alpha1.BackupJobPhaseRunning
|
||||
if err := r.Status().Patch(ctx, fresh, client.MergeFrom(base)); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
return ctrl.Result{RequeueAfter: kubernetesPollInterval}, nil
|
||||
}
|
||||
j.Status.StartedAt = fresh.Status.StartedAt
|
||||
j.Status.Phase = fresh.Status.Phase
|
||||
}
|
||||
|
||||
strategy := &strategyv1alpha1.Kubernetes{}
|
||||
if err := r.Get(ctx, client.ObjectKey{Name: resolved.StrategyRef.Name}, strategy); err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
return r.requeueStrategyNotReady(ctx, j, resolved.StrategyRef.Name)
|
||||
}
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
app, err := r.getApplicationUnstructured(ctx, j.Namespace, j.Spec.ApplicationRef)
|
||||
if err != nil {
|
||||
if apierrors.IsNotFound(err) || apimeta.IsNoMatchError(err) {
|
||||
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("Kubernetes application not found: %s/%s", j.Namespace, j.Spec.ApplicationRef.Name))
|
||||
}
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
appName := j.Spec.ApplicationRef.Name
|
||||
release := kubernetesReleaseName(appName)
|
||||
ds, err := resolveKubernetesDatastore(ctx, r.Interface, j.Namespace, appName)
|
||||
if err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("Kubernetes application %s/%s has no TenantControlPlane: %v", j.Namespace, appName, err))
|
||||
}
|
||||
return r.markBackupJobFailed(ctx, j, err.Error())
|
||||
}
|
||||
|
||||
// A restore that failed partway leaves the control plane stopped on a
|
||||
// possibly partial prefix, marked with the replica count it had. Backing
|
||||
// that up would turn the damage into a recovery point, so a run that has
|
||||
// not dumped anything yet refuses.
|
||||
if err := r.Get(ctx, client.ObjectKey{Namespace: j.Namespace, Name: jobNameForBackupJob(j)}, &batchv1.Job{}); apierrors.IsNotFound(err) {
|
||||
kcp, kerr := r.Interface.Resource(kamajiControlPlaneGVR).Namespace(j.Namespace).Get(ctx, release, metav1.GetOptions{})
|
||||
if kerr != nil && !apierrors.IsNotFound(kerr) && !apimeta.IsNoMatchError(kerr) {
|
||||
return ctrl.Result{}, kerr
|
||||
}
|
||||
if kerr == nil {
|
||||
if _, stopped := kcp.GetAnnotations()[kubernetesReplicasBeforeAnnotation]; stopped {
|
||||
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("Kubernetes application %s/%s is stopped by an unfinished restore; finish or repair the restore before backing it up", j.Namespace, appName))
|
||||
}
|
||||
}
|
||||
} else if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
if apimeta.FindStatusCondition(j.Status.Conditions, kubernetesCondCertificates) == nil {
|
||||
return r.checkKubernetesCertificates(ctx, j, strategy, release)
|
||||
}
|
||||
|
||||
objectKey := kubernetesObjectKey(j.Namespace, appName, j.Name)
|
||||
rendered, err := renderKubernetesTemplate(strategy.Spec.Template, appName, j.Namespace, kubernetesModeBackup, objectKey, *ds, resolved.Parameters)
|
||||
if err != nil {
|
||||
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("failed to template Kubernetes strategy: %v", err))
|
||||
}
|
||||
dump, err := ensureBackupBatchJob(ctx, r.Client, r.Scheme, j, j.Namespace, jobNameForBackupJob(j),
|
||||
map[string]string{
|
||||
kubernetesLabelMode: kubernetesModeBackup,
|
||||
backupsv1alpha1.OwningJobNameLabel: j.Name,
|
||||
backupsv1alpha1.OwningJobNamespaceLabel: j.Namespace,
|
||||
}, rendered)
|
||||
if err != nil {
|
||||
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("failed to ensure etcd dump Job: %v", err))
|
||||
}
|
||||
switch jobConditionState(dump) {
|
||||
case batchv1.JobFailed:
|
||||
msg := jobFailureMessage(dump)
|
||||
if msg == "" {
|
||||
msg = "etcd dump Job reported Failed"
|
||||
}
|
||||
return r.markBackupJobFailed(ctx, j, msg)
|
||||
case batchv1.JobComplete:
|
||||
default:
|
||||
return ctrl.Result{RequeueAfter: kubernetesPollInterval}, nil
|
||||
}
|
||||
|
||||
veleroBackups := &velerov1.BackupList{}
|
||||
if err := r.List(ctx, veleroBackups, client.InNamespace(veleroNamespace), client.MatchingLabels{
|
||||
backupsv1alpha1.OwningJobNamespaceLabel: j.Namespace,
|
||||
backupsv1alpha1.OwningJobNameLabel: j.Name,
|
||||
}); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
if len(veleroBackups.Items) == 0 {
|
||||
if err := r.labelKubernetesSecrets(ctx, j.Namespace, release); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
if err := r.createKubernetesVeleroBackup(ctx, j, strategy, app, resolved); err != nil {
|
||||
return r.failKubernetesBackupAfterDump(ctx, j, strategy, *ds, objectKey, resolved, fmt.Sprintf("failed to create Velero Backup: %v", err))
|
||||
}
|
||||
return ctrl.Result{RequeueAfter: kubernetesPollInterval}, nil
|
||||
}
|
||||
if len(veleroBackups.Items) > 1 {
|
||||
return r.markBackupJobFailed(ctx, j, "more than one Velero Backup references this BackupJob")
|
||||
}
|
||||
vb := &veleroBackups.Items[0]
|
||||
switch vb.Status.Phase {
|
||||
case velerov1.BackupPhaseCompleted:
|
||||
case velerov1.BackupPhaseFailed, velerov1.BackupPhasePartiallyFailed, velerov1.BackupPhaseFailedValidation:
|
||||
return r.failKubernetesBackupAfterDump(ctx, j, strategy, *ds, objectKey, resolved, formatVeleroBackupFailureMessageForBackupJob(ctx, r.Client, vb))
|
||||
default:
|
||||
return ctrl.Result{RequeueAfter: kubernetesPollInterval}, nil
|
||||
}
|
||||
|
||||
if j.Status.BackupRef != nil {
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
// A failed create is retried rather than failing the run: the dump and
|
||||
// the Velero backup already exist, and no Backup would own them.
|
||||
artifact, err := r.createKubernetesBackupArtifact(ctx, j, resolved, vb, *ds, objectKey)
|
||||
if err != nil {
|
||||
return ctrl.Result{}, fmt.Errorf("create Backup artifact: %w", err)
|
||||
}
|
||||
now := metav1.Now()
|
||||
j.Status.BackupRef = &corev1.LocalObjectReference{Name: artifact.Name}
|
||||
j.Status.CompletedAt = &now
|
||||
j.Status.Phase = backupsv1alpha1.BackupJobPhaseSucceeded
|
||||
apimeta.SetStatusCondition(&j.Status.Conditions, metav1.Condition{
|
||||
Type: "Ready",
|
||||
Status: metav1.ConditionTrue,
|
||||
Reason: "BackupCompleted",
|
||||
Message: "etcd prefix and Velero backup completed",
|
||||
})
|
||||
return ctrl.Result{}, r.Status().Update(ctx, j)
|
||||
}
|
||||
|
||||
// failKubernetesBackupAfterDump fails a run whose etcd dump is already in the
|
||||
// bucket. No Backup will own the dump, so a cleanup-mode Job owned by the
|
||||
// BackupJob removes it; the dump holds every Secret of the cluster.
|
||||
func (r *BackupJobReconciler) failKubernetesBackupAfterDump(ctx context.Context, j *backupsv1alpha1.BackupJob, strategy *strategyv1alpha1.Kubernetes, ds kubernetesDatastore, objectKey string, resolved *ResolvedBackupConfig, msg string) (ctrl.Result, error) {
|
||||
rendered, err := renderKubernetesTemplate(strategy.Spec.Template, j.Spec.ApplicationRef.Name, j.Namespace, kubernetesModeCleanup, objectKey, ds, resolved.Parameters)
|
||||
if err == nil {
|
||||
_, err = ensureBackupBatchJob(ctx, r.Client, r.Scheme, j, j.Namespace, j.Name+"-discard",
|
||||
map[string]string{
|
||||
kubernetesLabelMode: kubernetesModeCleanup,
|
||||
backupsv1alpha1.OwningJobNameLabel: j.Name,
|
||||
backupsv1alpha1.OwningJobNamespaceLabel: j.Namespace,
|
||||
}, rendered)
|
||||
}
|
||||
if err != nil {
|
||||
msg = fmt.Sprintf("%s; the etcd dump %s could not be scheduled for deletion: %v", msg, objectKey, err)
|
||||
}
|
||||
return r.markBackupJobFailed(ctx, j, msg)
|
||||
}
|
||||
|
||||
// checkKubernetesCertificates records on the BackupJob whether any
|
||||
// control-plane certificate expires inside its window. It never fails the
|
||||
// run: an expiring certificate is a reason to act, not to skip the backup.
|
||||
func (r *BackupJobReconciler) checkKubernetesCertificates(ctx context.Context, j *backupsv1alpha1.BackupJob, strategy *strategyv1alpha1.Kubernetes, release string) (ctrl.Result, error) {
|
||||
var secrets []corev1.Secret
|
||||
for _, suffix := range kubernetesCertificateSecrets {
|
||||
s := corev1.Secret{}
|
||||
err := r.Get(ctx, types.NamespacedName{Namespace: j.Namespace, Name: release + "-" + suffix}, &s)
|
||||
if apierrors.IsNotFound(err) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
secrets = append(secrets, s)
|
||||
}
|
||||
warnings := kubernetesCertificateWarnings(secrets, time.Now(), kubernetesBackupTTL(strategy), kubernetesCertThreshold(strategy))
|
||||
cond := metav1.Condition{
|
||||
Type: kubernetesCondCertificates,
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: "CertificatesValid",
|
||||
Message: "no control-plane certificate expires within the warning window",
|
||||
}
|
||||
if len(warnings) > 0 {
|
||||
cond.Status = metav1.ConditionTrue
|
||||
cond.Reason = "CertificatesExpiring"
|
||||
cond.Message = strings.Join(warnings, "; ")
|
||||
if r.Recorder != nil {
|
||||
r.Recorder.Event(j, corev1.EventTypeWarning, "CertificatesExpiring", cond.Message)
|
||||
}
|
||||
}
|
||||
apimeta.SetStatusCondition(&j.Status.Conditions, cond)
|
||||
if err := r.Status().Update(ctx, j); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
return ctrl.Result{RequeueAfter: kubernetesPollInterval}, nil
|
||||
}
|
||||
|
||||
func (r *BackupJobReconciler) labelKubernetesSecrets(ctx context.Context, namespace, release string) error {
|
||||
for _, suffix := range kubernetesUnlabelledSecrets {
|
||||
s := &corev1.Secret{}
|
||||
err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: release + "-" + suffix}, s)
|
||||
if apierrors.IsNotFound(err) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if s.Labels[helmInstanceLabel] == release {
|
||||
continue
|
||||
}
|
||||
base := s.DeepCopy()
|
||||
if s.Labels == nil {
|
||||
s.Labels = map[string]string{}
|
||||
}
|
||||
s.Labels[helmInstanceLabel] = release
|
||||
if err := r.Patch(ctx, s, client.MergeFrom(base)); err != nil {
|
||||
return fmt.Errorf("label Secret %s/%s: %w", namespace, s.Name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *BackupJobReconciler) createKubernetesVeleroBackup(ctx context.Context, j *backupsv1alpha1.BackupJob, strategy *strategyv1alpha1.Kubernetes, app map[string]any, resolved *ResolvedBackupConfig) error {
|
||||
spec, err := template.Template(&strategy.Spec.Velero.Spec, map[string]any{
|
||||
"Application": app,
|
||||
"Parameters": resolved.Parameters,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// The volumes an in-place restore deletes and replaces are listed before
|
||||
// Velero starts: each of them is then in the Velero backup unless it was
|
||||
// deleted first, and a volume created while the backup runs, which Velero
|
||||
// may have missed, is never deleted by a restore of it.
|
||||
volumes, err := r.kubernetesDataVolumeNames(ctx, j.Namespace, kubernetesReleaseName(j.Spec.ApplicationRef.Name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
vb := &velerov1.Backup{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
GenerateName: fmt.Sprintf("%s.%s-", j.Namespace, j.Name),
|
||||
Namespace: veleroNamespace,
|
||||
Labels: map[string]string{
|
||||
backupsv1alpha1.OwningJobNameLabel: j.Name,
|
||||
backupsv1alpha1.OwningJobNamespaceLabel: j.Namespace,
|
||||
},
|
||||
Annotations: map[string]string{kubernetesDataVolumesMetaKey: strings.Join(volumes, ",")},
|
||||
},
|
||||
Spec: *spec,
|
||||
}
|
||||
if err := r.Create(ctx, vb); err != nil {
|
||||
return err
|
||||
}
|
||||
if r.Recorder != nil {
|
||||
r.Recorder.Event(j, corev1.EventTypeNormal, "VeleroBackupCreated", fmt.Sprintf("Created Velero Backup %s/%s", veleroNamespace, vb.Name))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// kubernetesDataVolumeNames lists the claims backing the cluster's
|
||||
// PersistentVolumes: the DataVolumes and PVCs the CSI driver labels with the
|
||||
// cluster name. A PVC is listed even without a DataVolume, so an in-place
|
||||
// restore clears it too rather than leaving Velero to skip it.
|
||||
func (r *BackupJobReconciler) kubernetesDataVolumeNames(ctx context.Context, ns, release string) ([]string, error) {
|
||||
selector := clusterNameLabel + "=" + release
|
||||
seen := map[string]bool{}
|
||||
dvs, err := r.Interface.Resource(dataVolumeGVR).Namespace(ns).List(ctx, metav1.ListOptions{LabelSelector: selector})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, dv := range dvs.Items {
|
||||
seen[dv.GetName()] = true
|
||||
}
|
||||
pvcs := &corev1.PersistentVolumeClaimList{}
|
||||
if err := r.List(ctx, pvcs, client.InNamespace(ns), client.MatchingLabels{clusterNameLabel: release}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, pvc := range pvcs.Items {
|
||||
seen[pvc.Name] = true
|
||||
}
|
||||
names := make([]string, 0, len(seen))
|
||||
for name := range seen {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// createKubernetesBackupArtifact records, besides the Velero backup and the
|
||||
// etcd object, the cluster's HelmReleases and data DataVolumes. The
|
||||
// HelmReleases are kept here rather than in Velero because selecting them by
|
||||
// application label would also select the worker system disks, which the
|
||||
// lineage webhook labels after the same application.
|
||||
func (r *BackupJobReconciler) createKubernetesBackupArtifact(
|
||||
ctx context.Context,
|
||||
j *backupsv1alpha1.BackupJob,
|
||||
resolved *ResolvedBackupConfig,
|
||||
vb *velerov1.Backup,
|
||||
ds kubernetesDatastore,
|
||||
objectKey string,
|
||||
) (*backupsv1alpha1.Backup, error) {
|
||||
release := kubernetesReleaseName(j.Spec.ApplicationRef.Name)
|
||||
mds, err := r.Interface.Resource(machineDeploymentGVR).Namespace(j.Namespace).List(ctx, metav1.ListOptions{})
|
||||
if err != nil && !apierrors.IsNotFound(err) && !apimeta.IsNoMatchError(err) {
|
||||
return nil, err
|
||||
}
|
||||
var mdItems []unstructured.Unstructured
|
||||
if mds != nil {
|
||||
mdItems = mds.Items
|
||||
}
|
||||
var releases []map[string]any
|
||||
for _, name := range kubernetesHelmReleaseNames(mdItems, release) {
|
||||
hr, err := r.Interface.Resource(helmReleaseGVR).Namespace(j.Namespace).Get(ctx, name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
continue
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
releases = append(releases, sanitizeHelmRelease(hr))
|
||||
}
|
||||
releasesJSON, err := json.Marshal(releases)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
driverMD := map[string]string{
|
||||
veleroBackupNameMetadataKey: vb.Name,
|
||||
veleroBackupNamespaceMetadataKey: vb.Namespace,
|
||||
kubernetesObjectMetaKey: objectKey,
|
||||
kubernetesDatastorePrefixMetaKey: ds.Prefix,
|
||||
kubernetesDatastoreEndpointsMetaKey: strings.Join(ds.Endpoints, ","),
|
||||
kubernetesHelmReleasesMetaKey: string(releasesJSON),
|
||||
kubernetesDataVolumesMetaKey: vb.Annotations[kubernetesDataVolumesMetaKey],
|
||||
kubernetesControlPlaneUIDMetaKey: ds.ControlPlaneUID,
|
||||
}
|
||||
if c := apimeta.FindStatusCondition(j.Status.Conditions, kubernetesCondCertificates); c != nil && c.Status == metav1.ConditionTrue {
|
||||
driverMD[kubernetesCertWarningsMetaKey] = c.Message
|
||||
}
|
||||
for k, v := range resolved.Parameters {
|
||||
driverMD[kubernetesParamPrefix+k] = v
|
||||
}
|
||||
|
||||
backup := &backupsv1alpha1.Backup{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: j.Name, Namespace: j.Namespace},
|
||||
Spec: backupsv1alpha1.BackupSpec{
|
||||
ApplicationRef: j.Spec.ApplicationRef,
|
||||
StrategyRef: resolved.StrategyRef,
|
||||
TakenAt: metav1.Now(),
|
||||
DriverMetadata: driverMD,
|
||||
PlanRef: j.Spec.PlanRef,
|
||||
},
|
||||
Status: backupsv1alpha1.BackupStatus{
|
||||
Phase: backupsv1alpha1.BackupPhaseReady,
|
||||
Artifact: &backupsv1alpha1.BackupArtifact{URI: fmt.Sprintf("velero://%s/%s", vb.Namespace, vb.Name)},
|
||||
},
|
||||
}
|
||||
if err := r.Create(ctx, backup); err != nil {
|
||||
if !apierrors.IsAlreadyExists(err) {
|
||||
return nil, err
|
||||
}
|
||||
existing := &backupsv1alpha1.Backup{}
|
||||
if err := r.Get(ctx, types.NamespacedName{Namespace: backup.Namespace, Name: backup.Name}, existing); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return existing, nil
|
||||
}
|
||||
return backup, nil
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Backup deletion cleanup
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// cleanupKubernetesBackup removes both halves of a deleted Backup: the Velero
|
||||
// backup through a DeleteBackupRequest, and the etcd dump object through a
|
||||
// cleanup-mode Job, waiting for the Job before the Backup is released. The
|
||||
// escape hatches match the Redis driver's.
|
||||
func (r *BackupReconciler) cleanupKubernetesBackup(ctx context.Context, backup *backupsv1alpha1.Backup) (ctrl.Result, error) {
|
||||
if err := r.cleanupVeleroBackup(ctx, backup); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
jobName := backup.Name + "-cleanup"
|
||||
if backup.Annotations[redisSkipArtifactCleanupAnnotation] == "true" {
|
||||
existing := &batchv1.Job{}
|
||||
if err := r.Get(ctx, types.NamespacedName{Namespace: backup.Namespace, Name: jobName}, existing); err == nil {
|
||||
_ = r.deleteRedisCleanupJob(ctx, existing)
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
objectKey := backup.Spec.DriverMetadata[kubernetesObjectMetaKey]
|
||||
if objectKey == "" {
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
strategy := &strategyv1alpha1.Kubernetes{}
|
||||
if err := r.Get(ctx, client.ObjectKey{Name: backup.Spec.StrategyRef.Name}, strategy); err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
return r.releaseRedisCleanup(ctx, backup, objectKey, "strategy CR is gone"), nil
|
||||
}
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
job := &batchv1.Job{}
|
||||
err := r.Get(ctx, types.NamespacedName{Namespace: backup.Namespace, Name: jobName}, job)
|
||||
switch {
|
||||
case apierrors.IsNotFound(err):
|
||||
terminating, nsErr := r.namespaceTerminating(ctx, backup.Namespace)
|
||||
if nsErr != nil {
|
||||
return ctrl.Result{}, nsErr
|
||||
}
|
||||
if terminating {
|
||||
return r.releaseRedisCleanup(ctx, backup, objectKey, "namespace is terminating"), nil
|
||||
}
|
||||
if perr := ProjectBackupCredentials(ctx, r.Client, r.CredentialsConfig, backup.Namespace); perr != nil {
|
||||
return r.releaseRedisCleanup(ctx, backup, objectKey, fmt.Sprintf("cannot project credentials: %v", perr)), nil
|
||||
}
|
||||
rendered, rerr := renderKubernetesTemplate(strategy.Spec.Template, backup.Spec.ApplicationRef.Name, backup.Namespace,
|
||||
kubernetesModeCleanup, objectKey, kubernetesBackupDatastore(backup), kubernetesStrategyParameters(backup))
|
||||
if rerr != nil {
|
||||
return r.releaseRedisCleanup(ctx, backup, objectKey, fmt.Sprintf("cleanup template render failed: %v", rerr)), nil
|
||||
}
|
||||
cleanup := buildRedisCleanupJob(backup.Namespace, jobName, rendered)
|
||||
cleanup.Labels = map[string]string{kubernetesLabelMode: kubernetesModeCleanup}
|
||||
delete(cleanup.Spec.Template.Labels, redisLabelMode)
|
||||
maps.Copy(cleanup.Spec.Template.Labels, cleanup.Labels)
|
||||
if cerr := r.Create(ctx, cleanup); cerr != nil && !apierrors.IsAlreadyExists(cerr) {
|
||||
if apierrors.IsForbidden(cerr) || apierrors.IsInvalid(cerr) {
|
||||
return r.releaseRedisCleanup(ctx, backup, objectKey, fmt.Sprintf("cannot create cleanup Job: %v", cerr)), nil
|
||||
}
|
||||
return ctrl.Result{}, cerr
|
||||
}
|
||||
return ctrl.Result{RequeueAfter: kubernetesPollInterval}, nil
|
||||
case err != nil:
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
if !job.DeletionTimestamp.IsZero() {
|
||||
return ctrl.Result{RequeueAfter: kubernetesPollInterval}, nil
|
||||
}
|
||||
switch jobConditionState(job) {
|
||||
case batchv1.JobComplete:
|
||||
_ = r.deleteRedisCleanupJob(ctx, job)
|
||||
return ctrl.Result{}, nil
|
||||
case batchv1.JobFailed:
|
||||
attempts, _ := strconv.Atoi(backup.Annotations[kubernetesCleanupAttemptsAnnotation])
|
||||
attempts++
|
||||
if attempts >= kubernetesCleanupMaxAttempts {
|
||||
_ = r.deleteRedisCleanupJob(ctx, job)
|
||||
return r.releaseRedisCleanup(ctx, backup, objectKey, fmt.Sprintf("the cleanup Job failed %d times: %s", attempts, jobFailureMessage(job))), nil
|
||||
}
|
||||
base := backup.DeepCopy()
|
||||
if backup.Annotations == nil {
|
||||
backup.Annotations = map[string]string{}
|
||||
}
|
||||
backup.Annotations[kubernetesCleanupAttemptsAnnotation] = strconv.Itoa(attempts)
|
||||
if err := r.Patch(ctx, backup, client.MergeFrom(base)); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
_ = r.deleteRedisCleanupJob(ctx, job)
|
||||
return ctrl.Result{RequeueAfter: time.Duration(attempts) * 30 * time.Second}, nil
|
||||
default:
|
||||
return ctrl.Result{RequeueAfter: kubernetesPollInterval}, nil
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -464,7 +464,7 @@ func TestMariaDBArtifactSizeBytes_MinioClient(t *testing.T) {
|
||||
gotPath, gotPrefix, gotAuth = req.URL.Path, req.URL.Query().Get("prefix"), req.Header.Get("Authorization")
|
||||
gotDelimiter = req.URL.Query().Get("delimiter")
|
||||
w.Header().Set("Content-Type", "application/xml")
|
||||
fmt.Fprintf(w, `<?xml version="1.0" encoding="UTF-8"?>
|
||||
_, _ = fmt.Fprintf(w, `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
|
||||
<Name>bkt</Name><Prefix>tenant/db/</Prefix><KeyCount>1</KeyCount><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated>
|
||||
<Contents><Key>%s</Key><Size>31337</Size><ETag>"x"</ETag><LastModified>2026-10-21T10:01:00.000Z</LastModified></Contents>
|
||||
|
||||
@@ -30,8 +30,6 @@ import (
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
const (
|
||||
mariadbFieldManager = "cozystack-mariadb-backup-driver"
|
||||
|
||||
// mariadb-operator's MariaDB ApplicationDefinition (release.prefix). The
|
||||
// cozystack mariadb-rd renders the HelmRelease with releaseName =
|
||||
// "mariadb-" + appName (see packages/system/mariadb-rd/cozyrds/mariadb.yaml,
|
||||
|
||||
@@ -528,8 +528,8 @@ func (r *BackupJobReconciler) reconcileMongoDB(ctx context.Context, j *backupsv1
|
||||
}
|
||||
}
|
||||
|
||||
switch state := mdbBackup.Status.State; {
|
||||
case state == psmdbtypes.StateReady:
|
||||
switch state := mdbBackup.Status.State; state {
|
||||
case psmdbtypes.StateReady:
|
||||
// Backup completed successfully. Materialise the Cozystack artifact and
|
||||
// finalise the BackupJob. Idempotent: reuse the existing artifact if a
|
||||
// previous reconcile created it and then raced on the status update.
|
||||
@@ -570,7 +570,7 @@ func (r *BackupJobReconciler) reconcileMongoDB(ctx context.Context, j *backupsv1
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
|
||||
case state == psmdbtypes.StateError || state == psmdbtypes.StateRejected:
|
||||
case psmdbtypes.StateError, psmdbtypes.StateRejected:
|
||||
message := mdbBackup.Status.Error
|
||||
// The residual half of the injection race: on the useSystemBucket flow the
|
||||
// operator can latch state=error because it resolved the injected storage
|
||||
@@ -1740,8 +1740,8 @@ func (r *RestoreJobReconciler) reconcileMongoDBRestore(ctx context.Context, rest
|
||||
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf("failed to ensure psmdb.percona.com/PerconaServerMongoDBRestore: %v", err))
|
||||
}
|
||||
|
||||
switch state := mdbRestore.Status.State; {
|
||||
case state == psmdbtypes.StateReady:
|
||||
switch state := mdbRestore.Status.State; state {
|
||||
case psmdbtypes.StateReady:
|
||||
now := metav1.Now()
|
||||
restoreJob.Status.CompletedAt = &now
|
||||
restoreJob.Status.Phase = backupsv1alpha1.RestoreJobPhaseSucceeded
|
||||
@@ -1756,7 +1756,7 @@ func (r *RestoreJobReconciler) reconcileMongoDBRestore(ctx context.Context, rest
|
||||
}
|
||||
return ctrl.Result{}, nil
|
||||
|
||||
case state == psmdbtypes.StateError || state == psmdbtypes.StateRejected:
|
||||
case psmdbtypes.StateError, psmdbtypes.StateRejected:
|
||||
message := mdbRestore.Status.Error
|
||||
if message == "" {
|
||||
message = fmt.Sprintf("psmdb.percona.com/PerconaServerMongoDBRestore %s/%s reported state=%s", mdbRestore.Namespace, mdbRestore.Name, state)
|
||||
@@ -1855,7 +1855,7 @@ func (r *RestoreJobReconciler) resolveMongoDBBackupSource(ctx context.Context, b
|
||||
}
|
||||
if destination == "" {
|
||||
return nil, fmt.Errorf(
|
||||
"Backup has no restorable destination (the operator PerconaServerMongoDBBackup was reaped and no snapshot destination is persisted); re-run the BackupJob")
|
||||
"no restorable destination for the Backup (the operator PerconaServerMongoDBBackup was reaped and no snapshot destination is persisted); re-run the BackupJob")
|
||||
}
|
||||
src := &psmdbtypes.BackupSource{
|
||||
Type: psmdbtypes.BackupTypeLogical,
|
||||
|
||||
@@ -513,7 +513,7 @@ func (r *BackupJobReconciler) createRabbitmqBackupArtifact(
|
||||
// this run's success while describing something else.
|
||||
if existing.Spec.ApplicationRef.Kind != backup.Spec.ApplicationRef.Kind ||
|
||||
existing.Spec.ApplicationRef.Name != backup.Spec.ApplicationRef.Name {
|
||||
return nil, fmt.Errorf("Backup %s/%s already exists for a different application (%s/%s, not %s/%s)",
|
||||
return nil, fmt.Errorf("existing Backup %s/%s belongs to a different application (%s/%s, not %s/%s)",
|
||||
backup.Namespace, backup.Name,
|
||||
existing.Spec.ApplicationRef.Kind, existing.Spec.ApplicationRef.Name,
|
||||
backup.Spec.ApplicationRef.Kind, backup.Spec.ApplicationRef.Name)
|
||||
|
||||
@@ -204,6 +204,8 @@ func (r *RestoreJobReconciler) Reconcile(ctx context.Context, req ctrl.Request)
|
||||
return r.reconcileRedisRestore(ctx, restoreJob, backup)
|
||||
case strategyv1alpha1.KafkaStrategyKind:
|
||||
return r.reconcileKafkaRestore(ctx, restoreJob, backup)
|
||||
case strategyv1alpha1.KubernetesStrategyKind:
|
||||
return r.reconcileKubernetesRestore(ctx, restoreJob, backup)
|
||||
default:
|
||||
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf("StrategyRef.Kind not supported: %s", backup.Spec.StrategyRef.Kind))
|
||||
}
|
||||
@@ -382,6 +384,12 @@ func (r *RestoreJobReconciler) cleanupOnDelete(ctx context.Context, restoreJob *
|
||||
case strategyv1alpha1.VeleroStrategyKind:
|
||||
r.cleanupVeleroRestore(ctx, restoreJob)
|
||||
|
||||
case strategyv1alpha1.KubernetesStrategyKind:
|
||||
// Its Velero Restores are labelled like the Velero driver's, so the
|
||||
// same cleanup reaps them; a live cluster it stopped is resumed.
|
||||
r.cleanupVeleroRestore(ctx, restoreJob)
|
||||
r.cleanupKubernetesRestore(ctx, restoreJob)
|
||||
|
||||
case strategyv1alpha1.CNPGStrategyKind, strategyv1alpha1.JobStrategyKind, strategyv1alpha1.AltinityStrategyKind, strategyv1alpha1.MariaDBStrategyKind, strategyv1alpha1.MongoDBStrategyKind, strategyv1alpha1.FoundationDBStrategyKind, strategyv1alpha1.EtcdStrategyKind, strategyv1alpha1.RabbitmqStrategyKind, strategyv1alpha1.RedisStrategyKind, strategyv1alpha1.KafkaStrategyKind:
|
||||
// Nothing to clean up: these drivers don't materialise namespaced
|
||||
// artifacts that outlive the RestoreJob. (Etcd: the operator-side
|
||||
|
||||
@@ -65,12 +65,14 @@ func (s captureSink) WithName(string) logr.LogSink { return s }
|
||||
func TestRunThenDrain_FencesLeftBehindAreAnErrorNamingThem(t *testing.T) {
|
||||
logger, entries := newCaptureLogger()
|
||||
ctx := log.IntoContext(context.Background(), logger)
|
||||
RunThenDrain(ctx,
|
||||
if err := RunThenDrain(ctx,
|
||||
func(context.Context) error { return nil },
|
||||
func(context.Context) (int, error) {
|
||||
return 1, errors.Join(context.DeadlineExceeded, errors.New("still fenced: tenant-root/vm-disk-0"))
|
||||
},
|
||||
time.Minute)
|
||||
time.Minute); err != nil {
|
||||
t.Fatalf("RunThenDrain = %v, want the run's nil error", err)
|
||||
}
|
||||
for _, e := range *entries {
|
||||
if e.err != nil && strings.Contains(e.err.Error(), "tenant-root/vm-disk-0") {
|
||||
return
|
||||
@@ -84,10 +86,12 @@ func TestRunThenDrain_FencesLeftBehindAreAnErrorNamingThem(t *testing.T) {
|
||||
func TestRunThenDrain_AnUnansweredListingIsNotACountOfMinusOne(t *testing.T) {
|
||||
logger, entries := newCaptureLogger()
|
||||
ctx := log.IntoContext(context.Background(), logger)
|
||||
RunThenDrain(ctx,
|
||||
if err := RunThenDrain(ctx,
|
||||
func(context.Context) error { return nil },
|
||||
func(context.Context) (int, error) { return -1, context.DeadlineExceeded },
|
||||
time.Minute)
|
||||
time.Minute); err != nil {
|
||||
t.Fatalf("RunThenDrain = %v, want the run's nil error", err)
|
||||
}
|
||||
found := false
|
||||
for _, e := range *entries {
|
||||
for i := 0; i+1 < len(e.kv); i += 2 {
|
||||
@@ -108,10 +112,12 @@ func TestRunThenDrain_AnUnansweredListingIsNotACountOfMinusOne(t *testing.T) {
|
||||
func TestRunThenDrain_ACompleteDrainSaysSo(t *testing.T) {
|
||||
logger, entries := newCaptureLogger()
|
||||
ctx := log.IntoContext(context.Background(), logger)
|
||||
RunThenDrain(ctx,
|
||||
if err := RunThenDrain(ctx,
|
||||
func(context.Context) error { return nil },
|
||||
func(context.Context) (int, error) { return 0, nil },
|
||||
time.Minute)
|
||||
time.Minute); err != nil {
|
||||
t.Fatalf("RunThenDrain = %v, want the run's nil error", err)
|
||||
}
|
||||
for _, e := range *entries {
|
||||
if e.err == nil && strings.Contains(e.msg, "Released every uninstall fence") {
|
||||
return
|
||||
|
||||
@@ -486,11 +486,12 @@ func ownsSuspensionManagedFields(hr *helmv2.HelmRelease) bool {
|
||||
// fieldsManageSuspend reports whether a managed-fields set includes spec.suspend,
|
||||
// encoded by the apiserver as {"f:spec":{"f:suspend":{}}}.
|
||||
func fieldsManageSuspend(fs *metav1.FieldsV1) bool {
|
||||
if fs == nil || len(fs.Raw) == 0 {
|
||||
raw := fs.GetRawBytes()
|
||||
if len(raw) == 0 {
|
||||
return false
|
||||
}
|
||||
var top map[string]json.RawMessage
|
||||
if err := json.Unmarshal(fs.Raw, &top); err != nil {
|
||||
if err := json.Unmarshal(raw, &top); err != nil {
|
||||
return false
|
||||
}
|
||||
specRaw, ok := top["f:spec"]
|
||||
|
||||
@@ -83,7 +83,7 @@ func suspendManagedFields(manager string, op metav1.ManagedFieldsOperationType)
|
||||
Operation: op,
|
||||
APIVersion: helmv2.GroupVersion.String(),
|
||||
FieldsType: "FieldsV1",
|
||||
FieldsV1: &metav1.FieldsV1{Raw: []byte(`{"f:spec":{"f:suspend":{}}}`)},
|
||||
FieldsV1: metav1.NewFieldsV1(`{"f:spec":{"f:suspend":{}}}`),
|
||||
}}
|
||||
}
|
||||
|
||||
@@ -286,7 +286,7 @@ func TestOwnsSuspensionManagedFields(t *testing.T) {
|
||||
{"no managed fields", nil, false},
|
||||
{"plunger apply of other fields", []metav1.ManagedFieldsEntry{{
|
||||
Manager: suspendFieldManager, Operation: metav1.ManagedFieldsOperationApply,
|
||||
FieldsType: "FieldsV1", FieldsV1: &metav1.FieldsV1{Raw: []byte(`{"f:spec":{"f:chart":{}}}`)},
|
||||
FieldsType: "FieldsV1", FieldsV1: metav1.NewFieldsV1(`{"f:spec":{"f:chart":{}}}`),
|
||||
}}, false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
|
||||
@@ -3,8 +3,10 @@ package kubeovnplunger
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -153,12 +155,9 @@ func (r *KubeOVNPlunger) SetupWithManager(mgr ctrl.Manager, kubeOVNNamespace, ap
|
||||
Complete(r); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = mgr.Add(manager.RunnableFunc(func(ctx context.Context) error {
|
||||
go srv.ListenAndServe()
|
||||
<-ctx.Done()
|
||||
_ = srv.Shutdown(context.Background())
|
||||
return nil
|
||||
}))
|
||||
if err := mgr.Add(serveStatus(srv)); err != nil {
|
||||
return err
|
||||
}
|
||||
return mgr.Add(manager.RunnableFunc(func(ctx context.Context) error {
|
||||
ticker := time.NewTicker(rescanInterval)
|
||||
defer ticker.Stop()
|
||||
@@ -180,6 +179,25 @@ func (r *KubeOVNPlunger) SetupWithManager(mgr ctrl.Manager, kubeOVNNamespace, ap
|
||||
}))
|
||||
}
|
||||
|
||||
// serveStatus runs the status stream for as long as the manager does. A server
|
||||
// that cannot start fails the manager, as the metrics server does.
|
||||
func serveStatus(s *sse.Server) manager.RunnableFunc {
|
||||
return func(ctx context.Context) error {
|
||||
served := make(chan error, 1)
|
||||
go func() { served <- s.ListenAndServe() }()
|
||||
select {
|
||||
case err := <-served:
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
}
|
||||
_ = s.Shutdown(context.Background())
|
||||
if err := <-served; !errors.Is(err, http.ErrServerClosed) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type ExecOptions struct {
|
||||
Namespace string
|
||||
Pod string
|
||||
|
||||
@@ -219,7 +219,7 @@ func (r *KubeOVNPlunger) WriteClusterMetrics(db string, snaps []ovnstatus.Health
|
||||
r.metrics.allAgree.WithLabelValues(db, cid).Set(b2f(ecv.AllAgree))
|
||||
r.metrics.membersExpected.WithLabelValues(db, cid).Set(float64(expectedReplicas))
|
||||
r.metrics.membersObserved.WithLabelValues(db, cid).Set(float64(ecv.MembersCount))
|
||||
r.metrics.ipsExpected.WithLabelValues(db, cid).Set(float64(len(ecv.ConsensusResult.TruthView.Members))) // optional; or len(hints.ExpectedIPs)
|
||||
r.metrics.ipsExpected.WithLabelValues(db, cid).Set(float64(len(ecv.TruthView.Members))) // optional; or len(hints.ExpectedIPs)
|
||||
r.metrics.ipsObserved.WithLabelValues(db, cid).Set(float64(ecv.DistinctIPCount))
|
||||
r.metrics.excessMembers.WithLabelValues(db, cid).Set(float64(ecv.ExpectedExcess))
|
||||
r.metrics.missingMembers.WithLabelValues(db, cid).Set(float64(ecv.ExpectedShortfall))
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package kubeovnplunger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cozystack/cozystack/internal/sse"
|
||||
)
|
||||
|
||||
func runStatusServer(ctx context.Context, t *testing.T, addr string) (bool, error) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
t.Cleanup(cancel)
|
||||
s := sse.New(sse.Options{Addr: addr, Logger: log.New(io.Discard, "", 0)})
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- serveStatus(s)(ctx) }()
|
||||
select {
|
||||
case err := <-done:
|
||||
return true, err
|
||||
case <-time.After(2 * time.Second):
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
// A status server that cannot listen fails its runnable, and with it the
|
||||
// manager, rather than leaving the plunger running without it.
|
||||
func TestStatusServerReportsListenFailure(t *testing.T) {
|
||||
taken, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = taken.Close() })
|
||||
returned, err := runStatusServer(context.Background(), t, taken.Addr().String())
|
||||
if !returned || err == nil {
|
||||
t.Fatalf("serveStatus on an occupied address = %v (returned %v), want an error", err, returned)
|
||||
}
|
||||
}
|
||||
|
||||
// Stopping the manager stops the server, and that is not an error.
|
||||
func TestStatusServerStopsWithoutError(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
time.AfterFunc(100*time.Millisecond, cancel)
|
||||
returned, err := runStatusServer(ctx, t, "127.0.0.1:0")
|
||||
if !returned || err != nil {
|
||||
t.Fatalf("serveStatus after cancel = %v (returned %v), want nil", err, returned)
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package controller
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -15,10 +14,6 @@ import (
|
||||
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
||||
)
|
||||
|
||||
const (
|
||||
deletionRequeueDelay = 30 * time.Second
|
||||
)
|
||||
|
||||
// WorkloadMonitorReconciler reconciles a WorkloadMonitor object
|
||||
type WorkloadReconciler struct {
|
||||
client.Client
|
||||
|
||||
Loaded 100 of 337 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user