Previously I wrote about cty[^1] that can be used to generate valid YAML samples from a CRD ( now, including things like, valid Pattern generated random string, enums, default values, minimum int, minimum number of items.. etc ).
It also has a website that can be used to achieve the same thing here: https://crdtoyaml.com/
This update https://github.com/Skarlso/crd-to-sample-yaml/releases/tag/v... brings a feature with it, which I think is super cool. If not, well, then no. :)
Basically, inspired by `helm unittest` the user can define a test like this:
suite: test aws clusters infrastructure crds
template: sample-tests/crds/infrastructure.cluster.x-k8s.io_awsclusters.yaml
tests:
- it: matches AWSCluster crds correctly
asserts:
- matchSnapshot:
# this will generate one snapshot / CRD version and match all of them to the right version of the CRD
path: sample-tests/__snapshots__
- matchSnapshot:
path: sample-tests/__snapshots__
# generates a yaml file
minimal: true
- matchString:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
kind: AWSCluster
spec:
controlPlaneEndpoint:
host: string
port: 1
- matchString:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
kind: AWSCluster
spec:
controlPlaneEndpoint:
# this is failing the test because the type is number and not string
host: 12345
port: 1
This will take a CRD and test the CRD validates a given sample. In this case, it's a string and two snapshot values. The snapshots can be generated using `cty test sample-tests --update`.The snapshots will contain valid values, such as minimum integer, minimum number of items, defaults, enums, Patterns ( yes it generates random value that will satisfy a given pattern regex ) and more.
It will only ever match the right version with the right version. Meaning it won't try `v1beta2` to match to `v1alpha1`. Since CRD files can have multiple versions in them.
It can be added as a simple make target like helm unittest.
This is very much an alpha feature, so please, if you do use it, report any problems. :)
Thank you! Enjoy.
No comments yet.