diff --git a/.github/workflows/longevity_test.yml b/.github/workflows/longevity_test.yml index a2fa3b95c..02b956be1 100644 --- a/.github/workflows/longevity_test.yml +++ b/.github/workflows/longevity_test.yml @@ -3,9 +3,6 @@ on: schedule: # Run every day at 04:00 GMT (roughly 8pm PST) - cron: "0 4 * * *" - push: - branches: - - longetivityTest workflow_dispatch: inputs: user: @@ -54,22 +51,22 @@ jobs: with: fetch-depth: 0 # needed to get latest tag - - name: Get version string - id: version - run: | - echo version=$(git describe --tags --abbrev=0) | tee -a $GITHUB_OUTPUT - - - uses: actions/checkout@v3 - with: - ref: ${{ steps.version.outputs.version }} - - name: Setup Golang with cache uses: magnetikonline/action-golang-cache@v4 with: go-version-file: src/go.mod - - run: go build -o corso + - run: go build -o longevity-test ./cmd/longevity_test + - name: Get version string + id: version + run: | + echo version=$(git describe --tags --abbrev=0) | tee -a $GITHUB_OUTPUT + + - run: git checkout ${{ steps.version.outputs.version }} + + - run: go build -o corso + - run: mkdir ${CORSO_LOG_DIR} ########################################################################################################################################## @@ -194,6 +191,45 @@ jobs: ########################################################################################################################################## +# Backup Exchange Deletion test + - name: Backup Delete exchange test + id: delete-exchange-test + env: + SERVICE: "exchange" + DELETION_DAYS: 5 + run: | + set -euo pipefail + echo -e "\nDelete Backup exchange \n" >> ${CORSO_LOG_FILE} + ./longevity-test + +########################################################################################################################################## + +# Backup Onedrive Deletion test + - name: Backup Delete onedrive test + id: delete-onedrive-test + env: + SERVICE: "onedrive" + DELETION_DAYS: 5 + run: | + set -euo pipefail + echo -e "\nDelete Backup onedrive \n" >> ${CORSO_LOG_FILE} + ./longevity-test + +########################################################################################################################################## + +# Backup Sharepoint Deletion test + - name: Backup Delete Sharepoint test + id: delete-sharepoint-test + env: + SERVICE: "sharepoint" + DELETION_DAYS: 5 + run: | + set -euo pipefail + echo -e "\nDelete Backup sharepoint \n" >> ${CORSO_LOG_FILE} + ./longevity-test + +########################################################################################################################################## + # Maintenance test - name: Maintenance test Daily id: maintenance-test-daily diff --git a/src/cmd/longevity_test/longevity.go b/src/cmd/longevity_test/longevity.go new file mode 100644 index 000000000..37e01ae02 --- /dev/null +++ b/src/cmd/longevity_test/longevity.go @@ -0,0 +1,79 @@ +package main + +import ( + "context" + "fmt" + "os" + "strconv" + "time" + + "github.com/spf13/cobra" + + "github.com/alcionai/corso/src/cli/config" + "github.com/alcionai/corso/src/cli/utils" + "github.com/alcionai/corso/src/pkg/logger" + "github.com/alcionai/corso/src/pkg/path" + "github.com/alcionai/corso/src/pkg/store" +) + +func main() { + var ( + service path.ServiceType + cc = cobra.Command{} + ) + + cc.SetContext(context.Background()) + + if err := config.InitFunc(&cc, []string{}); err != nil { + return + } + + switch serviceName := os.Getenv("SERVICE"); serviceName { + case "exchange": + service = path.ExchangeService + case "onedrive": + service = path.OneDriveService + case "sharepoint": + service = path.SharePointService + default: + fatal(cc.Context(), "unknown service", nil) + } + + r, _, _, err := utils.GetAccountAndConnect(cc.Context(), service, nil) + if err != nil { + fatal(cc.Context(), "unable to connect account", err) + } + + defer r.Close(cc.Context()) + + backups, err := r.BackupsByTag(cc.Context(), store.Service(service)) + if err != nil { + fatal(cc.Context(), "unable to find backups", err) + } + + days, err := strconv.Atoi(os.Getenv("DELETION_DAYS")) + if err != nil { + fatal(cc.Context(), "invalid no of days provided", nil) + } + + for _, backup := range backups { + if backup.StartAndEndTime.CompletedAt.Before(time.Now().AddDate(0, 0, -days)) { + if err := r.DeleteBackup(cc.Context(), backup.ID.String()); err != nil { + fatal(cc.Context(), "deleting backup", err) + } + + logAndPrint(cc.Context(), "Deleted backup %s", backup.ID.String()) + } + } +} + +func fatal(ctx context.Context, msg string, err error) { + logger.CtxErr(ctx, err).Error("test failure: " + msg) + fmt.Println(msg+": ", err) + os.Exit(1) +} + +func logAndPrint(ctx context.Context, tmpl string, vs ...any) { + logger.Ctx(ctx).Infof(tmpl, vs...) + fmt.Printf(tmpl+"\n", vs...) +}