Skip to content

Commit

Permalink
initial commit for logging and journal entry, also fixed breaking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kapishmalik committed Apr 8, 2024
1 parent eb31e77 commit af59787
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 85 deletions.
18 changes: 17 additions & 1 deletion core/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"time"

v2 "github.com/SpectoLabs/hoverfly/core/handlers/v2"
"github.com/SpectoLabs/hoverfly/core/journal"
"github.com/SpectoLabs/hoverfly/core/models"
"github.com/pborman/uuid"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -117,7 +119,7 @@ func (action *Action) GetActionView(actionName string) v2.ActionView {
}
}

func (action *Action) Execute(pair *models.RequestResponsePair) error {
func (action *Action) Execute(pair *models.RequestResponsePair, journalIDChannel chan string, journal *journal.Journal) error {

pairViewBytes, err := json.Marshal(pair.ConvertToRequestResponsePairView())
if err != nil {
Expand All @@ -131,6 +133,9 @@ func (action *Action) Execute(pair *models.RequestResponsePair) error {
//adding 200 ms to include some buffer for it to return response
time.Sleep(time.Duration(200+action.DelayInMs) * time.Millisecond)

journalID := <-journalIDChannel
log.Info("Journal ID received ", journalID)

//if it is remote callback
if action.Remote != "" {

Expand All @@ -142,7 +147,10 @@ func (action *Action) Execute(pair *models.RequestResponsePair) error {
return err
}

correlationID := uuid.New()
invokedTime := time.Now()
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-CORRELATION-ID", correlationID)

resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand All @@ -152,16 +160,24 @@ func (action *Action) Execute(pair *models.RequestResponsePair) error {
return err
}

completionTime := time.Now()
journal.UpdatePostServeActionDetailsInJournal(journalID, pair.Response.PostServeAction, correlationID, invokedTime, completionTime, resp.StatusCode)
if resp.StatusCode != 200 {
log.Error("Remote post serve action did not process payload")

return nil
}
log.Info("Remote post serve action invoked successfully")
return nil
}

invokedTime := time.Now()
actionCommand := exec.Command(action.Binary, action.Script.Name())
actionCommand.Stdin = bytes.NewReader(pairViewBytes)
completionTime := time.Now()

journal.UpdatePostServeActionDetailsInJournal(journalID, pair.Response.PostServeAction, "", invokedTime, completionTime, 0)

var stdout bytes.Buffer
var stderr bytes.Buffer
actionCommand.Stdout = &stdout
Expand Down
19 changes: 16 additions & 3 deletions core/action/action_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package action_test

import (
"github.com/SpectoLabs/hoverfly/core/journal"
"github.com/gorilla/mux"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -110,7 +111,11 @@ func Test_ExecuteLocalPostServeAction(t *testing.T) {

originalPair := models.RequestResponsePair{Response: resp, Request: req}

err = newAction.Execute(&originalPair)
//not adding entry as update journal method will be tested in its file
journalIDChannel := make(chan string)
newJournal := journal.NewJournal()
journalIDChannel <- "1"
err = newAction.Execute(&originalPair, journalIDChannel, newJournal)
Expect(err).To(BeNil())
}

Expand All @@ -126,10 +131,14 @@ func Test_ExecuteRemotePostServeAction(t *testing.T) {
Body: "Normal body",
},
}
//not adding entry as update journal method will be tested in its file
journalIDChannel := make(chan string)
newJournal := journal.NewJournal()
journalIDChannel <- "1"

newAction, err := action.NewRemoteAction("test-callback", server.URL+"/process", 0)
Expect(err).To(BeNil())
err = newAction.Execute(&originalPair)
err = newAction.Execute(&originalPair, journalIDChannel, newJournal)
Expect(err).To(BeNil())
}

Expand All @@ -143,7 +152,11 @@ func Test_ExecuteRemotePostServeAction_WithUnReachableHost(t *testing.T) {
newAction, err := action.NewRemoteAction("test-callback", "http://test", 0)
Expect(err).To(BeNil())

err = newAction.Execute(&originalPair)
//not adding entry as update journal method will be tested in its file
journalIDChannel := make(chan string)
newJournal := journal.NewJournal()
journalIDChannel <- "1"
err = newAction.Execute(&originalPair, journalIDChannel, newJournal)
Expect(err).NotTo(BeNil())
}

Expand Down
2 changes: 1 addition & 1 deletion core/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func BenchmarkProcessRequest(b *testing.B) {
b.Run(bm.name, func(b *testing.B) {

for n := 0; n < b.N; n++ {
resp = hoverfly.processRequest(request)
resp, _ = hoverfly.processRequest(request)
}
})
}
Expand Down
21 changes: 15 additions & 6 deletions core/handlers/v2/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,21 @@ type JournalView struct {
}

type JournalEntryView struct {
Request RequestDetailsView `json:"request"`
Response ResponseDetailsView `json:"response"`
Mode string `json:"mode"`
TimeStarted string `json:"timeStarted"`
Latency float64 `json:"latency"`
Id string `json:"id"`
Request RequestDetailsView `json:"request"`
Response ResponseDetailsView `json:"response"`
Mode string `json:"mode"`
TimeStarted string `json:"timeStarted"`
Latency float64 `json:"latency"`
Id string `json:"id"`
PostServeActionEntry *PostServeActionEntryView `json:"postServeAction,omitEmpty"`
}

type PostServeActionEntryView struct {
ActionName string `json:"actionName"`
InvokedTime string `json:"invoked"`
CompletedTime string `json:"completed"`
CorrelationId string `json:"correlationId,omitempty"`
HttpStatus int `json:"status,omitempty"`
}

type JournalEntryFilterView struct {
Expand Down
15 changes: 9 additions & 6 deletions core/hoverfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,16 @@ func (hf *Hoverfly) StopProxy() {

// processRequest - processes incoming requests and based on proxy state (record/playback)
// returns HTTP response.
func (hf *Hoverfly) processRequest(req *http.Request) *http.Response {
func (hf *Hoverfly) processRequest(req *http.Request) (*http.Response, chan string) {
if hf.Cfg.CORS.Enabled {
response := hf.Cfg.CORS.InterceptPreflightRequest(req)
if response != nil {
return response
return response, nil
}
}
requestDetails, err := models.NewRequestDetailsFromHttpRequest(req)
if err != nil {
return modes.ErrorResponse(req, err, "Could not interpret HTTP request").Response
return modes.ErrorResponse(req, err, "Could not interpret HTTP request").Response, nil
}

modeName := hf.Cfg.GetMode()
Expand All @@ -208,7 +208,7 @@ func (hf *Hoverfly) processRequest(req *http.Request) *http.Response {
// and definitely don't delay people in capture mode
// Don't delete the error
if err != nil || modeName == modes.Capture {
return result.Response
return result.Response, nil
}

if result.IsResponseDelayable() {
Expand All @@ -220,12 +220,15 @@ func (hf *Hoverfly) processRequest(req *http.Request) *http.Response {
}

if result.PostServeActionInputDetails != nil {

journalIDChannel := make(chan string)
if postServeAction, ok := hf.PostServeActionDetails.Actions[result.PostServeActionInputDetails.PostServeAction]; ok {
go postServeAction.Execute(result.PostServeActionInputDetails.Pair)
go postServeAction.Execute(result.PostServeActionInputDetails.Pair, journalIDChannel, hf.Journal)
}
return result.Response, journalIDChannel
}

return result.Response
return result.Response, nil
}

func (hf *Hoverfly) applyResponseDelay(result modes.ProcessResult) {
Expand Down
2 changes: 2 additions & 0 deletions core/hoverfly_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ func (hf *Hoverfly) SetLocalPostServeAction(actionName string, binary string, sc
if err != nil {
return err
}
log.Info("Local post serve action is set")
return nil
}

Expand All @@ -494,6 +495,7 @@ func (hf *Hoverfly) SetRemotePostServeAction(actionName, remote string, delayInM
if err != nil {
return err
}
log.Info("Remote post serve action is set")
return nil
}

Expand Down
Loading

0 comments on commit af59787

Please sign in to comment.