Skip to content

Commit

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

This reverts commit 013e6e2.

Co-authored-by: Sarthak Shyngle <50234097+Sarthak160@users.noreply.github.com>
  • Loading branch information
charankamarapu and Sarthak160 committed Jan 9, 2024
1 parent 737d3ff commit 3b54e80
Show file tree
Hide file tree
Showing 19 changed files with 204 additions and 116 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 test -c "npm test" --delay 5 --coverage
sudo -E env PATH=$PATH ./../../keployv2 serve -c "npm test" --delay 5

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 test -c "npm test" --delay 5 --coverage
sudo -E env PATH=$PATH ./../../keployv2 serve -c "npm test" --delay 5

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), NewCmdExample(r.logger), NewCmdMockRecord(r.logger), NewCmdMockTest(r.logger), NewCmdGenerateConfig(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))

// add the registered keploy plugins as subcommands to the rootCmd
for _, sc := range r.subCommands {
Expand Down
144 changes: 144 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package cmd

import (
"os"
"path/filepath"

"github.com/spf13/cobra"
"go.keploy.io/server/pkg/service/serve"
"go.uber.org/zap"
)

func NewCmdServe(logger *zap.Logger) *Serve {
server := serve.NewServer(logger)
return &Serve{
server: server,
logger: logger,
}
}

type Serve struct {
server serve.Server
logger *zap.Logger
}

func (s *Serve) GetCmd() *cobra.Command {
var serveCmd = &cobra.Command{
Use: "serve",
Short: "run the keploy server to expose test apis",
Run: func(cmd *cobra.Command, args []string) {

path, err := cmd.Flags().GetString("path")
if err != nil {
s.logger.Error("failed to read the testcase path input")
return
}

//if user provides relative path
if len(path) > 0 && path[0] != '/' {
absPath, err := filepath.Abs(path)
if err != nil {
s.logger.Error("failed to get the absolute path from relative path", zap.Error(err))
return
}
path = absPath
} else if len(path) == 0 { // if user doesn't provide any path
cdirPath, err := os.Getwd()
if err != nil {
s.logger.Error("failed to get the path of current directory", zap.Error(err))
}
path = cdirPath
} else {
// user provided the absolute path
s.logger.Debug("", zap.Any("testPath", path))
}

path += "/keploy"

testReportPath := path + "/testReports"

s.logger.Info("", zap.Any("keploy test and mock path", path), zap.Any("keploy testReport path", testReportPath))

delay, err := cmd.Flags().GetUint64("delay")

if err != nil {
s.logger.Error("Failed to get the delay flag", zap.Error((err)))
return
}

pid, err := cmd.Flags().GetUint32("pid")

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

apiTimeout, err := cmd.Flags().GetUint64("apiTimeout")
if err != nil {
s.logger.Error("Failed to get the apiTimeout flag", zap.Error((err)))
}

port, err := cmd.Flags().GetUint32("port")

if err != nil {
s.logger.Error("Failed to get the port of keploy server", zap.Error((err)))
return
}
appCmd, err := cmd.Flags().GetString("command")

if err != nil {
s.logger.Error("Failed to get the command to run the user application", zap.Error((err)))
}
language, err := cmd.Flags().GetString("language")
if err != nil {
s.logger.Error("failed to read the programming language")
return
}
ports, err := cmd.Flags().GetUintSlice("passThroughPorts")
if err != nil {
s.logger.Error("failed to read the ports of outgoing calls to be ignored")
return
}

enableTele, err := cmd.Flags().GetBool("enableTele")
if err != nil {
s.logger.Error("failed to read the disable telemetry flag")
return
}

proxyPort, err := cmd.Flags().GetUint32("proxyport")
if err != nil {
s.logger.Error("failed to read the proxy port")
return
}
s.logger.Debug("the ports are", zap.Any("ports", ports))

s.server.Serve(path, proxyPort, testReportPath, delay, pid, port, language, ports, apiTimeout, appCmd, enableTele)
},
}

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

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

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

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

serveCmd.Flags().Uint64P("delay", "d", 5, "User provided time to run its application")
serveCmd.MarkFlagRequired("delay")

serveCmd.Flags().Uint64("apiTimeout", 5, "User provided timeout for calling its application")

serveCmd.Flags().UintSlice("passThroughPorts", []uint{}, "Ports of Outgoing dependency calls to be ignored as mocks")
serveCmd.Flags().Bool("enableTele", true, "Switch for telemetry")
serveCmd.Flags().MarkHidden("enableTele")

serveCmd.Flags().StringP("language", "l", "", "application programming language")
serveCmd.Flags().StringP("command", "c", "", "Command to start the user application")
serveCmd.MarkFlagRequired("command")

serveCmd.Hidden = true

return serveCmd
}
90 changes: 16 additions & 74 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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 @@ -25,7 +24,6 @@ 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 @@ -94,7 +92,6 @@ 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 @@ -145,36 +142,6 @@ 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 @@ -193,12 +160,6 @@ 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 @@ -300,48 +261,37 @@ 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))

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)
}

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 @@ -365,19 +315,11 @@ 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,8 +75,6 @@ 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 @@ -86,8 +84,10 @@ 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: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ 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
4 changes: 4 additions & 0 deletions pkg/service/serve/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Record Package Documentation

This package provides an interface to run a graphql server used in running keploy tests along with unit tests of the application. The interface methods will be
called from the `cmd` package.
File renamed without changes.

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 3b54e80

Please sign in to comment.