func buildCompiler() ( deps compilerDeps, compiler Compiler, ) { deps.compressor = fakecmd.NewFakeCompressor() deps.blobstore = &fakeblobstore.FakeBlobstore{} deps.fs = fakesys.NewFakeFileSystem() deps.runner = fakesys.NewFakeCmdRunner() deps.packageApplier = fakepa.NewFakePackageApplier() fakeBundleCollection := fakebc.NewFakeBundleCollection() bundleDefinition := boshmodels.Package{ Name: "pkg_name", Version: "pkg_version", } deps.bundle = fakeBundleCollection.FakeGet(bundleDefinition) deps.bundle.InstallPath = "/fake-dir/data/packages/pkg_name/pkg_version" deps.bundle.EnablePath = "/fake-dir/packages/pkg_name" deps.packagesBc = fakeBundleCollection compiler = NewConcreteCompiler( deps.compressor, deps.blobstore, deps.fs, deps.runner, boshdirs.NewDirectoriesProvider("/fake-dir"), deps.packageApplier, deps.packagesBc, ) return }
func TestNewDrainScript(t *testing.T) { runner := fakesys.NewFakeCmdRunner() fs := fakesys.NewFakeFileSystem() dirProvider := boshdir.NewDirectoriesProvider("/var/vcap") scriptProvider := NewConcreteDrainScriptProvider(runner, fs, dirProvider) drainScript := scriptProvider.NewDrainScript("foo") assert.Equal(t, drainScript.Path(), "/var/vcap/jobs/foo/bin/drain") }
func buildCompiler() ( compressor *fakecmd.FakeCompressor, blobstore *fakeblobstore.FakeBlobstore, fs *fakesys.FakeFileSystem, runner *fakesys.FakeCmdRunner, compiler Compiler, ) { compressor = fakecmd.NewFakeCompressor() blobstore = &fakeblobstore.FakeBlobstore{} fs = fakesys.NewFakeFileSystem() runner = fakesys.NewFakeCmdRunner() compiler = newConcreteCompiler(compressor, blobstore, fs, runner, boshdirs.NewDirectoriesProvider("/fake-dir")) return }
func NewFakePlatform() (platform *FakePlatform) { platform = new(FakePlatform) platform.Fs = fakesys.NewFakeFileSystem() platform.Runner = fakesys.NewFakeCmdRunner() platform.FakeCompressor = fakecmd.NewFakeCompressor() platform.FakeCopier = fakecmd.NewFakeCopier() platform.FakeVitalsService = fakevitals.NewFakeService() platform.DevicePathResolver = fakedpresolv.NewFakeDevicePathResolver() platform.AddUserToGroupsGroups = make(map[string][]string) platform.SetupSshPublicKeys = make(map[string]string) platform.UserPasswords = make(map[string]string) platform.ScsiDiskMap = make(map[string]string) return }
func init() { Describe("Testing with Ginkgo", func() { It("new drain script", func() { runner := fakesys.NewFakeCmdRunner() fs := fakesys.NewFakeFileSystem() dirProvider := boshdir.NewDirectoriesProvider("/var/vcap") scriptProvider := NewConcreteDrainScriptProvider(runner, fs, dirProvider) drainScript := scriptProvider.NewDrainScript("foo") Expect(drainScript.Path()).To(Equal("/var/vcap/jobs/foo/bin/drain")) }) }) }
func buildDrain() ( cmdRunner *fakesys.FakeCmdRunner, fs *fakesys.FakeFileSystem, notifier *fakenotif.FakeNotifier, specService *fakeas.FakeV1Service, action drainAction, ) { cmdRunner = fakesys.NewFakeCmdRunner() fs = fakesys.NewFakeFileSystem() notifier = fakenotif.NewFakeNotifier() specService = fakeas.NewFakeV1Service() dirProvider := boshdirs.NewDirectoriesProvider("/fake-dir") drainScriptProvider := boshdrain.NewDrainScriptProvider(cmdRunner, fs, dirProvider) action = newDrain(notifier, specService, drainScriptProvider) return }
func buildDrainScript(commandResult fakesys.FakeCmdResult) ( drainScript ConcreteDrainScript, params staticDrainParams, runner *fakesys.FakeCmdRunner, fs *fakesys.FakeFileSystem, ) { fs = fakesys.NewFakeFileSystem() runner = fakesys.NewFakeCmdRunner() drainScript = NewConcreteDrainScript(fs, runner, "/fake/script") params = staticDrainParams{ jobChange: "job_shutdown", hashChange: "hash_unchanged", updatedPackages: []string{"foo", "bar"}, } runner.AddCmdResult("/fake/script"+" job_shutdown hash_unchanged foo bar", commandResult) return }
func TestDecompressFileToDirUsesNoSameOwnerOption(t *testing.T) { fs, _ := getCompressorDependencies() cmdRunner := fakesys.NewFakeCmdRunner() dc := NewTarballCompressor(cmdRunner, fs) dstDir := createdTmpDir(t, fs) defer os.RemoveAll(dstDir) tarball := fixtureSrcTgz(t) err := dc.DecompressFileToDir(tarball, dstDir) assert.NoError(t, err) assert.Equal(t, 1, len(cmdRunner.RunCommands)) assert.Equal(t, []string{ "tar", "--no-same-owner", "-xzvf", tarball.Name(), "-C", dstDir, }, cmdRunner.RunCommands[0]) }
func (ia failingInterfaceAddress) GetIP() (string, error) { return "", errors.New("fake-get-ip-err") } var _ = Describe("arping", func() { const arpingIterations = 6 var ( fs *fakesys.FakeFileSystem cmdRunner *fakesys.FakeCmdRunner arping AddressBroadcaster ) 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"), }
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 zz.zz.zz.zz, yy.yy.yy.yy, xx.xx.xx.xx; ` Describe("ubuntuNetManager", func() { var ( fs *fakesys.FakeFileSystem cmdRunner *fakesys.FakeCmdRunner defaultNetworkResolver *fakenet.FakeDefaultNetworkResolver netManager NetManager ) BeforeEach(func() { fs = fakesys.NewFakeFileSystem() cmdRunner = fakesys.NewFakeCmdRunner() defaultNetworkResolver = &fakenet.FakeDefaultNetworkResolver{} logger := boshlog.NewLogger(boshlog.LevelNone) netManager = NewUbuntuNetManager(fs, cmdRunner, defaultNetworkResolver, 1*time.Millisecond, 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) 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) 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) 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) 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) Expect(err).ToNot(HaveOccurred()) Expect(len(cmdRunner.RunCommands)).To(Equal(0)) }) } Context("when dhclient3 is installed on the system", func() { BeforeEach(func() { cmdRunner.CommandExistsValue = true }) Context("when dhcp was not previously configured", func() { ItUpdatesDhcp3Config() ItRestartsDhcp() }) Context("when dhcp was previously configured with different configuration", func() { BeforeEach(func() { fs.WriteFileString("/etc/dhcp3/dhclient.conf", "fake-other-configuration") }) ItUpdatesDhcp3Config() ItRestartsDhcp() }) Context("when dhcp was previously configured with the same configuration", func() { BeforeEach(func() { fs.WriteFileString("/etc/dhcp3/dhclient.conf", expectedUbuntuDHCPConfig) }) ItUpdatesDhcp3Config() ItDoesNotRestartDhcp() }) }) 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.2", "10.80.130.1"}, }, } 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 sending arping", func() { err := netManager.SetupManualNetworking(networks, errCh) Expect(err).ToNot(HaveOccurred()) <-errCh // wait for all arpings Expect(cmdRunner.RunCommands[2]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "eth0", "192.168.195.6"})) Expect(cmdRunner.RunCommands[7]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "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 sending 6 arp pings", func() { err := netManager.SetupManualNetworking(networks, errCh) Expect(err).ToNot(HaveOccurred()) <-errCh // wait for all arpings Expect(cmdRunner.RunCommands[2]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "eth0", "192.168.195.6"})) Expect(cmdRunner.RunCommands[7]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "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 sending 6 arp ping", func() { err := netManager.SetupManualNetworking(networks, errCh) Expect(err).ToNot(HaveOccurred()) <-errCh // wait for all arpings Expect(len(cmdRunner.RunCommands)).To(Equal(6)) Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "eth0", "192.168.195.6"})) Expect(cmdRunner.RunCommands[5]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "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 zz.zz.zz.zz; prepend domain-name-servers yy.yy.yy.yy; prepend domain-name-servers xx.xx.xx.xx; ` 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 ( collector *fakestats.FakeStatsCollector fs *fakesys.FakeFileSystem cmdRunner *fakesys.FakeCmdRunner diskManager *fakedisk.FakeDiskManager dirProvider boshdirs.DirectoriesProvider platform Platform cdutil *fakecd.FakeCdUtil compressor boshcmd.Compressor copier boshcmd.Copier vitalsService boshvitals.Service logger boshlog.Logger ) BeforeEach(func() { collector = &fakestats.FakeStatsCollector{} fs = fakesys.NewFakeFileSystem() cmdRunner = fakesys.NewFakeCmdRunner() diskManager = fakedisk.NewFakeDiskManager() dirProvider = boshdirs.NewDirectoriesProvider("/fake-dir") cdutil = fakecd.NewFakeCdUtil() compressor = boshcmd.NewTarballCompressor(cmdRunner, fs) copier = boshcmd.NewCpCopier(cmdRunner, fs) vitalsService = boshvitals.NewService(collector, dirProvider) logger = boshlog.NewLogger(boshlog.LevelNone) }) JustBeforeEach(func() { netManager := boshnet.NewCentosNetManager(fs, cmdRunner, 1*time.Millisecond) platform = NewLinuxPlatform( fs, cmdRunner, collector, compressor, copier, dirProvider, vitalsService, cdutil, diskManager, netManager, 1*time.Millisecond, 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"}, }, } Context("when dhcp was not previously configured", func() { It("writes dhcp configuration", func() { err := platform.SetupDhcp(networks) 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 := platform.SetupDhcp(networks) Expect(err).ToNot(HaveOccurred()) Expect(len(cmdRunner.RunCommands)).To(Equal(1)) Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"service", "network", "restart"})) }) }) 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 := platform.SetupDhcp(networks) 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 := platform.SetupDhcp(networks) Expect(err).ToNot(HaveOccurred()) Expect(len(cmdRunner.RunCommands)).To(Equal(1)) Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"service", "network", "restart"})) }) }) 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 := platform.SetupDhcp(networks) 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 := platform.SetupDhcp(networks) Expect(err).ToNot(HaveOccurred()) Expect(len(cmdRunner.RunCommands)).To(Equal(0)) }) }) }) Describe("SetupManualNetworking", func() { 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.2", "10.80.130.1"}, }, } BeforeEach(func() { 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"}) }) It("sets up centos expected ifconfig", func() { err := platform.SetupManualNetworking(networks) 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 := platform.SetupManualNetworking(networks) 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 := platform.SetupManualNetworking(networks) Expect(err).ToNot(HaveOccurred()) fs.GetFileTestStat("/etc/sysconfig/network-scripts/ifcfg-eth0") fs.GetFileTestStat("/etc/resolv.conf") time.Sleep(100 * time.Millisecond) Expect(len(cmdRunner.RunCommands)).To(Equal(7)) Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"service", "network", "restart"})) }) It("runs arping commands", func() { err := platform.SetupManualNetworking(networks) Expect(err).ToNot(HaveOccurred()) fs.GetFileTestStat("/etc/sysconfig/network-scripts/ifcfg-eth0") fs.GetFileTestStat("/etc/resolv.conf") time.Sleep(100 * time.Millisecond) Expect(cmdRunner.RunCommands[1]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "eth0", "192.168.195.6"})) Expect(cmdRunner.RunCommands[6]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "eth0", "192.168.195.6"})) }) }) }) }
func init() { Describe("Testing with Ginkgo", func() { It("sfdisk partition", func() { runner := fakesys.NewFakeCmdRunner() runner.AddCmdResult("sfdisk -d /dev/sda", fakesys.FakeCmdResult{Stdout: devSdaSfdiskEmptyDump}) partitioner := createSfdiskPartitionerForTests(runner) partitions := []Partition{ {Type: PartitionTypeSwap, SizeInMb: 512}, {Type: PartitionTypeLinux, SizeInMb: 1024}, {Type: PartitionTypeLinux, SizeInMb: 512}, } partitioner.Partition("/dev/sda", partitions) Expect(1).To(Equal(len(runner.RunCommandsWithInput))) Expect(runner.RunCommandsWithInput[0]).To(Equal([]string{",512,S\n,1024,L\n,,L\n", "sfdisk", "-uM", "/dev/sda"})) }) It("sfdisk partition with no partition table", func() { runner := fakesys.NewFakeCmdRunner() runner.AddCmdResult("sfdisk -d /dev/sda", fakesys.FakeCmdResult{Stderr: devSdaSfdiskNotableDumpStderr}) partitioner := createSfdiskPartitionerForTests(runner) partitions := []Partition{ {Type: PartitionTypeSwap, SizeInMb: 512}, {Type: PartitionTypeLinux, SizeInMb: 1024}, {Type: PartitionTypeLinux, SizeInMb: 512}, } partitioner.Partition("/dev/sda", partitions) Expect(1).To(Equal(len(runner.RunCommandsWithInput))) Expect(runner.RunCommandsWithInput[0]).To(Equal([]string{",512,S\n,1024,L\n,,L\n", "sfdisk", "-uM", "/dev/sda"})) }) It("sfdisk get device size in mb", func() { runner := fakesys.NewFakeCmdRunner() runner.AddCmdResult("sfdisk -s /dev/sda", fakesys.FakeCmdResult{Stdout: fmt.Sprintf("%d\n", 40000*1024)}) partitioner := createSfdiskPartitionerForTests(runner) size, err := partitioner.GetDeviceSizeInMb("/dev/sda") Expect(err).ToNot(HaveOccurred()) Expect(uint64(40000)).To(Equal(size)) }) It("sfdisk partition when partitions already match", func() { runner := fakesys.NewFakeCmdRunner() runner.AddCmdResult("sfdisk -d /dev/sda", fakesys.FakeCmdResult{Stdout: devSdaSfdiskDump}) runner.AddCmdResult("sfdisk -s /dev/sda", fakesys.FakeCmdResult{Stdout: fmt.Sprintf("%d\n", 2048*1024)}) runner.AddCmdResult("sfdisk -s /dev/sda1", fakesys.FakeCmdResult{Stdout: fmt.Sprintf("%d\n", 525*1024)}) runner.AddCmdResult("sfdisk -s /dev/sda2", fakesys.FakeCmdResult{Stdout: fmt.Sprintf("%d\n", 1020*1024)}) runner.AddCmdResult("sfdisk -s /dev/sda3", fakesys.FakeCmdResult{Stdout: fmt.Sprintf("%d\n", 500*1024)}) partitioner := createSfdiskPartitionerForTests(runner) partitions := []Partition{ {Type: PartitionTypeSwap, SizeInMb: 512}, {Type: PartitionTypeLinux, SizeInMb: 1024}, {Type: PartitionTypeLinux, SizeInMb: 512}, } partitioner.Partition("/dev/sda", partitions) Expect(0).To(Equal(len(runner.RunCommandsWithInput))) }) It("sfdisk partition with last partition not matching size", func() { runner := fakesys.NewFakeCmdRunner() runner.AddCmdResult("sfdisk -d /dev/sda", fakesys.FakeCmdResult{Stdout: devSdaSfdiskDumpOnePartition}) runner.AddCmdResult("sfdisk -s /dev/sda", fakesys.FakeCmdResult{Stdout: fmt.Sprintf("%d\n", 2048*1024)}) runner.AddCmdResult("sfdisk -s /dev/sda1", fakesys.FakeCmdResult{Stdout: fmt.Sprintf("%d\n", 1024*1024)}) runner.AddCmdResult("sfdisk -s /dev/sda2", fakesys.FakeCmdResult{Stdout: fmt.Sprintf("%d\n", 512*1024)}) partitioner := createSfdiskPartitionerForTests(runner) partitions := []Partition{ {Type: PartitionTypeLinux, SizeInMb: 1024}, {Type: PartitionTypeLinux}, } partitioner.Partition("/dev/sda", partitions) Expect(1).To(Equal(len(runner.RunCommandsWithInput))) Expect(runner.RunCommandsWithInput[0]).To(Equal([]string{",1024,L\n,,L\n", "sfdisk", "-uM", "/dev/sda"})) }) It("sfdisk partition with last partition filling disk", func() { runner := fakesys.NewFakeCmdRunner() runner.AddCmdResult("sfdisk -d /dev/sda", fakesys.FakeCmdResult{Stdout: devSdaSfdiskDumpOnePartition}) runner.AddCmdResult("sfdisk -s /dev/sda", fakesys.FakeCmdResult{Stdout: fmt.Sprintf("%d\n", 2048*1024)}) runner.AddCmdResult("sfdisk -s /dev/sda1", fakesys.FakeCmdResult{Stdout: fmt.Sprintf("%d\n", 1024*1024)}) runner.AddCmdResult("sfdisk -s /dev/sda2", fakesys.FakeCmdResult{Stdout: fmt.Sprintf("%d\n", 1024*1024)}) partitioner := createSfdiskPartitionerForTests(runner) partitions := []Partition{ {Type: PartitionTypeLinux, SizeInMb: 1024}, {Type: PartitionTypeLinux}, } partitioner.Partition("/dev/sda", partitions) Expect(0).To(Equal(len(runner.RunCommandsWithInput))) }) }) }
func init() { Describe("Testing with Ginkgo", func() { It("compress files in dir", func() { fs, cmdRunner := getCompressorDependencies() dc := NewTarballCompressor(cmdRunner, fs) srcDir := fixtureSrcDir(GinkgoT()) tgzName, err := dc.CompressFilesInDir(srcDir) Expect(err).ToNot(HaveOccurred()) defer os.Remove(tgzName) dstDir := createdTmpDir(GinkgoT(), fs) defer os.RemoveAll(dstDir) _, _, err = cmdRunner.RunCommand("tar", "--no-same-owner", "-xzpf", tgzName, "-C", dstDir) Expect(err).ToNot(HaveOccurred()) content, err := fs.ReadFileString(dstDir + "/app.stdout.log") Expect(err).ToNot(HaveOccurred()) assert.Contains(GinkgoT(), content, "this is app stdout") content, err = fs.ReadFileString(dstDir + "/app.stderr.log") Expect(err).ToNot(HaveOccurred()) assert.Contains(GinkgoT(), content, "this is app stderr") content, err = fs.ReadFileString(dstDir + "/other_logs/other_app.stdout.log") Expect(err).ToNot(HaveOccurred()) assert.Contains(GinkgoT(), content, "this is other app stdout") }) It("decompress file to dir", func() { fs, cmdRunner := getCompressorDependencies() dc := NewTarballCompressor(cmdRunner, fs) dstDir := createdTmpDir(GinkgoT(), fs) defer os.RemoveAll(dstDir) err := dc.DecompressFileToDir(fixtureSrcTgz(GinkgoT()), dstDir) Expect(err).ToNot(HaveOccurred()) content, err := fs.ReadFileString(dstDir + "/not-nested-file") Expect(err).ToNot(HaveOccurred()) assert.Contains(GinkgoT(), content, "not-nested-file") content, err = fs.ReadFileString(dstDir + "/dir/nested-file") Expect(err).ToNot(HaveOccurred()) assert.Contains(GinkgoT(), content, "nested-file") content, err = fs.ReadFileString(dstDir + "/dir/nested-dir/double-nested-file") Expect(err).ToNot(HaveOccurred()) assert.Contains(GinkgoT(), content, "double-nested-file") content, err = fs.ReadFileString(dstDir + "/empty-dir") Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("is a directory")) content, err = fs.ReadFileString(dstDir + "/dir/empty-nested-dir") Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("is a directory")) }) It("decompress file to dir returns error", func() { nonExistentDstDir := filepath.Join(os.TempDir(), "TestDecompressFileToDirReturnsError") fs, cmdRunner := getCompressorDependencies() dc := NewTarballCompressor(cmdRunner, fs) err := dc.DecompressFileToDir(fixtureSrcTgz(GinkgoT()), nonExistentDstDir) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring(nonExistentDstDir)) }) It("decompress file to dir uses no same owner option", func() { fs, _ := getCompressorDependencies() cmdRunner := fakesys.NewFakeCmdRunner() dc := NewTarballCompressor(cmdRunner, fs) dstDir := createdTmpDir(GinkgoT(), fs) defer os.RemoveAll(dstDir) tarballPath := fixtureSrcTgz(GinkgoT()) err := dc.DecompressFileToDir(tarballPath, dstDir) Expect(err).ToNot(HaveOccurred()) Expect(1).To(Equal(len(cmdRunner.RunCommands))) assert.Equal(GinkgoT(), []string{ "tar", "--no-same-owner", "-xzvf", tarballPath, "-C", dstDir, }, cmdRunner.RunCommands[0]) }) }) }
func init() { Describe("Testing with Ginkgo", func() { It("linux format when using swap fs", func() { fakeRunner := fakesys.NewFakeCmdRunner() fakeFs := fakesys.NewFakeFileSystem() fakeRunner.AddCmdResult("blkid -p /dev/xvda1", fakesys.FakeCmdResult{Stdout: `xxxxx TYPE="ext4" yyyy zzzz`}) formatter := NewLinuxFormatter(fakeRunner, fakeFs) formatter.Format("/dev/xvda1", FileSystemSwap) Expect(2).To(Equal(len(fakeRunner.RunCommands))) Expect(fakeRunner.RunCommands[1]).To(Equal([]string{"mkswap", "/dev/xvda1"})) }) It("linux format when using swap fs and partition is swap", func() { fakeRunner := fakesys.NewFakeCmdRunner() fakeFs := fakesys.NewFakeFileSystem() fakeRunner.AddCmdResult("blkid -p /dev/xvda1", fakesys.FakeCmdResult{Stdout: `xxxxx TYPE="swap" yyyy zzzz`}) formatter := NewLinuxFormatter(fakeRunner, fakeFs) formatter.Format("/dev/xvda1", FileSystemSwap) Expect(1).To(Equal(len(fakeRunner.RunCommands))) Expect(fakeRunner.RunCommands[0]).To(Equal([]string{"blkid", "-p", "/dev/xvda1"})) }) It("linux format when using ext4 fs with lazy itable support", func() { fakeRunner := fakesys.NewFakeCmdRunner() fakeFs := fakesys.NewFakeFileSystem() fakeFs.WriteFile("/sys/fs/ext4/features/lazy_itable_init", []byte{}) fakeRunner.AddCmdResult("blkid -p /dev/xvda1", fakesys.FakeCmdResult{Stdout: `xxxxx TYPE="ext2" yyyy zzzz`}) formatter := NewLinuxFormatter(fakeRunner, fakeFs) formatter.Format("/dev/xvda2", FileSystemExt4) Expect(2).To(Equal(len(fakeRunner.RunCommands))) Expect(fakeRunner.RunCommands[1]).To(Equal([]string{"mke2fs", "-t", "ext4", "-j", "-E", "lazy_itable_init=1", "/dev/xvda2"})) }) It("linux format when using ext4 fs without lazy itable support", func() { fakeRunner := fakesys.NewFakeCmdRunner() fakeFs := fakesys.NewFakeFileSystem() fakeRunner.AddCmdResult("blkid -p /dev/xvda1", fakesys.FakeCmdResult{Stdout: `xxxxx TYPE="ext2" yyyy zzzz`}) formatter := NewLinuxFormatter(fakeRunner, fakeFs) formatter.Format("/dev/xvda2", FileSystemExt4) Expect(2).To(Equal(len(fakeRunner.RunCommands))) Expect(fakeRunner.RunCommands[1]).To(Equal([]string{"mke2fs", "-t", "ext4", "-j", "/dev/xvda2"})) }) It("linux format when using ext4 fs and partition is ext4", func() { fakeRunner := fakesys.NewFakeCmdRunner() fakeFs := fakesys.NewFakeFileSystem() fakeRunner.AddCmdResult("blkid -p /dev/xvda1", fakesys.FakeCmdResult{Stdout: `xxxxx TYPE="ext4" yyyy zzzz`}) formatter := NewLinuxFormatter(fakeRunner, fakeFs) formatter.Format("/dev/xvda1", FileSystemExt4) Expect(1).To(Equal(len(fakeRunner.RunCommands))) Expect(fakeRunner.RunCommands[0]).To(Equal([]string{"blkid", "-p", "/dev/xvda1"})) }) }) }
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 zz.zz.zz.zz; prepend domain-name-servers yy.yy.yy.yy; prepend domain-name-servers xx.xx.xx.xx; ` Describe("ubuntu", func() { var ( collector *fakestats.FakeStatsCollector fs *fakesys.FakeFileSystem cmdRunner *fakesys.FakeCmdRunner diskManager *fakedisk.FakeDiskManager dirProvider boshdirs.DirectoriesProvider platform Platform cdutil *fakecd.FakeCdUtil compressor boshcmd.Compressor copier boshcmd.Copier vitalsService boshvitals.Service logger boshlog.Logger ) BeforeEach(func() { collector = &fakestats.FakeStatsCollector{} fs = fakesys.NewFakeFileSystem() cmdRunner = fakesys.NewFakeCmdRunner() diskManager = fakedisk.NewFakeDiskManager() dirProvider = boshdirs.NewDirectoriesProvider("/fake-dir") cdutil = fakecd.NewFakeCdUtil() compressor = boshcmd.NewTarballCompressor(cmdRunner, fs) copier = boshcmd.NewCpCopier(cmdRunner, fs) vitalsService = boshvitals.NewService(collector, dirProvider) logger = boshlog.NewLogger(boshlog.LevelNone) }) JustBeforeEach(func() { netManager := boshnet.NewUbuntuNetManager(fs, cmdRunner, 1*time.Millisecond) platform = NewLinuxPlatform( fs, cmdRunner, collector, compressor, copier, dirProvider, vitalsService, cdutil, diskManager, netManager, 1*time.Millisecond, 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"}, }, } Context("when dhcp was not previously configured", func() { It("updates dhclient.conf", func() { err := platform.SetupDhcp(networks) Expect(err).ToNot(HaveOccurred()) dhcpConfig := fs.GetFileTestStat("/etc/dhcp3/dhclient.conf") Expect(dhcpConfig).ToNot(BeNil()) Expect(dhcpConfig.StringContents()).To(Equal(expectedUbuntuDHCPConfig)) }) It("restarts dhclient", func() { err := platform.SetupDhcp(networks) Expect(err).ToNot(HaveOccurred()) Expect(len(cmdRunner.RunCommands)).To(Equal(2)) Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"pkill", "dhclient3"})) Expect(cmdRunner.RunCommands[1]).To(Equal([]string{"/etc/init.d/networking", "restart"})) }) }) Context("when dhcp was previously configured with different configuration", func() { BeforeEach(func() { fs.WriteFileString("/etc/dhcp3/dhclient.conf", "fake-other-configuration") }) It("sets up dhcp and restarts dhclient", func() { err := platform.SetupDhcp(networks) Expect(err).ToNot(HaveOccurred()) dhcpConfig := fs.GetFileTestStat("/etc/dhcp3/dhclient.conf") Expect(dhcpConfig).ToNot(BeNil()) Expect(dhcpConfig.StringContents()).To(Equal(expectedUbuntuDHCPConfig)) }) It("sets up dhcp and restarts dhclient", func() { err := platform.SetupDhcp(networks) Expect(err).ToNot(HaveOccurred()) Expect(len(cmdRunner.RunCommands)).To(Equal(2)) Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"pkill", "dhclient3"})) Expect(cmdRunner.RunCommands[1]).To(Equal([]string{"/etc/init.d/networking", "restart"})) }) }) Context("when dhcp was previously configured with the same configuration", func() { BeforeEach(func() { fs.WriteFileString("/etc/dhcp3/dhclient.conf", expectedUbuntuDHCPConfig) }) It("does not restart dhclient", func() { err := platform.SetupDhcp(networks) Expect(err).ToNot(HaveOccurred()) dhcpConfig := fs.GetFileTestStat("/etc/dhcp3/dhclient.conf") Expect(dhcpConfig).ToNot(BeNil()) Expect(dhcpConfig.StringContents()).To(Equal(expectedUbuntuDHCPConfig)) }) It("does not restart dhclient", func() { err := platform.SetupDhcp(networks) Expect(err).ToNot(HaveOccurred()) Expect(len(cmdRunner.RunCommands)).To(Equal(0)) }) }) }) Describe("SetupManualNetworking", func() { 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.2", "10.80.130.1"}, }, } Context("when manual networking was not previously configured", func() { It("writes /etc/network/interfaces", func() { err := platform.SetupManualNetworking(networks) 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 := platform.SetupManualNetworking(networks) Expect(err).ToNot(HaveOccurred()) 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 := platform.SetupManualNetworking(networks) Expect(err).ToNot(HaveOccurred()) resolvConf := fs.GetFileTestStat("/etc/resolv.conf") Expect(resolvConf).ToNot(BeNil()) Expect(resolvConf.StringContents()).To(Equal(expectedUbuntuResolvConf)) }) It("starts sending arping", func() { err := platform.SetupManualNetworking(networks) Expect(err).ToNot(HaveOccurred()) time.Sleep(100 * time.Millisecond) Expect(cmdRunner.RunCommands[2]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "eth0", "192.168.195.6"})) Expect(cmdRunner.RunCommands[7]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "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 := platform.SetupManualNetworking(networks) 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 := platform.SetupManualNetworking(networks) Expect(err).ToNot(HaveOccurred()) 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 := platform.SetupManualNetworking(networks) Expect(err).ToNot(HaveOccurred()) resolvConf := fs.GetFileTestStat("/etc/resolv.conf") Expect(resolvConf).ToNot(BeNil()) Expect(resolvConf.StringContents()).To(Equal(expectedUbuntuResolvConf)) }) It("starts sending arping", func() { err := platform.SetupManualNetworking(networks) Expect(err).ToNot(HaveOccurred()) time.Sleep(100 * time.Millisecond) Expect(cmdRunner.RunCommands[2]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "eth0", "192.168.195.6"})) Expect(cmdRunner.RunCommands[7]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "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 := platform.SetupManualNetworking(networks) 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 := platform.SetupManualNetworking(networks) Expect(err).ToNot(HaveOccurred()) for _, cmd := range cmdRunner.RunCommands { Expect(cmd[0]).ToNot(Equal("service")) } }) It("updates /etc/resolv.conf for DNS", func() { err := platform.SetupManualNetworking(networks) Expect(err).ToNot(HaveOccurred()) resolvConf := fs.GetFileTestStat("/etc/resolv.conf") Expect(resolvConf).ToNot(BeNil()) Expect(resolvConf.StringContents()).To(Equal(expectedUbuntuResolvConf)) }) It("starts sending 6 arp ping", func() { err := platform.SetupManualNetworking(networks) Expect(err).ToNot(HaveOccurred()) time.Sleep(100 * time.Millisecond) Expect(len(cmdRunner.RunCommands)).To(Equal(6)) Expect(cmdRunner.RunCommands[0]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "eth0", "192.168.195.6"})) Expect(cmdRunner.RunCommands[5]).To(Equal([]string{"arping", "-c", "1", "-U", "-I", "eth0", "192.168.195.6"})) }) }) }) }) }
func init() { Describe("concreteCompiler", func() { var ( compiler Compiler compressor *fakecmd.FakeCompressor blobstore *fakeblobstore.FakeBlobstore fs *fakesys.FakeFileSystem runner *fakesys.FakeCmdRunner packageApplier *fakepa.FakePackageApplier packagesBc *fakebc.FakeBundleCollection ) BeforeEach(func() { compressor = fakecmd.NewFakeCompressor() blobstore = &fakeblobstore.FakeBlobstore{} fs = fakesys.NewFakeFileSystem() runner = fakesys.NewFakeCmdRunner() packageApplier = fakepa.NewFakePackageApplier() packagesBc = fakebc.NewFakeBundleCollection() compiler = NewConcreteCompiler( compressor, blobstore, fs, runner, FakeCompileDirProvider{Dir: "/fake-compile-dir"}, packageApplier, packagesBc, ) }) BeforeEach(func() { fs.MkdirAll("/fake-compile-dir", os.ModePerm) }) Describe("Compile", func() { var ( bundle *fakebc.FakeBundle pkg Package pkgDeps []boshmodels.Package ) BeforeEach(func() { bundle = packagesBc.FakeGet(boshmodels.Package{ Name: "pkg_name", Version: "pkg_version", }) bundle.InstallPath = "/fake-dir/data/packages/pkg_name/pkg_version" bundle.EnablePath = "/fake-dir/packages/pkg_name" compressor.CompressFilesInDirTarballPath = "/tmp/compressed-compiled-package" pkg, pkgDeps = getCompileArgs() }) It("returns blob id and sha1 of created compiled package", func() { blobstore.CreateBlobID = "fake-blob-id" blobstore.CreateFingerprint = "fake-blob-sha1" blobID, sha1, err := compiler.Compile(pkg, pkgDeps) Expect(err).ToNot(HaveOccurred()) Expect(blobID).To(Equal("fake-blob-id")) Expect(sha1).To(Equal("fake-blob-sha1")) }) It("cleans up all packages before applying dependent packages", func() { _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).ToNot(HaveOccurred()) Expect(packageApplier.ActionsCalled).To(Equal([]string{"KeepOnly", "Apply", "Apply"})) Expect(packageApplier.KeptOnlyPackages).To(BeEmpty()) }) It("returns an error if cleaning up packages fails", func() { packageApplier.KeepOnlyErr = errors.New("fake-keep-only-error") _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-keep-only-error")) }) It("fetches source package from blobstore", func() { _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).ToNot(HaveOccurred()) Expect(blobstore.GetBlobIDs[0]).To(Equal("blobstore_id")) Expect(blobstore.GetFingerprints[0]).To(Equal("sha1")) }) It("returns an error if removing compile target directory during uncompression fails", func() { fs.RegisterRemoveAllError("/fake-compile-dir/pkg_name", errors.New("fake-remove-error")) _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-remove-error")) }) It("returns an error if creating compile target directory during uncompression fails", func() { fs.RegisterMkdirAllError("/fake-compile-dir/pkg_name", errors.New("fake-mkdir-error")) _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-mkdir-error")) }) It("returns an error if removing temporary compile target directory during uncompression fails", func() { fs.RegisterRemoveAllError("/fake-compile-dir/pkg_name-bosh-agent-unpack", errors.New("fake-remove-error")) _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-remove-error")) }) It("returns an error if creating temporary compile target directory during uncompression fails", func() { fs.RegisterMkdirAllError("/fake-compile-dir/pkg_name-bosh-agent-unpack", errors.New("fake-mkdir-error")) _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-mkdir-error")) }) It("installs dependent packages", func() { _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).ToNot(HaveOccurred()) Expect(packageApplier.AppliedPackages).To(Equal(pkgDeps)) }) It("extracts source package to compile dir", func() { _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).ToNot(HaveOccurred()) Expect(fs.FileExists("/fake-compile-dir/pkg_name")).To(BeTrue()) Expect(compressor.DecompressFileToDirDirs[0]).To(Equal("/fake-compile-dir/pkg_name-bosh-agent-unpack")) Expect(compressor.DecompressFileToDirTarballPaths[0]).To(Equal(blobstore.GetFileName)) Expect(fs.RenameOldPaths[0]).To(Equal("/fake-compile-dir/pkg_name-bosh-agent-unpack")) Expect(fs.RenameNewPaths[0]).To(Equal("/fake-compile-dir/pkg_name")) }) It("installs, enables and later cleans up bundle", func() { _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).ToNot(HaveOccurred()) Expect(bundle.ActionsCalled).To(Equal([]string{ "InstallWithoutContents", "Enable", "Disable", "Uninstall", })) }) It("runs packaging script when packaging script exists", func() { compressor.DecompressFileToDirCallBack = func() { fs.WriteFileString("/fake-compile-dir/pkg_name/packaging", "hi") } _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).ToNot(HaveOccurred()) expectedCmd := boshsys.Command{ Name: "bash", Args: []string{"-x", "packaging"}, Env: map[string]string{ "BOSH_COMPILE_TARGET": "/fake-compile-dir/pkg_name", "BOSH_INSTALL_TARGET": "/fake-dir/packages/pkg_name", "BOSH_PACKAGE_NAME": "pkg_name", "BOSH_PACKAGE_VERSION": "pkg_version", }, WorkingDir: "/fake-compile-dir/pkg_name", } Expect(len(runner.RunComplexCommands)).To(Equal(1)) Expect(runner.RunComplexCommands[0]).To(Equal(expectedCmd)) }) It("does not run packaging script when script does not exist", func() { _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).ToNot(HaveOccurred()) Expect(runner.RunCommands).To(BeEmpty()) }) It("compresses compiled package", func() { _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).ToNot(HaveOccurred()) Expect(compressor.CompressFilesInDirDir).To(Equal("/fake-dir/data/packages/pkg_name/pkg_version")) }) It("uploads compressed package to blobstore", func() { compressor.CompressFilesInDirTarballPath = "/tmp/compressed-compiled-package" _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).ToNot(HaveOccurred()) Expect(blobstore.CreateFileName).To(Equal("/tmp/compressed-compiled-package")) }) It("returs error if uploading compressed package fails", func() { blobstore.CreateErr = errors.New("fake-create-err") _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).To(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("fake-create-err")) }) It("cleans up compressed package after uploading it to blobstore", func() { var beforeCleanUpTarballPath, afterCleanUpTarballPath string blobstore.CreateCallBack = func() { beforeCleanUpTarballPath = compressor.CleanUpTarballPath } _, _, err := compiler.Compile(pkg, pkgDeps) Expect(err).ToNot(HaveOccurred()) // Compressed package is not cleaned up before blobstore upload Expect(beforeCleanUpTarballPath).To(Equal("")) // Deleted after it was uploaded afterCleanUpTarballPath = compressor.CleanUpTarballPath Expect(afterCleanUpTarballPath).To(Equal("/tmp/compressed-compiled-package")) }) }) }) }
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"), })) }) }) }) }