123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- /*
- Copyright 2016 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.
- */
- package quota
- import (
- "testing"
- "k8s.io/kubernetes/pkg/api"
- "k8s.io/kubernetes/pkg/api/resource"
- )
- func TestEquals(t *testing.T) {
- testCases := map[string]struct {
- a api.ResourceList
- b api.ResourceList
- expected bool
- }{
- "isEqual": {
- a: api.ResourceList{},
- b: api.ResourceList{},
- expected: true,
- },
- "isEqualWithKeys": {
- a: api.ResourceList{
- api.ResourceCPU: resource.MustParse("100m"),
- api.ResourceMemory: resource.MustParse("1Gi"),
- },
- b: api.ResourceList{
- api.ResourceCPU: resource.MustParse("100m"),
- api.ResourceMemory: resource.MustParse("1Gi"),
- },
- expected: true,
- },
- "isNotEqualSameKeys": {
- a: api.ResourceList{
- api.ResourceCPU: resource.MustParse("200m"),
- api.ResourceMemory: resource.MustParse("1Gi"),
- },
- b: api.ResourceList{
- api.ResourceCPU: resource.MustParse("100m"),
- api.ResourceMemory: resource.MustParse("1Gi"),
- },
- expected: false,
- },
- "isNotEqualDiffKeys": {
- a: api.ResourceList{
- api.ResourceCPU: resource.MustParse("100m"),
- api.ResourceMemory: resource.MustParse("1Gi"),
- },
- b: api.ResourceList{
- api.ResourceCPU: resource.MustParse("100m"),
- api.ResourceMemory: resource.MustParse("1Gi"),
- api.ResourcePods: resource.MustParse("1"),
- },
- expected: false,
- },
- }
- for testName, testCase := range testCases {
- if result := Equals(testCase.a, testCase.b); result != testCase.expected {
- t.Errorf("%s expected: %v, actual: %v, a=%v, b=%v", testName, testCase.expected, result, testCase.a, testCase.b)
- }
- }
- }
- func TestMax(t *testing.T) {
- testCases := map[string]struct {
- a api.ResourceList
- b api.ResourceList
- expected api.ResourceList
- }{
- "noKeys": {
- a: api.ResourceList{},
- b: api.ResourceList{},
- expected: api.ResourceList{},
- },
- "toEmpty": {
- a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
- b: api.ResourceList{},
- expected: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
- },
- "matching": {
- a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
- b: api.ResourceList{api.ResourceCPU: resource.MustParse("150m")},
- expected: api.ResourceList{api.ResourceCPU: resource.MustParse("150m")},
- },
- "matching(reverse)": {
- a: api.ResourceList{api.ResourceCPU: resource.MustParse("150m")},
- b: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
- expected: api.ResourceList{api.ResourceCPU: resource.MustParse("150m")},
- },
- }
- for testName, testCase := range testCases {
- sum := Max(testCase.a, testCase.b)
- if result := Equals(testCase.expected, sum); !result {
- t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, sum)
- }
- }
- }
- func TestAdd(t *testing.T) {
- testCases := map[string]struct {
- a api.ResourceList
- b api.ResourceList
- expected api.ResourceList
- }{
- "noKeys": {
- a: api.ResourceList{},
- b: api.ResourceList{},
- expected: api.ResourceList{},
- },
- "toEmpty": {
- a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
- b: api.ResourceList{},
- expected: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
- },
- "matching": {
- a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
- b: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
- expected: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
- },
- }
- for testName, testCase := range testCases {
- sum := Add(testCase.a, testCase.b)
- if result := Equals(testCase.expected, sum); !result {
- t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, sum)
- }
- }
- }
- func TestSubtract(t *testing.T) {
- testCases := map[string]struct {
- a api.ResourceList
- b api.ResourceList
- expected api.ResourceList
- }{
- "noKeys": {
- a: api.ResourceList{},
- b: api.ResourceList{},
- expected: api.ResourceList{},
- },
- "value-empty": {
- a: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
- b: api.ResourceList{},
- expected: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
- },
- "empty-value": {
- a: api.ResourceList{},
- b: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
- expected: api.ResourceList{api.ResourceCPU: resource.MustParse("-100m")},
- },
- "value-value": {
- a: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
- b: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
- expected: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
- },
- }
- for testName, testCase := range testCases {
- sub := Subtract(testCase.a, testCase.b)
- if result := Equals(testCase.expected, sub); !result {
- t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, sub)
- }
- }
- }
- func TestResourceNames(t *testing.T) {
- testCases := map[string]struct {
- a api.ResourceList
- expected []api.ResourceName
- }{
- "empty": {
- a: api.ResourceList{},
- expected: []api.ResourceName{},
- },
- "values": {
- a: api.ResourceList{
- api.ResourceCPU: resource.MustParse("100m"),
- api.ResourceMemory: resource.MustParse("1Gi"),
- },
- expected: []api.ResourceName{api.ResourceMemory, api.ResourceCPU},
- },
- }
- for testName, testCase := range testCases {
- actualSet := ToSet(ResourceNames(testCase.a))
- expectedSet := ToSet(testCase.expected)
- if !actualSet.Equal(expectedSet) {
- t.Errorf("%s expected: %v, actual: %v", testName, expectedSet, actualSet)
- }
- }
- }
- func TestContains(t *testing.T) {
- testCases := map[string]struct {
- a []api.ResourceName
- b api.ResourceName
- expected bool
- }{
- "does-not-contain": {
- a: []api.ResourceName{api.ResourceMemory},
- b: api.ResourceCPU,
- expected: false,
- },
- "does-contain": {
- a: []api.ResourceName{api.ResourceMemory, api.ResourceCPU},
- b: api.ResourceCPU,
- expected: true,
- },
- }
- for testName, testCase := range testCases {
- if actual := Contains(testCase.a, testCase.b); actual != testCase.expected {
- t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, actual)
- }
- }
- }
- func TestIsZero(t *testing.T) {
- testCases := map[string]struct {
- a api.ResourceList
- expected bool
- }{
- "empty": {
- a: api.ResourceList{},
- expected: true,
- },
- "zero": {
- a: api.ResourceList{
- api.ResourceCPU: resource.MustParse("0"),
- api.ResourceMemory: resource.MustParse("0"),
- },
- expected: true,
- },
- "non-zero": {
- a: api.ResourceList{
- api.ResourceCPU: resource.MustParse("200m"),
- api.ResourceMemory: resource.MustParse("1Gi"),
- },
- expected: false,
- },
- }
- for testName, testCase := range testCases {
- if result := IsZero(testCase.a); result != testCase.expected {
- t.Errorf("%s expected: %v, actual: %v", testName, testCase.expected, result)
- }
- }
- }
- func TestIsNegative(t *testing.T) {
- testCases := map[string]struct {
- a api.ResourceList
- expected []api.ResourceName
- }{
- "empty": {
- a: api.ResourceList{},
- expected: []api.ResourceName{},
- },
- "some-negative": {
- a: api.ResourceList{
- api.ResourceCPU: resource.MustParse("-10"),
- api.ResourceMemory: resource.MustParse("0"),
- },
- expected: []api.ResourceName{api.ResourceCPU},
- },
- "all-negative": {
- a: api.ResourceList{
- api.ResourceCPU: resource.MustParse("-200m"),
- api.ResourceMemory: resource.MustParse("-1Gi"),
- },
- expected: []api.ResourceName{api.ResourceCPU, api.ResourceMemory},
- },
- }
- for testName, testCase := range testCases {
- actual := IsNegative(testCase.a)
- actualSet := ToSet(actual)
- expectedSet := ToSet(testCase.expected)
- if !actualSet.Equal(expectedSet) {
- t.Errorf("%s expected: %v, actual: %v", testName, expectedSet, actualSet)
- }
- }
- }
|