Skip to content
This repository was archived by the owner on Feb 2, 2022. It is now read-only.

Commit 2b2cb59

Browse files
stishkinstas
andauthored
Switch BVT runs to petstore container (rather than permanent live web service) (#78)
Co-authored-by: stas <statis@microsoft.com>
1 parent 7d7adc9 commit 2b2cb59

File tree

14 files changed

+192
-120
lines changed

14 files changed

+192
-120
lines changed

Scripts/Tests/bvt-petstore.py renamed to Scripts/Tests/bvt-petstore3.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,30 @@ def webhook_triggers_results(job_id, test_url):
3333
def time_span(t_start, t_end):
3434
return time.strftime("%H:%M:%S", time.gmtime(t_end - t_start))
3535

36-
def bvt(cli, definitions):
36+
def bvt(cli, definitions, subs):
3737
print('Getting available wehook events')
3838
webhook_events = cli.list_available_webhooks_events()
3939
try:
4040
test_url = webhooks_test_url(definitions.subscription, definitions.resource_group, definitions.test_infra)
4141
for event in webhook_events:
4242
print(f'Setting webhook for {event}')
43-
compile_webhook = cli.set_webhooks_subscription('petstore-compile', event, test_url)
44-
fuzz_webhook = cli.set_webhooks_subscription('petstore-fuzz', event, test_url)
43+
compile_webhook = cli.set_webhooks_subscription('petstore3-compile', event, test_url)
44+
fuzz_webhook = cli.set_webhooks_subscription('petstore3-fuzz', event, test_url)
4545

46-
added_compile = cli.list_webhooks('petstore-compile', event)
46+
added_compile = cli.list_webhooks('petstore3-compile', event)
4747
if len(added_compile) == 0:
48-
raise Exception('Expected petstore-compile webhooks not to be empty after creation')
48+
raise Exception('Expected petstore3-compile webhooks not to be empty after creation')
4949

50-
added_fuzz = cli.list_webhooks('petstore-fuzz', event)
50+
added_fuzz = cli.list_webhooks('petstore3-fuzz', event)
5151
if len(added_fuzz) == 0:
52-
raise Exception('Expected petstore-fuzz webhooks not to be empty after creation')
52+
raise Exception('Expected petstore3-fuzz webhooks not to be empty after creation')
5353

5454
t_pre_compile = time.time()
5555

5656
print('Compile')
57-
compile_config_path = os.path.abspath(os.path.join(cli_path, 'samples', 'restler', 'self-contained', 'swagger-petstore', 'restler.compile.json'))
57+
compile_config_path = os.path.abspath(os.path.join(cli_path, 'samples', 'restler', 'self-contained', 'swagger-petstore3', 'compile.json'))
5858

59-
compile_config = raft.RaftJobConfig(file_path=compile_config_path)
59+
compile_config = raft.RaftJobConfig(file_path=compile_config_path, substitutions=subs)
6060
compile_job = cli.new_job(compile_config)
6161
cli.poll(compile_job['jobId'], 10)
6262

@@ -81,11 +81,9 @@ def bvt(cli, definitions):
8181
f' {after_compile_pre_fuzz}')
8282

8383
print('Fuzz')
84-
fuzz_config_path = os.path.abspath(os.path.join(cli_path, 'samples', 'restler', 'self-contained', 'swagger-petstore', 'restler.fuzz.json'))
85-
subs = {}
84+
fuzz_config_path = os.path.abspath(os.path.join(cli_path, 'samples', 'restler', 'self-contained', 'swagger-petstore3', 'fuzz.json'))
8685
subs['{compile.jobId}'] = compile_job['jobId']
8786
fuzz_config = raft.RaftJobConfig(file_path=fuzz_config_path, substitutions=subs)
88-
fuzz_config.config['duration'] = '00:20:00'
8987
fuzz_job = cli.new_job(fuzz_config)
9088
cli.poll(fuzz_job['jobId'], 10)
9189

@@ -104,8 +102,8 @@ def bvt(cli, definitions):
104102
raise Exception('Expected job to be in completed state when retrieved job list.'
105103
f'{after_fuzz}')
106104

