-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bridge: convert to netlink ops and add e2e testing
- Loading branch information
Showing
4 changed files
with
259 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2016 CNI authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"testing" | ||
) | ||
|
||
func TestBridge(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "bridge Suite") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
// Copyright 2015 CNI authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"os" | ||
"syscall" | ||
|
||
"github.com/appc/cni/pkg/ops" | ||
"github.com/appc/cni/pkg/skel" | ||
"github.com/appc/cni/pkg/types" | ||
|
||
"github.com/vishvananda/netlink" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("bridge Operations", func() { | ||
var ( | ||
tops *ops.TestNetOps | ||
) | ||
|
||
BeforeEach(func() { | ||
tops = ops.NewTestNetOps(func(format string, args ...interface{}) { | ||
GinkgoT().Log(fmt.Sprintf(format, args...)) | ||
}).(*ops.TestNetOps) | ||
}) | ||
|
||
AfterEach(func() { | ||
ops.ClearTestNetOps() | ||
}) | ||
|
||
It("creates a bridge", func() { | ||
const IFNAME = "bridge0" | ||
|
||
conf := &NetConf{ | ||
NetConf: types.NetConf{ | ||
Name: "testConfig", | ||
Type: "bridge", | ||
}, | ||
BrName: IFNAME, | ||
IsGW: false, | ||
IPMasq: false, | ||
MTU: 5000, | ||
} | ||
|
||
bridge, err := setupBridge(conf) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(bridge.Attrs().Name).To(Equal(IFNAME)) | ||
|
||
// Double check that the link was added | ||
link, err := tops.LinkByName(IFNAME) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(link.Attrs().Name).To(Equal(IFNAME)) | ||
}) | ||
|
||
It("handles an existing bridge", func() { | ||
const IFNAME = "bridge0" | ||
|
||
err := tops.LinkAdd(&netlink.Bridge{ | ||
LinkAttrs: netlink.LinkAttrs{ | ||
Name: IFNAME, | ||
}, | ||
}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
link, err := tops.LinkByName(IFNAME) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(link.Attrs().Name).To(Equal(IFNAME)) | ||
ifindex := link.Attrs().Index | ||
|
||
conf := &NetConf{ | ||
NetConf: types.NetConf{ | ||
Name: "testConfig", | ||
Type: "bridge", | ||
}, | ||
BrName: IFNAME, | ||
IsGW: false, | ||
IPMasq: false, | ||
} | ||
|
||
bridge, err := setupBridge(conf) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(bridge.Attrs().Name).To(Equal(IFNAME)) | ||
Expect(bridge.Attrs().Index).To(Equal(ifindex)) | ||
|
||
// Double check that the link has the same ifindex | ||
link, err = tops.LinkByName(IFNAME) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(link.Attrs().Name).To(Equal(IFNAME)) | ||
Expect(link.Attrs().Index).To(Equal(ifindex)) | ||
}) | ||
|
||
It("configures and deconfigures a bridge and veth with ADD/DEL", func() { | ||
const NSPATH = "/some/fake/path/1" | ||
const BRNAME = "cni0" | ||
const IFNAME = "eth0" | ||
|
||
gwaddr, subnet, err := net.ParseCIDR("10.1.2.1/24") | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
CONF := fmt.Sprintf(`{ | ||
"name": "mynet", | ||
"type": "bridge", | ||
"bridge": "%s", | ||
"isGateway": true, | ||
"ipMasq": false, | ||
"ipam": { | ||
"type": "host-local", | ||
"subnet": "%s" | ||
} | ||
}`, BRNAME, subnet.String()) | ||
|
||
targetNetNS, err := tops.GetNS(NSPATH) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
args := &skel.CmdArgs{ | ||
ContainerID: "dummy", | ||
Netns: NSPATH, | ||
IfName: IFNAME, | ||
StdinData: []byte(CONF), | ||
} | ||
|
||
os.Setenv("CNI_COMMAND", "ADD") | ||
// Look for locally built plugins | ||
os.Setenv("CNI_PATH", os.Getenv("PATH")) | ||
os.Setenv("CNI_NETNS", NSPATH) | ||
os.Setenv("CNI_IFNAME", IFNAME) | ||
|
||
// Redirect stdout to capture plugin result | ||
oldStdout := os.Stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
err = cmdAdd(args) | ||
w.Close() | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// parse the result | ||
out, _ := ioutil.ReadAll(r) | ||
os.Stdout = oldStdout | ||
result := types.Result{} | ||
err = json.Unmarshal(out, &result) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// Make sure bridge link exists | ||
link, err := tops.LinkByName(BRNAME) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(link.Attrs().Name).To(Equal(BRNAME)) | ||
|
||
// Ensure bridge has gateway address | ||
addrs, err := tops.AddrList(link, syscall.AF_INET) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(len(addrs)).To(BeNumerically(">", 0)) | ||
found := false | ||
subnetPrefix, subnetBits := subnet.Mask.Size() | ||
for _, a := range addrs { | ||
aPrefix, aBits := a.IPNet.Mask.Size() | ||
if a.IPNet.IP.Equal(gwaddr) && aPrefix == subnetPrefix && aBits == subnetBits { | ||
found = true | ||
break | ||
} | ||
} | ||
Expect(found).To(Equal(true)) | ||
|
||
// Check for the veth link in the main namespace | ||
links, err := tops.LinkList() | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(len(links)).To(Equal(2)) | ||
for _, l := range links { | ||
if l.Attrs().Name != BRNAME { | ||
veth, isVeth := l.(*netlink.Veth) | ||
Expect(isVeth).To(Equal(true)) | ||
Expect(veth.PeerName).To(Equal(IFNAME)) | ||
} | ||
} | ||
|
||
// Find the veth peer in the container namespace | ||
var innerErr error | ||
var innerLink netlink.Link | ||
err = tops.WithNetNS(targetNetNS, false, func(*os.File) error { | ||
innerLink, innerErr = tops.LinkByName(IFNAME) | ||
return nil | ||
}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(innerErr).NotTo(HaveOccurred()) | ||
Expect(innerLink.Attrs().Name).To(Equal(IFNAME)) | ||
|
||
os.Setenv("CNI_COMMAND", "DEL") | ||
err = cmdDel(args) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
// Make sure macvlan link has been deleted | ||
err = tops.WithNetNS(targetNetNS, false, func(*os.File) error { | ||
innerLink, innerErr = tops.LinkByName(IFNAME) | ||
return nil | ||
}) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(innerErr).To(HaveOccurred()) | ||
Expect(innerLink).To(BeNil()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters