mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 12:13:55 +09:00
Merge branch 'main' into feat/kubernetes-nodes-talos-sysctls
Generated files (types, README, schema, cozyrds) are regenerated from the merged values.yaml. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <f@lex.la>
This commit is contained in:
448 files changed
+32546
-6025
No files matched your search
@@ -61,7 +61,8 @@ jobs:
|
||||
# base-repo PR file list — NOT from the presence/absence of an artifact the
|
||||
# fork's own build controls. Fail-closed if the fork build failed or the
|
||||
# pr-patch artifact is missing; green-and-stop only for a PR whose file list is
|
||||
# entirely under docs/ (verified against the base-repo API).
|
||||
# entirely under docs/ or the CI-only tools cmd/backport-batch/ and
|
||||
# cmd/api-gate/ (verified against the base-repo API).
|
||||
resolve:
|
||||
name: Resolve fork PR
|
||||
# Only fork PRs: head_repository differs from the base repository. Same-repo
|
||||
@@ -266,15 +267,21 @@ jobs:
|
||||
await decide('failure', 'PR file list hit the 3000-file API cap — cannot verify docs-only');
|
||||
return;
|
||||
}
|
||||
// Paths with nothing to build and nothing for e2e to install: docs,
|
||||
// and the CI-only Go tools no image builds. pull-requests.yaml's
|
||||
// plan job answers the same question with the same prefixes, and
|
||||
// hack/gate-concurrency-contract.bats holds the two lists equal.
|
||||
//
|
||||
// A rename reports the NEW path in `filename` and the old one in
|
||||
// `previous_filename`. Checking only `filename` would read a
|
||||
// packages/… -> docs/… rename as docs-only, when it in fact removes
|
||||
// a code path. BOTH sides must be under docs/ to count as docs-only.
|
||||
const isDocs = p => typeof p === 'string' && p.startsWith('docs/');
|
||||
const hasNonDocs = files.some(f =>
|
||||
!isDocs(f.filename) || (f.previous_filename && !isDocs(f.previous_filename)));
|
||||
if (!hasNonDocs) {
|
||||
await decide('success', 'docs-only PR (verified via PR file list) — e2e not required');
|
||||
// a code path. BOTH sides must be under these prefixes to count.
|
||||
const noRuntimePrefixes = ['docs/', 'cmd/backport-batch/', 'cmd/api-gate/'];
|
||||
const isNoRuntime = p => typeof p === 'string' && noRuntimePrefixes.some(x => p.startsWith(x));
|
||||
const hasRuntime = files.some(f =>
|
||||
!isNoRuntime(f.filename) || (f.previous_filename && !isNoRuntime(f.previous_filename)));
|
||||
if (!hasRuntime) {
|
||||
await decide('success', 'docs or CI-only tools (verified via PR file list) — e2e not required');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -543,7 +550,7 @@ jobs:
|
||||
# Covers a green full suite. It does not cover a tenant-Kubernetes suite
|
||||
# running into its node-join deadline, so a red there can end as a
|
||||
# cancelled job. Held to one figure across every lane that runs the suite.
|
||||
timeout-minutes: 215
|
||||
timeout-minutes: 265
|
||||
permissions:
|
||||
contents: read
|
||||
packages: read
|
||||
|
||||
@@ -137,7 +137,7 @@ jobs:
|
||||
# Covers a green full suite. It does not cover a tenant-Kubernetes suite
|
||||
# running into its node-join deadline, so a red there can end as a
|
||||
# cancelled job. Held to one figure across every lane that runs the suite.
|
||||
timeout-minutes: 215
|
||||
timeout-minutes: 265
|
||||
permissions:
|
||||
contents: read
|
||||
checks: write # Breakpoint action updates its dedicated "Breakpoint Open" Check Run.
|
||||
@@ -292,7 +292,7 @@ jobs:
|
||||
# collect-report packages cozyreport plus per-failed-test crust-gather
|
||||
# snapshots captured before Chainsaw cleanup.
|
||||
#
|
||||
# A ceiling of its own, not the job's 215 minutes. `Upload cozyreport.tgz`
|
||||
# A ceiling of its own, not the job's 265 minutes. `Upload cozyreport.tgz`
|
||||
# is the next step and the collector writes its tarball at the very end of
|
||||
# its run, so a read that never returns spends what is left of the job
|
||||
# budget, the job is cancelled, and the upload never runs -- the artifact
|
||||
|
||||
@@ -250,7 +250,7 @@ jobs:
|
||||
# Covers a green full suite. It does not cover a tenant-Kubernetes suite
|
||||
# running into its node-join deadline, so a red there can end as a
|
||||
# cancelled job. Held to one figure across every lane that runs the suite.
|
||||
timeout-minutes: 215
|
||||
timeout-minutes: 265
|
||||
permissions:
|
||||
contents: read
|
||||
packages: read
|
||||
@@ -385,7 +385,7 @@ jobs:
|
||||
path: _out/chainsaw-report.xml
|
||||
if-no-files-found: ignore
|
||||
|
||||
# A ceiling of its own, not the job's 215 minutes. `Upload cozyreport.tgz`
|
||||
# A ceiling of its own, not the job's 265 minutes. `Upload cozyreport.tgz`
|
||||
# is the next step and the collector writes its tarball at the very end of
|
||||
# its run, so a read that never returns spends what is left of the job
|
||||
# budget, the job is cancelled, and the upload never runs -- the artifact
|
||||
|
||||
@@ -75,6 +75,8 @@ jobs:
|
||||
statuses: write
|
||||
outputs:
|
||||
code: ${{ steps.p.outputs.code }}
|
||||
docs: ${{ steps.p.outputs.docs }}
|
||||
backport_batch: ${{ steps.p.outputs.backport_batch }}
|
||||
matrix: ${{ steps.p.outputs.matrix }}
|
||||
any: ${{ steps.p.outputs.any }}
|
||||
touched: ${{ steps.p.outputs.touched }}
|
||||
@@ -167,11 +169,27 @@ jobs:
|
||||
# alone here rather than widened by a side effect.
|
||||
git diff --name-only --no-renames "origin/${BASE_REF}...HEAD" > /tmp/changed_norenames.txt
|
||||
echo "Changed files:"; sed 's/^/ /' /tmp/changed_norenames.txt
|
||||
if grep -qvE '^docs/' /tmp/changed_norenames.txt; then
|
||||
# `code` is false when every path is under docs/ or in a CI-only Go
|
||||
# tool that no image builds (cmd/backport-batch, cmd/api-gate): there
|
||||
# is nothing to build and nothing for e2e to install. e2e-fork.yaml
|
||||
# answers the same question from the PR file list with the same
|
||||
# prefixes, and hack/gate-concurrency-contract.bats holds the two lists
|
||||
# equal.
|
||||
if grep -qvE '^(docs/|cmd/backport-batch/|cmd/api-gate/)' /tmp/changed_norenames.txt; then
|
||||
echo "code=true" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
echo "code=false" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
# The Bats contracts read docs, so a docs change runs them even when
|
||||
# `code` is false; a change confined to the CI-only tools does not.
|
||||
if grep -qE '^docs/' /tmp/changed_norenames.txt; then
|
||||
echo "docs=true" >> "$GITHUB_OUTPUT"
|
||||
else
|
||||
echo "docs=false" >> "$GITHUB_OUTPUT"
|
||||
fi
|
||||
backport_batch="$(hack/backport-batch-tests-needed.sh /tmp/changed_norenames.txt)"
|
||||
echo "backport-batch tests needed: $backport_batch"
|
||||
echo "backport_batch=$backport_batch" >> "$GITHUB_OUTPUT"
|
||||
matrix="$(hack/build-matrix.sh /tmp/changed.txt)"
|
||||
echo "Build matrix: $matrix"
|
||||
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
|
||||
@@ -293,9 +311,11 @@ jobs:
|
||||
contents: read
|
||||
needs: ["plan"]
|
||||
# Unit contracts read both code and documentation. Run this job for every
|
||||
# non-release PR: code changes take the complete unit path, while docs-only
|
||||
# changes run the Bats lane that owns those cross-tree contracts. Builds and
|
||||
# E2E remain gated on `code` below.
|
||||
# non-release PR: code changes take the complete unit path, docs changes run
|
||||
# the Bats lane that owns those cross-tree contracts, and the backport-batch
|
||||
# tests run whenever one of their inputs changed and at no other time. A
|
||||
# change confined to the CI-only tools runs no other lane. Builds and E2E
|
||||
# remain gated on `code` below.
|
||||
if: |
|
||||
!contains(github.event.pull_request.labels.*.name, 'release')
|
||||
steps:
|
||||
@@ -306,20 +326,21 @@ jobs:
|
||||
# The module cache is restored from the key build-main.yaml writes on
|
||||
# main, which every PR run can read, so a PR's first run starts warm. The
|
||||
# download fetches whatever the cache lacks, the whole set on a miss. The
|
||||
# Go steps are for code changes only: a docs-only run is the Bats lane alone.
|
||||
# Go steps are for code changes and the backport-batch tests only: a
|
||||
# docs-only run is the Bats lane alone.
|
||||
- name: Locate Go module cache
|
||||
if: needs.plan.outputs.code == 'true'
|
||||
if: needs.plan.outputs.code == 'true' || needs.plan.outputs.backport_batch == 'true'
|
||||
id: gomodcache
|
||||
run: echo "dir=$(go env GOMODCACHE)" >> "$GITHUB_OUTPUT"
|
||||
- name: Cache Go modules
|
||||
if: needs.plan.outputs.code == 'true'
|
||||
if: needs.plan.outputs.code == 'true' || needs.plan.outputs.backport_batch == 'true'
|
||||
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
|
||||
with:
|
||||
path: ${{ steps.gomodcache.outputs.dir }}
|
||||
key: go-mod-${{ runner.os }}-${{ hashFiles('**/go.sum') }}
|
||||
restore-keys: go-mod-${{ runner.os }}-
|
||||
- name: Download Go modules
|
||||
if: needs.plan.outputs.code == 'true'
|
||||
if: needs.plan.outputs.code == 'true' || needs.plan.outputs.backport_batch == 'true'
|
||||
run: |
|
||||
for mod in $(git ls-files go.mod '*/go.mod' | xargs -n1 dirname); do
|
||||
(cd "$mod" && "$GITHUB_WORKSPACE/hack/go-mod-download.sh")
|
||||
@@ -327,8 +348,9 @@ jobs:
|
||||
# Ephemeral runners don't carry the helm-unittest plugin the persistent
|
||||
# self-hosted runner accreted. The plugin embeds its own Helm, so its
|
||||
# version is pinned: a plugin release must not change test results. Bats is
|
||||
# installed on every run, docs-only ones included.
|
||||
# installed whenever a Bats lane runs, docs-only ones included.
|
||||
- name: Set up test toolchain
|
||||
if: needs.plan.outputs.code == 'true' || needs.plan.outputs.docs == 'true'
|
||||
env:
|
||||
CODE_CHANGED: ${{ needs.plan.outputs.code }}
|
||||
HELM_UNITTEST_VERSION: "1.1.2"
|
||||
@@ -374,8 +396,15 @@ jobs:
|
||||
if: needs.plan.outputs.code == 'true'
|
||||
run: make unit-tests test-controllers test-controllers-envtest -j4 -k --output-sync=target
|
||||
- name: Run Bats unit tests for docs-only changes
|
||||
if: needs.plan.outputs.code == 'false'
|
||||
if: needs.plan.outputs.code == 'false' && needs.plan.outputs.docs == 'true'
|
||||
run: make bats-unit-tests
|
||||
# Not part of `make unit-tests`: hack/backport-batch-tests-needed.sh
|
||||
# decides from cmd/backport-batch/test-inputs.txt, whatever `code` is.
|
||||
# `!cancelled()` for the reason the unit lane runs with -k: a red step
|
||||
# above must not hide this suite's own result.
|
||||
- name: Run backport-batch tests
|
||||
if: ${{ !cancelled() && needs.plan.outputs.backport_batch == 'true' }}
|
||||
run: make test-backport-batch
|
||||
- name: Upload unit test report
|
||||
if: always()
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
@@ -953,7 +982,7 @@ jobs:
|
||||
# Covers a green full suite. It does not cover a tenant-Kubernetes suite
|
||||
# running into its node-join deadline, so a red there can end as a
|
||||
# cancelled job. Held to one figure across every lane that runs the suite.
|
||||
timeout-minutes: 215
|
||||
timeout-minutes: 265
|
||||
permissions:
|
||||
contents: read
|
||||
packages: read
|
||||
@@ -1224,7 +1253,7 @@ jobs:
|
||||
|
||||
# ▸ Collect debug information (always runs)
|
||||
#
|
||||
# A ceiling of its own, not the job's 215 minutes. `Upload
|
||||
# A ceiling of its own, not the job's 265 minutes. `Upload
|
||||
# cozyreport.tgz` is the next step and the collector writes its tarball
|
||||
# at the very end of its run, so a read that never returns spends what
|
||||
# is left of the job budget, the job is cancelled, and the upload never
|
||||
@@ -1235,7 +1264,7 @@ jobs:
|
||||
# 30 minutes is measured against the step: it takes 3-42s across sampled
|
||||
# runs, on failed e2e jobs as well as green ones. It RESERVES nothing --
|
||||
# `timeout-minutes` caps a step, it does not carve budget out of the job,
|
||||
# so a suite that has already burned 210 of the 215 leaves this step five
|
||||
# so a suite that has already burned 260 of the 265 leaves this step five
|
||||
# minutes and the upload is cancelled with everything else. What the cap
|
||||
# buys is that a hung read cannot be the reason.
|
||||
# `continue-on-error` because the ceiling must not change what the job
|
||||
@@ -1427,7 +1456,7 @@ jobs:
|
||||
description = `release packages candidate did not verify (result: ${CANDIDATE_RESULT})`;
|
||||
} else if (CODE !== 'true') {
|
||||
state = 'success';
|
||||
description = 'docs-only PR — e2e not required';
|
||||
description = 'docs or CI-only tools — e2e not required';
|
||||
} else if (IS_RELEASE === 'true' && IS_FULL_E2E !== 'true') {
|
||||
// Release promotion PRs deliberately do not repeat e2e: the rc was
|
||||
// already verified against the immutable candidate whose digests
|
||||
|
||||
@@ -120,7 +120,7 @@ test:
|
||||
make -C packages/core/testing apply
|
||||
make -C packages/core/testing e2e
|
||||
|
||||
unit-tests: helm-unit-tests bats-unit-tests bats-posix-compat-tests go-unit-tests go-module-tests rd-presets-check test-check-readiness test-backport-batch migrations-target-check
|
||||
unit-tests: helm-unit-tests bats-unit-tests bats-posix-compat-tests go-unit-tests go-module-tests rd-presets-check test-check-readiness migrations-target-check
|
||||
|
||||
helm-unit-tests:
|
||||
hack/helm-unit-tests.sh
|
||||
@@ -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,7 +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.
|
||||
# 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
+3
-24
@@ -25,10 +25,10 @@ type ConfigSpec struct {
|
||||
// Kubernetes major.minor version to deploy
|
||||
// +kubebuilder:default:="v1.35"
|
||||
Version Version `json:"version"`
|
||||
// External hostname for Kubernetes cluster. Defaults to `<cluster-name>.<tenant-host>` if empty.
|
||||
// External hostname for Kubernetes cluster. Defaults to `kubernetes-<cluster-name>.<tenant-host>` if empty.
|
||||
// +kubebuilder:default:=""
|
||||
Host string `json:"host"`
|
||||
// Which infrastructure provider backs this cluster's worker VMs. `kubevirt` runs them inside this cluster; `proxmox` runs them on an external Proxmox VE cluster through capmox. The control plane is Kamaji either way — this selects the infrastructure half only. Must match the `substrate` of every KubernetesNodes pool attached to this cluster: the pools reference this cluster's infrastructure object by kind, and a mismatch leaves their Machines unreconciled. Switching an existing cluster is not supported; create a new one.
|
||||
// Which infrastructure provider backs this cluster's worker VMs. `kubevirt` runs them inside this cluster; `proxmox` runs them on an external Proxmox VE cluster through capmox. The control plane is Kamaji either way — this selects the infrastructure half only. Every KubernetesNodes pool of this cluster must set the same `substrate`: a pool set to the other one refuses to render once this cluster exists. Switching an existing cluster is not supported; create a new one.
|
||||
// +kubebuilder:default:="kubevirt"
|
||||
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="substrate is immutable"
|
||||
Substrate string `json:"substrate"`
|
||||
@@ -41,9 +41,6 @@ type ConfigSpec struct {
|
||||
// Kubernetes control-plane configuration.
|
||||
// +kubebuilder:default:={}
|
||||
ControlPlane ControlPlane `json:"controlPlane"`
|
||||
// Talos worker image configuration.
|
||||
// +kubebuilder:default:={}
|
||||
Talos Talos `json:"talos"`
|
||||
// OIDC authentication and per-user RBAC for the tenant kube-apiserver. See https://cozystack.io/docs/v1.6/kubernetes/oidc-authentication/ for the operator guide.
|
||||
// +kubebuilder:default:={}
|
||||
Oidc OIDC `json:"oidc"`
|
||||
@@ -53,7 +50,7 @@ type APIServer struct {
|
||||
// Admission plugins the tenant kube-apiserver enables, passed through to KamajiControlPlane `spec.admissionControllers`, which the control plane renders as `--enable-admission-plugins`. A non-empty list replaces the control plane's default set instead of adding to it. The kube-apiserver enables these plugins in addition to its own default ones, so leaving a plugin out does not turn it off; to turn one off, name it in `--disable-admission-plugins` in `controlPlane.apiServer.extraArgs` and keep it out of this list, since the apiserver refuses to start with a plugin in both, and the control plane's default set names several. Empty by default, which keeps that default set, taken from the TenantControlPlane CRD: CertificateApproval, CertificateSigning, CertificateSubjectRestriction, DefaultIngressClass, DefaultStorageClass, DefaultTolerationSeconds, LimitRanger, MutatingAdmissionWebhook, NamespaceLifecycle, PersistentVolumeClaimResize, Priority, ResourceQuota, RuntimeClass, ServiceAccount, StorageObjectInUseProtection, TaintNodesByCondition, ValidatingAdmissionWebhook. `NodeRestriction` is not in it.
|
||||
// +kubebuilder:default:={}
|
||||
AdmissionControllers []AdmissionController `json:"admissionControllers,omitempty"`
|
||||
// Extra command-line flags appended to the tenant kube-apiserver, passed through to KamajiControlPlane `spec.apiServer.extraArgs`. For OIDC use `spec.oidc.mode` — this passthrough is the escape hatch for unrelated apiserver flags. The tenant control plane sets each of the following flags itself and drops an entry naming one before the apiserver sees it. The chart moves an `--enable-admission-plugins=` entry into `controlPlane.apiServer.admissionControllers` and a `--kubelet-preferred-address-types=` entry into `controlPlane.kubelet.preferredAddressTypes` while that field is empty, and refuses any other entry naming one of them: `--advertise-address`, `--client-ca-file`, `--enable-admission-plugins`, `--service-cluster-ip-range`, `--kubelet-client-certificate`, `--kubelet-client-key`, `--kubelet-preferred-address-types`, `--proxy-client-cert-file`, `--proxy-client-key-file`, `--requestheader-allowed-names`, `--requestheader-client-ca-file`, `--secure-port`, `--service-account-key-file`, `--service-account-signing-key-file`, `--tls-cert-file`, `--tls-private-key-file`, `--etcd-servers`, `--etcd-cafile`, `--etcd-certfile`, `--etcd-keyfile`, `--etcd-prefix`, `--etcd-compaction-interval`, `--egress-selector-config-file`. When `spec.oidc.mode` is not `None` the chart also renders OIDC flags here, reads what you have written on them, and refuses some combinations outright; the Aggregated API servers section of the Kubernetes OIDC guide on cozystack.io carries which flags, which combinations, and why. Do NOT add legacy `--oidc-*` flags here when `spec.oidc.mode` is not `None`; the chart injects `--authentication-config` and the apiserver refuses to boot with both. Empty by default (no change to current behavior).
|
||||
// Extra command-line flags appended to the tenant kube-apiserver, passed through to KamajiControlPlane `spec.apiServer.extraArgs`. For OIDC use `spec.oidc.mode` — this passthrough is the escape hatch for unrelated apiserver flags. The tenant control plane sets each of the following flags itself and drops an entry naming one before the apiserver sees it. The chart moves an `--enable-admission-plugins=` entry into `controlPlane.apiServer.admissionControllers` and a `--kubelet-preferred-address-types=` entry into `controlPlane.kubelet.preferredAddressTypes` while that field is empty, and refuses any other entry naming one of them: `--advertise-address`, `--client-ca-file`, `--enable-admission-plugins`, `--service-cluster-ip-range`, `--kubelet-client-certificate`, `--kubelet-client-key`, `--kubelet-preferred-address-types`, `--proxy-client-cert-file`, `--proxy-client-key-file`, `--requestheader-allowed-names`, `--requestheader-client-ca-file`, `--secure-port`, `--service-account-key-file`, `--service-account-signing-key-file`, `--tls-cert-file`, `--tls-private-key-file`, `--etcd-servers`, `--etcd-cafile`, `--etcd-certfile`, `--etcd-keyfile`, `--etcd-prefix`, `--etcd-compaction-interval`, `--egress-selector-config-file`. When `spec.oidc.mode` is not `None` the chart also renders OIDC flags here, reads what you have written on them, and refuses some combinations outright; the Aggregated API servers section of the Kubernetes OIDC guide on cozystack.io carries which flags, which combinations, and why. Do NOT add legacy `--oidc-*` flags here when `spec.oidc.mode` is not `None`; the chart injects `--authentication-config` and the apiserver refuses to boot with both. Empty by default.
|
||||
// +kubebuilder:default:={}
|
||||
ExtraArgs []string `json:"extraArgs,omitempty"`
|
||||
// Extra volume mounts added to the tenant kube-apiserver container, passed through to KamajiControlPlane `spec.apiServer.extraVolumeMounts`. Each `name` must reference a volume declared in `extraVolumes`; the chart-managed talos secret volumes cannot be mounted. Each item is a core/v1 VolumeMount. Empty by default.
|
||||
@@ -349,24 +346,6 @@ type Scheduler struct {
|
||||
ResourcesPreset ResourcesPreset `json:"resourcesPreset"`
|
||||
}
|
||||
|
||||
type Talos struct {
|
||||
// Base URL of the Talos Image Factory that serves the worker OS disk image (the `openstack-amd64.raw.xz` raw artifact streamed in by CDI over HTTP). Defaults to the public factory. Point at a self-hosted Image Factory, a caching mirror, or an internal HTTP file server for air-gapped, rate-limited, or flaky-egress environments. No trailing slash.
|
||||
// +kubebuilder:default:="https://factory.talos.dev"
|
||||
ImageFactoryURL string `json:"imageFactoryURL"`
|
||||
// OCI repository prefix for the Talos installer image used by the in-guest `talos-reconcile` upgrade Job. Resolved as `<installerRepository>/<schematicID>:<version>`. Defaults to the public factory's installer path. Override for air-gapped or mirrored registries. No trailing slash.
|
||||
// +kubebuilder:default:="factory.talos.dev/installer"
|
||||
InstallerRepository string `json:"installerRepository"`
|
||||
// Talos `machine.registries.mirrors` passthrough for worker nodes: a map of upstream registry host to `{ endpoints: [ ... ] }`. Empty by default, so workers pull container images (the Talos `kubelet` image included) directly from the upstream registry. Point a host such as `ghcr.io` at an in-cluster pull-through mirror for air-gapped, rate-limited, or flaky-egress environments so a worker's boot does not depend on live public egress. Talos still falls back to the upstream registry unless a host also sets `skipFallback: true`, so a mirror alone does not enforce air-gap.
|
||||
// +kubebuilder:default:={}
|
||||
RegistryMirrors k8sRuntime.RawExtension `json:"registryMirrors"`
|
||||
// Talos image-factory schematic ID. Defaults to the cozystack-tested vanilla schematic. Operators using custom schematics (system extensions, kernel args) override here.
|
||||
// +kubebuilder:default:="ce4c980550dd2ab1b17bbf2b08801c7eb59418eafe8f279833297925d67c7515"
|
||||
SchematicID string `json:"schematicID"`
|
||||
// Talos release used for worker OS image and installer. Must satisfy the chart's Talos<->Kubernetes support matrix against the chosen `version`.
|
||||
// +kubebuilder:default:="v1.13.6"
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
type VeleroAddon struct {
|
||||
// Enable Velero.
|
||||
// +kubebuilder:default:=false
|
||||
|
||||
@@ -154,7 +154,6 @@ func (in *ConfigSpec) DeepCopyInto(out *ConfigSpec) {
|
||||
in.Proxmox.DeepCopyInto(&out.Proxmox)
|
||||
in.Addons.DeepCopyInto(&out.Addons)
|
||||
in.ControlPlane.DeepCopyInto(&out.ControlPlane)
|
||||
in.Talos.DeepCopyInto(&out.Talos)
|
||||
in.Oidc.DeepCopyInto(&out.Oidc)
|
||||
}
|
||||
|
||||
@@ -570,22 +569,6 @@ func (in *Scheduler) DeepCopy() *Scheduler {
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *Talos) DeepCopyInto(out *Talos) {
|
||||
*out = *in
|
||||
in.RegistryMirrors.DeepCopyInto(&out.RegistryMirrors)
|
||||
}
|
||||
|
||||
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Talos.
|
||||
func (in *Talos) DeepCopy() *Talos {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(Talos)
|
||||
in.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *VeleroAddon) DeepCopyInto(out *VeleroAddon) {
|
||||
*out = *in
|
||||
|
||||
+23
-23
@@ -18,11 +18,11 @@ type Config struct {
|
||||
}
|
||||
|
||||
type ConfigSpec struct {
|
||||
// Name of the parent Kubernetes cluster (the `Kubernetes` CR) in the same namespace this node pool attaches to. Required. The pool's CAPI objects are wired to the parent cluster `kubernetes-<cluster>`; the KubernetesNodes CR must be named `<cluster>-<pool>`. The pool name (the part after `<cluster>-`) must not collide with a nodeGroup still managed by the parent kubernetes chart (e.g. the default `md0`), or the render fails on ownership conflict.
|
||||
// Name of the parent Kubernetes cluster (the `Kubernetes` CR) in the same namespace this node pool attaches to. Required. The pool's CAPI objects are wired to the parent cluster `kubernetes-<cluster>`; the KubernetesNodes CR must be named `<cluster>-<pool>`. The pool's objects are named `kubernetes-<cluster>-<pool>`; when a MachineDeployment of that name already exists and belongs to another Helm release, the render fails instead of taking it over.
|
||||
// +kubebuilder:default:=""
|
||||
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="cluster is immutable"
|
||||
Cluster string `json:"cluster"`
|
||||
// StorageClass for the worker node system disk. When empty, the cluster default applies. Worker VMs live-migrate, so their disks need ReadWriteMany — the RWX access mode is supplied by the chosen StorageClass's CDI StorageProfile — and linstor-csi rejects RWX volumes that are not on a DRBD-backed StorageClass, so prefer a replicated/DRBD class. On an `osImage.builtin` pool this field also decides whether the pool renders at all: CDI takes the storage-layer clone path only when the worker disk and the golden share a StorageClass, and falls back to a host-assisted copy over the pod network across classes, so the chart refuses a pool whose class differs from its golden's. Leaving it empty resolves to the cluster's default StorageClass, and the same comparison is made against that. On a `builtin` pool the resolved class is then written onto the worker disk rather than left for the cluster to fill in at bind time, so a pool that has already cloned keeps binding on the class its disks are on even after an admin moves the default annotation; on the HTTP path an empty class is still left to the cluster.
|
||||
// StorageClass for the worker node system disk. When empty, the cluster default applies. Worker VMs live-migrate, so their disks need ReadWriteMany — the RWX access mode is supplied by the chosen StorageClass's CDI StorageProfile — and linstor-csi rejects RWX volumes that are not on a DRBD-backed StorageClass, so prefer a replicated/DRBD class. On an `osImage.builtin` pool this field also decides whether the pool renders at all: CDI takes the storage-layer clone path only when the worker disk and the golden share a StorageClass, and falls back to a host-assisted copy over the pod network across classes, so the chart refuses a pool whose class differs from its golden's. Leaving it empty resolves to the cluster's default StorageClass, and the same comparison is made against that. On a `builtin` pool the resolved class is then written onto the worker disk rather than left for the cluster to fill in at bind time, so a pool that has already cloned keeps binding on the class its disks are on even after an admin moves the default annotation; on the HTTP path an empty class is still left to the cluster. Has no effect on `substrate: proxmox`, where the worker disk is cloned from the VM template onto the template's storage or `proxmox.storage`.
|
||||
// +kubebuilder:default:="replicated"
|
||||
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="storageClass is immutable"
|
||||
StorageClass string `json:"storageClass"`
|
||||
@@ -32,10 +32,10 @@ type ConfigSpec struct {
|
||||
// Maximum number of replicas in this pool. Used as the cluster-autoscaler ceiling.
|
||||
// +kubebuilder:default:=10
|
||||
MaxReplicas int `json:"maxReplicas"`
|
||||
// Virtual machine instance type.
|
||||
// KubeVirt instance type that sizes each worker VM when `resources` is not set. Ignored when `resources.cpu` and `resources.memory` are both set, and therefore on `substrate: proxmox`, which requires them.
|
||||
// +kubebuilder:default:="u1.medium"
|
||||
InstanceType string `json:"instanceType"`
|
||||
// System disk size for the worker VM. Carries the Talos OS image, kubelet state, containerd image cache, and any local-path PVCs. How the OS image reaches the disk follows this pool's `osImage`: the default and `osImage.factory` stream the Image Factory raw artifact in over HTTP, while `osImage.builtin` makes the disk a storage-layer CDI clone of a shared golden image, with no HTTP fetch. A `builtin` pool must keep diskSize at or above the golden's storage size (6Gi in the shipped worker image catalog), because a CDI clone target cannot be smaller than its source -- the chart rejects a smaller value at render time rather than letting the clone stall. Sizing a `builtin` pool also has to allow for transient headroom the finished disk does not show: CDI populates a larger target by cloning the golden at the source size and then growing it, so a worker being provisioned occupies a temporary clone PVC at the golden's size on top of this diskSize, and both are multiplied by the replica count of a replicated StorageClass. The golden itself is a separate one-time cost for the whole cluster, not a per-worker one. So the peak is the golden, plus `diskSize + golden storage` for each worker cloning at that moment. A pool whose storage backend cannot spare that peak does not fail: the clone waits for space, the VM never boots, and the reason is visible only in the storage layer's own events.
|
||||
// System disk size for the worker VM. Carries the Talos OS image, kubelet state, containerd image cache, and any local-path PVCs. How the OS image reaches the disk follows this pool's `osImage`: the default and `osImage.factory` stream the Image Factory raw artifact in over HTTP, while `osImage.builtin` makes the disk a storage-layer CDI clone of a shared golden image, with no HTTP fetch. A `builtin` pool must keep diskSize at or above the golden's storage size (6Gi in the shipped worker image catalog), because a CDI clone target cannot be smaller than its source -- the chart rejects a smaller value at render time rather than letting the clone stall. Sizing a `builtin` pool also has to allow for transient headroom the finished disk does not show: CDI populates a larger target by cloning the golden at the source size and then growing it, so a worker being provisioned occupies a temporary clone PVC at the golden's size on top of this diskSize, and both are multiplied by the replica count of a replicated StorageClass. The golden itself is a separate one-time cost for the whole cluster, not a per-worker one. So the peak is the golden, plus `diskSize + golden storage` for each worker cloning at that moment. A pool whose storage backend cannot spare that peak does not fail: the clone waits for space, the VM never boots, and the reason is visible only in the storage layer's own events. Has no effect on `substrate: proxmox`, where the worker disk is a clone of the VM template's disk and keeps its size.
|
||||
// +kubebuilder:default:="20Gi"
|
||||
DiskSize resource.Quantity `json:"diskSize"`
|
||||
// List of node roles. Each role `r` labels nodes with `node-role.kubernetes.io/<r>`. Use `[ingress-nginx]` for a pool that hosts the tenant ingress-nginx controller.
|
||||
@@ -44,16 +44,16 @@ type ConfigSpec struct {
|
||||
// Explicit CPU and memory for each worker node, as an alternative to `instanceType` sizing. Optional: when omitted, the node is sized by `instanceType`. When both `cpu` and `memory` are set, they take precedence and `instanceType` is ignored (the instancetype is omitted from the VM, since KubeVirt cannot override an instancetype's CPU/memory). Set both `cpu` and `memory` together or neither; setting only one is rejected at render time. On the kubevirt substrate `cpu` is written into the VM as spelled and decoded as YAML, so a leading zero is read as octal, as in any Kubernetes manifest: `"010"` is eight cores, and the autoscaler capacity counts eight. The kubelet reservations still read the value as a decimal, so `"010"` reserves what `"10"` does.
|
||||
// +kubebuilder:default:={}
|
||||
Resources Resources `json:"resources,omitempty"`
|
||||
// CPU limit for the worker VM's virt-launcher `compute` container, which is the ceiling the guest vCPU threads, the QEMU emulator thread and the IO threads share. Distinct from `resources.cpu`, which sizes the guest itself. Cannot be combined with `instanceType` sizing: KubeVirt refuses a VM that references an instancetype and also sets `domain.resources.limits.cpu`, so the chart rejects that combination at render time. Sizing the pool by `resources` (both `cpu` and `memory`) is what drops the instancetype from the VM, which is where the limit is usable. When unset, KubeVirt derives the limit from the guest vCPU count in any namespace carrying a ResourceQuota that limits CPU, so a worker sized at N vCPUs runs under a ceiling of N with the emulation overhead taken out of the same N. Setting a value declares the limit on the VM directly, which is both what lands it on the container and what stops KubeVirt deriving one. It moves the CPU request with it: KubeVirt copies a declared CPU limit into the CPU request when the VM declares no request of its own, so a pool that sets only this field asks the scheduler for its whole ceiling instead of the fraction of the vCPU count it asked for before, and a ceiling the nodes cannot spare leaves the workers Pending on `Insufficient cpu`. Pair it with `podCpuRequest` to move the ceiling without moving the request. Zero means no ceiling at all rather than the tightest one, since the kubelet turns a zero CPU limit into no CFS quota while the declared key still stops KubeVirt deriving one — but it is only accepted together with `podCpuRequest: 0`, because KubeVirt keeps deriving a request from the vCPU count when the VM declares none and a request above a declared limit of zero is rejected when the worker's Pod is created. A negative value is refused at render time. Changing it rolls the pool, because the worker VM template is named by a hash of its content. Keep in sync with the parent kubernetes chart.
|
||||
// CPU limit for the worker VM's virt-launcher `compute` container, which is the ceiling the guest vCPU threads, the QEMU emulator thread and the IO threads share. Distinct from `resources.cpu`, which sizes the guest itself. Cannot be combined with `instanceType` sizing: KubeVirt refuses a VM that references an instancetype and also sets `domain.resources.limits.cpu`, so the chart rejects that combination at render time. Sizing the pool by `resources` (both `cpu` and `memory`) is what drops the instancetype from the VM, which is where the limit is usable. When unset, KubeVirt derives the limit from the guest vCPU count in any namespace carrying a ResourceQuota that limits CPU, so a worker sized at N vCPUs runs under a ceiling of N with the emulation overhead taken out of the same N. Setting a value declares the limit on the VM directly, which is both what lands it on the container and what stops KubeVirt deriving one. It moves the CPU request with it: KubeVirt copies a declared CPU limit into the CPU request when the VM declares no request of its own, so a pool that sets only this field asks the scheduler for its whole ceiling instead of the fraction of the vCPU count it asked for before, and a ceiling the nodes cannot spare leaves the workers Pending on `Insufficient cpu`. Pair it with `podCpuRequest` to move the ceiling without moving the request. Zero means no ceiling at all rather than the tightest one, since the kubelet turns a zero CPU limit into no CFS quota while the declared key still stops KubeVirt deriving one — but it is only accepted together with `podCpuRequest: 0`, because KubeVirt keeps deriving a request from the vCPU count when the VM declares none and a request above a declared limit of zero is rejected when the worker's Pod is created. A negative value is refused at render time. Changing it rolls the pool, because the worker VM template is named by a hash of its content.
|
||||
PodCpuLimit resource.Quantity `json:"podCpuLimit,omitempty"`
|
||||
// CPU request for the worker VM's virt-launcher `compute` container, which is what the scheduler reserves for a worker on its host and what weights it against neighbours on a contended one. Distinct from `resources.cpu`, which sizes the guest itself. It exists to be paired with `podCpuLimit`: KubeVirt copies a declared CPU limit into the request when the VM declares no request, so raising the ceiling alone raises what every worker in the pool asks the scheduler for, and this field is what holds the request still while the ceiling moves. Cannot be combined with `instanceType` sizing, for the same reason as `podCpuLimit`: KubeVirt refuses a VM that references an instancetype and also sets `domain.resources.requests.cpu`, so the chart rejects that combination at render time, as it does a value above `podCpuLimit`. Usable without a limit, where it only changes the scheduler's weighting: KubeVirt still derives the ceiling from the guest vCPU count in any namespace carrying a ResourceQuota that limits CPU, so keep the request at or below that count. A request above that derived ceiling is refused only when the worker's Pod is created, and deliberately not at render time: the ceiling exists only where such a ResourceQuota does, so refusing here would take away a configuration that works in a namespace without one. Zero is accepted on its own and reserves nothing, but zero beside a `podCpuLimit` is refused, because KubeVirt cannot tell an explicit zero request from an absent one and replaces both with the limit — which is the outcome this field exists to avoid, arrived at silently. A negative value is refused as well. When unset, KubeVirt computes the request from the guest vCPU count and the cluster CPU allocation ratio. Changing it rolls the pool, because the worker VM template is named by a hash of its content. Keep in sync with the parent kubernetes chart.
|
||||
// CPU request for the worker VM's virt-launcher `compute` container, which is what the scheduler reserves for a worker on its host and what weights it against neighbours on a contended one. Distinct from `resources.cpu`, which sizes the guest itself. It exists to be paired with `podCpuLimit`: KubeVirt copies a declared CPU limit into the request when the VM declares no request, so raising the ceiling alone raises what every worker in the pool asks the scheduler for, and this field is what holds the request still while the ceiling moves. Cannot be combined with `instanceType` sizing, for the same reason as `podCpuLimit`: KubeVirt refuses a VM that references an instancetype and also sets `domain.resources.requests.cpu`, so the chart rejects that combination at render time, as it does a value above `podCpuLimit`. Usable without a limit, where it only changes the scheduler's weighting: KubeVirt still derives the ceiling from the guest vCPU count in any namespace carrying a ResourceQuota that limits CPU, so keep the request at or below that count. A request above that derived ceiling is refused only when the worker's Pod is created, and deliberately not at render time: the ceiling exists only where such a ResourceQuota does, so refusing here would take away a configuration that works in a namespace without one. Zero is accepted on its own and reserves nothing, but zero beside a `podCpuLimit` is refused, because KubeVirt cannot tell an explicit zero request from an absent one and replaces both with the limit — which is the outcome this field exists to avoid, arrived at silently. A negative value is refused as well. When unset, KubeVirt computes the request from the guest vCPU count and the cluster CPU allocation ratio. Changing it rolls the pool, because the worker VM template is named by a hash of its content.
|
||||
PodCpuRequest resource.Quantity `json:"podCpuRequest,omitempty"`
|
||||
// List of GPUs to attach (NVIDIA driver requires at least 4 GiB RAM).
|
||||
// +kubebuilder:default:={}
|
||||
Gpus []GPU `json:"gpus,omitempty"`
|
||||
// Kernel modules loaded on every worker in this pool, emitted as Talos `machine.kernel.modules`. A Talos system extension installs a module but does not load it, so an extension-provided driver needs its modules declared here. Leave unset to let the chart decide: a pool holding at least one `nvidia.com/*` GPU gets `nvidia` (with `NVreg_NvLinkDisable=1`, which the gpu-operator driver container applies on non-Talos workers; on Talos that container has to be turned off or it clashes with the system extension, and nothing then mounts the ConfigMap carrying the parameter), `nvidia_uvm`, `nvidia_drm`, `nvidia_modeset` (that order — Talos loads the list in sequence and the last three depend on the first), and any other pool gets nothing. Set a non-empty list to replace the chart's choice entirely, or `[]` to opt out and emit no modules even on a GPU pool. The field carries no default, and unset is not the same value as `[]`: a generated parameter table that shows `[]` in its Default column is printing what it prints for any array with no default, not the effective value — leaving the field out selects the automatic set, writing `[]` disables it. The module still has to be in the image, and which extensions the image carries is the pool's schematic: a GPU pool points `osImage.factory.schematicID` (or `osImage.builtin.schematicID`) at a schematic with the NVIDIA extensions, and on Blackwell (GB202) that must be the open-kernel-modules extension. The automatic set keys on the `nvidia.com/` resource prefix alone and nothing cross-checks it against that schematic; on Talos v1.13 a declared module the image does not ship leaves its controller retrying rather than failing the boot, so the symptom is a missing driver, not a dead node. This value is part of the worker machine config, whose content hash names the `TalosConfigTemplate`, so changing it rolls every existing worker in the pool rather than only affecting nodes created later. The automatic set is itself such a change, on the upgrade that introduces this field, for every existing pool that holds an `nvidia.com/*` GPU and leaves the field unset: that pool rolls.
|
||||
// Kernel modules loaded on every worker in this pool, emitted as Talos `machine.kernel.modules`. A Talos system extension installs a module but does not load it, so an extension-provided driver needs its modules declared here. Leave unset to let the chart decide: a pool holding at least one `nvidia.com/*` GPU gets `nvidia` (with `NVreg_NvLinkDisable=1`, which the gpu-operator driver container applies on non-Talos workers; on Talos that container has to be turned off or it clashes with the system extension, and nothing then mounts the ConfigMap carrying the parameter), `nvidia_uvm`, `nvidia_drm`, `nvidia_modeset` (that order — Talos loads the list in sequence and the last three depend on the first), and any other pool gets nothing. Set a non-empty list to replace the chart's choice entirely, or `[]` to opt out and emit no modules even on a GPU pool. The field carries no default, and unset is not the same value as `[]`: a generated parameter table that shows `[]` in its Default column is printing what it prints for any array with no default, not the effective value — leaving the field out selects the automatic set, writing `[]` disables it. The module still has to be in the image, and which extensions the image carries is the pool's schematic: a GPU pool points `osImage.factory.schematicID` (or `osImage.builtin.schematicID`) at a schematic with the NVIDIA extensions, and on Blackwell (GB202) that must be the open-kernel-modules extension. The automatic set keys on the `nvidia.com/` resource prefix alone and nothing cross-checks it against that schematic; on Talos v1.13 a declared module the image does not ship leaves its controller retrying rather than failing the boot, so the symptom is a missing driver, not a dead node. This value is part of the worker machine config, whose content hash names the `TalosConfigTemplate`, so changing it rolls every existing worker in the pool rather than only affecting nodes created later.
|
||||
KernelModules []KernelModule `json:"kernelModules,omitempty"`
|
||||
// Kubelet resource reservations for this pool.
|
||||
// Kubelet resource reservations for this pool. An empty reservation is computed from the worker's size: `resources` when set, otherwise the `instanceType`.
|
||||
// +kubebuilder:default:={}
|
||||
Kubelet Kubelet `json:"kubelet,omitempty"`
|
||||
// Stream each worker VM's guest serial console into a `guest-console-log` container beside virt-launcher, readable with `kubectl logs`. The only view of a worker that stalls during boot before Talos apid answers, when neither a Node nor a certificate request exists to diagnose from. Two costs the field name does not show. Changing the value rolls the pool, because it is part of the worker VM template whose name is a hash of its content -- including the stuck VM whose console was wanted, so it buys visibility into the next boot rather than the current one. And it overrides the platform's cluster-wide `disableSerialConsoleLog`, set because that container has been seen holding virt-launcher in `PodInitializing` (kubevirt/kubevirt#15989), which would take the pool down rather than explain it -- so check that virt-launcher Pods reach Running after enabling it.
|
||||
@@ -62,25 +62,25 @@ type ConfigSpec struct {
|
||||
// Maximum number of unhealthy nodes tolerated in this pool before remediation is paused. The MachineHealthCheck admission webhook accepts either a bare integer ("0", "1", ...) or a percentage ("0%", "50%"); bare numeric strings are rejected, so the safer default is a percentage. Drop to "0%" once the pool is stable. Watch how a percentage behaves in a small group: the MachineHealthCheck controller scales it against the number of machines the check covers and rounds down, then remediates only while the unhealthy count is at or below that result. At "50%" a group of one therefore never remediates, because half of one rounds down to zero, and a group of two remediates one unhealthy machine but not two.
|
||||
// +kubebuilder:default:="50%"
|
||||
MaxUnhealthy string `json:"maxUnhealthy"`
|
||||
// Maximum time a Machine is allowed to spend reaching the Ready condition before it is remediated. Raise for slow first boots (Talos image fetch from factory.talos.dev or a busy storage class on the kubevirt-csi PVC populator). The MachineHealthCheck measures it from the later of the Machine's creation and its InfrastructureReady transition, so the clock restarts when the infrastructure reports ready: a worker gets this long for the disk image import and the VM to come up, and then this long again for the guest to boot and its kubelet to register with the tenant apiserver. Either leg stretches on a host under load, and a Machine remediated here is deleted before it ever joins, so its replacement starts the same sequence from the beginning.
|
||||
// Maximum time a Machine is allowed to spend reaching the Ready condition before it is remediated. Raise for slow first boots (the Talos image import from the Image Factory, or a storage class slow to provision or clone the worker disk). The MachineHealthCheck measures it from the later of the Machine's creation and its InfrastructureReady transition, so the clock restarts when the infrastructure reports ready: a worker gets this long for the disk image import and the VM to come up, and then this long again for the guest to boot and its kubelet to register with the tenant apiserver. Either leg stretches on a host under load, and a Machine remediated here is deleted before it ever joins, so its replacement starts the same sequence from the beginning.
|
||||
// +kubebuilder:default:="20m"
|
||||
NodeStartupTimeout string `json:"nodeStartupTimeout"`
|
||||
// How long a worker Node may report `Ready=Unknown` before the MachineHealthCheck remediates the Machine, which for a KubeVirt worker means deleting the VM. `Ready=Unknown` is what the node lifecycle controller writes once it stops hearing from a kubelet, on a grace period of its own, so the wall clock from a guest going quiet to a deleted worker is this value plus that grace period. A guest starved of CPU stops answering long before it is dead, so a value of a few tens of seconds deletes workers that would have come back on their own. Validated at render time like maxNodeProvisionTime: the CAPI webhook does not check these timeouts, and a zero or negative value would remediate the moment the condition flips. Keep in sync with the parent kubernetes chart.
|
||||
// How long a worker Node may report `Ready=Unknown` before the MachineHealthCheck remediates the Machine, which for a KubeVirt worker means deleting the VM. `Ready=Unknown` is what the node lifecycle controller writes once it stops hearing from a kubelet, on a grace period of its own, so the wall clock from a guest going quiet to a deleted worker is this value plus that grace period. A guest starved of CPU stops answering long before it is dead, so a value of a few tens of seconds deletes workers that would have come back on their own. Validated at render time like maxNodeProvisionTime: the CAPI webhook does not check these timeouts, and a zero or negative value would remediate the moment the condition flips.
|
||||
// +kubebuilder:default:="5m"
|
||||
ReadyUnknownTimeout string `json:"readyUnknownTimeout"`
|
||||
// How long a worker Node may report `Ready=False` before the MachineHealthCheck remediates the Machine. A Node that has registered but has no working CNI yet reports `Ready=False`, so this value is the whole budget a first boot gets for the CNI rollout to reach that node, and a worker deleted mid-rollout takes the rollout with it while its replacement starts over. Raising it delays replacing a worker that is genuinely dead, but not rescheduling its workload: Pods leave an unreachable Node on the node lifecycle controller's taint-based eviction and the toleration that carries, not on this timer. Validated at render time like maxNodeProvisionTime: the CAPI webhook does not check these timeouts, and a zero or negative value would remediate the moment the condition flips. Keep in sync with the parent kubernetes chart.
|
||||
// How long a worker Node may report `Ready=False` before the MachineHealthCheck remediates the Machine. A Node that has registered but has no working CNI yet reports `Ready=False`, so this value is the whole budget a first boot gets for the CNI rollout to reach that node, and a worker deleted mid-rollout takes the rollout with it while its replacement starts over. Raising it delays replacing a worker that is genuinely dead, but not rescheduling its workload: Pods leave an unreachable Node on the node lifecycle controller's taint-based eviction and the toleration that carries, not on this timer. Validated at render time like maxNodeProvisionTime: the CAPI webhook does not check these timeouts, and a zero or negative value would remediate the moment the condition flips.
|
||||
// +kubebuilder:default:="10m"
|
||||
ReadyFalseTimeout string `json:"readyFalseTimeout"`
|
||||
// How long the cluster-autoscaler waits for a newly created worker to register as a Node before it treats that machine as long-unregistered. Rendered onto this pool's MachineDeployment as the `cluster.x-k8s.io/autoscaling-options-maxnodeprovisiontime` annotation, which overrides the autoscaler's built-in default for this pool only. Two things change when the timer fires, and both make a slow join worse rather than better: the machine stops counting towards the pool's upcoming capacity, so the autoscaler asks for a replacement for capacity that is already on its way, and the machine becomes eligible for removal. Size it above the slowest join a healthy worker can take, counting the Talos image import, the guest boot and the CNI rollout. Accepted as a whole number of seconds, minutes or hours ("30m", "1h30m"), which is narrower than Go's duration syntax on purpose: the autoscaler ignores a value it cannot parse and falls back to its own default without failing, and the narrower shape cannot express a value that overflows the parser or a fraction it rounds to zero. The canonical form a duration serializes back to ("20m0s", "1h0m0s") is accepted. Keep in sync with the parent kubernetes chart.
|
||||
// How long the cluster-autoscaler waits for a newly created worker to register as a Node before it treats that machine as long-unregistered. Rendered onto this pool's MachineDeployment as the `cluster.x-k8s.io/autoscaling-options-maxnodeprovisiontime` annotation, which overrides the autoscaler's built-in default for this pool only. Two things change when the timer fires, and both make a slow join worse rather than better: the machine stops counting towards the pool's upcoming capacity, so the autoscaler asks for a replacement for capacity that is already on its way, and the machine becomes eligible for removal. Size it above the slowest join a healthy worker can take, counting the Talos image import, the guest boot and the CNI rollout. Accepted as a whole number of seconds, minutes or hours ("30m", "1h30m"), which is narrower than Go's duration syntax on purpose: the autoscaler ignores a value it cannot parse and falls back to its own default without failing, and the narrower shape cannot express a value that overflows the parser or a fraction it rounds to zero. The canonical form a duration serializes back to ("20m0s", "1h0m0s") is accepted.
|
||||
// +kubebuilder:default:="30m"
|
||||
MaxNodeProvisionTime string `json:"maxNodeProvisionTime"`
|
||||
// Kubernetes major.minor version the pool joins. Must not be ahead of the parent cluster's minor (workers may not lead the apiserver); it may lag behind during a rolling upgrade -- bump the parent Kubernetes CR first, then each pool -- and must satisfy the Talos<->Kubernetes support matrix against `talos.version`.
|
||||
// Kubernetes major.minor version the pool joins. Must not be ahead of the parent cluster's minor (workers may not lead the apiserver); it may lag behind during a rolling upgrade -- bump the parent Kubernetes CR first, then each pool. The render fails when it is ahead. Its pairing with the Talos release the pool resolves (`talos.version`, or the `osImage` override) is checked against the chart's Talos<->Kubernetes support matrix: for a Talos minor the matrix lists, a Kubernetes minor outside that minor's list is refused; a Talos minor the matrix does not list is not checked.
|
||||
// +kubebuilder:default:="v1.35"
|
||||
Version Version `json:"version"`
|
||||
// Worker OS image source for this pool. Default (omitted): import over HTTP from the `talos.*` Image Factory. Set `osImage.builtin` to clone a golden from the opt-in worker image catalog instead. Editing this on an existing pool re-images that pool.
|
||||
// +kubebuilder:default:={}
|
||||
OsImage WorkerImage `json:"osImage,omitempty"`
|
||||
// Talos worker image configuration. Keep in sync with the parent cluster's `talos`.
|
||||
// Talos worker image configuration for this pool.
|
||||
// +kubebuilder:default:={}
|
||||
Talos Talos `json:"talos"`
|
||||
// Which infrastructure provider backs this pool's worker VMs. `kubevirt` runs them inside this cluster on KubeVirt, sized by `instanceType` and booted from a Talos disk image CDI streams from the image factory. `proxmox` runs them on an external Proxmox VE cluster through capmox, sized by `resources`, and cloned from a Talos VM template that already exists on the hypervisor. The two substrates take different Talos platform images and different sizing inputs, so switching an existing pool is refused: the chart reads the live MachineDeployment's template kind and fails the render rather than rolling every worker to the other hypervisor and deleting the template the draining MachineSet still references. Create a new pool on the other substrate and scale this one down.
|
||||
@@ -111,13 +111,13 @@ type Kubelet struct {
|
||||
// Soft eviction threshold for memory (absolute like 1Gi or percentage like 10%).
|
||||
// +kubebuilder:default:="10%"
|
||||
EvictionSoftMemory string `json:"evictionSoftMemory,omitempty"`
|
||||
// CPU reserved for kubelet and container runtime. Auto-computed from instanceType if empty.
|
||||
// CPU reserved for kubelet and container runtime. When empty, 5% of the worker's CPU, clamped to 50m..500m.
|
||||
KubeReservedCpu string `json:"kubeReservedCpu,omitempty"`
|
||||
// Memory reserved for kubelet and container runtime. Auto-computed from instanceType if empty.
|
||||
// Memory reserved for kubelet and container runtime. When empty, 5% of the worker's memory, clamped to 256Mi..1Gi.
|
||||
KubeReservedMemory string `json:"kubeReservedMemory,omitempty"`
|
||||
// CPU reserved for host OS. Auto-computed from instanceType if empty.
|
||||
// CPU reserved for host OS. When empty, 5% of the worker's CPU, clamped to 50m..500m.
|
||||
SystemReservedCpu string `json:"systemReservedCpu,omitempty"`
|
||||
// Memory reserved for host OS. Auto-computed from instanceType if empty.
|
||||
// Memory reserved for host OS. When empty, 5% of the worker's memory, clamped to 256Mi..1Gi.
|
||||
SystemReservedMemory string `json:"systemReservedMemory,omitempty"`
|
||||
}
|
||||
|
||||
@@ -160,13 +160,13 @@ type Resources struct {
|
||||
}
|
||||
|
||||
type Talos struct {
|
||||
// Complete PEM certificate block(s) CDI must trust when importing this pool's worker OS disk image over HTTPS — the no-osImage default and `osImage.factory` path. Empty by default, so the importer uses only its system trust store, which is what the public factory needs. Set this when the factory in use (`imageFactoryURL` or `osImage.factory.imageFactoryURL`) is a self-hosted factory or mirror served by an internal CA, which the importer otherwise rejects with `x509: certificate signed by unknown authority`. The chart renders it into a ConfigMap in the release namespace — CDI requires certConfigMap to sit alongside the DataVolume — and references that from the worker DataVolume's `source.http.certConfigMap`; CDI appends it to the system pool rather than replacing it, so a publicly-trusted endpoint verifies either way. A pool on `osImage.builtin` clones a golden image and builds no HTTP source, so this field has no effect there.
|
||||
// Complete PEM certificate block(s) CDI must trust when importing this pool's worker OS disk image over HTTPS — the no-osImage default and `osImage.factory` path. Empty by default, so the importer uses only its system trust store, which is what the public factory needs. Set this when the factory in use (`imageFactoryURL` or `osImage.factory.imageFactoryURL`) is a self-hosted factory or mirror served by an internal CA, which the importer otherwise rejects with `x509: certificate signed by unknown authority`. The chart renders it into a ConfigMap in the release namespace — CDI requires certConfigMap to sit alongside the DataVolume — and references that from the worker DataVolume's `source.http.certConfigMap`; CDI appends it to the system pool rather than replacing it, so a publicly-trusted endpoint verifies either way. A pool on `osImage.builtin` clones a golden image and builds no HTTP source, so this field has no effect there. On `substrate: proxmox` it has no effect on the workers either: the ConfigMap is still rendered, but nothing references it.
|
||||
// +kubebuilder:default:=""
|
||||
ImageFactoryCA string `json:"imageFactoryCA,omitempty"`
|
||||
// Default base URL of the Talos Image Factory that serves the worker OS disk image (the `openstack-amd64.raw.xz` raw artifact streamed in by CDI over HTTP). Applies only when this pool imports over HTTP — the default and `osImage.factory`, which overrides it via `osImage.factory.imageFactoryURL`. A pool on `osImage.builtin` clones a golden image from the worker image catalog and does not use this field at all. Defaults to the public factory. Point at a self-hosted Image Factory, a caching mirror, or an internal HTTP file server for air-gapped, rate-limited, or flaky-egress environments. No trailing slash. It is interpolated into the worker DataVolume source URL, so on those HTTP paths it must be a plain `http(s)` URL and the render refuses anything else; a pool on `osImage.builtin` builds no such URL and is not held to it, so blanking this field there is legal.
|
||||
// Default base URL of the Talos Image Factory that serves the worker OS disk image (the `openstack-amd64.raw.xz` raw artifact streamed in by CDI over HTTP). Applies only when this pool imports over HTTP — the default and `osImage.factory`, which overrides it via `osImage.factory.imageFactoryURL`. A pool on `osImage.builtin` clones a golden image from the worker image catalog and does not use this field at all. Defaults to the public factory. Point at a self-hosted Image Factory, a caching mirror, or an internal HTTP file server for air-gapped, rate-limited, or flaky-egress environments. No trailing slash. It is interpolated into the worker DataVolume source URL, so on those HTTP paths it must be a plain `http(s)` URL and the render refuses anything else; a pool on `osImage.builtin` builds no such URL and is not held to it, so blanking this field there is legal. On `substrate: proxmox` the workers clone a VM template that already carries the OS, so this field has no effect there beyond that format check.
|
||||
// +kubebuilder:default:="https://factory.talos.dev"
|
||||
ImageFactoryURL string `json:"imageFactoryURL"`
|
||||
// OCI repository prefix for the Talos installer image used by the in-guest `talos-reconcile` upgrade Job. Resolved as `<installerRepository>/<schematicID>:<version>` against whatever this pool's `osImage` pins, falling back to `talos.schematicID` and `talos.version` — so the installer matches the OS the pool actually booted. The prefix itself has no `osImage.*` override, so an air-gapped pool needs it pointed at a reachable registry as well. Defaults to the public factory's installer path. Override for air-gapped or mirrored registries. No trailing slash.
|
||||
// OCI repository prefix for the Talos installer image, written into the worker machine config as `machine.install.image`. Resolved as `<installerRepository>/<schematicID>:<version>` against whatever this pool's `osImage` pins, falling back to `talos.schematicID` and `talos.version` — so the installer matches the OS the pool actually booted. The prefix itself has no `osImage.*` override, so an air-gapped pool needs it pointed at a reachable registry as well. Defaults to the public factory's installer path. Override for air-gapped or mirrored registries. No trailing slash.
|
||||
// +kubebuilder:default:="factory.talos.dev/installer"
|
||||
InstallerRepository string `json:"installerRepository"`
|
||||
// Talos `machine.registries.mirrors` passthrough for worker nodes: a map of upstream registry host to `{ endpoints: [ ... ] }`. Empty by default, so workers pull container images (the Talos `kubelet` image included) directly from the upstream registry. Point a host such as `ghcr.io` at an in-cluster pull-through mirror for air-gapped, rate-limited, or flaky-egress environments so a worker's boot does not depend on live public egress. Talos still falls back to the upstream registry unless a host also sets `skipFallback: true`, so a mirror alone does not enforce air-gap. This value is part of the worker machine config, whose content hash names the TalosConfigTemplate, so changing it rolls every existing worker in the pool rather than only affecting nodes created later.
|
||||
@@ -178,7 +178,7 @@ type Talos struct {
|
||||
// Sysctl overrides rendered into Talos `machine.sysctls` on this pool's guest workers only. Values must be strings (quote numeric values); for example, `user.max_user_namespaces: "11255"` enables the user namespaces required by the Talos gVisor extension. An empty map preserves Talos defaults and the existing bootstrap hash. Adding, changing or removing an override changes the content-hashed TalosConfigTemplate and rolls the pool's workers. Supported names and values depend on the worker's Talos release and kernel; unsafe settings can weaken guest isolation or prevent workers from booting. This does not install gVisor or configure a RuntimeClass, and does not change the management hosts, control plane or other pools.
|
||||
// +kubebuilder:default:={}
|
||||
Sysctls map[string]string `json:"sysctls,omitempty"`
|
||||
// Default Talos release for this pool's worker OS image and in-guest installer. The pool can pin its own release via `osImage.builtin.version` or `osImage.factory.version`; when `osImage` omits it, this value applies. Both ways in are checked against the chart's Talos<->Kubernetes support matrix, against whichever release the pool actually resolves to, so an override cannot slip past a check `talos.version` would have failed. The matrix rejects a pairing it lists as unsupported; it lists the Talos minors this chart ships and passes any other minor unchecked, so pointing either field at a Talos release the chart does not know about is the operator's call, not a checked one. Whichever release applies is interpolated into an image URL and a DataVolume name, so it must be a full `vMAJOR.MINOR.PATCH` release, optionally with a pre-release suffix -- the render refuses anything else, a minor-only `v1.13` included. The values schema types this as a bare string, so a malformed one is accepted by the CR and fails the pool's HelmRelease instead.
|
||||
// Default Talos release for this pool's worker OS image and for the installer image written into the worker machine config. The pool can pin its own release via `osImage.builtin.version` or `osImage.factory.version`; when `osImage` omits it, this value applies. Both ways in are checked against the chart's Talos<->Kubernetes support matrix, against whichever release the pool actually resolves to, so an override cannot slip past a check `talos.version` would have failed. The matrix lists, for each Talos minor the chart knows, the Kubernetes minors it supports and refuses a pool `version` outside that list; a Talos minor it does not list passes unchecked, so pointing either field at a Talos release the chart does not know about is the operator's call, not a checked one. Whichever release applies is interpolated into an image URL and a DataVolume name, so it must be a full `vMAJOR.MINOR.PATCH` release, optionally with a pre-release suffix -- the render refuses anything else, a minor-only `v1.13` included. The values schema types this as a bare string, so a malformed one is accepted by the CR and fails the pool's HelmRelease instead.
|
||||
// +kubebuilder:default:="v1.13.6"
|
||||
Version string `json:"version"`
|
||||
}
|
||||
@@ -198,7 +198,7 @@ type WorkerImageBuiltin struct {
|
||||
}
|
||||
|
||||
type WorkerImageFactory struct {
|
||||
// Base URL of the Image Factory serving the boot raw image. Defaults to `talos.imageFactoryURL`. No trailing slash. NOTE: this redirects only the worker boot-disk import; the in-guest upgrade installer image is always sourced from `talos.installerRepository`, so an air-gapped pool must also have `talos.installerRepository` pointed at a reachable registry.
|
||||
// Base URL of the Image Factory serving the boot raw image. Defaults to `talos.imageFactoryURL`. No trailing slash. This redirects only the worker boot-disk import; the installer image in the worker machine config is always sourced from `talos.installerRepository`, so an air-gapped pool must also have `talos.installerRepository` pointed at a reachable registry.
|
||||
ImageFactoryURL string `json:"imageFactoryURL,omitempty"`
|
||||
// Talos schematic ID. Defaults to `talos.schematicID`.
|
||||
SchematicID string `json:"schematicID,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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
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"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const modulePath = "github.com/cozystack/cozystack/"
|
||||
|
||||
// readTestInputs returns the entries of test-inputs.txt, the list CI reads to
|
||||
// decide whether a pull request runs these tests.
|
||||
func readTestInputs(t *testing.T) []string {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile("test-inputs.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var entries []string
|
||||
for _, line := range strings.Split(string(data), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
entries = append(entries, line)
|
||||
}
|
||||
if len(entries) == 0 {
|
||||
t.Fatal("test-inputs.txt lists no path")
|
||||
}
|
||||
return entries
|
||||
}
|
||||
|
||||
// covered reports whether a change to path matches an entry the way the plan
|
||||
// job matches it: a trailing slash is a directory prefix, anything else is
|
||||
// one file.
|
||||
func covered(entries []string, path string) bool {
|
||||
for _, e := range entries {
|
||||
if strings.HasSuffix(e, "/") && strings.HasPrefix(path, e) || path == e {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// A pull request that changes an input of these tests has to run them, and CI
|
||||
// knows the inputs only from test-inputs.txt. An import added here, or added to
|
||||
// a package this tool imports, lands in a pull request that touches a listed
|
||||
// path, so it runs this test, which fails until the new package is listed.
|
||||
func TestInputsListed(t *testing.T) {
|
||||
entries := readTestInputs(t)
|
||||
|
||||
out, err := exec.Command("go", "list", "-deps", "-test", "-f", "{{.ImportPath}}", ".").Output()
|
||||
if err != nil {
|
||||
t.Fatalf("go list: %v", err)
|
||||
}
|
||||
var deps int
|
||||
for _, pkg := range strings.Split(strings.TrimSpace(string(out)), "\n") {
|
||||
// A test variant prints as "pkg [pkg.test]"; the synthesized main
|
||||
// package as "pkg.test". Both are this directory.
|
||||
pkg, _, _ = strings.Cut(pkg, " ")
|
||||
pkg = strings.TrimSuffix(pkg, ".test")
|
||||
rel, ok := strings.CutPrefix(pkg, modulePath)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
deps++
|
||||
if !covered(entries, rel+"/x.go") {
|
||||
t.Errorf("these tests import %s, which test-inputs.txt does not list: add %s/", pkg, rel)
|
||||
}
|
||||
}
|
||||
if deps == 0 {
|
||||
t.Fatalf("go list named no package of %s, so nothing was checked:\n%s", modulePath, out)
|
||||
}
|
||||
|
||||
for _, page := range []string{policyPage, "docs/release/backport-tasks/x.md"} {
|
||||
if !covered(entries, page) {
|
||||
t.Errorf("the tests read %s, which test-inputs.txt does not cover", page)
|
||||
}
|
||||
}
|
||||
for _, f := range []string{"go.mod", "go.sum"} {
|
||||
if !covered(entries, f) {
|
||||
t.Errorf("test-inputs.txt does not list %s", f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A listed path that no longer exists makes the list read as broader than it
|
||||
// is, and usually means a dependency moved and its new home is missing.
|
||||
func TestInputsExist(t *testing.T) {
|
||||
for _, e := range readTestInputs(t) {
|
||||
if _, err := os.Stat("../../" + e); err != nil {
|
||||
t.Errorf("test-inputs.txt lists %s: %v", e, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
+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
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
# Every path whose change can change the result of
|
||||
# `go test ./cmd/backport-batch/...`. The plan job in
|
||||
# .github/workflows/pull-requests.yaml runs those tests when, and only when, the
|
||||
# pull request touches one of these paths.
|
||||
#
|
||||
# One repository-relative path per line. A trailing slash names a directory and
|
||||
# matches everything below it; anything else matches that file alone. Blank lines
|
||||
# and lines starting with # are ignored.
|
||||
#
|
||||
# TestInputsListed in inputs_test.go keeps this list in step with the code: it
|
||||
# fails when the tests' in-repo package dependencies (go list -deps -test) or the
|
||||
# docs pages they read are missing from it.
|
||||
|
||||
cmd/backport-batch/
|
||||
go.mod
|
||||
go.sum
|
||||
|
||||
# In-repo packages the tool and its tests import.
|
||||
internal/apigate/
|
||||
pkg/apis/apps/v1alpha1/
|
||||
pkg/apis/apps/validation/
|
||||
pkg/config/
|
||||
|
||||
# Pages the tests read from the tree.
|
||||
docs/release/backports.md
|
||||
docs/release/backport-tasks/
|
||||
@@ -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,9 @@ 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/dynamic"
|
||||
"k8s.io/client-go/dynamic/dynamicinformer"
|
||||
"k8s.io/client-go/informers"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
@@ -56,6 +60,7 @@ type options struct {
|
||||
tailIdleTimeout time.Duration
|
||||
shutdownTimeout time.Duration
|
||||
logs observabilityproxy.LogsLimits
|
||||
metrics observabilityproxy.MetricsConfig
|
||||
limiter observabilityproxy.LimiterConfig
|
||||
}
|
||||
|
||||
@@ -81,8 +86,23 @@ 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")
|
||||
// Only the refusal threshold: the lookup itself spans the window the
|
||||
// query reads, and that estimate is a loose upper bound (every "("
|
||||
// counts as an implicit subquery), so the default is long enough to
|
||||
// serve month-long panels at a dashboard step.
|
||||
fs.DurationVar(&o.metrics.App.Lookback, "metrics-app-lookback", 30*24*time.Hour, "how far before the window an application metrics query may read; one reading further back, or by an unknown amount, answers 422")
|
||||
fs.IntVar(&o.metrics.App.MaxPods, "metrics-app-max-pods", 500, "most pods of one application in the lookup window; more answer 422")
|
||||
fs.IntVar(&o.metrics.App.MaxPVCs, "metrics-app-max-pvcs", 500, "most PVCs of one application in the lookup window; more answer 422")
|
||||
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.Float64Var(&o.limiter.RatePerUser, "rate-limit-per-user", 10, "sustained client requests per second per user; an application metrics request makes three store calls (pod lookup, PVC lookup, query); 0 disables")
|
||||
fs.IntVar(&o.limiter.RateBurst, "rate-limit-burst", 20, "request burst per user")
|
||||
fs.IntVar(&o.limiter.MaxTailsPerUser, "max-tails-per-user", 5, "open tails per user; 0 disables")
|
||||
fs.IntVar(&o.limiter.MaxTails, "max-tails", 200, "open tails in total; 0 disables")
|
||||
@@ -94,6 +114,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 +137,19 @@ 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.metrics.App.Lookback <= 0 || o.metrics.App.MaxPods < 1 || o.metrics.App.MaxPVCs < 1:
|
||||
return o, errors.New("--metrics-app-lookback, --metrics-app-max-pods and --metrics-app-max-pvcs must be positive")
|
||||
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
|
||||
@@ -151,13 +189,24 @@ func run(o options) error {
|
||||
return fmt.Errorf("building client: %w", err)
|
||||
}
|
||||
|
||||
dynClient, err := dynamic.NewForConfig(restConfig)
|
||||
if err != nil {
|
||||
return fmt.Errorf("building dynamic client: %w", err)
|
||||
}
|
||||
|
||||
factory := informers.NewSharedInformerFactory(client, 0)
|
||||
namespaces := factory.Core().V1().Namespaces()
|
||||
lister := namespaces.Lister()
|
||||
appDefs := dynamicinformer.NewDynamicSharedInformerFactory(dynClient, 0).
|
||||
ForResource(observabilityproxy.ApplicationDefinitionsResource).Informer()
|
||||
if err := appDefs.AddIndexers(observabilityproxy.ApplicationPluralIndexers()); err != nil {
|
||||
return fmt.Errorf("indexing application definitions: %w", err)
|
||||
}
|
||||
factory.Start(ctx.Done())
|
||||
go appDefs.Run(ctx.Done())
|
||||
var ready atomic.Bool
|
||||
go func() {
|
||||
if cache.WaitForCacheSync(ctx.Done(), namespaces.Informer().HasSynced) {
|
||||
if cache.WaitForCacheSync(ctx.Done(), namespaces.Informer().HasSynced, appDefs.HasSynced) {
|
||||
ready.Store(true)
|
||||
}
|
||||
}()
|
||||
@@ -167,16 +216,18 @@ 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,
|
||||
}, observabilityproxy.NewAuthorizer(client, o.authCacheTTL), observabilityproxy.NewStoreResolver(lister, namespaces.Informer().HasSynced, o.rootTenant), reg)
|
||||
}, observabilityproxy.NewAuthorizer(client, o.authCacheTTL), observabilityproxy.NewStoreResolver(lister, namespaces.Informer().HasSynced, o.rootTenant),
|
||||
observabilityproxy.NewApplicationKinds(appDefs.GetIndexer(), appDefs.HasSynced), reg)
|
||||
|
||||
probes := http.NewServeMux()
|
||||
probes.HandleFunc("/healthz", func(w http.ResponseWriter, _ *http.Request) { _, _ = io.WriteString(w, "ok") })
|
||||
probes.HandleFunc("/readyz", func(w http.ResponseWriter, _ *http.Request) {
|
||||
if !ready.Load() {
|
||||
http.Error(w, "namespace cache not synced", http.StatusServiceUnavailable)
|
||||
http.Error(w, "namespace or application definition cache not synced", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
_, _ = io.WriteString(w, "ok")
|
||||
|
||||
@@ -32,6 +32,20 @@ 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.App.Lookback != 30*24*time.Hour || m.App.MaxPods != 500 || m.App.MaxPVCs != 500 {
|
||||
t.Errorf("application scope defaults changed: %+v", m.App)
|
||||
}
|
||||
if m.RootTenant != o.rootTenant {
|
||||
t.Errorf("metrics root tenant = %q, want --root-tenant", m.RootTenant)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsOverrides(t *testing.T) {
|
||||
@@ -48,6 +62,23 @@ 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",
|
||||
"--metrics-app-lookback=2h", "--metrics-app-max-pods=7", "--metrics-app-max-pvcs=3",
|
||||
})
|
||||
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 ||
|
||||
m.App.Lookback != 2*time.Hour || m.App.MaxPods != 7 || m.App.MaxPVCs != 3 {
|
||||
t.Errorf("overrides not applied: %+v", m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseFlagsInvalid(t *testing.T) {
|
||||
for _, args := range [][]string{
|
||||
{"--root-tenant="},
|
||||
@@ -69,7 +100,23 @@ 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"},
|
||||
{"--metrics-app-lookback=-1h"},
|
||||
// Every query reads some time before its window, so a zero lookback
|
||||
// would refuse every application metrics query.
|
||||
{"--metrics-app-lookback=0s"},
|
||||
{"--metrics-app-max-pods=0"},
|
||||
{"--metrics-app-max-pvcs=0"},
|
||||
{"--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 +124,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.
|
||||
|
||||
@@ -121,7 +121,7 @@ Be precise about TIA's scope — it is narrower than "skip E2E for unrelated PRs
|
||||
- **It trims only which suites `chainsaw test` runs, not the expensive stages.** `Prepare environment` and `Install Cozystack` (the full platform install) run regardless of the selection. TIA only decides the `CHAINSAW_SUITES` passed to the final E2E step. A perfect narrow saves the suite tail, not the bulk of wall-time. (Package image builds are scoped separately by `hack/build-matrix.sh`; the Talos leg runs when its direct or shared inputs changed and remains unconditional for forks, while installer/finalize runs for every non-docs PR. That is a distinct mechanism from TIA's suite trimming.)
|
||||
- **`Select E2E tests` runs *after* `Install Cozystack` and has no `always()`.** GitHub implicitly ANDs a step's `if:` with `success()`, so if install fails the selector (and `Run E2E tests`) are skipped entirely. On a red install you will not see TIA narrow anything — that is the ordering, not a selector bug.
|
||||
- **Most PRs legitimately escalate to the full suite.** Platform manifests live under `packages/core/`, Go under `api/`/`internal/`, helpers under `hack/`, plus the `Makefile` — touching any one triggers the full suite. So "all suites selected" is usually correct, not a failure to narrow.
|
||||
- **Two skip layers, with a gap.** The `plan` job computes a coarse docs-only gate (`code`, true for any change outside `docs/**`) that skips the whole build+E2E pipeline for docs-only PRs, plus a per-package build matrix (`hack/build-matrix.sh`) that scopes which images build. TIA's finer skip (`select-e2e.sh` ignoring `docs/`, `dashboards/`, `*.md` and the rest of `inert_config_pattern`) only trims the suite selection. A PR touching only `dashboards/*.json` therefore still runs install, then skips just the Chainsaw suites — near-zero savings. If you need such PRs to skip the pipeline, widen the `plan` docs-gate, not `select-e2e.sh`.
|
||||
- **Two skip layers, with a gap.** The `plan` job computes a coarse gate (`code`, false only when every changed path is under `docs/` or one of the CI-only Go tools no image builds, `cmd/backport-batch/` and `cmd/api-gate/`) that skips the whole build+E2E pipeline for such PRs, and `e2e-fork.yaml` skips on the same prefixes (`hack/gate-concurrency-contract.bats` holds the two lists equal), plus a per-package build matrix (`hack/build-matrix.sh`) that scopes which images build. TIA's finer skip (`select-e2e.sh` ignoring `docs/`, `dashboards/`, `*.md` and the rest of `inert_config_pattern`) only trims the suite selection. A PR touching only `dashboards/*.json` therefore still runs install, then skips just the Chainsaw suites — near-zero savings. If you need such PRs to skip the pipeline, widen the `plan` docs-gate, not `select-e2e.sh`.
|
||||
|
||||
### 8. Failure must be self-explanatory — diagnostics are first-class
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ A chart also declares the limits on its own resource name there, with `## @name
|
||||
|
||||
## Testing
|
||||
|
||||
- **Helm and BATS unit tests:** `make helm-unit-tests` runs `hack/helm-unit-tests.sh` over every package that defines a `test` target. A `test` target is expected to run helm-unittest: the sweep fails a package whose run reports no suite, because exiting 0 having asserted nothing is indistinguishable from passing. A target that drives something else belongs under another name, as `packages/core/testing` already does with its sandbox flow. `make bats-unit-tests` requires bats-core 1.5 or newer; GNU parallel is optional and the target runs serially without it. Run the complete local BATS and POSIX-compatibility pass with `pre-commit run bats-unit-tests --hook-stage manual --all-files`; pull-request CI runs both lanes for code changes and the BATS contracts for docs-only changes. On a host whose `/bin/sh` differs from CI, `make BATS_POSIX_SHELL=dash bats-posix-compat-tests` selects the same interpreter explicitly. `make unit-tests` runs the full unit suite — Helm, BATS, the targeted POSIX-shell compatibility pass, Go, and the preset/readiness checks. It does not include the controller tests under `./internal/...`; run `make test-controllers` as well. CI invokes both targets together.
|
||||
- **Helm and BATS unit tests:** `make helm-unit-tests` runs `hack/helm-unit-tests.sh` over every package that defines a `test` target. A `test` target is expected to run helm-unittest: the sweep fails a package whose run reports no suite, because exiting 0 having asserted nothing is indistinguishable from passing. A target that drives something else belongs under another name, as `packages/core/testing` already does with its sandbox flow. `make bats-unit-tests` requires bats-core 1.5 or newer; GNU parallel is optional and the target runs serially without it. Run the complete local BATS and POSIX-compatibility pass with `pre-commit run bats-unit-tests --hook-stage manual --all-files`; pull-request CI runs both lanes for code changes and the BATS contracts for docs-only changes. On a host whose `/bin/sh` differs from CI, `make BATS_POSIX_SHELL=dash bats-posix-compat-tests` selects the same interpreter explicitly. `make unit-tests` runs the full unit suite — Helm, BATS, the targeted POSIX-shell compatibility pass, Go, and the preset/readiness checks. It does not include the controller tests under `./internal/...`; run `make test-controllers` as well. CI invokes both targets together. The backport-batch tests are not part of it: run `make test-backport-batch` after changing anything listed in `cmd/backport-batch/test-inputs.txt`, which is also what CI keys them on.
|
||||
- **E2E tests:** Kyverno Chainsaw suites in `hack/e2e-chainsaw/` (one directory per app), run with `chainsaw test`. Cluster bootstrap (`hack/e2e-install-cozystack.bats`) and the OpenAPI checks (`hack/e2e-test-openapi.bats`) remain BATS. Conventions for writing and stabilising them — and the CI that runs them — live in [`e2e-testing.md`](./e2e-testing.md).
|
||||
- **Go tests:** standard `go test`, with Ginkgo/Gomega for controllers.
|
||||
|
||||
@@ -158,6 +158,8 @@ When a document could go in either `docs/` or the website, the test is whether a
|
||||
|
||||
Documenting a feature's current limitations is useful and belongs with the feature. Documenting the *development process* around it does not: no "What is NOT in Phase N", no "out of scope for this PR", no "this PR" phrasing, no action plans or review negotiation. A merged document describes what the code does now, and states a limitation as a limitation rather than as a roadmap position. Scope, phasing and follow-ups belong in the pull request description or an issue.
|
||||
|
||||
An app's README and the field descriptions in its `values.yaml` describe what the app does now, for the person using it: no history ("moves from", "previously", "used to", rollout phase names) and no account of how another chart behaved before. A change users must act on is announced by a `!` in the pull request title and described on the website's per-version upgrade page (`content/en/docs/next/operations/cluster/upgrade-to-vX.Y.md`), so an app README carries no history or breaking-changes section. A description says what the field does, what it accepts and what it refuses, not what it replaced, and does not tell the reader to keep it in sync with another chart; when two charts have to agree, a test says so. A change that touches a README or a description fixes what is wrong in that place while it is there.
|
||||
|
||||
A document that names an unmerged pull request or an unreleased dependency needs a tracking issue, because nothing re-checks that claim later and it silently becomes false once the thing ships.
|
||||
|
||||
### Domain references for specific tasks
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ git diff --name-only $PREV..$NEW -- 'packages/*/charts/*/Chart.yaml' 'packages/*
|
||||
done
|
||||
|
||||
# B. Out-of-Chart.yaml pins (Talos, tenant K8s, vendored images)
|
||||
for path in packages/core/talos/images/talos/profiles/installer.yaml packages/core/installer/values.yaml packages/apps/kubernetes/values.yaml; do
|
||||
for path in packages/core/talos/images/talos/profiles/installer.yaml packages/core/installer/values.yaml packages/apps/kubernetes/values.yaml packages/apps/kubernetes-nodes/values.yaml; do
|
||||
git diff $PREV..$NEW -- "$path" | head -40
|
||||
done
|
||||
git diff --name-only $PREV..$NEW -- 'images/*/Dockerfile'
|
||||
|
||||
@@ -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"
|
||||
|
||||
+422
-109
@@ -1,14 +1,28 @@
|
||||
#!/usr/bin/env bats
|
||||
# Behavioural tests for the shipped Altinity (ClickHouse) backup/restore
|
||||
# strategy script, extracted from the rendered cozy-default-altinity Strategy
|
||||
# CR and EXECUTED against a stubbed clickhouse-backup HTTP API, with the real
|
||||
# jq doing the archive selection. Which archive a restore reads back is
|
||||
# decided by that selection over list/remote, so it is exercised against
|
||||
# several archives rather than string-matched.
|
||||
# Behavioural tests for the shipped ClickHouse (Altinity) strategy script,
|
||||
# extracted from the rendered cozy-default-altinity Strategy CR and EXECUTED
|
||||
# against a stubbed clickhouse-backup sidecar API.
|
||||
#
|
||||
# The script runs with `bash`, not `sh`, for the reason given in
|
||||
# hack/redis-backup-strategy.bats: it opens `set -euo pipefail`, which the
|
||||
# production image's busybox ash supports and the CI runner's dash rejects.
|
||||
# The contract pinned here is the start gate: the sidecar serialises its
|
||||
# operations and answers any new one with HTTP 423 while another is "in
|
||||
# progress", so a strategy Pod that starts its operation without waiting for
|
||||
# an idle sidecar burns a Job attempt in seconds. The gate has to fail the Pod
|
||||
# rather than proceed when the sidecar never becomes idle. Once started, an
|
||||
# operation the sidecar no longer lists (its action log lives in memory and a
|
||||
# restart empties it) has to fail the Pod too, since the Job has no deadline.
|
||||
# A Job retry must not start its operation twice: it takes over the
|
||||
# restore_remote, or the create_remote of the archive the controller named,
|
||||
# that the attempt before it left running, and a few lost polls do not end an
|
||||
# attempt in the first place.
|
||||
#
|
||||
# Which archive a restore reads back is decided by the script's own jq
|
||||
# selection over list/remote, so it is exercised against several archives
|
||||
# rather than string-matched: exactly the archive the Backup recorded, or for
|
||||
# a Backup that records none, the newest one stamped no later than its takenAt.
|
||||
#
|
||||
# Run from the repo root with make bats-unit-tests. The rendered script is run
|
||||
# with `bash`, not `sh`: it opens `set -euo pipefail`, which the production
|
||||
# image's busybox ash supports but the CI runner's /bin/sh (dash) rejects.
|
||||
|
||||
load test_helper
|
||||
|
||||
@@ -16,12 +30,13 @@ CHART=packages/system/backupstrategy-controller
|
||||
|
||||
# Render the shipped strategy's in-Pod script into $1 and substitute the
|
||||
# controller-render placeholders the way the driver fills them: $2 mode, $3
|
||||
# app (release and source application), $4 the archive name recorded on the
|
||||
# Backup (empty for a Backup that records none), $5 the Backup name, $6 its
|
||||
# takenAt in the archive-name layout. An empty render fails early, as does a
|
||||
# placeholder left unsubstituted.
|
||||
# app (release and source application), $4 the archive name the controller
|
||||
# chose or the Backup recorded (empty, the default, for none), $5 the Backup
|
||||
# name, $6 its takenAt in the archive-name layout (default: later than every
|
||||
# archive the stub lists). An empty render fails early, as does a placeholder
|
||||
# left unsubstituted: these tests EXECUTE it.
|
||||
render_script() {
|
||||
out=$1; mode=$2; app=$3; archive=$4; backup=$5; takenat=$6
|
||||
out=$1; mode=$2; app=$3; archive=${4-}; backup=${5-bkp}; takenat=${6-99991231T235959Z}
|
||||
helm template t "$CHART" -n cozy-backup-controller \
|
||||
--set backupStorage.bucketNameOverride=test-bucket 2>/dev/null \
|
||||
| yq eval 'select(.kind == "Altinity") | .spec.template.spec.containers[0].args[0]' - \
|
||||
@@ -39,56 +54,118 @@ render_script() {
|
||||
fi
|
||||
}
|
||||
|
||||
# Stub the clickhouse-backup API behind curl, first on PATH. list/remote
|
||||
# serves $state/remote; actions serves $state/actions plus one success row per
|
||||
# operation the script has issued, and after its first read swaps in
|
||||
# $state/actions.next when a test seeds one (an operation that finishes while
|
||||
# the script waits). Every request is logged to $state/curl.log. sleep is
|
||||
# stubbed to fail after a few calls, so a script that never sees its
|
||||
# operation finish fails the test instead of hanging it.
|
||||
# A curl stub standing in for the sidecar API, put first on PATH next to a
|
||||
# no-op sleep. State lives in $1:
|
||||
# unreachable present: every request fails as a refused connection (7)
|
||||
# down a number: that many next requests fail the same way, as
|
||||
# before the sidecar has started
|
||||
# busy a number: that many GET /backup/actions answers still list
|
||||
# an operation of someone else "in progress"
|
||||
# op the command line of the sidecar's own operation, written
|
||||
# by an accepted POST the way the sidecar logs it ("--resume"
|
||||
# before the name of a create_remote asking for it), or
|
||||
# seeded by a test as the operation
|
||||
# an earlier attempt of the same Job started
|
||||
# op_left how many more GET /backup/actions answers list op as
|
||||
# "in progress" before it ends with op_result
|
||||
# op_duration op_left given to an operation a POST starts (default 0)
|
||||
# op_result the status op ends with (default success)
|
||||
# prior extra action-log rows, verbatim, older than op
|
||||
# remote present: the list/remote answer, verbatim, in place of the
|
||||
# one archive listed by default
|
||||
# restarted present: the action log no longer lists op, as after the
|
||||
# sidecar restarted and lost its in-memory log
|
||||
# fail_on_post a POST that is accepted makes that many following
|
||||
# GET /backup/actions requests fail (curl exit 7)
|
||||
# idle created by an answer that lists nothing in progress; a
|
||||
# POST is refused (423, curl -f exit 22) unless one was seen
|
||||
# since the last accepted POST, and while anything runs
|
||||
# accepted one line per POST the sidecar accepted
|
||||
# requests one line per request: method and URL
|
||||
# runaway created when the script exceeds 200 requests; that request
|
||||
# and every later one fails, so a polling loop that never ends
|
||||
# stops the test instead of hanging it
|
||||
make_stubs() {
|
||||
bin=$1; state=$2
|
||||
mkdir -p "$bin"
|
||||
: > "$state/remote"
|
||||
: > "$state/actions"
|
||||
mkdir -p "$bin" "$state"
|
||||
cat > "$bin/curl" <<STUB
|
||||
#!/usr/bin/env bash
|
||||
set -eu
|
||||
method=GET; url=""
|
||||
#!/bin/bash
|
||||
state="$state"
|
||||
method=GET
|
||||
url=
|
||||
prev=
|
||||
for a in "\$@"; do
|
||||
case "\$a" in POST) method=POST ;; http://*) url="\$a" ;; esac
|
||||
if [ "\$prev" = "-X" ]; then method=\$a; fi
|
||||
case "\$a" in http://*) url=\$a ;; esac
|
||||
prev=\$a
|
||||
done
|
||||
echo "\$method \$url" >> "$state/curl.log"
|
||||
echo "\$method \$url" >> "\$state/requests"
|
||||
if [ "\$(wc -l < "\$state/requests")" -gt 200 ]; then touch "\$state/runaway"; exit 7; fi
|
||||
if [ -e "\$state/unreachable" ]; then exit 7; fi
|
||||
num() { cat "\$state/\$1" 2>/dev/null || echo 0; }
|
||||
down=\$(num down)
|
||||
if [ "\$down" -gt 0 ]; then echo \$((down - 1)) > "\$state/down"; exit 7; fi
|
||||
running=0
|
||||
if [ -s "\$state/op" ] && [ ! -e "\$state/restarted" ] && [ "\$(num op_left)" -gt 0 ]; then running=1; fi
|
||||
case "\$method \$url" in
|
||||
"GET "*/backup/status) echo '{}' ;;
|
||||
"GET "*/backup/list/remote) cat "$state/remote" ;;
|
||||
"POST "*)
|
||||
if [ "\$(num busy)" -gt 0 ] || [ "\$running" = 1 ] || [ ! -e "\$state/idle" ]; then exit 22; fi
|
||||
case "\$url" in
|
||||
*/create_remote\?name=*)
|
||||
name=\${url#*name=}; name=\${name%%&*}
|
||||
case "\$url" in *[?\&]resume=true*) cmd="create_remote --resume \$name" ;; *) cmd="create_remote \$name" ;; esac
|
||||
;;
|
||||
*/restore_remote/*) cmd="restore_remote \${url##*/}" ;;
|
||||
*) exit 22 ;;
|
||||
esac
|
||||
echo "\$cmd" > "\$state/op"
|
||||
echo "\$cmd" >> "\$state/accepted"
|
||||
num op_duration > "\$state/op_left"
|
||||
num fail_on_post > "\$state/fail_polls"
|
||||
rm -f "\$state/idle"
|
||||
;;
|
||||
"GET "*/backup/actions)
|
||||
cat "$state/actions"
|
||||
if [ -f "$state/actions.done" ]; then cat "$state/actions.done"; fi
|
||||
if [ -f "$state/actions.next" ]; then mv "$state/actions.next" "$state/actions"; fi
|
||||
fail=\$(num fail_polls)
|
||||
if [ "\$fail" -gt 0 ]; then echo \$((fail - 1)) > "\$state/fail_polls"; exit 7; fi
|
||||
echo '{"command":"create_remote clickhouse-myapp-20260101T000000Z","status":"error","start":"2026-01-01 00:00:00","error":"old"}'
|
||||
cat "\$state/prior" 2>/dev/null || true
|
||||
inprog=0
|
||||
busy=\$(num busy)
|
||||
if [ "\$busy" -gt 0 ]; then
|
||||
echo \$((busy - 1)) > "\$state/busy"
|
||||
echo '{"command":"upload --resumable=1 clickhouse-myapp-20260101T000000Z","status":"in progress","start":"2026-01-01 00:01:00"}'
|
||||
inprog=1
|
||||
fi
|
||||
if [ -s "\$state/op" ] && [ ! -e "\$state/restarted" ]; then
|
||||
left=\$(num op_left)
|
||||
if [ "\$left" -gt 0 ]; then
|
||||
echo \$((left - 1)) > "\$state/op_left"
|
||||
status="in progress"
|
||||
inprog=1
|
||||
else
|
||||
status=\$(cat "\$state/op_result" 2>/dev/null || echo success)
|
||||
fi
|
||||
echo "{\"command\":\"\$(cat "\$state/op")\",\"status\":\"\$status\",\"start\":\"2026-01-01 00:02:00\",\"error\":\"boom\"}"
|
||||
fi
|
||||
if [ "\$inprog" = 0 ]; then touch "\$state/idle"; fi
|
||||
;;
|
||||
"POST "*/backup/create_remote*)
|
||||
name=\${url#*name=}; name=\${name%%&*}
|
||||
printf '{"command":"create_remote --resume %s","status":"success","start":"2099-01-01 00:00:00"}\n' "\$name" >> "$state/actions.done"
|
||||
"GET "*/backup/list/remote)
|
||||
if [ -e "\$state/remote" ]; then cat "\$state/remote"; else
|
||||
echo '{"name":"clickhouse-myapp-20260101T000000Z","created":"2026-01-01 00:00:00"}'
|
||||
fi
|
||||
;;
|
||||
"POST "*/backup/restore_remote/*)
|
||||
printf '{"command":"restore_remote %s","status":"success","start":"2099-01-01 00:00:00"}\n' "\${url##*/}" >> "$state/actions.done"
|
||||
;;
|
||||
*) echo "unexpected request: \$method \$url" >&2; exit 22 ;;
|
||||
esac
|
||||
exit 0
|
||||
STUB
|
||||
cat > "$bin/sleep" <<STUB
|
||||
#!/usr/bin/env bash
|
||||
n=\$(cat "$state/sleeps" 2>/dev/null || echo 0)
|
||||
echo \$((n + 1)) > "$state/sleeps"
|
||||
if [ "\$n" -ge 3 ]; then echo "sleep budget spent: the operation never finished" >&2; exit 1; fi
|
||||
cat > "$bin/sleep" <<'STUB'
|
||||
#!/bin/sh
|
||||
exit 0
|
||||
STUB
|
||||
chmod 0755 "$bin/curl" "$bin/sleep"
|
||||
export API_USERNAME=u API_PASSWORD=p
|
||||
}
|
||||
|
||||
# One list/remote row per argument, "name" or "name|desc"; created mirrors
|
||||
# the name's timestamp, which is the order the pre-fix selection sorted by.
|
||||
# the name's timestamp.
|
||||
remote_rows() {
|
||||
for row in "$@"; do
|
||||
name=${row%%|*}; desc="tar, regular"
|
||||
@@ -98,49 +175,216 @@ remote_rows() {
|
||||
done
|
||||
}
|
||||
|
||||
accepted() {
|
||||
if [ -e "$1/state/accepted" ]; then wc -l < "$1/state/accepted" | tr -d ' '; else echo 0; fi
|
||||
}
|
||||
|
||||
run_script() {
|
||||
tmp=$1
|
||||
API_USERNAME=u API_PASSWORD=p PATH="$tmp/bin:$PATH" bash "$tmp/s.sh"
|
||||
}
|
||||
|
||||
@test "an empty render fails the fixture instead of passing as a no-op" {
|
||||
tmp=$(mktemp -d)
|
||||
CHART=$tmp/no-such-chart
|
||||
if render_script "$tmp/s.sh" restore myapp "" bkp 20260304T050622Z 2>/dev/null; then
|
||||
if render_script "$tmp/s.sh" backup myapp 2>/dev/null; then
|
||||
echo "FAIL: render_script reported success with no chart"
|
||||
false
|
||||
fi
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
|
||||
@test "backup on an idle sidecar starts create_remote and waits for its success" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
render_script "$tmp/s.sh" backup myapp
|
||||
run_script "$tmp"
|
||||
grep -q '^POST .*/backup/create_remote?name=clickhouse-myapp-' "$tmp/state/requests"
|
||||
}
|
||||
|
||||
@test "backup waits until the running operation finishes before create_remote" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo 3 > "$tmp/state/busy"
|
||||
render_script "$tmp/s.sh" backup myapp
|
||||
run_script "$tmp"
|
||||
grep -q '^create_remote --resume clickhouse-myapp-' "$tmp/state/accepted"
|
||||
[ "$(cat "$tmp/state/busy")" = 0 ]
|
||||
}
|
||||
|
||||
@test "restore on an idle sidecar starts restore_remote of the listed archive" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
render_script "$tmp/s.sh" restore myapp
|
||||
run_script "$tmp"
|
||||
grep -q '^POST .*/backup/restore_remote/clickhouse-myapp-20260101T000000Z$' "$tmp/state/requests"
|
||||
}
|
||||
|
||||
@test "backup fails without starting create_remote when the sidecar stays busy" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo 1000 > "$tmp/state/busy"
|
||||
render_script "$tmp/s.sh" backup myapp
|
||||
if run_script "$tmp"; then echo "FAIL: a busy sidecar must fail the Pod"; false; fi
|
||||
if grep -q '^POST ' "$tmp/state/requests"; then echo "FAIL: no operation may start on a busy sidecar"; false; fi
|
||||
}
|
||||
|
||||
@test "backup fails without starting create_remote when the sidecar never answers" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
touch "$tmp/state/unreachable"
|
||||
render_script "$tmp/s.sh" backup myapp
|
||||
if run_script "$tmp"; then echo "FAIL: an unreachable sidecar must fail the Pod"; false; fi
|
||||
if grep -q '^POST ' "$tmp/state/requests"; then echo "FAIL: no operation may start on an unreachable sidecar"; false; fi
|
||||
}
|
||||
|
||||
@test "restore fails without starting restore_remote when the sidecar never answers" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
touch "$tmp/state/unreachable"
|
||||
render_script "$tmp/s.sh" restore myapp
|
||||
if run_script "$tmp" 2> "$tmp/err"; then echo "FAIL: an unreachable sidecar must fail the Pod"; false; fi
|
||||
if grep -q '^POST ' "$tmp/state/requests"; then echo "FAIL: no restore may start on an unreachable sidecar"; false; fi
|
||||
grep -q 'list/remote failed 60 times in a row' "$tmp/err"
|
||||
[ "$(grep -c '^GET .*/backup/list/remote$' "$tmp/state/requests")" = 60 ]
|
||||
}
|
||||
|
||||
@test "restore fails without starting restore_remote when the sidecar stays busy" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo 1000 > "$tmp/state/busy"
|
||||
render_script "$tmp/s.sh" restore myapp
|
||||
if run_script "$tmp"; then echo "FAIL: a busy sidecar must fail the Pod"; false; fi
|
||||
if grep -q '^POST ' "$tmp/state/requests"; then echo "FAIL: no restore may start on a busy sidecar"; false; fi
|
||||
}
|
||||
|
||||
@test "backup fails instead of polling forever when the sidecar restarts after create_remote" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
touch "$tmp/state/restarted"
|
||||
render_script "$tmp/s.sh" backup myapp
|
||||
if run_script "$tmp" 2> "$tmp/err"; then echo "FAIL: an operation lost from the action log must fail the Pod"; false; fi
|
||||
grep -q 'no longer in the sidecar action log' "$tmp/err"
|
||||
grep -q '^POST .*/backup/create_remote?name=clickhouse-myapp-' "$tmp/state/requests"
|
||||
if [ -e "$tmp/state/runaway" ]; then echo "FAIL: the script kept polling for an operation the sidecar no longer lists"; false; fi
|
||||
}
|
||||
|
||||
@test "restore waits for a sidecar that has not started yet" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo 10 > "$tmp/state/down"
|
||||
render_script "$tmp/s.sh" restore myapp
|
||||
run_script "$tmp"
|
||||
[ "$(accepted "$tmp")" = 1 ]
|
||||
}
|
||||
|
||||
@test "restore waits until another operation finishes, then starts its own" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo 3 > "$tmp/state/busy"
|
||||
render_script "$tmp/s.sh" restore myapp
|
||||
run_script "$tmp"
|
||||
[ "$(accepted "$tmp")" = 1 ]
|
||||
grep -qx 'restore_remote clickhouse-myapp-20260101T000000Z' "$tmp/state/accepted"
|
||||
}
|
||||
|
||||
@test "failed polls after restore_remote, short of six in a row, do not fail the attempt" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo 3 > "$tmp/state/op_duration"
|
||||
echo 5 > "$tmp/state/fail_on_post"
|
||||
render_script "$tmp/s.sh" restore myapp
|
||||
run_script "$tmp"
|
||||
[ "$(accepted "$tmp")" = 1 ]
|
||||
}
|
||||
|
||||
@test "a retry after an attempt lost its polls joins the running restore instead of starting another" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo 20 > "$tmp/state/op_duration"
|
||||
echo 6 > "$tmp/state/fail_on_post"
|
||||
render_script "$tmp/s.sh" restore myapp
|
||||
if run_script "$tmp"; then echo "FAIL: six failed polls in a row must fail the attempt"; false; fi
|
||||
[ "$(accepted "$tmp")" = 1 ]
|
||||
echo 0 > "$tmp/state/fail_polls"
|
||||
run_script "$tmp"
|
||||
if [ "$(accepted "$tmp")" != 1 ]; then echo "FAIL: the retry started restore_remote a second time"; false; fi
|
||||
[ "$(cat "$tmp/state/op_left")" = 0 ]
|
||||
}
|
||||
|
||||
@test "restore does not join an operation running on another archive" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo 'restore_remote clickhouse-myapp-20251231T000000Z' > "$tmp/state/op"
|
||||
echo 3 > "$tmp/state/op_left"
|
||||
render_script "$tmp/s.sh" restore myapp
|
||||
run_script "$tmp"
|
||||
[ "$(accepted "$tmp")" = 1 ]
|
||||
grep -qx 'restore_remote clickhouse-myapp-20260101T000000Z' "$tmp/state/accepted"
|
||||
}
|
||||
|
||||
# The sidecar never trims its action log, so a finished restore_remote row of
|
||||
# the same archive may belong to an earlier RestoreJob; only a running one may
|
||||
# be taken over.
|
||||
@test "restore starts restore_remote again when the log holds a finished one of the same archive" {
|
||||
for status in success error cancel; do
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo "{\"command\":\"restore_remote clickhouse-myapp-20260101T000000Z\",\"status\":\"$status\",\"start\":\"2026-01-01 00:01:30\"}" > "$tmp/state/prior"
|
||||
render_script "$tmp/s.sh" restore myapp
|
||||
run_script "$tmp"
|
||||
if [ "$(accepted "$tmp")" != 1 ]; then echo "FAIL: a $status row of an earlier restore must not stand in for this one"; false; fi
|
||||
done
|
||||
}
|
||||
|
||||
@test "a restore that ends in error fails the Pod" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo error > "$tmp/state/op_result"
|
||||
render_script "$tmp/s.sh" restore myapp
|
||||
if run_script "$tmp"; then echo "FAIL: a failed restore must fail the Pod"; false; fi
|
||||
[ "$(accepted "$tmp")" = 1 ]
|
||||
}
|
||||
|
||||
@test "a cancelled backup fails the Pod instead of polling forever" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo cancel > "$tmp/state/op_result"
|
||||
render_script "$tmp/s.sh" backup myapp
|
||||
if run_script "$tmp"; then echo "FAIL: a cancelled operation must fail the Pod"; false; fi
|
||||
if [ -e "$tmp/state/runaway" ]; then echo "FAIL: the script kept polling a cancelled operation"; false; fi
|
||||
}
|
||||
|
||||
@test "restore reads back the archive the Backup recorded, not the newest one" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp"
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
render_script "$tmp/s.sh" restore myapp clickhouse-myapp-20260304T050607Z bkp-a 20260304T050622Z
|
||||
remote_rows clickhouse-myapp-20260304T040000Z clickhouse-myapp-20260304T050607Z \
|
||||
clickhouse-myapp-20260304T060000Z clickhouse-myapp-20260304T070000Z > "$tmp/remote"
|
||||
PATH="$tmp/bin:$PATH" bash "$tmp/s.sh"
|
||||
grep -qxF 'POST http://chi-clickhouse-myapp-clickhouse-0-0.tenant-x.svc:7171/backup/restore_remote/clickhouse-myapp-20260304T050607Z' "$tmp/curl.log"
|
||||
[ "$(grep -c '/backup/restore_remote/' "$tmp/curl.log")" -eq 1 ]
|
||||
rm -rf "$tmp"
|
||||
clickhouse-myapp-20260304T060000Z clickhouse-myapp-20260304T070000Z > "$tmp/state/remote"
|
||||
run_script "$tmp"
|
||||
grep -qxF 'POST http://chi-clickhouse-myapp-clickhouse-0-0.tenant-x.svc:7171/backup/restore_remote/clickhouse-myapp-20260304T050607Z' "$tmp/state/requests"
|
||||
[ "$(accepted "$tmp")" = 1 ]
|
||||
}
|
||||
|
||||
@test "restore fails naming the recorded archive when remote storage lacks it" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp"
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
render_script "$tmp/s.sh" restore myapp clickhouse-myapp-20260304T050607Z bkp-a 20260304T050622Z
|
||||
remote_rows clickhouse-myapp-20260304T040000Z clickhouse-myapp-20260304T060000Z > "$tmp/remote"
|
||||
remote_rows clickhouse-myapp-20260304T040000Z clickhouse-myapp-20260304T060000Z > "$tmp/state/remote"
|
||||
out=""
|
||||
if out=$(PATH="$tmp/bin:$PATH" bash "$tmp/s.sh" 2>&1); then
|
||||
if out=$(run_script "$tmp" 2>&1); then
|
||||
echo "FAIL: restore succeeded without the recorded archive: $out"
|
||||
false
|
||||
fi
|
||||
printf '%s\n' "$out" | grep -qF 'archive clickhouse-myapp-20260304T050607Z recorded on Backup bkp-a is not in remote storage'
|
||||
if grep -qF '/backup/restore_remote/' "$tmp/curl.log"; then
|
||||
echo "FAIL: restored another archive: $(cat "$tmp/curl.log")"
|
||||
if grep -qF '/backup/restore_remote/' "$tmp/state/requests"; then
|
||||
echo "FAIL: restored another archive: $(cat "$tmp/state/requests")"
|
||||
false
|
||||
fi
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
|
||||
@test "restore of a Backup without a recorded archive takes the newest one stamped by its takenAt, and warns" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp"
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
render_script "$tmp/s.sh" restore myapp "" bkp-a 20260304T050800Z
|
||||
# Each decoy sorts after the expected archive and is ruled out by one
|
||||
# filter: broken, not a bare timestamp after the prefix, or stamped after
|
||||
@@ -148,71 +392,140 @@ remote_rows() {
|
||||
remote_rows clickhouse-myapp-20260304T040000Z clickhouse-myapp-20260304T050607Z \
|
||||
"clickhouse-myapp-20260304T050700Z|broken (can't stat metadata.json)" \
|
||||
clickhouse-myapp-20260304T050710Z-copy \
|
||||
clickhouse-myapp-20260304T060000Z > "$tmp/remote"
|
||||
out=$(PATH="$tmp/bin:$PATH" bash "$tmp/s.sh" 2>&1)
|
||||
grep -qxF 'POST http://chi-clickhouse-myapp-clickhouse-0-0.tenant-x.svc:7171/backup/restore_remote/clickhouse-myapp-20260304T050607Z' "$tmp/curl.log"
|
||||
[ "$(grep -c '/backup/restore_remote/' "$tmp/curl.log")" -eq 1 ]
|
||||
myapp-20260304050720-0f3c9a1e5b7d \
|
||||
clickhouse-myapp-20260304T060000Z > "$tmp/state/remote"
|
||||
out=$(run_script "$tmp" 2>&1)
|
||||
grep -qxF 'POST http://chi-clickhouse-myapp-clickhouse-0-0.tenant-x.svc:7171/backup/restore_remote/clickhouse-myapp-20260304T050607Z' "$tmp/state/requests"
|
||||
[ "$(accepted "$tmp")" = 1 ]
|
||||
printf '%s\n' "$out" | grep -qF 'WARNING: Backup bkp-a records no archive name'
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
|
||||
@test "restore of a Backup without a recorded archive fails when no archive is stamped by its takenAt" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp"
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
render_script "$tmp/s.sh" restore myapp "" bkp-a 20260304T050800Z
|
||||
remote_rows clickhouse-myapp-20260304T060000Z clickhouse-myapp-20260304T070000Z > "$tmp/remote"
|
||||
remote_rows clickhouse-myapp-20260304T060000Z clickhouse-myapp-20260304T070000Z > "$tmp/state/remote"
|
||||
out=""
|
||||
if out=$(PATH="$tmp/bin:$PATH" bash "$tmp/s.sh" 2>&1); then
|
||||
if out=$(run_script "$tmp" 2>&1); then
|
||||
echo "FAIL: restore succeeded with every archive newer than takenAt: $out"
|
||||
false
|
||||
fi
|
||||
printf '%s\n' "$out" | grep -qF 'no archive of myapp stamped no later than 20260304T050800Z'
|
||||
if grep -qF '/backup/restore_remote/' "$tmp/curl.log"; then
|
||||
echo "FAIL: restored an archive newer than takenAt: $(cat "$tmp/curl.log")"
|
||||
if grep -qF '/backup/restore_remote/' "$tmp/state/requests"; then
|
||||
echo "FAIL: restored an archive newer than takenAt: $(cat "$tmp/state/requests")"
|
||||
false
|
||||
fi
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
|
||||
@test "backup writes the archive under the name the controller chose" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp"
|
||||
render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z "" ""
|
||||
PATH="$tmp/bin:$PATH" bash "$tmp/s.sh"
|
||||
grep -qxF 'POST http://chi-clickhouse-myapp-clickhouse-0-0.tenant-x.svc:7171/backup/create_remote?name=clickhouse-myapp-20260304T050607Z&resume=true' "$tmp/curl.log"
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
|
||||
@test "a retried backup waits on the attempt the sidecar is still running" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp"
|
||||
render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z "" ""
|
||||
printf '{"command":"create_remote --resume clickhouse-myapp-20260304T050607Z","status":"in progress","start":"2026-03-04 05:06:08"}\n' > "$tmp/actions"
|
||||
printf '{"command":"create_remote --resume clickhouse-myapp-20260304T050607Z","status":"success","start":"2026-03-04 05:06:08"}\n' > "$tmp/actions.next"
|
||||
PATH="$tmp/bin:$PATH" bash "$tmp/s.sh"
|
||||
# The sidecar answers a second operation with 423 while one runs.
|
||||
if grep -qF 'POST ' "$tmp/curl.log"; then
|
||||
echo "FAIL: started another operation over the running one: $(cat "$tmp/curl.log")"
|
||||
false
|
||||
fi
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
|
||||
@test "a retried backup after a failed attempt resumes the same archive" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp"
|
||||
render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z "" ""
|
||||
printf '{"command":"create_remote clickhouse-myapp-20260304T050607Z","status":"error","start":"2026-03-04 05:06:08","error":"S3 PutObject 403"}\n' > "$tmp/actions"
|
||||
PATH="$tmp/bin:$PATH" bash "$tmp/s.sh"
|
||||
grep -qxF 'POST http://chi-clickhouse-myapp-clickhouse-0-0.tenant-x.svc:7171/backup/create_remote?name=clickhouse-myapp-20260304T050607Z&resume=true' "$tmp/curl.log"
|
||||
rm -rf "$tmp"
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z
|
||||
run_script "$tmp"
|
||||
grep -qxF 'POST http://chi-clickhouse-myapp-clickhouse-0-0.tenant-x.svc:7171/backup/create_remote?name=clickhouse-myapp-20260304T050607Z&resume=true' "$tmp/state/requests"
|
||||
}
|
||||
|
||||
@test "backup falls back to a timestamped name when the controller supplies none" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp"
|
||||
render_script "$tmp/s.sh" backup myapp "" "" ""
|
||||
PATH="$tmp/bin:$PATH" bash "$tmp/s.sh"
|
||||
grep -qE '^POST http://[^ ]*/backup/create_remote\?name=clickhouse-myapp-[0-9]{8}T[0-9]{6}Z&resume=true$' "$tmp/curl.log"
|
||||
rm -rf "$tmp"
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
render_script "$tmp/s.sh" backup myapp ""
|
||||
run_script "$tmp"
|
||||
grep -qE '^POST http://[^ ]*/backup/create_remote\?name=clickhouse-myapp-[0-9]{8}T[0-9]{6}Z&resume=true$' "$tmp/state/requests"
|
||||
}
|
||||
|
||||
# The sidecar answers a second operation with 423 while one runs, so a retry
|
||||
# that POSTed over its predecessor's upload would burn every attempt left.
|
||||
@test "a retried backup takes over the upload of its archive the sidecar is still running" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo 'create_remote --resume clickhouse-myapp-20260304T050607Z' > "$tmp/state/op"
|
||||
echo 3 > "$tmp/state/op_left"
|
||||
render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z
|
||||
run_script "$tmp"
|
||||
if grep -q '^POST ' "$tmp/state/requests"; then
|
||||
echo "FAIL: started another operation over the running one: $(cat "$tmp/state/requests")"
|
||||
false
|
||||
fi
|
||||
[ "$(cat "$tmp/state/op_left")" = 0 ]
|
||||
}
|
||||
|
||||
@test "a taken-over backup that ends in error fails the Pod" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo 'create_remote --resume clickhouse-myapp-20260304T050607Z' > "$tmp/state/op"
|
||||
echo 2 > "$tmp/state/op_left"
|
||||
echo error > "$tmp/state/op_result"
|
||||
render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z
|
||||
if run_script "$tmp"; then echo "FAIL: a taken-over upload that failed must fail the Pod"; false; fi
|
||||
if grep -q '^POST ' "$tmp/state/requests"; then
|
||||
echo "FAIL: started another operation over the running one: $(cat "$tmp/state/requests")"
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
@test "a retried backup after a failed attempt resumes the same archive" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo '{"command":"create_remote clickhouse-myapp-20260304T050607Z","status":"error","start":"2026-01-01 00:01:30","error":"S3 PutObject 403"}' > "$tmp/state/prior"
|
||||
render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z
|
||||
run_script "$tmp"
|
||||
grep -qxF 'POST http://chi-clickhouse-myapp-clickhouse-0-0.tenant-x.svc:7171/backup/create_remote?name=clickhouse-myapp-20260304T050607Z&resume=true' "$tmp/state/requests"
|
||||
}
|
||||
|
||||
@test "backup does not take over an upload running for another archive" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo 'create_remote --resume clickhouse-myapp-20260304T040000Z' > "$tmp/state/op"
|
||||
echo 3 > "$tmp/state/op_left"
|
||||
render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z
|
||||
run_script "$tmp"
|
||||
[ "$(accepted "$tmp")" = 1 ]
|
||||
grep -qx 'create_remote --resume clickhouse-myapp-20260304T050607Z' "$tmp/state/accepted"
|
||||
}
|
||||
|
||||
# The archive is named per BackupJob, so a success row of exactly this
|
||||
# upload is an earlier attempt of the same BackupJob that finished while it
|
||||
# could not see it. Uploading again with resume would re-upload over the
|
||||
# finished archive and break it if that second upload dies halfway.
|
||||
@test "a retried backup takes the upload of its archive an earlier attempt finished as its result" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo '{"command":"create_remote --resume clickhouse-myapp-20260304T050607Z","status":"success","start":"2026-01-01 00:01:30"}' > "$tmp/state/prior"
|
||||
render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z
|
||||
run_script "$tmp"
|
||||
if grep -q '^POST ' "$tmp/state/requests"; then
|
||||
echo "FAIL: uploaded again over a finished archive: $(cat "$tmp/state/requests")"
|
||||
false
|
||||
fi
|
||||
}
|
||||
|
||||
@test "a retried backup resumes an upload of its archive that ended in error or was cancelled" {
|
||||
for status in error cancel; do
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo "{\"command\":\"create_remote --resume clickhouse-myapp-20260304T050607Z\",\"status\":\"$status\",\"start\":\"2026-01-01 00:01:30\"}" > "$tmp/state/prior"
|
||||
render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z
|
||||
run_script "$tmp"
|
||||
if [ "$(accepted "$tmp")" != 1 ]; then echo "FAIL: a $status row must lead to a resumed upload"; false; fi
|
||||
done
|
||||
}
|
||||
|
||||
@test "a retried backup resumes when its latest upload failed after an earlier one succeeded" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
printf '%s\n' \
|
||||
'{"command":"create_remote --resume clickhouse-myapp-20260304T050607Z","status":"success","start":"2026-01-01 00:01:00"}' \
|
||||
'{"command":"create_remote --resume clickhouse-myapp-20260304T050607Z","status":"error","start":"2026-01-01 00:01:30"}' > "$tmp/state/prior"
|
||||
render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z
|
||||
run_script "$tmp"
|
||||
[ "$(accepted "$tmp")" = 1 ]
|
||||
}
|
||||
|
||||
@test "backup does not take another archive's finished upload as its own" {
|
||||
tmp=$(mktemp -d)
|
||||
make_stubs "$tmp/bin" "$tmp/state"
|
||||
echo '{"command":"create_remote --resume clickhouse-myapp-20260304T040000Z","status":"success","start":"2026-01-01 00:01:30"}' > "$tmp/state/prior"
|
||||
render_script "$tmp/s.sh" backup myapp clickhouse-myapp-20260304T050607Z
|
||||
run_script "$tmp"
|
||||
[ "$(accepted "$tmp")" = 1 ]
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
#!/usr/bin/env bats
|
||||
# Unit tests for hack/backport-batch-tests-needed.sh, which decides whether a
|
||||
# pull request runs `make test-backport-batch`, and for its wiring into the
|
||||
# checks job of pull-requests.yaml.
|
||||
#
|
||||
# Test-level EXIT traps replace Bats' own handler and hide failing TAP results.
|
||||
# Cleanup follows aborting assertions; see docs/agents/e2e-testing.md.
|
||||
|
||||
load test_helper
|
||||
|
||||
CLASSIFIER="hack/backport-batch-tests-needed.sh"
|
||||
WF=".github/workflows/pull-requests.yaml"
|
||||
|
||||
classify() {
|
||||
list="$1"
|
||||
shift
|
||||
printf '%s\n' "$@" > "$list"
|
||||
"$CLASSIFIER" "$list"
|
||||
}
|
||||
|
||||
@test "a change to the tool, an imported package, a page its tests read or the module runs the tests" {
|
||||
tmp=$(mktemp)
|
||||
[ "$(classify "$tmp" cmd/backport-batch/report_test.go)" = true ]
|
||||
[ "$(classify "$tmp" internal/apigate/surface.go)" = true ]
|
||||
[ "$(classify "$tmp" pkg/config/config.go)" = true ]
|
||||
[ "$(classify "$tmp" docs/release/backports.md)" = true ]
|
||||
[ "$(classify "$tmp" docs/release/backport-tasks/conflict.md)" = true ]
|
||||
[ "$(classify "$tmp" go.sum)" = true ]
|
||||
# One input among unrelated paths is enough.
|
||||
[ "$(classify "$tmp" packages/apps/mariadb/values.yaml pkg/config/config.go)" = true ]
|
||||
rm -f "$tmp"
|
||||
}
|
||||
|
||||
@test "a change that reaches none of its inputs does not run the tests" {
|
||||
tmp=$(mktemp)
|
||||
[ "$(classify "$tmp" packages/apps/mariadb/values.yaml)" = false ]
|
||||
[ "$(classify "$tmp" cmd/api-gate/main.go)" = false ]
|
||||
[ "$(classify "$tmp" hack/backport-batch-scheduled.sh)" = false ]
|
||||
[ "$(classify "$tmp" docs/release.md docs/release/other.md)" = false ]
|
||||
# A file entry is not a prefix: backports.md does not cover backports.md.bak,
|
||||
# and a directory entry does not cover a sibling sharing its name as prefix.
|
||||
[ "$(classify "$tmp" docs/release/backports.md.bak)" = false ]
|
||||
[ "$(classify "$tmp" pkg/configx/x.go)" = false ]
|
||||
[ "$(classify "$tmp" internal/apigate-old/x.go)" = false ]
|
||||
rm -f "$tmp"
|
||||
}
|
||||
|
||||
@test "entries are trimmed as the Go test trims them" {
|
||||
tmp=$(mktemp -d)
|
||||
printf '%s\n' cmd/backport-batch/main.go > "$tmp/diff"
|
||||
printf ' cmd/backport-batch/ \r\n' > "$tmp/inputs"
|
||||
[ "$("$CLASSIFIER" "$tmp/diff" "$tmp/inputs")" = true ]
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
|
||||
@test "an inputs file with no entry is refused rather than read as never" {
|
||||
tmp=$(mktemp -d)
|
||||
printf '%s\n' cmd/backport-batch/main.go > "$tmp/diff"
|
||||
printf '# only a comment\n\n' > "$tmp/inputs"
|
||||
if "$CLASSIFIER" "$tmp/diff" "$tmp/inputs" >"$tmp/out" 2>"$tmp/err"; then
|
||||
echo "an empty inputs file must fail, it printed: $(cat "$tmp/out")" >&2
|
||||
exit 1
|
||||
fi
|
||||
grep -q 'lists no path' "$tmp/err"
|
||||
rm -rf "$tmp"
|
||||
}
|
||||
|
||||
@test "the checks job runs the target on the classifier's answer alone" {
|
||||
plan_run=$(yq -r '.jobs.plan.steps[] | select(.id == "p") | .run' "$WF")
|
||||
printf '%s\n' "$plan_run" | grep -qF 'backport_batch="$(hack/backport-batch-tests-needed.sh /tmp/changed_norenames.txt)"'
|
||||
[ "$(yq -r '.jobs.plan.outputs.backport_batch' "$WF")" = '${{ steps.p.outputs.backport_batch }}' ]
|
||||
|
||||
step_if=$(yq -r '.jobs.checks.steps[] | select(.name == "Run backport-batch tests") | .if' "$WF")
|
||||
step_run=$(yq -r '.jobs.checks.steps[] | select(.name == "Run backport-batch tests") | .run' "$WF")
|
||||
# !cancelled(): a failed unit lane above must not skip this suite too.
|
||||
[ "$step_if" = "\${{ !cancelled() && needs.plan.outputs.backport_batch == 'true' }}" ]
|
||||
[ "$step_run" = 'make test-backport-batch' ]
|
||||
|
||||
# And nowhere else: the unit lane every code PR runs must not carry it.
|
||||
prereqs=$(sed -n 's/^unit-tests:[[:space:]]*//p' Makefile)
|
||||
[ -n "$prereqs" ]
|
||||
case " $prereqs " in
|
||||
*" test-backport-batch "*)
|
||||
echo "make unit-tests still runs test-backport-batch on every code PR: $prereqs" >&2
|
||||
exit 1 ;;
|
||||
esac
|
||||
}
|
||||
Executable
+44
@@ -0,0 +1,44 @@
|
||||
#!/bin/sh
|
||||
# Print whether a changed-path list touches an input of
|
||||
# `go test ./cmd/backport-batch/...`, as listed in
|
||||
# cmd/backport-batch/test-inputs.txt. An entry ending in / matches every path
|
||||
# below it, any other entry matches that file alone. The caller supplies the
|
||||
# rename-blind list so both sides of a rename are classified.
|
||||
#
|
||||
# Usage: hack/backport-batch-tests-needed.sh <changed-files> [<inputs-file>]
|
||||
set -eu
|
||||
|
||||
inputs="${2:-cmd/backport-batch/test-inputs.txt}"
|
||||
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ] || [ ! -r "$1" ] || [ ! -r "$inputs" ]; then
|
||||
echo "usage: backport-batch-tests-needed.sh <readable-changed-files> [<readable-inputs-file>]" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
# An inputs file with no entry would answer false for every diff and switch the
|
||||
# tests off without a word; refuse instead.
|
||||
if ! grep -qvE '^[[:space:]]*(#|$)' "$inputs"; then
|
||||
echo "backport-batch-tests-needed: $inputs lists no path" >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
# Entries are trimmed the way TestInputsListed trims them, so a trailing space
|
||||
# or a CR cannot make the Go test accept an entry this never matches.
|
||||
awk '
|
||||
NR == FNR {
|
||||
e = $0
|
||||
sub(/^[[:space:]]+/, "", e)
|
||||
sub(/[[:space:]]+$/, "", e)
|
||||
if (e != "" && e !~ /^#/) entries[++n] = e
|
||||
next
|
||||
}
|
||||
{
|
||||
for (i = 1; i <= n; i++) {
|
||||
e = entries[i]
|
||||
if ((substr(e, length(e)) == "/" && index($0, e) == 1) || $0 == e) {
|
||||
found = 1
|
||||
exit
|
||||
}
|
||||
}
|
||||
}
|
||||
END { print (found ? "true" : "false") }
|
||||
' "$inputs" "$1"
|
||||
@@ -67,7 +67,13 @@ if [ -z "$CHANGED" ] || [ "$CHANGED" = "FULL" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if grep -qE "$full_rebuild_pattern" "$CHANGED"; then
|
||||
# CI-only Go tools that no image builds. Dropped before the full-rebuild check,
|
||||
# whose cmd/ would otherwise rebuild everything for an edit no image contains.
|
||||
# hack/select-e2e.sh skips the same two, and a diff made only of them and docs/
|
||||
# never reaches this script's build jobs (the plan job's `code` is false).
|
||||
tooling_pattern='^cmd/(backport-batch|api-gate)/'
|
||||
|
||||
if grep -vE "$tooling_pattern" "$CHANGED" | grep -qE "$full_rebuild_pattern"; then
|
||||
emit_json "$units"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
@@ -97,6 +97,23 @@ load test_helper
|
||||
rm -f "$tmp"
|
||||
}
|
||||
|
||||
@test "the CI-only tools do not force the full matrix" {
|
||||
tmp=$(mktemp)
|
||||
# No image builds cmd/backport-batch or cmd/api-gate, so they select nothing
|
||||
# and leave the rest of the diff to its own rules.
|
||||
printf '%s\n' cmd/backport-batch/report_test.go cmd/api-gate/main.go > "$tmp"
|
||||
out=$(hack/build-matrix.sh "$tmp")
|
||||
[ "$out" = '[]' ]
|
||||
printf '%s\n' cmd/backport-batch/main.go packages/apps/mariadb/Makefile > "$tmp"
|
||||
out=$(hack/build-matrix.sh "$tmp")
|
||||
[ "$out" = '["packages/apps/mariadb"]' ]
|
||||
# Every other cmd/ directory is a shipped binary and still rebuilds all.
|
||||
printf '%s\n' cmd/backport-batch/main.go cmd/cozystack-api/main.go > "$tmp"
|
||||
out=$(hack/build-matrix.sh "$tmp")
|
||||
[ "$out" = "$(hack/build-matrix.sh)" ]
|
||||
rm -f "$tmp"
|
||||
}
|
||||
|
||||
@test "a package with no image target selects nothing" {
|
||||
tmp=$(mktemp)
|
||||
# postgres ships no in-repo image build, so it is not a build unit.
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
#!/usr/bin/env bats
|
||||
# -----------------------------------------------------------------------------
|
||||
# The tenant cluster's pod and service CIDRs are written by two charts:
|
||||
#
|
||||
# * packages/apps/kubernetes renders them into the CAPI Cluster's
|
||||
# spec.clusterNetwork, which the control plane is built from;
|
||||
# * packages/apps/kubernetes-nodes renders them into every worker's Talos
|
||||
# machine config (cluster.network.podSubnets / serviceSubnets), inside the
|
||||
# TalosConfigTemplate the talos-reconcile Job applies.
|
||||
#
|
||||
# Each chart carries its own literal, and neither render fails when the two
|
||||
# disagree: the workers boot, join, and route pod and Service traffic over
|
||||
# ranges the control plane does not use. This guard renders both charts and
|
||||
# compares the lists, so a range moved on one side only fails here.
|
||||
#
|
||||
# The worker side is read the way the Job produces it: the heredoc is cut out
|
||||
# of the Job's command, run through sh, and the TalosConfigTemplate it prints is
|
||||
# parsed, then the machine config inside it. A text grep of the Job would also
|
||||
# match a comment.
|
||||
#
|
||||
# Requires: helm, yq (mikefarah v4+). Runs under both `bats` and the in-repo
|
||||
# cozytest.sh runner, so TMP is provisioned inline per test and cleaned without
|
||||
# an EXIT trap.
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
load test_helper
|
||||
|
||||
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
|
||||
|
||||
# $1: output directory. Writes pods and services, one CIDR per line.
|
||||
cluster_cidrs() {
|
||||
helm template kubernetes-myk8s "$REPO_ROOT/packages/apps/kubernetes" \
|
||||
--namespace tenant-test \
|
||||
-f "$REPO_ROOT/packages/apps/kubernetes/tests/values-ci.yaml" \
|
||||
> "$1/cluster.yaml" 2> "$1/cluster.err" \
|
||||
|| { cat "$1/cluster.err" >&2; return 1; }
|
||||
yq 'select(.kind == "Cluster" and .apiVersion == "cluster.x-k8s.io/v1beta1") | .spec.clusterNetwork.pods.cidrBlocks[]' \
|
||||
"$1/cluster.yaml" > "$1/cluster.pods"
|
||||
yq 'select(.kind == "Cluster" and .apiVersion == "cluster.x-k8s.io/v1beta1") | .spec.clusterNetwork.services.cidrBlocks[]' \
|
||||
"$1/cluster.yaml" > "$1/cluster.services"
|
||||
}
|
||||
|
||||
# $1: output directory. Writes pool.pods and pool.services, one CIDR per line.
|
||||
pool_cidrs() {
|
||||
helm template kubernetes-nodes-myk8s-md0 "$REPO_ROOT/packages/apps/kubernetes-nodes" \
|
||||
--namespace tenant-test \
|
||||
--set cluster=myk8s \
|
||||
--set _cluster.cluster-domain=cozy.local \
|
||||
--set instanceType="" \
|
||||
--set resources.cpu=2 --set resources.memory=4Gi \
|
||||
--show-only templates/talos-reconcile-job.yaml \
|
||||
> "$1/pool.yaml" 2> "$1/pool.err" \
|
||||
|| { cat "$1/pool.err" >&2; return 1; }
|
||||
yq 'select(.kind == "Job") | .spec.template.spec.containers[0].command[2]' "$1/pool.yaml" > "$1/cmd.sh"
|
||||
awk '
|
||||
/(^|="?\$\()cat <<EOF/ { print "cat <<EOF"; inblock=1; next }
|
||||
inblock && /^EOF$/ { print "EOF"; inblock=0; next }
|
||||
inblock { print }
|
||||
' "$1/cmd.sh" > "$1/heredoc.sh"
|
||||
sh "$1/heredoc.sh" > "$1/tct.yaml" 2> "$1/heredoc.err" \
|
||||
|| { cat "$1/heredoc.err" >&2; return 1; }
|
||||
yq 'select(.kind == "TalosConfigTemplate") | .spec.template.spec.data' "$1/tct.yaml" > "$1/machineconfig.yaml"
|
||||
yq '.cluster.network.podSubnets[]' "$1/machineconfig.yaml" > "$1/pool.pods"
|
||||
yq '.cluster.network.serviceSubnets[]' "$1/machineconfig.yaml" > "$1/pool.services"
|
||||
}
|
||||
|
||||
# $1: directory, $2: pods or services, $3: what to call it in a message.
|
||||
assert_agree() {
|
||||
cluster=$(cat "$1/cluster.$2")
|
||||
pool=$(cat "$1/pool.$2")
|
||||
# An empty read means a field moved or a render came out short; comparing
|
||||
# "" with "" would pass on nothing.
|
||||
if [ -z "$cluster" ] || [ -z "$pool" ]; then
|
||||
echo "FAIL: could not read the $3 CIDRs from both renders."
|
||||
echo " kubernetes Cluster.spec.clusterNetwork: '${cluster:-<missing>}'"
|
||||
echo " kubernetes-nodes machine config cluster.network: '${pool:-<missing>}'"
|
||||
return 1
|
||||
fi
|
||||
if [ "$cluster" != "$pool" ]; then
|
||||
echo "FAIL: the tenant $3 CIDRs differ between the two charts."
|
||||
echo " packages/apps/kubernetes (CAPI Cluster): $cluster"
|
||||
echo " packages/apps/kubernetes-nodes (worker machine config): $pool"
|
||||
echo "Workers would route $3 traffic over a range the control plane does not"
|
||||
echo "use. Change both charts together. Moving the worker side also renames"
|
||||
echo "the content-hashed TalosConfigTemplate and rolls every worker."
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
@test "kubernetes and kubernetes-nodes render the same pod CIDRs" {
|
||||
TMP=$(mktemp -d)
|
||||
cluster_cidrs "$TMP"
|
||||
pool_cidrs "$TMP"
|
||||
assert_agree "$TMP" pods pod
|
||||
rm -rf "$TMP"
|
||||
}
|
||||
|
||||
@test "kubernetes and kubernetes-nodes render the same service CIDRs" {
|
||||
TMP=$(mktemp -d)
|
||||
cluster_cidrs "$TMP"
|
||||
pool_cidrs "$TMP"
|
||||
assert_agree "$TMP" services service
|
||||
rm -rf "$TMP"
|
||||
}
|
||||
@@ -78,19 +78,16 @@ CATALOG_VALUES="$REPO_ROOT/packages/system/kubernetes-worker-image/values.yaml"
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Second family of default-drift guards on the same chart: the Talos <-> Kubernetes
|
||||
# support matrix. The render guard keyed only on `talos.version`, so a pool could
|
||||
# reach a different Talos release through `osImage.builtin.version` /
|
||||
# `osImage.factory.version` and boot a kubelet outside its support window with no
|
||||
# render failure. The table now lives once in templates/_helpers.tpl and both
|
||||
# paths consult it.
|
||||
# support matrix. A pool reaches a Talos release through `talos.version` or through
|
||||
# `osImage.builtin.version` / `osImage.factory.version`, and the table in
|
||||
# templates/_helpers.tpl has to be consulted for whichever one the pool boots.
|
||||
#
|
||||
# The symmetry is structural, not observable from a render: the matrix ships one
|
||||
# row (`v1.13`) whose Kubernetes list is a superset of `values.schema.json`'s
|
||||
# `version` enum, so no schema-valid pairing can make either call site fail today.
|
||||
# A helm-unittest case would have to put an out-of-enum `version` in, and
|
||||
# helm-unittest validates the schema, so it cannot. These two guards cover what a
|
||||
# render cannot: that both call sites exist, and that the table still means
|
||||
# something for the defaults the chart ships.
|
||||
# The matrix ships one row (`v1.13`) whose Kubernetes list is a superset of
|
||||
# `values.schema.json`'s `version` enum, so no schema-valid pairing makes either
|
||||
# call site fail today. hack/kubernetes-nodes-talos-matrix.bats renders the
|
||||
# refusals with the schema skipped; these two guards cover what it does not: that
|
||||
# both call sites stay wired, and that the table still means something for the
|
||||
# defaults the chart ships.
|
||||
|
||||
NODES_TEMPLATE="$REPO_ROOT/packages/apps/kubernetes-nodes/templates/nodegroup.yaml"
|
||||
NODES_SCHEMA="$REPO_ROOT/packages/apps/kubernetes-nodes/values.schema.json"
|
||||
@@ -148,30 +145,3 @@ NODES_SCHEMA="$REPO_ROOT/packages/apps/kubernetes-nodes/values.schema.json"
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Third guard, and the premise the first one rests on. The catalog is keyed off
|
||||
# the kubernetes-nodes chart's `talos` defaults, but the parent kubernetes chart
|
||||
# carries its own copy of the same block since the Phase 2 split, and its own
|
||||
# values.yaml says to keep the two in sync. Nothing enforced that: the pool
|
||||
# chart's `make update` copies files/versions.yaml and images/kubectl.tag from
|
||||
# the parent and not the talos block, so a one-sided bump leaves the parent's
|
||||
# support-matrix guard reasoning about one Talos release while every worker boots
|
||||
# another — and the catalog check above would go on passing, because it only ever
|
||||
# reads one of the two files.
|
||||
|
||||
PARENT_VALUES="$REPO_ROOT/packages/apps/kubernetes/values.yaml"
|
||||
|
||||
@test "the parent kubernetes chart and kubernetes-nodes agree on their Talos defaults" {
|
||||
for key in version schematicID imageFactoryURL installerRepository registryMirrors; do
|
||||
parent=$(yq ".talos.${key}" "$PARENT_VALUES")
|
||||
pool=$(yq ".talos.${key}" "$APP_VALUES")
|
||||
if [ "$parent" != "$pool" ]; then
|
||||
echo "talos.${key} differs between the two charts:" >&2
|
||||
echo " packages/apps/kubernetes/values.yaml: $parent" >&2
|
||||
echo " packages/apps/kubernetes-nodes/values.yaml: $pool" >&2
|
||||
echo "both are declared 'keep in sync' and a worker pool follows the second while the parent's support-matrix guard reads the first" >&2
|
||||
return 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
+1
-1
@@ -121,7 +121,7 @@
|
||||
# the number; do not expect to find it written here.
|
||||
#
|
||||
# TWO ceilings sit outside it, and they fail differently. The job is capped at
|
||||
# `timeout-minutes: 215`, and reaching that cancels every step still to come --
|
||||
# `timeout-minutes: 265`, and reaching that cancels every step still to come --
|
||||
# the artifact upload among them. The step running this script is capped at 30 of
|
||||
# its own, and reaching THAT ends only this step: the uploads after it still run,
|
||||
# and `continue-on-error` on that step keeps the overrun out of the job's result,
|
||||
|
||||
+1
-1
@@ -208,7 +208,7 @@ _cozy_on_exit() {
|
||||
# capture would read as a complete one listing fewer crash-loops than
|
||||
# actually occurred. This handler's whole worst case is ~1350s (330 + 390
|
||||
# + 630, each leg including its 30s kill grace) against a job capped at
|
||||
# 215 minutes with no per-step timeout —
|
||||
# 265 minutes with no per-step timeout —
|
||||
# deliberate, for the same reason recorded in hack/e2e-chainsaw/.chainsaw.yaml:
|
||||
# a bounded, honest wait beats a kill that collects nothing.
|
||||
if [ "$_prev_rc" -ne 0 ]; then
|
||||
|
||||
@@ -35,8 +35,9 @@
|
||||
# a human's job, and reviewers should not assume CI did it for them.
|
||||
#
|
||||
# A docs-only PR skips builds and E2E, but the `checks` job still runs the Bats
|
||||
# lane. Otherwise editing this map, or another document audited by a unit
|
||||
# contract, would exempt the changed input from its own guard.
|
||||
# lane whenever a docs/ path changed. Otherwise editing this map, or another
|
||||
# document audited by a unit contract, would exempt the changed input from its
|
||||
# own guard.
|
||||
|
||||
load test_helper
|
||||
|
||||
@@ -117,9 +118,18 @@ map_section() {
|
||||
|
||||
[ "$job_if" = "!contains(github.event.pull_request.labels.*.name, 'release')" ]
|
||||
[ "$code_if" = "needs.plan.outputs.code == 'true'" ]
|
||||
[ "$docs_if" = "needs.plan.outputs.code == 'false'" ]
|
||||
[ "$docs_if" = "needs.plan.outputs.code == 'false' && needs.plan.outputs.docs == 'true'" ]
|
||||
[ "$docs_run" = 'make bats-unit-tests' ]
|
||||
[ "$code_run" = 'make unit-tests test-controllers test-controllers-envtest -j4 -k --output-sync=target' ]
|
||||
|
||||
# `code` is false for the CI-only tools too, so the docs lane is keyed on a
|
||||
# docs/ path being present, not on `code` alone. Pin that the output means
|
||||
# exactly that and that Bats gets installed for it.
|
||||
plan_run=$(yq -r '.jobs.plan.steps[] | select(.id == "p") | .run' "$WORKFLOW")
|
||||
printf '%s\n' "$plan_run" | grep -qF "if grep -qE '^docs/' /tmp/changed_norenames.txt; then"
|
||||
[ "$(yq -r '.jobs.plan.outputs.docs' "$WORKFLOW")" = '${{ steps.p.outputs.docs }}' ]
|
||||
toolchain_if=$(yq -r '.jobs.checks.steps[] | select(.name == "Set up test toolchain") | .if' "$WORKFLOW")
|
||||
[ "$toolchain_if" = "needs.plan.outputs.code == 'true' || needs.plan.outputs.docs == 'true'" ]
|
||||
}
|
||||
|
||||
@test "the trigger map and the PR-template checklist list the same repositories" {
|
||||
|
||||
@@ -66,14 +66,14 @@ spec:
|
||||
# script: it SIGKILLs the shell alone, orphaned children hold the pipe open,
|
||||
# and the op overruns (#4268 measured 606s under a 300s ceiling), so the
|
||||
# nominal ~5m was a figure a busy catch could exceed, not a cap. Tests run
|
||||
# sequentially, and the E2E job is capped at 215 timeout-minutes. A cascade
|
||||
# sequentially, and the E2E job is capped at 265 timeout-minutes. A cascade
|
||||
# reddening many suites can therefore spend a large share of the job in
|
||||
# diagnostics. That is deliberate — the old ceiling
|
||||
# bought its speed by collecting nothing at all — and it is bounded: both legs
|
||||
# only approach their ceilings on a genuinely degraded cluster, a systemic
|
||||
# failure usually reds the install gate and skips the E2E step entirely, and
|
||||
# the report-upload steps are `if: always()`. That last point does NOT extend
|
||||
# to a job timeout: a 215-minute cap ends the job `cancelled`, and the ~4m48s
|
||||
# to a job timeout: a 265-minute cap ends the job `cancelled`, and the ~4m48s
|
||||
# grace window is consumed by the earlier `Collect report` step, so both
|
||||
# cozyreport.tgz and the job log are lost (measured on runs 29557003429 and
|
||||
# 29568562337). If this ever does start truncating runs, bound the number of
|
||||
|
||||
@@ -18,7 +18,7 @@ cozy_e2e_storage_class() {
|
||||
esac
|
||||
}
|
||||
|
||||
# talos_spec_block: emit the tenant Kubernetes CR `spec.talos` block with the Talos
|
||||
# talos_spec_block: emit the tenant KubernetesNodes CR `spec.talos` block with the Talos
|
||||
# OS image cache (imageFactoryURL) when the in-sandbox cache is up. Prints nothing
|
||||
# when it falls back to the public default, so the chart default applies.
|
||||
# Indented for insertion directly under `spec:` in the heredoc below.
|
||||
@@ -5702,8 +5702,9 @@ YAML
|
||||
|
||||
# Point worker DataVolume imports at the in-sandbox Talos OS image cache when it
|
||||
# is up, falling back to the public factory otherwise. Worker image pulls use
|
||||
# the chart's public-registry default. Emitted right under spec: as
|
||||
# `talos: { imageFactoryURL: ... }`, or nothing when the default applies.
|
||||
# the chart's public-registry default. Emitted right under spec: of the
|
||||
# KubernetesNodes pool below as `talos: { imageFactoryURL: ... }`, or nothing
|
||||
# when the default applies. The Kubernetes CR takes no worker image field.
|
||||
local talos_block
|
||||
talos_block=$(talos_spec_block)
|
||||
|
||||
@@ -5714,7 +5715,6 @@ metadata:
|
||||
name: "${test_name}"
|
||||
namespace: tenant-test
|
||||
spec:
|
||||
${talos_block}
|
||||
${oidc_block}
|
||||
addons:
|
||||
certManager:
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
# and the teardown. This is the third suite in a full run that brings up a
|
||||
# tenant cluster, next to kubernetes-latest and kubernetes-previous. A failure
|
||||
# that stalls worker join everywhere reddens all three, and their red paths
|
||||
# together can exceed the E2E job's 215-minute cap, which cancels the job and
|
||||
# together can exceed the E2E job's 265-minute cap, which cancels the job and
|
||||
# loses its log and report. The cap is sized for the red path of the
|
||||
# kubernetes pair only; this suite is not counted in it, and that is an
|
||||
# accepted risk.
|
||||
|
||||
@@ -80,7 +80,8 @@ spec:
|
||||
# wait, all on the passing path. Raising the ceiling to cover them is not
|
||||
# available -- the guard in hack/run-kubernetes-serial-console_test.bats
|
||||
# holds COZY_OP_CEILING equal to BOTH suites' `timeout:`, and two suites
|
||||
# at 79m plus their 22m teardowns exceed the job's own 215-minute cap.
|
||||
# at 79m plus their 22m teardowns take 202m, which with the install and
|
||||
# the rest of a full run does not fit the job's own 265-minute cap.
|
||||
# What is available is a measurement: on the green run of this branch
|
||||
# (33724179053) the whole Test took 569.72s against this 4020s op, with
|
||||
# kubernetes-previous, which carries no OIDC fold, at 470.19s. The
|
||||
|
||||
@@ -49,10 +49,10 @@
|
||||
# Best-effort: never fail the suite on the band-aid. Remove once tenant workers
|
||||
# no longer bulk-pull the OS image from the public internet in CI.
|
||||
local sid ver
|
||||
sid=$(yq '.talos.schematicID' packages/apps/kubernetes/values.yaml 2>/dev/null)
|
||||
ver=$(yq '.talos.version' packages/apps/kubernetes/values.yaml 2>/dev/null)
|
||||
sid=$(yq '.talos.schematicID' packages/apps/kubernetes-nodes/values.yaml 2>/dev/null)
|
||||
ver=$(yq '.talos.version' packages/apps/kubernetes-nodes/values.yaml 2>/dev/null)
|
||||
if [ -z "$sid" ] || [ "$sid" = "null" ] || [ -z "$ver" ] || [ "$ver" = "null" ]; then
|
||||
echo "WARNING: could not read talos.schematicID/version from values.yaml — skipping mirror (fallback to public factory)"
|
||||
echo "WARNING: could not read talos.schematicID/version from packages/apps/kubernetes-nodes/values.yaml — skipping mirror (fallback to public factory)"
|
||||
return 0
|
||||
fi
|
||||
# The CiliumClusterwideNetworkPolicy in the manifest is intentionally NOT applied
|
||||
@@ -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
|
||||
}
|
||||
@@ -14,7 +14,7 @@
|
||||
# This Deployment fetches the image ONCE at e2e-install time with an aggressive
|
||||
# curl retry loop (decoupling the one flaky external fetch from the many
|
||||
# per-worker imports), then serves it locally over plain HTTP with range support.
|
||||
# Tenant `Kubernetes` CRs point `spec.talos.imageFactoryURL` at this Service
|
||||
# Tenant `KubernetesNodes` CRs point `spec.talos.imageFactoryURL` at this Service
|
||||
# (see hack/e2e-chainsaw/_lib/run-kubernetes.sh) so every worker imports from the
|
||||
# warm local copy instead of the public internet. Tenant namespaces run under a
|
||||
# default-deny Cilium egress installed by the tenant chart, whose allow rules cover
|
||||
@@ -28,7 +28,7 @@
|
||||
# mirror can only help, never make CI worse.
|
||||
#
|
||||
# The __SCHEMATIC_ID__ / __TALOS_VERSION__ placeholders are substituted at apply
|
||||
# time by hack/e2e-install-cozystack.bats from packages/apps/kubernetes/values.yaml.
|
||||
# time by hack/e2e-install-cozystack.bats from packages/apps/kubernetes-nodes/values.yaml.
|
||||
#
|
||||
# CI band-aid, not a product artifact. The durable fix is the overridable
|
||||
# `talos.imageFactoryURL` chart value this manifest exercises; a self-hosted
|
||||
|
||||
@@ -266,24 +266,56 @@ resolve_silent_conclusions() {
|
||||
plan_block="$(job_block plan "$PULL_REQUESTS" | code_lines)"
|
||||
[ -n "$plan_block" ]
|
||||
|
||||
count="$(printf '%s\n' "$plan_block" | grep -c "grep -qvE '\^docs/' /tmp/changed_norenames.txt" || true)"
|
||||
count="$(printf '%s\n' "$plan_block" | grep -c "grep -qvE '\^(.*)' /tmp/changed_norenames.txt" || true)"
|
||||
[ "${count:-0}" -eq 1 ]
|
||||
|
||||
# The rename-detected list may still feed the build matrix, but never this.
|
||||
count="$(printf '%s\n' "$plan_block" | grep -c "grep -qvE '\^docs/' /tmp/changed.txt" || true)"
|
||||
count="$(printf '%s\n' "$plan_block" | grep -c "grep -qvE '\^(.*)' /tmp/changed.txt" || true)"
|
||||
[ "${count:-0}" -eq 0 ]
|
||||
|
||||
# The fork half of the same agreement: both sides of a rename are inspected.
|
||||
# Pinned on the docs-only expression itself — `previous_filename` also appears
|
||||
# Pinned on the skip expression itself — `previous_filename` also appears
|
||||
# in the TIA file list a few lines down, so counting the identifier would stay
|
||||
# satisfied by that one with this check deleted.
|
||||
block="$(job_block resolve "$FORK" | code_lines)"
|
||||
[ -n "$block" ]
|
||||
count="$(printf '%s\n' "$block" \
|
||||
| grep -c 'f\.previous_filename && !isDocs(f\.previous_filename)' || true)"
|
||||
| grep -c 'f\.previous_filename && !isNoRuntime(f\.previous_filename)' || true)"
|
||||
[ "${count:-0}" -eq 1 ]
|
||||
}
|
||||
|
||||
@test "both lanes skip e2e for the same path prefixes" {
|
||||
# The rename test above pins HOW each lane reads its list; this one pins
|
||||
# WHAT it skips. A prefix added to one lane only is a fork PR that one lane
|
||||
# reads as nothing-to-build and the other as needing the pr-patch artifact,
|
||||
# which nothing will ever produce.
|
||||
plan_prefixes="$(job_block plan "$PULL_REQUESTS" | code_lines \
|
||||
| sed -nE "s,.*grep -qvE '\^\(([^)]*)\)' /tmp/changed_norenames\.txt.*,\1,p" \
|
||||
| tr '|' '\n' | sort)"
|
||||
fork_prefixes="$(job_block resolve "$FORK" | code_lines \
|
||||
| sed -nE "s,.*const noRuntimePrefixes = \[([^]]*)\];.*,\1,p" \
|
||||
| tr ',' '\n' | tr -d " '" | sort)"
|
||||
[ -n "$plan_prefixes" ]
|
||||
printf '%s\n' "$plan_prefixes" | grep -qx 'docs/'
|
||||
if [ "$plan_prefixes" != "$fork_prefixes" ]; then
|
||||
echo "plan skips:" >&2; printf ' %s\n' $plan_prefixes >&2
|
||||
echo "fork skips:" >&2; printf ' %s\n' $fork_prefixes >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# The tools in that list must also be the ones the build matrix and the suite
|
||||
# selector leave alone in a mixed diff; otherwise a tool skipped on its own
|
||||
# still rebuilds everything beside a one-line package change.
|
||||
tools="$(printf '%s\n' "$plan_prefixes" | grep -vx 'docs/')"
|
||||
[ -n "$tools" ]
|
||||
matrix_tools="$(sed -nE "s,^tooling_pattern='\^cmd/\(([^)]*)\)/'$,\1,p" "$REPO_ROOT/hack/build-matrix.sh" \
|
||||
| tr '|' '\n' | sed 's,^,cmd/,; s,$,/,' | sort)"
|
||||
select_tools="$(sed -nE 's,^ (cmd/[^)]*)\)$,\1,p' "$REPO_ROOT/hack/select-e2e.sh" \
|
||||
| tr '|' '\n' | sed 's,\*$,,' | sort)"
|
||||
[ "$tools" = "$matrix_tools" ]
|
||||
[ "$tools" = "$select_tools" ]
|
||||
}
|
||||
|
||||
@test "same-repo gate: every publishing guard names the same label set" {
|
||||
# `plan`, `verify-release-candidate`, `e2e-report` and the concurrency key
|
||||
# each decide from the label name whether this run owns the status. Renaming
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env bats
|
||||
# Unit test: the refusals of the Talos to Kubernetes support-matrix guard in
|
||||
# packages/apps/kubernetes-nodes (kubernetes-nodes.assertTalosSupportsKubernetes,
|
||||
# called from templates/nodegroup.yaml for talos.version and again for the
|
||||
# release osImage resolves to).
|
||||
#
|
||||
# On the shipped chart a refusal is unreachable: the values schema offers only
|
||||
# Kubernetes versions the v1.13 row lists, and files/versions.yaml, which another
|
||||
# template fails on first, holds the same set. The guard is there for the day a
|
||||
# Talos row stops listing a version the chart still offers. So this renders a
|
||||
# copy of the chart with --skip-schema-validation and one extra versions.yaml row
|
||||
# for a version the matrix does not list. The pairings the guard lets through
|
||||
# are in the chart's own tests/talos_k8s_support_matrix_test.yaml.
|
||||
|
||||
load test_helper
|
||||
|
||||
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
|
||||
CHART="$REPO_ROOT/packages/apps/kubernetes-nodes"
|
||||
|
||||
# render_pool <args...> renders the pool chart copy with the given --set flags and
|
||||
# prints helm's output, errors included. The copy lives in the test's own tmpdir.
|
||||
render_pool() {
|
||||
copy="$BATS_TEST_TMPDIR/chart"
|
||||
if [ ! -d "$copy" ]; then
|
||||
cp -RL "$CHART" "$copy"
|
||||
printf '"v1.30": "v1.30.14"\n' >> "$copy/files/versions.yaml"
|
||||
fi
|
||||
helm template kubernetes-nodes-myk8s-md0 "$copy" --namespace tenant-test \
|
||||
--skip-schema-validation \
|
||||
--set cluster=myk8s \
|
||||
--set _cluster.cluster-domain=cozy.local \
|
||||
--set minReplicas=1 --set maxReplicas=3 \
|
||||
--set instanceType= --set resources.cpu=2 --set resources.memory=4Gi \
|
||||
--set diskSize=20Gi --set storageClass=replicated \
|
||||
"$@" 2>&1
|
||||
}
|
||||
|
||||
@test "an in-window pairing renders on the same copy, so the refusals below are the guard's" {
|
||||
out=$(render_pool --set version=v1.31) || { echo "$out" >&2; return 1; }
|
||||
printf '%s\n' "$out" | grep -q '^kind: KubevirtMachineTemplate$'
|
||||
}
|
||||
|
||||
@test "a Kubernetes version outside the row of talos.version is refused" {
|
||||
if out=$(render_pool --set version=v1.30); then
|
||||
echo "render succeeded with Kubernetes v1.30 on the default Talos" >&2
|
||||
return 1
|
||||
fi
|
||||
printf '%s\n' "$out" | grep -qF "Kubernetes v1.30 is not supported by Talos v1.13.6. Supported versions: [v1.31 v1.32 v1.33 v1.34 v1.35 v1.36]. Bump 'version' or set talos.version explicitly." \
|
||||
|| { echo "$out" >&2; return 1; }
|
||||
}
|
||||
|
||||
@test "an older patch release of a listed Talos minor is checked against the same row" {
|
||||
if out=$(render_pool --set version=v1.30 --set talos.version=v1.13.0); then
|
||||
echo "render succeeded with Kubernetes v1.30 on Talos v1.13.0: the guard is not keyed on the minor" >&2
|
||||
return 1
|
||||
fi
|
||||
printf '%s\n' "$out" | grep -qF 'Kubernetes v1.30 is not supported by Talos v1.13.0.' \
|
||||
|| { echo "$out" >&2; return 1; }
|
||||
}
|
||||
|
||||
# talos.version names a minor the matrix does not list, so the first call lets the
|
||||
# pool through and only the call fed the osImage release can refuse it.
|
||||
@test "a Kubernetes version outside the row of osImage.factory.version is refused" {
|
||||
if out=$(render_pool --set version=v1.30 --set talos.version=v1.12.0 --set osImage.factory.version=v1.13.0); then
|
||||
echo "render succeeded with Kubernetes v1.30 on Talos v1.13.0 reached through osImage.factory" >&2
|
||||
return 1
|
||||
fi
|
||||
printf '%s\n' "$out" | grep -qF 'Kubernetes v1.30 is not supported by Talos v1.13.0.' \
|
||||
&& printf '%s\n' "$out" | grep -qF 'nodeGroup md0 reaches this Talos release through its osImage override' \
|
||||
|| { echo "$out" >&2; return 1; }
|
||||
}
|
||||
|
||||
@test "a Kubernetes version outside the row of osImage.builtin.version is refused" {
|
||||
if out=$(render_pool --set version=v1.30 --set talos.version=v1.12.0 --set osImage.builtin.version=v1.13.0); then
|
||||
echo "render succeeded with Kubernetes v1.30 on Talos v1.13.0 reached through osImage.builtin" >&2
|
||||
return 1
|
||||
fi
|
||||
printf '%s\n' "$out" | grep -qF 'Kubernetes v1.30 is not supported by Talos v1.13.0.' \
|
||||
&& printf '%s\n' "$out" | grep -qF 'nodeGroup md0 reaches this Talos release through its osImage override' \
|
||||
|| { echo "$out" >&2; return 1; }
|
||||
}
|
||||
@@ -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 ]
|
||||
}
|
||||
Loaded 100 of 448 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user