@@ -3,7 +3,9 @@ package main
3
3
import (
4
4
"testing"
5
5
6
+ "k8s.io/apimachinery/pkg/api/errors"
6
7
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8
+ "k8s.io/apimachinery/pkg/util/intstr"
7
9
"k8s.io/client-go/kubernetes"
8
10
"k8s.io/client-go/kubernetes/fake"
9
11
"k8s.io/client-go/pkg/api/v1"
@@ -82,6 +84,107 @@ func TestRun(t *testing.T) {
82
84
stopCh <- struct {}{}
83
85
}
84
86
87
+ func TestRemoveInvalidPDBs (t * testing.T ) {
88
+ deplabels := map [string ]string {"foo" : "deployment" }
89
+ sslabels := map [string ]string {"foo" : "statefulset" }
90
+ replicas := int32 (2 )
91
+
92
+ one := intstr .FromInt (1 )
93
+ pdbs := []* pv1beta1.PodDisruptionBudget {
94
+ {
95
+ ObjectMeta : metav1.ObjectMeta {
96
+ Name : "pdb-1" ,
97
+ Labels : ownerLabels ,
98
+ },
99
+ Spec : pv1beta1.PodDisruptionBudgetSpec {
100
+ Selector : & metav1.LabelSelector {
101
+ MatchLabels : deplabels ,
102
+ },
103
+ MinAvailable : & one ,
104
+ },
105
+ },
106
+ {
107
+ ObjectMeta : metav1.ObjectMeta {
108
+ Name : "pdb-2" ,
109
+ Labels : ownerLabels ,
110
+ },
111
+ Spec : pv1beta1.PodDisruptionBudgetSpec {
112
+ Selector : & metav1.LabelSelector {
113
+ MatchLabels : sslabels ,
114
+ },
115
+ MinAvailable : & one ,
116
+ },
117
+ },
118
+ }
119
+
120
+ deployments := []* v1beta1.Deployment {
121
+ {
122
+ ObjectMeta : metav1.ObjectMeta {
123
+ Name : "deployment-1" ,
124
+ Labels : deplabels ,
125
+ },
126
+ Spec : v1beta1.DeploymentSpec {
127
+ Replicas : & replicas ,
128
+ Selector : & metav1.LabelSelector {
129
+ MatchLabels : deplabels ,
130
+ },
131
+ Template : v1.PodTemplateSpec {
132
+ ObjectMeta : metav1.ObjectMeta {
133
+ Labels : deplabels ,
134
+ },
135
+ },
136
+ },
137
+ },
138
+ }
139
+
140
+ statefulSets := []* v1beta1.StatefulSet {
141
+ {
142
+ ObjectMeta : metav1.ObjectMeta {
143
+ Name : "stateful-set-1" ,
144
+ Labels : sslabels ,
145
+ },
146
+ Spec : v1beta1.StatefulSetSpec {
147
+ Replicas : & replicas ,
148
+ Selector : & metav1.LabelSelector {
149
+ MatchLabels : sslabels ,
150
+ },
151
+ Template : v1.PodTemplateSpec {
152
+ ObjectMeta : metav1.ObjectMeta {
153
+ Labels : sslabels ,
154
+ },
155
+ },
156
+ },
157
+ },
158
+ }
159
+
160
+ namespaces := []* v1.Namespace {
161
+ {
162
+ ObjectMeta : metav1.ObjectMeta {
163
+ Name : "default" ,
164
+ },
165
+ },
166
+ }
167
+
168
+ controller := & PDBController {
169
+ Interface : setupMockKubernetes (t , pdbs , deployments , statefulSets , namespaces ),
170
+ }
171
+
172
+ err := controller .addPDBs (namespaces [0 ])
173
+ if err != nil {
174
+ t .Error (err )
175
+ }
176
+
177
+ for _ , pdb := range []string {"pdb-1" , "pdb-2" } {
178
+ pdbResource , err := controller .Interface .PolicyV1beta1 ().PodDisruptionBudgets ("default" ).Get (pdb , metav1.GetOptions {})
179
+ if err == nil {
180
+ t .Fatalf ("unexpected pdb (%s) found: %v" , pdb , pdbResource )
181
+ }
182
+ if ! errors .IsNotFound (err ) {
183
+ t .Fatalf ("unexpected error: %s" , err )
184
+ }
185
+ }
186
+ }
187
+
85
188
func TestAddPDBs (t * testing.T ) {
86
189
labels := map [string ]string {"foo" : "bar" }
87
190
notFoundLabels := map [string ]string {"bar" : "foo" }
0 commit comments