Skip to content

Commit

Permalink
Adjust unit tests for namespace decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Dudoladov committed Feb 16, 2018
1 parent 0a9e6bd commit bbe2801
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
10 changes: 8 additions & 2 deletions pkg/spec/types.go
Expand Up @@ -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 == "" {
Expand Down
16 changes: 11 additions & 5 deletions pkg/spec/types_test.go
Expand Up @@ -5,29 +5,35 @@ 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)
}
if actual != tt.expected {
t.Errorf("expected: %v, got %#v", tt.expected, actual)
}
}

}

func TestNamespacedNameMarshal(t *testing.T) {
Expand All @@ -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)
}
Expand Down

0 comments on commit bbe2801

Please sign in to comment.