From bbe2801d6985a7140c1e8235275edd6140b93630 Mon Sep 17 00:00:00 2001 From: Sergey Dudoladov Date: Fri, 16 Feb 2018 11:13:08 +0100 Subject: [PATCH] Adjust unit tests for namespace decoding --- pkg/spec/types.go | 10 ++++++++-- pkg/spec/types_test.go | 16 +++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pkg/spec/types.go b/pkg/spec/types.go index 7694e4ca2..7a3078674 100644 --- a/pkg/spec/types.go +++ b/pkg/spec/types.go @@ -165,13 +165,19 @@ func (n NamespacedName) MarshalJSON() ([]byte, error) { // Decode converts a (possibly unqualified) string into the namespaced name object. func (n *NamespacedName) Decode(value string) error { + return n.DecodeWorker(value, GetOperatorNamespace()) +} + +// DecodeWorker separates the decode logic to (unit) test +// from obtaining the operator namespace that depends on k8s mounting files at runtime +func (n *NamespacedName) DecodeWorker(value, operatorNamespace string) error { name := types.NewNamespacedNameFromString(value) if strings.Trim(value, string(types.Separator)) != "" && name == (types.NamespacedName{}) { name.Name = value - name.Namespace = GetOperatorNamespace() + name.Namespace = operatorNamespace } else if name.Namespace == "" { - name.Namespace = GetOperatorNamespace() + name.Namespace = operatorNamespace } if name.Name == "" { diff --git a/pkg/spec/types_test.go b/pkg/spec/types_test.go index 05045445b..7611501ad 100644 --- a/pkg/spec/types_test.go +++ b/pkg/spec/types_test.go @@ -5,22 +5,27 @@ import ( "testing" ) +const ( + mockOperatorNamespace = "acid" +) + var nnTests = []struct { s string expected NamespacedName expectedMarshal []byte }{ - {`acid/cluster`, NamespacedName{Namespace: "acid", Name: "cluster"}, []byte(`"acid/cluster"`)}, - {`/name`, NamespacedName{Namespace: "default", Name: "name"}, []byte(`"default/name"`)}, - {`test`, NamespacedName{Namespace: "default", Name: "test"}, []byte(`"default/test"`)}, + {`acid/cluster`, NamespacedName{Namespace: mockOperatorNamespace, Name: "cluster"}, []byte(`"acid/cluster"`)}, + {`/name`, NamespacedName{Namespace: mockOperatorNamespace, Name: "name"}, []byte(`"acid/name"`)}, + {`test`, NamespacedName{Namespace: mockOperatorNamespace, Name: "test"}, []byte(`"acid/test"`)}, } var nnErr = []string{"test/", "/", "", "//"} func TestNamespacedNameDecode(t *testing.T) { + for _, tt := range nnTests { var actual NamespacedName - err := actual.Decode(tt.s) + err := actual.DecodeWorker(tt.s, mockOperatorNamespace) if err != nil { t.Errorf("decode error: %v", err) } @@ -28,6 +33,7 @@ func TestNamespacedNameDecode(t *testing.T) { t.Errorf("expected: %v, got %#v", tt.expected, actual) } } + } func TestNamespacedNameMarshal(t *testing.T) { @@ -47,7 +53,7 @@ func TestNamespacedNameMarshal(t *testing.T) { func TestNamespacedNameError(t *testing.T) { for _, tt := range nnErr { var actual NamespacedName - err := actual.Decode(tt) + err := actual.DecodeWorker(tt, mockOperatorNamespace) if err == nil { t.Errorf("error expected for %q, got: %#v", tt, actual) }