summaryrefslogtreecommitdiffstats
path: root/testing-scheduler/server/src/test_parser.py
blob: 7b471517481e287b293d4735defcea387847f3f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python

##############################################################################
# Copyright (c) 2018 HUAWEI TECHNOLOGIES CO.,LTD and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import click
import os
import yaml
import json
import collections
from src.step.step_manager import TestStepManager
from src.conductor_processor.workflow import WorkflowFile
from conductorclient.run_new_workflow import WorkflowMgr

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
CONDUCTOR_SERVER_ADDR = "http://conductor_conductor-server_1:8080"
STORE_TASK_PATH = "/tmp/generate_task.json"
STORE_WF_PATH = "/tmp/generate_workflow.json"


@click.command()
@click.option("--filepath", help="file path of test case")
def cmdParse(filepath):
    parse(filepath)


def parse(filepath):
    filePrefix, fileName = os.path.split(filepath)
    print '------------ start to parse the test case:' + \
        '%s ----------------' % fileName
    with open(filepath) as f:
        yaml_file = yaml.load(f)
        parseTestcase(yaml_file['schema'], fileName)

    workflowId = runWorkFlow()
    print '------------------- parse executes end -------------------------'

    return workflowId


def parseTestcase(schema, tcName='testcase0'):
    if schema is None:
        return parseLog(False, reason='schema not found.')
    steps = schema['steps']
    if steps is None:
        return parseLog(False, reason='steps is invalid.')
    flows = schema['flows']
    if flows is None:
        return parseLog(False, reasion='flows is invalid.')
    # steps is a list, step is dict. no json here.
    # steps = sorted(steps, sortById)

    # load context
    contextDict = {}
    contextDir = os.path.join(BASE_DIR, "env", "context", "context.yaml")
    with open(contextDir, "r") as f:
        contextDict = yaml.load(f)
    #
    testStepMgr = TestStepManager(contextDict)

    stepObjArr = []
    for step in steps:
        if 'args' not in step:
            step['args'] = {}
        # type and action can be extended, default couple is 'test' & 'start'.
        if 'type' not in step:
            step['type'] = 'test'
            step['action'] = 'start'

        stepObj = testStepMgr.getStepObj(
            step['type'], step['id'], step['name'], step['service'],
            step['action'], step['args'])
        stepObjArr.append(stepObj)

    # generate workflow by 'flow' and 'step'
    tcName = os.path.splitext(tcName)[0]
    wfFileObj = WorkflowFile(tcName)
    workflowDict, taskMetaList = wfFileObj.generateMetaData(flows, stepObjArr)

    with open(STORE_TASK_PATH, 'w') as f:
        f.write(json.dumps({'task_group': taskMetaList}, indent=True))
    with open(STORE_WF_PATH, 'w') as f:
        f.write(json.dumps(workflowDict, indent=True))


def parseWebTestcase(webTestcase):
    print 'parseWebTestcase----------------------------'

    stepList = webTestcase['stepList']
    mainOrdersList = webTestcase['mainOrdersList']
    subflowList = webTestcase['subflowList']

    parseData = collections.OrderedDict()
    parseData['schema'] = collections.OrderedDict()
    parseData['schema']['steps'] = []
    parseData['schema']['flows'] = []

    parseStepList = parseData['schema']['steps']
    parseFlowList = parseData['schema']['flows']
    stepIndexDict = {}
    # parse stepList
    for index in range(len(stepList)):
        stepItem = stepList[index]
        parseStep = collections.OrderedDict()

        parseStep['id'] = index + 1
        parseStep['name'] = stepItem['name']
        parseStep['service'] = collections.OrderedDict()
        parseStep['service']['name'] = stepItem['service']
        parseStep['service']['interface'] = stepItem['action']
        parseStep['action'] = 'start'
        parseStep['args'] = {}
        for paramItem in stepItem['params']:
            parseStep['args'][paramItem['key']] = transParamString(
                paramItem['value'])

        parseStepList.append(parseStep)
        stepIndexDict[parseStep['name']] = parseStep['id']
    # parse flows
    # parse mainflow
    print stepIndexDict
    typeDict = {1: 'normal', 2: 'switch', 3: 'parallel'}
    mainFlow = collections.OrderedDict()
    mainFlow['name'] = 'main'
    mainFlow['orders'] = []
    mainFlow['orders'] = parseOrderList(
        mainOrdersList, stepIndexDict, typeDict)
    parseFlowList.append(mainFlow)

    # parse subflow
    for subflowItem in subflowList:
        replaceSubflow = collections.OrderedDict()
        replaceSubflow['name'] = subflowItem['name']
        replaceSubflow['orders'] = parseOrderList(
            subflowItem['orderList'], stepIndexDict, typeDict)
        parseFlowList.append(replaceSubflow)

    print 'END parseWebTestcase----------------------------'
    return parseData


