Merge pull request #49842 from clairew/v1helper-OpaqueIntResourceName-unit-tests

Automatic merge from submit-queue (batch tested with PRs 49842, 50649)

Add Unit Test: opaque int resource name

**What this PR does / why we need it**: https://github.com/kubernetes/kubernetes/issues/49384, adding unit tests for functions related to the prefix OpaqueIntResourceName in /pkg/api/v1helper

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2017-08-14 23:14:01 -07:00 committed by GitHub
commit bb1e3b09eb

View File

@ -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