From 4444959285ad2b6058b8633364f1cfcbcb178e09 Mon Sep 17 00:00:00 2001
From: "stu126940@mail.uni-kiel.de" <stu126940@mail.uni-kiel.de>
Date: Tue, 4 Jan 2022 16:00:46 +0100
Subject: [PATCH] fix test for actions

---
 .../examples/operator/example-benchmark.yaml  |  2 +-
 .../theodolite/benchmark/ActionCommand.kt     |  5 ++-
 .../theodolite/benchmark/ActionCommandTest.kt | 38 +++++++++++++++----
 .../benchmark/ErrorChannelMessage.kt          | 17 +++++++++
 4 files changed, 51 insertions(+), 11 deletions(-)
 create mode 100644 theodolite/src/test/kotlin/theodolite/benchmark/ErrorChannelMessage.kt

diff --git a/theodolite/examples/operator/example-benchmark.yaml b/theodolite/examples/operator/example-benchmark.yaml
index efb3737d8..84371d868 100644
--- a/theodolite/examples/operator/example-benchmark.yaml
+++ b/theodolite/examples/operator/example-benchmark.yaml
@@ -21,7 +21,7 @@ spec:
             matchLabels:
               app: busybox1
         exec:
-          command: ["rm", "test-folder"]
+          command: ["touch", "test-folder"]
           timeoutSeconds: 90
   loadGenerator:
     resources:
diff --git a/theodolite/src/main/kotlin/theodolite/benchmark/ActionCommand.kt b/theodolite/src/main/kotlin/theodolite/benchmark/ActionCommand.kt
index 4387e6648..03297e821 100644
--- a/theodolite/src/main/kotlin/theodolite/benchmark/ActionCommand.kt
+++ b/theodolite/src/main/kotlin/theodolite/benchmark/ActionCommand.kt
@@ -76,8 +76,9 @@ class ActionCommand(val client: NamespacedKubernetesClient) {
                 }
             }
         }
-        logger.debug { "Execution Output Stream is \n $out" }
-        logger.debug { "Execution Error Stream is \n $error" }
+        logger.info { "Execution Output Stream is \n $out" }
+        logger.info { "Execution Error Stream is \n $error" }
+        logger.info { "ERRORCHANNEL IS: \n $errChannelStream" }
         return getExitCode(errChannelStream)
     }
 
diff --git a/theodolite/src/test/kotlin/theodolite/benchmark/ActionCommandTest.kt b/theodolite/src/test/kotlin/theodolite/benchmark/ActionCommandTest.kt
index f97bc0a2e..f6ef7d912 100644
--- a/theodolite/src/test/kotlin/theodolite/benchmark/ActionCommandTest.kt
+++ b/theodolite/src/test/kotlin/theodolite/benchmark/ActionCommandTest.kt
@@ -4,15 +4,15 @@ import io.fabric8.kubernetes.api.model.Pod
 import io.fabric8.kubernetes.api.model.PodBuilder
 import io.fabric8.kubernetes.api.model.PodListBuilder
 import io.fabric8.kubernetes.client.server.mock.KubernetesServer
-import io.fabric8.kubernetes.client.server.mock.OutputStreamMessage
 import io.fabric8.kubernetes.client.utils.Utils
 import io.quarkus.test.junit.QuarkusTest
 import org.junit.jupiter.api.*
+import org.junit.jupiter.api.Assertions.assertEquals
+import org.junit.jupiter.api.Assertions.assertTrue
 import theodolite.execution.operator.TheodoliteController
 import theodolite.execution.operator.TheodoliteOperator
 import theodolite.util.ActionCommandFailedException
 
-
 @QuarkusTest
 class ActionCommandTest {
     private val server = KubernetesServer(false, false)
@@ -40,7 +40,6 @@ class ActionCommandTest {
         val podList = PodListBuilder().build()
         podList.items.add(0, ready)
 
-
         server
             .expect()
             .withPath("/api/v1/namespaces/test/pods?labelSelector=${Utils.toUrlEncoded("app=pod")}")
@@ -58,7 +57,15 @@ class ActionCommandTest {
             .expect()
             .withPath("/api/v1/namespaces/test/pods/pod1/exec?command=ls&stdout=true&stderr=true")
             .andUpgradeToWebSocket()
-            .open(OutputStreamMessage("Test-Output"))
+            .open(ErrorChannelMessage("{\"metadata\":{},\"status\":\"Success\"}\n"))
+            .done()
+            .always()
+
+        server
+            .expect()
+            .withPath("/api/v1/namespaces/test/pods/pod1/exec?command=error-command&stdout=true&stderr=true")
+            .andUpgradeToWebSocket()
+            .open(ErrorChannelMessage("{\"metadata\":{},\"status\":\"failed\", \"details\":{}}\n"))
             .done()
             .always()
     }
@@ -88,11 +95,11 @@ class ActionCommandTest {
 
     @Test
     fun testGetPodName() {
-        Assertions.assertEquals("pod1", ActionCommand(client = server.client).getPodName(mutableMapOf("app" to "pod")))
+        assertEquals("pod1", ActionCommand(client = server.client).getPodName(mutableMapOf("app" to "pod")))
     }
 
     @Test
-    fun testAction() {
+    fun testActionSuccess() {
         val action = Action()
         action.selector = ActionSelector()
         action.selector.pod = PodSelector()
@@ -101,7 +108,22 @@ class ActionCommandTest {
         action.exec.command = arrayOf("ls")
         action.exec.timeoutSeconds = 10L
 
-        val e = assertThrows<ActionCommandFailedException> { run { action.exec(server.client) } }
-        assert(e.message.equals("Could not determine the exit code, no information given"))
+        action.exec(server.client)
+        assertEquals(
+            "/api/v1/namespaces/test/pods/pod1/exec?command=ls&stdout=true&stderr=true",
+            server.lastRequest.path)
+    }
+
+    @Test
+    fun testActionFailed() {
+        val action = Action()
+        action.selector = ActionSelector()
+        action.selector.pod = PodSelector()
+        action.selector.pod.matchLabels = mutableMapOf("app" to "pod")
+        action.exec = Command()
+        action.exec.command = arrayOf("error-command")
+        action.exec.timeoutSeconds = 10L
+
+        assertThrows<ActionCommandFailedException> { run { action.exec(server.client) } }
     }
 }
diff --git a/theodolite/src/test/kotlin/theodolite/benchmark/ErrorChannelMessage.kt b/theodolite/src/test/kotlin/theodolite/benchmark/ErrorChannelMessage.kt
new file mode 100644
index 000000000..df57a2529
--- /dev/null
+++ b/theodolite/src/test/kotlin/theodolite/benchmark/ErrorChannelMessage.kt
@@ -0,0 +1,17 @@
+package theodolite.benchmark
+
+import io.fabric8.mockwebserver.internal.WebSocketMessage
+import java.nio.charset.StandardCharsets
+
+class ErrorChannelMessage(body: String) : WebSocketMessage(0L, getBodyBytes(OUT_STREAM_ID, body), true, true) {
+    companion object {
+        private const val OUT_STREAM_ID: Byte = 3
+        private fun getBodyBytes(prefix: Byte, body: String): ByteArray {
+            val original = body.toByteArray(StandardCharsets.UTF_8)
+            val prefixed = ByteArray(original.size + 1)
+            prefixed[0] = prefix
+            System.arraycopy(original, 0, prefixed, 1, original.size)
+            return prefixed
+        }
+    }
+}
-- 
GitLab