1
16
17 package create
18
19 import (
20 "testing"
21
22 corev1 "k8s.io/api/core/v1"
23 apiequality "k8s.io/apimachinery/pkg/api/equality"
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 )
26
27 func TestCreateQuota(t *testing.T) {
28 hards := []string{"cpu=1", "cpu=1,pods=42"}
29 var resourceQuotaSpecLists []corev1.ResourceList
30 for _, hard := range hards {
31 resourceQuotaSpecList, err := populateResourceListV1(hard)
32 if err != nil {
33 t.Errorf("unexpected error: %v", err)
34 }
35 resourceQuotaSpecLists = append(resourceQuotaSpecLists, resourceQuotaSpecList)
36 }
37
38 tests := map[string]struct {
39 options *QuotaOpts
40 expected *corev1.ResourceQuota
41 }{
42 "single resource": {
43 options: &QuotaOpts{
44 Name: "my-quota",
45 Hard: hards[0],
46 Scopes: "",
47 },
48 expected: &corev1.ResourceQuota{
49 TypeMeta: metav1.TypeMeta{
50 Kind: "ResourceQuota",
51 APIVersion: "v1",
52 },
53 ObjectMeta: metav1.ObjectMeta{
54 Name: "my-quota",
55 },
56 Spec: corev1.ResourceQuotaSpec{
57 Hard: resourceQuotaSpecLists[0],
58 },
59 },
60 },
61 "single resource with a scope": {
62 options: &QuotaOpts{
63 Name: "my-quota",
64 Hard: hards[0],
65 Scopes: "BestEffort",
66 },
67 expected: &corev1.ResourceQuota{
68 TypeMeta: metav1.TypeMeta{
69 Kind: "ResourceQuota",
70 APIVersion: "v1",
71 },
72 ObjectMeta: metav1.ObjectMeta{
73 Name: "my-quota",
74 },
75 Spec: corev1.ResourceQuotaSpec{
76 Hard: resourceQuotaSpecLists[0],
77 Scopes: []corev1.ResourceQuotaScope{"BestEffort"},
78 },
79 },
80 },
81 "multiple resources": {
82 options: &QuotaOpts{
83 Name: "my-quota",
84 Hard: hards[1],
85 Scopes: "BestEffort",
86 },
87 expected: &corev1.ResourceQuota{
88 TypeMeta: metav1.TypeMeta{
89 Kind: "ResourceQuota",
90 APIVersion: "v1",
91 },
92 ObjectMeta: metav1.ObjectMeta{
93 Name: "my-quota",
94 },
95 Spec: corev1.ResourceQuotaSpec{
96 Hard: resourceQuotaSpecLists[1],
97 Scopes: []corev1.ResourceQuotaScope{"BestEffort"},
98 },
99 },
100 },
101 "single resource with multiple scopes": {
102 options: &QuotaOpts{
103 Name: "my-quota",
104 Hard: hards[0],
105 Scopes: "BestEffort,NotTerminating",
106 },
107 expected: &corev1.ResourceQuota{
108 TypeMeta: metav1.TypeMeta{
109 Kind: "ResourceQuota",
110 APIVersion: "v1",
111 },
112 ObjectMeta: metav1.ObjectMeta{
113 Name: "my-quota",
114 },
115 Spec: corev1.ResourceQuotaSpec{
116 Hard: resourceQuotaSpecLists[0],
117 Scopes: []corev1.ResourceQuotaScope{"BestEffort", "NotTerminating"},
118 },
119 },
120 },
121 }
122
123 for name, tc := range tests {
124 t.Run(name, func(t *testing.T) {
125 resourceQuota, err := tc.options.createQuota()
126 if err != nil {
127 t.Errorf("unexpected error:\n%#v\n", err)
128 return
129 }
130 if !apiequality.Semantic.DeepEqual(resourceQuota, tc.expected) {
131 t.Errorf("expected:\n%#v\ngot:\n%#v", tc.expected, resourceQuota)
132 }
133 })
134 }
135 }
136
View as plain text