-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement tests for Virtual machine module.
Created a test to evaluate the creation of virtual machines. Signed-off-by: Saeed Rezaee <saeed.rezaee@kynetics.it>
- Loading branch information
1 parent
0c7da68
commit cc584f3
Showing
5 changed files
with
204 additions
and
9 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,7 @@ | |
.DS_Store | ||
|
||
**/build/** | ||
**/out/** | ||
**/out/** | ||
|
||
local.properties | ||
detekt-baseline.xml |
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
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
73 changes: 73 additions & 0 deletions
73
virtual-device/src/test/kotlin/org/eclipse/hara/ddiclient/virtualdevice/MainTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
virtual-device/src/test/kotlin/org/eclipse/hara/ddiclient/virtualdevice/ManagementApi.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}) | ||
} |