Ejemplo n.º 1
0
	BeforeEach(func() {
		fs = fakesys.NewFakeFileSystem()
		cmdRunner = fakesys.NewFakeCmdRunner()
		logger := boshlog.NewLogger(boshlog.LevelNone)
		arping = NewArping(cmdRunner, fs, logger, arpingIterations, 0, 0)
	})

	Describe("BroadcastMACAddresses", func() {
		BeforeEach(func() {
			fs.WriteFile("/sys/class/net/eth0", []byte{})
			fs.WriteFile("/sys/class/net/eth1", []byte{})
		})

		It("runs arping commands for each interface", func() {
			addresses := []boship.InterfaceAddress{
				boship.NewSimpleInterfaceAddress("eth0", "192.168.195.6"),
				boship.NewSimpleInterfaceAddress("eth1", "127.0.0.1"),
			}

			arping.BroadcastMACAddresses(addresses)

			countA := 0
			countB := 0

			a := []string{"arping", "-c", "1", "-U", "-I", "eth0", "192.168.195.6"}
			b := []string{"arping", "-c", "1", "-U", "-I", "eth1", "127.0.0.1"}

			for i := 0; i < arpingIterations*2; i++ {
				cmd := cmdRunner.RunCommands[i]
				if reflect.DeepEqual(cmd, a) {
					countA++
func init() {
	const expectedUbuntuDHCPConfig = `# Generated by bosh-agent

option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;

send host-name "<hostname>";

request subnet-mask, broadcast-address, time-offset, routers,
	domain-name, domain-name-servers, domain-search, host-name,
	netbios-name-servers, netbios-scope, interface-mtu,
	rfc3442-classless-static-routes, ntp-servers;

prepend domain-name-servers xx.xx.xx.xx, yy.yy.yy.yy, zz.zz.zz.zz;
`

	Describe("ubuntuNetManager", func() {
		var (
			fs                     *fakesys.FakeFileSystem
			cmdRunner              *fakesys.FakeCmdRunner
			defaultNetworkResolver *fakenet.FakeDefaultNetworkResolver
			ipResolver             *fakeip.FakeIPResolver
			addressBroadcaster     *fakearp.FakeAddressBroadcaster
			netManager             NetManager
		)

		BeforeEach(func() {
			fs = fakesys.NewFakeFileSystem()
			cmdRunner = fakesys.NewFakeCmdRunner()
			defaultNetworkResolver = &fakenet.FakeDefaultNetworkResolver{}
			ipResolver = &fakeip.FakeIPResolver{}
			addressBroadcaster = &fakearp.FakeAddressBroadcaster{}
			logger := boshlog.NewLogger(boshlog.LevelNone)
			netManager = NewUbuntuNetManager(
				fs,
				cmdRunner,
				defaultNetworkResolver,
				ipResolver,
				addressBroadcaster,
				logger,
			)
		})

		Describe("SetupDhcp", func() {
			networks := boshsettings.Networks{
				"bosh": boshsettings.Network{
					Default: []string{"dns"},
					DNS:     []string{"xx.xx.xx.xx", "yy.yy.yy.yy", "zz.zz.zz.zz"},
				},
				"vip": boshsettings.Network{
					Default: []string{},
					DNS:     []string{"aa.aa.aa.aa"},
				},
			}

			ItRestartsDhcp := func() {
				Context("when ifconfig version is 0.7", func() {
					BeforeEach(func() {
						cmdRunner.AddCmdResult("ifup --version", fakesys.FakeCmdResult{
							Stdout: "ifup version 0.7.47",
						})
					})

					It("restarts dhclient", func() {
						err := netManager.SetupDhcp(networks, nil)
						Expect(err).ToNot(HaveOccurred())

						Expect(len(cmdRunner.RunCommands)).To(Equal(3))
						Expect(cmdRunner.RunCommands[1]).To(Equal([]string{"ifdown", "-a", "--no-loopback"}))
						Expect(cmdRunner.RunCommands[2]).To(Equal([]string{"ifup", "-a", "--no-loopback"}))
					})
				})

				Context("when ifconfig version is 0.6", func() {
					BeforeEach(func() {
						cmdRunner.AddCmdResult("ifup --version", fakesys.FakeCmdResult{
							Stdout: "ifup version 0.6.0",
						})
					})

					It("restarts dhclient", func() {
						err := netManager.SetupDhcp(networks, nil)
						Expect(err).ToNot(HaveOccurred())

						Expect(len(cmdRunner.RunCommands)).To(Equal(3))
						Expect(cmdRunner.RunCommands[1]).To(Equal([]string{"ifdown", "-a", "--exclude=lo"}))
						Expect(cmdRunner.RunCommands[2]).To(Equal([]string{"ifup", "-a", "--exclude=lo"}))
					})
				})
			}

			ItUpdatesDhcp3Config := func() {
				It("updates /etc/dhcp3/dhclient.conf", func() {
					err := netManager.SetupDhcp(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					dhcpConfig := fs.GetFileTestStat("/etc/dhcp3/dhclient.conf")
					Expect(dhcpConfig).ToNot(BeNil())
					Expect(dhcpConfig.StringContents()).To(Equal(expectedUbuntuDHCPConfig))
				})
			}

			ItUpdatesDhcpConfig := func() {
				It("updates /etc/dhcp/dhclient.conf", func() {
					err := netManager.SetupDhcp(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					dhcpConfig := fs.GetFileTestStat("/etc/dhcp/dhclient.conf")
					Expect(dhcpConfig).ToNot(BeNil())
					Expect(dhcpConfig.StringContents()).To(Equal(expectedUbuntuDHCPConfig))
				})
			}

			ItDoesNotRestartDhcp := func() {
				It("does not restart dhclient", func() {
					err := netManager.SetupDhcp(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					Expect(len(cmdRunner.RunCommands)).To(Equal(0))
				})
			}

			ItBroadcastsMACAddresses := func() {
				It("starts broadcasting the MAC addresses", func() {
					errCh := make(chan error)

					err := netManager.SetupDhcp(networks, errCh)
					Expect(err).ToNot(HaveOccurred())

					<-errCh // wait for all arpings

					Expect(addressBroadcaster.BroadcastMACAddressesAddresses).To(Equal([]boship.InterfaceAddress{
						boship.NewResolvingInterfaceAddress("eth0", ipResolver),
					}))
				})
			}

			Context("when dhclient3 is installed on the system", func() {
				BeforeEach(func() { cmdRunner.CommandExistsValue = true })

				Context("when dhcp was not previously configured", func() {
					ItUpdatesDhcp3Config()
					ItRestartsDhcp()
					ItBroadcastsMACAddresses()
				})

				Context("when dhcp was previously configured with different configuration", func() {
					BeforeEach(func() {
						fs.WriteFileString("/etc/dhcp3/dhclient.conf", "fake-other-configuration")
					})

					ItUpdatesDhcp3Config()
					ItRestartsDhcp()
					ItBroadcastsMACAddresses()
				})

				Context("when dhcp was previously configured with the same configuration", func() {
					BeforeEach(func() {
						fs.WriteFileString("/etc/dhcp3/dhclient.conf", expectedUbuntuDHCPConfig)
					})

					ItUpdatesDhcp3Config()
					ItDoesNotRestartDhcp()
					ItBroadcastsMACAddresses()
				})
			})

			Context("when dhclient3 is not installed on the system", func() {
				BeforeEach(func() { cmdRunner.CommandExistsValue = false })

				Context("when dhcp was not previously configured", func() {
					ItUpdatesDhcpConfig()
					ItRestartsDhcp()
				})

				Context("when dhcp was previously configured with different configuration", func() {
					BeforeEach(func() {
						fs.WriteFileString("/etc/dhcp/dhclient.conf", "fake-other-configuration")
					})

					ItUpdatesDhcpConfig()
					ItRestartsDhcp()
				})

				Context("when dhcp was previously configured with the same configuration", func() {
					BeforeEach(func() {
						fs.WriteFileString("/etc/dhcp/dhclient.conf", expectedUbuntuDHCPConfig)
					})

					ItUpdatesDhcpConfig()
					ItDoesNotRestartDhcp()
				})
			})
		})

		Describe("SetupManualNetworking", func() {
			var errCh chan error

			BeforeEach(func() {
				errCh = make(chan error)
			})

			BeforeEach(func() {
				// For mac addr to interface resolution
				fs.WriteFile("/sys/class/net/eth0", []byte{})
				fs.WriteFileString("/sys/class/net/eth0/address", "22:00:0a:1f:ac:2a\n")
				fs.SetGlob("/sys/class/net/*", []string{"/sys/class/net/eth0"})
			})

			networks := boshsettings.Networks{
				"bosh": boshsettings.Network{
					Default: []string{"dns", "gateway"},
					IP:      "192.168.195.6",
					Netmask: "255.255.255.0",
					Gateway: "192.168.195.1",
					Mac:     "22:00:0a:1f:ac:2a",
					DNS:     []string{"10.80.130.1", "10.80.130.2"},
				},
			}

			Context("when manual networking was not previously configured", func() {
				It("writes /etc/network/interfaces", func() {
					err := netManager.SetupManualNetworking(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					networkConfig := fs.GetFileTestStat("/etc/network/interfaces")
					Expect(networkConfig).ToNot(BeNil())
					Expect(networkConfig.StringContents()).To(Equal(expectedUbuntuNetworkInterfaces))
				})

				It("restarts networking", func() {
					err := netManager.SetupManualNetworking(networks, errCh)
					Expect(err).ToNot(HaveOccurred())

					<-errCh // wait for all arpings

					Expect(len(cmdRunner.RunCommands) >= 2).To(BeTrue())
					Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"service", "network-interface", "stop", "INTERFACE=eth0"}))
					Expect(cmdRunner.RunCommands[1]).To(Equal([]string{"service", "network-interface", "start", "INTERFACE=eth0"}))
				})

				It("updates dns", func() {
					err := netManager.SetupManualNetworking(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					resolvConf := fs.GetFileTestStat("/etc/resolv.conf")
					Expect(resolvConf).ToNot(BeNil())
					Expect(resolvConf.StringContents()).To(Equal(expectedUbuntuResolvConf))
				})

				It("starts broadcasting the MAC addresses", func() {
					err := netManager.SetupManualNetworking(networks, errCh)
					Expect(err).ToNot(HaveOccurred())

					<-errCh // wait for all arpings

					Expect(addressBroadcaster.BroadcastMACAddressesAddresses).To(Equal([]boship.InterfaceAddress{
						boship.NewSimpleInterfaceAddress("eth0", "192.168.195.6"),
					}))
				})
			})

			Context("when manual networking was previously configured with different configuration", func() {
				BeforeEach(func() {
					fs.WriteFileString("/etc/network/interfaces", "fake-manual-config")
				})

				It("updates /etc/network/interfaces", func() {
					err := netManager.SetupManualNetworking(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					networkConfig := fs.GetFileTestStat("/etc/network/interfaces")
					Expect(networkConfig).ToNot(BeNil())
					Expect(networkConfig.StringContents()).To(Equal(expectedUbuntuNetworkInterfaces))
				})

				It("restarts networking", func() {
					err := netManager.SetupManualNetworking(networks, errCh)
					Expect(err).ToNot(HaveOccurred())

					<-errCh // wait for all arpings

					Expect(len(cmdRunner.RunCommands) >= 2).To(BeTrue())
					Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"service", "network-interface", "stop", "INTERFACE=eth0"}))
					Expect(cmdRunner.RunCommands[1]).To(Equal([]string{"service", "network-interface", "start", "INTERFACE=eth0"}))
				})

				It("updates dns", func() {
					err := netManager.SetupManualNetworking(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					resolvConf := fs.GetFileTestStat("/etc/resolv.conf")
					Expect(resolvConf).ToNot(BeNil())
					Expect(resolvConf.StringContents()).To(Equal(expectedUbuntuResolvConf))
				})

				It("starts broadcasting the MAC addresses", func() {
					err := netManager.SetupManualNetworking(networks, errCh)
					Expect(err).ToNot(HaveOccurred())

					<-errCh // wait for all arpings

					Expect(addressBroadcaster.BroadcastMACAddressesAddresses).To(Equal([]boship.InterfaceAddress{
						boship.NewSimpleInterfaceAddress("eth0", "192.168.195.6"),
					}))
				})
			})

			Context("when manual networking was previously configured with same configuration", func() {
				BeforeEach(func() {
					fs.WriteFileString("/etc/network/interfaces", expectedUbuntuNetworkInterfaces)
				})

				It("keeps same /etc/network/interfaces", func() {
					err := netManager.SetupManualNetworking(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					networkConfig := fs.GetFileTestStat("/etc/network/interfaces")
					Expect(networkConfig).ToNot(BeNil())
					Expect(networkConfig.StringContents()).To(Equal(expectedUbuntuNetworkInterfaces))
				})

				It("does not restart networking because configuration did not change", func() {
					err := netManager.SetupManualNetworking(networks, errCh)
					Expect(err).ToNot(HaveOccurred())

					<-errCh // wait for all arpings

					for _, cmd := range cmdRunner.RunCommands {
						Expect(cmd[0]).ToNot(Equal("service"))
					}
				})

				It("updates /etc/resolv.conf for DNS", func() {
					err := netManager.SetupManualNetworking(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					resolvConf := fs.GetFileTestStat("/etc/resolv.conf")
					Expect(resolvConf).ToNot(BeNil())
					Expect(resolvConf.StringContents()).To(Equal(expectedUbuntuResolvConf))
				})

				It("starts broadcasting the MAC addresses", func() {
					err := netManager.SetupManualNetworking(networks, errCh)
					Expect(err).ToNot(HaveOccurred())

					<-errCh // wait for all arpings

					Expect(addressBroadcaster.BroadcastMACAddressesAddresses).To(Equal([]boship.InterfaceAddress{
						boship.NewSimpleInterfaceAddress("eth0", "192.168.195.6"),
					}))
				})
			})
		})
	})
}
Ejemplo n.º 3
0
func (c customNetwork) ToInterfaceAddress() boship.InterfaceAddress {
	return boship.NewSimpleInterfaceAddress(c.Interface, c.IP)
}
Ejemplo n.º 4
0
		})

		It("restarts networking", func() {
			err := netManager.SetupManualNetworking(networks, errCh)
			Expect(err).ToNot(HaveOccurred())

			fs.GetFileTestStat("/etc/sysconfig/network-scripts/ifcfg-eth0")
			fs.GetFileTestStat("/etc/resolv.conf")

			<-errCh // wait for all arpings

			Expect(len(cmdRunner.RunCommands)).To(Equal(1))
			Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"service", "network", "restart"}))
		})

		It("starts broadcasting the MAC addresses", func() {
			err := netManager.SetupManualNetworking(networks, errCh)
			Expect(err).ToNot(HaveOccurred())

			fs.GetFileTestStat("/etc/sysconfig/network-scripts/ifcfg-eth0")
			fs.GetFileTestStat("/etc/resolv.conf")

			<-errCh // wait for all arpings

			Expect(addressBroadcaster.BroadcastMACAddressesAddresses).To(Equal([]boship.InterfaceAddress{
				boship.NewSimpleInterfaceAddress("eth0", "192.168.195.6"),
			}))
		})
	})
})
func init() {
	const expectedCentosDHCPConfig = `# Generated by bosh-agent

option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;

send host-name "<hostname>";

request subnet-mask, broadcast-address, time-offset, routers,
	domain-name, domain-name-servers, domain-search, host-name,
	netbios-name-servers, netbios-scope, interface-mtu,
	rfc3442-classless-static-routes, ntp-servers;

prepend domain-name-servers xx.xx.xx.xx;
prepend domain-name-servers yy.yy.yy.yy;
prepend domain-name-servers zz.zz.zz.zz;
`

	const expectedCentosIfcfg = `DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.195.6
NETMASK=255.255.255.0
BROADCAST=192.168.195.255
GATEWAY=192.168.195.1
ONBOOT=yes`

	Describe("centos", func() {
		var (
			fs                     *fakesys.FakeFileSystem
			cmdRunner              *fakesys.FakeCmdRunner
			defaultNetworkResolver *fakenet.FakeDefaultNetworkResolver
			ipResolver             *fakeip.FakeIPResolver
			addressBroadcaster     *fakearp.FakeAddressBroadcaster
			netManager             NetManager
		)

		BeforeEach(func() {
			fs = fakesys.NewFakeFileSystem()
			cmdRunner = fakesys.NewFakeCmdRunner()
			defaultNetworkResolver = &fakenet.FakeDefaultNetworkResolver{}
			ipResolver = &fakeip.FakeIPResolver{}
			addressBroadcaster = &fakearp.FakeAddressBroadcaster{}
			logger := boshlog.NewLogger(boshlog.LevelNone)
			netManager = NewCentosNetManager(
				fs,
				cmdRunner,
				defaultNetworkResolver,
				ipResolver,
				addressBroadcaster,
				logger,
			)
		})

		Describe("SetupDhcp", func() {
			networks := boshsettings.Networks{
				"bosh": boshsettings.Network{
					Default: []string{"dns"},
					DNS:     []string{"xx.xx.xx.xx", "yy.yy.yy.yy", "zz.zz.zz.zz"},
				},
				"vip": boshsettings.Network{
					Default: []string{},
					DNS:     []string{"aa.aa.aa.aa"},
				},
			}

			ItBroadcastsMACAddresses := func() {
				It("starts broadcasting the MAC addresses", func() {
					errCh := make(chan error)

					err := netManager.SetupDhcp(networks, errCh)
					Expect(err).ToNot(HaveOccurred())

					<-errCh // wait for all arpings

					Expect(addressBroadcaster.BroadcastMACAddressesAddresses).To(Equal(
						[]boship.InterfaceAddress{
							// Resolve IP address because IP may not be known
							boship.NewResolvingInterfaceAddress("eth0", ipResolver),
						},
					))
				})
			}

			Context("when dhcp was not previously configured", func() {
				It("writes dhcp configuration", func() {
					err := netManager.SetupDhcp(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					dhcpConfig := fs.GetFileTestStat("/etc/dhcp/dhclient.conf")
					Expect(dhcpConfig).ToNot(BeNil())
					Expect(dhcpConfig.StringContents()).To(Equal(expectedCentosDHCPConfig))
				})

				It("restarts network", func() {
					err := netManager.SetupDhcp(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					Expect(len(cmdRunner.RunCommands)).To(Equal(1))
					Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"service", "network", "restart"}))
				})

				ItBroadcastsMACAddresses()
			})

			Context("when dhcp was previously configured with different configuration", func() {
				BeforeEach(func() {
					fs.WriteFileString("/etc/dhcp/dhclient.conf", "fake-other-configuration")
				})

				It("updates dhcp configuration", func() {
					err := netManager.SetupDhcp(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					dhcpConfig := fs.GetFileTestStat("/etc/dhcp/dhclient.conf")
					Expect(dhcpConfig).ToNot(BeNil())
					Expect(dhcpConfig.StringContents()).To(Equal(expectedCentosDHCPConfig))
				})

				It("restarts network", func() {
					err := netManager.SetupDhcp(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					Expect(len(cmdRunner.RunCommands)).To(Equal(1))
					Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"service", "network", "restart"}))
				})

				ItBroadcastsMACAddresses()
			})

			Context("when dhcp was previously configured with same configuration", func() {
				BeforeEach(func() {
					fs.WriteFileString("/etc/dhcp/dhclient.conf", expectedCentosDHCPConfig)
				})

				It("keeps dhcp configuration", func() {
					err := netManager.SetupDhcp(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					dhcpConfig := fs.GetFileTestStat("/etc/dhcp/dhclient.conf")
					Expect(dhcpConfig).ToNot(BeNil())
					Expect(dhcpConfig.StringContents()).To(Equal(expectedCentosDHCPConfig))
				})

				It("does not restart network", func() {
					err := netManager.SetupDhcp(networks, nil)
					Expect(err).ToNot(HaveOccurred())

					Expect(len(cmdRunner.RunCommands)).To(Equal(0))
				})

				ItBroadcastsMACAddresses()
			})
		})

		Describe("SetupManualNetworking", func() {
			var errCh chan error

			BeforeEach(func() {
				errCh = make(chan error)
			})

			BeforeEach(func() {
				fs.WriteFileString("/sys/class/net/eth0/address", "22:00:0a:1f:ac:2a\n")
				fs.SetGlob("/sys/class/net/*", []string{"/sys/class/net/eth0"})
			})

			networks := boshsettings.Networks{
				"bosh": boshsettings.Network{
					Default: []string{"dns", "gateway"},
					IP:      "192.168.195.6",
					Netmask: "255.255.255.0",
					Gateway: "192.168.195.1",
					Mac:     "22:00:0a:1f:ac:2a",
					DNS:     []string{"10.80.130.1", "10.80.130.2"},
				},
			}

			It("sets up centos expected ifconfig", func() {
				err := netManager.SetupManualNetworking(networks, nil)
				Expect(err).ToNot(HaveOccurred())

				networkConfig := fs.GetFileTestStat("/etc/sysconfig/network-scripts/ifcfg-eth0")
				Expect(networkConfig).ToNot(BeNil())
				Expect(networkConfig.StringContents()).To(Equal(expectedCentosIfcfg))
			})

			It("sets up centos /etc/resolv.conf", func() {
				err := netManager.SetupManualNetworking(networks, nil)
				Expect(err).ToNot(HaveOccurred())

				resolvConf := fs.GetFileTestStat("/etc/resolv.conf")
				Expect(resolvConf).ToNot(BeNil())
				Expect(resolvConf.StringContents()).To(Equal(expectedCentosResolvConf))
			})

			It("restarts networking", func() {
				err := netManager.SetupManualNetworking(networks, errCh)
				Expect(err).ToNot(HaveOccurred())

				fs.GetFileTestStat("/etc/sysconfig/network-scripts/ifcfg-eth0")
				fs.GetFileTestStat("/etc/resolv.conf")

				<-errCh // wait for all arpings

				Expect(len(cmdRunner.RunCommands)).To(Equal(1))
				Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"service", "network", "restart"}))
			})

			It("starts broadcasting the MAC addresses", func() {
				err := netManager.SetupManualNetworking(networks, errCh)
				Expect(err).ToNot(HaveOccurred())

				fs.GetFileTestStat("/etc/sysconfig/network-scripts/ifcfg-eth0")
				fs.GetFileTestStat("/etc/resolv.conf")

				<-errCh // wait for all arpings

				Expect(addressBroadcaster.BroadcastMACAddressesAddresses).To(Equal([]boship.InterfaceAddress{
					boship.NewSimpleInterfaceAddress("eth0", "192.168.195.6"),
				}))
			})
		})
	})
}