Skip to content

Commit

Permalink
removing fallback option and from view - review comment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kapishmalik committed May 6, 2024
1 parent 5ee1ba7 commit bd29820
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 81 deletions.
41 changes: 1 addition & 40 deletions core/cmd/hoverfly/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ var (
cors = flag.Bool("cors", false, "Enable CORS support")
noImportCheck = flag.Bool("no-import-check", false, "Skip duplicate request check when importing simulations")

pacFile = flag.String("pac-file", "", "Path to the pac file to be imported on startup")
fallbackPostServeAction = flag.String("fallback-post-serve-action", "", "Set fallback post serve action by passing the binary and the path of the action script and delay in Ms separated by space or remote and deply in Ms separated by space. (i.e. '-fallback-post-serve-action \"http://localhost:8080 2000\"')")
pacFile = flag.String("pac-file", "", "Path to the pac file to be imported on startup")

clientAuthenticationDestination = flag.String("client-authentication-destination", "", "Regular expression of destination with client authentication")
clientAuthenticationClientCert = flag.String("client-authentication-client-cert", "", "Path to the client certification file used for authentication")
Expand Down Expand Up @@ -590,44 +589,6 @@ func main() {
}
}

if *fallbackPostServeAction != "" {
splitPostServeAction := strings.Split(*fallbackPostServeAction, " ")
if len(splitPostServeAction) == 3 {
delayInMs, err := strconv.Atoi(splitPostServeAction[2])
if err != nil {
//default to 1000 incase of error
delayInMs = 1000
}

if fileContents, err := ioutil.ReadFile(splitPostServeAction[1]); err == nil {
err = hoverfly.SetLocalPostServeAction("", splitPostServeAction[0], string(fileContents), delayInMs)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"import": *fallbackPostServeAction,
}).Fatal("Failed to import fallback post serve action")
}
}
} else if len(splitPostServeAction) == 2 {
delayInMs, err := strconv.Atoi(splitPostServeAction[1])
if err != nil {
//default to 1000 in case of error
delayInMs = 1000
}
err = hoverfly.SetRemotePostServeAction(" ", splitPostServeAction[0], delayInMs)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"import": *fallbackPostServeAction,
}).Fatal("Failed to import fallback post serve action")
}
} else {
log.WithFields(log.Fields{
"import": *fallbackPostServeAction,
}).Fatal("Failed to import fallback post serve action due to invalid input passed")
}
}

if len(templatingDataSourceFlags) > 0 {

for _, v := range templatingDataSourceFlags {
Expand Down
3 changes: 1 addition & 2 deletions core/handlers/v2/postserveactiondetails_views.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package v2

type PostServeActionDetailsView struct {
Actions []ActionView `json:"actions,omitempty"`
FallbackAction *ActionView `json:"fallbackAction,omitempty"`
Actions []ActionView `json:"actions,omitempty"`
}

type ActionView struct {
Expand Down
6 changes: 2 additions & 4 deletions core/hoverfly_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,14 +468,12 @@ func (hf *Hoverfly) GetAllPostServeActions() v2.PostServeActionDetailsView {
actions = append(actions, action.GetActionView(actionName))
}

var fallbackActionView v2.ActionView
if hf.PostServeActionDetails.FallbackAction != nil {
fallbackActionView = hf.PostServeActionDetails.FallbackAction.GetActionView("")
actions = append(actions, hf.PostServeActionDetails.FallbackAction.GetActionView(""))
}

return v2.PostServeActionDetailsView{
Actions: actions,
FallbackAction: &fallbackActionView,
Actions: actions,
}
}

Expand Down
8 changes: 4 additions & 4 deletions core/hoverfly_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1397,16 +1397,16 @@ func TestHoverfly_GetPostServeActions_WithFallback(t *testing.T) {
postServeActions := unit.GetAllPostServeActions()

Expect(postServeActions).NotTo(BeNil())
Expect(postServeActions.Actions).To(HaveLen(2))
Expect(postServeActions.Actions).To(HaveLen(3))
Expect(postServeActions.Actions[0].ActionName).To(Equal("test-local-callback"))
Expect(postServeActions.Actions[0].Binary).To(Equal("python3"))
Expect(postServeActions.Actions[0].DelayInMs).To(Equal(1900))
Expect(postServeActions.Actions[1].ActionName).To(Equal("test-remote-callback"))
Expect(postServeActions.Actions[1].Remote).To(Equal("http://localhost"))
Expect(postServeActions.Actions[1].DelayInMs).To(Equal(1800))
Expect(postServeActions.FallbackAction).NotTo(BeNil())
Expect(postServeActions.FallbackAction.Remote).To(Equal("http://localhost:8081"))
Expect(postServeActions.FallbackAction.DelayInMs).To(Equal(1800))
Expect(postServeActions.Actions[2]).NotTo(BeNil())
Expect(postServeActions.Actions[2].Remote).To(Equal("http://localhost:8081"))
Expect(postServeActions.Actions[2].DelayInMs).To(Equal(1800))
}

func TestHoverfly_SetLocalPostServeAction(t *testing.T) {
Expand Down
24 changes: 4 additions & 20 deletions functional-tests/core/ft_postserveaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,6 @@ var _ = Describe("Manage post serve actions in hoverfly", func() {
Expect(postServeActionDetails.Actions[0].DelayInMs).To(Equal(1300))
})
})

Context("hoverfly with fallback remote post-serve-action", func() {

BeforeEach(func() {
hoverfly.Start("-fallback-post-serve-action", "http://localhost:8080 1300")
})

It("Should return post serve action details", func() {
postServeActionDetails := hoverfly.GetAllPostServeAction()
Expect(postServeActionDetails).NotTo(BeNil())
Expect(postServeActionDetails.FallbackAction).NotTo(BeNil())
Expect(postServeActionDetails.FallbackAction.ActionName).To(Equal(""))
Expect(postServeActionDetails.FallbackAction.Remote).To(Equal("http://localhost:8080"))
Expect(postServeActionDetails.FallbackAction.DelayInMs).To(Equal(1300))
})
})
})

