portmap: increase test coverage to 1.0.0 and older spec versions

Signed-off-by: Dan Williams <dcbw@redhat.com>
This commit is contained in:
Dan Williams 2021-02-15 10:08:24 -06:00
parent 8f7fe6d8e8
commit d2e5b5decb
2 changed files with 641 additions and 628 deletions

View File

@ -25,7 +25,7 @@ import (
"path/filepath"
"github.com/containernetworking/cni/libcni"
current "github.com/containernetworking/cni/pkg/types/100"
"github.com/containernetworking/cni/pkg/types/100"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/containernetworking/plugins/pkg/testutils"
"github.com/coreos/go-iptables/iptables"
@ -37,19 +37,9 @@ import (
const TIMEOUT = 90
var _ = Describe("portmap integration tests", func() {
var (
configList *libcni.NetworkConfigList
cniConf *libcni.CNIConfig
targetNS ns.NetNS
containerPort int
session *gexec.Session
)
BeforeEach(func() {
var err error
rawConfig := `{
"cniVersion": "0.3.0",
func makeConfig(ver string) *libcni.NetworkConfigList {
configList, err := libcni.ConfListFromBytes([]byte(fmt.Sprintf(`{
"cniVersion": "%s",
"name": "cni-portmap-unit-test",
"plugins": [
{
@ -70,15 +60,25 @@ var _ = Describe("portmap integration tests", func() {
}
}
]
}`
configList, err = libcni.ConfListFromBytes([]byte(rawConfig))
}`, ver)))
Expect(err).NotTo(HaveOccurred())
return configList
}
var _ = Describe("portmap integration tests", func() {
var (
cniConf *libcni.CNIConfig
targetNS ns.NetNS
containerPort int
session *gexec.Session
)
BeforeEach(func() {
// turn PATH in to CNI_PATH
dirs := filepath.SplitList(os.Getenv("PATH"))
cniConf = &libcni.CNIConfig{Path: dirs}
var err error
targetNS, err = testutils.NewNS()
Expect(err).NotTo(HaveOccurred())
fmt.Fprintln(GinkgoWriter, "namespace:", targetNS.Path())
@ -90,14 +90,18 @@ var _ = Describe("portmap integration tests", func() {
AfterEach(func() {
session.Terminate().Wait()
if targetNS != nil {
targetNS.Close()
}
testutils.UnmountNS(targetNS)
})
for _, ver := range []string{"0.3.0", "0.3.1", "0.4.0", "1.0.0"} {
// Redefine ver inside for scope so real value is picked up by each dynamically defined It()
// See Gingkgo's "Patterns for dynamically generating tests" documentation.
ver := ver
Describe("Creating an interface in a namespace with the ptp plugin", func() {
// This needs to be done using Ginkgo's asynchronous testing mode.
It("forwards a TCP port on ipv4", func(done Done) {
It(fmt.Sprintf("[%s] forwards a TCP port on ipv4", ver), func(done Done) {
var err error
hostPort := rand.Intn(10000) + 1025
runtimeConfig := libcni.RuntimeConf{
@ -114,6 +118,7 @@ var _ = Describe("portmap integration tests", func() {
},
},
}
configList := makeConfig(ver)
// Make delete idempotent, so we can clean up on failure
netDeleted := false
@ -146,7 +151,7 @@ var _ = Describe("portmap integration tests", func() {
_, err = ipt.List("nat", dnatChainName)
Expect(err).NotTo(HaveOccurred())
result, err := current.GetResult(resI)
result, err := types100.GetResult(resI)
Expect(err).NotTo(HaveOccurred())
var contIP net.IP
@ -221,7 +226,7 @@ var _ = Describe("portmap integration tests", func() {
close(done)
}, TIMEOUT*9)
It("forwards a UDP port on ipv4 and keep working after creating a second container with the same HostPort", func(done Done) {
It(fmt.Sprintf("[%s] forwards a UDP port on ipv4 and keep working after creating a second container with the same HostPort", ver), func(done Done) {
var err error
hostPort := rand.Intn(10000) + 1025
runtimeConfig := libcni.RuntimeConf{
@ -238,6 +243,7 @@ var _ = Describe("portmap integration tests", func() {
},
},
}
configList := makeConfig(ver)
// Make delete idempotent, so we can clean up on failure
netDeleted := false
@ -261,7 +267,7 @@ var _ = Describe("portmap integration tests", func() {
err = cmd.Run()
Expect(err).NotTo(HaveOccurred())
result, err := current.GetResult(resI)
result, err := types100.GetResult(resI)
Expect(err).NotTo(HaveOccurred())
var contIP net.IP
@ -357,7 +363,7 @@ var _ = Describe("portmap integration tests", func() {
Expect(err).NotTo(HaveOccurred())
defer deleteNetwork2()
result2, err := current.GetResult(resI2)
result2, err := types100.GetResult(resI2)
Expect(err).NotTo(HaveOccurred())
var contIP2 net.IP
@ -417,6 +423,7 @@ var _ = Describe("portmap integration tests", func() {
close(done)
}, TIMEOUT*9)
})
}
})
// testEchoServer returns true if we found an echo server on the port

View File

@ -27,12 +27,17 @@ var _ = Describe("portmapping configuration", func() {
netName := "testNetName"
containerID := "icee6giejonei6sohng6ahngee7laquohquee9shiGo7fohferakah3Feiyoolu2pei7ciPhoh7shaoX6vai3vuf0ahfaeng8yohb9ceu0daez5hashee8ooYai5wa3y"
for _, ver := range []string{"0.3.0", "0.3.1", "0.4.0", "1.0.0"} {
// Redefine ver inside for scope so real value is picked up by each dynamically defined It()
// See Gingkgo's "Patterns for dynamically generating tests" documentation.
ver := ver
Context("config parsing", func() {
It("Correctly parses an ADD config", func() {
configBytes := []byte(`{
It(fmt.Sprintf("[%s] correctly parses an ADD config", ver), func() {
configBytes := []byte(fmt.Sprintf(`{
"name": "test",
"type": "portmap",
"cniVersion": "0.3.1",
"cniVersion": "%s",
"runtimeConfig": {
"portMappings": [
{ "hostPort": 8080, "containerPort": 80, "protocol": "tcp"},
@ -68,10 +73,10 @@ var _ = Describe("portmapping configuration", func() {
}
]
}
}`)
}`, ver))
c, _, err := parseConfig(configBytes, "container")
Expect(err).NotTo(HaveOccurred())
Expect(c.CNIVersion).To(Equal("0.3.1"))
Expect(c.CNIVersion).To(Equal(ver))
Expect(c.ConditionsV4).To(Equal(&[]string{"a", "b"}))
Expect(c.ConditionsV6).To(Equal(&[]string{"c", "d"}))
fvar := false
@ -84,19 +89,19 @@ var _ = Describe("portmapping configuration", func() {
Expect(c.ContIPv6).To(Equal(*n))
})
It("Correctly parses a DEL config", func() {
It(fmt.Sprintf("[%s] correctly parses a DEL config", ver), func() {
// When called with DEL, neither runtimeConfig nor prevResult may be specified
configBytes := []byte(`{
configBytes := []byte(fmt.Sprintf(`{
"name": "test",
"type": "portmap",
"cniVersion": "0.3.1",
"cniVersion": "%s",
"snat": false,
"conditionsV4": ["a", "b"],
"conditionsV6": ["c", "d"]
}`)
}`, ver))
c, _, err := parseConfig(configBytes, "container")
Expect(err).NotTo(HaveOccurred())
Expect(c.CNIVersion).To(Equal("0.3.1"))
Expect(c.CNIVersion).To(Equal(ver))
Expect(c.ConditionsV4).To(Equal(&[]string{"a", "b"}))
Expect(c.ConditionsV6).To(Equal(&[]string{"c", "d"}))
fvar := false
@ -104,11 +109,11 @@ var _ = Describe("portmapping configuration", func() {
Expect(c.Name).To(Equal("test"))
})
It("fails with invalid mappings", func() {
configBytes := []byte(`{
It(fmt.Sprintf("[%s] fails with invalid mappings", ver), func() {
configBytes := []byte(fmt.Sprintf(`{
"name": "test",
"type": "portmap",
"cniVersion": "0.3.1",
"cniVersion": "%s",
"snat": false,
"conditionsV4": ["a", "b"],
"conditionsV6": ["c", "d"],
@ -117,16 +122,16 @@ var _ = Describe("portmapping configuration", func() {
{ "hostPort": 0, "containerPort": 80, "protocol": "tcp"}
]
}
}`)
}`, ver))
_, _, err := parseConfig(configBytes, "container")
Expect(err).To(MatchError("Invalid host port number: 0"))
})
It("Does not fail on missing prevResult interface index", func() {
configBytes := []byte(`{
It(fmt.Sprintf("[%s] does not fail on missing prevResult interface index", ver), func() {
configBytes := []byte(fmt.Sprintf(`{
"name": "test",
"type": "portmap",
"cniVersion": "0.3.1",
"cniVersion": "%s",
"runtimeConfig": {
"portMappings": [
{ "hostPort": 8080, "containerPort": 80, "protocol": "tcp"}
@ -145,7 +150,7 @@ var _ = Describe("portmapping configuration", func() {
}
]
}
}`)
}`, ver))
_, _, err := parseConfig(configBytes, "container")
Expect(err).NotTo(HaveOccurred())
})
@ -153,7 +158,7 @@ var _ = Describe("portmapping configuration", func() {
Describe("Generating chains", func() {
Context("for DNAT", func() {
It("generates a correct standard container chain", func() {
It(fmt.Sprintf("[%s] generates a correct standard container chain", ver), func() {
ch := genDnatChain(netName, containerID)
Expect(ch).To(Equal(chain{
@ -161,10 +166,10 @@ var _ = Describe("portmapping configuration", func() {
name: "CNI-DN-bfd599665540dd91d5d28",
entryChains: []string{TopLevelDNATChainName},
}))
configBytes := []byte(`{
configBytes := []byte(fmt.Sprintf(`{
"name": "test",
"type": "portmap",
"cniVersion": "0.3.1",
"cniVersion": "%s",
"runtimeConfig": {
"portMappings": [
{ "hostPort": 8080, "containerPort": 80, "protocol": "tcp"},
@ -180,7 +185,7 @@ var _ = Describe("portmapping configuration", func() {
"snat": true,
"conditionsV4": ["a", "b"],
"conditionsV6": ["c", "d"]
}`)
}`, ver))
conf, _, err := parseConfig(configBytes, "foo")
Expect(err).NotTo(HaveOccurred())
@ -279,7 +284,7 @@ var _ = Describe("portmapping configuration", func() {
}))
})
It("generates a correct chain with external mark", func() {
It(fmt.Sprintf("[%s] generates a correct chain with external mark", ver), func() {
ch := genDnatChain(netName, containerID)
Expect(ch).To(Equal(chain{
@ -287,10 +292,10 @@ var _ = Describe("portmapping configuration", func() {
name: "CNI-DN-bfd599665540dd91d5d28",
entryChains: []string{TopLevelDNATChainName},
}))
configBytes := []byte(`{
configBytes := []byte(fmt.Sprintf(`{
"name": "test",
"type": "portmap",
"cniVersion": "0.3.1",
"cniVersion": "%s",
"runtimeConfig": {
"portMappings": [
{ "hostPort": 8080, "containerPort": 80, "protocol": "tcp"}
@ -299,7 +304,7 @@ var _ = Describe("portmapping configuration", func() {
"externalSetMarkChain": "PLZ-SET-MARK",
"conditionsV4": ["a", "b"],
"conditionsV6": ["c", "d"]
}`)
}`, ver))
conf, _, err := parseConfig(configBytes, "foo")
Expect(err).NotTo(HaveOccurred())
@ -315,7 +320,7 @@ var _ = Describe("portmapping configuration", func() {
}))
})
It("generates a correct top-level chain", func() {
It(fmt.Sprintf("[%s] generates a correct top-level chain", ver), func() {
ch := genToplevelDnatChain()
Expect(ch).To(Equal(chain{
@ -326,7 +331,7 @@ var _ = Describe("portmapping configuration", func() {
}))
})
It("generates the correct mark chains", func() {
It(fmt.Sprintf("[%s] generates the correct mark chains", ver), func() {
masqBit := 5
ch := genSetMarkChain(masqBit)
Expect(ch).To(Equal(chain{
@ -359,4 +364,5 @@ var _ = Describe("portmapping configuration", func() {
})
})
})
}
})