Skip to content

Commit

Permalink
add tests for chained alert
Browse files Browse the repository at this point in the history
Signed-off-by: Surya Sashank Nistala <[email protected]>
  • Loading branch information
eirsep committed Jun 19, 2023
1 parent 7da61b3 commit e0dd805
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ data class ChainedAlertTrigger(
lateinit var name: String
lateinit var severity: String
lateinit var condition: Script
val queryIds: MutableList<String> = mutableListOf()
val actions: MutableList<Action> = mutableListOf()

if (xcp.currentToken() != XContentParser.Token.START_OBJECT && xcp.currentToken() != XContentParser.Token.FIELD_NAME) {
Expand All @@ -119,16 +118,6 @@ data class ChainedAlertTrigger(
}
xcp.nextToken()
}
QUERY_IDS_FIELD -> {
XContentParserUtils.ensureExpectedToken(
XContentParser.Token.START_ARRAY,
xcp.currentToken(),
xcp
)
while (xcp.nextToken() != XContentParser.Token.END_ARRAY) {
queryIds.add(xcp.text())
}
}
ACTIONS_FIELD -> {
XContentParserUtils.ensureExpectedToken(
XContentParser.Token.START_ARRAY,
Expand Down
10 changes: 10 additions & 0 deletions src/test/kotlin/org/opensearch/commons/alerting/AlertTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.opensearch.commons.alerting.model.Alert
import kotlin.test.assertTrue

class AlertTests {
@Test
Expand Down Expand Up @@ -58,4 +59,13 @@ class AlertTests {
val activeAlert = randomAlert().copy(state = Alert.State.ACTIVE)
Assertions.assertFalse(activeAlert.isAcknowledged(), "Alert is acknowledged")
}

@Test
fun `test chained alert`() {
val workflow = randomWorkflow()
val trigger = randomChainedAlertTrigger()
val alert = randomChainedAlert(workflow = workflow, trigger = trigger)
assertEquals(alert.monitorId, "")
assertEquals(alert.id, "")
}
}
18 changes: 17 additions & 1 deletion src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import org.opensearch.search.builder.SearchSourceBuilder
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.Random
import java.util.UUID

const val ALL_ACCESS_ROLE = "all_access"

Expand Down Expand Up @@ -171,7 +172,7 @@ fun randomWorkflow(
enabled: Boolean = Random().nextBoolean(),
enabledTime: Instant? = if (enabled) Instant.now().truncatedTo(ChronoUnit.MILLIS) else null,
lastUpdateTime: Instant = Instant.now().truncatedTo(ChronoUnit.MILLIS),
triggers: List<Trigger> = (1..RandomNumbers.randomIntBetween(Random(), 0, 10)).map { randomChainedAlertTrigger() },
triggers: List<Trigger> = listOf(randomChainedAlertTrigger()),
): Workflow {
val delegates = mutableListOf<Delegate>()
if (!monitorIds.isNullOrEmpty()) {
Expand Down Expand Up @@ -523,6 +524,21 @@ fun randomAlert(monitor: Monitor = randomQueryLevelMonitor()): Alert {
)
}

fun randomChainedAlert(
workflow: Workflow = randomWorkflow(),
trigger: ChainedAlertTrigger = randomChainedAlertTrigger(),
): Alert {
return Alert(
startTime = Instant.now(),
lastNotificationTime = Instant.now(),
state = Alert.State.ACTIVE,
errorMessage = "error",
executionId = UUID.randomUUID().toString(),
chainedAlertTrigger = trigger,
workflow = workflow
)
}

fun randomActionExecutionResult(
actionId: String = UUIDs.base64UUID(),
lastExecutionTime: Instant = Instant.now().truncatedTo(ChronoUnit.MILLIS),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.opensearch.commons.alerting.model.action.Throttle
import org.opensearch.commons.alerting.randomAction
import org.opensearch.commons.alerting.randomActionExecutionPolicy
import org.opensearch.commons.alerting.randomBucketLevelTrigger
import org.opensearch.commons.alerting.randomChainedAlertTrigger
import org.opensearch.commons.alerting.randomDocumentLevelTrigger
import org.opensearch.commons.alerting.randomQueryLevelMonitor
import org.opensearch.commons.alerting.randomQueryLevelTrigger
Expand Down Expand Up @@ -122,6 +123,16 @@ class WriteableTests {
Assertions.assertEquals(trigger, newTrigger, "Round tripping DocumentLevelTrigger doesn't work")
}

@Test
fun `test chained alert trigger as stream`() {
val trigger = randomChainedAlertTrigger()
val out = BytesStreamOutput()
trigger.writeTo(out)
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
val newTrigger = ChainedAlertTrigger.readFrom(sin)
Assertions.assertEquals(trigger, newTrigger, "Round tripping DocumentLevelTrigger doesn't work")
}

@Test
fun `test searchinput as stream`() {
val input = SearchInput(emptyList(), SearchSourceBuilder())
Expand Down

0 comments on commit e0dd805

Please sign in to comment.