|
| 1 | +using FluentAssertions; |
| 2 | + |
| 3 | +using k8s.Models; |
| 4 | + |
| 5 | +using KubeOps.Abstractions.Entities; |
| 6 | +using KubeOps.Abstractions.Entities.Attributes; |
| 7 | + |
| 8 | +namespace KubeOps.Transpiler.Test; |
| 9 | + |
| 10 | +public sealed partial class CrdsMlcTest |
| 11 | +{ |
| 12 | + private const string Rule1 = "has(self.https) || self.kind != 'https'"; |
| 13 | + private const string Message1 = "https object must be specified if kind is https"; |
| 14 | + private const string FieldPath1 = ".property"; |
| 15 | + private const string Reason1 = "reason"; |
| 16 | + private const string MessageExpression1 = "\"https object must be specified if kind is \" + string(self.kind)"; |
| 17 | + |
| 18 | + private const string Rule2 = "has(self.workflow) || self.kind != 'my-workflow"; |
| 19 | + private const string Message2 = "workflow must be specified if handling is workflow"; |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void Should_Set_Validations() |
| 23 | + { |
| 24 | + var crd = _mlc.Transpile(typeof(SingleValidateAttrEntity)); |
| 25 | + |
| 26 | + var specProperties = crd.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["property"]; |
| 27 | + specProperties.XKubernetesValidations.Should().HaveCount(1); |
| 28 | + specProperties.XKubernetesValidations[0].Rule.Should().Be(Rule1); |
| 29 | + specProperties.XKubernetesValidations[0].Message.Should().Be(Message1); |
| 30 | + specProperties.XKubernetesValidations[0].MessageExpression.Should().BeNull(); |
| 31 | + specProperties.XKubernetesValidations[0].FieldPath.Should().BeNull(); |
| 32 | + specProperties.XKubernetesValidations[0].Reason.Should().BeNull(); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void Should_Set_MultipleValidations() |
| 37 | + { |
| 38 | + var crd = _mlc.Transpile(typeof(MultiValidateAttrEntity)); |
| 39 | + |
| 40 | + var specProperties = crd.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["property"]; |
| 41 | + specProperties.XKubernetesValidations.Should().HaveCount(2); |
| 42 | + specProperties.XKubernetesValidations[0].Rule.Should().Be(Rule1); |
| 43 | + specProperties.XKubernetesValidations[0].Message.Should().Be(Message1); |
| 44 | + specProperties.XKubernetesValidations[0].MessageExpression.Should().BeNull(); |
| 45 | + specProperties.XKubernetesValidations[0].FieldPath.Should().BeNull(); |
| 46 | + specProperties.XKubernetesValidations[0].Reason.Should().BeNull(); |
| 47 | + specProperties.XKubernetesValidations[1].Rule.Should().Be(Rule2); |
| 48 | + specProperties.XKubernetesValidations[1].Message.Should().Be(Message2); |
| 49 | + specProperties.XKubernetesValidations[1].MessageExpression.Should().BeNull(); |
| 50 | + specProperties.XKubernetesValidations[1].FieldPath.Should().BeNull(); |
| 51 | + specProperties.XKubernetesValidations[1].Reason.Should().BeNull(); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void Should_Omit_Validations() |
| 56 | + { |
| 57 | + var crd = _mlc.Transpile(typeof(NoValidateAttrEntity)); |
| 58 | + |
| 59 | + var specProperties = crd.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["property"]; |
| 60 | + specProperties.XKubernetesValidations.Should().BeNull(); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void Should_Set_ValidationFields() |
| 65 | + { |
| 66 | + var crd = _mlc.Transpile(typeof(AllFieldsValidateAttrEntity)); |
| 67 | + |
| 68 | + var specProperties = crd.Spec.Versions[0].Schema.OpenAPIV3Schema.Properties["property"]; |
| 69 | + specProperties.XKubernetesValidations.Should().HaveCount(1); |
| 70 | + specProperties.XKubernetesValidations[0].Rule.Should().Be(Rule1); |
| 71 | + specProperties.XKubernetesValidations[0].Message.Should().Be(Message1); |
| 72 | + specProperties.XKubernetesValidations[0].MessageExpression.Should().Be(MessageExpression1); |
| 73 | + specProperties.XKubernetesValidations[0].FieldPath.Should().Be(FieldPath1); |
| 74 | + specProperties.XKubernetesValidations[0].Reason.Should().Be(Reason1); |
| 75 | + } |
| 76 | + |
| 77 | + #region Test Entity Classes |
| 78 | + |
| 79 | + [KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")] |
| 80 | + public sealed class NoValidateAttrEntity : CustomKubernetesEntity |
| 81 | + { |
| 82 | + public string Property { get; set; } = null!; |
| 83 | + } |
| 84 | + |
| 85 | + [KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")] |
| 86 | + public sealed class SingleValidateAttrEntity : CustomKubernetesEntity |
| 87 | + { |
| 88 | + [ValidationRule(Rule1, message: Message1)] |
| 89 | + public string Property { get; set; } = null!; |
| 90 | + } |
| 91 | + |
| 92 | + [KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")] |
| 93 | + public sealed class MultiValidateAttrEntity : CustomKubernetesEntity |
| 94 | + { |
| 95 | + [ValidationRule(Rule1, message: Message1)] |
| 96 | + [ValidationRule(Rule2, message: Message2)] |
| 97 | + public string Property { get; set; } = null!; |
| 98 | + } |
| 99 | + |
| 100 | + [KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")] |
| 101 | + public sealed class AllFieldsValidateAttrEntity : CustomKubernetesEntity |
| 102 | + { |
| 103 | + [ValidationRule(Rule1, FieldPath1, Message1, MessageExpression1, Reason1)] |
| 104 | + public string Property { get; set; } = null!; |
| 105 | + } |
| 106 | + |
| 107 | + #endregion Test Entity Classes |
| 108 | +} |
0 commit comments