Skip to content

Commit

Permalink
fix: validate Splunk Observability release channel
Browse files Browse the repository at this point in the history
  • Loading branch information
mkaras-nobl9 committed Sep 23, 2024
1 parent eef86e8 commit a0c84e1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
8 changes: 7 additions & 1 deletion internal/manifest/v1alpha/examples/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func Direct() []Example {
return examples
}

var alphaChannelDirects = []v1alpha.DataSourceType{
v1alpha.SplunkObservability,
}

var betaChannelDirects = []v1alpha.DataSourceType{
v1alpha.AzureMonitor,
v1alpha.Honeycomb,
Expand Down Expand Up @@ -77,7 +81,9 @@ func (d directExample) Generate() v1alphaDirect.Direct {
Unit: defaultQueryDelay.Unit,
},
}
if slices.Contains(betaChannelDirects, typ) {
if slices.Contains(alphaChannelDirects, typ) {
direct.Spec.ReleaseChannel = v1alpha.ReleaseChannelAlpha
} else if slices.Contains(betaChannelDirects, typ) {
direct.Spec.ReleaseChannel = v1alpha.ReleaseChannelBeta
} else {
direct.Spec.ReleaseChannel = v1alpha.ReleaseChannelStable
Expand Down
27 changes: 26 additions & 1 deletion manifest/v1alpha/direct/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ var specValidation = govy.New[Spec](
Rules(exactlyOneDataSourceTypeValidationRule).
Rules(
historicalDataRetrievalValidationRule,
queryDelayGreaterThanOrEqualToDefaultValidationRule),
queryDelayGreaterThanOrEqualToDefaultValidationRule,
releaseChannelValidationRule),
govy.For(func(s Spec) v1alpha.ReleaseChannel { return s.ReleaseChannel }).
WithName("releaseChannel").
OmitEmpty().
Expand Down Expand Up @@ -223,6 +224,7 @@ var (
const (
errCodeExactlyOneDataSourceType = "exactly_one_data_source_type"
errCodeQueryDelayGreaterThanOrEqualToDefault = "query_delay_greater_than_or_equal_to_default"
errUnsupportedReleaseChannel = "unsupported_release_channel"
)

var exactlyOneDataSourceTypeValidationRule = govy.NewRule(func(spec Spec) error {
Expand Down Expand Up @@ -383,6 +385,29 @@ var queryDelayGreaterThanOrEqualToDefaultValidationRule = govy.NewRule(func(spec
return nil
}).WithErrorCode(errCodeQueryDelayGreaterThanOrEqualToDefault)

const errorCodeUnsupportedReleaseChannel = "unsupported_release_channel"

var releaseChannelValidationRule = govy.NewRule(func(spec Spec) error {
typ, _ := spec.GetType()
if typ != v1alpha.SplunkObservability && spec.ReleaseChannel == v1alpha.ReleaseChannelAlpha {
return govy.NewPropertyError(
"releaseChannel",
spec.ReleaseChannel,
errors.New("must be one of [stable, beta]"),
)
}

if typ == v1alpha.SplunkObservability && spec.ReleaseChannel != v1alpha.ReleaseChannelAlpha {
return govy.NewPropertyError(
"releaseChannel",
spec.ReleaseChannel,
errors.New("must be alpha for Splunk Observability"),
)
}

return nil
}).WithErrorCode(errorCodeUnsupportedReleaseChannel)

const errorCodeHTTPSSchemeRequired = "https_scheme_required"

func urlPropertyRules[S any](getter govy.PropertyGetter[string, S]) govy.PropertyRules[*url.URL, S] {
Expand Down
24 changes: 24 additions & 0 deletions manifest/v1alpha/direct/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ func TestValidateSpec_ReleaseChannel(t *testing.T) {
Code: rules.ErrorCodeOneOf,
})
})
t.Run("data source type using supported release channel", func(t *testing.T) {
direct := validDirect(v1alpha.Datadog)
direct.Spec.ReleaseChannel = v1alpha.ReleaseChannelBeta
err := validate(direct)
testutils.AssertNoError(t, direct, err)
})
t.Run("data source type using unsupported release channel", func(t *testing.T) {
direct := validDirect(v1alpha.Datadog)
direct.Spec.ReleaseChannel = v1alpha.ReleaseChannelAlpha
err := validate(direct)
testutils.AssertContainsErrors(t, direct, err, 1, testutils.ExpectedError{
Prop: "spec.releaseChannel",
Code: errUnsupportedReleaseChannel,
})
})
t.Run("data source type using unsupported release channel", func(t *testing.T) {
direct := validDirect(v1alpha.SplunkObservability)
direct.Spec.ReleaseChannel = v1alpha.ReleaseChannelStable
err := validate(direct)
testutils.AssertContainsErrors(t, direct, err, 1, testutils.ExpectedError{
Prop: "spec.releaseChannel",
Code: errUnsupportedReleaseChannel,
})
})
}

func TestValidateSpec_QueryDelay(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion manifest/v1alpha/release_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ func (r *ReleaseChannel) UnmarshalText(text []byte) error {
}

func ReleaseChannelValidation() govy.Rule[ReleaseChannel] {
return rules.OneOf(ReleaseChannelStable, ReleaseChannelBeta)
return rules.OneOf(ReleaseChannelStable, ReleaseChannelBeta, ReleaseChannelAlpha)
}

0 comments on commit a0c84e1

Please sign in to comment.