Example #1
0
func deleteFiles(fs boshsys.FileSystem, path string, filenamePrefix string) (int, error) {
	var deletedFilesCount int
	files, err := fs.Glob(fmt.Sprintf("%s%s*", path, filenamePrefix))
	if err != nil {
		return deletedFilesCount, bosherr.WrapError(err, "Glob command failed")
	}
	for _, file := range files {
		err = fs.RemoveAll(file)
		if err != nil {
			return deletedFilesCount, bosherr.WrapErrorf(err, "deleting %s failed", file)
		}
		deletedFilesCount++
	}
	return deletedFilesCount, err
}
	BeforeEach(func() {
		logger := boshlog.NewLogger(boshlog.LevelNone)
		cmdRunner = boshsys.NewExecCmdRunner(logger)
		fs = boshsys.NewOsFileSystem(logger)
		tmpDir, err := fs.TempDir("tarballCompressor-test")
		Expect(err).NotTo(HaveOccurred())
		dstDir = filepath.Join(tmpDir, "TestCompressor")
		compressor = NewTarballCompressor(cmdRunner, fs)
	})

	BeforeEach(func() {
		fs.MkdirAll(dstDir, os.ModePerm)
	})

	AfterEach(func() {
		fs.RemoveAll(dstDir)
	})

	Describe("CompressFilesInDir", func() {
		It("compresses the files in the given directory", func() {
			srcDir := fixtureSrcDir()
			tgzName, err := compressor.CompressFilesInDir(srcDir)
			Expect(err).ToNot(HaveOccurred())
			defer os.Remove(tgzName)

			tarballContents, _, _, err := cmdRunner.RunCommand("tar", "-tf", tgzName)
			Expect(err).ToNot(HaveOccurred())

			contentElements := strings.Split(strings.TrimSpace(tarballContents), "\n")

			Expect(contentElements).To(ConsistOf(
Example #3
0
		index         FileIndex
	)

	BeforeEach(func() {
		logger := boshlog.NewLogger(boshlog.LevelNone)
		fs = boshsys.NewOsFileSystem(logger)

		file, err := fs.TempFile("file-index")
		Expect(err).ToNot(HaveOccurred())

		indexFilePath = file.Name()

		err = file.Close()
		Expect(err).ToNot(HaveOccurred())

		err = fs.RemoveAll(indexFilePath)
		Expect(err).ToNot(HaveOccurred())

		index = NewFileIndex(indexFilePath, fs)
	})

	AfterEach(func() {
		err := fs.RemoveAll(indexFilePath)
		Expect(err).ToNot(HaveOccurred())
	})

	Describe("Save/Find", func() {
		It("returns true if item is found by key", func() {
			k1 := Key{Key: "key-1"}
			v1 := Value{Name: "value-1", Count: 1}
			err := index.Save(k1, v1)
				Expect(err).ToNot(HaveOccurred())
				renderedJob1 := NewRenderedJob(bireljob.Job{Name: "fake-job-name-1"}, renderedJobDir1, fs, logger)
				defer func() { err := renderedJob1.Delete(); Expect(err).ToNot(HaveOccurred()) }()
				err = fs.WriteFileString(filepath.Join(renderedJobDir1, "script-0"), "fake-rendered-job-1-script-0-content")
				Expect(err).ToNot(HaveOccurred())
				renderedJobList.Add(renderedJob1)

				// compress
				archive, err := renderedJobListCompressor.Compress(renderedJobList)
				Expect(err).ToNot(HaveOccurred())
				defer func() { err := archive.Delete(); Expect(err).ToNot(HaveOccurred()) }()

				// decompress
				renderedJobListDir, err := fs.TempDir("RenderedJobListCompressorTest")
				Expect(err).ToNot(HaveOccurred())
				defer func() { err := fs.RemoveAll(renderedJobListDir); Expect(err).ToNot(HaveOccurred()) }()
				err = compressor.DecompressFileToDir(archive.Path(), renderedJobListDir, boshcmd.CompressorOptions{})
				Expect(err).ToNot(HaveOccurred())

				// verify that archive contained rendered scripts from job 0
				content, err := fs.ReadFileString(filepath.Join(renderedJobListDir, "fake-job-name-0", "script-0"))
				Expect(err).ToNot(HaveOccurred())
				Expect(content).To(Equal("fake-rendered-job-0-script-0-content"))
				content, err = fs.ReadFileString(filepath.Join(renderedJobListDir, "fake-job-name-0", "script-1"))
				Expect(err).ToNot(HaveOccurred())
				Expect(content).To(Equal("fake-rendered-job-0-script-1-content"))

				// verify that archive contained rendered scripts from job 1
				content, err = fs.ReadFileString(filepath.Join(renderedJobListDir, "fake-job-name-1", "script-0"))
				Expect(err).ToNot(HaveOccurred())
				Expect(content).To(Equal("fake-rendered-job-1-script-0-content"))