github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/network/devices/bridge_linux_test.go (about) 1 package devices_test 2 3 import ( 4 "fmt" 5 "net" 6 7 "github.com/cloudfoundry-incubator/garden-linux/network/devices" 8 . "github.com/onsi/ginkgo" 9 . "github.com/onsi/gomega" 10 ) 11 12 var _ = Describe("Bridge Management", func() { 13 var ( 14 b devices.Bridge 15 name string 16 addr string 17 ip net.IP 18 subnet *net.IPNet 19 ) 20 21 BeforeEach(func() { 22 name = fmt.Sprintf("gdn-test-intf-%d", GinkgoParallelNode()) 23 24 var err error 25 addr = "10.9.0.1/30" 26 ip, subnet, err = net.ParseCIDR(addr) 27 Expect(err).ToNot(HaveOccurred()) 28 }) 29 30 AfterEach(func() { 31 cleanup(name) 32 }) 33 34 Describe("Create", func() { 35 Context("when the bridge does not already exist", func() { 36 It("creates a bridge", func() { 37 _, err := b.Create(name, ip, subnet) 38 Expect(err).ToNot(HaveOccurred()) 39 }) 40 41 It("sets the bridge name", func() { 42 bridge, err := b.Create(name, ip, subnet) 43 Expect(err).ToNot(HaveOccurred()) 44 45 Expect(bridge.Name).To(Equal(name)) 46 }) 47 48 It("sets the bridge address", func() { 49 bridge, err := b.Create(name, ip, subnet) 50 Expect(err).ToNot(HaveOccurred()) 51 52 addrs, err := bridge.Addrs() 53 Expect(err).ToNot(HaveOccurred()) 54 55 Expect(addrs).To(HaveLen(1)) 56 Expect(addrs[0].String()).To(Equal(addr)) 57 }) 58 }) 59 60 Context("when the bridge exists", func() { 61 var ( 62 existingIfc *net.Interface 63 ) 64 BeforeEach(func() { 65 var err error 66 existingIfc, err = b.Create(name, ip, subnet) 67 Expect(err).ToNot(HaveOccurred()) 68 }) 69 70 It("returns the interface for it", func() { 71 ifc, err := b.Create(name, ip, subnet) 72 Expect(err).ToNot(HaveOccurred()) 73 Expect(ifc).To(Equal(existingIfc)) 74 }) 75 76 It("does not change the existing bridge", func() { 77 ip2, subnet2, _ := net.ParseCIDR("10.8.0.2/30") 78 _, err := b.Create(name, ip2, subnet2) 79 Expect(err).ToNot(HaveOccurred()) 80 81 intf, err := net.InterfaceByName(name) 82 Expect(err).ToNot(HaveOccurred()) 83 84 addrs, err := intf.Addrs() 85 Expect(err).ToNot(HaveOccurred()) 86 87 Expect(addrs[0].String()).To(Equal(addr)) 88 }) 89 }) 90 }) 91 92 Describe("Destroy", func() { 93 Context("when the bridge exists", func() { 94 It("deletes it", func() { 95 br, err := b.Create(name, ip, subnet) 96 Expect(err).ToNot(HaveOccurred()) 97 98 // sanity check 99 Expect(interfaceNames()).To(ContainElement(name)) 100 101 // delete 102 Expect(b.Destroy(br.Name)).To(Succeed()) 103 104 // should be gone 105 Eventually(interfaceNames).ShouldNot(ContainElement(name)) 106 }) 107 }) 108 109 Context("when the bridge does not exist", func() { 110 It("does not return an error (because Destroy should be idempotent)", func() { 111 Expect(b.Destroy("something")).To(Succeed()) 112 }) 113 }) 114 }) 115 }) 116 117 func interfaceNames() []string { 118 intfs, err := net.Interfaces() 119 Expect(err).ToNot(HaveOccurred()) 120 121 v := make([]string, 0) 122 for _, i := range intfs { 123 v = append(v, i.Name) 124 } 125 126 return v 127 }