[Feature] CUE tests for X-Definitions, run with vela def test
Is there an existing issue for this?
- I have searched the existing issues
What problem or limitation does this address?
An X-Definition is code, but there is no way to test one on its own. Today a definition author can:
vela def vetit, which checks that the CUE compiles, not that it does the right thing;vela def renderit, then eyeball the output, with nothing to fail when it changes;- deploy it to a cluster and watch, which is slow, needs a real controller, and cannot reach the branches that only trigger on unusual input or state.
So definitions regress silently. Status logic (healthPolicy, customStatus) is effectively never exercised before it reaches a cluster; workflow steps that call providers (vela/kube, vela/http, vela/config) cannot be run without real side effects; and nothing tells an author which branch of a template a parameter change broke.
To see what this costs, we wrote tests for the definitions KubeVela ships: 77 files, about 560 cases. They turned up around 40 defects that no existing check catches, among them probes accepted as parameters but never rendered, status templates that fail under CUE 0.14, a step that ignores its own required field, parameters with two conflicting defaults, and a hostPath volume the API server silently turns into an emptyDir.
Describe the solution you'd like
Tests written in CUE, in a *_test.cue file beside the definition, and run with a new vela def test command:
import "vela/test"
"renders a Deployment selecting its component": test.#ComponentRender & {
definition: "webservice"
context: {name: "web", appName: "shop"}
parameter: image: "shop:1.0"
expect: output: spec: selector: matchLabels: "app.oam.dev/component": "web"
}Each case names a function from a vela/test package, gives it inputs, and states what it expects. Expectations match by CUE subsumption, so a case states only what it cares about; @exact(), @not() and @contains() refine a field.
One function per way the controller evaluates a definition, each going through the controller's own code rather than a copy:
| Function | Covers |
|---|---|
#ComponentRender, #TraitRender |
template output and outputs, parameter and template errors |
#ComponentStatus, #TraitStatus |
health, message and details against given live objects |
#AddonRender |
a component from a local addon, prepared as enabling it would |
#PolicyRender, #ApplicationPolicyRender |
workload-bearing and Application-scoped policies |
#WorkflowStepExec |
a step run through the workflow engine against a local envtest API server |
#SourceExec |
a SourceDefinition resolved through the source engine, including its cache key and TTL |
For the cases that run against a cluster:
- Mocks. Provider calls are recorded and can be asserted; any can be mocked, and calls that leave the cluster (
vela/http,vela/registry) must be. - State.
resources:lists what exists before a case, andexpect: resources:what should exist after. Provider calls marked@before(),@beforeEach(),@after()or@afterEach()set up and tear down, with Ginkgo's semantics, and whatever a case creates is cleaned up when its scope ends. - Offline. Nothing reads the kubeconfig's cluster; a test run never touches a real one.
Beyond the CLI:
- The same cases run from Go, through a Ginkgo helper, so a repo of definitions can run them in its own test suite.
- Shipped definitions carry their own tests, and CI fails when a shipped definition has none, unless it is listed as untested with a reason.
- A case marked
@pending(reason)pins a known defect: it is reported, not run, and starts passing once the defect is fixed, so a bug report and its regression test are the same thing.
Alternatives considered
- Golden files from
vela def render. Cheap, but a snapshot asserts everything, so every unrelated change churns it, and it cannot cover status, steps or sources, which need live state or provider calls. - Go tests per definition. Works for definitions authored in Go through defkit, but most definitions, including every one KubeVela ships, are CUE, and their authors should not need Go to test them.
- End-to-end tests only. Already exist for a few definitions. They prove wiring, not a definition's branches, and are too slow to write one per case.
- Separate hook functions (
test.#Beforeetc.) instead of attributes. Annotating ordinary provider calls with@before()and the rest reuses functions authors already know, and keeps setup in the same language as the calls under test.
Additional context
Implemented in three stacked PRs, to be merged in order:
- #7400: the controller changes the harness needs, usable on their own: rendering a single policy or Application-scoped policy, a source's cache policy, typed validation errors, and
*_test.cueskipped wherever definitions are read from a directory. - #7401: the
vela/testpackage, thevela def testcommand and the Ginkgo helper. - #7402: tests for the shipped component, trait, workflow step and source definitions, with the defects they found marked
@pending.
Docs for vela def test and vela/test will follow in kubevela.github.io.
Source: kubevela/kubevela