107-
if m != 2:
108-
raise Exception('Expected 2 after compile job step'
105+
if m != 3:
106+
raise Exception('Expected 3 after compile job step'
109107
f' for job {fuzz_job["jobId"]}'
110108
f' got {m}'
111109
f' {after_fuzz}')
@@ -136,7 +134,7 @@ def bvt(cli, definitions):
136134
print('Validating that bugs posted events matches total bugs found in job status')
137135
total_bugs_found = 0
138136
for r in job_status_events:
139-
if r['Data']['State'] == 'Completed' and r['Data']['AgentName'] != r['Data']['JobId']:
137+
if r['Data']['State'] == 'Completed' and r['Data']['AgentName'] != r['Data']['JobId'] and r['Data']['Tool'] == 'RESTler':
140138
total_bugs_found += r['Data']['Metrics']['TotalBugBucketsCount']
141139

142140
print(f'Total bugs found: {total_bugs_found}')
@@ -161,12 +159,11 @@ def bvt(cli, definitions):
161159
if len(deleted_fuzz) > 0:
162160
raise Exception('Expected petstore-fuzz webhooks to be empty after deletion, instead got %s', deleted_compile)
163161

164-
165-
166162
if __name__ == "__main__":
167163
formatter = argparse.ArgumentDefaultsHelpFormatter
168164
parser = argparse.ArgumentParser(description='bvt', formatter_class=formatter)
169165
raft.add_defaults_and_secret_args(parser)
166+
parser.add_argument('--build', required=True)
170167
args = parser.parse_args()
171168

172169
if args.defaults_context_json:
@@ -179,4 +176,10 @@ def bvt(cli, definitions):
179176
definitions = RaftDefinitions(defaults)
180177
defaults['secret'] = args.secret
181178
cli = RaftCLI(defaults)
182-
bvt(cli, definitions)
179+
subs = {
180+
"{build-url}" : os.environ.get('SYSTEM_COLLECTIONURI'),
181+
"{build-id}" : os.environ.get('BUILD_BUILDID'),
182+
"{ci-run}" : args.build.replace('.', '-')
183+
}
184+
print(f"SUBS: {subs}")
185+
bvt(cli, definitions, subs)

ado/stages/test/steps/bvt.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ steps:
1313
azureSubscription: $(raft-subscription)
1414
scriptType: 'pscore'
1515
scriptLocation: 'inlineScript'
16-
inlineScript: "python Scripts/Tests/bvt.py --defaults-context-json '$(raft-defaults)' --secret $(bvt-secret) --host $(bvt-host)"
16+
inlineScript: "python Scripts/Tests/bvt-petstore3.py --build $(Build.BuildNumber) --defaults-context-json '$(raft-defaults)' --secret $(bvt-secret)"

cli/samples/restler/self-contained/swagger-petstore/restler.compile.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
"URL" : "http://localhost:8080/api/swagger.json"
44
},
55
"webhook": {
6-
"name": "petstore-compile",
7-
"metadata": {
8-
"action" : "compile"
9-
}
6+
"name": "petstore-compile"
107
},
118
"testTargets" : {
129
"targets" : [

cli/samples/restler/self-contained/swagger-petstore/restler.fuzz.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
{
22
"host": "localhost",
33
"webhook": {
4-
"name": "petstore-fuzz",
5-
"metadata": {
6-
"app" : "petstore",
7-
"swagger_version" : "v2",
8-
"action" : "fuzz"
9-
}
4+
"name": "petstore-fuzz"
105
},
116

127
"readonlyFileShareMounts": [

cli/samples/restler/self-contained/swagger-petstore/restler.test.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
"host": "localhost",
33

44
"webhook": {
5-
"name": "petstore",
6-
"metadata": {
7-
"app" : "petstore",
8-
"swagger_version" : "v2",
9-
"action" : "test"
10-
}
5+
"name": "petstore-test"
116
},
127
"readonlyFileShareMounts": [
138
{

cli/samples/restler/self-contained/swagger-petstore/run.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ def run(compile, test, fuzz, replay):
1414
cli = RaftCLI()
1515
# Create compilation job configuration
1616
compile_job_config = RaftJobConfig(file_path=compile)
17-
# add webhook metadata that will be included in every triggered webhook by Compile job
18-
compile_job_config.add_metadata({"branch":"wizbangFeature"})
1917
print('Compile')
2018
# submit a new job with the Compile config and get new job ID
2119
compile_job = cli.new_job(compile_job_config)
@@ -37,8 +35,6 @@ def run(compile, test, fuzz, replay):
3735
# create a new job config with Fuzz configuration JSON
3836
fuzz_job_config = RaftJobConfig(file_path=fuzz, substitutions=subs)
3937
print('Fuzz')
40-
# add webhook metadata that will included in every triggered webhook by Fuzz job
41-
fuzz_job_config.add_metadata({"branch":"wizbangFeature"})
4238
# create new fuzz job configuration
4339
fuzz_job = cli.new_job(fuzz_job_config)
4440

cli/samples/restler/self-contained/swagger-petstore3/restler.compile.json renamed to cli/samples/restler/self-contained/swagger-petstore3/compile.json

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
{
1+
{
2+
"rootFileShare" : "{ci-run}",
3+
"namePrefix" : "petstore3-compile-",
24
"swaggerLocation": {
35
"URL" : "http://localhost:8082/api/v3/openapi.json"
46
},
57

6-
"resources": {
7-
"Cores": 4,
8-
"MemoryGBs": 8
9-
},
10-
118
"testTargets" : {
12-
"resources" : {
13-
"Cores" : 2,
14-
"MemoryGBs" : 4
15-
},
169
"targets" : [
1710
{
1811
"Container" : "swaggerapi/petstore3",
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
{
2+
"rootFileShare" : "{ci-run}",
3+
"namePrefix" : "petstore3-test-fuzz-lean-",
24
"host": "localhost",
35

46
"resources": {
57
"Cores": 4,
68
"MemoryGBs": 8
79
},
810

11+
"webhook":{
12+
"name" : "petstore3-fuzz",
13+
"metadata" : {
14+
"buildUrl" : "{build-url}/Raft/_build/results?buildId={build-id}&view=results"
15+
}
16+
},
17+
918
"readonlyFileShareMounts": [
1019
{
11-
"FileShareName": "{compile.jobId}",
12-
"MountPath": "/job-compile"
20+
"FileShareName": "{ci-run}",
21+
"MountPath": "/ci-run"
1322
}
1423
],
1524

@@ -26,46 +35,41 @@
2635
"Run" : {
2736
"Command" : "/bin/sh",
2837
"Arguments" : ["-c", "java -jar /swagger-petstore/jetty-runner.jar --log $RAFT_WORK_DIRECTORY/yyyy_mm_dd-requests.log --port 8081 /swagger-petstore/server.war"]
29-
},
30-
"Shell" : "/bin/sh",
31-
"OutputFolder" : "restler_petstore3"
38+
}
3239
},
3340
{
3441
"Container" : "swaggerapi/petstore3",
35-
"Port" : 8080,
36-
"ExpectedDurationUntilReady" : "00:00:30",
37-
"PostRun" : {
38-
"Command" : "/bin/sh",
39-
"Arguments" : ["-c", "cp /var/log/*-requests.log $RAFT_WORK_DIRECTORY"],
40-
"ExpectedRunDuration" : "00:00:10"
41-
},
42-
"Shell" : "/bin/sh",
43-
"OutputFolder" : "zap_petstore3"
42+
"Port" : 8082,
43+
"ExpectedDurationUntilReady" : "00:02:00",
44+
"Run" : {
45+
"Command" : "/bin/sh",
46+
"Arguments" : ["-c", "java -jar /swagger-petstore/jetty-runner.jar --log $RAFT_WORK_DIRECTORY/yyyy_mm_dd-requests.log --port 8082 /swagger-petstore/server.war"]
47+
}
4448
}
4549
]
4650
},
4751
"tasks": [
4852
{
4953
"toolName": "RESTler",
5054
"outputFolder": "fuzz",
51-
"duration": "00:30:00",
55+
"duration": "00:10:00",
5256
"toolConfiguration": {
5357
"task": "Fuzz",
5458
"runConfiguration": {
5559
"targetEndpointConfiguration" : {
5660
"Port" : 8081
5761
},
5862
"useSsl" : false,
59-
"inputFolderPath": "/job-compile/compile"
63+
"inputFolderPath": "/ci-run/{compile.jobId}/compile"
6064
}
6165
}
62-
},
66+
},
6367
{
64-
"toolName" : "ZAP",
65-
"outputFolder" : "zap",
68+
"toolName": "ZAP",
69+
"outputFolder": "zap",
6670
"swaggerLocation": {
67-
"URL" : "http://localhost:8080/api/v3/openapi.json"
71+
"URL" : "http://localhost:8082/api/v3/openapi.json"
6872
}
6973
}
7074
]
71-
}
75+
}

cli/samples/restler/self-contained/swagger-petstore3/run.py

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,60 @@
44
import pathlib
55
import sys
66
import os
7+
import json
78

89
cur_dir = os.path.dirname(os.path.abspath(__file__))
9-
sys.path.append(os.path.join(cur_dir, '..', '..', '..', '..'))
10-
from raft_sdk.raft_service import RaftCLI, RaftJobConfig, RaftJobError
10+
sys.path.append(os.path.join(cur_dir, '..'))
11+
from raft_sdk.raft_service import RaftCLI, RaftJobConfig, RaftJobError, RaftDefinitions
1112

12-
def run(compile, test, fuzz):
13-
# instantiate RAFT CLI
14-
cli = RaftCLI()
1513

14+
def run(cli, config, subs):
1615
# Create compilation job configuration
17-
compile_job_config = RaftJobConfig(file_path=compile)
18-
print('Compile')
16+
job_config = RaftJobConfig(file_path=config, substitutions=subs)
17+
print(f'Running {config}')
1918
# submit a new job with the Compile config and get new job ID
20-
compile_job = cli.new_job(compile_job_config)
19+
job = cli.new_job(job_config)
2120
# wait for a job with ID from compile_job to finish the run
22-
cli.poll(compile_job['jobId'])
23-
24-
# use compile job as input for fuzz job
25-
subs = {}
26-
subs['{compile.jobId}'] = compile_job['jobId']
27-
28-
test_job_config = RaftJobConfig(file_path=test, substitutions=subs)
29-
print('Test')
30-
# create new fuzz job configuration
31-
test_job = cli.new_job(test_job_config)
32-
# wait for job ID from fuzz_job to finish the run
33-
cli.poll(test_job['jobId'])
34-
35-
# create a new job config with Fuzz configuration JSON
36-
fuzz_job_config = RaftJobConfig(file_path=fuzz, substitutions=subs)
37-
print('Fuzz')
38-
# create new fuzz job configuration
39-
fuzz_job = cli.new_job(fuzz_job_config)
40-
41-
# wait for job ID from fuzz_job to finish the run
42-
cli.poll(fuzz_job['jobId'])
21+
cli.poll(job['jobId'])
22+
return job['jobId']
4323

4424
if __name__ == "__main__":
4525
try:
46-
run(os.path.join(cur_dir, "restler.compile.json"),
47-
os.path.join(cur_dir, "restler.test.json"),
48-
os.path.join(cur_dir, "restler.zap.fuzz.json"))
26+
defaults = None
27+
28+
if sys.argv[1] == '--build':
29+
build_id = sys.argv[2].replace(".", "-")
30+
print(f"BUILD ID : {build_id}")
31+
32+
with open(os.path.join(cur_dir, '..', 'defaults.json'), 'r') as defaults_json:
33+
defaults = json.load(defaults_json)
34+
if sys.argv[3] == '--secret':
35+
defaults['secret'] = sys.argv[4]
36+
37+
# instantiate RAFT CLI
38+
cli = RaftCLI(defaults)
39+
defs = RaftDefinitions(defaults)
40+
41+
compile_job_id = None
42+
subs = {
43+
"{ci-run}" : f"{build_id}",
44+
"{build-url}" : os.environ['SYSTEM_COLLECTIONURI'],
45+
"{build-id}" : os.environ['BUILD_BUILDID'],
46+
"{raft-subscription}": defs.subscription,
47+
"{raft-resource-group}" : defs.resource_group,
48+
"{raft-storage-account}" : defs.storage_account
49+
}
50+
for arg in sys.argv[1:]:
51+
if arg == 'compile':
52+
compile_job_id = run(cli, os.path.join(cur_dir, 'compile.json'), subs)
53+
subs['{compile.jobId}'] = compile_job_id
54+
55+
if arg == 'test':
56+
run(cli, os.path.join(cur_dir, "test.json"), subs),
57+
58+
if arg == 'test-fuzz-lean':
59+
run(cli, os.path.join(cur_dir, "test-fuzz-lean.json"), subs),
60+
4961
except RaftJobError as ex:
50-
print(f'ERROR: {ex.message}')
62+
print(f'ERROR: {ex.message}')
63+
sys.exit(1)

0 commit comments

Comments
 (0)