From 4c2bad9f91f6926e8f006ea4ebf5dbda705faef4 Mon Sep 17 00:00:00 2001 From: Gergely Brautigam <182850+Skarlso@users.noreply.github.com> Date: Thu, 23 May 2024 20:16:43 +0200 Subject: [PATCH] updated README and added more tests --- README.md | 21 +++++++++++++++++++ pkg/generate_test.go | 20 ++++++++++++++++++ pkg/testdata/sample_crd.yaml | 4 ++-- ...example_with_example_for_field_golden.yaml | 4 ++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 pkg/testdata/sample_crd_with_minimal_example_with_example_for_field_golden.yaml diff --git a/README.md b/README.md index 89200f5..5ac8e04 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,27 @@ To get an HTML output provide the format flag like this: cty generate -c delivery.krok.app_krokcommands --comments --format html ``` +### Minimal required CRD sample + +It's possible to generate a sample YAML for a CRD that will make the CRD validation pass. Meaning, it will only contain +samples for fields that are actually required. All other fields will be ignored. + +For example, a CRD having a single required field with an example and the rest being optional would generate something +like this: + +```yaml +apiVersion: delivery.krok.app/v1alpha1 +kind: KrokCommand +spec: + image: "krok-hook/slack-notification:v0.0.1" +``` + +To run cty with minimal required fields, pass in `--minimal` to the command like this: + +``` +cty generate -c delivery.krok.app_krokcommands --comments --minimal --format html +``` + ## WASM frontend There is a WASM based frontend that can be started by navigating into the `wasm` folder and running the following make diff --git a/pkg/generate_test.go b/pkg/generate_test.go index 1febcaf..cb6a69e 100644 --- a/pkg/generate_test.go +++ b/pkg/generate_test.go @@ -91,3 +91,23 @@ func TestGenerateMinimal(t *testing.T) { assert.Equal(t, golden, buffer.Bytes()) } + +func TestGenerateMinimalWithExample(t *testing.T) { + content, err := os.ReadFile(filepath.Join("testdata", "sample_crd_with_example.yaml")) + require.NoError(t, err) + + crd := &v1beta1.CustomResourceDefinition{} + require.NoError(t, yaml.Unmarshal(content, crd)) + + var output []byte + buffer := bytes.NewBuffer(output) + + parser := NewParser(crd.Spec.Group, crd.Spec.Names.Kind, false, true) + version := crd.Spec.Versions[0] + require.NoError(t, parser.ParseProperties(version.Name, buffer, version.Schema.OpenAPIV3Schema.Properties, RootRequiredFields)) + + golden, err := os.ReadFile(filepath.Join("testdata", "sample_crd_with_minimal_example_with_example_for_field_golden.yaml")) + require.NoError(t, err) + + assert.Equal(t, golden, buffer.Bytes()) +} diff --git a/pkg/testdata/sample_crd.yaml b/pkg/testdata/sample_crd.yaml index f04ad89..404b0b1 100644 --- a/pkg/testdata/sample_crd.yaml +++ b/pkg/testdata/sample_crd.yaml @@ -48,8 +48,8 @@ spec: description: Enabled defines if this command can be executed or not. type: boolean image: - description: 'Image defines the image name and tag of the command - example: krok-hook/slack-notification:v0.0.1' + description: 'Image defines the image name and tag of the command. + example: krok-hook/slack-notification:v0.0.1' type: string platforms: description: Platforms holds all the platforms which this command diff --git a/pkg/testdata/sample_crd_with_minimal_example_with_example_for_field_golden.yaml b/pkg/testdata/sample_crd_with_minimal_example_with_example_for_field_golden.yaml new file mode 100644 index 0000000..6b27da6 --- /dev/null +++ b/pkg/testdata/sample_crd_with_minimal_example_with_example_for_field_golden.yaml @@ -0,0 +1,4 @@ +apiVersion: delivery.krok.app/v1alpha1 +kind: KrokCommand +spec: + image: "krok-hook/slack-notification:v0.0.1"