77
88 "github.com/stretchr/testify/assert"
99 "github.com/stretchr/testify/require"
10+ corev1 "k8s.io/api/core/v1"
1011 apierrors "k8s.io/apimachinery/pkg/api/errors"
1112 "k8s.io/apimachinery/pkg/api/resource"
1213 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -17,6 +18,24 @@ import (
1718 "sigs.k8s.io/controller-runtime/pkg/client/fake"
1819)
1920
21+ var (
22+ fooPod = podFromResources ("foo1" , "foo" , podResource {
23+ containers : []containerResources {
24+ {cpu : "1" , memory : "1Gi" },
25+ {cpu : "2" , memory : "1Gi" },
26+ },
27+ phase : corev1 .PodRunning ,
28+ })
29+ foo2Pod = podFromResources ("foo2" , "foo" , podResource {
30+ containers : []containerResources {{memory : "1Gi" }},
31+ phase : corev1 .PodRunning ,
32+ })
33+ foobarPod = podFromResources ("foo" , "bar" , podResource {
34+ containers : []containerResources {{memory : "1337Gi" }},
35+ phase : corev1 .PodRunning ,
36+ })
37+ )
38+
2039func TestRatioValidator_Handle (t * testing.T ) {
2140 ctx := context .Background ()
2241 tests := map [string ]struct {
@@ -36,16 +55,9 @@ func TestRatioValidator_Handle(t *testing.T) {
3655 "Fetch_Namespace" : {
3756 namespace : "foo" ,
3857 objects : []client.Object {
39- podFromResources ("foo1" , "foo" , []containerResources {
40- {cpu : "1" , memory : "1Gi" },
41- {cpu : "2" , memory : "1Gi" },
42- }),
43- podFromResources ("foo2" , "foo" , []containerResources {
44- {memory : "1Gi" },
45- }),
46- podFromResources ("foo" , "bar" , []containerResources {
47- {memory : "1337Gi" },
48- }),
58+ fooPod ,
59+ foo2Pod ,
60+ foobarPod ,
4961 },
5062 memory : "3Gi" ,
5163 cpu : "3" ,
@@ -63,32 +75,23 @@ func TestRatioValidator_Handle(t *testing.T) {
6375 "Fetch_OtherNamespace" : {
6476 namespace : "bar" ,
6577 objects : []client.Object {
66- podFromResources ("foo1" , "foo" , []containerResources {
67- {cpu : "1" , memory : "1Gi" },
68- {cpu : "2" , memory : "1Gi" },
69- }),
70- podFromResources ("foo2" , "foo" , []containerResources {
71- {memory : "1Gi" },
72- }),
73- podFromResources ("foo" , "bar" , []containerResources {
74- {memory : "1337Gi" },
75- }),
78+ fooPod ,
79+ foo2Pod ,
80+ foobarPod ,
7681 },
7782 memory : "1337Gi" ,
7883 cpu : "0" ,
7984 },
8085 "Fetch_WronglyDisabledNamespace" : {
8186 namespace : "notdisabled-bar" ,
8287 objects : []client.Object {
83- podFromResources ("foo1" , "foo" , []containerResources {
84- {cpu : "1" , memory : "1Gi" },
85- {cpu : "2" , memory : "1Gi" },
86- }),
87- podFromResources ("foo2" , "foo" , []containerResources {
88- {memory : "1Gi" },
89- }),
90- podFromResources ("foo" , "notdisabled-bar" , []containerResources {
91- {memory : "1337Gi" },
88+ fooPod ,
89+ foo2Pod ,
90+ podFromResources ("foo" , "notdisabled-bar" , podResource {
91+ containers : []containerResources {
92+ {memory : "1337Gi" },
93+ },
94+ phase : corev1 .PodRunning ,
9295 }),
9396 },
9497 memory : "1337Gi" ,
@@ -98,31 +101,33 @@ func TestRatioValidator_Handle(t *testing.T) {
98101 "Fetch_DisabledNamespace" : {
99102 namespace : "disabled-bar" ,
100103 objects : []client.Object {
101- podFromResources ("foo1" , "foo" , []containerResources {
102- {cpu : "1" , memory : "1Gi" },
103- {cpu : "2" , memory : "1Gi" },
104- }),
105- podFromResources ("foo2" , "foo" , []containerResources {
106- {memory : "1Gi" },
107- }),
108- podFromResources ("foo" , "disabled-bar" , []containerResources {
109- {memory : "1337Gi" },
104+ fooPod ,
105+ foo2Pod ,
106+ podFromResources ("foo" , "disabled-bar" , podResource {
107+ containers : []containerResources {
108+ {memory : "1337Gi" },
109+ },
110+ phase : corev1 .PodRunning ,
110111 }),
111112 },
112113 err : ErrorDisabled ,
113114 },
114115 "Fetch_OtherDisabledNamespace" : {
115116 namespace : "disabled-foo" ,
116117 objects : []client.Object {
117- podFromResources ("foo1" , "disabled-foo" , []containerResources {
118- {cpu : "1" , memory : "1Gi" },
119- {cpu : "2" , memory : "1Gi" },
120- }),
121- podFromResources ("foo2" , "foo" , []containerResources {
122- {memory : "1Gi" },
123- }),
124- podFromResources ("foo" , "disabled-bar" , []containerResources {
125- {memory : "1337Gi" },
118+ podFromResources ("foo1" , "disabled-foo" , podResource {
119+ containers : []containerResources {
120+ {cpu : "1" , memory : "1Gi" },
121+ {cpu : "2" , memory : "1Gi" },
122+ },
123+ phase : corev1 .PodRunning ,
124+ }),
125+ foo2Pod ,
126+ podFromResources ("foo" , "disabled-bar" , podResource {
127+ containers : []containerResources {
128+ {memory : "1337Gi" },
129+ },
130+ phase : corev1 .PodRunning ,
126131 }),
127132 },
128133 err : ErrorDisabled ,
@@ -131,15 +136,13 @@ func TestRatioValidator_Handle(t *testing.T) {
131136 namespace : "foo" ,
132137 orgLabel : "appuio.io/org" ,
133138 objects : []client.Object {
134- podFromResources ("foo1" , "foo" , []containerResources {
135- {cpu : "1" , memory : "1Gi" },
136- {cpu : "2" , memory : "1Gi" },
137- }),
138- podFromResources ("foo2" , "foo" , []containerResources {
139- {memory : "1Gi" },
140- }),
141- podFromResources ("foo" , "disabled-bar" , []containerResources {
142- {memory : "1337Gi" },
139+ fooPod ,
140+ foo2Pod ,
141+ podFromResources ("foo" , "disabled-bar" , podResource {
142+ containers : []containerResources {
143+ {memory : "1337Gi" },
144+ },
145+ phase : corev1 .PodRunning ,
143146 }),
144147 },
145148 err : ErrorDisabled ,
@@ -148,16 +151,20 @@ func TestRatioValidator_Handle(t *testing.T) {
148151 namespace : "org" ,
149152 orgLabel : "appuio.io/org" ,
150153 objects : []client.Object {
151- podFromResources ("foo1" , "org" , []containerResources {
152- {cpu : "1" , memory : "1Gi" },
153- {cpu : "2" , memory : "1Gi" },
154- }),
155- podFromResources ("foo2" , "org" , []containerResources {
156- {memory : "1Gi" },
157- }),
158- podFromResources ("foo" , "bar" , []containerResources {
159- {memory : "1337Gi" },
160- }),
154+ podFromResources ("foo1" , "org" , podResource {
155+ containers : []containerResources {
156+ {cpu : "1" , memory : "1Gi" },
157+ {cpu : "2" , memory : "1Gi" },
158+ },
159+ phase : corev1 .PodRunning ,
160+ }),
161+ podFromResources ("foo2" , "org" , podResource {
162+ containers : []containerResources {
163+ {memory : "1Gi" },
164+ },
165+ phase : corev1 .PodRunning ,
166+ }),
167+ foobarPod ,
161168 },
162169 memory : "3Gi" ,
163170 cpu : "3" ,
0 commit comments