Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Shuting Zhao <shutting06@gmail.com>
  • Loading branch information
realshuting committed Apr 5, 2021
1 parent 8affebb commit 741f230
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
10 changes: 4 additions & 6 deletions pkg/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ func ExtractResources(newRaw []byte, request *v1beta1.AdmissionRequest) (unstruc
if err != nil {
return emptyResource, emptyResource, fmt.Errorf("failed to convert new raw to unstructured: %v", err)
}
if newResource.GetKind() == "Namespace" {
newResource.SetNamespace("")
}
}

// Old Resource
Expand All @@ -116,9 +113,6 @@ func ExtractResources(newRaw []byte, request *v1beta1.AdmissionRequest) (unstruc
if err != nil {
return emptyResource, emptyResource, fmt.Errorf("failed to convert old raw to unstructured: %v", err)
}
if oldResource.GetKind() == "Namespace" {
oldResource.SetNamespace("")
}
}

return newResource, oldResource, err
Expand All @@ -137,6 +131,10 @@ func ConvertResource(raw []byte, group, version, kind, namespace string) (unstru
obj.SetNamespace(namespace)
}

if obj.GetKind() == "Namespace" && obj.GetNamespace() != "" {
obj.SetNamespace("")
}

return *obj, nil
}

Expand Down
73 changes: 73 additions & 0 deletions pkg/utils/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,76 @@ func Test_higherVersion(t *testing.T) {
v, err = isVersionHigher("v1.5.9-rc2", 1, 5, 9)
assert.Assert(t, v == false && err == nil)
}

func Test_ConvertResource(t *testing.T) {
testCases := []struct {
name string
raw string
group, version, kind string
namespace string
expectedNamespace string
}{
{
name: "test-namespaced-resource-secret-with-namespace",
raw: `{"apiVersion": "v1","data": {"password": "YXNkO2xma2o4OTJsIC1uCg=="},"kind": "Secret","metadata": {"name": "my-secret","namespace": "test"},"type": "Opaque"}`,
group: "",
version: "v1",
kind: "Secret",
namespace: "mynamespace",
expectedNamespace: "mynamespace",
},
{
name: "test-namespaced-resource-secret-without-namespace",
raw: `{"apiVersion": "v1","data": {"password": "YXNkO2xma2o4OTJsIC1uCg=="},"kind": "Secret","metadata": {"name": "my-secret"},"type": "Opaque"}`,
group: "",
version: "v1",
kind: "Secret",
namespace: "mynamespace",
expectedNamespace: "mynamespace",
},
{
name: "test-cluster-resource-namespace-with-namespace",
raw: `{"apiVersion": "v1","kind": "Namespace","metadata": {"name": "my-namespace","namespace": "oldnamespace"},"type": "Opaque"}`,
group: "",
version: "v1",
kind: "Namespace",
namespace: "newnamespace",
expectedNamespace: "",
},
{
name: "test-cluster-resource-namespace-without-namespace",
raw: `{"apiVersion": "v1","kind": "Namespace","metadata": {"name": "my-namespace"},"type": "Opaque"}`,
group: "",
version: "v1",
kind: "Namespace",
namespace: "newnamespace",
expectedNamespace: "",
},
{
name: "test-cluster-resource-cluster-role-with-namespace",
raw: `{"apiVersion": "rbac.authorization.k8s.io/v1","kind": "ClusterRole","metadata": {"name": "my-cluster-role","namespace":"test"},"rules": [{"apiGroups": ["*"],"resources": ["namespaces"],"verbs": ["watch"]}]}`,
group: "rbac.authorization.k8s.io",
version: "v1",
kind: "ClusterRole",
namespace: "",
expectedNamespace: "",
},
{
name: "test-cluster-resource-cluster-role-without-namespace",
raw: `{"apiVersion": "rbac.authorization.k8s.io/v1","kind": "ClusterRole","metadata": {"name": "my-cluster-role"},"rules": [{"apiGroups": ["*"],"resources": ["namespaces"],"verbs": ["watch"]}]}`,
group: "rbac.authorization.k8s.io",
version: "v1",
kind: "ClusterRole",
namespace: "",
expectedNamespace: "",
},
}

for _, test := range testCases {
resource, err := ConvertResource([]byte(test.raw), test.group, test.version, test.kind, test.namespace)
assert.NilError(t, err)
assert.Assert(t, resource.GetNamespace() == test.expectedNamespace)
break
}

}

0 comments on commit 741f230

Please sign in to comment.