File: echo_test.go

package info (click to toggle)
golang-github-containernetworking-plugins 1.1.1%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,672 kB
  • sloc: sh: 132; makefile: 11
file content (98 lines) | stat: -rw-r--r-- 2,756 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main_test

import (
	"fmt"
	"io/ioutil"
	"net"
	"os/exec"
	"strings"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
	"github.com/onsi/gomega/gexec"
)

var serverBinaryPath, clientBinaryPath string

var _ = SynchronizedBeforeSuite(func() []byte {
	serverBinaryPath, err := gexec.Build("github.com/containernetworking/plugins/pkg/testutils/echo/server")
	Expect(err).NotTo(HaveOccurred())
	clientBinaryPath, err := gexec.Build("github.com/containernetworking/plugins/pkg/testutils/echo/client")
	Expect(err).NotTo(HaveOccurred())
	return []byte(strings.Join([]string{serverBinaryPath, clientBinaryPath}, ","))
}, func(data []byte) {
	binaries := strings.Split(string(data), ",")
	serverBinaryPath = binaries[0]
	clientBinaryPath = binaries[1]
})

var _ = SynchronizedAfterSuite(func() {}, func() {
	gexec.CleanupBuildArtifacts()
})

var _ = Describe("Echosvr", func() {
	var session *gexec.Session
	BeforeEach(func() {
		var err error
		cmd := exec.Command(serverBinaryPath)
		session, err = gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
		Expect(err).NotTo(HaveOccurred())
	})

	AfterEach(func() {
		session.Kill().Wait()
	})

	Context("Server test", func() {
		It("starts and doesn't terminate immediately", func() {
			Consistently(session).ShouldNot(gexec.Exit())
		})

		tryConnect := func() (net.Conn, error) {
			programOutput := session.Out.Contents()
			addr := strings.TrimSpace(string(programOutput))

			conn, err := net.Dial("tcp", addr)
			if err != nil {
				return nil, err
			}
			return conn, err
		}

		It("prints its listening address to stdout", func() {
			Eventually(session.Out).Should(gbytes.Say("\n"))
			conn, err := tryConnect()
			Expect(err).NotTo(HaveOccurred())
			conn.Close()
		})

		It("will echo data back to us", func() {
			Eventually(session.Out).Should(gbytes.Say("\n"))
			conn, err := tryConnect()
			Expect(err).NotTo(HaveOccurred())
			defer conn.Close()

			fmt.Fprintf(conn, "hello\n")
			Expect(ioutil.ReadAll(conn)).To(Equal([]byte("hello")))
		})
	})

	Context("Client Server Test", func() {
		It("starts and doesn't terminate immediately", func() {
			Consistently(session).ShouldNot(gexec.Exit())
		})

		It("connects successfully using echo client", func() {
			Eventually(session.Out).Should(gbytes.Say("\n"))
			serverAddress := strings.TrimSpace(string(session.Out.Contents()))
			fmt.Println("Server address", string(serverAddress))

			cmd := exec.Command(clientBinaryPath, "-target", serverAddress, "-message", "hello")
			clientSession, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
			Expect(err).NotTo(HaveOccurred())
			Eventually(clientSession.Out).Should(gbytes.Say("hello"))
			Eventually(clientSession).Should(gexec.Exit())
		})
	})
})