From 699aa4741693956a9e6f1c34fae2d90626dbf5f8 Mon Sep 17 00:00:00 2001 From: Michael Zalimeni <michael.zalimeni@hashicorp.com> Date: Thu, 31 Aug 2023 15:59:29 -0400 Subject: [PATCH] fix: make UNSPECIFIED protocol pass validation (#18634) We explicitly enumerate the allowed protocols in validation, so this change is necessary to use the new enum value. Also add tests for enum validators to ensure they stay aligned to protos unless we explicitly want them to diverge. --- internal/catalog/internal/types/validators.go | 4 +++- .../catalog/internal/types/validators_test.go | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/internal/catalog/internal/types/validators.go b/internal/catalog/internal/types/validators.go index 94691107f934..a9934156e298 100644 --- a/internal/catalog/internal/types/validators.go +++ b/internal/catalog/internal/types/validators.go @@ -139,7 +139,9 @@ func validatePortName(name string) error { func validateProtocol(protocol pbcatalog.Protocol) error { switch protocol { - case pbcatalog.Protocol_PROTOCOL_TCP, + case pbcatalog.Protocol_PROTOCOL_UNSPECIFIED, + // means pbcatalog.FailoverMode_FAILOVER_MODE_TCP + pbcatalog.Protocol_PROTOCOL_TCP, pbcatalog.Protocol_PROTOCOL_HTTP, pbcatalog.Protocol_PROTOCOL_HTTP2, pbcatalog.Protocol_PROTOCOL_GRPC, diff --git a/internal/catalog/internal/types/validators_test.go b/internal/catalog/internal/types/validators_test.go index a3790e9f9e05..d62c675cf927 100644 --- a/internal/catalog/internal/types/validators_test.go +++ b/internal/catalog/internal/types/validators_test.go @@ -334,6 +334,26 @@ func TestValidatePortName(t *testing.T) { }) } +func TestValidateProtocol(t *testing.T) { + // this test simply verifies that we accept all enum values specified in our proto + // in order to avoid validator drift. + for name, value := range pbcatalog.Protocol_value { + t.Run(name, func(t *testing.T) { + require.NoError(t, validateProtocol(pbcatalog.Protocol(value))) + }) + } +} + +func TestValidateHealth(t *testing.T) { + // this test simply verifies that we accept all enum values specified in our proto + // in order to avoid validator drift. + for name, value := range pbcatalog.Health_value { + t.Run(name, func(t *testing.T) { + require.NoError(t, validateHealth(pbcatalog.Health(value))) + }) + } +} + func TestValidateWorkloadAddress(t *testing.T) { type testCase struct { addr *pbcatalog.WorkloadAddress