Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Add unit test for gc artifact #339

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ migration-create: ## Create a new migration file
@migrate create -dir ./pkg/dal/migrations/mysql -seq -digits 4 -ext sql $(MIGRATION_NAME)

sql-format: ## Format all sql files
@find ${PWD}/pkg/dal/migrations -type f -iname "*.sql" -print | xargs pg_format -s 2 --inplace
@find ${PWD}/pkg -type f -iname "*.sql" -print | xargs pg_format -s 2 --inplace

changelog: ## Generate changelog
@docker run -v "${PWD}":/workdir quay.io/git-chglog/git-chglog:latest --next-tag $(VERSION) -o CHANGELOG.md
Expand Down
1 change: 1 addition & 0 deletions pkg/daemon/gc/gc_artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type gcArtifact struct {

func (g gcArtifact) Run(runnerID int64) error {
defer close(g.runnerChan)
defer close(g.webhookChan)
g.runnerChan <- decoratorStatus{Daemon: enums.DaemonGcArtifact, Status: enums.TaskCommonStatusDoing, Started: true}

var err error
Expand Down
184 changes: 111 additions & 73 deletions pkg/daemon/gc/gc_artifact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,76 +14,114 @@

package gc

// import (
// "context"
// "testing"
// "time"

// "github.com/rs/zerolog/log"
// "github.com/spf13/viper"
// "github.com/stretchr/testify/assert"

// "github.com/go-sigma/sigma/pkg/dal"
// "github.com/go-sigma/sigma/pkg/dal/dao"
// "github.com/go-sigma/sigma/pkg/dal/models"
// "github.com/go-sigma/sigma/pkg/logger"
// "github.com/go-sigma/sigma/pkg/tests"
// "github.com/go-sigma/sigma/pkg/types/enums"
// "github.com/go-sigma/sigma/pkg/utils/ptr"
// )

// func TestGcArtifact(t *testing.T) {
// viper.SetDefault("log.level", "debug")
// logger.SetLevel("debug")
// assert.NoError(t, tests.Initialize(t))
// assert.NoError(t, tests.DB.Init())
// defer func() {
// conn, err := dal.DB.DB()
// assert.NoError(t, err)
// assert.NoError(t, conn.Close())
// assert.NoError(t, tests.DB.DeInit())
// }()

// ctx := log.Logger.WithContext(context.Background())

// namespaceServiceFactory := dao.NewNamespaceServiceFactory()
// repositoryServiceFactory := dao.NewRepositoryServiceFactory()
// artifactServiceFactory := dao.NewArtifactServiceFactory()
// userServiceFactory := dao.NewUserServiceFactory()

// userService := userServiceFactory.New()
// userObj := &models.User{Username: "gc-artifact", Password: ptr.Of("test"), Email: ptr.Of("test@gmail.com")}
// err := userService.Create(ctx, userObj)
// assert.NoError(t, err)

// namespaceService := namespaceServiceFactory.New()
// namespaceObj := &models.Namespace{Name: "test", Visibility: enums.VisibilityPrivate}
// err = namespaceService.Create(ctx, namespaceObj)
// assert.NoError(t, err)

// repositoryService := repositoryServiceFactory.New()
// repositoryObj := &models.Repository{Name: "test/busybox", NamespaceID: namespaceObj.ID, Visibility: enums.VisibilityPrivate}
// err = repositoryService.Create(ctx, repositoryObj, dao.AutoCreateNamespace{UserID: userObj.ID})
// assert.NoError(t, err)

// artifactService := artifactServiceFactory.New()
// artifactObj := &models.Artifact{
// RepositoryID: repositoryObj.ID,
// Digest: "sha256:812535778d12027c8dd62a23e0547009560b2710c7da7ea2cd83a935ccb525ba",
// Size: 123,
// ContentType: "test",
// Raw: []byte("test"),
// CreatedAt: time.Now().Add(time.Hour * 73 * -1),
// UpdatedAt: time.Now().Add(time.Hour * 73 * -1),
// }
// err = artifactService.Create(ctx, artifactObj)
// assert.NoError(t, err)

// g := gc{
// namespaceServiceFactory: namespaceServiceFactory,
// repositoryServiceFactory: repositoryServiceFactory,
// artifactServiceFactory: artifactServiceFactory,
// }
// err = g.gcArtifact(ctx, "")
// assert.NoError(t, err)
// }
import (
"context"
"fmt"
"os"
"strings"
"testing"

"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"

"github.com/go-sigma/sigma/pkg/dal"
"github.com/go-sigma/sigma/pkg/dal/dao"
"github.com/go-sigma/sigma/pkg/logger"
"github.com/go-sigma/sigma/pkg/tests"
"github.com/go-sigma/sigma/pkg/types/enums"
)

func TestGcArtifactNormal(t *testing.T) {
viper.SetDefault("log.level", "debug")
logger.SetLevel("debug")
assert.NoError(t, tests.Initialize(t))
assert.NoError(t, tests.DB.Init())
defer func() {
conn, err := dal.DB.DB()
assert.NoError(t, err)
assert.NoError(t, conn.Close())
assert.NoError(t, tests.DB.DeInit())
}()

ctx := log.Logger.WithContext(context.Background())

sql, err := os.ReadFile(fmt.Sprintf("./testdata/gc_artifact_normal.%s.sql", tests.DB.GetName()))
assert.NoError(t, err)

for _, s := range strings.Split(string(sql), ";\n") {
s := strings.TrimSpace(s)
if len(s) == 0 {
continue
}
err = dal.DB.Debug().Exec(s).Error
assert.NoError(t, err)
}

var runnerChan = make(chan decoratorStatus, 4)
var webhookChan = make(chan decoratorWebhook, 4)

runner := initGc(ctx, enums.DaemonGcArtifact, runnerChan, webhookChan)
err = runner.Run(1)
assert.NoError(t, err)

var webhookArr = make([]string, 0, 10)
for status := range webhookChan {
webhookArr = append(webhookArr, string(status.Meta.Action))
}
assert.Equal(t, []string{"Started", "Finished"}, webhookArr)

var statusArr = make([]string, 0, 10)
for status := range runnerChan {
statusArr = append(statusArr, string(status.Status))
}
assert.Equal(t, []string{"Doing", "Doing", "Success"}, statusArr)

artifactService := dao.NewArtifactServiceFactory().New()
_, err = artifactService.Get(ctx, 5)
assert.Equal(t, gorm.ErrRecordNotFound, err)
artifact1, err := artifactService.Get(ctx, 1)
assert.NoError(t, err)
assert.Equal(t, "application/vnd.oci.image.manifest.v1+json", artifact1.ContentType)
assert.Equal(t, int64(1), artifact1.ID)
artifact2, err := artifactService.Get(ctx, 2)
assert.NoError(t, err)
assert.Equal(t, "application/vnd.oci.image.manifest.v1+json", artifact1.ContentType)
assert.Equal(t, int64(2), artifact2.ID)
artifact3, err := artifactService.Get(ctx, 3)
assert.NoError(t, err)
assert.Equal(t, "application/vnd.oci.image.manifest.v1+json", artifact1.ContentType)
assert.Equal(t, int64(3), artifact3.ID)
artifact4, err := artifactService.Get(ctx, 4)
assert.NoError(t, err)
assert.Equal(t, "application/vnd.oci.image.manifest.v1+json", artifact1.ContentType)
assert.Equal(t, int64(4), artifact4.ID)

runnerChan = make(chan decoratorStatus, 4)
webhookChan = make(chan decoratorWebhook, 4)
runner = initGc(ctx, enums.DaemonGcArtifact, runnerChan, webhookChan)
err = runner.Run(1)
assert.NoError(t, err)

webhookArr = make([]string, 0, 10)
for status := range webhookChan {
webhookArr = append(webhookArr, string(status.Meta.Action))
}
assert.Equal(t, []string{"Started", "Finished"}, webhookArr)

statusArr = make([]string, 0, 10)
for status := range runnerChan {
statusArr = append(statusArr, string(status.Status))
}
assert.Equal(t, []string{"Doing", "Doing", "Success"}, statusArr)

_, err = artifactService.Get(ctx, 1)
assert.Equal(t, gorm.ErrRecordNotFound, err)
_, err = artifactService.Get(ctx, 2)
assert.Equal(t, gorm.ErrRecordNotFound, err)
_, err = artifactService.Get(ctx, 3)
assert.Equal(t, gorm.ErrRecordNotFound, err)
_, err = artifactService.Get(ctx, 4)
assert.Equal(t, gorm.ErrRecordNotFound, err)
}
Loading
Loading