func createBlobManager() (blobManager BlobManager, fs *fakesys.FakeFileSystem) {
	fs = fakesys.NewFakeFileSystem()
	dirProvider := boshdir.NewDirectoriesProvider("/var/vcap")

	blobManager = NewBlobManager(fs, dirProvider)
	return
}
func TestConfigure(t *testing.T) {
	jobsBc, _, _, monitor, applier := buildJobApplier()
	job := buildJob()

	fs := fakesys.NewFakeFileSystem()
	fs.WriteToFile("/path/to/job/monit", "some conf")
	fs.GlobPaths = []string{"/path/to/job/subjob.monit"}

	jobsBc.GetDirPath = "/path/to/job"
	jobsBc.GetDirFs = fs

	err := applier.Configure(job, 0)
	assert.NoError(t, err)

	assert.Equal(t, "/path/to/job/*.monit", fs.GlobPattern)
	assert.Equal(t, 2, len(monitor.AddJobArgs))

	firstArgs := fakemon.AddJobArgs{
		Name:       job.Name,
		Index:      0,
		ConfigPath: "/path/to/job/monit",
	}

	secondArgs := fakemon.AddJobArgs{
		Name:       job.Name + "_subjob",
		Index:      0,
		ConfigPath: "/path/to/job/subjob.monit",
	}
	assert.Equal(t, firstArgs, monitor.AddJobArgs[0])
	assert.Equal(t, secondArgs, monitor.AddJobArgs[1])
}
func TestApplyCopiesFromDecompressedTmpPathToInstallPath(t *testing.T) {
	jobsBc, blobstore, compressor, _, applier := buildJobApplier()
	job := buildJob()
	job.Source.PathInArchive = "fake-path-in-archive"

	fs := fakesys.NewFakeFileSystem()
	fs.TempDirDir = "fake-tmp-dir"
	fs.MkdirAll("fake-tmp-dir", os.FileMode(0))

	jobsBc.InstallFs = fs
	jobsBc.InstallPath = "fake-install-dir"
	fs.MkdirAll("fake-install-dir", os.FileMode(0))

	blobstore.GetFileName = "/dev/null"

	compressor.DecompressFileToDirCallBack = func() {
		fs.MkdirAll("fake-tmp-dir/fake-path-in-archive", os.FileMode(0))
		fs.WriteToFile("fake-tmp-dir/fake-path-in-archive/file", "file-contents")
	}

	err := applier.Apply(job)
	assert.NoError(t, err)
	fileInArchiveStat := fs.GetFileTestStat("fake-install-dir/file")
	assert.NotNil(t, fileInArchiveStat)
	assert.Equal(t, "file-contents", fileInArchiveStat.Content)
}
Esempio n. 4
0
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
}
Esempio n. 5
0
func getNewProvider() (provider provider) {
	dirProvider := boshsys.NewDirectoriesProvider("/var/vcap")
	fs := fakefs.NewFakeFileSystem()

	provider = NewProvider(boshlog.NewLogger(boshlog.LEVEL_NONE), fs, dirProvider)
	return
}
func init() {
	Describe("dummyInfrastructure", func() {
		It("get settings", func() {
			fs := fakefs.NewFakeFileSystem()
			dirProvider := boshdir.NewDirectoriesProvider("/var/vcap")
			platform := fakeplatform.NewFakePlatform()
			fakeDevicePathResolver := fakedpresolv.NewFakeDevicePathResolver(1*time.Millisecond, platform.GetFs())

			settingsPath := filepath.Join(dirProvider.BoshDir(), "dummy-cpi-agent-env.json")

			expectedSettings := boshsettings.Settings{
				AgentID: "123-456-789",
				Blobstore: boshsettings.Blobstore{
					Type: boshsettings.BlobstoreTypeDummy,
				},
				Mbus: "nats://127.0.0.1:4222",
			}
			existingSettingsBytes, _ := json.Marshal(expectedSettings)
			fs.WriteFile(settingsPath, existingSettingsBytes)

			dummy := NewDummyInfrastructure(fs, dirProvider, platform, fakeDevicePathResolver)

			settings, err := dummy.GetSettings()
			Expect(err).ToNot(HaveOccurred())
			assert.Equal(GinkgoT(), settings, boshsettings.Settings{
				AgentID:   "123-456-789",
				Blobstore: boshsettings.Blobstore{Type: boshsettings.BlobstoreTypeDummy},
				Mbus:      "nats://127.0.0.1:4222",
			})
		})

		It("get settings errs when settings file does not exist", func() {
			fs := fakefs.NewFakeFileSystem()
			dirProvider := boshdir.NewDirectoriesProvider("/var/vcap")
			platform := fakeplatform.NewFakePlatform()
			fakeDevicePathResolver := fakedpresolv.NewFakeDevicePathResolver(1*time.Millisecond, platform.GetFs())

			dummy := NewDummyInfrastructure(fs, dirProvider, platform, fakeDevicePathResolver)

			_, err := dummy.GetSettings()
			Expect(err).To(HaveOccurred())
			Expect(err.Error()).To(ContainSubstring("Read settings file"))
		})
	})
}
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 TestGetSettingsErrsWhenSettingsFileDoesNotExist(t *testing.T) {
	fs := fakefs.NewFakeFileSystem()
	dirProvider := boshdir.NewDirectoriesProvider("/var/vcap")

	dummy := newDummyInfrastructure(fs, dirProvider)

	_, err := dummy.GetSettings()
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "Read settings file")
}
Esempio n. 9
0
func buildService(NTPData string) (service Service) {
	fs := fakefs.NewFakeFileSystem()
	dirProvider := boshdir.NewDirectoriesProvider("/var/vcap")

	if NTPData != "" {
		fs.WriteToFile("/var/vcap/bosh/log/ntpdate.out", NTPData)
	}

	service = NewConcreteService(fs, dirProvider)
	return
}
func TestApplyErrsWhenJobDecompressErrs(t *testing.T) {
	jobsBc, _, compressor, applier := buildJobApplier()
	pkg := buildJob()

	compressor.DecompressFileToDirError = errors.New("fake-decompress-error")

	fs := fakesys.NewFakeFileSystem()
	jobsBc.InstallFs = fs

	err := applier.Apply(pkg)
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "fake-decompress-error")
}
Esempio n. 11
0
func init() {
	Describe("Testing with Ginkgo", func() {
		It("get settings", func() {

			fs := fakefs.NewFakeFileSystem()
			dirProvider := boshdir.NewDirectoriesProvider("/var/vcap")
			platform := fakeplatform.NewFakePlatform()

			settingsPath := filepath.Join(dirProvider.BaseDir(), "bosh", "settings.json")

			expectedSettings := boshsettings.Settings{AgentId: "123-456-789", Blobstore: boshsettings.Blobstore{Type: boshsettings.BlobstoreTypeDummy}, Mbus: "nats://127.0.0.1:4222"}
			existingSettingsBytes, _ := json.Marshal(expectedSettings)
			fs.WriteToFile(settingsPath, string(existingSettingsBytes))

			dummy := NewDummyInfrastructure(fs, dirProvider, platform)

			settings, err := dummy.GetSettings()
			assert.NoError(GinkgoT(), err)

			assert.Equal(GinkgoT(), settings, boshsettings.Settings{
				AgentId:   "123-456-789",
				Blobstore: boshsettings.Blobstore{Type: boshsettings.BlobstoreTypeDummy},
				Mbus:      "nats://127.0.0.1:4222",
			})
		})
		It("get settings errs when settings file does not exist", func() {

			fs := fakefs.NewFakeFileSystem()
			dirProvider := boshdir.NewDirectoriesProvider("/var/vcap")
			platform := fakeplatform.NewFakePlatform()

			dummy := NewDummyInfrastructure(fs, dirProvider, platform)

			_, err := dummy.GetSettings()
			assert.Error(GinkgoT(), err)
			assert.Contains(GinkgoT(), err.Error(), "Read settings file")
		})
	})
}
func TestApplyInstallsAndEnablesJob(t *testing.T) {
	jobsBc, _, _, applier := buildJobApplier()
	pkg := buildJob()

	fs := fakesys.NewFakeFileSystem()
	jobsBc.InstallFs = fs
	jobsBc.InstallPath = "fake-install-dir"
	fs.MkdirAll("fake-install-dir", os.FileMode(0))

	err := applier.Apply(pkg)
	assert.NoError(t, err)
	assert.True(t, jobsBc.IsInstalled(pkg))
	assert.True(t, jobsBc.IsEnabled(pkg))
}
func TestApplyErrsWhenTempDirErrs(t *testing.T) {
	jobsBc, blobstore, _, _, applier := buildJobApplier()
	job := buildJob()

	fs := fakesys.NewFakeFileSystem()
	fs.TempDirError = errors.New("fake-filesystem-tempdir-error")
	jobsBc.InstallFs = fs

	blobstore.GetFileName = "/dev/null"

	err := applier.Apply(job)
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "fake-filesystem-tempdir-error")
}
Esempio n. 14
0
func TestApplyInstallsAndEnablesJob(t *testing.T) {
	jobsBc, _, _, _, applier := buildJobApplier()
	job, bundle := buildJob(jobsBc)

	fs := fakesys.NewFakeFileSystem()
	bundle.InstallFs = fs
	bundle.InstallPath = "fake-install-dir"
	fs.MkdirAll("fake-install-dir", os.FileMode(0))

	err := applier.Apply(job)
	assert.NoError(t, err)
	assert.True(t, bundle.Installed)
	assert.True(t, bundle.Enabled)
}
Esempio n. 15
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
}
Esempio n. 16
0
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
}
Esempio n. 17
0
func startServer() (serverURL string, handler HttpsHandler, fs *fakesys.FakeFileSystem) {
	serverURL = "https://*****:*****@127.0.0.1:6900"
	mbusUrl, _ := url.Parse(serverURL)
	logger := boshlog.NewLogger(boshlog.LEVEL_NONE)
	fs = fakesys.NewFakeFileSystem()
	dirProvider := boshdir.NewDirectoriesProvider("/var/vcap")
	handler = NewHttpsHandler(mbusUrl, logger, fs, dirProvider)

	go handler.Start(func(req boshhandler.Request) (resp boshhandler.Response) {
		receivedRequest = req
		return boshhandler.NewValueResponse("expected value")
	})
	return
}
func TestApplyErrsWhenJobEnableFails(t *testing.T) {
	jobsBc, _, _, applier := buildJobApplier()
	pkg := buildJob()

	fs := fakesys.NewFakeFileSystem()
	jobsBc.InstallFs = fs
	jobsBc.InstallPath = "fake-install-dir"
	fs.MkdirAll("fake-install-dir", os.FileMode(0))

	jobsBc.EnableError = errors.New("fake-enable-error")

	err := applier.Apply(pkg)
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "fake-enable-error")
}
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 TestApplyErrsWhenCopyAllErrs(t *testing.T) {
	jobsBc, blobstore, _, _, applier := buildJobApplier()
	job := buildJob()

	fs := fakesys.NewFakeFileSystem()
	fs.TempDirDir = "fake-tmp-dir"
	fs.CopyDirEntriesError = errors.New("fake-copy-dir-entries-error")

	jobsBc.InstallFs = fs

	blobstore.GetFileName = "/dev/null"

	err := applier.Apply(job)
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "fake-copy-dir-entries-error")
}
Esempio n. 21
0
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
}
Esempio n. 22
0
func NewFakePlatform() (platform *FakePlatform) {
	platform = new(FakePlatform)
	platform.Fs = fakesys.NewFakeFileSystem()
	platform.Runner = &fakesys.FakeCmdRunner{}
	platform.FakeStatsCollector = &fakestats.FakeStatsCollector{}
	platform.FakeCompressor = fakecmd.NewFakeCompressor()
	platform.FakeCopier = fakecmd.NewFakeCopier()
	platform.FakeVitalsService = fakevitals.NewFakeService()
	platform.FakeDiskManager = fakedisk.NewFakeDiskManager()

	platform.DevicePathResolver = boshdevicepathresolver.NewFakeDevicePathResolver(1*time.Millisecond, platform.Fs)

	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 TestApplyErrsWhenTempDirErrs(t *testing.T) {
	jobsBc, blobstore, _, applier := buildJobApplier()
	pkg := buildJob()

	file, err := os.Open("/dev/null")
	assert.NoError(t, err)
	defer file.Close()

	fs := fakesys.NewFakeFileSystem()
	fs.TempDirError = errors.New("fake-filesystem-tempdir-error")
	jobsBc.InstallFs = fs

	blobstore.GetFile = file

	err = applier.Apply(pkg)
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "fake-filesystem-tempdir-error")
}
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 TestApplyDownloadsAndCleansUpJob(t *testing.T) {
	jobsBc, blobstore, _, _, applier := buildJobApplier()
	job := buildJob()
	job.Source.BlobstoreId = "fake-blobstore-id"
	job.Source.Sha1 = "blob-sha1"

	fs := fakesys.NewFakeFileSystem()
	jobsBc.InstallFs = fs
	jobsBc.InstallPath = "fake-install-dir"
	fs.MkdirAll("fake-install-dir", os.FileMode(0))

	blobstore.GetFileName = "/dev/null"

	err := applier.Apply(job)
	assert.NoError(t, err)
	assert.Equal(t, "fake-blobstore-id", blobstore.GetBlobIds[0])
	assert.Equal(t, "blob-sha1", blobstore.GetFingerprints[0])
	assert.Equal(t, blobstore.GetFileName, blobstore.CleanUpFileName)
}
func TestApplyDecompressesJobToTmpPathAndCleansItUp(t *testing.T) {
	jobsBc, blobstore, compressor, _, applier := buildJobApplier()
	job := buildJob()

	fs := fakesys.NewFakeFileSystem()
	fs.TempDirDir = "fake-tmp-dir"
	fs.MkdirAll("fake-tmp-dir", os.FileMode(0))

	jobsBc.InstallFs = fs
	jobsBc.InstallPath = "fake-install-dir"
	fs.MkdirAll("fake-install-dir", os.FileMode(0))

	blobstore.GetFileName = "/dev/null"

	err := applier.Apply(job)
	assert.NoError(t, err)
	assert.Equal(t, blobstore.GetFileName, compressor.DecompressFileToDirTarballPaths[0])
	assert.Equal(t, "fake-tmp-dir", compressor.DecompressFileToDirDirs[0])
	assert.Nil(t, fs.GetFileTestStat(fs.TempDirDir))
}
func TestApplyDownloadsAndCleansUpJob(t *testing.T) {
	jobsBc, blobstore, _, applier := buildJobApplier()
	pkg := buildJob()
	pkg.Source.BlobstoreId = "fake-blobstore-id"

	file, err := os.Open("/dev/null")
	assert.NoError(t, err)
	defer file.Close()

	fs := fakesys.NewFakeFileSystem()
	jobsBc.InstallFs = fs
	jobsBc.InstallPath = "fake-install-dir"
	fs.MkdirAll("fake-install-dir", os.FileMode(0))

	blobstore.GetFile = file

	err = applier.Apply(pkg)
	assert.NoError(t, err)
	assert.Equal(t, "fake-blobstore-id", blobstore.GetBlobId)
	assert.Equal(t, file, blobstore.CleanUpFile)
}
func TestGetSettings(t *testing.T) {
	fs := fakefs.NewFakeFileSystem()
	dirProvider := boshdir.NewDirectoriesProvider("/var/vcap")

	settingsPath := filepath.Join(dirProvider.BaseDir(), "bosh", "settings.json")

	expectedSettings := boshsettings.Settings{AgentId: "123-456-789", Blobstore: boshsettings.Blobstore{Type: boshsettings.BlobstoreTypeDummy}, Mbus: "nats://127.0.0.1:4222"}
	existingSettingsBytes, _ := json.Marshal(expectedSettings)
	fs.WriteToFile(settingsPath, string(existingSettingsBytes))

	dummy := newDummyInfrastructure(fs, dirProvider)

	settings, err := dummy.GetSettings()
	assert.NoError(t, err)

	assert.Equal(t, settings, boshsettings.Settings{
		AgentId:   "123-456-789",
		Blobstore: boshsettings.Blobstore{Type: boshsettings.BlobstoreTypeDummy},
		Mbus:      "nats://127.0.0.1:4222",
	})
}
func TestApplySetsExecutableBitForFilesInBin(t *testing.T) {
	jobsBc, blobstore, compressor, _, applier := buildJobApplier()
	job := buildJob()
	job.Source.PathInArchive = "fake-path-in-archive"

	blobstore.GetFileName = "/dev/null"

	fs := fakesys.NewFakeFileSystem()
	fs.TempDirDir = "fake-tmp-dir"

	jobsBc.InstallFs = fs
	jobsBc.InstallPath = "fake-install-dir"

	compressor.DecompressFileToDirCallBack = func() {
		fs.WriteToFile("fake-tmp-dir/fake-path-in-archive/bin/test1", "")
		fs.WriteToFile("fake-tmp-dir/fake-path-in-archive/bin/test2", "")
		fs.WriteToFile("fake-tmp-dir/fake-path-in-archive/config/test", "")
	}

	fs.GlobPaths = []string{"fake-install-dir/bin/test1", "fake-install-dir/bin/test2"}

	err := applier.Apply(job)
	assert.NoError(t, err)

	assert.Equal(t, "fake-install-dir/bin/*", fs.GlobPattern)

	testBin1Stats := fs.GetFileTestStat("fake-install-dir/bin/test1")
	assert.NotNil(t, testBin1Stats)
	assert.Equal(t, 0755, int(testBin1Stats.FileMode))

	testBin2Stats := fs.GetFileTestStat("fake-install-dir/bin/test2")
	assert.NotNil(t, testBin2Stats)
	assert.Equal(t, 0755, int(testBin2Stats.FileMode))

	testConfigStats := fs.GetFileTestStat("fake-install-dir/config/test")
	assert.NotNil(t, testConfigStats)
	assert.NotEqual(t, 0755, int(testConfigStats.FileMode))
}
func TestApplyDecompressesJobToTmpPath(t *testing.T) {
	jobsBc, blobstore, compressor, applier := buildJobApplier()
	pkg := buildJob()

	file, err := os.Open("/dev/null")
	assert.NoError(t, err)
	defer file.Close()

	fs := fakesys.NewFakeFileSystem()
	fs.TempDirDir = "fake-tmp-dir"
	fs.MkdirAll("fake-tmp-dir", os.FileMode(0))

	jobsBc.InstallFs = fs
	jobsBc.InstallPath = "fake-install-dir"
	fs.MkdirAll("fake-install-dir", os.FileMode(0))

	blobstore.GetFile = file

	err = applier.Apply(pkg)
	assert.NoError(t, err)
	assert.Equal(t, file, compressor.DecompressFileToDirTarball)
	assert.Equal(t, "fake-tmp-dir", compressor.DecompressFileToDirDir)
}