Skip to content

Commit

Permalink
cmd/osbuild-service-maintenance: add test for filtering SIs
Browse files Browse the repository at this point in the history
  • Loading branch information
croissanne committed Oct 23, 2024
1 parent 04a5ca6 commit 661f39c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
17 changes: 12 additions & 5 deletions cmd/osbuild-service-maintenance/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
52 changes: 52 additions & 0 deletions cmd/osbuild-service-maintenance/aws_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions cmd/osbuild-service-maintenance/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

var FilterReservations = filterReservations

0 comments on commit 661f39c

Please sign in to comment.