Context("set local post serve action", func() {
Expand Down Expand Up @@ -133,10 +117,10 @@ var _ = Describe("Manage post serve actions in hoverfly", func() {
It("Should set post serve action", func() {
postServeActionDetails := hoverfly.SetRemotePostServeAction("", "http://localhost:8080", 1600)
Expect(postServeActionDetails).NotTo(BeNil())
Expect(postServeActionDetails.FallbackAction).NotTo(BeNil())
Expect(postServeActionDetails.FallbackAction.ActionName).To(Equal(""))
Expect(postServeActionDetails.FallbackAction.Remote).To(Equal("http://localhost:8080"))
Expect(postServeActionDetails.FallbackAction.DelayInMs).To(Equal(1600))
Expect(postServeActionDetails.Actions).NotTo(BeNil())
Expect(postServeActionDetails.Actions[0].ActionName).To(Equal(""))
Expect(postServeActionDetails.Actions[0].Remote).To(Equal("http://localhost:8080"))
Expect(postServeActionDetails.Actions[0].DelayInMs).To(Equal(1600))
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion functional-tests/hoverctl/postserveaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ var _ = Describe("When I use hoverctl", func() {
Expect(output).To(ContainSubstring("Success"))

output = functional_tests.Run(hoverctlBinary, "post-serve-action", "get-all")
Expect(output).To(ContainSubstring("default"))
Expect(output).To(ContainSubstring("fallback"))
Expect(output).To(ContainSubstring("http://localhost"))
Expect(output).To(ContainSubstring("1700"))
})
Expand Down
18 changes: 8 additions & 10 deletions hoverctl/cmd/postserveaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,16 @@ func getPostServeActionsTabularData(postServeActions v2.PostServeActionDetailsVi
actionData := []string{action.ActionName, action.Binary, getContentShorthand(action.ScriptContent), fmt.Sprint(action.DelayInMs)}
localPostServeActionsData = append(localPostServeActionsData, actionData)
} else {
actionData := []string{action.ActionName, action.Remote, fmt.Sprint(action.DelayInMs)}
var actionName string
if action.ActionName == "" {
actionName = "fallback"
} else {
actionName = action.ActionName
}
actionData := []string{actionName, action.Remote, fmt.Sprint(action.DelayInMs)}
remotePostServeActionData = append(remotePostServeActionData, actionData)
}
}
action := *postServeActions.FallbackAction
const defaultActionName = "default"
if action.Remote == "" {
actionData := []string{defaultActionName, action.Binary, getContentShorthand(action.ScriptContent), fmt.Sprint(action.DelayInMs)}
localPostServeActionsData = append(localPostServeActionsData, actionData)
} else {
actionData := []string{defaultActionName, action.Remote, fmt.Sprint(action.DelayInMs)}
remotePostServeActionData = append(remotePostServeActionData, actionData)
}

return localPostServeActionsData, remotePostServeActionData
}

0 comments on commit bd29820

Please sign in to comment.