Skip to content

Commit

Permalink
Implement tests for Virtual machine module.
Browse files Browse the repository at this point in the history
Created a test to evaluate the creation of virtual machines.

Signed-off-by: Saeed Rezaee <saeed.rezaee@kynetics.it>
  • Loading branch information
SaeedRe authored and diegorondini committed Mar 6, 2024
1 parent 0c7da68 commit cc584f3
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -33,4 +33,7 @@
.DS_Store

**/build/**
**/out/**
**/out/**

local.properties
detekt-baseline.xml
53 changes: 46 additions & 7 deletions build.gradle
Expand Up @@ -126,6 +126,7 @@ project(':virtual-device'){
group 'org.eclipse.hara.hara-virtual-device'
version app_version
apply plugin: 'application'
apply plugin: 'jacoco'

dependencies {
implementation rootProject
Expand All @@ -134,6 +135,35 @@ project(':virtual-device'){
implementation project(':ddi-consumer:ddi-api')
implementation deps.slf4j_simple
implementation deps.okhttp
testImplementation deps.kotlin_stdlib
testImplementation deps.kotlin_x
testImplementation tdeps.testng
testImplementation tdeps.mockk
testImplementation deps.retrofit
testImplementation deps.okhttp
testImplementation deps.okhttp_logging
testImplementation deps.retrofit_converter
}

jacocoTestReport {
dependsOn test
sourceSets sourceSets.main

reports {
xml.required = true
html.required = true
}
}

test {
dependsOn ':waitingHawkbitServer'

useTestNG()

afterTest { desc, result ->
logger.quiet "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
finalizedBy jacocoTestReport
}

mainClassName = 'org.eclipse.hara.ddiclient.virtualdevice.MainKt'
Expand All @@ -160,20 +190,27 @@ detekt {
buildUponDefaultConfig = true
}

test {
useTestNG()
finalizedBy jacocoTestReport
}

jacocoTestReport {
dependsOn tasks.withType(Test)

sourceSets sourceSets.main
subprojects.each {
sourceSets it.sourceSets.main
}

executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/test.exec")

classDirectories.setFrom(files(sourceSets.main.output) +
files(subprojects.sourceSets.main.output))
sourceDirectories.setFrom(files(sourceSets.main.allSource.srcDirs) +
files(subprojects.sourceSets.main.allSource.srcDirs))
additionalSourceDirs.setFrom(files(sourceSets.main.allSource.srcDirs) +
files(subprojects.sourceSets.main.allSource.srcDirs))

reports {
xml.required = true
html.required = true
}
dependsOn test
}

def dockerClientDir = new File(project.buildDir, "docker-client")
Expand Down Expand Up @@ -257,11 +294,14 @@ test {
systemProperty("LOG_INTERNAL", project.findProperty("logInternal") ?: "false")
systemProperty("BACKOFF_INTERVAL_SECONDS", 45)

dependsOn ':virtual-device:test'

useTestNG()

afterTest { desc, result ->
logger.quiet "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
finalizedBy jacocoTestReport
}


Expand All @@ -274,7 +314,6 @@ dependencies{
implementation deps.gson
implementation deps.retrofit_converter
testImplementation tdeps.testng
testImplementation tdeps.testng
testImplementation deps.kotlin_stdlib
testImplementation deps.kotlin_x
testImplementation deps.joda
Expand Down
3 changes: 2 additions & 1 deletion dependencies.gradle
Expand Up @@ -26,6 +26,7 @@ ext {
joda:'joda-time:joda-time:2.10.13'
]
test_dependencies = [
testng: 'org.testng:testng:7.9.0'
testng: 'org.testng:testng:7.9.0',
mockk: 'io.mockk:mockk:1.12.0'
]
}
@@ -0,0 +1,73 @@
/*
*
* * Copyright © 2017-2024 Kynetics LLC
* *
* * This program and the accompanying materials are made
* * available under the terms of the Eclipse Public License 2.0
* * which is available at https://www.eclipse.org/legal/epl-2.0/
* *
* * SPDX-License-Identifier: EPL-2.0
*
*/

