package dataplane_test // The data plane checks a follow-up before the operator writes it, and // the CRDs check it again, so the two must hold the same bounds. import ( "os" "regexp" "testing" "time" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "sigs.k8s.io/yaml" "tangled.org/guid.foo/simmer/dataplane" ) func schemaIn(t *testing.T, file string) apiextensionsv1.JSONSchemaProps { t.Helper() raw, err := os.ReadFile("../deploy/crds/" + file) if err != nil { t.Fatal(err) } crd := &apiextensionsv1.CustomResourceDefinition{} if err := yaml.UnmarshalStrict(raw, crd); err != nil { t.Fatal(err) } return *crd.Spec.Versions[0].Schema.OpenAPIV3Schema } func TestTheSessionCRDHoldsAFollowUpsReasonToTheDataPlanesLength(t *testing.T) { reason := schemaIn(t, "sessions.yaml").Properties["status"].Properties["followUp"].Properties["reason"] if reason.MaxLength == nil || *reason.MaxLength != dataplane.MaxFollowUpReason { t.Errorf("the CRD holds a reason to %v characters, and the data plane to %d", reason.MaxLength, dataplane.MaxFollowUpReason) } } func TestTheMandateCRDHoldsMaxFollowUpToTheDataPlanesShortest(t *testing.T) { maxFollowUp := schemaIn(t, "mandates.yaml").Properties["spec"].Properties["sessions"].Properties["maxFollowUp"] shortest := regexp.MustCompile(`duration\(self\) >= duration\('([^']+)'\)`) for _, rule := range maxFollowUp.XValidations { if m := shortest.FindStringSubmatch(rule.Rule); m != nil { if d, err := time.ParseDuration(m[1]); err != nil || d != dataplane.MinFollowUp { t.Errorf("the CRD's shortest maxFollowUp is %s, and the data plane's shortest follow-up is %s", m[1], dataplane.MinFollowUp) } return } } t.Error("no rule of maxFollowUp names its shortest duration") }