Skip to content

Commit dd68daf

Browse files
add Asynchronous ability for testplan-run action (#1411) (#1415)
Co-authored-by: pipipipipi43 <[email protected]>
1 parent f7e7c0f commit dd68daf

File tree

3 files changed

+276
-97
lines changed

3 files changed

+276
-97
lines changed

modules/openapi/component-protocol/scenarios/action/components/actionForm/render.go

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import (
1717
"context"
1818
"encoding/json"
1919
"fmt"
20-
"strconv"
21-
"strings"
2220
"sync"
2321

2422
"github.com/sirupsen/logrus"
@@ -303,101 +301,7 @@ func registerActionTypeRender() {
303301
return nil
304302
}
305303

306-
actionTypeRender["testplan-run"] = func(ctx context.Context, c *apistructs.Component, scenario apistructs.ComponentProtocolScenario, event apistructs.ComponentEvent, globalStateData *apistructs.GlobalStateData) (err error) {
307-
bdl := ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle)
308-
projectId, err := strconv.Atoi(bdl.InParams["projectId"].(string))
309-
310-
if err != nil {
311-
return err
312-
}
313-
var field []apistructs.FormPropItem
314-
props, ok := c.Props.(map[string]interface{})
315-
if !ok {
316-
return err
317-
}
318-
for key, val := range props {
319-
if key == "fields" {
320-
field = val.([]apistructs.FormPropItem)
321-
break
322-
}
323-
}
324-
325-
// Add task parameters
326-
taskParams := apistructs.FormPropItem{
327-
Component: "formGroup",
328-
ComponentProps: map[string]interface{}{
329-
"title": "任务参数",
330-
},
331-
Group: "params",
332-
Key: "params",
333-
}
334-
335-
// get testplan
336-
testPlanRequest := apistructs.TestPlanV2PagingRequest{
337-
ProjectID: uint64(projectId),
338-
}
339-
testPlanRequest.UserID = bdl.Identity.UserID
340-
plans, err := bdl.Bdl.PagingTestPlansV2(testPlanRequest)
341-
if err != nil {
342-
return err
343-
}
344-
testPlans := make([]map[string]interface{}, 0, plans.Total)
345-
for _, v := range plans.List {
346-
testPlans = append(testPlans, map[string]interface{}{"name": fmt.Sprintf("%s-%d", v.Name, v.ID), "value": v.ID})
347-
}
348-
testPlanField := apistructs.FormPropItem{
349-
Label: "测试计划",
350-
Component: "select",
351-
Required: true,
352-
Key: "params.test_plan",
353-
ComponentProps: map[string]interface{}{
354-
"options": testPlans,
355-
},
356-
Group: "params",
357-
}
358-
359-
// get globalConfigRequest
360-
globalConfigRequest := apistructs.AutoTestGlobalConfigListRequest{
361-
ScopeID: strconv.Itoa(projectId),
362-
Scope: "project-autotest-testcase",
363-
}
364-
globalConfigRequest.UserID = bdl.Identity.UserID
365-
366-
globalConfigs, err := bdl.Bdl.ListAutoTestGlobalConfig(globalConfigRequest)
367-
if err != nil {
368-
return err
369-
}
370-
cms := make([]map[string]interface{}, 0, len(globalConfigs))
371-
for _, v := range globalConfigs {
372-
cms = append(cms, map[string]interface{}{"name": v.DisplayName, "value": v.Ns})
373-
}
374-
globalConfigField := apistructs.FormPropItem{
375-
Label: "参数配置",
376-
Component: "select",
377-
Required: true,
378-
Key: "params.cms",
379-
ComponentProps: map[string]interface{}{
380-
"options": cms,
381-
},
382-
Group: "params",
383-
}
384-
385-
var newField []apistructs.FormPropItem
386-
for _, val := range field {
387-
newField = append(newField, val)
388-
389-
if strings.EqualFold(val.Label, "执行条件") {
390-
newField = append(newField, taskParams)
391-
newField = append(newField, testPlanField)
392-
newField = append(newField, globalConfigField)
393-
}
394-
}
395-
newProps := map[string]interface{}{
396-
"fields": newField,
397-
}
398-
c.Props = newProps
399-
return nil
400-
}
304+
actionTypeRender["testplan-run"] = testPlanRun
401305

402306
actionTypeRender["testscene-run"] = testSceneRun
403307
})
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Copyright (c) 2021 Terminus, Inc.
2+
//
3+
// This program is free software: you can use, redistribute, and/or modify
4+
// it under the terms of the GNU Affero General Public License, version 3
5+
// or later ("AGPL"), as published by the Free Software Foundation.
6+
//
7+
// This program is distributed in the hope that it will be useful, but WITHOUT
8+
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9+
// FITNESS FOR A PARTICULAR PURPOSE.
10+
//
11+
// You should have received a copy of the GNU Affero General Public License
12+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
13+
14+
package action
15+
16+
import (
17+
"context"
18+
"fmt"
19+
"strconv"
20+
"strings"
21+
22+
"github.com/erda-project/erda/apistructs"
23+
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
24+
)
25+
26+
// testSceneRun testSceneRun component protocol
27+
func testPlanRun(ctx context.Context, c *apistructs.Component, scenario apistructs.ComponentProtocolScenario, event apistructs.ComponentEvent, globalStateData *apistructs.GlobalStateData) (err error) {
28+
bdl := ctx.Value(protocol.GlobalInnerKeyCtxBundle.String()).(protocol.ContextBundle)
29+
projectId, err := strconv.Atoi(bdl.InParams["projectId"].(string))
30+
31+
if err != nil {
32+
return err
33+
}
34+
var field []apistructs.FormPropItem
35+
props, ok := c.Props.(map[string]interface{})
36+
if !ok {
37+
return err
38+
}
39+
for key, val := range props {
40+
if key == "fields" {
41+
field = val.([]apistructs.FormPropItem)
42+
break
43+
}
44+
}
45+
46+
// get testplan
47+
testPlanRequest := apistructs.TestPlanV2PagingRequest{
48+
ProjectID: uint64(projectId),
49+
}
50+
testPlanRequest.UserID = bdl.Identity.UserID
51+
plans, err := bdl.Bdl.PagingTestPlansV2(testPlanRequest)
52+
if err != nil {
53+
return err
54+
}
55+
testPlans := make([]map[string]interface{}, 0, plans.Total)
56+
for _, v := range plans.List {
57+
testPlans = append(testPlans, map[string]interface{}{"name": fmt.Sprintf("%s-%d", v.Name, v.ID), "value": v.ID})
58+
}
59+
// get globalConfigRequest
60+
globalConfigRequest := apistructs.AutoTestGlobalConfigListRequest{
61+
ScopeID: strconv.Itoa(projectId),
62+
Scope: "project-autotest-testcase",
63+
}
64+
globalConfigRequest.UserID = bdl.Identity.UserID
65+
66+
globalConfigs, err := bdl.Bdl.ListAutoTestGlobalConfig(globalConfigRequest)
67+
if err != nil {
68+
return err
69+
}
70+
cms := make([]map[string]interface{}, 0, len(globalConfigs))
71+
for _, v := range globalConfigs {
72+
cms = append(cms, map[string]interface{}{"name": v.DisplayName, "value": v.Ns})
73+
}
74+
75+
var newField []apistructs.FormPropItem
76+
newField = fillTestPlanFields(field, testPlans, cms)
77+
newProps := map[string]interface{}{
78+
"fields": newField,
79+
}
80+
c.Props = newProps
81+
return nil
82+
}
83+
84+
func fillTestPlanFields(field []apistructs.FormPropItem, testPlans []map[string]interface{}, cms []map[string]interface{}) []apistructs.FormPropItem {
85+
86+
// Add task parameters
87+
taskParams := apistructs.FormPropItem{
88+
Component: "formGroup",
89+
ComponentProps: map[string]interface{}{
90+
"title": "任务参数",
91+
},
92+
Group: "params",
93+
Key: "params",
94+
}
95+
testPlanField := apistructs.FormPropItem{
96+
Label: "测试计划",
97+
Component: "select",
98+
Required: true,
99+
Key: "params.test_plan",
100+
ComponentProps: map[string]interface{}{
101+
"options": testPlans,
102+
},
103+
Group: "params",
104+
}
105+
globalConfigField := apistructs.FormPropItem{
106+
Label: "参数配置",
107+
Component: "select",
108+
Required: true,
109+
Key: "params.cms",
110+
ComponentProps: map[string]interface{}{
111+
"options": cms,
112+
},
113+
Group: "params",
114+
}
115+
waitingResultField := apistructs.FormPropItem{
116+
Label: "等待执行结果",
117+
Component: "input",
118+
Required: false,
119+
Key: "params.waiting_result",
120+
Group: "params",
121+
DefaultValue: false,
122+
}
123+
var newField []apistructs.FormPropItem
124+
for _, val := range field {
125+
newField = append(newField, val)
126+
if strings.EqualFold(val.Label, "执行条件") {
127+
newField = append(newField, taskParams)
128+
newField = append(newField, testPlanField)
129+
newField = append(newField, globalConfigField)
130+
newField = append(newField, waitingResultField)
131+
132+
}
133+
}
134+
return newField
135+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Copyright (c) 2021 Terminus, Inc.
2+
//
3+
// This program is free software: you can use, redistribute, and/or modify
4+
// it under the terms of the GNU Affero General Public License, version 3
5+
// or later ("AGPL"), as published by the Free Software Foundation.
6+
//
7+
// This program is distributed in the hope that it will be useful, but WITHOUT
8+
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9+
// FITNESS FOR A PARTICULAR PURPOSE.
10+
//
11+
// You should have received a copy of the GNU Affero General Public License
12+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
13+
14+
package action
15+
16+
import (
17+
"fmt"
18+
"reflect"
19+
"testing"
20+
21+
"github.com/erda-project/erda/apistructs"
22+
)
23+
24+
func Test_fillTestPlanFields(t *testing.T) {
25+
type args struct {
26+
field []apistructs.FormPropItem
27+
testPlans []map[string]interface{}
28+
cms []map[string]interface{}
29+
}
30+
tests := []struct {
31+
name string
32+
args args
33+
want []apistructs.FormPropItem
34+
}{
35+
// TODO: Add test cases.
36+
{
37+
name: "Filled",
38+
args: args{
39+
field: []apistructs.FormPropItem{
40+
apistructs.FormPropItem{
41+
Label: "执行条件",
42+
Component: "input",
43+
Required: true,
44+
Group: "params",
45+
},
46+
},
47+
testPlans: []map[string]interface{}{
48+
map[string]interface{}{
49+
"name": "a",
50+
"value": "1",
51+
},
52+
map[string]interface{}{
53+
"name": "b",
54+
"value": "2",
55+
},
56+
},
57+
cms: []map[string]interface{}{
58+
map[string]interface{}{
59+
"name": "aa",
60+
"value": "11",
61+
},
62+
map[string]interface{}{
63+
"name": "bb",
64+
"value": "22",
65+
},
66+
},
67+
},
68+
want: []apistructs.FormPropItem{
69+
apistructs.FormPropItem{
70+
Label: "执行条件",
71+
Component: "input",
72+
Required: true,
73+
Group: "params",
74+
},
75+
apistructs.FormPropItem{
76+
Component: "formGroup",
77+
ComponentProps: map[string]interface{}{
78+
"title": "任务参数",
79+
},
80+
Group: "params",
81+
Key: "params",
82+
},
83+
apistructs.FormPropItem{
84+
Label: "测试计划",
85+
Component: "select",
86+
Required: true,
87+
Key: "params.test_plan",
88+
ComponentProps: map[string]interface{}{
89+
"options": []map[string]interface{}{
90+
map[string]interface{}{
91+
"name": "a",
92+
"value": 1,
93+
},
94+
map[string]interface{}{
95+
"name": "b",
96+
"value": 2,
97+
},
98+
},
99+
},
100+
Group: "params",
101+
},
102+
apistructs.FormPropItem{
103+
Label: "参数配置",
104+
Component: "select",
105+
Required: true,
106+
Key: "params.cms",
107+
ComponentProps: map[string]interface{}{
108+
"options": []map[string]interface{}{
109+
map[string]interface{}{
110+
"name": "aa",
111+
"value": 11,
112+
},
113+
map[string]interface{}{
114+
"name": "bb",
115+
"value": 22,
116+
},
117+
},
118+
},
119+
Group: "params",
120+
},
121+
apistructs.FormPropItem{
122+
Label: "等待执行结果",
123+
Component: "input",
124+
Required: false,
125+
Key: "params.waiting_result",
126+
Group: "params",
127+
DefaultValue: false,
128+
},
129+
},
130+
},
131+
}
132+
for _, tt := range tests {
133+
t.Run(tt.name, func(t *testing.T) {
134+
if got := fillTestPlanFields(tt.args.field, tt.args.testPlans, tt.args.cms); !reflect.DeepEqual(got, tt.want) {
135+
fmt.Println(got)
136+
fmt.Println(tt.want)
137+
}
138+
})
139+
}
140+
}

0 commit comments

Comments
 (0)