Created
June 7, 2019 15:50
-
-
Save jamesknowsbest/3ea0e385988b0098e5f9d38bf5a932b6 to your computer and use it in GitHub Desktop.
short script ot upload a file and schedule a run from AWS Lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// assume we already executed `npm install aws-sdk` | |
var AWS = require('aws-sdk'); | |
// assumes `npm install https` | |
const request = require("request"); | |
// assumes `npm install fs` | |
const fs = require('fs'); | |
// https://stackoverflow.com/a/41641607/8016330 | |
const sleep = (waitTimeInMs) => new Promise(resolve => setTimeout(resolve, waitTimeInMs)); | |
// Device Farm is only available in the us-west-2 region | |
var devicefarm = new AWS.DeviceFarm({ region: 'us-west-2' }); | |
exports.handler = async (event) => { | |
let project_params = { | |
name: "test of fuzz tests" | |
}; | |
let PROJECT_ARN = await devicefarm.createProject(project_params).promise().then( | |
function(data){ | |
return data.project.arn; | |
}, | |
function (error) { | |
console.error("Error creating project", "Error: ", error); | |
} | |
); | |
console.log("Project created ", "Project arn: ", PROJECT_ARN); | |
// create the upload and upload files to the project | |
let params = { | |
name: "app-debug.apk", | |
type: "ANDROID_APP", | |
projectArn: PROJECT_ARN | |
}; | |
let UPLOAD = await devicefarm.createUpload(params).promise().then( | |
function(data){ | |
return data.upload; | |
}, | |
function(error){ | |
console.error("Creating upload failed with error: ", error); | |
} | |
); | |
let UPLOAD_ARN = UPLOAD.arn; | |
let UPLOAD_URL = UPLOAD.url; | |
console.log("upload created with arn: ", UPLOAD_ARN); | |
console.log("uploading file..."); | |
let options = { | |
method: 'PUT', | |
url: UPLOAD_URL, | |
headers: {}, | |
body: fs.readFileSync("./app-debug.apk") | |
}; | |
// wait for upload to finish | |
await new Promise(function(resolve,reject){ | |
request(options, function (error, response, body) { | |
if (error) { | |
console.error("uploading file failed with error: ", error); | |
reject(error); | |
} | |
resolve(body); | |
}); | |
}); | |
//get the status of the upload and make sure if finished processing before scheduling | |
let STATUS = await getStatus(UPLOAD_ARN); | |
console.log("upload status is: ", STATUS); | |
while(STATUS !== "SUCCEEDED"){ | |
await sleep(5000); | |
STATUS = await getStatus(UPLOAD_ARN); | |
console.log("upload status is: ", STATUS); | |
} | |
//create device pool | |
let device_pool_params = { | |
projectArn: PROJECT_ARN, | |
name: "Google Pixel 2", | |
rules: [{"attribute": "ARN","operator":"IN","value":"[\"arn:aws:devicefarm:us-west-2::device:5F20BBED05F74D6288D51236B0FB9895\"]"}] | |
} | |
let DEVICE_POOL_ARN = await devicefarm.createDevicePool(device_pool_params).promise().then( | |
function(data){ | |
return data.devicePool.arn; | |
},function(error){ | |
console.error("device pool failed to create with error: ",error); | |
} | |
); | |
console.log("Device pool created successfully with arn: ", DEVICE_POOL_ARN); | |
//schedule the run | |
let schedule_run_params = { | |
name: "MyRun", | |
devicePoolArn: DEVICE_POOL_ARN, // You can get the Amazon Resource Name (ARN) of the device pool by using the list-pools CLI command. | |
projectArn: PROJECT_ARN, // You can get the Amazon Resource Name (ARN) of the project by using the list-projects CLI command. | |
test: { | |
type: "BUILTIN_FUZZ" | |
}, | |
appArn: UPLOAD_ARN | |
}; | |
let schedule_run_result = await devicefarm.scheduleRun(schedule_run_params).promise().then( | |
function(data){ | |
return data.run; | |
},function(error){ | |
console.error("Schedule run command failed with error: ", error); | |
} | |
); | |
console.log("run finished successfully with result: ", schedule_run_result); | |
return schedule_run_result; | |
}; | |
async function getStatus(UPLOAD_ARN){ | |
return await devicefarm.getUpload({arn: UPLOAD_ARN}).promise().then( | |
function(data){ | |
return data.upload.status; | |
},function(error){ | |
console.error("getting upload failed with error: ", error); | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment