Skip to content

Commit

Permalink
test: add test for template instance
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt committed Mar 23, 2020
1 parent ddccbd7 commit 0241213
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 191 deletions.
3 changes: 0 additions & 3 deletions engine/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,6 @@ func (a *API) Serve(ctx context.Context) error {
sdk.GoRoutine(ctx, "audit.ComputeWorkflowAudit", func(ctx context.Context) {
audit.ComputeWorkflowAudit(ctx, a.DBConnectionFactory.GetDBMap)
}, a.PanicDump())
sdk.GoRoutine(ctx, "audit.ComputeTemplateAudit", func(ctx context.Context) {
audit.ComputeTemplateAudit(ctx, a.DBConnectionFactory.GetDBMap)
}, a.PanicDump())
sdk.GoRoutine(ctx, "auditCleanerRoutine(ctx", func(ctx context.Context) {
auditCleanerRoutine(ctx, a.DBConnectionFactory.GetDBMap)
})
Expand Down
40 changes: 0 additions & 40 deletions engine/api/audit/audit_template.go

This file was deleted.

53 changes: 0 additions & 53 deletions engine/api/event/publish_workflow_template.go

This file was deleted.

40 changes: 32 additions & 8 deletions engine/api/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,34 @@ func (api *API) postTemplateHandler() service.Handler {
return err
}

tx, err := api.mustDB().Begin()
if err != nil {
return sdk.WithStack(err)
}
defer tx.Rollback() // nolint

// duplicate couple of group id and slug will failed with sql constraint
if err := workflowtemplate.Insert(api.mustDB(), &data); err != nil {
if err := workflowtemplate.Insert(tx, &data); err != nil {
return err
}

newTemplate, err := workflowtemplate.LoadByID(ctx, api.mustDB(), data.ID, workflowtemplate.LoadOptions.Default)
newTemplate, err := workflowtemplate.LoadByID(ctx, tx, data.ID, workflowtemplate.LoadOptions.Default)
if err != nil {
return err
}

event.PublishWorkflowTemplateAdd(ctx, *newTemplate, getAPIConsumer(ctx))
if err := workflowtemplate.CreateAuditAdd(tx, *newTemplate, getAPIConsumer(ctx)); err != nil {
return err
}

if err := workflowtemplate.LoadOptions.WithAudits(ctx, api.mustDB(), newTemplate); err != nil {
if err := workflowtemplate.LoadOptions.WithAudits(ctx, tx, newTemplate); err != nil {
return err
}

if err := tx.Commit(); err != nil {
return sdk.WithStack(err)
}

// aggregate extra data for ui
newTemplate.Editable = true

Expand Down Expand Up @@ -221,6 +233,12 @@ func (api *API) putTemplateHandler() service.Handler {
return sdk.WithStack(sdk.ErrForbidden)
}

tx, err := api.mustDB().Begin()
if err != nil {
return sdk.WithStack(err)
}
defer tx.Rollback() // nolint

// update fields from request data
clone := sdk.WorkflowTemplate(*old)
clone.Update(data)
Expand All @@ -230,21 +248,27 @@ func (api *API) putTemplateHandler() service.Handler {
return err
}

if err := workflowtemplate.Update(api.mustDB(), &clone); err != nil {
if err := workflowtemplate.Update(tx, &clone); err != nil {
return err
}

newTemplate, err := workflowtemplate.LoadByID(ctx, api.mustDB(), clone.ID, workflowtemplate.LoadOptions.Default)
newTemplate, err := workflowtemplate.LoadByID(ctx, tx, clone.ID, workflowtemplate.LoadOptions.Default)
if err != nil {
return err
}

event.PublishWorkflowTemplateUpdate(ctx, *old, *newTemplate, data.ChangeMessage, getAPIConsumer(ctx))
if err := workflowtemplate.CreateAuditUpdate(tx, *old, *newTemplate, data.ChangeMessage, getAPIConsumer(ctx)); err != nil {
return err
}

if err := workflowtemplate.LoadOptions.WithAudits(ctx, api.mustDB(), newTemplate); err != nil {
if err := workflowtemplate.LoadOptions.WithAudits(ctx, tx, newTemplate); err != nil {
return err
}

if err := tx.Commit(); err != nil {
return sdk.WithStack(err)
}

// aggregate extra data for ui
newTemplate.Editable = true

Expand Down
108 changes: 35 additions & 73 deletions engine/api/workflowtemplate/audit.go
Original file line number Diff line number Diff line change
@@ -1,115 +1,77 @@
package workflowtemplate

import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/go-gorp/gorp"

"github.com/ovh/cds/sdk"
)

var (
Audits = map[string]sdk.Audit{
fmt.Sprintf("%T", sdk.EventWorkflowTemplateAdd{}): addWorkflowTemplateAudit{},
fmt.Sprintf("%T", sdk.EventWorkflowTemplateUpdate{}): updateWorkflowTemplateAudit{},
fmt.Sprintf("%T", sdk.EventWorkflowTemplateInstanceAdd{}): addWorkflowTemplateInstanceAudit{},
fmt.Sprintf("%T", sdk.EventWorkflowTemplateInstanceUpdate{}): updateWorkflowTemplateInstanceAudit{},
}
)

type addWorkflowTemplateAudit struct{}

func (a addWorkflowTemplateAudit) Compute(ctx context.Context, db gorp.SqlExecutor, e sdk.Event) error {
var wtEvent sdk.EventWorkflowTemplateAdd
if err := json.Unmarshal(e.Payload, &wtEvent); err != nil {
return sdk.WrapError(err, "Unable to unmarshal payload")
}

// CreateAuditAdd create an audit for template add.
func CreateAuditAdd(db gorp.SqlExecutor, wt sdk.WorkflowTemplate, u sdk.Identifiable) error {
return InsertAudit(db, &sdk.AuditWorkflowTemplate{
AuditCommon: sdk.AuditCommon{
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
EventType: "WorkflowTemplateAdd",
Created: time.Now(),
TriggeredBy: u.GetUsername(),
},
WorkflowTemplateID: wtEvent.WorkflowTemplate.ID,
DataAfter: wtEvent.WorkflowTemplate,
WorkflowTemplateID: wt.ID,
DataAfter: wt,
})
}

type updateWorkflowTemplateAudit struct{}

func (a updateWorkflowTemplateAudit) Compute(ctx context.Context, db gorp.SqlExecutor, e sdk.Event) error {
var wtEvent sdk.EventWorkflowTemplateUpdate
if err := json.Unmarshal(e.Payload, &wtEvent); err != nil {
return sdk.WrapError(err, "Unable to unmarshal payload")
}

// CreateAuditUpdate create an audit for template update.
func CreateAuditUpdate(db gorp.SqlExecutor, oldT, newT sdk.WorkflowTemplate, changeMessage string, u sdk.Identifiable) error {
return InsertAudit(db, &sdk.AuditWorkflowTemplate{
AuditCommon: sdk.AuditCommon{
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
EventType: "WorkflowTemplateUpdate",
Created: time.Now(),
TriggeredBy: u.GetUsername(),
},
WorkflowTemplateID: wtEvent.NewWorkflowTemplate.ID,
ChangeMessage: wtEvent.ChangeMessage,
DataBefore: wtEvent.OldWorkflowTemplate,
DataAfter: wtEvent.NewWorkflowTemplate,
WorkflowTemplateID: newT.ID,
ChangeMessage: changeMessage,
DataBefore: oldT,
DataAfter: newT,
})
}

type addWorkflowTemplateInstanceAudit struct{}

func (a addWorkflowTemplateInstanceAudit) Compute(ctx context.Context, db gorp.SqlExecutor, e sdk.Event) error {
var wtEvent sdk.EventWorkflowTemplateInstanceAdd
if err := json.Unmarshal(e.Payload, &wtEvent); err != nil {
return sdk.WrapError(err, "Unable to unmarshal payload")
}

b, err := json.Marshal(wtEvent.WorkflowTemplateInstance)
// CreateAuditInstanceAdd create an audit for template instance add.
func CreateAuditInstanceAdd(db gorp.SqlExecutor, wti sdk.WorkflowTemplateInstance, u sdk.Identifiable) error {
b, err := json.Marshal(wti)
if err != nil {
return sdk.WrapError(err, "Unable to marshal workflow template instance")
return sdk.WrapError(err, "unable to marshal workflow template instance")
}

return InsertInstanceAudit(db, &sdk.AuditWorkflowTemplateInstance{
AuditCommon: sdk.AuditCommon{
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
EventType: "WorkflowTemplateInstanceAdd",
Created: time.Now(),
TriggeredBy: u.GetUsername(),
},
WorkflowTemplateInstanceID: wtEvent.WorkflowTemplateInstance.ID,
WorkflowTemplateInstanceID: wti.ID,
DataType: "json",
DataAfter: string(b),
})
}

type updateWorkflowTemplateInstanceAudit struct{}

func (a updateWorkflowTemplateInstanceAudit) Compute(ctx context.Context, db gorp.SqlExecutor, e sdk.Event) error {
var wtEvent sdk.EventWorkflowTemplateInstanceUpdate
if err := json.Unmarshal(e.Payload, &wtEvent); err != nil {
return sdk.WrapError(err, "Unable to unmarshal payload")
}

before, err := json.Marshal(wtEvent.OldWorkflowTemplateInstance)
// CreateAuditInstanceUpdate create an audit for template instance update.
func CreateAuditInstanceUpdate(db gorp.SqlExecutor, oldI, newI sdk.WorkflowTemplateInstance, u sdk.Identifiable) error {
before, err := json.Marshal(oldI)
if err != nil {
return sdk.WrapError(err, "Unable to marshal workflow template instance")
return sdk.WrapError(err, "unable to marshal workflow template instance")
}

after, err := json.Marshal(wtEvent.NewWorkflowTemplateInstance)
after, err := json.Marshal(newI)
if err != nil {
return sdk.WrapError(err, "Unable to marshal workflow template instance")
return sdk.WrapError(err, "unable to marshal workflow template instance")
}

return InsertInstanceAudit(db, &sdk.AuditWorkflowTemplateInstance{
AuditCommon: sdk.AuditCommon{
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
EventType: "WorkflowTemplateInstanceUpdate",
Created: time.Now(),
TriggeredBy: u.GetUsername(),
},
WorkflowTemplateInstanceID: wtEvent.NewWorkflowTemplateInstance.ID,
WorkflowTemplateInstanceID: newI.ID,
DataType: "json",
DataBefore: string(before),
DataAfter: string(after),
Expand Down
10 changes: 6 additions & 4 deletions engine/api/workflowtemplate/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package workflowtemplate
import (
"context"

"github.com/ovh/cds/engine/api/event"

"github.com/go-gorp/gorp"

"github.com/ovh/cds/sdk"
Expand All @@ -29,7 +27,9 @@ func Push(ctx context.Context, db gorp.SqlExecutor, wt *sdk.WorkflowTemplate, u
return nil, err
}

event.PublishWorkflowTemplateAdd(ctx, *newTemplate, u)
if err := CreateAuditAdd(db, *newTemplate, u); err != nil {
return nil, err
}

return []sdk.Message{sdk.NewMessage(sdk.MsgWorkflowTemplateImportedInserted, newTemplate.Group.Name, newTemplate.Slug)}, nil
}
Expand All @@ -51,7 +51,9 @@ func Push(ctx context.Context, db gorp.SqlExecutor, wt *sdk.WorkflowTemplate, u
return nil, err
}

event.PublishWorkflowTemplateUpdate(ctx, *old, *newTemplate, "", u)
if err := CreateAuditUpdate(db, *old, *newTemplate, "", u); err != nil {
return nil, err
}

return []sdk.Message{sdk.NewMessage(sdk.MsgWorkflowTemplateImportedUpdated, newTemplate.Group.Name, newTemplate.Slug)}, nil
}
Loading

0 comments on commit 0241213

Please sign in to comment.