8000 fix(api): mutex & conditions on manual run (#4726) · ovh/cds@7afd34e · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 7afd34e

Browse files
yesnaultbnjjj
authored andcommitted
fix(api): mutex & conditions on manual run (#4726)
close #4305 Signed-off-by: Yvonnick Esnault <yvonnick.esnault@corp.ovh.com>
1 parent c0b8218 commit 7afd34e

File tree

4 files changed

+100
-9
lines changed

4 files changed

+100
-9
lines changed

engine/api/workflow/process_node.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func processNode(ctx context.Context, db gorp.SqlExecutor, store cache.Store, pr
251251

252252
// CONDITION
253253
if !checkCondition(wr, n.Context.Conditions, nr.BuildParameters) {
254-
log.Debug("Condition failed %d/%d %+v", wr.ID, n.ID, nr.BuildParameters)
254+
log.Debug("Condition failed on processNode %d/%d %+v", wr.ID, n.ID, nr.BuildParameters)
255255
return nil, false, nil
256256
}
257257

@@ -373,8 +373,9 @@ func processNode(ctx context.Context, db gorp.SqlExecutor, store cache.Store, pr
373373
return nil, false, sdk.WrapError(err, "unable to update workflow run")
374374
}
375375

376-
//Mutex is locked. exit without error
377-
return report, false, nil
376+
// Mutex is locked, but it is as the workflow is ok to be run (conditions ok).
377+
// it's ok exit without error
378+
return report, true, nil
378379
}
379380
//Mutex is free, continue
380381
}

engine/api/workflow/process_outgoinghook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func processNodeOutGoingHook(ctx context.Context, db gorp.SqlExecutor, store cac
131131
}
132132

133133
if !checkCondition(wr, node.Context.Conditions, hookRun.BuildParameters) {
134-
log.Debug("Condition failed %d/%d %+v", wr.ID, node.ID, hookRun.BuildParameters)
134+
log.Debug("Condition failed on processNodeOutGoingHook %d/%d %+v", wr.ID, node.ID, hookRun.BuildParameters)
135135
return report, false, nil
136136
}
137137

engine/api/workflow/run_workflow.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ func runFromHook(ctx context.Context, db gorp.SqlExecutor, store cache.Store, p
6767
if errWR != nil {
6868
return nil, sdk.WrapError(errWR, "RunFromHook> Unable to process workflow run")
6969
}
70-
// if there is no report and the wf was not run -> condition not ok, set NeverBuilt
71-
// if it was not run due to a mutex -> the report is not nil in this case
72-
// so, we let the wf with status building by returning the report with no error
73-
if r1 == nil && !hasRun {
70+
if !hasRun {
7471
wr.Status = sdk.StatusNeverBuilt.String()
7572
wr.LastExecution = time.Now()
7673
report.Add(wr)

engine/api/workflow_run_test.go

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ func Test_postWorkflowRunHandlerWithoutRightConditionsOnHook(t *testing.T) {
15191519
assert.Equal(t, 400, rec.Code)
15201520
}
15211521

1522-
func Test_postWorkflowRunHandlerWithMutex(t *testing.T) {
1522+
func Test_postWorkflowRunHandlerHookWithMutex(t *testing.T) {
15231523
api, db, router, end := newTestAPI(t, bootstrap.InitiliazeDB)
15241524
defer end()
15251525
u, pass := assets.InsertAdminUser(api.mustDB())
@@ -1750,6 +1750,99 @@ func Test_postWorkflowRunHandler_Forbidden(t *testing.T) {
17501750
router.Mux.ServeHTTP(rec, req)
17511751
assert.Equal(t, 404, rec.Code)
17521752
}
1753+
1754+
func Test_postWorkflowRunHandler_ConditionNotOK(t *testing.T) {
1755+
api, db, router, end := newTestAPI(t, bootstrap.InitiliazeDB)
1756+
defer end()
1757+
u, pass := assets.InsertAdminUser(api.mustDB())
1758+
key := sdk.RandomString(10)
1759+
proj := assets.InsertTestProject(t, db, api.Cache, key, key, u)
1760+
1761+
gr := &sdk.Group{
1762+
Name: sdk.RandomString(10),
1763+
}
1764+
test.NoError(t, group.InsertGroup(db, gr))
1765+
test.NoError(t, group.InsertGroupInProject(api.mustDB(), proj.ID, gr.ID, 7))
1766+
1767+
//First pipeline
1768+
pip := sdk.Pipeline{
1769+
ProjectID: proj.ID,
1770+
ProjectKey: proj.Key,
1771+
Name: "pip1",
1772+
}
1773+
test.NoError(t, pipeline.InsertPipeline(api.mustDB(), api.Cache, proj, &pip, u))
1774+
1775+
env := &sdk.Environment{
1776+
Name: sdk.RandomString(10),
1777+
ProjectKey: proj.Key,
1778+
ProjectID: proj.ID,
1779+
}
1780+
test.NoError(t, environment.InsertEnvironment(api.mustDB(), env))
1781+
1782+
proj2, errp := project.Load(api.mustDB(), api.Cache, proj.Key, u, project.LoadOptions.WithPipelines, project.LoadOptions.WithEnvironments)
1783+
test.NoError(t, errp)
1784+
1785+
w := sdk.Workflow{
1786+
Name: "test_1",
1787+
ProjectID: proj.ID,
1788+
ProjectKey: proj.Key,
1789+
WorkflowData: &sdk.WorkflowData{
1790+
Node: sdk.Node{
1791+
Name: "root",
1792+
Type: sdk.NodeTypePipeline,
1793+
Context: &sdk.NodeContext{
1794+
PipelineID: pip.ID,
1795+
EnvironmentID: env.ID,
1796+
Conditions: sdk.WorkflowNodeConditions{
1797+
LuaScript: "return false",
1798+
},
1799+
},
1800+
},
1801+
},
1802+
}
1803+
1804+
test.NoError(t, workflow.Insert(api.mustDB(), api.Cache, &w, proj2, u))
1805+
1806+
test.NoError(t, user.UpdateUser(api.mustDB(), *u))
1807+
1808+
//Prepare request
1809+
vars := map[string]string{
1810+
"key": proj.Key,
1811+
"permWorkflowName": w.Name,
1812+
}
1813+
uri := router.GetRoute("POST", api.postWorkflowRunHandler, vars)
1814+
test.NotEmpty(t, uri)
1815+
1816+
opts := &sdk.WorkflowRunPostHandlerOption{
1817+
Manual: &sdk.WorkflowNodeRunManual{
1818+
Payload: map[string]string{"foo": "bar"},
1819+
},
1820+
}
1821+
req := assets.NewAuthentifiedRequest(t, u, pass, "POST", uri, opts)
1822+
1823+
//Do the request
1824+
rec := httptest.NewRecorder()
1825+
router.Mux.ServeHTTP(rec, req)
1826+
1827+
assert.Equal(t, 202, rec.Code)
1828+
1829+
// it's an async call, wait a bit the let cds take care of the previous request
1830+
time.Sleep(3 * time.Second)
1831+
1832+
lastRun, err := workflow.LoadLastRun(api.mustDB(), proj.Key, w.Name, workflow.LoadRunOptions{})
1833+
test.NoError(t, err)
1834+
assert.Equal(t, int64(1), lastRun.Number)
1835+
assert.Equal(t, sdk.StatusNeverBuilt.String(), lastRun.Status)
1836+
// check "Run conditions aren't ok" info
1837+
var found bool
1838+
for _, info := range lastRun.Infos {
1839+
if info.Message.ID == sdk.MsgWorkflowConditionError.ID {
1840+
found = true
1841+
}
1842+
}
1843+
assert.Equal(t, true, found)
1844+
}
1845+
17531846
func Test_postWorkflowRunHandler_BadPayload(t *testing.T) {
17541847
api, db, router, end := newTestAPI(t, bootstrap.InitiliazeDB)
17551848
defer end()

0 commit comments

Comments
 (0)
0