From 0da9d6dfd24e4634098f214e646f65f1046efd6f Mon Sep 17 00:00:00 2001 From: volmedo Date: Mon, 21 Oct 2019 23:00:35 +0200 Subject: [PATCH] Add unmarshaling code and tests Signed-off-by: volmedo --- .../jaegertracing/v1/deployment_strategy.go | 49 +++++++++++++++++++ .../v1/deployment_strategy_test.go | 35 +++++++++++++ pkg/apis/jaegertracing/v1/jaeger_types.go | 22 --------- 3 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 pkg/apis/jaegertracing/v1/deployment_strategy.go create mode 100644 pkg/apis/jaegertracing/v1/deployment_strategy_test.go diff --git a/pkg/apis/jaegertracing/v1/deployment_strategy.go b/pkg/apis/jaegertracing/v1/deployment_strategy.go new file mode 100644 index 0000000000..93d48c5f5a --- /dev/null +++ b/pkg/apis/jaegertracing/v1/deployment_strategy.go @@ -0,0 +1,49 @@ +package v1 + +import ( + "errors" + "strings" +) + +// DeploymentStrategy represents the possible values for deployment strategies +// +k8s:openapi-gen=true +type DeploymentStrategy string + +const ( + // DeploymentStrategyDeprecatedAllInOne represents the (deprecated) 'all-in-one' deployment strategy + // +k8s:openapi-gen=true + DeploymentStrategyDeprecatedAllInOne DeploymentStrategy = "all-in-one" + + // DeploymentStrategyAllInOne represents the 'allInOne' deployment strategy (default) + // +k8s:openapi-gen=true + DeploymentStrategyAllInOne DeploymentStrategy = "allinone" + + // DeploymentStrategyStreaming represents the 'streaming' deployment strategy + // +k8s:openapi-gen=true + DeploymentStrategyStreaming DeploymentStrategy = "streaming" + + // DeploymentStrategyProduction represents the 'production' deployment strategy + // +k8s:openapi-gen=true + DeploymentStrategyProduction DeploymentStrategy = "production" +) + +// UnmarshalText implements encoding.TextUnmarshaler to ensure that JSON strings +// in jaeger specs values are interpreted in a case-insensitive manner +func (ds *DeploymentStrategy) UnmarshalText(text []byte) error { + if ds == nil { + return errors.New("DeploymentStrategy: UnmarshalText on nil pointer") + } + + switch strings.ToLower(string(text)) { + default: + *ds = DeploymentStrategyAllInOne + case string(DeploymentStrategyDeprecatedAllInOne): + *ds = DeploymentStrategyDeprecatedAllInOne + case string(DeploymentStrategyStreaming): + *ds = DeploymentStrategyStreaming + case string(DeploymentStrategyProduction): + *ds = DeploymentStrategyProduction + } + + return nil +} diff --git a/pkg/apis/jaegertracing/v1/deployment_strategy_test.go b/pkg/apis/jaegertracing/v1/deployment_strategy_test.go new file mode 100644 index 0000000000..d90d554122 --- /dev/null +++ b/pkg/apis/jaegertracing/v1/deployment_strategy_test.go @@ -0,0 +1,35 @@ +package v1 + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestUnmarshalJSON(t *testing.T) { + tcs := map[string]struct { + json string + expected DeploymentStrategy + }{ + "allInOne": {json: `"allInOne"`, expected: DeploymentStrategyAllInOne}, + "streaming": {json: `"streaming"`, expected: DeploymentStrategyStreaming}, + "production": {json: `"production"`, expected: DeploymentStrategyProduction}, + "all-in-one": {json: `"all-in-one"`, expected: DeploymentStrategyDeprecatedAllInOne}, + "ALLinONE": {json: `"ALLinONE"`, expected: DeploymentStrategyAllInOne}, + "StReAmInG": {json: `"StReAmInG"`, expected: DeploymentStrategyStreaming}, + "Production": {json: `"Production"`, expected: DeploymentStrategyProduction}, + "All-IN-One": {json: `"All-IN-One"`, expected: DeploymentStrategyDeprecatedAllInOne}, + "random value": {json: `"random value"`, expected: DeploymentStrategyAllInOne}, + "empty string": {json: `""`, expected: DeploymentStrategyAllInOne}, + } + + for name, tc := range tcs { + t.Run(name, func(t *testing.T) { + ds := DeploymentStrategy("") + err := json.Unmarshal([]byte(tc.json), &ds) + assert.NoError(t, err) + assert.Equal(t, tc.expected, ds) + }) + } +} diff --git a/pkg/apis/jaegertracing/v1/jaeger_types.go b/pkg/apis/jaegertracing/v1/jaeger_types.go index a27f862b5c..c58344ecfb 100644 --- a/pkg/apis/jaegertracing/v1/jaeger_types.go +++ b/pkg/apis/jaegertracing/v1/jaeger_types.go @@ -49,28 +49,6 @@ const ( IngressSecurityOAuthProxy IngressSecurityType = "oauth-proxy" ) -// DeploymentStrategy represents the possible values for deployment strategies -// +k8s:openapi-gen=true -type DeploymentStrategy string - -const ( - // DeploymentStrategyDeprecatedAllInOne represents the (deprecated) 'all-in-one' deployment strategy - // +k8s:openapi-gen=true - DeploymentStrategyDeprecatedAllInOne DeploymentStrategy = "all-in-one" - - // DeploymentStrategyAllInOne represents the 'allInOne' deployment strategy (default) - // +k8s:openapi-gen=true - DeploymentStrategyAllInOne DeploymentStrategy = "allinone" - - // DeploymentStrategyStreaming represents the 'streaming' deployment strategy - // +k8s:openapi-gen=true - DeploymentStrategyStreaming DeploymentStrategy = "streaming" - - // DeploymentStrategyProduction represents the 'production' deployment strategy - // +k8s:openapi-gen=true - DeploymentStrategyProduction DeploymentStrategy = "production" -) - // JaegerSpec defines the desired state of Jaeger // +k8s:openapi-gen=true type JaegerSpec struct {