Longevity test- delete backup after n days (#3790)
<!-- PR description--> Longevity Test- Delete the backups older than n no of days. Currently n's value is 5. --- #### Does this PR need a docs update or release note? - [x] ⛔ No #### Type of change <!--- Please check the type of change your PR introduces: ---> - [ ] 🤖 Supportability/Tests #### Issue(s) <!-- Can reference multiple issues. Use one of the following "magic words" - "closes, fixes" to auto-close the Github issue. --> * https://github.com/alcionai/corso/issues/3679 #### Test Plan <!-- How will this be tested prior to merging.--> - [x] ⚡ Unit test
This commit is contained in:
parent
1872f81d70
commit
dd325211f3
62
.github/workflows/longevity_test.yml
vendored
62
.github/workflows/longevity_test.yml
vendored
@ -3,9 +3,6 @@ on:
|
|||||||
schedule:
|
schedule:
|
||||||
# Run every day at 04:00 GMT (roughly 8pm PST)
|
# Run every day at 04:00 GMT (roughly 8pm PST)
|
||||||
- cron: "0 4 * * *"
|
- cron: "0 4 * * *"
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- longetivityTest
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
user:
|
user:
|
||||||
@ -54,22 +51,22 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
fetch-depth: 0 # needed to get latest tag
|
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
|
- name: Setup Golang with cache
|
||||||
uses: magnetikonline/action-golang-cache@v4
|
uses: magnetikonline/action-golang-cache@v4
|
||||||
with:
|
with:
|
||||||
go-version-file: src/go.mod
|
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}
|
- 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
|
# Maintenance test
|
||||||
- name: Maintenance test Daily
|
- name: Maintenance test Daily
|
||||||
id: maintenance-test-daily
|
id: maintenance-test-daily
|
||||||
|
|||||||
79
src/cmd/longevity_test/longevity.go
Normal file
79
src/cmd/longevity_test/longevity.go
Normal file
@ -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...)
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user