Esempio n. 1
0
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"))
		})
	})
}
Esempio n. 3
0
	fakeudev "github.com/cloudfoundry/bosh-agent/platform/cdrom/udevdevice/fakes"
	fakesys "github.com/cloudfoundry/bosh-agent/system/fakes"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("LinuxCdrom", func() {
	var (
		udev   *fakeudev.FakeUdevDevice
		runner *fakesys.FakeCmdRunner
		cd     Cdrom
	)

	BeforeEach(func() {
		udev = fakeudev.NewFakeUdevDevice()
		runner = fakesys.NewFakeCmdRunner()
	})

	JustBeforeEach(func() {
		cd = NewLinuxCdrom("/dev/sr0", udev, runner)
	})

	Describe("WaitForMedia", func() {
		It("polls the cdrom to force udev to notice it", func() {
			err := cd.WaitForMedia()
			Expect(err).NotTo(HaveOccurred())
			Expect(udev.KickDeviceFile).To(Equal("/dev/sr0"))
		})

		It("waits for udev to settle outstanding kernel events", func() {
			err := cd.WaitForMedia()
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("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"}))
		})
	})
}
import (
	. "github.com/cloudfoundry/bosh-agent/platform/cdrom/udevdevice"
	fakes "github.com/cloudfoundry/bosh-agent/system/fakes"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("ConcreteUdevDevice", func() {
	var (
		cmdRunner *fakes.FakeCmdRunner
		udev      ConcreteUdevDevice
	)

	BeforeEach(func() {
		cmdRunner = fakes.NewFakeCmdRunner()
	})

	JustBeforeEach(func() {
		udev = NewConcreteUdevDevice(cmdRunner)
	})

	Describe("#Settle", func() {
		Context("if `udevadm` is a runnable command", func() {
			BeforeEach(func() {
				cmdRunner.AvailableCommands["udevadm"] = true
			})

			It("runs `udevadm settle`", func() {
				err := udev.Settle()
				Expect(err).NotTo(HaveOccurred())