From 4f545e37c6eb439086a6a266d5cbb35b6b42ea07 Mon Sep 17 00:00:00 2001 From: changzhen Date: Wed, 16 Mar 2022 14:13:33 +0800 Subject: [PATCH] lifted ut from the k8s codebase for resourcequota Signed-off-by: changzhen --- pkg/util/lifted/resourcehelpers_test.go | 85 +++++++++++++++++++ pkg/util/lifted/validateresourcequota_test.go | 73 ++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 pkg/util/lifted/resourcehelpers_test.go create mode 100644 pkg/util/lifted/validateresourcequota_test.go diff --git a/pkg/util/lifted/resourcehelpers_test.go b/pkg/util/lifted/resourcehelpers_test.go new file mode 100644 index 000000000..65acbda12 --- /dev/null +++ b/pkg/util/lifted/resourcehelpers_test.go @@ -0,0 +1,85 @@ +/* +Copyright 2015 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// This code is directly lifted from the Kubernetes codebase in order to avoid relying on the k8s.io/kubernetes package. +// For reference: +// https://github.com/kubernetes/kubernetes/blob/release-1.23/pkg/apis/core/helper/helpers_test.go + +package lifted + +import ( + "fmt" + v1 "k8s.io/api/core/v1" + "testing" +) + +func TestIsNativeResource(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 { + t.Run(fmt.Sprintf("resourceName input=%s, expected value=%v", tc.resourceName, tc.expectVal), func(t *testing.T) { + t.Parallel() + v := IsNativeResource(tc.resourceName) + if v != tc.expectVal { + t.Errorf("Got %v but expected %v", v, tc.expectVal) + } + }) + } +} + +func TestIsStandardResource(t *testing.T) { + testCases := []struct { + input string + output bool + }{ + {"cpu", true}, + {"memory", true}, + {"disk", false}, + {"blah", false}, + {"x.y.z", false}, + {"hugepages-2Mi", true}, + {"requests.hugepages-2Mi", true}, + } + for i, tc := range testCases { + if IsStandardResourceName(tc.input) != tc.output { + t.Errorf("case[%d], input: %s, expected: %t, got: %t", i, tc.input, tc.output, !tc.output) + } + } +} diff --git a/pkg/util/lifted/validateresourcequota_test.go b/pkg/util/lifted/validateresourcequota_test.go new file mode 100644 index 000000000..de2ff8c27 --- /dev/null +++ b/pkg/util/lifted/validateresourcequota_test.go @@ -0,0 +1,73 @@ +/* +Copyright 2014 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// This code is directly lifted from the Kubernetes codebase in order to avoid relying on the k8s.io/kubernetes package. +// For reference: +// https://github.com/kubernetes/kubernetes/blob/release-1.23/pkg/apis/core/validation/validation_test.go#L15475-L15518 + +package lifted + +import ( + "strings" + "testing" + + "k8s.io/apimachinery/pkg/util/validation/field" +) + +func TestValidateResourceNames(t *testing.T) { + table := []struct { + input string + success bool + expect string + }{ + {"memory", true, ""}, + {"cpu", true, ""}, + {"storage", true, ""}, + {"requests.cpu", true, ""}, + {"requests.memory", true, ""}, + {"requests.storage", true, ""}, + {"limits.cpu", true, ""}, + {"limits.memory", true, ""}, + {"network", false, ""}, + {"disk", false, ""}, + {"", false, ""}, + {".", false, ""}, + {"..", false, ""}, + {"my.favorite.app.co/12345", true, ""}, + {"my.favorite.app.co/_12345", false, ""}, + {"my.favorite.app.co/12345_", false, ""}, + {"kubernetes.io/..", false, ""}, + {"kubernetes.io/" + strings.Repeat("a", 63), true, ""}, + {"kubernetes.io/" + strings.Repeat("a", 64), false, ""}, + {"kubernetes.io//", false, ""}, + {"kubernetes.io", false, ""}, + {"kubernetes.io/will/not/work/", false, ""}, + } + for k, item := range table { + err := validateResourceName(item.input, field.NewPath("field")) + if len(err) != 0 && item.success { + t.Errorf("expected no failure for input %q", item.input) + } else if len(err) == 0 && !item.success { + t.Errorf("expected failure for input %q", item.input) + for i := range err { + detail := err[i].Detail + if detail != "" && !strings.Contains(detail, item.expect) { + t.Errorf("%d: expected error detail either empty or %s, got %s", k, item.expect, detail) + } + } + } + } +}