Skip to content

Commit ee74f11

Browse files
author
Chase Engelbrecht
committed
Add tests
Signed-off-by: Chase Engelbrecht <engechas@dev-dsk-engechas-2a-28f138b0.us-west-2.amazon.com>
1 parent ebd4132 commit ee74f11

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

alerting/src/test/kotlin/org/opensearch/alerting/transport/TransportGetMonitorActionTests.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55

66
package org.opensearch.alerting.transport
77

8+
import com.carrotsearch.randomizedtesting.ThreadFilter
9+
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters
810
import org.junit.Before
911
import org.mockito.ArgumentCaptor
1012
import org.mockito.ArgumentMatchers.any
1113
import org.mockito.Mockito
14+
import org.mockito.Mockito.never
1215
import org.mockito.Mockito.verify
16+
import org.opensearch.action.search.SearchRequest
1317
import org.opensearch.action.support.ActionFilters
1418
import org.opensearch.alerting.AlertingPlugin.Companion.TENANT_ID_HEADER
1519
import org.opensearch.alerting.settings.AlertingSettings
@@ -34,8 +38,13 @@ import java.util.concurrent.CompletableFuture
3438
import java.util.concurrent.CompletionStage
3539
import org.mockito.Mockito.`when` as whenever
3640

41+
@ThreadLeakFilters(filters = [TransportGetMonitorActionTests.CoroutineThreadFilter::class])
3742
class TransportGetMonitorActionTests : OpenSearchTestCase() {
3843

44+
class CoroutineThreadFilter : ThreadFilter {
45+
override fun reject(t: Thread): Boolean = t.name.startsWith("DefaultDispatcher-worker")
46+
}
47+
3948
private lateinit var client: Client
4049
private lateinit var sdkClient: SdkClient
4150
private lateinit var transportService: TransportService
@@ -133,6 +142,29 @@ class TransportGetMonitorActionTests : OpenSearchTestCase() {
133142
verify(listener).onFailure(any())
134143
}
135144

145+
fun `test multi-tenancy enabled skips getAssociatedWorkflows search`() {
146+
val settings = Settings.builder()
147+
.put("plugins.alerting.multi_tenancy_enabled", true)
148+
.build()
149+
150+
val action = createAction(settings)
151+
// Invoke the private getAssociatedWorkflows method via reflection
152+
val method = action.javaClass.getDeclaredMethod("getAssociatedWorkflows", String::class.java, kotlin.coroutines.Continuation::class.java)
153+
method.isAccessible = true
154+
155+
// Use runBlocking to call the suspend function
156+
val result = kotlinx.coroutines.runBlocking {
157+
kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn<List<*>> { cont ->
158+
method.invoke(action, "test-monitor-id", cont)
159+
}
160+
}
161+
162+
// Should return empty list without searching
163+
assertTrue(result.isEmpty())
164+
// client.search should never be called
165+
verify(client, never()).search(any(SearchRequest::class.java), any())
166+
}
167+
136168
private fun invokeDoExecute(
137169
action: TransportGetMonitorAction,
138170
request: GetMonitorRequest,

alerting/src/test/kotlin/org/opensearch/alerting/transport/TransportIndexMonitorActionTests.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,23 @@ class TransportIndexMonitorActionTests : OpenSearchTestCase() {
140140
assertEquals("", AlertingSettings.JOB_QUEUE_NAME.get(Settings.EMPTY))
141141
assertEquals("", AlertingSettings.EXTERNAL_SCHEDULER_ROLE_ARN.get(Settings.EMPTY))
142142
}
143+
144+
fun `test multi-tenancy enabled skips scheduled job index init`() {
145+
val settings = Settings.builder()
146+
.put("plugins.alerting.multi_tenancy_enabled", true)
147+
.build()
148+
// When multi-tenancy is enabled, start() should skip scheduledJobIndices calls.
149+
// ScheduledJobIndices.scheduledJobIndexExists() calls clusterService.state() which
150+
// is not stubbed (returns null). If the skip logic is broken, this would NPE.
151+
val action = createAction(settings)
152+
assertNotNull(action)
153+
}
154+
155+
fun `test multi-tenancy disabled uses scheduled job index`() {
156+
val settings = Settings.builder()
157+
.put("plugins.alerting.multi_tenancy_enabled", false)
158+
.build()
159+
val action = createAction(settings)
160+
assertNotNull(action)
161+
}
143162
}

0 commit comments

Comments
 (0)