kubecli: vm: add simple roundtrip test
Add a verby basic roundtrip test for StreamInterface.
package kubecli
import (
+ "fmt"
+ "io"
"net/http"
"github.com/gorilla/websocket"
@@ -156,6 +158,68 @@ var _ = Describe("Kubevirt VM Client", func() {
Expect(err).To(HaveOccurred())
})
+ It("should exchange data with the VM", func() {
+ vncPath := "/apis/subresources.kubevirt.io/v1alpha1/namespaces/default/virtualmachines/testvm/vnc"
+
+ server.AppendHandlers(ghttp.CombineHandlers(
+ ghttp.VerifyRequest("GET", vncPath),
+ func(w http.ResponseWriter, r *http.Request) {
+ c, err := upgrader.Upgrade(w, r, nil)
+ if err != nil {
+ panic("server upgrader failed")
+ }
+ defer c.Close()
+
+ for {
+ mt, message, err := c.ReadMessage()
+ if err != nil {
+ io.WriteString(GinkgoWriter, fmt.Sprintf("server read failed: %v\n", err))
+ break
+ }
+
+ err = c.WriteMessage(mt, message)
+ if err != nil {
+ io.WriteString(GinkgoWriter, fmt.Sprintf("server write failed: %v\n", err))
+ break
+ }
+ }
+ },
+ ))
+
+ By("establishing connection")
+
+ vnc, err := client.VM(k8sv1.NamespaceDefault).VNC("testvm")
+ Expect(err).ToNot(HaveOccurred())
+
+ By("wiring the pipes")
+
+ pipeInReader, pipeInWriter := io.Pipe()
+ pipeOutReader, pipeOutWriter := io.Pipe()
+
+ go func() {
+ vnc.Stream(StreamOptions{
+ In: pipeInReader,
+ Out: pipeOutWriter,
+ })
+ }()
+
+ By("sending data around")
+ msg := "hello, vnc!"
+ bufIn := make([]byte, 64)
+ copy(bufIn[:], msg)
+
+ _, err = pipeInWriter.Write(bufIn)
+ Expect(err).ToNot(HaveOccurred())
+
+ By("reading back data")
+ bufOut := make([]byte, 64)
+ _, err = pipeOutReader.Read(bufOut)
+ Expect(err).ToNot(HaveOccurred())
+
+ By("checking the result")
+ Expect(bufOut).To(Equal(bufIn))
+ })
+
AfterEach(func() {
server.Close()
})
