@@ -6,8 +6,10 @@ import (
6
6
"testing"
7
7
8
8
"github.com/stretchr/testify/assert"
9
+ appsv1 "k8s.io/api/apps/v1"
9
10
corev1 "k8s.io/api/core/v1"
10
11
"k8s.io/apimachinery/pkg/api/resource"
12
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11
13
12
14
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/olm/overrides/inject"
13
15
)
@@ -1288,3 +1290,184 @@ func TestAffinityAPIChanges(t *testing.T) {
1288
1290
value := reflect .ValueOf (corev1.Affinity {})
1289
1291
assert .Equal (t , 3 , value .NumField (), "It seems the corev1.Affinity API has changed. Please revisit the inject.OverrideDeploymentAffinity implementation" )
1290
1292
}
1293
+
1294
+ func TestInjectAnnotationsIntoDeployment (t * testing.T ) {
1295
+ tests := []struct {
1296
+ name string
1297
+ deployment * appsv1.Deployment
1298
+ annotations map [string ]string
1299
+ expected * appsv1.Deployment
1300
+ }{
1301
+ {
1302
+ // Nil Deployment is injected with annotations
1303
+ // Expected: Deployment is nil
1304
+ name : "WithNilDeployment" ,
1305
+ deployment : nil ,
1306
+ annotations : map [string ]string {"foo" : "bar" },
1307
+ expected : nil , // raises an error
1308
+ },
1309
+ {
1310
+ // Deployment with no Annotations is injected with annotations
1311
+ // Expected: Annotations is empty
1312
+ name : "WithEmptyAnnotations" ,
1313
+ deployment : & appsv1.Deployment {},
1314
+ annotations : map [string ]string {"foo" : "bar" },
1315
+ expected : & appsv1.Deployment {
1316
+ ObjectMeta : metav1.ObjectMeta {
1317
+ Annotations : map [string ]string {"foo" : "bar" },
1318
+ },
1319
+ Spec : appsv1.DeploymentSpec {
1320
+ Template : corev1.PodTemplateSpec {
1321
+ ObjectMeta : metav1.ObjectMeta {
1322
+ Annotations : map [string ]string {
1323
+ "foo" : "bar" ,
1324
+ },
1325
+ },
1326
+ },
1327
+ },
1328
+ },
1329
+ },
1330
+ {
1331
+ // Deployment with existing Annotations is injected with annotations
1332
+ // Expected: Existing Annotations are not overwritten
1333
+ name : "WithExistingAnnotations" ,
1334
+ deployment : & appsv1.Deployment {
1335
+ ObjectMeta : metav1.ObjectMeta {
1336
+ Annotations : map [string ]string {
1337
+ "common" : "default-deploy-annotation" ,
1338
+ "deploy" : "default-deploy-annotation" ,
1339
+ },
1340
+ },
1341
+ Spec : appsv1.DeploymentSpec {
1342
+ Template : corev1.PodTemplateSpec {
1343
+ ObjectMeta : metav1.ObjectMeta {
1344
+ Annotations : map [string ]string {
1345
+ "common" : "default-pod-annotation" ,
1346
+ "pod" : "default-pod-annotation" ,
1347
+ },
1348
+ },
1349
+ },
1350
+ },
1351
+ },
1352
+ annotations : map [string ]string {
1353
+ "common" : "override-annotation" ,
1354
+ "deploy" : "override-annotation" ,
1355
+ "pod" : "override-annotation" ,
1356
+ "foo" : "bar" ,
1357
+ },
1358
+ expected : & appsv1.Deployment {
1359
+ ObjectMeta : metav1.ObjectMeta {
1360
+ Annotations : map [string ]string {
1361
+ // no overrides
1362
+ "common" : "default-deploy-annotation" ,
1363
+ "deploy" : "default-deploy-annotation" ,
1364
+ // there was no default for "pod" on the deployment
1365
+ "pod" : "override-annotation" ,
1366
+ "foo" : "bar" ,
1367
+ },
1368
+ },
1369
+ Spec : appsv1.DeploymentSpec {
1370
+ Template : corev1.PodTemplateSpec {
1371
+ ObjectMeta : metav1.ObjectMeta {
1372
+ Annotations : map [string ]string {
1373
+ // no overrides
1374
+ "common" : "default-pod-annotation" ,
1375
+ "pod" : "default-pod-annotation" ,
1376
+ // there was no default for "deploy" on the pod
1377
+ "deploy" : "override-annotation" ,
1378
+ "foo" : "bar" ,
1379
+ },
1380
+ },
1381
+ },
1382
+ },
1383
+ },
1384
+ },
1385
+ {
1386
+ // Existing Deployment is left alone if annotations is nil
1387
+ // Expected: Deployment is not changed
1388
+ name : "WithNilAnnotations" ,
1389
+ deployment : & appsv1.Deployment {
1390
+ ObjectMeta : metav1.ObjectMeta {
1391
+ Annotations : map [string ]string {
1392
+ "deploy" : "default-annotation" ,
1393
+ },
1394
+ },
1395
+ Spec : appsv1.DeploymentSpec {
1396
+ Template : corev1.PodTemplateSpec {
1397
+ ObjectMeta : metav1.ObjectMeta {
1398
+ Annotations : map [string ]string {
1399
+ "pod" : "default-annotation" ,
1400
+ },
1401
+ },
1402
+ },
1403
+ },
1404
+ },
1405
+ annotations : nil ,
1406
+ expected : & appsv1.Deployment {
1407
+ ObjectMeta : metav1.ObjectMeta {
1408
+ Annotations : map [string ]string {
1409
+ "deploy" : "default-annotation" ,
1410
+ },
1411
+ },
1412
+ Spec : appsv1.DeploymentSpec {
1413
+ Template : corev1.PodTemplateSpec {
1414
+ ObjectMeta : metav1.ObjectMeta {
1415
+ Annotations : map [string ]string {
1416
+ "pod" : "default-annotation" ,
1417
+ },
1418
+ },
1419
+ },
1420
+ },
1421
+ },
1422
+ },
1423
+ {
1424
+ // Existing Deployment retains its annotations if the annotations is an empty map
1425
+ // Expected: Deployment annotations are not changed
1426
+ name : "WithEmptyAnnotations" ,
1427
+ deployment : & appsv1.Deployment {
1428
+ ObjectMeta : metav1.ObjectMeta {
1429
+ Annotations : map [string ]string {
1430
+ "deploy" : "default-annotation" ,
1431
+ },
1432
+ },
1433
+ Spec : appsv1.DeploymentSpec {
1434
+ Template : corev1.PodTemplateSpec {
1435
+ ObjectMeta : metav1.ObjectMeta {
1436
+ Annotations : map [string ]string {
1437
+ "pod" : "default-annotation" ,
1438
+ },
1439
+ },
1440
+ },
1441
+ },
1442
+ },
1443
+ annotations : map [string ]string {},
1444
+ expected : & appsv1.Deployment {
1445
+ ObjectMeta : metav1.ObjectMeta {
1446
+ Annotations : map [string ]string {
1447
+ "deploy" : "default-annotation" ,
1448
+ },
1449
+ },
1450
+ Spec : appsv1.DeploymentSpec {
1451
+ Template : corev1.PodTemplateSpec {
1452
+ ObjectMeta : metav1.ObjectMeta {
1453
+ Annotations : map [string ]string {
1454
+ "pod" : "default-annotation" ,
1455
+ },
1456
+ },
1457
+ },
1458
+ },
1459
+ },
1460
+ },
1461
+ }
1462
+
1463
+ for _ , tt := range tests {
1464
+ t .Run (tt .name , func (t * testing.T ) {
1465
+ inject .InjectAnnotationsIntoDeployment (tt .deployment , tt .annotations )
1466
+
1467
+ podSpecWant := tt .expected
1468
+ podSpecGot := tt .deployment
1469
+
1470
+ assert .Equal (t , podSpecWant , podSpecGot )
1471
+ })
1472
+ }
1473
+ }
0 commit comments