| 1 | package main_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "net" |
| 7 | "os/exec" |
| 8 | "strings" |
| 9 | |
| 10 | . "github.com/onsi/ginkgo/v2" |
| 11 | . "github.com/onsi/gomega" |
| 12 | "github.com/onsi/gomega/gbytes" |
| 13 | "github.com/onsi/gomega/gexec" |
| 14 | ) |
| 15 | |
| 16 | var serverBinaryPath, clientBinaryPath string |
| 17 | |
| 18 | var _ = SynchronizedBeforeSuite(func() []byte { |
| 19 | serverBinaryPath, err := gexec.Build("github.com/containernetworking/plugins/pkg/testutils/echo/server") |
| 20 | Expect(err).NotTo(HaveOccurred()) |
| 21 | clientBinaryPath, err := gexec.Build("github.com/containernetworking/plugins/pkg/testutils/echo/client") |
| 22 | Expect(err).NotTo(HaveOccurred()) |
| 23 | return []byte(strings.Join([]string{serverBinaryPath, clientBinaryPath}, ",")) |
| 24 | }, func(data []byte) { |
| 25 | binaries := strings.Split(string(data), ",") |
| 26 | serverBinaryPath = binaries[0] |
| 27 | clientBinaryPath = binaries[1] |
| 28 | }) |
| 29 | |
| 30 | var _ = SynchronizedAfterSuite(func() {}, func() { |
| 31 | gexec.CleanupBuildArtifacts() |
| 32 | }) |
| 33 | |
| 34 | var _ = Describe("Echosvr", func() { |
| 35 | var session *gexec.Session |
| 36 | BeforeEach(func() { |
| 37 | var err error |
| 38 | cmd := exec.Command(serverBinaryPath) |
| 39 | session, err = gexec.Start(cmd, GinkgoWriter, GinkgoWriter) |
| 40 | Expect(err).NotTo(HaveOccurred()) |
| 41 | }) |
| 42 | |
| 43 | AfterEach(func() { |
| 44 | session.Kill().Wait() |
| 45 | }) |
| 46 | |
| 47 | Context("Server test", func() { |
| 48 | It("starts and doesn't terminate immediately", func() { |
| 49 | Consistently(session).ShouldNot(gexec.Exit()) |
| 50 | }) |
| 51 | |
| 52 | tryConnect := func() (net.Conn, error) { |
| 53 | programOutput := session.Out.Contents() |
| 54 | addr := strings.TrimSpace(string(programOutput)) |
| 55 | |
| 56 | conn, err := net.Dial("tcp", addr) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | return conn, err |
| 61 | } |
| 62 | |
| 63 | It("prints its listening address to stdout", func() { |
| 64 | Eventually(session.Out).Should(gbytes.Say("\n")) |
| 65 | conn, err := tryConnect() |
| 66 | Expect(err).NotTo(HaveOccurred()) |
| 67 | conn.Close() |
| 68 | }) |
| 69 | |
| 70 | It("will echo data back to us", func() { |
| 71 | Eventually(session.Out).Should(gbytes.Say("\n")) |
| 72 | conn, err := tryConnect() |
| 73 | Expect(err).NotTo(HaveOccurred()) |
| 74 | defer conn.Close() |
| 75 | |
| 76 | fmt.Fprintf(conn, "hello\n") |
| 77 | Expect(io.ReadAll(conn)).To(Equal([]byte("hello"))) |
| 78 | }) |
| 79 | }) |
| 80 | |
| 81 | Context("Client Server Test", func() { |
| 82 | It("starts and doesn't terminate immediately", func() { |
| 83 | Consistently(session).ShouldNot(gexec.Exit()) |
| 84 | }) |
| 85 | |
| 86 | It("connects successfully using echo client", func() { |
| 87 | Eventually(session.Out).Should(gbytes.Say("\n")) |
| 88 | serverAddress := strings.TrimSpace(string(session.Out.Contents())) |
| 89 | fmt.Println("Server address", serverAddress) |
| 90 | |
| 91 | cmd := exec.Command(clientBinaryPath, "-target", serverAddress, "-message", "hello") |
| 92 | clientSession, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter) |
| 93 | Expect(err).NotTo(HaveOccurred()) |
| 94 | Eventually(clientSession.Out).Should(gbytes.Say("hello")) |
| 95 | Eventually(clientSession).Should(gexec.Exit()) |
| 96 | }) |
| 97 | }) |
| 98 | }) |