Ejemplo n.º 1
0
func TestStartServiceRetriesWhenConnectionRefused(t *testing.T) {
	fakeHttpClient := http_fakes.NewFakeHttpClient()
	fakeHttpClient.SetNilResponse()
	fakeHttpClient.Error = errors.New("some error")

	client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond)

	err := client.StartService("test-service")
	assert.Equal(t, fakeHttpClient.CallCount, 20)
	assert.Error(t, err)
}
Ejemplo n.º 2
0
func TestStartServiceRetriesWhenNon200Response(t *testing.T) {
	fakeHttpClient := http_fakes.NewFakeHttpClient()
	fakeHttpClient.StatusCode = 500
	fakeHttpClient.SetMessage("fake error message")

	client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond)

	err := client.StartService("test-service")
	assert.Equal(t, fakeHttpClient.CallCount, 20)
	assert.Error(t, err)
}
Ejemplo n.º 3
0
func TestStatusRetriesWhenConnectionRefused(t *testing.T) {
	fakeHttpClient := http_fakes.NewFakeHttpClient()
	fakeHttpClient.SetNilResponse()
	fakeHttpClient.Error = errors.New("some error")

	client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient)
	client.delayBetweenRetries = 1 * time.Millisecond

	_, err := client.Status()
	assert.Equal(t, fakeHttpClient.CallCount, 20)
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "some error")
}
Ejemplo n.º 4
0
func TestStatusRetriesWhenNon200Response(t *testing.T) {
	fakeHttpClient := http_fakes.NewFakeHttpClient()
	fakeHttpClient.StatusCode = 500
	fakeHttpClient.SetMessage("fake error message")

	client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient)
	client.delayBetweenRetries = 1 * time.Millisecond

	_, err := client.Status()
	assert.Equal(t, fakeHttpClient.CallCount, 20)
	assert.Error(t, err)
	assert.Contains(t, err.Error(), "fake error message")
}
Ejemplo n.º 5
0
func init() {
	Describe("Testing with Ginkgo", func() {
		It("services in group returns services when found", func() {
		})
		It("services in group errors when not found", func() {
		})
		It("start service", func() {

			var calledMonit bool

			handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				calledMonit = true
				assert.Equal(GinkgoT(), r.Method, "POST")
				assert.Equal(GinkgoT(), r.URL.Path, "/test-service")
				assert.Equal(GinkgoT(), r.PostFormValue("action"), "start")
				assert.Equal(GinkgoT(), r.Header.Get("Content-Type"), "application/x-www-form-urlencoded")

				expectedAuthEncoded := base64.URLEncoding.EncodeToString([]byte("fake-user:fake-pass"))
				assert.Equal(GinkgoT(), r.Header.Get("Authorization"), fmt.Sprintf("Basic %s", expectedAuthEncoded))
			})
			ts := httptest.NewServer(handler)
			defer ts.Close()

			client := NewHttpClient(ts.Listener.Addr().String(), "fake-user", "fake-pass", http.DefaultClient, 1*time.Millisecond)

			err := client.StartService("test-service")
			assert.NoError(GinkgoT(), err)
			assert.True(GinkgoT(), calledMonit)
		})
		It("start service retries when non200 response", func() {

			fakeHttpClient := http_fakes.NewFakeHttpClient()
			fakeHttpClient.StatusCode = 500
			fakeHttpClient.SetMessage("fake error message")

			client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond)

			err := client.StartService("test-service")
			assert.Equal(GinkgoT(), fakeHttpClient.CallCount, 20)
			assert.Error(GinkgoT(), err)
		})
		It("start service retries when connection refused", func() {

			fakeHttpClient := http_fakes.NewFakeHttpClient()
			fakeHttpClient.SetNilResponse()
			fakeHttpClient.Error = errors.New("some error")

			client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond)

			err := client.StartService("test-service")
			assert.Equal(GinkgoT(), fakeHttpClient.CallCount, 20)
			assert.Error(GinkgoT(), err)
		})
		It("stop service", func() {

			var calledMonit bool

			handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				calledMonit = true
				assert.Equal(GinkgoT(), r.Method, "POST")
				assert.Equal(GinkgoT(), r.URL.Path, "/test-service")
				assert.Equal(GinkgoT(), r.PostFormValue("action"), "stop")
				assert.Equal(GinkgoT(), r.Header.Get("Content-Type"), "application/x-www-form-urlencoded")

				expectedAuthEncoded := base64.URLEncoding.EncodeToString([]byte("fake-user:fake-pass"))
				assert.Equal(GinkgoT(), r.Header.Get("Authorization"), fmt.Sprintf("Basic %s", expectedAuthEncoded))
			})
			ts := httptest.NewServer(handler)
			defer ts.Close()

			client := NewHttpClient(ts.Listener.Addr().String(), "fake-user", "fake-pass", http.DefaultClient, 1*time.Millisecond)

			err := client.StopService("test-service")
			assert.NoError(GinkgoT(), err)
			assert.True(GinkgoT(), calledMonit)
		})
		It("stop service retries when non200 response", func() {

			fakeHttpClient := http_fakes.NewFakeHttpClient()
			fakeHttpClient.StatusCode = 500
			fakeHttpClient.SetMessage("fake error message")

			client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond)

			err := client.StopService("test-service")
			assert.Equal(GinkgoT(), fakeHttpClient.CallCount, 20)
			assert.Error(GinkgoT(), err)
			assert.Contains(GinkgoT(), err.Error(), "fake error message")
		})
		It("stop service retries when connection refused", func() {

			fakeHttpClient := http_fakes.NewFakeHttpClient()
			fakeHttpClient.SetNilResponse()
			fakeHttpClient.Error = errors.New("some error")

			client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond)

			err := client.StopService("test-service")
			assert.Equal(GinkgoT(), fakeHttpClient.CallCount, 20)
			assert.Error(GinkgoT(), err)
			assert.Contains(GinkgoT(), err.Error(), "some error")
		})
		It("services in group", func() {

			monitStatusFilePath, _ := filepath.Abs("../../../../fixtures/monit_status.xml")
			assert.NotNil(GinkgoT(), monitStatusFilePath)

			file, err := os.Open(monitStatusFilePath)
			assert.NoError(GinkgoT(), err)
			defer file.Close()

			handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				io.Copy(w, file)
				assert.Equal(GinkgoT(), r.Method, "GET")
				assert.Equal(GinkgoT(), r.URL.Path, "/_status2")
				assert.Equal(GinkgoT(), r.URL.Query().Get("format"), "xml")
			})
			ts := httptest.NewServer(handler)
			defer ts.Close()

			client := NewHttpClient(ts.Listener.Addr().String(), "fake-user", "fake-pass", http.DefaultClient, 1*time.Millisecond)

			services, err := client.ServicesInGroup("vcap")
			assert.NoError(GinkgoT(), err)
			assert.Equal(GinkgoT(), []string{"dummy"}, services)
		})
		It("decode status", func() {

			monitStatusFilePath, _ := filepath.Abs("../../../../fixtures/monit_status.xml")
			assert.NotNil(GinkgoT(), monitStatusFilePath)

			file, err := os.Open(monitStatusFilePath)
			assert.NoError(GinkgoT(), err)
			defer file.Close()

			handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				io.Copy(w, file)
				assert.Equal(GinkgoT(), r.Method, "GET")
				assert.Equal(GinkgoT(), r.URL.Path, "/_status2")
				assert.Equal(GinkgoT(), r.URL.Query().Get("format"), "xml")
			})
			ts := httptest.NewServer(handler)
			defer ts.Close()

			client := NewHttpClient(ts.Listener.Addr().String(), "fake-user", "fake-pass", http.DefaultClient, 1*time.Millisecond)

			status, err := client.Status()
			assert.NoError(GinkgoT(), err)
			dummyServices := status.ServicesInGroup("vcap")
			assert.Equal(GinkgoT(), 1, len(dummyServices))
		})
		It("status retries when non200 response", func() {

			fakeHttpClient := http_fakes.NewFakeHttpClient()
			fakeHttpClient.StatusCode = 500
			fakeHttpClient.SetMessage("fake error message")

			client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond)

			_, err := client.Status()
			assert.Equal(GinkgoT(), fakeHttpClient.CallCount, 20)
			assert.Error(GinkgoT(), err)
			assert.Contains(GinkgoT(), err.Error(), "fake error message")
		})
		It("status retries when connection refused", func() {

			fakeHttpClient := http_fakes.NewFakeHttpClient()
			fakeHttpClient.SetNilResponse()
			fakeHttpClient.Error = errors.New("some error")

			client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond)

			_, err := client.Status()
			assert.Equal(GinkgoT(), fakeHttpClient.CallCount, 20)
			assert.Error(GinkgoT(), err)
			assert.Contains(GinkgoT(), err.Error(), "some error")
		})
	})
}
Ejemplo n.º 6
0
func init() {
	Describe("httpClient", func() {
		var (
			logger = boshlog.NewLogger(boshlog.LEVEL_NONE)
		)

		It("services in group returns services when found", func() {})

		It("services in group errors when not found", func() {})

		Describe("StartService", func() {
			It("start service", func() {
				var calledMonit bool

				handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
					calledMonit = true
					Expect(r.Method).To(Equal("POST"))
					Expect(r.URL.Path).To(Equal("/test-service"))
					Expect(r.PostFormValue("action")).To(Equal("start"))
					Expect(r.Header.Get("Content-Type")).To(Equal("application/x-www-form-urlencoded"))

					expectedAuthEncoded := base64.URLEncoding.EncodeToString([]byte("fake-user:fake-pass"))
					Expect(r.Header.Get("Authorization")).To(Equal(fmt.Sprintf("Basic %s", expectedAuthEncoded)))
				})
				ts := httptest.NewServer(handler)
				defer ts.Close()

				client := NewHttpClient(ts.Listener.Addr().String(), "fake-user", "fake-pass", http.DefaultClient, 1*time.Millisecond, logger)

				err := client.StartService("test-service")
				Expect(err).ToNot(HaveOccurred())
				Expect(calledMonit).To(BeTrue())
			})

			It("start service retries when non200 response", func() {
				fakeHttpClient := http_fakes.NewFakeHttpClient()
				fakeHttpClient.StatusCode = 500
				fakeHttpClient.SetMessage("fake error message")

				client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond, logger)

				err := client.StartService("test-service")
				Expect(fakeHttpClient.CallCount).To(Equal(20))
				Expect(err).To(HaveOccurred())
			})

			It("start service retries when connection refused", func() {
				fakeHttpClient := http_fakes.NewFakeHttpClient()
				fakeHttpClient.SetNilResponse()
				fakeHttpClient.Error = errors.New("some error")

				client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond, logger)

				err := client.StartService("test-service")
				Expect(fakeHttpClient.CallCount).To(Equal(20))
				Expect(err).To(HaveOccurred())
			})
		})

		Describe("StopService", func() {
			It("stop service", func() {
				var calledMonit bool

				handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
					calledMonit = true
					Expect(r.Method).To(Equal("POST"))
					Expect(r.URL.Path).To(Equal("/test-service"))
					Expect(r.PostFormValue("action")).To(Equal("stop"))
					Expect(r.Header.Get("Content-Type")).To(Equal("application/x-www-form-urlencoded"))

					expectedAuthEncoded := base64.URLEncoding.EncodeToString([]byte("fake-user:fake-pass"))
					Expect(r.Header.Get("Authorization")).To(Equal(fmt.Sprintf("Basic %s", expectedAuthEncoded)))
				})
				ts := httptest.NewServer(handler)
				defer ts.Close()

				client := NewHttpClient(ts.Listener.Addr().String(), "fake-user", "fake-pass", http.DefaultClient, 1*time.Millisecond, logger)

				err := client.StopService("test-service")
				Expect(err).ToNot(HaveOccurred())
				Expect(calledMonit).To(BeTrue())
			})

			It("stop service retries when non200 response", func() {
				fakeHttpClient := http_fakes.NewFakeHttpClient()
				fakeHttpClient.StatusCode = 500
				fakeHttpClient.SetMessage("fake error message")

				client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond, logger)

				err := client.StopService("test-service")
				Expect(fakeHttpClient.CallCount).To(Equal(20))
				Expect(err).To(HaveOccurred())
				Expect(err.Error()).To(ContainSubstring("fake error message"))
			})

			It("stop service retries when connection refused", func() {
				fakeHttpClient := http_fakes.NewFakeHttpClient()
				fakeHttpClient.SetNilResponse()
				fakeHttpClient.Error = errors.New("some error")

				client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond, logger)

				err := client.StopService("test-service")
				Expect(fakeHttpClient.CallCount).To(Equal(20))
				Expect(err).To(HaveOccurred())
				Expect(err.Error()).To(ContainSubstring("some error"))
			})
		})

		Describe("UnmonitorService", func() {
			It("issues a call to unmontor service by name", func() {
				var calledMonit bool

				handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
					calledMonit = true
					Expect(r.Method).To(Equal("POST"))
					Expect(r.URL.Path).To(Equal("/test-service"))
					Expect(r.PostFormValue("action")).To(Equal("unmonitor"))
					Expect(r.Header.Get("Content-Type")).To(Equal("application/x-www-form-urlencoded"))

					expectedAuthEncoded := base64.URLEncoding.EncodeToString([]byte("fake-user:fake-pass"))
					Expect(r.Header.Get("Authorization")).To(Equal(fmt.Sprintf("Basic %s", expectedAuthEncoded)))
				})

				ts := httptest.NewServer(handler)
				defer ts.Close()

				client := NewHttpClient(ts.Listener.Addr().String(), "fake-user", "fake-pass", http.DefaultClient, 1*time.Millisecond, logger)

				err := client.UnmonitorService("test-service")
				Expect(err).ToNot(HaveOccurred())
				Expect(calledMonit).To(BeTrue())
			})

			It("retries when non200 response", func() {
				fakeHttpClient := http_fakes.NewFakeHttpClient()
				fakeHttpClient.StatusCode = 500
				fakeHttpClient.SetMessage("fake-http-response-message")

				client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond, logger)

				err := client.UnmonitorService("test-service")
				Expect(err).To(HaveOccurred())
				Expect(err.Error()).To(ContainSubstring("fake-http-response-message"))

				Expect(fakeHttpClient.CallCount).To(Equal(20))
			})

			It("retries when connection refused", func() {
				fakeHttpClient := http_fakes.NewFakeHttpClient()
				fakeHttpClient.SetNilResponse()
				fakeHttpClient.Error = errors.New("fake-http-error")

				client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond, logger)

				err := client.UnmonitorService("test-service")
				Expect(err).To(HaveOccurred())
				Expect(err.Error()).To(ContainSubstring("fake-http-error"))

				Expect(fakeHttpClient.CallCount).To(Equal(20))
			})
		})

		Describe("ServicesInGroup", func() {
			It("services in group", func() {
				monitStatusFilePath, _ := filepath.Abs("../../../../fixtures/monit_status.xml")
				Expect(monitStatusFilePath).ToNot(BeNil())

				file, err := os.Open(monitStatusFilePath)
				Expect(err).ToNot(HaveOccurred())
				defer file.Close()

				handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
					io.Copy(w, file)
					Expect(r.Method).To(Equal("GET"))
					Expect(r.URL.Path).To(Equal("/_status2"))
					Expect(r.URL.Query().Get("format")).To(Equal("xml"))
				})
				ts := httptest.NewServer(handler)
				defer ts.Close()

				client := NewHttpClient(ts.Listener.Addr().String(), "fake-user", "fake-pass", http.DefaultClient, 1*time.Millisecond, logger)

				services, err := client.ServicesInGroup("vcap")
				Expect(err).ToNot(HaveOccurred())
				Expect([]string{"dummy"}).To(Equal(services))
			})
		})

		Describe("Status", func() {
			It("decode status", func() {
				monitStatusFilePath, _ := filepath.Abs("../../../../fixtures/monit_status.xml")
				Expect(monitStatusFilePath).ToNot(BeNil())

				file, err := os.Open(monitStatusFilePath)
				Expect(err).ToNot(HaveOccurred())
				defer file.Close()

				handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
					io.Copy(w, file)
					Expect(r.Method).To(Equal("GET"))
					Expect(r.URL.Path).To(Equal("/_status2"))
					Expect(r.URL.Query().Get("format")).To(Equal("xml"))
				})
				ts := httptest.NewServer(handler)
				defer ts.Close()

				client := NewHttpClient(ts.Listener.Addr().String(), "fake-user", "fake-pass", http.DefaultClient, 1*time.Millisecond, logger)

				status, err := client.Status()
				Expect(err).ToNot(HaveOccurred())
				dummyServices := status.ServicesInGroup("vcap")
				Expect(1).To(Equal(len(dummyServices)))
			})

			It("status retries when non200 response", func() {
				fakeHttpClient := http_fakes.NewFakeHttpClient()
				fakeHttpClient.StatusCode = 500
				fakeHttpClient.SetMessage("fake error message")

				client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond, logger)

				_, err := client.Status()
				Expect(fakeHttpClient.CallCount).To(Equal(20))
				Expect(err).To(HaveOccurred())
				Expect(err.Error()).To(ContainSubstring("fake error message"))
			})

			It("status retries when connection refused", func() {
				fakeHttpClient := http_fakes.NewFakeHttpClient()
				fakeHttpClient.SetNilResponse()
				fakeHttpClient.Error = errors.New("some error")

				client := NewHttpClient("agent.example.com", "fake-user", "fake-pass", fakeHttpClient, 1*time.Millisecond, logger)

				err := client.StartService("hello")
				Expect(fakeHttpClient.CallCount).To(Equal(20))
				Expect(err).To(HaveOccurred())
				Expect(err.Error()).To(ContainSubstring("some error"))

				for _, req := range fakeHttpClient.RequestBodies {
					Expect(req).To(Equal("action=start"))
				}
			})
		})
	})
}