From 4cff9dab46eb3e44289b3da71cf8bf952367a372 Mon Sep 17 00:00:00 2001 From: chenguoyan01 Date: Wed, 18 Oct 2017 00:24:35 +0800 Subject: [PATCH] add unit tests in /pkg/api/v1helper Change-Id: I97b1c1d5521d6db6b96bc3659d0484f5527cb6cc --- pkg/api/v1/helper/helpers_test.go | 125 ++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/pkg/api/v1/helper/helpers_test.go b/pkg/api/v1/helper/helpers_test.go index 3b8e5f8783..3a890420a6 100644 --- a/pkg/api/v1/helper/helpers_test.go +++ b/pkg/api/v1/helper/helpers_test.go @@ -23,10 +23,135 @@ import ( "k8s.io/api/core/v1" apiequality "k8s.io/apimachinery/pkg/api/equality" + "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" ) +func TestIsDefaultNamespaceResource(t *testing.T) { + testCases := []struct { + resourceName v1.ResourceName + expectVal bool + }{ + { + resourceName: "pod.alpha.kubernetes.io/opaque-int-resource-foo", + expectVal: true, + }, + { + resourceName: "kubernetes.io/resource-foo", + expectVal: true, + }, + { + resourceName: "foo", + expectVal: true, + }, + { + resourceName: "a/b", + expectVal: false, + }, + { + resourceName: "", + expectVal: true, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(fmt.Sprintf("resourceName input=%s, expected value=%v", tc.resourceName, tc.expectVal), func(t *testing.T) { + t.Parallel() + v := IsDefaultNamespaceResource(tc.resourceName) + if v != tc.expectVal { + t.Errorf("Got %v but expected %v", v, tc.expectVal) + } + }) + } +} + +func TestHugePageSizeFromResourceName(t *testing.T) { + expected100m, _ := resource.ParseQuantity("100m") + testCases := []struct { + resourceName v1.ResourceName + expectVal resource.Quantity + expectErr bool + }{ + { + resourceName: "pod.alpha.kubernetes.io/opaque-int-resource-foo", + expectVal: resource.Quantity{}, + expectErr: true, + }, + { + resourceName: "hugepages-", + expectVal: resource.Quantity{}, + expectErr: true, + }, + { + resourceName: "hugepages-100m", + expectVal: expected100m, + expectErr: false, + }, + { + resourceName: "", + expectVal: resource.Quantity{}, + expectErr: true, + }, + } + + for i, tc := range testCases { + tc := tc + t.Run(fmt.Sprintf("resourceName input=%s, expected value=%v", tc.resourceName, tc.expectVal), func(t *testing.T) { + t.Parallel() + v, err := HugePageSizeFromResourceName(tc.resourceName) + if err == nil && tc.expectErr { + t.Errorf("[%v]expected error but got none.", i) + } + if err != nil && !tc.expectErr { + t.Errorf("[%v]did not expect error but got: %v", i, err) + } + if v != tc.expectVal { + t.Errorf("Got %v but expected %v", v, tc.expectVal) + } + }) + } +} + +func TestIsOvercommitAllowed(t *testing.T) { + testCases := []struct { + resourceName v1.ResourceName + expectVal bool + }{ + { + resourceName: "pod.alpha.kubernetes.io/opaque-int-resource-foo", + expectVal: true, + }, + { + resourceName: "kubernetes.io/resource-foo", + expectVal: true, + }, + { + resourceName: "alpha.kubernetes.io/nvidia-gpu", + expectVal: false, + }, + { + resourceName: "hugepages-100m", + expectVal: false, + }, + { + resourceName: "", + expectVal: true, + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(fmt.Sprintf("resourceName input=%s, expected value=%v", tc.resourceName, tc.expectVal), func(t *testing.T) { + t.Parallel() + v := IsOvercommitAllowed(tc.resourceName) + if v != tc.expectVal { + t.Errorf("Got %v but expected %v", v, tc.expectVal) + } + }) + } +} func TestIsOpaqueIntResourceName(t *testing.T) { // resourceName input with the correct OpaqueIntResourceName prefix ("pod.alpha.kubernetes.io/opaque-int-resource-") should pass testCases := []struct { resourceName v1.ResourceName