From 0946096f00226cb73ee7ed1cba86a60c7d97fc70 Mon Sep 17 00:00:00 2001 From: "xu.zhu" Date: Mon, 25 Sep 2023 14:57:46 +0800 Subject: [PATCH] fix: lint and ut Signed-off-by: xu.zhu --- core/controller/application/controller.go | 6 ++++-- core/controller/application/controller_test.go | 6 ++++++ core/controller/cluster/controller_basic_v2.go | 6 ++++-- core/controller/cluster/controller_test.go | 3 +++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/core/controller/application/controller.go b/core/controller/application/controller.go index 83ef0294..8306336f 100644 --- a/core/controller/application/controller.go +++ b/core/controller/application/controller.go @@ -884,7 +884,9 @@ func (c *controller) recordMemberCreatedEvent(ctx context.Context, applicationID }, }) } - if _, err := c.eventMgr.CreateEvent(ctx, events...); err != nil { - log.Warningf(ctx, "failed to create event, err: %s", err.Error()) + if len(events) > 0 { + if _, err := c.eventMgr.CreateEvent(ctx, events...); err != nil { + log.Warningf(ctx, "failed to create event, err: %s", err.Error()) + } } } diff --git a/core/controller/application/controller_test.go b/core/controller/application/controller_test.go index f18b2d59..85c8ddf4 100644 --- a/core/controller/application/controller_test.go +++ b/core/controller/application/controller_test.go @@ -41,6 +41,7 @@ import ( tmodels "github.com/horizoncd/horizon/pkg/template/models" trmodels "github.com/horizoncd/horizon/pkg/templaterelease/models" trschema "github.com/horizoncd/horizon/pkg/templaterelease/schema" + usermodel "github.com/horizoncd/horizon/pkg/user/models" userservice "github.com/horizoncd/horizon/pkg/user/service" "github.com/golang/mock/gomock" @@ -293,6 +294,9 @@ func TestMain(m *testing.M) { if err := db.AutoMigrate(&eventmodels.Event{}); err != nil { panic(err) } + if err := db.AutoMigrate(&usermodel.User{}); err != nil { + panic(err) + } ctx = context.TODO() ctx = context.WithValue(ctx, common.UserContextKey(), &userauth.DefaultInfo{ Name: "Tony", @@ -363,6 +367,7 @@ func Test(t *testing.T) { clusterMgr: manager.ClusterMgr, userSvc: userservice.NewService(manager), eventMgr: manager.EventMgr, + memberManager: manager.MemberMgr, } group, err := manager.GroupMgr.Create(ctx, &groupmodels.Group{ @@ -518,6 +523,7 @@ func TestV2(t *testing.T) { clusterMgr: manager.ClusterMgr, userSvc: userservice.NewService(manager), eventMgr: manager.EventMgr, + memberManager: manager.MemberMgr, } group, err := manager.GroupMgr.Create(ctx, &groupmodels.Group{ diff --git a/core/controller/cluster/controller_basic_v2.go b/core/controller/cluster/controller_basic_v2.go index 7365e200..1bd4ff6c 100644 --- a/core/controller/cluster/controller_basic_v2.go +++ b/core/controller/cluster/controller_basic_v2.go @@ -844,8 +844,10 @@ func (c *controller) recordMemberCreatedEvent(ctx context.Context, clusterID uin }, }) } - if _, err := c.eventMgr.CreateEvent(ctx, events...); err != nil { - log.Warningf(ctx, "failed to create event, err: %s", err.Error()) + if len(events) > 0 { + if _, err := c.eventMgr.CreateEvent(ctx, events...); err != nil { + log.Warningf(ctx, "failed to create event, err: %s", err.Error()) + } } } diff --git a/core/controller/cluster/controller_test.go b/core/controller/cluster/controller_test.go index 29183df6..a0c5bd4d 100644 --- a/core/controller/cluster/controller_test.go +++ b/core/controller/cluster/controller_test.go @@ -654,6 +654,7 @@ func test(t *testing.T) { tagMgr: tagManager, applicationGitRepo: applicationGitRepo, eventMgr: manager.EventMgr, + memberManager: manager.MemberMgr, tokenSvc: tokenservice.NewService(manager, tokenconfig.Config{ JwtSigningKey: "horizon", CallbackTokenExpireIn: time.Hour * 2, @@ -1421,6 +1422,7 @@ func testV2(t *testing.T) { registryFty: registryFty, cd: mockCd, eventMgr: manager.EventMgr, + memberManager: manager.MemberMgr, } applicationGitRepo.EXPECT().GetApplication(gomock.Any(), applicationName, gomock.Any()). Return(&appgitrepo.GetResponse{ @@ -1647,6 +1649,7 @@ func testUpgrade(t *testing.T) { cd: mockCd, eventMgr: manager.EventMgr, templateUpgradeMapper: templateUpgradeMapper, + memberManager: manager.MemberMgr, } applicationGitRepo.EXPECT().GetApplication(ctx, gomock.Any(), gomock.Any()).