func captureStdout(buf *gbytes.Buffer) *os.File {
	stdout := os.Stdout
	r, w, err := os.Pipe()
	Expect(err).NotTo(HaveOccurred())
	os.Stdout = w
	go func() {
		_, err = io.Copy(buf, r)
		buf.Close()
		r.Close()
	}()
	Expect(err).NotTo(HaveOccurred())
	return stdout
}
예제 #2
0
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
)

var _ = Describe("Metricsreporter", func() {
	var buffer *gbytes.Buffer
	var reporter *metricsreporter.MetricsReporter

	BeforeEach(func() {
		buffer = gbytes.NewBuffer()
		reporter = metricsreporter.New(time.Millisecond, buffer)
		go reporter.Start()
	})

	AfterEach(func() {
		buffer.Close()
		reporter.Stop()
	})

	It("should increment sent counter", func() {
		reporter.IncrementSentMessages()
		reporter.IncrementSentMessages()
		Eventually(buffer).Should(gbytes.Say("2, 0"))
	})

	It("should increment received counter", func() {
		reporter.IncrementReceivedMessages()
		Eventually(buffer).Should(gbytes.Say("0, 1"))
	})

	It("should report metric after reportTime is up", func() {
			err = os.MkdirAll(sourceDir, 0755)
			Ω(err).ShouldNot(HaveOccurred())

			request = out.OutRequest{
				Source: s3resource.Source{
					Bucket: "bucket-name",
				},
			}

			s3client = &fakes.FakeS3Client{}
			stderr = gbytes.NewBuffer()
			command = out.NewOutCommand(stderr, s3client)
		})

		AfterEach(func() {
			stderr.Close()
			err := os.RemoveAll(tmpPath)
			Ω(err).ShouldNot(HaveOccurred())
		})

		createFile := func(path string) {
			fullPath := filepath.Join(sourceDir, path)
			err := os.MkdirAll(filepath.Dir(fullPath), 0755)
			Ω(err).ShouldNot(HaveOccurred())

			file, err := os.Create(fullPath)
			Ω(err).ShouldNot(HaveOccurred())
			file.Close()
		}

		Describe("finding files to upload with From param", func() {