Skip to content

Commit

Permalink
feat: add support for replacing baseUrl of test case url (keploy#1916)
Browse files Browse the repository at this point in the history
* feat: add support for replacing request originof test case url

Signed-off-by: gouravkrosx <[email protected]>

* fix: condition of replacing base-url

Signed-off-by: gouravkrosx <[email protected]>

* chore: refactor code

Signed-off-by: gouravkrosx <[email protected]>

* fix: execution blocked due to no context cancellation

Signed-off-by: gouravkrosx <[email protected]>

* fix: lint

Signed-off-by: gouravkrosx <[email protected]>

* chore: addressed PR comments

Signed-off-by: gouravkrosx <[email protected]>

---------

Signed-off-by: gouravkrosx <[email protected]>
Former-commit-id: 6c052f7
  • Loading branch information
gouravkrosx authored Jun 8, 2024
1 parent 7e58e2d commit 8ef7e7f
Show file tree
Hide file tree
Showing 13 changed files with 288 additions and 291 deletions.
79 changes: 0 additions & 79 deletions cli/mock.go

This file was deleted.

29 changes: 14 additions & 15 deletions cli/provider/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func (c *CmdConfigurator) AddFlags(cmd *cobra.Command) error {
cmd.Flags().Bool("removeUnusedMocks", c.cfg.Test.RemoveUnusedMocks, "Clear the unused mocks for the passed test-sets")
cmd.Flags().Bool("goCoverage", c.cfg.Test.GoCoverage, "Enable go coverage reporting for the testcases")
cmd.Flags().Bool("fallBackOnMiss", c.cfg.Test.FallBackOnMiss, "Enable connecting to actual service if mock not found during test mode")
cmd.Flags().String("basePath", c.cfg.Test.BasePath, "Custom api basePath/origin to replace the actual basePath/origin in the testcases; App flag is ignored and app will not be started & instrumented when this is set since the application running on a different machine")
cmd.Flags().Bool("mocking", true, "enable/disable mocking for the testcases")
} else {
cmd.Flags().Uint64("recordTimer", 0, "User provided time to record its application")
Expand Down Expand Up @@ -363,28 +364,18 @@ func (c *CmdConfigurator) ValidateFlags(ctx context.Context, cmd *cobra.Command)

switch cmd.Name() {
case "record", "test":
bypassPorts, err := cmd.Flags().GetUintSlice("passThroughPorts")
if err != nil {
errMsg := "failed to read the ports of outgoing calls to be ignored"
utils.LogError(c.logger, err, errMsg)
return errors.New(errMsg)
}
config.SetByPassPorts(c.cfg, bypassPorts)

// handle the app command
if c.cfg.Command == "" {
utils.LogError(c.logger, nil, "missing required -c flag or appCmd in config file")
if c.cfg.InDocker {
c.logger.Info(`Example usage: keploy test -c "docker run -p 8080:8080 --network myNetworkName myApplicationImageName" --delay 6`)
} else {
c.logger.Info(LogExample(RootExamples))
if !alreadyRunning(cmd.Name(), c.cfg.Test.BasePath) {
return c.noCommandError()
}
return errors.New("missing required -c flag or appCmd in config file")
}

// set the command type
c.cfg.CommandType = string(utils.FindDockerCmd(c.cfg.Command))

if c.cfg.GenerateGithubActions {
if c.cfg.GenerateGithubActions && utils.CmdType(c.cfg.CommandType) != utils.Empty {
defer utils.GenerateGithubActions(c.logger, c.cfg.Command)
}
if c.cfg.InDocker {
Expand Down Expand Up @@ -439,8 +430,16 @@ func (c *CmdConfigurator) ValidateFlags(ctx context.Context, cmd *cobra.Command)
utils.LogError(c.logger, err, "error while getting absolute path")
return errors.New("failed to get the absolute path")
}

c.cfg.Path = absPath + "/keploy"

bypassPorts, err := cmd.Flags().GetUintSlice("passThroughPorts")
if err != nil {
errMsg := "failed to read the ports of outgoing calls to be ignored"
utils.LogError(c.logger, err, errMsg)
return errors.New(errMsg)
}
config.SetByPassPorts(c.cfg, bypassPorts)

if cmd.Name() == "test" {
//check if the keploy folder exists
if _, err := os.Stat(c.cfg.Path); os.IsNotExist(err) {
Expand Down
22 changes: 22 additions & 0 deletions cli/provider/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package provider

import (
"errors"

"go.keploy.io/server/v2/utils"
)

func (c *CmdConfigurator) noCommandError() error {
utils.LogError(c.logger, nil, "missing required -c flag or appCmd in config file")
if c.cfg.InDocker {
c.logger.Info(`Example usage: keploy test -c "docker run -p 8080:8080 --network myNetworkName myApplicationImageName" --delay 6`)
} else {
c.logger.Info(LogExample(RootExamples))
}
return errors.New("missing required -c flag or appCmd in config file")
}

// alreadyRunning checks that during test mode, if user provides the basePath, then it implies that the application is already running somewhere.
func alreadyRunning(cmd, basePath string) bool {
return (cmd == "test" && basePath != "")
}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type Test struct {
Language string `json:"language" yaml:"language" mapstructure:"language"`
RemoveUnusedMocks bool `json:"removeUnusedMocks" yaml:"removeUnusedMocks" mapstructure:"removeUnusedMocks"`
FallBackOnMiss bool `json:"fallBackOnMiss" yaml:"fallBackOnMiss" mapstructure:"fallBackOnMiss"`
BasePath string `json:"basePath" yaml:"basePath" mapstructure:"basePath"`
Mocking bool `json:"mocking" yaml:"mocking" mapstructure:"mocking"`
}

Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ test:
mongoPassword: "default@123"
language: ""
removeUnusedMocks: false
basePath: ""
mocking: true
record:
recordTimer: 0s
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"go.keploy.io/server/v2/utils"
"go.keploy.io/server/v2/utils/log"
//pprof for debugging
// _ "net/http/pprof"
//_ "net/http/pprof"
)

// version is the version of the server and will be injected during build by ldflags, same with dsn
Expand Down Expand Up @@ -47,6 +47,7 @@ func main() {
// return
// }
// }()

printLogo()
ctx := utils.NewCtx()
start(ctx)
Expand Down
19 changes: 13 additions & 6 deletions pkg/graph/schema.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/service/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (r *Recorder) Start(ctx context.Context) error {
return fmt.Errorf(stopReason)
}

newTestSetID = pkg.NewID(testSetIDs, models.TestSetPattern)
newTestSetID = pkg.NextID(testSetIDs, models.TestSetPattern)

// setting up the environment for recording
appID, err = r.instrumentation.Setup(ctx, r.config.Command, models.SetupOptions{Container: r.config.ContainerName, DockerNetwork: r.config.NetworkName, DockerDelay: r.config.BuildDelay})
Expand Down
Loading

0 comments on commit 8ef7e7f

Please sign in to comment.