package org.eclipse.hara.ddiclient.virtualdevice

import io.mockk.every
import io.mockk.mockkObject
import kotlinx.coroutines.cancel
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import okhttp3.Credentials
import org.testng.Assert
import org.testng.annotations.BeforeClass
import org.testng.annotations.Test

class MainTest {

companion object {
const val HAWKBIT_URL = "http://localhost:8081"
val BASIC_AUTH = Credentials.basic("test", "test")
val GATEWAY_TOKEN = "66076ab945a127dd80b15e9011995109"

}

private val devicesControllerId = mutableListOf<String>()
private lateinit var managementApi: ManagementApi

@BeforeClass
fun setUp() {
mockkObject(Configuration)
every { Configuration.url } returns HAWKBIT_URL
every { Configuration.poolSize } returns 2
every { Configuration.gatewayToken } returns GATEWAY_TOKEN
every { Configuration.controllerIdGenerator } returns {
val id = "VirtualDevice-number-$it"
devicesControllerId.add(id)
id
}
managementApi = ManagementClient.createManagementApi(HAWKBIT_URL)
runBlocking {
managementApi.setPollingTime(BASIC_AUTH, ServerSystemConfig("00:00:10"))
}

}

@Test(enabled = true)
fun testVirtualDeviceCreationAndPolling() {
runBlocking {
main()

delay(2_000)
val targets = managementApi.getAllTargets(BASIC_AUTH)
val testTargets =
targets.content.filter { devicesControllerId.contains(it.controllerId) }
Assert.assertEquals(devicesControllerId.size, testTargets.size)
testTargets
.forEachIndexed { index, deviceTarget ->
Assert.assertEquals(devicesControllerId[index], deviceTarget.controllerId)
Assert.assertNotNull(deviceTarget.lastControllerRequestAt)
}
virtualMachineGlobalScope.cancel()
}
}
}
@@ -0,0 +1,79 @@
/*
*
* * Copyright © 2017-2024 Kynetics LLC
* *
* * This program and the accompanying materials are made
* * available under the terms of the Eclipse Public License 2.0
* * which is available at https://www.eclipse.org/legal/epl-2.0/
* *
* * SPDX-License-Identifier: EPL-2.0
*
*/

package org.eclipse.hara.ddiclient.virtualdevice

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.http.GET
import retrofit2.http.Header
import java.util.concurrent.Executors
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.PUT
import java.text.SimpleDateFormat
import java.util.Date

val LOG_HTTP: Boolean = System.getProperty("LOG_HTTP", "false").toBoolean()

interface ManagementApi {
companion object {
const val BASE_V1_REQUEST_MAPPING = "/rest/v1"
}

@GET("$BASE_V1_REQUEST_MAPPING/targets")
suspend fun getAllTargets(
@Header("Authorization") auth: String
): HawkbitTargets

@PUT("$BASE_V1_REQUEST_MAPPING/system/configs/pollingTime")
suspend fun setPollingTime(
@Header("Authorization") auth: String,
@Body body: ServerSystemConfig
)
}

object ManagementClient {
fun createManagementApi(baseUrl: String): ManagementApi {
val client = OkHttpClient.Builder().addOkhttpLogger().build()
val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.callbackExecutor(Executors.newSingleThreadExecutor())
.build()
return retrofit.create(ManagementApi::class.java)
}

}

class HawkbitTargets(val content: List<DeviceTarget> )

class DeviceTarget(val controllerId: String?, val lastControllerRequestAt: Long?)

data class ServerSystemConfig(val value: Any)

val currentTime: String
get() = SimpleDateFormat("HH.mm.ss.SSS").format(Date())


fun OkHttpClient.Builder.addOkhttpLogger(): OkHttpClient.Builder = apply {
val logger = HttpLoggingInterceptor.Logger { message ->
if (LOG_HTTP) {
println("$currentTime: OkHttp: $message")
}
}
addInterceptor(HttpLoggingInterceptor(logger).apply {
level = HttpLoggingInterceptor.Level.BODY
})
}

0 comments on commit cc584f3

Please sign in to comment.