|
| 1 | +import * as http from 'http'; |
| 2 | +import * as https from 'https'; |
| 3 | +import * as admin from 'firebase-admin'; |
| 4 | +import * as _ from 'lodash'; |
| 5 | +import * as utils from './test-utils'; |
| 6 | + |
| 7 | +interface AndroidDevice { |
| 8 | + androidModelId: string; |
| 9 | + androidVersionId: string; |
| 10 | + locale: string; |
| 11 | + orientation: string; |
| 12 | +} |
| 13 | + |
| 14 | +const TESTING_API_SERVICE_NAME = 'testing.googleapis.com'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Creates a new TestMatrix in Test Lab which is expected to be rejected as |
| 18 | + * invalid. |
| 19 | + * |
| 20 | + * @param projectId Project for which the test run will be created |
| 21 | + * @param testId Test id which will be encoded in client info details |
| 22 | + */ |
| 23 | +export async function startTestRun(projectId: string, testId: string) { |
| 24 | + const accessToken = await admin.credential |
| 25 | + .applicationDefault() |
| 26 | + .getAccessToken(); |
| 27 | + const device = await fetchDefaultDevice(accessToken); |
| 28 | + return await createTestMatrix(accessToken, projectId, testId, device); |
| 29 | +} |
| 30 | + |
| 31 | +async function fetchDefaultDevice( |
| 32 | + accessToken: admin.GoogleOAuthAccessToken |
| 33 | +): Promise<AndroidDevice> { |
| 34 | + const response = await utils.makeRequest( |
| 35 | + requestOptions(accessToken, 'GET', '/v1/testEnvironmentCatalog/ANDROID') |
| 36 | + ); |
| 37 | + const data = JSON.parse(response); |
| 38 | + const models = _.get(data, 'androidDeviceCatalog.models', []); |
| 39 | + const defaultModels = models.filter( |
| 40 | + (m) => |
| 41 | + m.tags !== undefined && |
| 42 | + m.tags.indexOf('default') > -1 && |
| 43 | + m.supportedVersionIds !== undefined && |
| 44 | + m.supportedVersionIds.length > 0 |
| 45 | + ); |
| 46 | + |
| 47 | + if (defaultModels.length === 0) { |
| 48 | + throw new Error('No default device found'); |
| 49 | + } |
| 50 | + |
| 51 | + const model = defaultModels[0]; |
| 52 | + const versions = model.supportedVersionIds; |
| 53 | + |
| 54 | + return <AndroidDevice>{ |
| 55 | + androidModelId: model.id, |
| 56 | + androidVersionId: versions[versions.length - 1], |
| 57 | + locale: 'en', |
| 58 | + orientation: 'portrait', |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +function createTestMatrix( |
| 63 | + accessToken: admin.GoogleOAuthAccessToken, |
| 64 | + projectId: string, |
| 65 | + testId: string, |
| 66 | + device: AndroidDevice |
| 67 | +): Promise<string> { |
| 68 | + const options = requestOptions( |
| 69 | + accessToken, |
| 70 | + 'POST', |
| 71 | + '/v1/projects/' + projectId + '/testMatrices' |
| 72 | + ); |
| 73 | + const body = { |
| 74 | + projectId: projectId, |
| 75 | + testSpecification: { |
| 76 | + androidRoboTest: { |
| 77 | + appApk: { |
| 78 | + gcsPath: 'gs://path/to/non-existing-app.apk', |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + environmentMatrix: { |
| 83 | + androidDeviceList: { |
| 84 | + androidDevices: [device], |
| 85 | + }, |
| 86 | + }, |
| 87 | + resultStorage: { |
| 88 | + googleCloudStorage: { |
| 89 | + gcsPath: 'gs://' + admin.storage().bucket().name, |
| 90 | + }, |
| 91 | + }, |
| 92 | + clientInfo: { |
| 93 | + name: 'CloudFunctionsSDKIntegrationTest', |
| 94 | + clientInfoDetails: { |
| 95 | + key: 'testId', |
| 96 | + value: testId, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }; |
| 100 | + return utils.makeRequest(options, JSON.stringify(body)); |
| 101 | +} |
| 102 | + |
| 103 | +function requestOptions( |
| 104 | + accessToken: admin.GoogleOAuthAccessToken, |
| 105 | + method: string, |
| 106 | + path: string |
| 107 | +): https.RequestOptions { |
| 108 | + return { |
| 109 | + method: method, |
| 110 | + hostname: TESTING_API_SERVICE_NAME, |
| 111 | + path: path, |
| 112 | + headers: { |
| 113 | + Authorization: 'Bearer ' + accessToken.access_token, |
| 114 | + 'Content-Type': 'application/json', |
| 115 | + }, |
| 116 | + }; |
| 117 | +} |
0 commit comments