Adjust unit tests for namespace decoding

This commit is contained in:
Sergey Dudoladov 2018-02-16 11:13:08 +01:00
parent 0a9e6bd8d2
commit bbe2801d69
2 changed files with 19 additions and 7 deletions

View File

@ -165,13 +165,19 @@ func (n NamespacedName) MarshalJSON() ([]byte, error) {
// Decode converts a (possibly unqualified) string into the namespaced name object. // Decode converts a (possibly unqualified) string into the namespaced name object.
func (n *NamespacedName) Decode(value string) error { 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) name := types.NewNamespacedNameFromString(value)
if strings.Trim(value, string(types.Separator)) != "" && name == (types.NamespacedName{}) { if strings.Trim(value, string(types.Separator)) != "" && name == (types.NamespacedName{}) {
name.Name = value name.Name = value
name.Namespace = GetOperatorNamespace() name.Namespace = operatorNamespace
} else if name.Namespace == "" { } else if name.Namespace == "" {
name.Namespace = GetOperatorNamespace() name.Namespace = operatorNamespace
} }
if name.Name == "" { if name.Name == "" {

View File

@ -5,22 +5,27 @@ import (
"testing" "testing"
) )
const (
mockOperatorNamespace = "acid"
)
var nnTests = []struct { var nnTests = []struct {
s string s string
expected NamespacedName expected NamespacedName
expectedMarshal []byte expectedMarshal []byte
}{ }{
{`acid/cluster`, NamespacedName{Namespace: "acid", Name: "cluster"}, []byte(`"acid/cluster"`)}, {`acid/cluster`, NamespacedName{Namespace: mockOperatorNamespace, Name: "cluster"}, []byte(`"acid/cluster"`)},
{`/name`, NamespacedName{Namespace: "default", Name: "name"}, []byte(`"default/name"`)}, {`/name`, NamespacedName{Namespace: mockOperatorNamespace, Name: "name"}, []byte(`"acid/name"`)},
{`test`, NamespacedName{Namespace: "default", Name: "test"}, []byte(`"default/test"`)}, {`test`, NamespacedName{Namespace: mockOperatorNamespace, Name: "test"}, []byte(`"acid/test"`)},
} }
var nnErr = []string{"test/", "/", "", "//"} var nnErr = []string{"test/", "/", "", "//"}
func TestNamespacedNameDecode(t *testing.T) { func TestNamespacedNameDecode(t *testing.T) {
for _, tt := range nnTests { for _, tt := range nnTests {
var actual NamespacedName var actual NamespacedName
err := actual.Decode(tt.s) err := actual.DecodeWorker(tt.s, mockOperatorNamespace)
if err != nil { if err != nil {
t.Errorf("decode error: %v", err) t.Errorf("decode error: %v", err)
} }
@ -28,6 +33,7 @@ func TestNamespacedNameDecode(t *testing.T) {
t.Errorf("expected: %v, got %#v", tt.expected, actual) t.Errorf("expected: %v, got %#v", tt.expected, actual)
} }
} }
} }
func TestNamespacedNameMarshal(t *testing.T) { func TestNamespacedNameMarshal(t *testing.T) {
@ -47,7 +53,7 @@ func TestNamespacedNameMarshal(t *testing.T) {
func TestNamespacedNameError(t *testing.T) { func TestNamespacedNameError(t *testing.T) {
for _, tt := range nnErr { for _, tt := range nnErr {
var actual NamespacedName var actual NamespacedName
err := actual.Decode(tt) err := actual.DecodeWorker(tt, mockOperatorNamespace)
if err == nil { if err == nil {
t.Errorf("error expected for %q, got: %#v", tt, actual) t.Errorf("error expected for %q, got: %#v", tt, actual)
} }