| 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 mainscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/ |
| 16 | |
| 17 | import ( |
| 18 | "netscip-go gomod github.com/golang/go/src go1.24.2 net/" |
| 19 | "os/execscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/" |
| 20 | "path/filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/" |
| 21 | "strconvscip-go gomod github.com/golang/go/src go1.24.2 strconv/" |
| 22 | "stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/" |
| 23 | "testingscip-go gomod github.com/golang/go/src go1.24.2 testing/" |
| 24 | |
| 25 | . "github.com/onsi/ginkgo/v2scip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/" |
| 26 | . "github.com/onsi/gomegascip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/" |
| 27 | "github.com/onsi/gomega/gbytesscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gbytes`/" |
| 28 | "github.com/onsi/gomega/gexecscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/" |
| 29 | |
| 30 | "github.com/containernetworking/plugins/pkg/nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/" |
| 31 | ) |
| 32 | |
| 33 | func TestPortmapscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/TestPortmap().(t *testingscip-go gomod github.com/golang/go/src go1.24.2 testing/.Tscip-go gomod github.com/golang/go/src go1.24.2 testing/T#) { |
| 34 | RegisterFailHandlerscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/RegisterFailHandler().(Failscip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/Fail().) |
| 35 | RunSpecsscip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/RunSpecs().(t, "plugins/meta/portmap") |
| 36 | } |
| 37 | |
| 38 | var echoServerBinaryPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/echoServerBinaryPath., echoClientBinaryPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/echoClientBinaryPath. string |
| 39 | |
| 40 | var _ = SynchronizedBeforeSuitescip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/SynchronizedBeforeSuite().(func() []byte { |
| 41 | serverBinaryPath, err := gexecscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/.Buildscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/Build().("github.com/containernetworking/plugins/pkg/testutils/echo/server") |
| 42 | Expectscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/Expect().(err).NotToscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/types`/Assertion#NotTo().(HaveOccurredscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/HaveOccurred().()) |
| 43 | clientBinaryPath, err := gexecscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/.Buildscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/Build().("github.com/containernetworking/plugins/pkg/testutils/echo/client") |
| 44 | Expectscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/Expect().(err).NotToscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/types`/Assertion#NotTo().(HaveOccurredscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/HaveOccurred().()) |
| 45 | return []byte(stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.Joinscip-go gomod github.com/golang/go/src go1.24.2 strings/Join().([]string{serverBinaryPath, clientBinaryPath}, ",")) |
| 46 | }, func(datascip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/_:data. []byte) { |
| 47 | binaries := stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.Splitscip-go gomod github.com/golang/go/src go1.24.2 strings/Split().(string(datascip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/_:data.), ",") |
| 48 | echoServerBinaryPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/echoServerBinaryPath. = binaries[0] |
| 49 | echoClientBinaryPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/echoClientBinaryPath. = binaries[1] |
| 50 | }) |
| 51 | |
| 52 | var _ = SynchronizedAfterSuitescip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/SynchronizedAfterSuite().(func() {}, func() { |
| 53 | gexecscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/.CleanupBuildArtifactsscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/CleanupBuildArtifacts().() |
| 54 | }) |
| 55 | |
| 56 | func startInNetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/startInNetNS().(binPath string, netNS nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) (*gexecscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/.Sessionscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/Session#, error) { |
| 57 | baseName := filepathscip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.24.2 `path/filepath`/Base().(netNS.Pathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/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 := execscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.24.2 `os/exec`/Command().("ip", "netns", "exec", baseName, binPath) |
| 61 | session, err := gexecscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/.Startscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/Start().(cmd, GinkgoWriterscip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/GinkgoWriter., GinkgoWriterscip-go gomod github.com/onsi/ginkgo/v2 v2.25.1 `github.com/onsi/ginkgo/v2`/GinkgoWriter.) |
| 62 | return session, err |
| 63 | } |
| 64 | |
| 65 | func StartEchoServerInNamespacescip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/StartEchoServerInNamespace().(netNS nsscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/.NetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/pkg/ns`/NetNS#) (int, *gexecscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/.Sessionscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/Session#) { |
| 66 | session, err := startInNetNSscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/startInNetNS().(echoServerBinaryPathscip-go gomod github.com/containernetworking/plugins . `github.com/containernetworking/plugins/plugins/meta/portmap`/echoServerBinaryPath., netNS) |
| 67 | Expectscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/Expect().(err).NotToscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/types`/Assertion#NotTo().(HaveOccurredscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/HaveOccurred().()) |
| 68 | |
| 69 | // wait for it to print it's address on stdout |
| 70 | Eventuallyscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/Eventually().(session.Outscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/Session#Out.).Shouldscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/types`/AsyncAssertion#Should().(gbytesscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gbytes`/.Sayscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gbytes`/Say().("\n")) |
| 71 | _, portString, err := netscip-go gomod github.com/golang/go/src go1.24.2 net/.SplitHostPortscip-go gomod github.com/golang/go/src go1.24.2 net/SplitHostPort().(stringsscip-go gomod github.com/golang/go/src go1.24.2 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.24.2 strings/TrimSpace().(string(session.Outscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gexec`/Session#Out..Contentsscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/gbytes`/Buffer#Contents().()))) |
| 72 | Expectscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/Expect().(err).NotToscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/types`/Assertion#NotTo().(HaveOccurredscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/HaveOccurred().()) |
| 73 | |
| 74 | port, err := strconvscip-go gomod github.com/golang/go/src go1.24.2 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.24.2 strconv/Atoi().(portString) |
| 75 | Expectscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/Expect().(err).NotToscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega/types`/Assertion#NotTo().(HaveOccurredscip-go gomod github.com/onsi/gomega v1.38.1 `github.com/onsi/gomega`/HaveOccurred().()) |
| 76 | return port, session |
| 77 | } |