flannel: add unit tests for config loading
backfill unit tests to add coverage for loadFlannelNetConf and loadFlannelSubnetEnv
This commit is contained in:
parent
fa264e6e36
commit
f0daefa63d
@ -11,7 +11,7 @@
|
|||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
package main_test
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
package main_test
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -35,6 +35,8 @@ var _ = Describe("Flannel", func() {
|
|||||||
input string
|
input string
|
||||||
debug *noop_debug.Debug
|
debug *noop_debug.Debug
|
||||||
expectedCmdArgs skel.CmdArgs
|
expectedCmdArgs skel.CmdArgs
|
||||||
|
subnetFile string
|
||||||
|
stateDir string
|
||||||
)
|
)
|
||||||
|
|
||||||
const delegateInput = `
|
const delegateInput = `
|
||||||
@ -61,6 +63,14 @@ FLANNEL_MTU=1472
|
|||||||
FLANNEL_IPMASQ=true
|
FLANNEL_IPMASQ=true
|
||||||
`
|
`
|
||||||
|
|
||||||
|
var writeSubnetEnv = func(contents string) string {
|
||||||
|
file, err := ioutil.TempFile("", "subnet.env")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
_, err = file.WriteString(contents)
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
return file.Name()
|
||||||
|
}
|
||||||
|
|
||||||
var cniCommand = func(command, input string) *exec.Cmd {
|
var cniCommand = func(command, input string) *exec.Cmd {
|
||||||
toReturn := exec.Command(paths.PathToPlugin)
|
toReturn := exec.Command(paths.PathToPlugin)
|
||||||
toReturn.Env = []string{
|
toReturn.Env = []string{
|
||||||
@ -86,31 +96,25 @@ FLANNEL_IPMASQ=true
|
|||||||
ReportVersionSupport: []string{"0.1.0", "0.2.0", "0.3.0"},
|
ReportVersionSupport: []string{"0.1.0", "0.2.0", "0.3.0"},
|
||||||
}
|
}
|
||||||
Expect(debug.WriteDebug(debugFileName)).To(Succeed())
|
Expect(debug.WriteDebug(debugFileName)).To(Succeed())
|
||||||
|
|
||||||
|
// flannel subnet.env
|
||||||
|
subnetFile = writeSubnetEnv(flannelSubnetEnv)
|
||||||
|
|
||||||
|
// flannel state dir
|
||||||
|
stateDir, err = ioutil.TempDir("", "stateDir")
|
||||||
|
Expect(err).NotTo(HaveOccurred())
|
||||||
|
input = fmt.Sprintf(inputTemplate, subnetFile, stateDir)
|
||||||
})
|
})
|
||||||
|
|
||||||
AfterEach(func() {
|
AfterEach(func() {
|
||||||
os.Remove(debugFileName)
|
os.Remove(debugFileName)
|
||||||
|
os.Remove(subnetFile)
|
||||||
|
os.Remove(stateDir)
|
||||||
})
|
})
|
||||||
|
|
||||||
Describe("CNI lifecycle", func() {
|
Describe("CNI lifecycle", func() {
|
||||||
Context("when subnetFile and stateDir are specified", func() {
|
|
||||||
var (
|
|
||||||
subnetFile string
|
|
||||||
stateDir string
|
|
||||||
)
|
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
var err error
|
|
||||||
file, err := ioutil.TempFile("", "subnet.env")
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
|
||||||
_, err = file.WriteString(flannelSubnetEnv)
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
|
||||||
subnetFile = file.Name()
|
|
||||||
|
|
||||||
stateDir, err = ioutil.TempDir("", "stateDir")
|
|
||||||
Expect(err).NotTo(HaveOccurred())
|
|
||||||
input = fmt.Sprintf(inputTemplate, subnetFile, stateDir)
|
|
||||||
|
|
||||||
expectedCmdArgs = skel.CmdArgs{
|
expectedCmdArgs = skel.CmdArgs{
|
||||||
ContainerID: "some-container-id",
|
ContainerID: "some-container-id",
|
||||||
Netns: "/some/netns/path",
|
Netns: "/some/netns/path",
|
||||||
@ -122,11 +126,6 @@ FLANNEL_IPMASQ=true
|
|||||||
cmd = cniCommand("ADD", input)
|
cmd = cniCommand("ADD", input)
|
||||||
})
|
})
|
||||||
|
|
||||||
AfterEach(func() {
|
|
||||||
os.Remove(subnetFile)
|
|
||||||
os.Remove(stateDir)
|
|
||||||
})
|
|
||||||
|
|
||||||
It("uses stateDir for storing network configuration", func() {
|
It("uses stateDir for storing network configuration", func() {
|
||||||
By("calling ADD")
|
By("calling ADD")
|
||||||
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
|
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
|
||||||
@ -169,5 +168,61 @@ FLANNEL_IPMASQ=true
|
|||||||
Expect(path).ShouldNot(BeAnExistingFile())
|
Expect(path).ShouldNot(BeAnExistingFile())
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Describe("loadFlannelNetConf", func() {
|
||||||
|
Context("when subnetFile and stateDir are specified", func() {
|
||||||
|
It("loads flannel network config", func() {
|
||||||
|
conf, err := loadFlannelNetConf([]byte(input))
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
|
Expect(conf.Name).To(Equal("cni-flannel"))
|
||||||
|
Expect(conf.Type).To(Equal("flannel"))
|
||||||
|
Expect(conf.SubnetFile).To(Equal(subnetFile))
|
||||||
|
Expect(conf.StateDir).To(Equal(stateDir))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("when defaulting subnetFile and stateDir", func() {
|
||||||
|
BeforeEach(func() {
|
||||||
|
input = `{
|
||||||
|
"name": "cni-flannel",
|
||||||
|
"type": "flannel",
|
||||||
|
"delegate": ` +
|
||||||
|
delegateInput +
|
||||||
|
`}`
|
||||||
|
})
|
||||||
|
|
||||||
|
It("loads flannel network config with defaults", func() {
|
||||||
|
conf, err := loadFlannelNetConf([]byte(input))
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
|
Expect(conf.Name).To(Equal("cni-flannel"))
|
||||||
|
Expect(conf.Type).To(Equal("flannel"))
|
||||||
|
Expect(conf.SubnetFile).To(Equal(defaultSubnetFile))
|
||||||
|
Expect(conf.StateDir).To(Equal(defaultStateDir))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Describe("loadFlannelSubnetEnv", func() {
|
||||||
|
Context("when flannel subnet env is valid", func() {
|
||||||
|
It("loads flannel subnet config", func() {
|
||||||
|
conf, err := loadFlannelSubnetEnv(subnetFile)
|
||||||
|
Expect(err).ShouldNot(HaveOccurred())
|
||||||
|
Expect(conf.nw.String()).To(Equal("10.1.0.0/16"))
|
||||||
|
Expect(conf.sn.String()).To(Equal("10.1.17.0/24"))
|
||||||
|
var mtu uint = 1472
|
||||||
|
Expect(*conf.mtu).To(Equal(mtu))
|
||||||
|
Expect(*conf.ipmasq).To(BeTrue())
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
Context("when flannel subnet env is invalid", func() {
|
||||||
|
BeforeEach(func() {
|
||||||
|
subnetFile = writeSubnetEnv("foo=bar")
|
||||||
|
})
|
||||||
|
It("returns an error", func() {
|
||||||
|
_, err := loadFlannelSubnetEnv(subnetFile)
|
||||||
|
Expect(err).To(MatchError(ContainSubstring("missing FLANNEL_NETWORK, FLANNEL_SUBNET, FLANNEL_MTU, FLANNEL_IPMASQ")))
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user