diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2022-10-10 15:57:05 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-10 15:57:05 +0200 |
| commit | a832ea376e360f3029036a9570c244fb9080e91f (patch) | |
| tree | a54e3f9d3a44a0248ef1b17430eed9b5d47ff5de /opendc-web/opendc-web-server/src/test | |
| parent | 7ba3b953300c46b4e3afcde17cd3dd14b1af8406 (diff) | |
| parent | 4ebe2f28ba940aabdaa1f57653fbe86a91582ebd (diff) | |
merge: Add support for accounting of user simulation time (#108)
This pull request adds support for tracking and limiting the simulation time consumed per user.
These changes allow users to consume 60 minutes of shared compute resources for simulations
in OpenDC per month.
Closes #103
## Implementation Notes :hammer_and_pick:
* Limit exception mapper to WebApplicationException
* Add support for accounting simulation time
* Add API for querying user accounting data
* Show monthly simulation budget in UI
## External Dependencies :four_leaf_clover:
* N/A
## Breaking API Changes :warning:
* The web runner now also sends the runtime of the simulation job to the server, in order to
update the consumed simulation time in the database.
Diffstat (limited to 'opendc-web/opendc-web-server/src/test')
3 files changed, 277 insertions, 5 deletions
diff --git a/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/runner/JobResourceTest.kt b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/runner/JobResourceTest.kt index 9aca58e9..4a86c928 100644 --- a/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/runner/JobResourceTest.kt +++ b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/runner/JobResourceTest.kt @@ -63,7 +63,7 @@ class JobResourceTest { private val dummyTopology = Topology(1, 1, "test", emptyList(), Instant.now(), Instant.now()) private val dummyTrace = Trace("bitbrains", "Bitbrains", "vm") private val dummyScenario = Scenario(1, 1, dummyPortfolio, "test", Workload(dummyTrace, 1.0), dummyTopology, OperationalPhenomena(false, false), "test") - private val dummyJob = Job(1, dummyScenario, JobState.PENDING, Instant.now(), Instant.now()) + private val dummyJob = Job(1, dummyScenario, JobState.PENDING, Instant.now(), Instant.now(), 0) @BeforeEach fun setUp() { @@ -151,10 +151,10 @@ class JobResourceTest { @Test @TestSecurity(user = "testUser", roles = ["runner"]) fun testUpdateNonExistent() { - every { jobService.updateState(1, any(), any()) } returns null + every { jobService.updateState(1, any(), any(), any()) } returns null Given { - body(Job.Update(JobState.PENDING)) + body(Job.Update(JobState.PENDING, 0)) contentType(ContentType.JSON) } When { post("/1") @@ -170,10 +170,10 @@ class JobResourceTest { @Test @TestSecurity(user = "testUser", roles = ["runner"]) fun testUpdateState() { - every { jobService.updateState(1, any(), any()) } returns dummyJob.copy(state = JobState.CLAIMED) + every { jobService.updateState(1, any(), any(), any()) } returns dummyJob.copy(state = JobState.CLAIMED) Given { - body(Job.Update(JobState.CLAIMED)) + body(Job.Update(JobState.CLAIMED, 0)) contentType(ContentType.JSON) } When { post("/1") diff --git a/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/user/UserResourceTest.kt b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/user/UserResourceTest.kt new file mode 100644 index 00000000..36af20f4 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/rest/user/UserResourceTest.kt @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022 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.web.server.rest.user + +import io.quarkus.test.common.http.TestHTTPEndpoint +import io.quarkus.test.junit.QuarkusTest +import io.quarkus.test.security.TestSecurity +import io.restassured.http.ContentType +import io.restassured.module.kotlin.extensions.Then +import io.restassured.module.kotlin.extensions.When +import org.hamcrest.Matchers +import org.junit.jupiter.api.Test + +/** + * Test suite for [UserResource]. + */ +@QuarkusTest +@TestHTTPEndpoint(UserResource::class) +class UserResourceTest { + /** + * Test that tries to obtain the profile of the active user. + */ + @Test + @TestSecurity(user = "testUser", roles = ["openid"]) + fun testMe() { + When { + get("me") + } Then { + statusCode(200) + contentType(ContentType.JSON) + + body("userId", Matchers.equalTo("testUser")) + body("accounting.simulationTime", Matchers.equalTo(0)) + body("accounting.simulationTimeBudget", Matchers.greaterThan(0)) + } + } + + /** + * Test that tries to obtain the profile of the active user without authorization. + */ + @Test + fun testMeUnauthorized() { + When { + get("me") + } Then { + statusCode(401) + } + } +} diff --git a/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/service/UserAccountingServiceTest.kt b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/service/UserAccountingServiceTest.kt new file mode 100644 index 00000000..fdf04787 --- /dev/null +++ b/opendc-web/opendc-web-server/src/test/kotlin/org/opendc/web/server/service/UserAccountingServiceTest.kt @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2022 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.web.server.service + +import io.mockk.every +import io.mockk.mockk +import io.quarkus.test.junit.QuarkusTest +import org.junit.jupiter.api.Assertions.assertAll +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.opendc.web.server.model.UserAccounting +import org.opendc.web.server.repository.UserAccountingRepository +import java.time.Duration +import java.time.LocalDate +import javax.persistence.EntityExistsException + +/** + * Test suite for the [UserAccountingService]. + */ +@QuarkusTest +class UserAccountingServiceTest { + /** + * The [UserAccountingRepository] that is mocked. + */ + private val repository: UserAccountingRepository = mockk() + + /** + * The [UserAccountingService] instance under test. + */ + private val service: UserAccountingService = UserAccountingService(repository, Duration.ofHours(1)) + + @Test + fun testGetUserDoesNotExist() { + val userId = "test" + + every { repository.findForUser(userId) } returns null + + val accounting = service.getAccounting(userId) + + assertTrue(accounting.periodEnd.isAfter(LocalDate.now())) + assertEquals(0, accounting.simulationTime) + } + + @Test + fun testGetUserDoesExist() { + val userId = "test" + + val now = LocalDate.now() + val periodEnd = now.plusMonths(1) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600).also { it.simulationTime = 32 } + + val accounting = service.getAccounting(userId) + + assertAll( + { assertEquals(periodEnd, accounting.periodEnd) }, + { assertEquals(32, accounting.simulationTime) }, + { assertEquals(3600, accounting.simulationTimeBudget) } + ) + } + + @Test + fun testHasBudgetUserDoesNotExist() { + val userId = "test" + + every { repository.findForUser(userId) } returns null + + assertTrue(service.hasSimulationBudget(userId)) + } + + @Test + fun testHasBudget() { + val userId = "test" + val periodEnd = LocalDate.now().plusMonths(2) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600) + + assertTrue(service.hasSimulationBudget(userId)) + } + + @Test + fun testHasBudgetExceededButPeriodExpired() { + val userId = "test" + val periodEnd = LocalDate.now().minusMonths(2) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600).also { it.simulationTime = 3900 } + + assertTrue(service.hasSimulationBudget(userId)) + } + + @Test + fun testHasBudgetPeriodExpired() { + val userId = "test" + val periodEnd = LocalDate.now().minusMonths(2) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600) + + assertTrue(service.hasSimulationBudget(userId)) + } + + @Test + fun testHasBudgetExceeded() { + val userId = "test" + val periodEnd = LocalDate.now().plusMonths(1) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600).also { it.simulationTime = 3900 } + + assertFalse(service.hasSimulationBudget(userId)) + } + + @Test + fun testConsumeBudgetNewUser() { + val userId = "test" + + every { repository.findForUser(userId) } returns null + every { repository.save(any()) } returns Unit + + assertFalse(service.consumeSimulationBudget(userId, 10)) + } + + @Test + fun testConsumeBudgetNewUserExceeded() { + val userId = "test" + + every { repository.findForUser(userId) } returns null + every { repository.save(any()) } returns Unit + + assertTrue(service.consumeSimulationBudget(userId, 4000)) + } + + @Test + fun testConsumeBudgetNewUserConflict() { + val userId = "test" + + val periodEnd = LocalDate.now().plusMonths(1) + + every { repository.findForUser(userId) } returns null andThen UserAccounting(userId, periodEnd, 3600) + every { repository.save(any()) } throws EntityExistsException() + every { repository.consumeBudget(any(), any()) } answers { + val accounting = it.invocation.args[0] as UserAccounting + accounting.simulationTime -= it.invocation.args[1] as Int + true + } + + assertFalse(service.consumeSimulationBudget(userId, 10)) + } + + @Test + fun testConsumeBudgetResetSuccess() { + val userId = "test" + + val periodEnd = LocalDate.now().minusMonths(2) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600).also { it.simulationTime = 3900 } + every { repository.resetBudget(any(), any(), any()) } answers { + val accounting = it.invocation.args[0] as UserAccounting + accounting.periodEnd = it.invocation.args[1] as LocalDate + accounting.simulationTime = it.invocation.args[2] as Int + true + } + + assertTrue(service.consumeSimulationBudget(userId, 4000)) + } + + @Test + fun testInfiniteConflict() { + val userId = "test" + + val periodEnd = LocalDate.now().plusMonths(1) + + every { repository.findForUser(userId) } returns UserAccounting(userId, periodEnd, 3600) + every { repository.consumeBudget(any(), any()) } answers { + val accounting = it.invocation.args[0] as UserAccounting + accounting.simulationTime -= it.invocation.args[1] as Int + false + } + + assertThrows<IllegalStateException> { service.consumeSimulationBudget(userId, 10) } + } +} |