# parse orderlist from web edition to server edition
def parseOrderList(orderList, stepIndexDict, typeDict):
    replaceList = []
    for orderItem in orderList:
        replaceOrder = collections.OrderedDict()
        orderType = typeDict[orderItem['type']]
        replaceOrder['type'] = orderType
        if orderType == 'normal':
            stepId = stepIndexDict[orderItem['step']]
            replaceOrder['step'] = stepId
        elif orderType == 'switch':
            replaceOrder['value'] = orderItem['value']
            replaceOrder['cases'] = collections.OrderedDict()
            for caseItem in orderItem['cases']:
                caseValue = caseItem['value']
                caseOrderType = caseItem['orderType']
                caseOrderValue = caseItem['orderValue']
                if caseOrderType == "step":
                    orderInCase = collections.OrderedDict()
                    orderInCase['type'] = 'normal'
                    orderInCase['step'] = stepIndexDict[caseOrderValue]
                    replaceOrder['cases'][caseValue] = [orderInCase]
                else:
                    replaceOrder['cases'][caseValue] = caseOrderValue
        else:
            replaceOrder['parallel'] = collections.OrderedDict()
            pIndex = 1
            for branchItem in orderItem['branches']:
                pKey = 'p' + str(pIndex)
                branchOrderType = branchItem['orderType']
                branchOrderValue = branchItem['orderValue']
                if branchOrderType == "step":
                    replaceBranchItem = collections.OrderedDict()
                    replaceBranchItem['type'] = 'normal'
                    replaceBranchItem['step'] = stepIndexDict[branchOrderValue]
                    replaceOrder['parallel'][pKey] = [replaceBranchItem]
                else:
                    replaceOrder['parallel'][pKey] = branchOrderValue
                pIndex += 1
        replaceList.append(replaceOrder)
    return replaceList


def transParamString(val):
    if type(val) != str:
        return val
    if '.' not in val:
        if val.isdigit():
            return int(val)
    try:
        f = float(val)
        return f
    except ValueError:
        return val


def getWebTestcase(originTcDict):
    print "getWebTestcase----------------------------------"
    webTcDict = {
        "stepList": [],
        "mainOrdersList": [],
        "subflowList": []
    }
    stepList = webTcDict['stepList']
    subflowList = webTcDict['subflowList']
    if originTcDict is None:
        return webTcDict
    originContent = originTcDict['schema']
    originSteps = originContent['steps']
    stepIndexDict = {}
    # transform steps to stepList
    for stepItem in originSteps:
        replaceStep = {}
        replaceStep['name'] = stepItem['name']
        replaceStep['service'] = stepItem['service']['name']
        replaceStep['action'] = stepItem['service']['interface']
        replaceStep['params'] = []
        if 'args' in stepItem:
            for (key, value) in stepItem['args'].items():
                replaceParam = {}
                replaceParam['key'] = key
                replaceParam['value'] = value
                replaceStep['params'].append(replaceParam)
        stepList.append(replaceStep)
        stepIndexDict[stepItem['id']] = stepItem['name']

    # transform main flow
    originFlows = originContent['flows']
    originMainflow = {}
    for flowIndex in range(len(originFlows)):
        flowItem = originFlows[flowIndex]
        if flowItem['name'] == 'main':
            originMainflow = flowItem
            originFlows.pop(flowIndex)
            break
    typeDict = {'normal': 1, 'switch': 2, 'parallel': 3}
    webTcDict['mainOrdersList'] = getOrderList(
        originMainflow['orders'], stepIndexDict, typeDict)

    # transform subflows
    for originSubflow in originFlows:
        replaceSubflow = {}
        replaceSubflow['name'] = originSubflow['name']
        replaceSubflow['orderList'] = getOrderList(
            originSubflow['orders'], stepIndexDict, typeDict)
        subflowList.append(replaceSubflow)

    # return web edition of testcase
    print "END getWebTestcase----------------------------------"
    return webTcDict


def getOrderList(originOrderList, stepIndexDict, typeDict):
    replaceOrderList = []
    for orderItem in originOrderList:
        replaceOrderItem = {}
        orderType = orderItem['type']
        replaceOrderItem['type'] = typeDict[orderType]
        if orderType == 'normal':
            stepName = stepIndexDict[orderItem['step']]
            replaceOrderItem['step'] = stepName
        elif orderType == 'switch':
            replaceOrderItem['value'] = orderItem['value']
            replaceOrderItem['cases'] = []
            for (caseValue, ordersInCase) in orderItem['cases'].items():
                replaceCase = {}
                replaceCase['value'] = caseValue
                if type(ordersInCase) == list:
                    replaceCase['orderType'] = 'step'
                    caseStepName = stepIndexDict[ordersInCase[0]['step']]
                    replaceCase['orderValue'] = caseStepName
                else:
                    replaceCase['orderType'] = 'flow'
                    replaceCase['orderValue'] = ordersInCase
                replaceOrderItem['cases'].append(replaceCase)
        else:
            replaceOrderItem['branches'] = []
            for paraIndex in orderItem['parallel']:
                paraItem = orderItem['parallel'][paraIndex]
                replaceBranch = {}
                if type(paraItem) == list:
                    replaceBranch['orderType'] = 'step'
                    branchStepName = stepIndexDict[paraItem[0]['step']]
                    replaceBranch['orderValue'] = branchStepName
                else:
                    replaceBranch['orderType'] = 'flow'
                    replaceBranch['orderValue'] = paraItem
                replaceOrderItem['branches'].append(replaceBranch)
        replaceOrderList.append(replaceOrderItem)

    return replaceOrderList


def runWorkFlow():
    wfMgr = WorkflowMgr(CONDUCTOR_SERVER_ADDR)
    wfMgr.setTaskDefFromFile(STORE_TASK_PATH)
    wfMgr.setWorkflowFromFile(STORE_WF_PATH)
    inputParam = {'input': 'fake'}
    workflowId = wfMgr.startWorkflow(inputParam)
    return workflowId


def parseLog(flag, **msg):
    return {'result': flag, 'message': msg}


if __name__ == "__main__":
    cmdParse()