Exemplo n.º 1
0
			Expect(runScriptResult.Tag).To(Equal("my-tag"))
			Expect(runScriptResult.Error).To(BeNil())
		})

		It("returns an error if it fails to create logs directory", func() {
			fs.MkdirAllError = errors.New("fake-mkdir-all-error")

			runScriptResult := genericScript.Run()

			Expect(runScriptResult.Tag).To(Equal("my-tag"))
			Expect(runScriptResult.Error.Error()).To(Equal("fake-mkdir-all-error"))
		})

		It("returns an error if it fails to open stdout/stderr log file", func() {
			fs.OpenFileErr = errors.New("fake-open-file-error")

			runScriptResult := genericScript.Run()

			Expect(runScriptResult.Tag).To(Equal("my-tag"))
			Expect(runScriptResult.Error.Error()).To(Equal("fake-open-file-error"))
		})

		Context("when command succeeds", func() {

			BeforeEach(func() {
				cmdRunner.AddCmdResult("/path-to-script", fakesys.FakeCmdResult{
					Stdout:     "fake-stdout",
					Stderr:     "fake-stderr",
					ExitStatus: 0,
					Error:      nil,
			It("returns a 404", func() {
				postBody := `{"method":"ping","arguments":["foo","bar"], "reply_to": "reply to me!"}`
				postPayload := strings.NewReader(postBody)

				httpResponse, err := httpClient.Post(serverURL+"/blobs/123", "application/json", postPayload)
				Expect(err).ToNot(HaveOccurred())

				defer httpResponse.Body.Close()

				Expect(httpResponse.StatusCode).To(Equal(404))
			})
		})

		Context("when file does not exist", func() {
			It("returns a 404", func() {
				fs.OpenFileErr = errors.New("no such file or directory")
				httpResponse, err := httpClient.Get(serverURL + "/blobs/123")
				Expect(err).ToNot(HaveOccurred())

				defer httpResponse.Body.Close()
				Expect(httpResponse.StatusCode).To(Equal(404))
			})
		})

		Context("when file does not have correct permissions", func() {
			It("returns a 500", func() {
				fs.OpenFileErr = errors.New("permission denied")
				httpResponse, err := httpClient.Get(serverURL + "/blobs/123")
				Expect(err).ToNot(HaveOccurred())

				defer httpResponse.Body.Close()
Exemplo n.º 3
0
		logger.Error("TAG", "some %s info to log", "awesome")

		contents, err := fs.ReadFileString(logFile.Name())
		Expect(err).ToNot(HaveOccurred())

		expectedContent := expectedLogFormat("TAG", "ERROR - some awesome info to log")
		Expect(contents).To(MatchRegexp(expectedContent))
	})

	It("does not log at NONE level", func() {
		logger, logFile, err := New(boshlog.LevelNone, logFile.Name(), DefaultLogFileMode, fs)
		Expect(err).ToNot(HaveOccurred())

		logger.Error("TAG", "some %s info to log", "awesome")

		contents, err := fs.ReadFileString(logFile.Name())
		Expect(err).ToNot(HaveOccurred())

		Expect(contents).To(Equal(""))
	})

	It("errors if the file cannot be openned for writing", func() {
		fs.OpenFileErr = errors.New("fake-fs-error")

		_, _, err := New(boshlog.LevelNone, logFile.Name(), DefaultLogFileMode, fs)
		Expect(err).To(HaveOccurred())
		Expect(err.Error()).To(ContainSubstring("fake-fs-error"))
	})
})