-
Notifications
You must be signed in to change notification settings - Fork 119
/
run_canary_in_devicefarm.sh
executable file
·152 lines (137 loc) · 6.45 KB
/
run_canary_in_devicefarm.sh
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
#!/bin/bash
project_arn=$DEVICEFARM_PROJECT_ARN
max_devices=$NUMBER_OF_DEVICES_TO_TEST
module_name=$1
canary_test_name=$2
file_name="$module_name-debug-androidTest.apk"
full_path="$module_name/build/outputs/apk/androidTest/debug/$file_name"
if [[ -z "${project_arn}" ]]; then
echo "DEVICEFARM_PROJECT_ARN environment variable not set."
exit 1
fi
if [[ -z "${max_devices}" ]]; then
echo "NUMBER_OF_DEVICES_TO_TEST not set. Defaulting to 1."
max_devices=1
fi
# Function to setup the app uploads in device farm
function createUpload {
test_type=$1
upload_response=`aws devicefarm create-upload --type $test_type \
--content-type="application/octet-stream" \
--project-arn="$project_arn" \
--name="$file_name" \
--query="upload.[url, arn]" \
--region="us-west-2" \
--output=text`
echo $upload_response
}
echo 'Uploading test package'
# Create an upload for the instrumentation test package
read -a result <<< $(createUpload "INSTRUMENTATION_TEST_PACKAGE")
test_package_url=${result[0]}
test_package_upload_arn=${result[1]}
# Upload the apk
curl -H "Content-Type:application/octet-stream" -T $full_path $test_package_url
# Create an upload for the app package (They're the same, but they have to be setup in device farm)
echo 'Uploading app package'
read -a result <<< $(createUpload "ANDROID_APP")
app_package_url=${result[0]}
app_package_upload_arn=${result[1]}
# Upload the apk
curl -H "Content-Type:application/octet-stream" -T $full_path $app_package_url
# Wait to make sure the upload completes. This should actually make a get-upload call and check the status.
echo "Waiting for uploads to complete"
sleep 10
# Get oldest device we can test against.
minDevice=$(aws devicefarm list-devices \
--region="us-west-2" \
--filters '[
{"attribute":"AVAILABILITY","operator":"EQUALS","values":["HIGHLY_AVAILABLE"]},
{"attribute":"PLATFORM","operator":"EQUALS","values":["ANDROID"]},
{"attribute":"OS_VERSION","operator":"GREATER_THAN_OR_EQUALS","values":["8"]},
{"attribute":"OS_VERSION","operator":"LESS_THAN","values":["8.1"]},
{"attribute":"MANUFACTURER","operator":"IN","values":["Google", "Pixel", "Samsung"]}
]' \
| jq -r '.devices[0].arn')
# Get middle device we can test against.
middleDevice=$(aws devicefarm list-devices \
--region="us-west-2" \
--filters '[
{"attribute":"AVAILABILITY","operator":"EQUALS","values":["HIGHLY_AVAILABLE"]},
{"attribute":"PLATFORM","operator":"EQUALS","values":["ANDROID"]},
{"attribute":"OS_VERSION","operator":"GREATER_THAN_OR_EQUALS","values":["10"]},
{"attribute":"OS_VERSION","operator":"LESS_THAN","values":["11"]},
{"attribute":"MANUFACTURER","operator":"IN","values":["Samsung"]}
]' \
| jq -r '.devices[0].arn')
# Get latest device we can test against.
latestDevice=$(aws devicefarm list-devices \
--region="us-west-2" \
--filters '[
{"attribute":"AVAILABILITY","operator":"EQUALS","values":["HIGHLY_AVAILABLE"]},
{"attribute":"PLATFORM","operator":"EQUALS","values":["ANDROID"]},
{"attribute":"OS_VERSION","operator":"GREATER_THAN_OR_EQUALS","values":["14"]},
{"attribute":"MANUFACTURER","operator":"IN","values":["Google", "Pixel"]}
]' \
| jq -r '.devices[0].arn')
# IF we fail to find our required test devices, fail.
if [[ -z "${minDevice}" || -z "${middleDevice}" || -z "${latestDevice}" ]]; then
echo "Failed to grab 3 required devices for integration tests."
exit 1
fi
# Function to cancel duplicate runs for same code source in device farm.
function stopDuplicates {
echo "Stopping duplicate runs"
name="$file_name-$CODEBUILD_SOURCE_VERSION"
read -a running_arns <<< $(aws devicefarm list-runs \
--arn="$project_arn" \
--query="runs[?(status == 'RUNNING' || status == 'PENDING') && name == '${name}'].arn" \
--region="us-west-2" \
--max-items=5 \
| jq -r '.[]')
for arn in "${running_arns[@]}"
do
## Just consume the result and do nothing with it.
result=`aws devicefarm stop-run --arn $arn --region="us-west-2" --query="run.name"`
done
}
stopDuplicates
# Schedule the test run in device farm
echo "Scheduling test run"
run_arn=$(aws devicefarm schedule-run --project-arn=$project_arn \
--app-arn="$app_package_upload_arn" \
--device-selection-configuration='{
"filters": [
{"attribute": "ARN", "operator":"IN", "values":["'$minDevice'", "'$middleDevice'", "'$latestDevice'"]}
],
"maxDevices": '$max_devices'
}' \
--name="$file_name-$CODEBUILD_SOURCE_VERSION" \
--test="type=INSTRUMENTATION,testPackageArn=$test_package_upload_arn,filter=$canary_test_name" \
--execution-configuration="jobTimeoutMinutes=30,videoCapture=false" \
--query="run.arn" \
--output=text \
--region="us-west-2")
status='NONE'
result='NONE'
# Wait for the test to complete
while true; do
run_status_response=`aws devicefarm get-run --arn="$run_arn" --region="us-west-2" --query="run.[status, result]" --output text`
read -a result_arr <<< $run_status_response
status=${result_arr[0]}
result=${result_arr[1]}
if [ "$status" = "COMPLETED" ]
then
break
fi
sleep 30
done
echo "Status = $status Result = $result"
./scripts/generate_df_testrun_report --run_arn="$run_arn" --module_name="$module_name" --pr="$CODEBUILD_SOURCE_VERSION" --output_path="build/allTests/$module_name/"
# If the result is PASSED, then exit with a return code 0
if [ "$result" = "PASSED" ]
then
exit 0
fi
# Otherwise, exit with a non-zero.
exit 1