mirror of
https://github.com/dsx-ai-factory/nvke.git
synced 2026-12-07 04:35:58 +08:00
* feat(api): gate openapi.yaml on backward compatibility `generate-check` proves the committed *.gen.go match api/openapi.yaml. It says nothing about whether the specification change itself is safe. A removed field, a narrowed type or a newly required property regenerates perfectly -- the generated code matches the new spec exactly -- and breaks every client already built against the old one. Regeneration correctness and compatibility are different questions, and only the first was being asked. Adds `make -C controller api-compat-check`, a member of the merge gate. It compares the working-tree specification against the newest published controller/api version tag and fails on any breaking change that is not waived. The baseline is the tag, not the merge base. controller-api-tag.yml publishes controller/api/vX.Y.Z on every main commit that touches the directory, and that tag is what a consumer pins. It is also only ever a patch bump, so no tag has ever signalled a break -- which is why the gate has to hold the contract that the version number does not. Breaking changes are waived one at a time in controller/api/openapi-compat-waivers.yaml, each with a reason, a named person and a review date, and a waiver that matches nothing fails the gate. That is the rule GOVULNCHECK_WAIVED is already held to: an entry cannot outlive its reason. Waivers match on change kind, JSONPath and property rather than on the engine's changeHash, which covers the shape of a change and not its location -- in one sample diff 89 of 91 breaking changes shared five hashes. It fails closed throughout. A missing baseline (a shallow checkout has no tags), an unparsable specification, an empty or unrecognized engine report, and a missing waiver file are all errors. A gate that could not run has not found the change compatible, and the workflow checks out with fetch-depth: 0 so the usual cause cannot arise silently. The change engine is pb33f/openapi-changes, pinned as OPENAPI_CHANGES_VERSION and run with `go run` so nothing is installed. It is from the same author as vacuum, over the same libopenapi parser already lints this specification, so the gate and the linter agree on what the document says. Its breaking/additive classification is used as-is; what this repository owns is what happens next. Documents the versioning and deprecation behaviour in controller/api/README.md: there is one unversioned /api surface, the Go module tag is the pinnable artifact, and deprecation is additive while removal is the breaking step that needs a waiver. Also separates the authored files in that directory from the generated ones. Signed-off-by: Terry Howe <thowe@nvidia.com> * feat(api): let a sunset deprecation discharge a removal The gate landed with one route past it: every breaking change is waived, including a removal. That makes the patient route -- announce it, wait, then remove -- cost a reviewer exactly what the abrupt one costs, which is the wrong incentive for the one operation a compatibility gate exists to encourage. So removal gets a second route. An operation marked `deprecated: true`, carrying an `x-sunset` date, whose date has passed, may now be removed with no waiver at all: the promise was made, kept and discharged, and there is nothing left for anyone to sign. Removing it early, or without having deprecated it, is unchanged -- still breaking, still waivable. The price of offering that route is that the deprecation has to be real, which is what the new rules enforce. `deprecated: true` with no `x-sunset`, an `x-sunset` that is not a YYYY-MM-DD date, a date less than 180 days out, and an `x-sunset` on an operation that is not marked deprecated all fail. Each is a deprecation that announces nothing a consumer can act on. Every rule is scoped to what changed, never to the state of the document. An operation whose deprecation fields are identical on both sides is not examined at all, however they are set. A state rule would fail a pull request that never touched the specification the moment some older deprecation drifted out of compliance, and a gate that fails unrelated work is a gate someone turns off. The findings are plain `change` values, so everything already built for the engine's findings works on them: the waiver file matches them the same way, the failure message prints the same pasteable stub, and a waiver for one goes stale the same way. They are deliberately not emitted for removals, which the engine already reports -- two findings for one edit would mean two waivers for one decision. Discharge covers operations only. The engine reports a removed operation as a single change naming it, which is what makes "this exact removal was announced" decidable; a removed property or parameter is reported in a shape where it is not. The two removal shapes were confirmed against the engine rather than assumed. --report now resolves both specifications too. It replaces the engine, not the documents, and a mode that skipped them would be a mode in which a removal looked sanctioned because nothing had checked. Signed-off-by: Terry Howe <thowe@nvidia.com> --------- Signed-off-by: Terry Howe <thowe@nvidia.com>
246 lines
8.8 KiB
Go
246 lines
8.8 KiB
Go
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// now is fixed, so a test cannot start passing or failing because of the date it
|
|
// is run on. Every sunset below is expressed relative to it.
|
|
var now = time.Date(2026, 6, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
// spec builds a minimal document with one path and the operation fields under
|
|
// test. Only `paths` is parsed, so the rest of an OpenAPI document would be
|
|
// noise here.
|
|
const testPath = "/a"
|
|
|
|
func spec(ops map[string]string) []byte {
|
|
out := "openapi: 3.0.3\npaths:\n " + testPath + ":\n"
|
|
for method, body := range ops {
|
|
out += " " + method + ":\n operationId: op\n"
|
|
if body != "" {
|
|
out += body
|
|
}
|
|
}
|
|
return []byte(out)
|
|
}
|
|
|
|
func deprecated(sunset string) string {
|
|
if sunset == "" {
|
|
return " deprecated: true\n"
|
|
}
|
|
return fmt.Sprintf(" deprecated: true\n x-sunset: %q\n", sunset)
|
|
}
|
|
|
|
// The rules are scoped to what changed, not to the state of the document. A
|
|
// pull request that does not touch the specification must not fail because some
|
|
// deprecation that predates it is out of compliance -- a gate that fails work
|
|
// unrelated to it is a gate someone switches off.
|
|
func TestUnchangedDeprecationsAreNotReExamined(t *testing.T) {
|
|
both := spec(map[string]string{"get": deprecated("")})
|
|
|
|
result, err := evaluateLifecycle(both, both, now, 180)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, result.Findings, "a pre-existing non-compliant deprecation is not this change's problem")
|
|
}
|
|
|
|
func TestDeprecationMustBeReal(t *testing.T) {
|
|
base := spec(map[string]string{"get": ""})
|
|
|
|
tests := []struct {
|
|
name string
|
|
revision string
|
|
want string
|
|
}{
|
|
{
|
|
name: "deprecated with no sunset announces nothing actionable",
|
|
revision: deprecated(""),
|
|
want: changeDeprecatedWithoutSunset,
|
|
},
|
|
{
|
|
name: "a sunset without deprecated reaches no client",
|
|
revision: " x-sunset: \"2027-06-01\"\n",
|
|
want: changeSunsetWithoutDeprecated,
|
|
},
|
|
{
|
|
name: "a sunset that is not a date cannot be honored",
|
|
revision: deprecated("next spring"),
|
|
want: changeSunsetNotADate,
|
|
},
|
|
{
|
|
name: "a fortnight is not notice",
|
|
revision: deprecated("2026-06-14"),
|
|
want: changeSunsetTooSoon,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := evaluateLifecycle(base, spec(map[string]string{"get": tt.revision}), now, 180)
|
|
require.NoError(t, err)
|
|
require.Len(t, result.Findings, 1)
|
|
assert.Equal(t, tt.want, result.Findings[0].ChangeText)
|
|
assert.True(t, result.Findings[0].Breaking, "a finding that is not breaking would not fail the gate")
|
|
assert.Equal(t, "$.paths['/a'].get", result.Findings[0].Path)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAFullNoticePeriodIsAccepted(t *testing.T) {
|
|
base := spec(map[string]string{"get": ""})
|
|
revision := spec(map[string]string{"get": deprecated("2026-12-01")}) // 183 days
|
|
|
|
result, err := evaluateLifecycle(base, revision, now, 180)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, result.Findings)
|
|
}
|
|
|
|
// The point of the whole file: a removal that followed the process costs no
|
|
// waiver. Without this, deprecating first and removing abruptly cost a reviewer
|
|
// exactly the same, and the patient route has nothing to recommend it.
|
|
func TestARemovalPastItsSunsetNeedsNoWaiver(t *testing.T) {
|
|
base := spec(map[string]string{"get": deprecated("2026-01-01")})
|
|
revision := []byte("openapi: 3.0.3\npaths: {}\n")
|
|
|
|
result, err := evaluateLifecycle(base, revision, now, 180)
|
|
require.NoError(t, err)
|
|
assert.True(t, result.SanctionedOps["/a GET"])
|
|
assert.True(t, result.SanctionedPaths["/a"], "every operation of the path went, so the path item went")
|
|
|
|
kept, discharged := applyLifecycle([]change{
|
|
{Breaking: true, ChangeText: "object_removed", Path: "$.paths", Property: "/a"},
|
|
}, result)
|
|
assert.Empty(t, kept)
|
|
require.Len(t, discharged, 1)
|
|
}
|
|
|
|
func TestARemovalBeforeItsSunsetStillFails(t *testing.T) {
|
|
base := spec(map[string]string{"get": deprecated("2027-01-01")})
|
|
revision := []byte("openapi: 3.0.3\npaths: {}\n")
|
|
|
|
result, err := evaluateLifecycle(base, revision, now, 180)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, result.SanctionedOps)
|
|
|
|
removal := change{Breaking: true, ChangeText: "object_removed", Path: "$.paths", Property: "/a"}
|
|
kept, discharged := applyLifecycle([]change{removal}, result)
|
|
assert.Equal(t, []change{removal}, kept, "it is still breaking, and still waivable")
|
|
assert.Empty(t, discharged)
|
|
}
|
|
|
|
func TestARemovalThatWasNeverDeprecatedStillFails(t *testing.T) {
|
|
base := spec(map[string]string{"get": ""})
|
|
revision := []byte("openapi: 3.0.3\npaths: {}\n")
|
|
|
|
result, err := evaluateLifecycle(base, revision, now, 180)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, result.SanctionedOps)
|
|
}
|
|
|
|
// A path that loses one of its operations is reported differently from a path
|
|
// that disappears whole, so the two are discharged differently. Getting this
|
|
// wrong in the permissive direction would sanction the surviving operations too.
|
|
func TestOneOperationOfAPathIsDischargedOnItsOwn(t *testing.T) {
|
|
base := spec(map[string]string{"get": deprecated("2026-01-01"), "post": ""})
|
|
revision := spec(map[string]string{"post": ""})
|
|
|
|
result, err := evaluateLifecycle(base, revision, now, 180)
|
|
require.NoError(t, err)
|
|
assert.True(t, result.SanctionedOps["/a GET"])
|
|
assert.False(t, result.SanctionedPaths["/a"], "the path survives, so removing it whole is not sanctioned")
|
|
|
|
kept, discharged := applyLifecycle([]change{
|
|
{Breaking: true, ChangeText: "property_removed", Path: "$.paths['/a']", Property: "get"},
|
|
{Breaking: true, ChangeText: "object_removed", Path: "$.paths", Property: "/a"},
|
|
}, result)
|
|
require.Len(t, discharged, 1)
|
|
assert.Equal(t, "property_removed", discharged[0].ChangeText)
|
|
require.Len(t, kept, 1)
|
|
assert.Equal(t, "object_removed", kept[0].ChangeText)
|
|
}
|
|
|
|
// Discharge is for removals and nothing else. A narrowed type inside an
|
|
// operation whose sunset happens to have passed is a separate break.
|
|
func TestDischargeDoesNotReachOtherChangesUnderTheSamePath(t *testing.T) {
|
|
result := lifecycleResult{
|
|
SanctionedOps: map[string]bool{"/a GET": true},
|
|
SanctionedPaths: map[string]bool{"/a": true},
|
|
}
|
|
other := change{Breaking: true, ChangeText: "property_removed", Path: "$.paths['/a'].get.responses", Property: "200"}
|
|
|
|
kept, discharged := applyLifecycle([]change{other}, result)
|
|
assert.Equal(t, []change{other}, kept)
|
|
assert.Empty(t, discharged)
|
|
}
|
|
|
|
// Additive changes are carried through untouched: discharge decides which
|
|
// breaking changes stop counting, and must not quietly drop anything else.
|
|
func TestAdditiveChangesAreLeftAlone(t *testing.T) {
|
|
additive := change{ChangeText: "object_added", Path: "$.paths", Property: "/b"}
|
|
|
|
kept, discharged := applyLifecycle([]change{additive}, lifecycleResult{})
|
|
assert.Equal(t, []change{additive}, kept)
|
|
assert.Empty(t, discharged)
|
|
}
|
|
|
|
// A path item holds keys that are not operations. Decoding `parameters`, which
|
|
// is a sequence, as an operation fails -- and this specification has them.
|
|
func TestNonOperationKeysOfAPathItemAreSkipped(t *testing.T) {
|
|
doc := []byte("openapi: 3.0.3\npaths:\n /a:\n summary: a path\n parameters:\n - name: id\n in: query\n get:\n deprecated: true\n")
|
|
|
|
ops, err := parseOperations(doc, "test")
|
|
require.NoError(t, err)
|
|
assert.Len(t, ops, 1)
|
|
assert.True(t, ops["/a GET"].Deprecated)
|
|
}
|
|
|
|
func TestPathFromJSONPath(t *testing.T) {
|
|
tests := []struct {
|
|
in string
|
|
want string
|
|
wantOK bool
|
|
}{
|
|
{in: "$.paths['/clusters']", want: "/clusters", wantOK: true},
|
|
{in: "$.paths['/clusters/{clusterID}']", want: "/clusters/{clusterID}", wantOK: true},
|
|
{in: "$.paths['/clusters'].get", wantOK: false},
|
|
{in: "$.paths", wantOK: false},
|
|
{in: "$.components.schemas['Cluster']", wantOK: false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.in, func(t *testing.T) {
|
|
got, ok := pathFromJSONPath(tt.in)
|
|
assert.Equal(t, tt.wantOK, ok)
|
|
if tt.wantOK {
|
|
assert.Equal(t, tt.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The real specification has to parse, or every rule here is theory.
|
|
func TestTheShippedSpecificationParses(t *testing.T) {
|
|
raw, err := os.ReadFile("../../api/openapi.yaml")
|
|
require.NoError(t, err)
|
|
|
|
ops, err := parseOperations(raw, "working-tree")
|
|
require.NoError(t, err)
|
|
assert.NotEmpty(t, ops)
|
|
|
|
// A named operation, so the assertion proves the paths were really walked
|
|
// rather than that some non-empty map came back. /clusters GET is the one
|
|
// operation this API cannot lose while remaining this API -- an endpoint
|
|
// picked for being interesting instead goes away on its own schedule and
|
|
// takes this test with it, which is how the previous sentinel
|
|
// (/cniPlugins GET) broke when #1241 retired the CNI plugin catalog.
|
|
assert.Contains(t, ops, "/clusters GET")
|
|
}
|