From 4dfae28c5bd656806a7baf7855c95770f4ad0ed8 Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Wed, 2 Nov 2022 17:20:00 +0100 Subject: refactor(compute/service): Do not split interface and implementation This change inlines the implementation of the compute service into the `ComputeService` interface. We do not intend to provide multiple implementations of the service. In addition, this approach makes more sense for a Java implementation. --- .../opendc/compute/service/ComputeServiceTest.kt | 22 +- .../opendc/compute/service/InternalFlavorTest.kt | 81 ------ .../opendc/compute/service/InternalImageTest.kt | 82 ------ .../opendc/compute/service/InternalServerTest.kt | 305 --------------------- .../opendc/compute/service/ServiceFlavorTest.kt | 67 +++++ .../org/opendc/compute/service/ServiceImageTest.kt | 67 +++++ .../opendc/compute/service/ServiceServerTest.kt | 286 +++++++++++++++++++ .../service/scheduler/FilterSchedulerTest.kt | 2 +- 8 files changed, 423 insertions(+), 489 deletions(-) delete mode 100644 opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalFlavorTest.kt delete mode 100644 opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalImageTest.kt delete mode 100644 opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalServerTest.kt create mode 100644 opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ServiceFlavorTest.kt create mode 100644 opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ServiceImageTest.kt create mode 100644 opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ServiceServerTest.kt (limited to 'opendc-compute/opendc-compute-service/src/test') diff --git a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt index a64c0885..4dc1cfa8 100644 --- a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt +++ b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ComputeServiceTest.kt @@ -50,6 +50,7 @@ import org.opendc.compute.service.scheduler.filters.VCpuFilter import org.opendc.compute.service.scheduler.weights.RamWeigher import org.opendc.simulator.kotlin.SimulationCoroutineScope import org.opendc.simulator.kotlin.runSimulation +import java.time.Duration import java.util.UUID /** @@ -66,7 +67,7 @@ internal class ComputeServiceTest { filters = listOf(ComputeFilter(), VCpuFilter(allocationRatio = 1.0), RamFilter(allocationRatio = 1.0)), weighers = listOf(RamWeigher()) ) - service = ComputeService(scope.dispatcher, computeScheduler) + service = ComputeService(scope.dispatcher, computeScheduler, Duration.ofMinutes(5)) } @Test @@ -299,25 +300,6 @@ internal class ComputeServiceTest { verify(exactly = 0) { host.canFit(server) } } - @Test - fun testServerInvalidType() = scope.runSimulation { - val host = mockk(relaxUnitFun = true) - val server = mockk(relaxUnitFun = true) - val listeners = mutableListOf() - - every { host.uid } returns UUID.randomUUID() - every { host.model } returns HostModel(4 * 2600.0, 4, 2048) - every { host.state } returns HostState.UP - every { host.canFit(any()) } returns true - every { host.addListener(any()) } answers { listeners.add(it.invocation.args[0] as HostListener) } - - service.addHost(host) - - assertThrows { - listeners.forEach { it.onStateChanged(host, server, ServerState.RUNNING) } - } - } - @Test fun testServerDeploy() = scope.runSimulation { val host = mockk(relaxUnitFun = true) diff --git a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalFlavorTest.kt b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalFlavorTest.kt deleted file mode 100644 index fe92f7f2..00000000 --- a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalFlavorTest.kt +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2021 AtLarge Research - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.opendc.compute.service - -import io.mockk.every -import io.mockk.mockk -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertNotEquals -import org.junit.jupiter.api.Test -import org.opendc.compute.api.Flavor -import org.opendc.compute.service.internal.ComputeServiceImpl -import org.opendc.compute.service.internal.InternalFlavor -import java.util.UUID - -/** - * Test suite for the [InternalFlavor] implementation. - */ -class InternalFlavorTest { - @Test - fun testEquality() { - val service = mockk() - val uid = UUID.randomUUID() - val a = InternalFlavor(service, uid, "test", 1, 1024, mutableMapOf(), mutableMapOf()) - val b = InternalFlavor(service, uid, "test", 1, 1024, mutableMapOf(), mutableMapOf()) - - assertEquals(a, b) - } - - @Test - fun testEqualityWithDifferentType() { - val service = mockk() - val uid = UUID.randomUUID() - val a = InternalFlavor(service, uid, "test", 1, 1024, mutableMapOf(), mutableMapOf()) - - val b = mockk(relaxUnitFun = true) - every { b.uid } returns uid - - assertEquals(a, b) - } - - @Test - fun testInequalityWithDifferentType() { - val service = mockk() - val uid = UUID.randomUUID() - val a = InternalFlavor(service, uid, "test", 1, 1024, mutableMapOf(), mutableMapOf()) - - val b = mockk(relaxUnitFun = true) - every { b.uid } returns UUID.randomUUID() - - assertNotEquals(a, b) - } - - @Test - fun testInequalityWithIncorrectType() { - val service = mockk() - val uid = UUID.randomUUID() - val a = InternalFlavor(service, uid, "test", 1, 1024, mutableMapOf(), mutableMapOf()) - - assertNotEquals(a, Unit) - } -} diff --git a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalImageTest.kt b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalImageTest.kt deleted file mode 100644 index d60aa628..00000000 --- a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalImageTest.kt +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2021 AtLarge Research - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.opendc.compute.service - -import io.mockk.every -import io.mockk.mockk -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertNotEquals -import org.junit.jupiter.api.Test -import org.opendc.compute.api.Image -import org.opendc.compute.service.internal.ComputeServiceImpl -import org.opendc.compute.service.internal.InternalFlavor -import org.opendc.compute.service.internal.InternalImage -import java.util.UUID - -/** - * Test suite for the [InternalFlavor] implementation. - */ -class InternalImageTest { - @Test - fun testEquality() { - val service = mockk() - val uid = UUID.randomUUID() - val a = InternalImage(service, uid, "test", mutableMapOf(), mutableMapOf()) - val b = InternalImage(service, uid, "test", mutableMapOf(), mutableMapOf()) - - assertEquals(a, b) - } - - @Test - fun testEqualityWithDifferentType() { - val service = mockk() - val uid = UUID.randomUUID() - val a = InternalImage(service, uid, "test", mutableMapOf(), mutableMapOf()) - - val b = mockk(relaxUnitFun = true) - every { b.uid } returns uid - - assertEquals(a, b) - } - - @Test - fun testInequalityWithDifferentType() { - val service = mockk() - val uid = UUID.randomUUID() - val a = InternalImage(service, uid, "test", mutableMapOf(), mutableMapOf()) - - val b = mockk(relaxUnitFun = true) - every { b.uid } returns UUID.randomUUID() - - assertNotEquals(a, b) - } - - @Test - fun testInequalityWithIncorrectType() { - val service = mockk() - val uid = UUID.randomUUID() - val a = InternalImage(service, uid, "test", mutableMapOf(), mutableMapOf()) - - assertNotEquals(a, Unit) - } -} diff --git a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalServerTest.kt b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalServerTest.kt deleted file mode 100644 index 05a8160e..00000000 --- a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/InternalServerTest.kt +++ /dev/null @@ -1,305 +0,0 @@ -/* - * Copyright (c) 2021 AtLarge Research - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package org.opendc.compute.service - -import io.mockk.coVerify -import io.mockk.every -import io.mockk.mockk -import io.mockk.verify -import kotlinx.coroutines.yield -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertNotEquals -import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.assertThrows -import org.opendc.compute.api.Server -import org.opendc.compute.api.ServerState -import org.opendc.compute.service.driver.Host -import org.opendc.compute.service.internal.ComputeServiceImpl -import org.opendc.compute.service.internal.InternalFlavor -import org.opendc.compute.service.internal.InternalImage -import org.opendc.compute.service.internal.InternalServer -import org.opendc.simulator.kotlin.runSimulation -import java.util.UUID - -/** - * Test suite for the [InternalServer] implementation. - */ -class InternalServerTest { - @Test - fun testEquality() { - val service = mockk() - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - - val a = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - val b = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - - assertEquals(a, b) - } - - @Test - fun testEqualityWithDifferentType() { - val service = mockk() - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val a = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - - val b = mockk(relaxUnitFun = true) - every { b.uid } returns uid - - assertEquals(a, b) - } - - @Test - fun testInequalityWithDifferentType() { - val service = mockk() - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val a = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - - val b = mockk(relaxUnitFun = true) - every { b.uid } returns UUID.randomUUID() - - assertNotEquals(a, b) - } - - @Test - fun testInequalityWithIncorrectType() { - val service = mockk() - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val a = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - - assertNotEquals(a, Unit) - } - - @Test - fun testStartTerminatedServer() = runSimulation { - val service = mockk() - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val server = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - - every { service.schedule(any()) } answers { ComputeServiceImpl.SchedulingRequest(it.invocation.args[0] as InternalServer, 0) } - - server.start() - - verify(exactly = 1) { service.schedule(server) } - assertEquals(ServerState.PROVISIONING, server.state) - } - - @Test - fun testStartDeletedServer() = runSimulation { - val service = mockk() - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val server = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - - server.state = ServerState.DELETED - - assertThrows { server.start() } - } - - @Test - fun testStartProvisioningServer() = runSimulation { - val service = mockk() - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val server = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - - server.state = ServerState.PROVISIONING - - server.start() - - assertEquals(ServerState.PROVISIONING, server.state) - } - - @Test - fun testStartRunningServer() = runSimulation { - val service = mockk() - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val server = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - - server.state = ServerState.RUNNING - - server.start() - - assertEquals(ServerState.RUNNING, server.state) - } - - @Test - fun testStopProvisioningServer() = runSimulation { - val service = mockk() - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val server = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - val request = ComputeServiceImpl.SchedulingRequest(server, 0) - - every { service.schedule(any()) } returns request - - server.start() - server.stop() - - assertTrue(request.isCancelled) - assertEquals(ServerState.TERMINATED, server.state) - } - - @Test - fun testStopTerminatedServer() = runSimulation { - val service = mockk() - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val server = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - - server.state = ServerState.TERMINATED - server.stop() - - assertEquals(ServerState.TERMINATED, server.state) - } - - @Test - fun testStopDeletedServer() = runSimulation { - val service = mockk() - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val server = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - - server.state = ServerState.DELETED - server.stop() - - assertEquals(ServerState.DELETED, server.state) - } - - @Test - fun testStopRunningServer() = runSimulation { - val service = mockk() - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val server = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - val host = mockk(relaxUnitFun = true) - - server.state = ServerState.RUNNING - server.host = host - server.stop() - yield() - - coVerify { host.stop(server) } - } - - @Test - fun testDeleteProvisioningServer() = runSimulation { - val service = mockk(relaxUnitFun = true) - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val server = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - val request = ComputeServiceImpl.SchedulingRequest(server, 0) - - every { service.schedule(any()) } returns request - - server.start() - server.delete() - - assertTrue(request.isCancelled) - assertEquals(ServerState.DELETED, server.state) - verify { service.delete(server) } - } - - @Test - fun testDeleteTerminatedServer() = runSimulation { - val service = mockk(relaxUnitFun = true) - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val server = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - - server.state = ServerState.TERMINATED - server.delete() - - assertEquals(ServerState.DELETED, server.state) - - verify { service.delete(server) } - } - - @Test - fun testDeleteDeletedServer() = runSimulation { - val service = mockk(relaxUnitFun = true) - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val server = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - - server.state = ServerState.DELETED - server.delete() - - assertEquals(ServerState.DELETED, server.state) - } - - @Test - fun testDeleteRunningServer() = runSimulation { - val service = mockk(relaxUnitFun = true) - val uid = UUID.randomUUID() - val flavor = mockFlavor() - val image = mockImage() - val server = InternalServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) - val host = mockk(relaxUnitFun = true) - - server.state = ServerState.RUNNING - server.host = host - server.delete() - yield() - - coVerify { host.delete(server) } - verify { service.delete(server) } - } - - private fun mockFlavor(): InternalFlavor { - val flavor = mockk() - every { flavor.name } returns "c5.large" - every { flavor.uid } returns UUID.randomUUID() - every { flavor.cpuCount } returns 2 - every { flavor.memorySize } returns 4096 - return flavor - } - - private fun mockImage(): InternalImage { - val image = mockk() - every { image.name } returns "ubuntu-20.04" - every { image.uid } returns UUID.randomUUID() - return image - } -} diff --git a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ServiceFlavorTest.kt b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ServiceFlavorTest.kt new file mode 100644 index 00000000..7938f789 --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ServiceFlavorTest.kt @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service + +import io.mockk.every +import io.mockk.mockk +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotEquals +import org.junit.jupiter.api.Test +import org.opendc.compute.api.Flavor +import java.util.UUID + +/** + * Test suite for the [ServiceFlavor] implementation. + */ +class ServiceFlavorTest { + @Test + fun testEquality() { + val service = mockk() + val uid = UUID.randomUUID() + val a = ServiceFlavor(service, uid, "test", 1, 1024, mutableMapOf(), mutableMapOf()) + val b = ServiceFlavor(service, uid, "test", 1, 1024, mutableMapOf(), mutableMapOf()) + + assertEquals(a, b) + } + + @Test + fun testInequalityWithDifferentType() { + val service = mockk() + val uid = UUID.randomUUID() + val a = ServiceFlavor(service, uid, "test", 1, 1024, mutableMapOf(), mutableMapOf()) + + val b = mockk(relaxUnitFun = true) + every { b.uid } returns UUID.randomUUID() + + assertNotEquals(a, b) + } + + @Test + fun testInequalityWithIncorrectType() { + val service = mockk() + val uid = UUID.randomUUID() + val a = ServiceFlavor(service, uid, "test", 1, 1024, mutableMapOf(), mutableMapOf()) + + assertNotEquals(a, Unit) + } +} diff --git a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ServiceImageTest.kt b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ServiceImageTest.kt new file mode 100644 index 00000000..c36d75f4 --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ServiceImageTest.kt @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service + +import io.mockk.every +import io.mockk.mockk +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotEquals +import org.junit.jupiter.api.Test +import org.opendc.compute.api.Image +import java.util.UUID + +/** + * Test suite for the [ServiceFlavor] implementation. + */ +class ServiceImageTest { + @Test + fun testEquality() { + val service = mockk() + val uid = UUID.randomUUID() + val a = ServiceImage(service, uid, "test", mutableMapOf(), mutableMapOf()) + val b = ServiceImage(service, uid, "test", mutableMapOf(), mutableMapOf()) + + assertEquals(a, b) + } + + @Test + fun testInequalityWithDifferentType() { + val service = mockk() + val uid = UUID.randomUUID() + val a = ServiceImage(service, uid, "test", mutableMapOf(), mutableMapOf()) + + val b = mockk(relaxUnitFun = true) + every { b.uid } returns UUID.randomUUID() + + assertNotEquals(a, b) + } + + @Test + fun testInequalityWithIncorrectType() { + val service = mockk() + val uid = UUID.randomUUID() + val a = ServiceImage(service, uid, "test", mutableMapOf(), mutableMapOf()) + + assertNotEquals(a, Unit) + } +} diff --git a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ServiceServerTest.kt b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ServiceServerTest.kt new file mode 100644 index 00000000..f9fcd27b --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/ServiceServerTest.kt @@ -0,0 +1,286 @@ +/* + * Copyright (c) 2021 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service + +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import kotlinx.coroutines.yield +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotEquals +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.opendc.compute.api.Server +import org.opendc.compute.api.ServerState +import org.opendc.compute.service.driver.Host +import org.opendc.simulator.kotlin.runSimulation +import java.util.UUID + +/** + * Test suite for the [ServiceServer] implementation. + */ +class ServiceServerTest { + @Test + fun testEquality() { + val service = mockk() + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + + val a = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + val b = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + + assertEquals(a, b) + } + + @Test + fun testInequalityWithDifferentType() { + val service = mockk() + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val a = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + + val b = mockk(relaxUnitFun = true) + every { b.uid } returns UUID.randomUUID() + + assertNotEquals(a, b) + } + + @Test + fun testInequalityWithIncorrectType() { + val service = mockk() + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val a = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + + assertNotEquals(a, Unit) + } + + @Test + fun testStartTerminatedServer() = runSimulation { + val service = mockk() + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val server = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + + every { service.schedule(any()) } answers { ComputeService.SchedulingRequest(it.invocation.args[0] as ServiceServer, 0) } + + server.start() + + verify(exactly = 1) { service.schedule(server) } + assertEquals(ServerState.PROVISIONING, server.state) + } + + @Test + fun testStartDeletedServer() = runSimulation { + val service = mockk() + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val server = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + + server.setState(ServerState.DELETED) + + assertThrows { server.start() } + } + + @Test + fun testStartProvisioningServer() = runSimulation { + val service = mockk() + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val server = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + + server.setState(ServerState.PROVISIONING) + + server.start() + + assertEquals(ServerState.PROVISIONING, server.state) + } + + @Test + fun testStartRunningServer() = runSimulation { + val service = mockk() + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val server = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + + server.setState(ServerState.RUNNING) + + server.start() + + assertEquals(ServerState.RUNNING, server.state) + } + + @Test + fun testStopProvisioningServer() = runSimulation { + val service = mockk() + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val server = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + val request = ComputeService.SchedulingRequest(server, 0) + + every { service.schedule(any()) } returns request + + server.start() + server.stop() + + assertTrue(request.isCancelled) + assertEquals(ServerState.TERMINATED, server.state) + } + + @Test + fun testStopTerminatedServer() = runSimulation { + val service = mockk() + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val server = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + + server.setState(ServerState.TERMINATED) + server.stop() + + assertEquals(ServerState.TERMINATED, server.state) + } + + @Test + fun testStopDeletedServer() = runSimulation { + val service = mockk() + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val server = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + + server.setState(ServerState.DELETED) + server.stop() + + assertEquals(ServerState.DELETED, server.state) + } + + @Test + fun testStopRunningServer() = runSimulation { + val service = mockk() + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val server = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + val host = mockk(relaxUnitFun = true) + + server.setState(ServerState.RUNNING) + server.host = host + server.stop() + yield() + + verify { host.stop(server) } + } + + @Test + fun testDeleteProvisioningServer() = runSimulation { + val service = mockk(relaxUnitFun = true) + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val server = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + val request = ComputeService.SchedulingRequest(server, 0) + + every { service.schedule(any()) } returns request + + server.start() + server.delete() + + assertTrue(request.isCancelled) + assertEquals(ServerState.DELETED, server.state) + verify { service.delete(server) } + } + + @Test + fun testDeleteTerminatedServer() = runSimulation { + val service = mockk(relaxUnitFun = true) + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val server = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + + server.setState(ServerState.TERMINATED) + server.delete() + + assertEquals(ServerState.DELETED, server.state) + + verify { service.delete(server) } + } + + @Test + fun testDeleteDeletedServer() = runSimulation { + val service = mockk(relaxUnitFun = true) + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val server = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + + server.setState(ServerState.DELETED) + server.delete() + + assertEquals(ServerState.DELETED, server.state) + } + + @Test + fun testDeleteRunningServer() = runSimulation { + val service = mockk(relaxUnitFun = true) + val uid = UUID.randomUUID() + val flavor = mockFlavor() + val image = mockImage() + val server = ServiceServer(service, uid, "test", flavor, image, mutableMapOf(), mutableMapOf()) + val host = mockk(relaxUnitFun = true) + + server.setState(ServerState.RUNNING) + server.host = host + server.delete() + yield() + + verify { host.delete(server) } + verify { service.delete(server) } + } + + private fun mockFlavor(): ServiceFlavor { + val flavor = mockk() + every { flavor.name } returns "c5.large" + every { flavor.uid } returns UUID.randomUUID() + every { flavor.cpuCount } returns 2 + every { flavor.memorySize } returns 4096 + return flavor + } + + private fun mockImage(): ServiceImage { + val image = mockk() + every { image.name } returns "ubuntu-20.04" + every { image.uid } returns UUID.randomUUID() + return image + } +} diff --git a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/scheduler/FilterSchedulerTest.kt b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/scheduler/FilterSchedulerTest.kt index 4608bf37..4af6f7ec 100644 --- a/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/scheduler/FilterSchedulerTest.kt +++ b/opendc-compute/opendc-compute-service/src/test/kotlin/org/opendc/compute/service/scheduler/FilterSchedulerTest.kt @@ -30,9 +30,9 @@ import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertAll import org.junit.jupiter.api.assertThrows import org.opendc.compute.api.Server +import org.opendc.compute.service.HostView import org.opendc.compute.service.driver.HostModel import org.opendc.compute.service.driver.HostState -import org.opendc.compute.service.internal.HostView import org.opendc.compute.service.scheduler.filters.ComputeFilter import org.opendc.compute.service.scheduler.filters.DifferentHostFilter import org.opendc.compute.service.scheduler.filters.InstanceCountFilter -- cgit v1.2.3