Skip to content

Instantly share code, notes, and snippets.

@jlsherrill
Created August 28, 2023 13:31
Show Gist options
  • Save jlsherrill/efe0bd375a46f32ed3a562e130bb8371 to your computer and use it in GitHub Desktop.
Save jlsherrill/efe0bd375a46f32ed3a562e130bb8371 to your computer and use it in GitHub Desktop.
testing for tasks
package main
import (
"encoding/json"
"time"
"github.com/content-services/content-sources-backend/pkg/api"
"github.com/content-services/content-sources-backend/pkg/config"
"github.com/content-services/content-sources-backend/pkg/dao"
"github.com/content-services/content-sources-backend/pkg/db"
"github.com/content-services/content-sources-backend/pkg/models"
"github.com/content-services/content-sources-backend/pkg/seeds"
uuid2 "github.com/google/uuid"
"github.com/openlyinc/pointy"
)
type CleanupTestCase struct {
name string
task models.TaskInfo
beDeleted bool
}
func newTask(orgId string) models.TaskInfo {
var queued = time.Now()
var started = time.Now().Add(time.Minute * 5)
var finished = time.Now().Add(time.Minute * 10)
var taskError = "test task error"
var payload, _ = json.Marshal(map[string]string{"url": "https://example.com"})
var task = models.TaskInfo{
Id: uuid2.New(),
Typename: "test task type",
Payload: payload,
OrgId: orgId,
RepositoryUUID: uuid2.New(),
Token: uuid2.New(),
Queued: &queued,
Started: &started,
Finished: &finished,
Error: &taskError,
Status: "test task status",
}
return task
}
func NewTaskForCleanup(orgId string, taskType string, finishedAt time.Time, status string, repoConfig api.RepositoryResponse) models.TaskInfo {
task := newTask(orgId)
task.Typename = taskType
task.Status = status
task.RepositoryUUID, _ = uuid2.Parse(repoConfig.RepositoryUUID)
task.OrgId = repoConfig.OrgID
task.Finished = pointy.Pointer(finishedAt)
task.Started = pointy.Pointer(finishedAt.Add(-1 * time.Hour))
task.Queued = pointy.Pointer(finishedAt.Add(-2 * time.Hour))
return task
}
func main() {
db.Connect()
d := db.DB
orgId := "Task Test"
seeds.SeedRepositoryConfigurations(d, 2, seeds.SeedOptions{OrgID: orgId})
repoConfigDao := dao.GetRepositoryConfigDao(d)
results, _, _ := repoConfigDao.List(orgId, api.PaginationData{Limit: 2}, api.FilterData{})
repoToKeep := results.Data[0]
repoToDel := results.Data[1]
cases := []CleanupTestCase{
{
name: "oldIntrospect",
task: NewTaskForCleanup(orgId, config.IntrospectTask, time.Now().Add(-32*24*time.Hour),
config.TaskStatusCompleted, repoToKeep),
beDeleted: true,
},
{
name: "newIntrospect",
task: NewTaskForCleanup(orgId, config.IntrospectTask, time.Now().Add(-1*24*time.Hour),
config.TaskStatusCompleted, repoToKeep),
beDeleted: false,
},
{
name: "oldFailedIntrospect",
task: NewTaskForCleanup(orgId, config.IntrospectTask, time.Now().Add(-32*24*time.Hour),
config.TaskStatusFailed, repoToKeep),
beDeleted: true,
},
{
name: "orphanSnapshot",
task: NewTaskForCleanup(orgId, config.RepositorySnapshotTask, time.Now().Add(-1*24*time.Hour),
config.TaskStatusCompleted, repoToDel),
beDeleted: true,
},
{
name: "nonOrphanSnapshot",
task: NewTaskForCleanup(orgId, config.RepositorySnapshotTask, time.Now().Add(-1*24*time.Hour),
config.TaskStatusCompleted, repoToKeep),
beDeleted: false,
},
{
name: "oldDelete",
task: NewTaskForCleanup(orgId, config.DeleteRepositorySnapshotsTask, time.Now().Add(-11*24*time.Hour),
config.TaskStatusCompleted, repoToKeep),
beDeleted: true,
},
{
name: "oldFailedDelete",
task: NewTaskForCleanup(orgId, config.DeleteRepositorySnapshotsTask, time.Now().Add(-32*24*time.Hour),
config.TaskStatusFailed, repoToKeep),
beDeleted: false,
},
{
name: "newDelete",
task: NewTaskForCleanup(orgId, config.DeleteRepositorySnapshotsTask, time.Now().Add(-1*24*time.Hour),
config.TaskStatusCompleted, repoToKeep),
beDeleted: false,
},
}
for _, testCase := range cases {
d.Create(&testCase.task)
}
repoConfigDao.Delete(repoToDel.OrgID, repoToDel.UUID)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment