cmd/osbuild-service-maintenance: add test for filtering SIs

This commit is contained in:
Sanne Raymaekers 2024-10-22 17:24:50 +02:00
parent 04a5ca6965
commit 661f39cbb9
3 changed files with 67 additions and 5 deletions

View file

@ -6,6 +6,7 @@ import (
"sync"
"time"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/sirupsen/logrus"
"golang.org/x/sync/semaphore"
@ -88,6 +89,16 @@ func AWSCleanup(maxConcurrentRequests int, dryRun bool, accessKeyID, accessKey s
if err != nil {
return fmt.Errorf("Unable to describe instances by tag %w", err)
}
instanceIDs := filterReservations(reservations)
err = a.TerminateInstances(instanceIDs)
if err != nil {
return fmt.Errorf("Unable to terminate secure instances: %w", err)
}
return nil
}
func filterReservations(reservations []ec2types.Reservation) []string {
var instanceIDs []string
for _, res := range reservations {
for _, i := range res.Instances {
@ -96,9 +107,5 @@ func AWSCleanup(maxConcurrentRequests int, dryRun bool, accessKeyID, accessKey s
}
}
}
err = a.TerminateInstances(instanceIDs)
if err != nil {
return fmt.Errorf("Unable to terminate secure instances: %w", err)
}
return nil
return instanceIDs
}

View file

@ -0,0 +1,52 @@
package main_test
import (
"testing"
"time"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/stretchr/testify/require"
main "github.com/osbuild/osbuild-composer/cmd/osbuild-service-maintenance"
"github.com/osbuild/osbuild-composer/internal/common"
)
func TestFilterReservations(t *testing.T) {
reservations := []ec2types.Reservation{
{
Instances: []ec2types.Instance{
{
LaunchTime: common.ToPtr(time.Now().Add(-time.Hour * 24)),
InstanceId: common.ToPtr("not filtered"),
},
},
},
{
Instances: []ec2types.Instance{
{
LaunchTime: common.ToPtr(time.Now().Add(-time.Minute * 121)),
InstanceId: common.ToPtr("not filtered"),
},
},
},
{
Instances: []ec2types.Instance{
{
LaunchTime: common.ToPtr(time.Now().Add(-time.Minute * 119)),
InstanceId: common.ToPtr("filtered"),
},
},
},
{
Instances: []ec2types.Instance{
{
LaunchTime: common.ToPtr(time.Now()),
InstanceId: common.ToPtr("filtered"),
},
},
},
}
instanceIDs := main.FilterReservations(reservations)
require.Equal(t, []string{"not filtered", "not filtered"}, instanceIDs)
}

View file

@ -0,0 +1,3 @@
package main
var FilterReservations = filterReservations