Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephBK committed Jul 11, 2019
1 parent 70c117a commit a5bf0d0
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 356 deletions.
102 changes: 100 additions & 2 deletions buildSrc/src/main/groovy/com/cliqz/gradle/TestdroidTask.groovy
Original file line number Diff line number Diff line change
@@ -1,18 +1,116 @@
package com.cliqz.gradle

import okhttp3.FormBody
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import okhttp3.Response
import okio.BufferedSink
import org.apache.http.entity.mime.content.FileBody
import org.bouncycastle.cert.ocsp.Req
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import groovy.json.JsonSlurper
import org.jetbrains.annotations.NotNull

class TestdroidTask extends DefaultTask {
class TestDroidTask extends DefaultTask {

static final PROJECT_ID = System.getenv("PROJECT_ID")
static final API_KEY = System.getenv("API_KEY")
static final FRAMEWORK_ID = System.getenv("FRAMEWORK_ID")
static final DEVICE_GROUP_ID = System.getenv("DEVICE_GROUP_ID")

final OkHttpClient client = new OkHttpClient()
final slurper = new JsonSlurper()

@TaskAction
def connectTestdroid() {
println("Hello, world!!!")
// 1. Does the project identified by the id param exists? Decide yourself how to pass this param
final url = new URL("https://cloud.bitbar.com/api/me/projects/${PROJECT_ID}")
final request = url.openConnection()
request.setRequestProperty('Authorization', "Basic ${getBasicAuth(API_KEY)}")
if (request.responseCode == HttpURLConnection.HTTP_OK) {
logger.info("Project exists")
} else {
logger.error("No project for the given id: ${PROJECT_ID}")
}
// 2. Upload the 2 apks and store the ids
final apkID = uploadFile("/Users/kiizajosephbazaare/browser-android/app/build/outputs/apk/cliqz/debug/app-cliqz-arm64-v8a-debug.apk")
final testsID = uploadFile("/Users/kiizajosephbazaare/browser-android/app/build/outputs/apk/androidTest/cliqz/debug/app-cliqz-debug-androidTest.apk")
// 3. Create the testrun and run it
final runID = performTestRun(apkID, testsID)
// 4. Poll every minute (os so) to check the job finished
String state = pollTestRunStatus(runID)
int timeTaken = 0
while (state!= "FINISHED"){
sleep(60000)
timeTaken+=1
state = pollTestRunStatus(runID)
}
logger.info("Tests finished in ${timeTaken} minutes")
// 5. Download the artifacts and put them in a predefined folder (where Azure/Jenkins can find it)
// 6. log error if any test failed, otherwise just return
}

def getBasicAuth(String apiKey) {
"${apiKey}:".bytes.encodeBase64().toString()
}

def uploadFile(String filepath){
final file = new File(filepath)
final mediaType = MediaType.parse('application/octet-stream')
def requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart('file', file.name, RequestBody.create(file, mediaType))
.build()
def request = new Request.Builder()
.header('Authorization', "Basic ${getBasicAuth(API_KEY)}")
.url("https://cloud.bitbar.com/api/me/files")
.post(requestBody)
.build()
final response = client.newCall(request).execute()
if (!response.isSuccessful()) throw new IOException("Unexpected file" + response.code)
final json = slurper.parseText(response.body.string())
json.id
}

def performTestRun(long appFileID, long testFileID){
final mediaType = MediaType.parse('application/json')
final content = """
{
"osType":"ANDROID",
"projectId":"$PROJECT_ID",
"files":[
{"id":"$appFileID"},
{"id":"$testFileID"}
],
"frameworkId":"$FRAMEWORK_ID",
"deviceGroupId":"$DEVICE_GROUP_ID"
}""".stripIndent()

def request = new Request.Builder()
.header('Authorization', "Basic ${getBasicAuth(API_KEY)}")
.url("https://cloud.bitbar.com/api/me/runs")
.post(RequestBody.create(content, mediaType))
.build()
final response = client.newCall(request).execute()
if (!response.isSuccessful()) throw new IOException("Test run not possible " + response.code)
final testRun = slurper.parseText(response.body.string())
response.close()
testRun.id
}

def pollTestRunStatus(long testID){
final poll = new URL("https://cloud.bitbar.com/api/me/projects/${PROJECT_ID}/runs/${testID}")
final check = poll.openConnection()
check.setRequestProperty('Authorization', "Basic ${getBasicAuth(API_KEY)}")
if (check.responseCode == HttpURLConnection.HTTP_OK){
final pollStatus = slurper.parseText (check.content.text)
pollStatus.state
} else {
logger.error("No Test Run for the given id: ${testID}")
}
}
}
7 changes: 6 additions & 1 deletion buildSrc/src/main/java/com/cliqz/gradle/CliqzPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ class CliqzPlugin: Plugin<Project> {
private fun createTestdroidTask(project: Project, variant: ApplicationVariant) {
val taskName = "connect${variant.name.capitalize()}TestDroid"
// Make sure this task depends on the assembling of the app and tests, we need those file to be uploaded
project.tasks.register(taskName, TestdroidTask::class.java)
val assembleTestProvider = project.tasks.named("assemble${variant.name.capitalize()}AndroidTest")
val assembleProvider = project.tasks.named("assemble${variant.name.capitalize()}")
val taskProvider = project.tasks.register(taskName, TestDroidTask::class.java)
taskProvider.configure {
it.dependsOn(assembleProvider, assembleTestProvider)
}
}

private fun createCliqzConfigTasks(project: Project, variant: ApplicationVariant) {
Expand Down
25 changes: 0 additions & 25 deletions testdroid/build.gradle

This file was deleted.

144 changes: 0 additions & 144 deletions testdroid/src/main/groovy/TestDroid.groovy

This file was deleted.

Loading

0 comments on commit a5bf0d0

Please sign in to comment.