Skip to content

Commit

Permalink
fix and test the query
Browse files Browse the repository at this point in the history
  • Loading branch information
parametalol committed Sep 13, 2023
1 parent bc4c810 commit 8998b8f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 4 additions & 6 deletions internal/dinosaur/pkg/services/dinosaur.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,12 +505,10 @@ func (k *dinosaurService) DeprovisionExpiredDinosaurs(dinosaurAgeInHours int) *e
dbConn := k.connectionFactory.New().
Model(&dbapi.CentralRequest{})

// WHERE ( instance_type = 'eval' AND created_at <= ? OR grace_from IS NOT NULL AND grace_from < ? ) AND status NOT IN ( 'deleting', 'deprovision' )
dbConn = dbConn.Where(
dbConn.Where("instance_type = ?", types.EVAL.String()).
Where("created_at <= ?", now.Add(-1*time.Duration(dinosaurAgeInHours)*time.Hour)).
Or(dbConn.Where("grace_from IS NOT NULL").
Where("grace_from < ", now.Add(-gracePeriod)))).
dbConn = dbConn.Where(dbConn.
Where("instance_type = ?", types.EVAL.String()).
Where("created_at <= ?", now.Add(-1*time.Duration(dinosaurAgeInHours)*time.Hour)).
Or("grace_from IS NOT NULL").Where("grace_from < ?", now.Add(-gracePeriod))).
Where("status NOT IN (?)", dinosaurDeletionStatuses)

db := dbConn.Updates(map[string]interface{}{
Expand Down
15 changes: 15 additions & 0 deletions internal/dinosaur/pkg/services/dinosaur_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stackrox/acs-fleet-manager/pkg/api"
"github.com/stackrox/acs-fleet-manager/pkg/auth"
"github.com/stackrox/acs-fleet-manager/pkg/db"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
)

Expand Down Expand Up @@ -172,3 +173,17 @@ func Test_dinosaurService_Get(t *testing.T) {
})
}
}

func Test_dinosaurService_DeprovisionExpiredDinosaursQuery(t *testing.T) {
m := mocket.Catcher.Reset().NewMock().WithQuery(`UPDATE "central_requests" ` +
`SET "deletion_timestamp"=$1,"status"=$2,"updated_at"=$3 WHERE ` +
`(instance_type = $4 AND created_at <= $5 OR grace_from IS NOT NULL AND grace_from < $6) ` +
`AND status NOT IN ($7,$8) AND "central_requests"."deleted_at" IS NULL`).
OneTime()
k := &dinosaurService{
connectionFactory: db.NewMockConnectionFactory(nil),
}
svcErr := k.DeprovisionExpiredDinosaurs(0)
assert.Nil(t, svcErr)
assert.True(t, m.Triggered)
}

0 comments on commit 8998b8f

Please sign in to comment.