Hello everyone!<p>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 ).<p>It also has a website that can be used to achieve the same thing here: <a href="https://crdtoyaml.com/" rel="nofollow">https://crdtoyaml.com/</a><p>This update <a href="https://github.com/Skarlso/crd-to-sample-yaml/releases/tag/v0.8.0">https://github.com/Skarlso/crd-to-sample-yaml/releases/tag/v...</a> brings a feature with it, which I think is super cool. If not, well, then no. :)<p>Basically, inspired by `helm unittest` the user can define a test like this:<p><pre><code> 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
</code></pre>
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`.<p>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.<p>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.<p>It can be added as a simple make target like helm unittest.<p>This is very much an alpha feature, so please, if you do use it, report any problems. :)<p>Thank you!
Enjoy.<p>[^1]: <a href="https://github.com/Skarlso/crd-to-sample-yaml">https://github.com/Skarlso/crd-to-sample-yaml</a>