Debian Codegraph @

plugins/meta/portmap/portmap_suite_test.go

raw symbols SCIP
1 // Copyright 2017 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package main
16
17 import (
18 "net"
19 "os/exec"
20 "path/filepath"
21 "strconv"
22 "strings"
23 "testing"
24
25 . "github.com/onsi/ginkgo/v2"
26 . "github.com/onsi/gomega"
27 "github.com/onsi/gomega/gbytes"
28 "github.com/onsi/gomega/gexec"
29
30 "github.com/containernetworking/plugins/pkg/ns"
31 )
32
33 func TestPortmap(t *testing.T) {
34 RegisterFailHandler(Fail)
35 RunSpecs(t, "plugins/meta/portmap")
36 }
37
38 var echoServerBinaryPath, echoClientBinaryPath string
39
40 var _ = SynchronizedBeforeSuite(func() []byte {
41 serverBinaryPath, err := gexec.Build("github.com/containernetworking/plugins/pkg/testutils/echo/server")
42 Expect(err).NotTo(HaveOccurred())
43 clientBinaryPath, err := gexec.Build("github.com/containernetworking/plugins/pkg/testutils/echo/client")
44 Expect(err).NotTo(HaveOccurred())
45 return []byte(strings.Join([]string{serverBinaryPath, clientBinaryPath}, ","))
46 }, func(data []byte) {
47 binaries := strings.Split(string(data), ",")
48 echoServerBinaryPath = binaries[0]
49 echoClientBinaryPath = binaries[1]
50 })
51
52 var _ = SynchronizedAfterSuite(func() {}, func() {
53 gexec.CleanupBuildArtifacts()
54 })
55
56 func startInNetNS(binPath string, netNS ns.NetNS) (*gexec.Session, error) {
57 baseName := filepath.Base(netNS.Path())
58 // we are relying on the netNS path living in /var/run/netns
59 // where `ip netns exec` can find it
60 cmd := exec.Command("ip", "netns", "exec", baseName, binPath)
61 session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
62 return session, err
63 }
64
65 func StartEchoServerInNamespace(netNS ns.NetNS) (int, *gexec.Session) {
66 session, err := startInNetNS(echoServerBinaryPath, netNS)
67 Expect(err).NotTo(HaveOccurred())
68
69 // wait for it to print it's address on stdout
70 Eventually(session.Out).Should(gbytes.Say("\n"))
71 _, portString, err := net.SplitHostPort(strings.TrimSpace(string(session.Out.Contents())))
72 Expect(err).NotTo(HaveOccurred())
73
74 port, err := strconv.Atoi(portString)
75 Expect(err).NotTo(HaveOccurred())
76 return port, session
77 }