From 24e256c2258badbf084717be60dad7547526ec85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Sch=C3=BCller?= Date: Wed, 4 Dec 2024 12:47:57 +0100 Subject: [PATCH] osbuild-service-maintenance: add test for `allTerminated` HMS-3632 --- cmd/osbuild-service-maintenance/aws_test.go | 76 +++++++++++++++++++ .../export_test.go | 1 + 2 files changed, 77 insertions(+) diff --git a/cmd/osbuild-service-maintenance/aws_test.go b/cmd/osbuild-service-maintenance/aws_test.go index 494c03c76..507e34151 100644 --- a/cmd/osbuild-service-maintenance/aws_test.go +++ b/cmd/osbuild-service-maintenance/aws_test.go @@ -125,3 +125,79 @@ func TestCheckValidParent(t *testing.T) { require.Equal(t, tc.result, main.CheckValidParent("testChildId", tc.parent)) } } + +func TestAllTerminated(t *testing.T) { + tests := + []struct { + reservations []ec2types.Reservation + result bool + }{ + // no reservations + { + reservations: []ec2types.Reservation{}, + result: true, + }, + // no instances + { + reservations: []ec2types.Reservation{ + {Instances: []ec2types.Instance{}}, + }, + result: true, + }, + { + reservations: []ec2types.Reservation{ + {Instances: []ec2types.Instance{{}, {}}}, + }, + result: true, + }, + { + reservations: []ec2types.Reservation{ + {Instances: []ec2types.Instance{{ + State: &ec2types.InstanceState{ + Name: ec2types.InstanceStateNamePending, + }, + }}}, + }, + result: false, + }, + { + reservations: []ec2types.Reservation{ + {Instances: []ec2types.Instance{{ + State: &ec2types.InstanceState{ + Name: ec2types.InstanceStateNameRunning, + }, + }}}, + }, + result: false, + }, + { + reservations: []ec2types.Reservation{ + {Instances: []ec2types.Instance{{ + State: &ec2types.InstanceState{ + Name: ec2types.InstanceStateNameRunning, + }, + }, + { + State: &ec2types.InstanceState{ + Name: ec2types.InstanceStateNameTerminated, + }, + }, + }}, + }, + result: false, + }, + { + reservations: []ec2types.Reservation{ + {Instances: []ec2types.Instance{{ + State: &ec2types.InstanceState{ + Name: ec2types.InstanceStateNameTerminated, + }, + }}}, + }, + result: true, + }, + } + for _, tc := range tests { + require.Equal(t, tc.result, main.AllTerminated(tc.reservations)) + } +} diff --git a/cmd/osbuild-service-maintenance/export_test.go b/cmd/osbuild-service-maintenance/export_test.go index 563072b44..a0e9b65b6 100644 --- a/cmd/osbuild-service-maintenance/export_test.go +++ b/cmd/osbuild-service-maintenance/export_test.go @@ -2,3 +2,4 @@ package main var FilterOnTooOld = filterOnTooOld var CheckValidParent = checkValidParent +var AllTerminated = allTerminated