Skip to content

Commit

Permalink
fix: lint and ut
Browse files Browse the repository at this point in the history
Signed-off-by: xu.zhu <[email protected]>
  • Loading branch information
xuzhu-591 committed Sep 19, 2023
1 parent 6d40918 commit 98dcedf
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 17 deletions.
4 changes: 3 additions & 1 deletion core/controller/cluster/controller_basic_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
userauth "github.com/horizoncd/horizon/pkg/authentication/user"
clustergitrepo "github.com/horizoncd/horizon/pkg/cluster/gitrepo"
"github.com/horizoncd/horizon/pkg/cluster/models"
eventmodels "github.com/horizoncd/horizon/pkg/event/models"
"github.com/horizoncd/horizon/pkg/git"
groupmodels "github.com/horizoncd/horizon/pkg/group/models"
membermodels "github.com/horizoncd/horizon/pkg/member/models"
Expand All @@ -31,7 +32,7 @@ func TestCreatePipelineRun(t *testing.T) {
if err := db.AutoMigrate(&appmodels.Application{}, &models.Cluster{},
&regionmodels.Region{}, &membermodels.Member{}, &registrymodels.Registry{},
&prmodels.Pipelinerun{}, &groupmodels.Group{}, &prmodels.Check{},
&usermodel.User{}); err != nil {
&usermodel.User{}, &eventmodels.Event{}); err != nil {
panic(err)
}
param := managerparam.InitManager(db)
Expand Down Expand Up @@ -61,6 +62,7 @@ func TestCreatePipelineRun(t *testing.T) {
regionMgr: param.RegionMgr,
clusterGitRepo: mockClusterGitRepo,
commitGetter: mockGitGetter,
eventMgr: param.EventMgr,
}

_, err := param.UserMgr.Create(ctx, &usermodel.User{
Expand Down
6 changes: 4 additions & 2 deletions core/controller/pipelinerun/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ type Controller interface {
Cancel(ctx context.Context, pipelinerunID uint) error

ListCheckRuns(ctx context.Context, q *q.Query) ([]*prmodels.CheckRun, error)
CreateCheckRun(ctx context.Context, pipelineRunID uint, request *CreateOrUpdateCheckRunRequest) (*prmodels.CheckRun, error)
CreateCheckRun(ctx context.Context, pipelineRunID uint,
request *CreateOrUpdateCheckRunRequest) (*prmodels.CheckRun, error)
ListPRMessages(ctx context.Context, pipelineRunID uint, q *q.Query) (int, []*PrMessage, error)
CreatePRMessage(ctx context.Context, pipelineRunID uint, request *CreatePrMessageRequest) (*prmodels.PRMessage, error)
}
Expand Down Expand Up @@ -336,7 +337,8 @@ func (c *controller) CreateCheck(ctx context.Context, check *prmodels.Check) (*p
return c.prMgr.Check.Create(ctx, check)
}

func (c *controller) UpdateCheckRunByID(ctx context.Context, checkRunID uint, request *CreateOrUpdateCheckRunRequest) error {
func (c *controller) UpdateCheckRunByID(ctx context.Context, checkRunID uint,
request *CreateOrUpdateCheckRunRequest) error {
const op = "pipelinerun controller: update check run"
defer wlog.Start(ctx, op).StopPrint()

Expand Down
63 changes: 59 additions & 4 deletions core/controller/pipelinerun/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,9 @@ func TestExecutePipelineRun(t *testing.T) {

func TestCheckRun(t *testing.T) {
db, _ := orm.NewSqliteDB("")
if err := db.AutoMigrate(&prmodels.CheckRun{}); err != nil {
if err := db.AutoMigrate(&groupmodels.Group{}, &membermodels.Member{}, &applicationmodel.Application{},
&clustermodel.Cluster{}, &prmodels.CheckRun{}, &prmodels.Pipelinerun{},
&usermodel.User{}, &prmodels.Check{}); err != nil {
panic(err)
}
param := managerparam.InitManager(db)
Expand All @@ -536,19 +538,72 @@ func TestCheckRun(t *testing.T) {
})

ctrl := controller{
prMgr: param.PRMgr,
clusterMgr: param.ClusterMgr,
prMgr: param.PRMgr,
prSvc: prservice.NewService(param),
}

_, err := ctrl.CreateCheckRun(ctx, 1, &CreateOrUpdateCheckRunRequest{
_, err := param.UserMgr.Create(ctx, &usermodel.User{
Name: "Tony",
})
assert.NoError(t, err)

group, err := param.GroupMgr.Create(ctx, &groupmodels.Group{
Name: "test",
})
assert.NoError(t, err)

app, err := param.ApplicationMgr.Create(ctx, &applicationmodel.Application{
Name: "test",
GroupID: group.ID,
}, nil)
assert.NoError(t, err)

cluster, err := param.ClusterMgr.Create(ctx, &clustermodel.Cluster{
Name: "cluster",
ApplicationID: app.ID,
}, nil, nil)
assert.NoError(t, err)

// create a check under cluster
check, err := ctrl.CreateCheck(ctx, &pipelinemodel.Check{
Resource: common.Resource{
ResourceID: cluster.ID,
Type: "clusters",
},
})

pr, err := ctrl.prMgr.PipelineRun.Create(ctx, &prmodels.Pipelinerun{
ID: 1,
Status: string(prmodels.StatusPending),
ClusterID: cluster.ID,
})
assert.NoError(t, err)

_, err = ctrl.CreateCheckRun(ctx, pr.ID, &CreateOrUpdateCheckRunRequest{
Name: "test",
Status: string(prmodels.CheckStatusQueue),
CheckID: check.ID,
Message: "hello",
DetailURL: "https://www.google.com",
})
assert.NoError(t, err)

err = ctrl.UpdateCheckRunByID(ctx, 1, &CreateOrUpdateCheckRunRequest{
Status: string(prmodels.CheckStatusSuccess),
})
assert.NoError(t, err)

cr, err := ctrl.GetCheckRunByID(ctx, 1)
assert.NoError(t, err)
assert.Equal(t, string(cr.Status), string(prmodels.CheckStatusSuccess))

prInDB, err := ctrl.GetPipelinerun(ctx, pr.ID)
assert.NoError(t, err)
assert.Equal(t, string(prInDB.Status), string(prmodels.StatusReady))

keyWords := make(map[string]interface{})
keyWords[common.CheckrunQueryByPipelinerunID] = 1
keyWords[common.CheckrunQueryByPipelinerunID] = strconv.Itoa(int(pr.ID))
query := q.New(keyWords)
checkRuns, err := ctrl.ListCheckRuns(ctx, query)
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion core/http/api/v2/cluster/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (api *API) RegisterRoute(engine *gin.Engine) {
HandlerFunc: api.DeleteFavorite,
}, {
Method: http.MethodPost,
Pattern: fmt.Sprintf("/clusters/:%v/pipelinerun", common.ParamClusterID),
Pattern: fmt.Sprintf("/clusters/:%v/pipelineruns", common.ParamClusterID),
HandlerFunc: api.CreatePipelineRun,
},
}
Expand Down
1 change: 0 additions & 1 deletion core/http/api/v2/pipelinerun/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ func (a *API) ListCheckRuns(c *gin.Context) {
return
}
response.SuccessWithData(c, checkRuns)

}

func (a *API) CreateCheckRun(c *gin.Context) {
Expand Down
6 changes: 3 additions & 3 deletions core/http/api/v2/pipelinerun/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (api *API) RegisterRoute(engine *gin.Engine) {
},
{
Method: http.MethodGet,
Pattern: fmt.Sprintf("/checkruns"),
Pattern: "/checkruns",
HandlerFunc: api.ListCheckRuns,
},
{
Expand All @@ -86,12 +86,12 @@ func (api *API) RegisterRoute(engine *gin.Engine) {
},
{
Method: http.MethodGet,
Pattern: fmt.Sprintf("/pipelineruns/:%v/message", _pipelinerunIDParam),
Pattern: fmt.Sprintf("/pipelineruns/:%v/messages", _pipelinerunIDParam),
HandlerFunc: api.ListPrMessages,
},
{
Method: http.MethodPost,
Pattern: fmt.Sprintf("/pipelineruns/:%v/message", _pipelinerunIDParam),
Pattern: fmt.Sprintf("/pipelineruns/:%v/messages", _pipelinerunIDParam),
HandlerFunc: api.CreatePrMessage,
},
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/go-redis/redis/v8 v8.3.3
github.com/golang-jwt/jwt/v4 v4.4.3
github.com/golang/mock v1.6.0
github.com/google/go-containerregistry v0.1.3 // indirect
github.com/google/go-containerregistry v0.1.3
github.com/google/go-github/v41 v41.0.0
github.com/google/uuid v1.2.0
github.com/gorilla/mux v1.8.0
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,6 @@ github.com/matttproud/golang_protobuf_extensions v1.0.0/go.mod h1:D8He9yQNgCq6Z5
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2 h1:g+4J5sZg6osfvEfkRZxJ1em0VT95/UOZgi/l7zi1/oE=
github.com/maxbrunsfeld/counterfeiter/v6 v6.2.2/go.mod h1:eD9eIE7cdwcMi9rYluz88Jz2VyhSmden33/aXg4oVIY=
github.com/mbilski/exhaustivestruct v1.1.0/go.mod h1:OeTBVxQWoEmB2J2JCHmXWPJ0aksxSUOUy+nvtVEfzXc=
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b h1:j7+1HpAFS1zy5+Q4qx1fWh90gTKwiN4QCGoY9TWyyO4=
Expand Down Expand Up @@ -2588,7 +2587,6 @@ k8s.io/client-go v0.20.10 h1:TgAL2pqcNWMH4eZoS9Uw0BLh2lu5a2A4pmegjp5pmsk=
k8s.io/client-go v0.20.10/go.mod h1:fFg+aLoasv/R+xiVaWjxeqGFYltzgQcOQzkFaSRfnJ0=
k8s.io/cloud-provider v0.20.10/go.mod h1:8HQ0NgW661PiH+QK1BPYD0QorpaxOXQs1ZsMfOe3uc0=
k8s.io/cluster-bootstrap v0.20.10/go.mod h1:D+cqd8iJYeajaIUBUca4J3f/87L4qiFvMPzM5qs8x1o=
k8s.io/code-generator v0.20.10 h1:xENIQkVW8G2chwm0/HNwNIR3gcbF2XADK+jVS9/UCTE=
k8s.io/code-generator v0.20.10/go.mod h1:i6FmG+QxaLxvJsezvZp0q/gAEzzOz3U53KFibghWToU=
k8s.io/component-base v0.20.10 h1:QNlekT6M2zBb4feHHmZ+YHZHcDbhbrYS7xHHY+v+kOE=
k8s.io/component-base v0.20.10/go.mod h1:ZKOEin1xu68aJzxgzl5DZSp5J1IrjAOPlPN90/t6OI8=
Expand All @@ -2602,7 +2600,6 @@ k8s.io/gengo v0.0.0-20191108084044-e500ee069b5c/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8
k8s.io/gengo v0.0.0-20200205140755-e0e292d8aa12/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/gengo v0.0.0-20200428234225-8167cfdcfc14/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/gengo v0.0.0-20201113003025-83324d819ded h1:JApXBKYyB7l9xx+DK7/+mFjC7A9Bt5A93FPvFD0HIFE=
k8s.io/gengo v0.0.0-20201113003025-83324d819ded/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/heapster v1.2.0-beta.1/go.mod h1:h1uhptVXMwC8xtZBYsPXKVi8fpdlYkTs6k949KozGrM=
k8s.io/helm v2.17.0+incompatible h1:Bpn6o1wKLYqKM3+Osh8e+1/K2g/GsQJ4F4yNF2+deao=
Expand Down
2 changes: 1 addition & 1 deletion pkg/eventhandler/wlgenerator/wlgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (w *WebhookLogGenerator) Process(ctx context.Context, events []*models.Even
} else if !ok {
continue
}
log.Debugf(ctx, "event %d matches webhook %d", event.ID, webhook.URL)
log.Debugf(ctx, "event %d matches webhook %s", event.ID, webhook.URL)
// 3.2 add webhook to the list
if _, ok := conditionsToCreate[event.ID]; !ok {
conditionsToCreate[event.ID] = map[uint]messageDependency{}
Expand Down

0 comments on commit 98dcedf

Please sign in to comment.