Skip to content

Commit

Permalink
add TestCase for GC
Browse files Browse the repository at this point in the history
  • Loading branch information
haiqingchq committed Nov 22, 2024
1 parent 823e646 commit 9edf416
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions internal/apps/dop/dicehub/release/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package release
import (
"encoding/json"
"fmt"
"gopkg.in/stretchr/testify.v1/assert"
"io"
"os"
"reflect"
Expand All @@ -27,6 +28,7 @@ import (
"bou.ke/monkey"
"github.com/DATA-DOG/go-sqlmock"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"

"github.com/erda-project/erda-proto-go/core/dicehub/release/pb"
"github.com/erda-project/erda/apistructs"
Expand Down Expand Up @@ -572,3 +574,83 @@ version: "2.0"
})
}
}

func TestReleaseServiceGC(t *testing.T) {
testCase := []struct {
name string
dbPath string
dbEngine string
releases []db.Release
}{
{
name: "baseTest",
dbPath: "./testdata/test.db",
dbEngine: "sqlite3",
releases: []db.Release{
{
ReleaseID: "1",
ReleaseName: "chenhaiqing",
Desc: "",
Dice: "",
Addon: "",
Changelog: "",
IsStable: false,
IsFormal: false,
IsProjectRelease: false,
Modes: "",
Labels: "",
GitBranch: "",
Version: "",
OrgID: 0,
ProjectID: 1,
ApplicationID: 2,
ProjectName: "chenhaiqing",
ApplicationName: "chenhaiqing",
UserID: "",
ClusterName: "",
Resources: "",
Reference: 0,
CrossCluster: false,
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
IsLatest: false,
},
},
},
}
for _, tt := range testCase {
t.Run(tt.name, func(t *testing.T) {
if _, err := os.Stat(tt.dbPath); os.IsNotExist(err) {
_, err = os.Create(tt.dbPath)
if err != nil {
t.Fatal(err)
}
}
dbClient, err := gorm.Open("sqlite3", "./testdata/test.db")
if err != nil {
t.Fatal(err)
}
if !dbClient.HasTable(&db.Release{}) {
dbClient.CreateTable(db.Release{})
}
rs := ReleaseService{
db: &db.ReleaseConfigDB{
DB: dbClient,
},
Config: &releaseConfig{MaxTimeReserved: "72"},
}
// insert data into db to test
for _, release := range tt.releases {
rs.db.Create(&release)
}
now := time.Now()
err = rs.RemoveDeprecatedsReleases(now)
assert.NoError(t, err)
dbClient.Exec("delete from dice_release")
err = os.Remove(tt.dbPath)
if err != nil {
t.Fatal(err)
}
})
}
}

0 comments on commit 9edf416

Please sign in to comment.