From aeb3b761b2e9037c0029cd4404c83b95e64aa0b5 Mon Sep 17 00:00:00 2001 From: clairew Date: Thu, 27 Jul 2017 01:06:34 -0700 Subject: [PATCH] OpaqueIntResourceName unit tests --- pkg/api/v1/helper/helpers_test.go | 59 +++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/pkg/api/v1/helper/helpers_test.go b/pkg/api/v1/helper/helpers_test.go index 0d6bb77661c..3b8e5f87837 100644 --- a/pkg/api/v1/helper/helpers_test.go +++ b/pkg/api/v1/helper/helpers_test.go @@ -17,6 +17,7 @@ limitations under the License. package helper import ( + "fmt" "reflect" "testing" @@ -26,6 +27,64 @@ import ( "k8s.io/apimachinery/pkg/labels" ) +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 + expectVal bool + }{ + { + resourceName: "pod.alpha.kubernetes.io/opaque-int-resource-foo", + expectVal: true, // resourceName should pass because the resourceName has the correct prefix. + }, + { + resourceName: "foo", + expectVal: false, // resourceName should fail because the resourceName has the wrong prefix. + }, + { + resourceName: "", + expectVal: false, // resourceName should fail, empty resourceName. + }, + } + + 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 := IsOpaqueIntResourceName(tc.resourceName) + if v != tc.expectVal { + t.Errorf("Got %v but expected %v", v, tc.expectVal) + } + }) + } +} + +func TestOpaqueIntResourceName(t *testing.T) { // each output should have the correct appended prefix ("pod.alpha.kubernetes.io/opaque-int-resource-") for opaque counted resources. + testCases := []struct { + name string + expectVal v1.ResourceName + }{ + { + name: "foo", + expectVal: "pod.alpha.kubernetes.io/opaque-int-resource-foo", // append prefix to input string foo + }, + { + name: "", + expectVal: "pod.alpha.kubernetes.io/opaque-int-resource-", // append prefix to input empty string + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(fmt.Sprintf("name input=%s, expected value=%s", tc.name, tc.expectVal), func(t *testing.T) { + t.Parallel() + v := OpaqueIntResourceName(tc.name) + if v != tc.expectVal { + t.Errorf("Got %v but expected %v", v, tc.expectVal) + } + }) + } +} + func TestAddToNodeAddresses(t *testing.T) { testCases := []struct { existing []v1.NodeAddress