Skip to content
This repository has been archived by the owner on Oct 8, 2020. It is now read-only.

Commit

Permalink
Added gtg endpoint (#20)
Browse files Browse the repository at this point in the history
* Added gtg endpoint

* Added tests for the gtg endpoint

* Updated badges. Removed coveralls badge branch info
  • Loading branch information
davidbalazs93 committed Apr 4, 2017
1 parent 3ed3468 commit b3a841f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![CircleCI](https://circleci.com/gh/Financial-Times/content-preview.svg?style=svg)](https://circleci.com/gh/Financial-Times/content-preview) [![Coverage Status](https://coveralls.io/repos/github/Financial-Times/content-preview/badge.svg?branch=master)](https://coveralls.io/github/Financial-Times/content-preview?branch=master)
[![CircleCI](https://circleci.com/gh/Financial-Times/content-preview.svg?style=shield)](https://circleci.com/gh/Financial-Times/content-preview) [![Coverage Status](https://coveralls.io/repos/github/Financial-Times/content-preview/badge.svg)](https://coveralls.io/github/Financial-Times/content-preview)

# Content Preview Service (content-preview)

Expand Down
10 changes: 7 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package main

import (
"net/http"
"os"
"time"

fthealth "github.com/Financial-Times/go-fthealth/v1a"
oldhttphandlers "github.com/Financial-Times/http-handlers-go/httphandlers"
"github.com/Financial-Times/service-status-go/gtg"
"github.com/Financial-Times/service-status-go/httphandlers"
"github.com/Sirupsen/logrus"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/jawher/mow.cli"
"net/http"
"os"
"time"
)

const serviceDescription = "A RESTful API for retrieving and transforming content to preview data"
Expand Down Expand Up @@ -154,6 +156,8 @@ func setupServiceHandler(sc ServiceConfig, metricsHandler Metrics, contentHandle
oldhttphandlers.TransactionAwareRequestLoggingHandler(logrus.StandardLogger(), contentHandler))})
r.Path(httphandlers.BuildInfoPath).HandlerFunc(httphandlers.BuildInfoHandler)
r.Path(httphandlers.PingPath).HandlerFunc(httphandlers.PingHandler)
gtgHandler := httphandlers.NewGoodToGoHandler(gtg.StatusChecker(sc.gtgCheck))
r.Path(httphandlers.GTGPath).HandlerFunc(gtgHandler)
r.Path("/__health").Handler(handlers.MethodHandler{"GET": http.HandlerFunc(fthealth.Handler(sc.serviceName, serviceDescription, sc.nativeContentSourceCheck(), sc.transformerServiceCheck()))})
r.Path("/__metrics").Handler(handlers.MethodHandler{"GET": http.HandlerFunc(metricsHttpEndpoint)})
return r
Expand Down
90 changes: 82 additions & 8 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@ package main
import (
"bytes"
"encoding/json"
fthealth "github.com/Financial-Times/go-fthealth/v1a"
tid "github.com/Financial-Times/transactionid-utils-go"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"

fthealth "github.com/Financial-Times/go-fthealth/v1a"
tid "github.com/Financial-Times/transactionid-utils-go"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
)

var contentPreviewService *httptest.Server
var methodeApiMock *httptest.Server
var methodeArticleTransformerMock *httptest.Server

const sourceAppName = "Native Content Service"
const transformAppName = "Native Content Transformer Service"

func startMethodeApiMock(status string) {
r := mux.NewRouter()
if status == "happy" {
Expand Down Expand Up @@ -143,8 +149,8 @@ func startContentPreviewService() {
methodArticleTransformerUrl,
nativeContentAppHealthUri,
transformAppHealthUrl,
"Native Content Service",
"Native Content Transformer Service",
sourceAppName,
transformAppName,
"panic guide",
"panic guide",
"business impact",
Expand All @@ -161,7 +167,7 @@ func startContentPreviewService() {
contentPreviewService = httptest.NewServer(h)
}

func TestShouldReturn200AndTrasformerOutput(t *testing.T) {
func TestShouldReturn200AndTransformerOutput(t *testing.T) {
startMethodeApiMock("happy")
startMethodeArticleTransformerMock("happy")
startContentPreviewService()
Expand Down Expand Up @@ -346,3 +352,71 @@ func TestShouldBeUnhealthyWhenTransformerIsNotHappy(t *testing.T) {
}

}

func TestGtgShouldReturn503WhenMethodeApiIsNotHappy(t *testing.T) {
startMethodeApiMock("unhappy")
startMethodeArticleTransformerMock("happy")
startContentPreviewService()
defer stopServices()

resp, err := http.Get(contentPreviewService.URL + "/__gtg")
if err != nil {
panic(err)
}
defer resp.Body.Close()

assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode, "Response status code should be 503")

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
assert.Fail(t, "No error should be returned when reading the response body")
}
expectedRespBody := fmt.Sprintf("%s service is not responding with OK. status=%d", sourceAppName, http.StatusInternalServerError)
actualRespBody := string(body)
assert.Equal(t, expectedRespBody, actualRespBody)
}

func TestGtgShouldReturn503WhenTransformerIsNotHappy(t *testing.T) {
startMethodeApiMock("happy")
startMethodeArticleTransformerMock("unhappy")
startContentPreviewService()
defer stopServices()

resp, err := http.Get(contentPreviewService.URL + "/__gtg")
if err != nil {
panic(err)
}
defer resp.Body.Close()

assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode, "Response status code should be 503")

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
assert.Fail(t, "No error should be returned when reading the response body")
}
expectedRespBody := fmt.Sprintf("%s service is not responding with OK. status=%d", transformAppName, http.StatusInternalServerError)
actualRespBody := string(body)
assert.Equal(t, expectedRespBody, actualRespBody)
}

func TestGtgShouldReturn200(t *testing.T) {
startMethodeApiMock("happy")
startMethodeArticleTransformerMock("happy")
startContentPreviewService()
defer stopServices()

resp, err := http.Get(contentPreviewService.URL + "/__gtg")
if err != nil {
panic(err)
}
defer resp.Body.Close()

assert.Equal(t, http.StatusOK, resp.StatusCode, "Response status code should be 200")

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
assert.Fail(t, "No error should be returned when reading the response body")
}
actualRespBody := string(body)
assert.Equal(t, "OK", actualRespBody)
}
18 changes: 17 additions & 1 deletion healthchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"errors"
"fmt"
fthealth "github.com/Financial-Times/go-fthealth/v1a"
"net/http"

fthealth "github.com/Financial-Times/go-fthealth/v1a"
"github.com/Financial-Times/service-status-go/gtg"
)

func (sc *ServiceConfig) nativeContentSourceCheck() fthealth.Check {
Expand Down Expand Up @@ -33,6 +35,20 @@ func (sc *ServiceConfig) transformerServiceCheck() fthealth.Check {
}
}

func (sc *ServiceConfig) gtgCheck() gtg.Status {
msg, err := checkServiceAvailability(sc.sourceAppName, sc.sourceAppHealthUri, sc.sourceAppAuth, "")
if err != nil {
return gtg.Status{GoodToGo: false, Message: msg}
}

msg, err = checkServiceAvailability(sc.transformAppName, sc.transformAppHealthUri, "", sc.transformAppHostHeader)
if err != nil {
return gtg.Status{GoodToGo: false, Message: msg}
}

return gtg.Status{GoodToGo: true}
}

func checkServiceAvailability(serviceName string, healthUri string, auth string, hostHeader string) (string, error) {
req, err := http.NewRequest("GET", healthUri, nil)
if auth != "" {
Expand Down

0 comments on commit b3a841f

Please sign in to comment.