Skip to content

Commit

Permalink
feat: add coverage flag to implement serve within test itself (keplo…
Browse files Browse the repository at this point in the history
…y#1300)

* add coverage flag to implement serve

Signed-off-by: EraKin575 <tejaskumar574@gmail.com>

* removed serve.go and moved functionality to test

Signed-off-by: EraKin575 <tejaskumar574@gmail.com>

* changed workflow according to new changes

Signed-off-by: EraKin575 <tejaskumar574@gmail.com>

* changed workflow according to new changes

Signed-off-by: EraKin575 <tejaskumar574@gmail.com>

---------

Signed-off-by: EraKin575 <tejaskumar574@gmail.com>
  • Loading branch information
EraKin575 committed Jan 8, 2024
1 parent c0cadfe commit 013e6e2
Show file tree
Hide file tree
Showing 19 changed files with 116 additions and 204 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ done
# Start keploy in test mode.
sudo -E env PATH=$PATH ./../../keployv2 test -c 'npm start' --delay 10

sudo -E env PATH=$PATH ./../../keployv2 serve -c "npm test" --delay 5
sudo -E env PATH=$PATH ./../../keployv2 test -c "npm test" --delay 5 --coverage

sudo -E env PATH=$PATH ./../../keployv2 test -c 'npm start' --delay 10 --testsets test-set-0

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_workflow_scripts/node-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ done
# Start keploy in test mode.
sudo -E env PATH=$PATH ./../../keployv2 test -c 'npm start' --delay 10

sudo -E env PATH=$PATH ./../../keployv2 serve -c "npm test" --delay 5
sudo -E env PATH=$PATH ./../../keployv2 test -c "npm test" --delay 5 --coverage

sudo -E env PATH=$PATH ./../../keployv2 test -c 'npm start' --delay 10 --testsets test-set-0

Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (r *Root) execute() {
r.logger = setupLogger()
r.logger = modifyToSentryLogger(r.logger, sentry.CurrentHub().Client())
defer deleteLogs(r.logger)
r.subCommands = append(r.subCommands, NewCmdRecord(r.logger), NewCmdTest(r.logger), NewCmdServe(r.logger), NewCmdExample(r.logger), NewCmdMockRecord(r.logger), NewCmdMockTest(r.logger), NewCmdGenerateConfig(r.logger))
r.subCommands = append(r.subCommands, NewCmdRecord(r.logger), NewCmdTest(r.logger), NewCmdExample(r.logger), NewCmdMockRecord(r.logger), NewCmdMockTest(r.logger), NewCmdGenerateConfig(r.logger))

// add the registered keploy plugins as subcommands to the rootCmd
for _, sc := range r.subCommands {
Expand Down
144 changes: 0 additions & 144 deletions cmd/serve.go

This file was deleted.

90 changes: 74 additions & 16 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/spf13/cobra"
"go.keploy.io/server/pkg/graph"
"go.keploy.io/server/pkg/models"
"go.keploy.io/server/pkg/service/test"
"go.keploy.io/server/utils"
Expand All @@ -24,6 +25,7 @@ func NewCmdTest(logger *zap.Logger) *Test {
}
}


func readTestConfig(configPath string) (*models.Test, error) {
file, err := os.OpenFile(configPath, os.O_RDONLY, os.ModePerm)
if err != nil {
Expand Down Expand Up @@ -92,6 +94,7 @@ func (t *Test) getTestConfig(path *string, proxyPort *uint32, appCmd *string, te
type Test struct {
tester test.Tester
logger *zap.Logger

}

func (t *Test) GetCmd() *cobra.Command {
Expand Down Expand Up @@ -142,6 +145,36 @@ func (t *Test) GetCmd() *cobra.Command {
return err
}

coverage, err := cmd.Flags().GetBool("coverage")
if err != nil {
t.logger.Error("Failed to get the coverage flag", zap.Error((err)))
return err
}
var lang string
var pid uint32
var port uint32
if !coverage {

lang, err = cmd.Flags().GetString("language")
if err != nil {
t.logger.Error("failed to read the programming language")
return err
}

pid, err = cmd.Flags().GetUint32("pid")
if err != nil {
t.logger.Error("Failed to get the pid of the application", zap.Error((err)))
return err
}

port, err = cmd.Flags().GetUint32("port")
if err != nil {
t.logger.Error("Failed to get the port of keploy server", zap.Error((err)))
return err
}

}

buildDelay, err := cmd.Flags().GetDuration("buildDelay")
if err != nil {
t.logger.Error("Failed to get the build-delay flag", zap.Error((err)))
Expand All @@ -160,6 +193,12 @@ func (t *Test) GetCmd() *cobra.Command {
return err
}

// port, err := cmd.Flags().GetUint32("port")
// if err != nil {
// t.logger.Error("failed to read the port of keploy server")
// return err
// }

proxyPort, err := cmd.Flags().GetUint32("proxyport")
if err != nil {
t.logger.Error("failed to read the proxyport")
Expand Down Expand Up @@ -261,37 +300,48 @@ func (t *Test) GetCmd() *cobra.Command {
}
}

//flags like lang, pid, port cannot be used unless called the serve method
// Check if the coverage flag is set

t.logger.Debug("the ports are", zap.Any("ports", ports))

mongoPassword, err := cmd.Flags().GetString("mongoPassword")
if err != nil {
t.logger.Error("failed to read the ports of outgoing calls to be ignored")
return err
}
t.logger.Debug("the configuration for mocking mongo connection", zap.Any("password", mongoPassword))

t.tester.Test(path, testReportPath, appCmd, test.TestOptions{
Tests: tests,
AppContainer: appContainer,
AppNetwork: networkName,
MongoPassword: mongoPassword,
Delay: delay,
BuildDelay: buildDelay,
PassThroughPorts: ports,
ApiTimeout: apiTimeout,
ProxyPort: proxyPort,
GlobalNoise: globalNoise,
TestsetNoise: testsetNoise,
WithCoverage: withCoverage,
CoverageReportPath: coverageReportPath,
}, enableTele)
t.logger.Debug("the configuration for mocking mongo connection", zap.Any("password", mongoPassword))

if coverage {
g := graph.NewGraph(t.logger)
g.Serve(path, proxyPort, testReportPath, delay, pid, port, lang, ports, apiTimeout, appCmd, enableTele)
} else {
t.tester.Test(path, testReportPath, appCmd, test.TestOptions{
Tests: tests,
AppContainer: appContainer,
AppNetwork: networkName,
MongoPassword: mongoPassword,
Delay: delay,
BuildDelay: buildDelay,
PassThroughPorts: ports,
ApiTimeout: apiTimeout,
ProxyPort: proxyPort,
GlobalNoise: globalNoise,
TestsetNoise: testsetNoise,
WithCoverage: withCoverage,
CoverageReportPath: coverageReportPath,
}, enableTele)
}

return nil
},
}

testCmd.Flags().StringP("path", "p", "", "Path to local directory where generated testcases/mocks are stored")

testCmd.Flags().Uint32("port", 6789, "Port at which you want to run graphql Server")

testCmd.Flags().Uint32("proxyport", 0, "Choose a port to run Keploy Proxy.")

testCmd.Flags().StringP("command", "c", "", "Command to start the user application")
Expand All @@ -315,11 +365,19 @@ func (t *Test) GetCmd() *cobra.Command {

testCmd.Flags().String("coverageReportPath", "", "Write a go coverage profile to the file in the given directory.")

testCmd.Flags().StringP("language", "l", "", "application programming language")

testCmd.Flags().Uint32("pid", 0, "Process id of your application.")

testCmd.Flags().Bool("enableTele", true, "Switch for telemetry")

testCmd.Flags().MarkHidden("enableTele")

testCmd.Flags().Bool("withCoverage", false, "Capture the code coverage of the go binary in the command flag.")
testCmd.Flags().Lookup("withCoverage").NoOptDefVal = "true"

testCmd.Flags().Bool("coverage", false, "Capture the code coverage of the go binary in the command flag.")
testCmd.Flags().Lookup("coverage").NoOptDefVal = "true"
testCmd.SilenceUsage = true
testCmd.SilenceErrors = true

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ require (
github.com/TheZeroSlave/zapsentry v1.18.0
github.com/agnivade/levenshtein v1.1.1
github.com/getsentry/sentry-go v0.17.0
github.com/google/uuid v1.5.0
github.com/hashicorp/go-memdb v1.3.4
github.com/jackc/pgproto3/v2 v2.3.2
github.com/vektah/gqlparser/v2 v2.5.8
github.com/xdg-go/pbkdf2 v1.0.0
Expand All @@ -84,10 +86,8 @@ require (

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.0 // indirect
github.com/hashicorp/go-memdb v1.3.4 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.3 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ github.com/hashicorp/go-immutable-radix v1.3.0 h1:8exGP7ego3OmkfksihtSouGMZ+hQrh
github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-memdb v1.3.4 h1:XSL3NR682X/cVk2IeV0d70N4DZ9ljI885xAEU8IoK3c=
github.com/hashicorp/go-memdb v1.3.4/go.mod h1:uBTr1oQbtuMgd1SSGoR8YV27eT3sBHbYiNm53bMpgSg=
github.com/hashicorp/go-uuid v1.0.0 h1:RS8zrF7PhGwyNPOtxSClXXj9HA8feRnJzgnI1RJCSnM=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
Expand Down

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

File renamed without changes.
File renamed without changes.
File renamed without changes.

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

Loading

0 comments on commit 013e6e2

Please sign in to comment.